pub struct ChaCha20 { /* private fields */ }Expand description
ChaCha20 stream cipher state (RFC 8439).
Holds the initial state (key + nonce + counter) and the current
counter value. Buffers the leftover bytes from the previous
keystream block so partial calls to apply_keystream work
correctly.
Implementations§
Source§impl ChaCha20
impl ChaCha20
Sourcepub fn new(key: &[u8; 32], nonce: &[u8; 12], counter: u32) -> Self
pub fn new(key: &[u8; 32], nonce: &[u8; 12], counter: u32) -> Self
Initialise a ChaCha20 cipher with a 32-byte key, a 12-byte nonce, and an initial 32-bit block counter.
Per RFC 8439 §2.4 the AEAD construction starts the cipher at counter = 1 (counter = 0 produces the one-time Poly1305 key). Standalone ChaCha20 users typically start at counter = 0 or 1 – whatever their protocol specifies.
Sourcepub fn apply_keystream(&mut self, data: &mut [u8])
pub fn apply_keystream(&mut self, data: &mut [u8])
XOR the keystream into data in place. Encrypts or decrypts
indifferently (the cipher is symmetric).
Handles arbitrary lengths and partial blocks: the cipher
remembers leftover keystream bytes between calls, so
cipher.apply_keystream(b"hi"); cipher.apply_keystream(b"!")
produces the same output as one call with b"hi!".