Skip to content
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

aes-gcm: avoid exposing plaintext on tag verification failure #551

Merged
Merged
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
2 changes: 1 addition & 1 deletion aes-gcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ where
// TODO(tarcieri): interleave encryption with GHASH
// See: <https://github.com/RustCrypto/AEADs/issues/74>
let expected_tag = self.compute_tag(mask, associated_data, buffer);
ctr.apply_keystream_partial(buffer.into());

use subtle::ConstantTimeEq;
if expected_tag[..TagSize::to_usize()].ct_eq(tag).into() {
ctr.apply_keystream_partial(buffer.into());
Ok(())
} else {
Err(Error)
Expand Down
2 changes: 1 addition & 1 deletion aes-gcm/tests/aes128gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod common;

use self::common::TestVector;
use aes_gcm::aead::{generic_array::GenericArray, Aead, KeyInit, Payload};
use aes_gcm::aead::{generic_array::GenericArray, Aead, AeadInPlace, KeyInit, Payload};
use aes_gcm::Aes128Gcm;
use hex_literal::hex;

Expand Down
2 changes: 1 addition & 1 deletion aes-gcm/tests/aes256gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod common;

use self::common::TestVector;
use aes_gcm::aead::{generic_array::GenericArray, Aead, KeyInit, Payload};
use aes_gcm::aead::{generic_array::GenericArray, Aead, AeadInPlace, KeyInit, Payload};
use aes_gcm::Aes256Gcm;
use hex_literal::hex;

Expand Down
21 changes: 20 additions & 1 deletion aes-gcm/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,27 @@ macro_rules! tests {

let cipher = <$aead>::new(key);
assert!(cipher.decrypt(nonce, payload).is_err());
}

#[test]
fn decrypt_in_place_detached_modified() {
let vector = &$vectors.iter().last().unwrap();
let key = GenericArray::from_slice(vector.key);
let nonce = GenericArray::from_slice(vector.nonce);

let mut buffer = Vec::from(vector.ciphertext);
assert!(!buffer.is_empty());

// Tweak the first byte
let mut tag = GenericArray::clone_from_slice(vector.tag);
tag[0] ^= 0xaa;

let cipher = <$aead>::new(key);
assert!(cipher
.decrypt_in_place_detached(nonce, &[], &mut buffer, &tag)
.is_err());

// TODO(tarcieri): test ciphertext is unmodified in in-place API
assert_eq!(vector.ciphertext, buffer);
}
};
}