Skip to main content

chacha20/
variants.rs

1//! ChaCha variant-specific configurations.
2
3mod sealed {
4    pub trait Sealed {}
5}
6
7/// A trait that distinguishes some ChaCha variants. Contains configurations
8/// for "Legacy" DJB variant and the IETF variant.
9pub trait Variant: sealed::Sealed {
10    /// The counter's type.
11    #[cfg(not(feature = "cipher"))]
12    type Counter: Copy;
13
14    /// The counter's type.
15    #[cfg(feature = "cipher")]
16    type Counter: cipher::StreamCipherCounter;
17
18    /// Takes a slice of `state[12..NONCE_INDEX]` to convert it into
19    /// `Self::Counter`.
20    fn get_block_pos(row: &[u32]) -> Self::Counter;
21
22    /// Breaks down the `Self::Counter` type into a u32 array for setting the
23    /// block pos.
24    fn set_block_pos(row: &mut [u32], pos: Self::Counter);
25
26    /// A helper method for calculating the remaining blocks using these types
27    fn remaining_blocks(block_pos: Self::Counter) -> Option<usize>;
28}
29
30/// IETF ChaCha configuration to use a 32-bit counter and 96-bit nonce.
31#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for Ietf {
}
#[automatically_derived]impl ::core::clone::Clone for Ietf {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::Copy for Ietf { }Copy, #[automatically_derived]impl ::core::fmt::Debug for Ietf {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match *self {}
    }
}Debug)]
32pub enum Ietf {}
33
34impl sealed::Sealed for Ietf {}
35
36impl Variant for Ietf {
37    type Counter = u32;
38
39    #[inline(always)]
40    fn get_block_pos(row: &[u32]) -> u32 {
41        row[0]
42    }
43
44    #[inline(always)]
45    fn set_block_pos(row: &mut [u32], pos: u32) {
46        row[0] = pos;
47    }
48
49    #[inline(always)]
50    fn remaining_blocks(block_pos: u32) -> Option<usize> {
51        let remaining = u32::MAX - block_pos;
52        remaining.try_into().ok()
53    }
54}
55
56/// DJB variant specific features: 64-bit counter and 64-bit nonce.
57#[cfg(any(feature = "legacy", feature = "rng"))]
58#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for Legacy {
}
#[automatically_derived]impl ::core::clone::Clone for Legacy {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::Copy for Legacy { }Copy, #[automatically_derived]impl ::core::fmt::Debug for Legacy {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match *self {}
    }
}Debug)]
59pub enum Legacy {}
60
61#[cfg(any(feature = "legacy", feature = "rng"))]
62impl sealed::Sealed for Legacy {}
63
64#[cfg(any(feature = "legacy", feature = "rng"))]
65impl Variant for Legacy {
66    type Counter = u64;
67
68    #[inline(always)]
69    fn get_block_pos(row: &[u32]) -> u64 {
70        (u64::from(row[1]) << 32) | u64::from(row[0])
71    }
72
73    #[inline(always)]
74    fn set_block_pos(row: &mut [u32], pos: u64) {
75        row[0] = (pos & 0xFFFF_FFFF) as u32;
76        row[1] = (pos >> 32) as u32;
77    }
78
79    #[inline(always)]
80    fn remaining_blocks(block_pos: u64) -> Option<usize> {
81        let remaining = u64::MAX - block_pos;
82        remaining.try_into().ok()
83    }
84}