From 27f1530977aa77928d514fd10242a80864dc87c8 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 9 Jun 2020 11:36:22 -0700 Subject: [PATCH] aes-gcm: use more descriptive generic parameter names (#166) Renames the generic parameters: - `B` => `Aes` to reflect it should be instantiated with AES - `N` => `NonceSize` which is consistent with the associated type name --- aes-gcm/src/ctr.rs | 24 ++++++++--------- aes-gcm/src/lib.rs | 66 +++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/aes-gcm/src/ctr.rs b/aes-gcm/src/ctr.rs index 1e6f3ff0..f46da05c 100644 --- a/aes-gcm/src/ctr.rs +++ b/aes-gcm/src/ctr.rs @@ -14,16 +14,16 @@ type Block128 = GenericArray; pub(crate) const BLOCK_SIZE: usize = 16; /// CTR mode with a 32-bit big endian counter -pub(crate) struct Ctr32 +pub(crate) struct Ctr32 where - B: BlockCipher, - B::ParBlocks: ArrayLength>, + Aes: BlockCipher, + Aes::ParBlocks: ArrayLength>, { /// Block cipher - block_cipher: PhantomData, + block_cipher: PhantomData, /// Keystream buffer - buffer: GenericArray, + buffer: GenericArray, /// Current CTR value counter_block: Block128, @@ -32,10 +32,10 @@ where base_counter: u32, } -impl Ctr32 +impl Ctr32 where - B: BlockCipher, - B::ParBlocks: ArrayLength>, + Aes: BlockCipher, + Aes::ParBlocks: ArrayLength>, { /// Instantiate a new CTR instance pub fn new(j0: Block128) -> Self { @@ -61,17 +61,17 @@ where } /// Apply AES-CTR keystream to the given input buffer - pub fn apply_keystream(&mut self, block_cipher: &B, msg: &mut [u8]) { - for chunk in msg.chunks_mut(BLOCK_SIZE * B::ParBlocks::to_usize()) { + pub fn apply_keystream(&mut self, block_cipher: &Aes, msg: &mut [u8]) { + for chunk in msg.chunks_mut(BLOCK_SIZE * Aes::ParBlocks::to_usize()) { self.apply_keystream_blocks(block_cipher, chunk); } } /// Apply `B::ParBlocks` parallel blocks of keystream to the input buffer - fn apply_keystream_blocks(&mut self, block_cipher: &B, msg: &mut [u8]) { + fn apply_keystream_blocks(&mut self, block_cipher: &Aes, msg: &mut [u8]) { let mut counter = u32::from_be_bytes(self.counter_block[12..].try_into().unwrap()); let n_blocks = msg.chunks(BLOCK_SIZE).count(); - debug_assert!(n_blocks <= B::ParBlocks::to_usize()); + debug_assert!(n_blocks <= Aes::ParBlocks::to_usize()); for block in self.buffer.iter_mut().take(n_blocks) { *block = self.counter_block; diff --git a/aes-gcm/src/lib.rs b/aes-gcm/src/lib.rs index 297b26b8..82a5fa18 100644 --- a/aes-gcm/src/lib.rs +++ b/aes-gcm/src/lib.rs @@ -151,7 +151,7 @@ pub type Aes256Gcm = AesGcm; /// AES-GCM: generic over an underlying AES implementation and nonce size. /// -/// This type is generic to support substuting alternative AES implementations +/// This type is generic to support substituting alternative AES implementations /// (e.g. embedded hardware implementations) /// /// It is NOT intended to be instantiated with any block cipher besides AES! @@ -163,42 +163,42 @@ pub type Aes256Gcm = AesGcm; /// /// If in doubt, use the built-in [`Aes128Gcm`] and [`Aes256Gcm`] type aliases. #[derive(Clone)] -pub struct AesGcm +pub struct AesGcm where - B: BlockCipher, - B::ParBlocks: ArrayLength>, - N: ArrayLength, + Aes: BlockCipher, + Aes::ParBlocks: ArrayLength>, + NonceSize: ArrayLength, { /// Encryption cipher - cipher: B, + cipher: Aes, /// GHASH authenticator ghash: GHash, /// Length of the nonce - nonce_size: PhantomData, + nonce_size: PhantomData, } -impl NewAead for AesGcm +impl NewAead for AesGcm where - B: BlockCipher + NewBlockCipher, - B::ParBlocks: ArrayLength>, - N: ArrayLength, + Aes: BlockCipher + NewBlockCipher, + Aes::ParBlocks: ArrayLength>, + NonceSize: ArrayLength, { - type KeySize = B::KeySize; + type KeySize = Aes::KeySize; - fn new(key: &Key) -> Self { - B::new(key).into() + fn new(key: &Key) -> Self { + Aes::new(key).into() } } -impl From for AesGcm +impl From for AesGcm where - B: BlockCipher, - B::ParBlocks: ArrayLength>, - N: ArrayLength, + Aes: BlockCipher, + Aes::ParBlocks: ArrayLength>, + NonceSize: ArrayLength, { - fn from(cipher: B) -> Self { + fn from(cipher: Aes) -> Self { let mut ghash_key = GenericArray::default(); cipher.encrypt_block(&mut ghash_key); @@ -215,19 +215,19 @@ where } } -impl AeadInPlace for AesGcm +impl AeadInPlace for AesGcm where - B: BlockCipher, - B::ParBlocks: ArrayLength>, - N: ArrayLength, + Aes: BlockCipher, + Aes::ParBlocks: ArrayLength>, + NonceSize: ArrayLength, { - type NonceSize = N; + type NonceSize = NonceSize; type TagSize = U16; type CiphertextOverhead = U0; fn encrypt_in_place_detached( &self, - nonce: &GenericArray, + nonce: &GenericArray, associated_data: &[u8], buffer: &mut [u8], ) -> Result { @@ -250,7 +250,7 @@ where fn decrypt_in_place_detached( &self, - nonce: &GenericArray, + nonce: &GenericArray, associated_data: &[u8], buffer: &mut [u8], tag: &Tag, @@ -275,11 +275,11 @@ where } } -impl AesGcm +impl AesGcm where - B: BlockCipher, - B::ParBlocks: ArrayLength>, - N: ArrayLength, + Aes: BlockCipher, + Aes::ParBlocks: ArrayLength>, + NonceSize: ArrayLength, { /// Initialize counter mode. /// @@ -290,8 +290,8 @@ where /// > If len(IV)=96, then J0 = IV || 0{31} || 1. /// > If len(IV) ≠ 96, then let s = 128 ⎡len(IV)/128⎤-len(IV), and /// > J0=GHASH(IV||0s+64||[len(IV)]64). - fn init_ctr(&self, nonce: &GenericArray) -> Ctr32 { - let j0 = if N::to_usize() == 12 { + fn init_ctr(&self, nonce: &GenericArray) -> Ctr32 { + let j0 = if NonceSize::to_usize() == 12 { let mut block = GenericArray::default(); block[..12].copy_from_slice(nonce); block[15] = 1; @@ -301,7 +301,7 @@ where ghash.update_padded(nonce); let mut block = GenericArray::default(); - let nonce_bits = (N::to_usize() as u64) * 8; + let nonce_bits = (NonceSize::to_usize() as u64) * 8; block[8..].copy_from_slice(&nonce_bits.to_be_bytes()); ghash.update(&block);