From e72b5441095e16754fa0ee899c5c1de0ee70b0dd Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 19 Mar 2022 11:28:28 -0600 Subject: [PATCH] ctr: use `BlockEncrypt` instead of `BlockEncryptMut` As far as I can tell this is just a mistake: the only state in CTR mode is the counter, and there is no reason to mandate some sort of stateful block cipher as part of the bounds. A `BlockCipherMut` bound is a significant impediment to upgrading the AEAD crates, which all impl `AeadInPlace` instead of `AeadMutInPlace`, store block cipher instances (which avoids re-expanding keys), and need to be able to share non-mutable references to those block cipher instances. --- ctr/src/ctr_core.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/ctr/src/ctr_core.rs b/ctr/src/ctr_core.rs index 7868f5b..a69f1a5 100644 --- a/ctr/src/ctr_core.rs +++ b/ctr/src/ctr_core.rs @@ -1,7 +1,7 @@ use crate::{backend::Closure, CtrFlavor}; use cipher::{ crypto_common::{InnerUser, IvSizeUser}, - AlgorithmName, BlockCipher, BlockEncryptMut, BlockSizeUser, InnerIvInit, Iv, IvState, + AlgorithmName, BlockCipher, BlockEncrypt, BlockSizeUser, InnerIvInit, Iv, IvState, StreamCipherCore, StreamCipherSeekCore, StreamClosure, }; use core::fmt; @@ -13,7 +13,7 @@ use cipher::zeroize::ZeroizeOnDrop; #[derive(Clone)] pub struct CtrCore where - C: BlockEncryptMut + BlockCipher, + C: BlockEncrypt + BlockCipher, F: CtrFlavor, { cipher: C, @@ -22,7 +22,7 @@ where impl BlockSizeUser for CtrCore where - C: BlockEncryptMut + BlockCipher, + C: BlockEncrypt + BlockCipher, F: CtrFlavor, { type BlockSize = C::BlockSize; @@ -30,7 +30,7 @@ where impl StreamCipherCore for CtrCore where - C: BlockEncryptMut + BlockCipher, + C: BlockEncrypt + BlockCipher, F: CtrFlavor, { #[inline] @@ -41,13 +41,13 @@ where #[inline] fn process_with_backend(&mut self, f: impl StreamClosure) { let Self { cipher, ctr_nonce } = self; - cipher.encrypt_with_backend_mut(Closure:: { ctr_nonce, f }); + cipher.encrypt_with_backend(Closure:: { ctr_nonce, f }); } } impl StreamCipherSeekCore for CtrCore where - C: BlockEncryptMut + BlockCipher, + C: BlockEncrypt + BlockCipher, F: CtrFlavor, { type Counter = F::Backend; @@ -65,7 +65,7 @@ where impl InnerUser for CtrCore where - C: BlockEncryptMut + BlockCipher, + C: BlockEncrypt + BlockCipher, F: CtrFlavor, { type Inner = C; @@ -73,7 +73,7 @@ where impl IvSizeUser for CtrCore where - C: BlockEncryptMut + BlockCipher, + C: BlockEncrypt + BlockCipher, F: CtrFlavor, { type IvSize = C::BlockSize; @@ -81,7 +81,7 @@ where impl InnerIvInit for CtrCore where - C: BlockEncryptMut + BlockCipher, + C: BlockEncrypt + BlockCipher, F: CtrFlavor, { #[inline] @@ -95,7 +95,7 @@ where impl IvState for CtrCore where - C: BlockEncryptMut + BlockCipher, + C: BlockEncrypt + BlockCipher, F: CtrFlavor, { #[inline] @@ -106,7 +106,7 @@ where impl AlgorithmName for CtrCore where - C: BlockEncryptMut + BlockCipher + AlgorithmName, + C: BlockEncrypt + BlockCipher + AlgorithmName, F: CtrFlavor, { fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -120,7 +120,7 @@ where impl fmt::Debug for CtrCore where - C: BlockEncryptMut + BlockCipher + AlgorithmName, + C: BlockEncrypt + BlockCipher + AlgorithmName, F: CtrFlavor, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -136,7 +136,7 @@ where #[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))] impl ZeroizeOnDrop for CtrCore where - C: BlockEncryptMut + BlockCipher + ZeroizeOnDrop, + C: BlockEncrypt + BlockCipher + ZeroizeOnDrop, F: CtrFlavor, F::CtrNonce: ZeroizeOnDrop, {