Skip to content

Handles variable-length nonce #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions aes-gcm/src/ctr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Counter mode implementation

use block_cipher_trait::generic_array::{
typenum::{Unsigned, U12, U16},
typenum::{Unsigned, U16},
ArrayLength, GenericArray,
};
use block_cipher_trait::BlockCipher;
Expand Down Expand Up @@ -35,10 +35,14 @@ where
B::ParBlocks: ArrayLength<GenericArray<u8, B::BlockSize>>,
{
/// Instantiate a new CTR instance
pub fn new(nonce: &GenericArray<u8, U12>) -> Self {
pub fn new(nonce: &[u8]) -> Self {
let mut counter_block = GenericArray::default();
counter_block[..12].copy_from_slice(nonce.as_slice());
counter_block[15] = 1;
if nonce.len() == 12 {
counter_block[..12].copy_from_slice(nonce);
counter_block[15] = 1;
} else {
counter_block[..].copy_from_slice(nonce);
}

Self {
block_cipher: PhantomData,
Expand Down
29 changes: 24 additions & 5 deletions aes-gcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,32 @@ where
B: BlockCipher<BlockSize = U16>,
B::ParBlocks: ArrayLength<GenericArray<u8, B::BlockSize>>,
{
type NonceSize = U12;
type TagSize = U16;
type CiphertextOverhead = U0;

fn encrypt_in_place_detached(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require a change in the Aead trait. See RustCrypto/traits#65

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I did that too

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah, apologies!

associated_data: &[u8],
buffer: &mut [u8],
) -> Result<Tag, Error> {
if buffer.len() as u64 > P_MAX || associated_data.len() as u64 > A_MAX {
return Err(Error);
}

// Handles variable-length nonce
let nonce = if nonce.len() != 12 {
let ghash = &mut self.ghash.clone();
ghash.update_padded(nonce);
let nonce = ghash.result_reset().into_bytes().to_vec();
nonce
} else {
nonce.to_vec()
};

// TODO(tarcieri): interleave encryption with GHASH
// See: <https://github.com/RustCrypto/AEADs/issues/74>
let mut ctr = Ctr32::new(nonce);
let mut ctr = Ctr32::new(nonce.as_ref());
ctr.seek(1);
ctr.apply_keystream(&self.cipher, buffer);

Expand All @@ -212,7 +221,7 @@ where

fn decrypt_in_place_detached(
&self,
nonce: &GenericArray<u8, Self::NonceSize>,
nonce: &[u8],
associated_data: &[u8],
buffer: &mut [u8],
tag: &Tag,
Expand All @@ -221,10 +230,20 @@ where
return Err(Error);
}

// Handles variable-length nonce
let nonce = if nonce.len() != 12 {
let ghash = &mut self.ghash.clone();
ghash.update_padded(nonce);
let nonce = ghash.result_reset().into_bytes().to_vec();
nonce
} else {
nonce.to_vec()
};

// TODO(tarcieri): interleave encryption with GHASH
// See: <https://github.com/RustCrypto/AEADs/issues/74>
let mut expected_tag = compute_tag(&mut self.ghash.clone(), associated_data, buffer);
let mut ctr = Ctr32::new(nonce);
let mut ctr = Ctr32::new(nonce.as_ref());
ctr.apply_keystream(&self.cipher, expected_tag.as_mut_slice());

use subtle::ConstantTimeEq;
Expand Down