Skip to main content

chacha20_keystream_block

Function chacha20_keystream_block 

Source
pub fn chacha20_keystream_block(
    key: &[u8],
    counter: u32,
    nonce: &[u8],
    out: &mut [u8],
) -> Result<(), CryptoError>
Expand description

Produce ChaCha20 keystream bytes for a given 32-byte key, 32-bit block counter, and 12-byte nonce (RFC 8439 / RFC 9001 §5.4.4).

The keystream is generated starting at block counter and XORed against an all-zero buffer, so out is filled with raw keystream. For QUIC header protection the caller passes counter taken from the first 4 bytes of the header-protection sample (little-endian), the nonce from the remaining 12 sample bytes, and a 5-byte out buffer to receive the mask.

out may be any length up to one ChaCha20 keystream block beyond the counter that does not overflow the 32-bit counter; for QUIC it is 5 bytes.

§Errors

Returns CryptoError::InvalidKey if key is not 32 bytes, CryptoError::InvalidNonce if nonce is not 12 bytes, and CryptoError::BadInput if out is empty.