chacha20_poly1305: drop mutable requirement

* The get_keystream method exposes the keystream for a block for
special case scenarios. Generally the cipher state should only be
updated with teh apply_keystream method.
This commit is contained in:
Nick Johnson 2025-02-19 12:12:28 -08:00
parent 415945cd2b
commit 30920c4d84
No known key found for this signature in database
GPG Key ID: 97B34267D0DBC8BF
2 changed files with 3 additions and 6 deletions

View File

@ -328,11 +328,8 @@ impl ChaCha20 {
} }
} }
/// Get the keystream block at a specified block. /// Get the keystream for specified block.
pub fn get_keystream(&mut self, block: u32) -> [u8; 64] { pub fn get_keystream(&self, block: u32) -> [u8; 64] { self.keystream_at_block(block) }
self.block(block);
self.keystream_at_block(self.block_count)
}
/// Update the index of the keystream to the given byte. /// Update the index of the keystream to the given byte.
pub fn seek(&mut self, seek: u32) { pub fn seek(&mut self, seek: u32) {

View File

@ -118,7 +118,7 @@ impl ChaCha20Poly1305 {
tag: [u8; 16], tag: [u8; 16],
aad: Option<&[u8]>, aad: Option<&[u8]>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut chacha = ChaCha20::new_from_block(self.key, self.nonce, 0); let chacha = ChaCha20::new_from_block(self.key, self.nonce, 0);
let keystream = chacha.get_keystream(0); let keystream = chacha.get_keystream(0);
let mut poly = let mut poly =
Poly1305::new(keystream[..32].try_into().expect("slicing produces 32-byte slice")); Poly1305::new(keystream[..32].try_into().expect("slicing produces 32-byte slice"));