pub struct ChaCha<const ROUNDS: usize, const IS_IETF: bool> { /* private fields */ }Expand description
ChaCha stream cipher.
IS_IETF selects the nonce/counter layout:
false(DJB original): 64-bit counter at words 12–13, 64-bit nonce at words 14–15.true(IETF / RFC 8439): 32-bit counter at word 12, 96-bit nonce at words 13–15.
The IETF layout can only encrypt up to CHACHA20_IETF_MAX_LEN bytes per (key, nonce) pair;
xor_keystream panics instead of wrapping the counter.
Implementations§
Source§impl<const ROUNDS: usize> ChaCha<ROUNDS, false>
impl<const ROUNDS: usize> ChaCha<ROUNDS, false>
Sourcepub fn new(key: &[u8; 32], nonce: &[u8; 8]) -> ChaCha<ROUNDS, false>
pub fn new(key: &[u8; 32], nonce: &[u8; 8]) -> ChaCha<ROUNDS, false>
Create a new ChaCha instance with the DJB nonce layout (8-byte nonce, 64-bit counter).
Sourcepub fn set_counter(&mut self, counter: u64)
pub fn set_counter(&mut self, counter: u64)
Set the ChaCha counter (words 12 and 13). It can be used to move forward and backward in the keystream.
Source§impl<const ROUNDS: usize> ChaCha<ROUNDS, true>
impl<const ROUNDS: usize> ChaCha<ROUNDS, true>
Sourcepub fn new(key: &[u8; 32], nonce: &[u8; 12]) -> ChaCha<ROUNDS, true>
pub fn new(key: &[u8; 32], nonce: &[u8; 12]) -> ChaCha<ROUNDS, true>
Create a new ChaCha instance with the IETF (RFC 8439) nonce layout (12-byte nonce, 32-bit counter).
Sourcepub fn set_counter(&mut self, counter: u32)
pub fn set_counter(&mut self, counter: u32)
Set the ChaCha counter (word 12). The counter is a u32. It can be used to move forward and backward in the keystream.
The (key, nonce, counter) triple must never be reused across distinct messages. Setting
the counter near u32::MAX leaves little room before the counter space is exhausted;
streaming past it will panic rather than reuse keystream (see CHACHA20_IETF_MAX_LEN).
Trait Implementations§
Source§impl<const ROUNDS: usize, const IS_IETF: bool> StreamCipher for ChaCha<ROUNDS, IS_IETF>
impl<const ROUNDS: usize, const IS_IETF: bool> StreamCipher for ChaCha<ROUNDS, IS_IETF>
Source§fn xor_keystream(&mut self, in_out: &mut [u8])
fn xor_keystream(&mut self, in_out: &mut [u8])
XOR plaintext with the ChaCha keystream.
§Panics
For the IETF layout, panics if this call would advance the 32-bit block counter past
u32::MAX and silently reuse keystream. This happens when encrypting more than
CHACHA20_IETF_MAX_LEN bytes under one key/nonce, or after
set_counter jumps near the end of the counter space.