Skip to content

Commit

Permalink
aes-gcm: use more descriptive generic parameter names (#166)
Browse files Browse the repository at this point in the history
Renames the generic parameters:

- `B` => `Aes` to reflect it should be instantiated with AES
- `N` => `NonceSize` which is consistent with the associated type name
  • Loading branch information
tarcieri committed Jun 9, 2020
1 parent 9c5540b commit 27f1530
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
24 changes: 12 additions & 12 deletions aes-gcm/src/ctr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ type Block128 = GenericArray<u8, U16>;
pub(crate) const BLOCK_SIZE: usize = 16;

/// CTR mode with a 32-bit big endian counter
pub(crate) struct Ctr32<B>
pub(crate) struct Ctr32<Aes>
where
B: BlockCipher<BlockSize = U16>,
B::ParBlocks: ArrayLength<GenericArray<u8, B::BlockSize>>,
Aes: BlockCipher<BlockSize = U16>,
Aes::ParBlocks: ArrayLength<GenericArray<u8, Aes::BlockSize>>,
{
/// Block cipher
block_cipher: PhantomData<B>,
block_cipher: PhantomData<Aes>,

/// Keystream buffer
buffer: GenericArray<Block128, B::ParBlocks>,
buffer: GenericArray<Block128, Aes::ParBlocks>,

/// Current CTR value
counter_block: Block128,
Expand All @@ -32,10 +32,10 @@ where
base_counter: u32,
}

impl<B> Ctr32<B>
impl<Aes> Ctr32<Aes>
where
B: BlockCipher<BlockSize = U16>,
B::ParBlocks: ArrayLength<GenericArray<u8, B::BlockSize>>,
Aes: BlockCipher<BlockSize = U16>,
Aes::ParBlocks: ArrayLength<GenericArray<u8, Aes::BlockSize>>,
{
/// Instantiate a new CTR instance
pub fn new(j0: Block128) -> Self {
Expand All @@ -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;
Expand Down
66 changes: 33 additions & 33 deletions aes-gcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub type Aes256Gcm = AesGcm<Aes256, U12>;

/// 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!
Expand All @@ -163,42 +163,42 @@ pub type Aes256Gcm = AesGcm<Aes256, U12>;
///
/// If in doubt, use the built-in [`Aes128Gcm`] and [`Aes256Gcm`] type aliases.
#[derive(Clone)]
pub struct AesGcm<B, N>
pub struct AesGcm<Aes, NonceSize>
where
B: BlockCipher<BlockSize = U16>,
B::ParBlocks: ArrayLength<Block<B>>,
N: ArrayLength<u8>,
Aes: BlockCipher<BlockSize = U16>,
Aes::ParBlocks: ArrayLength<Block<Aes>>,
NonceSize: ArrayLength<u8>,
{
/// Encryption cipher
cipher: B,
cipher: Aes,

/// GHASH authenticator
ghash: GHash,

/// Length of the nonce
nonce_size: PhantomData<N>,
nonce_size: PhantomData<NonceSize>,
}

impl<B, N> NewAead for AesGcm<B, N>
impl<Aes, NonceSize> NewAead for AesGcm<Aes, NonceSize>
where
B: BlockCipher<BlockSize = U16> + NewBlockCipher,
B::ParBlocks: ArrayLength<Block<B>>,
N: ArrayLength<u8>,
Aes: BlockCipher<BlockSize = U16> + NewBlockCipher,
Aes::ParBlocks: ArrayLength<Block<Aes>>,
NonceSize: ArrayLength<u8>,
{
type KeySize = B::KeySize;
type KeySize = Aes::KeySize;

fn new(key: &Key<B>) -> Self {
B::new(key).into()
fn new(key: &Key<Aes>) -> Self {
Aes::new(key).into()
}
}

impl<B, N> From<B> for AesGcm<B, N>
impl<Aes, NonceSize> From<Aes> for AesGcm<Aes, NonceSize>
where
B: BlockCipher<BlockSize = U16>,
B::ParBlocks: ArrayLength<Block<B>>,
N: ArrayLength<u8>,
Aes: BlockCipher<BlockSize = U16>,
Aes::ParBlocks: ArrayLength<Block<Aes>>,
NonceSize: ArrayLength<u8>,
{
fn from(cipher: B) -> Self {
fn from(cipher: Aes) -> Self {
let mut ghash_key = GenericArray::default();
cipher.encrypt_block(&mut ghash_key);

Expand All @@ -215,19 +215,19 @@ where
}
}

impl<B, N> AeadInPlace for AesGcm<B, N>
impl<Aes, NonceSize> AeadInPlace for AesGcm<Aes, NonceSize>
where
B: BlockCipher<BlockSize = U16>,
B::ParBlocks: ArrayLength<Block<B>>,
N: ArrayLength<u8>,
Aes: BlockCipher<BlockSize = U16>,
Aes::ParBlocks: ArrayLength<Block<Aes>>,
NonceSize: ArrayLength<u8>,
{
type NonceSize = N;
type NonceSize = NonceSize;
type TagSize = U16;
type CiphertextOverhead = U0;

fn encrypt_in_place_detached(
&self,
nonce: &GenericArray<u8, N>,
nonce: &GenericArray<u8, NonceSize>,
associated_data: &[u8],
buffer: &mut [u8],
) -> Result<Tag, Error> {
Expand All @@ -250,7 +250,7 @@ where

fn decrypt_in_place_detached(
&self,
nonce: &GenericArray<u8, N>,
nonce: &GenericArray<u8, NonceSize>,
associated_data: &[u8],
buffer: &mut [u8],
tag: &Tag,
Expand All @@ -275,11 +275,11 @@ where
}
}

impl<B, N> AesGcm<B, N>
impl<Aes, NonceSize> AesGcm<Aes, NonceSize>
where
B: BlockCipher<BlockSize = U16>,
B::ParBlocks: ArrayLength<Block<B>>,
N: ArrayLength<u8>,
Aes: BlockCipher<BlockSize = U16>,
Aes::ParBlocks: ArrayLength<Block<Aes>>,
NonceSize: ArrayLength<u8>,
{
/// Initialize counter mode.
///
Expand All @@ -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<u8, N>) -> Ctr32<B> {
let j0 = if N::to_usize() == 12 {
fn init_ctr(&self, nonce: &GenericArray<u8, NonceSize>) -> Ctr32<Aes> {
let j0 = if NonceSize::to_usize() == 12 {
let mut block = GenericArray::default();
block[..12].copy_from_slice(nonce);
block[15] = 1;
Expand All @@ -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);

Expand Down

0 comments on commit 27f1530

Please sign in to comment.