diff --git a/Cargo.lock b/Cargo.lock index 76778c83b..dd3e6aa61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,7 +85,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cipher" -version = "0.2.5" +version = "0.3.0-pre" dependencies = [ "blobby 0.3.0", "generic-array 0.14.4", @@ -118,7 +118,7 @@ dependencies = [ [[package]] name = "crypto-mac" -version = "0.10.0" +version = "0.11.0-pre" dependencies = [ "blobby 0.3.0", "cipher", diff --git a/cipher/Cargo.toml b/cipher/Cargo.toml index 6a1fde75a..9c37e7aa6 100644 --- a/cipher/Cargo.toml +++ b/cipher/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cipher" description = "Traits for describing block ciphers and stream ciphers" -version = "0.2.5" +version = "0.3.0-pre" authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" readme = "README.md" diff --git a/cipher/src/block.rs b/cipher/src/block.rs index 551e9d14a..212642488 100644 --- a/cipher/src/block.rs +++ b/cipher/src/block.rs @@ -53,8 +53,7 @@ pub trait NewBlockCipher: Sized { } } -/// The trait which defines in-place encryption and decryption -/// over single block or several blocks in parallel. +/// Trait which marks a type as being a block cipher. pub trait BlockCipher { /// Size of the block in bytes type BlockSize: ArrayLength; @@ -62,19 +61,19 @@ pub trait BlockCipher { /// Number of blocks which can be processed in parallel by /// cipher implementation type ParBlocks: ArrayLength>; +} +/// Encrypt-only functionality for block ciphers +pub trait BlockEncrypt: BlockCipher { /// Encrypt block in-place fn encrypt_block(&self, block: &mut Block); - /// Decrypt block in-place - fn decrypt_block(&self, block: &mut Block); - /// Encrypt several blocks in parallel using instruction level parallelism /// if possible. /// /// If `ParBlocks` equals to 1 it's equivalent to `encrypt_block`. #[inline] - fn encrypt_blocks(&self, blocks: &mut ParBlocks) { + fn encrypt_par_blocks(&self, blocks: &mut ParBlocks) { for block in blocks.iter_mut() { self.encrypt_block(block); } @@ -82,14 +81,14 @@ pub trait BlockCipher { /// Encrypt a slice of blocks, leveraging parallelism when available. #[inline] - fn encrypt_slice(&self, mut blocks: &mut [Block]) { + fn encrypt_blocks(&self, mut blocks: &mut [Block]) { let pb = Self::ParBlocks::to_usize(); if pb > 1 { let mut iter = blocks.chunks_exact_mut(pb); for chunk in &mut iter { - self.encrypt_blocks(chunk.try_into().unwrap()) + self.encrypt_par_blocks(chunk.try_into().unwrap()) } blocks = iter.into_remainder(); @@ -99,13 +98,19 @@ pub trait BlockCipher { self.encrypt_block(block); } } +} + +/// Decrypt-only functionality for block ciphers +pub trait BlockDecrypt: BlockCipher { + /// Decrypt block in-place + fn decrypt_block(&self, block: &mut Block); /// Decrypt several blocks in parallel using instruction level parallelism /// if possible. /// /// If `ParBlocks` equals to 1 it's equivalent to `decrypt_block`. #[inline] - fn decrypt_blocks(&self, blocks: &mut ParBlocks) { + fn decrypt_par_blocks(&self, blocks: &mut ParBlocks) { for block in blocks.iter_mut() { self.decrypt_block(block); } @@ -113,14 +118,14 @@ pub trait BlockCipher { /// Decrypt a slice of blocks, leveraging parallelism when available. #[inline] - fn decrypt_slice(&self, mut blocks: &mut [Block]) { + fn decrypt_blocks(&self, mut blocks: &mut [Block]) { let pb = Self::ParBlocks::to_usize(); if pb > 1 { let mut iter = blocks.chunks_exact_mut(pb); for chunk in &mut iter { - self.decrypt_blocks(chunk.try_into().unwrap()) + self.decrypt_par_blocks(chunk.try_into().unwrap()) } blocks = iter.into_remainder(); @@ -132,56 +137,32 @@ pub trait BlockCipher { } } -/// Stateful block cipher which permits `&mut self` access. +/// Encrypt-only functionality for block ciphers with mutable access to `self`. /// /// 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; - +pub trait BlockEncryptMut: BlockCipher { /// Encrypt block in-place - fn encrypt_block(&mut self, block: &mut GenericArray); + fn encrypt_block_mut(&mut self, block: &mut Block); +} +/// Decrypt-only functionality for block ciphers with mutable access to `self`. +/// +/// The main use case for this trait is hardware encryption engines which +/// require `&mut self` access to an underlying hardware peripheral. +pub trait BlockDecryptMut: BlockCipher { /// Decrypt block in-place - fn decrypt_block(&mut self, block: &mut GenericArray); + fn decrypt_block_mut(&mut self, block: &mut Block); } -impl BlockCipherMut for Alg { - type BlockSize = Alg::BlockSize; - - #[inline] - fn encrypt_block(&mut self, block: &mut GenericArray) { - ::encrypt_block(self, block); - } - - #[inline] - fn decrypt_block(&mut self, block: &mut GenericArray) { - ::decrypt_block(self, block); +impl BlockEncryptMut for Alg { + fn encrypt_block_mut(&mut self, block: &mut Block) { + self.encrypt_block(block); } } -impl BlockCipher for &Alg { - type BlockSize = Alg::BlockSize; - type ParBlocks = Alg::ParBlocks; - - #[inline] - fn encrypt_block(&self, block: &mut Block) { - Alg::encrypt_block(self, block); - } - - #[inline] - fn decrypt_block(&self, block: &mut Block) { - Alg::decrypt_block(self, block); - } - - #[inline] - fn encrypt_blocks(&self, blocks: &mut ParBlocks) { - Alg::encrypt_blocks(self, blocks); - } - - #[inline] - fn decrypt_blocks(&self, blocks: &mut ParBlocks) { - Alg::decrypt_blocks(self, blocks); +impl BlockDecryptMut for Alg { + fn decrypt_block_mut(&mut self, block: &mut Block) { + self.decrypt_block(block); } } diff --git a/cipher/src/lib.rs b/cipher/src/lib.rs index 9440c5783..d2cb92938 100644 --- a/cipher/src/lib.rs +++ b/cipher/src/lib.rs @@ -20,7 +20,9 @@ pub mod block; pub mod stream; pub use crate::{ - block::{BlockCipher, BlockCipherMut, NewBlockCipher}, + block::{ + BlockCipher, BlockDecrypt, BlockDecryptMut, BlockEncrypt, BlockEncryptMut, NewBlockCipher, + }, stream::{NewStreamCipher, StreamCipher, SyncStreamCipher, SyncStreamCipherSeek}, }; pub use generic_array::{self, typenum::consts}; diff --git a/cipher/src/stream.rs b/cipher/src/stream.rs index ebc022c5c..ccf29fca2 100644 --- a/cipher/src/stream.rs +++ b/cipher/src/stream.rs @@ -16,7 +16,7 @@ pub use generic_array::{self, typenum::consts}; #[cfg(feature = "dev")] pub use blobby; -use crate::block::{BlockCipher, BlockCipherMut, NewBlockCipher}; +use crate::block::{BlockCipher, NewBlockCipher}; use core::convert::{TryFrom, TryInto}; use generic_array::typenum::Unsigned; use generic_array::{ArrayLength, GenericArray}; @@ -166,46 +166,17 @@ pub trait FromBlockCipher { ) -> Self; } -/// Trait for initializing a stream cipher from a mutable block cipher -pub trait FromBlockCipherMut { - /// Block cipher - type BlockCipher: BlockCipherMut; - /// Nonce size in bytes - type NonceSize: ArrayLength; - - /// Instantiate a stream cipher from a block cipher - fn from_block_cipher_mut( - cipher: Self::BlockCipher, - nonce: &GenericArray, - ) -> Self; -} - -impl FromBlockCipherMut for C -where - C: FromBlockCipher, -{ - type BlockCipher = ::BlockCipher; - type NonceSize = ::NonceSize; - - fn from_block_cipher_mut( - cipher: Self::BlockCipher, - nonce: &GenericArray, - ) -> C { - C::from_block_cipher(cipher, nonce) - } -} - impl NewStreamCipher for C where - C: FromBlockCipherMut, + C: FromBlockCipher, C::BlockCipher: NewBlockCipher, { - type KeySize = <::BlockCipher as NewBlockCipher>::KeySize; - type NonceSize = ::NonceSize; + type KeySize = <::BlockCipher as NewBlockCipher>::KeySize; + type NonceSize = ::NonceSize; fn new(key: &Key, nonce: &Nonce) -> C { - C::from_block_cipher_mut( - <::BlockCipher as NewBlockCipher>::new(key), + C::from_block_cipher( + <::BlockCipher as NewBlockCipher>::new(key), nonce, ) } @@ -218,7 +189,7 @@ where .map_err(|_| InvalidKeyNonceLength) .map(|cipher| { let nonce = GenericArray::from_slice(nonce); - Self::from_block_cipher_mut(cipher, nonce) + Self::from_block_cipher(cipher, nonce) }) } } diff --git a/crypto-mac/Cargo.toml b/crypto-mac/Cargo.toml index c2a7121d8..7be37f044 100644 --- a/crypto-mac/Cargo.toml +++ b/crypto-mac/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "crypto-mac" description = "Trait for Message Authentication Code (MAC) algorithms" -version = "0.10.0" +version = "0.11.0-pre" authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" readme = "README.md" @@ -13,7 +13,7 @@ categories = ["cryptography", "no-std"] [dependencies] generic-array = "0.14" -cipher = { version = "0.2", optional = true, path = "../cipher" } +cipher = { version = "=0.3.0-pre", optional = true, path = "../cipher" } subtle = { version = "2", default-features = false } blobby = { version = "0.3", optional = true } diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml index a33603937..b11faf574 100644 --- a/crypto/Cargo.toml +++ b/crypto/Cargo.toml @@ -13,10 +13,10 @@ edition = "2018" [dependencies] aead = { version = "0.3", optional = true, path = "../aead" } -cipher = { version = "0.2", optional = true, path = "../cipher" } +cipher = { version = "=0.3.0-pre", optional = true, path = "../cipher" } digest = { version = "0.9", optional = true, path = "../digest" } elliptic-curve = { version = "=0.7.0-pre", optional = true, path = "../elliptic-curve" } -mac = { version = "0.10", package = "crypto-mac", optional = true, path = "../crypto-mac" } +mac = { version = "=0.11.0-pre", package = "crypto-mac", optional = true, path = "../crypto-mac" } signature = { version = "1.2.0", optional = true, default-features = false, path = "../signature" } universal-hash = { version = "0.4", optional = true, path = "../universal-hash" }