diff --git a/block-cipher/src/lib.rs b/block-cipher/src/lib.rs index e7ea86e0d..caa9c2973 100644 --- a/block-cipher/src/lib.rs +++ b/block-cipher/src/lib.rs @@ -99,3 +99,32 @@ pub trait BlockCipher { } } } + +/// Stateful block cipher which permits `&mut self` access. +/// +/// The main use case for this trait is hardware encryption engines which +/// require `&mut self` access to an underlying hardware peripheral. +pub trait BlockCipherMut { + /// Size of the block in bytes + type BlockSize: ArrayLength; + + /// Encrypt block in-place + fn encrypt_block(&mut self, block: &mut GenericArray); + + /// Decrypt block in-place + fn decrypt_block(&mut self, block: &mut GenericArray); +} + +impl BlockCipherMut for Alg { + type BlockSize = Alg::BlockSize; + + /// Encrypt block in-place + fn encrypt_block(&mut self, block: &mut GenericArray) { + ::encrypt_block(self, block); + } + + /// Decrypt block in-place + fn decrypt_block(&mut self, block: &mut GenericArray) { + ::decrypt_block(self, block); + } +}