Skip to content

Commit b35ad22

Browse files
committed
aes_gcm: Add counter test for max input/output length.
For *ring* 0.17.12, both of these tests pass for default release mode, release mode with overflow-checks=true, and debug mode. For prior releases, `test_aes_gcm_counter_blocks_max_minus_one` passes in all three modes. For prior releases, `test_aes_gcm_counter_blocks_max` passes in default release mode: ``` $ cargo test --lib test_aes_gcm_counter_blocks --release [snip] test aead::aes::aes_gcm_tests::test_aes_gcm_counter_blocks_max ... ok test aead::aes::aes_gcm_tests::test_aes_gcm_counter_blocks_max_minus_one ... ok ``` But fails when overflow checks are enabled (including debug mode): ``` $ RUSTFLAGS="-C overflow-checks=true" cargo test --lib test_aes_gcm_counter_blocks [snip] test aead::aes::aes_gcm_tests::test_aes_gcm_counter_blocks_max_minus_one ... ok test aead::aes::aes_gcm_tests::test_aes_gcm_counter_blocks_max ... FAILED ```
1 parent 7b06be8 commit b35ad22

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/aead/aes.rs

+38
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,41 @@ mod tests {
236236
Key::new(key, cpu::features()).unwrap()
237237
}
238238
}
239+
240+
// These AES-GCM-specific tests are here instead of in `aes_gcm` because
241+
// `Counter`'s API isn't visible (enough) to aes_gcm.
242+
#[cfg(test)]
243+
mod aes_gcm_tests {
244+
use super::{super::aes_gcm::MAX_IN_OUT_LEN, *};
245+
use core::num::NonZeroU32;
246+
247+
#[test]
248+
fn test_aes_gcm_counter_blocks_max() {
249+
test_aes_gcm_counter_blocks(MAX_IN_OUT_LEN, &[0, 0, 0, 0]);
250+
}
251+
252+
#[test]
253+
fn test_aes_gcm_counter_blocks_max_minus_one() {
254+
test_aes_gcm_counter_blocks(MAX_IN_OUT_LEN - BLOCK_LEN, &[0xff, 0xff, 0xff, 0xff]);
255+
}
256+
257+
fn test_aes_gcm_counter_blocks(in_out_len: usize, expected_final_counter: &[u8; 4]) {
258+
fn ctr32(ctr: &Counter) -> &[u8; 4] {
259+
(&ctr.0[12..]).try_into().unwrap()
260+
}
261+
262+
assert_eq!(in_out_len % BLOCK_LEN, 0);
263+
let blocks = u32::try_from(in_out_len / BLOCK_LEN)
264+
.ok()
265+
.and_then(NonZeroU32::new)
266+
.unwrap();
267+
268+
let nonce = Nonce::assume_unique_for_key([1; 12]);
269+
let mut ctr = Counter::one(nonce);
270+
assert_eq!(ctr32(&ctr), &[0, 0, 0, 1]);
271+
let _tag_iv = ctr.increment();
272+
assert_eq!(ctr32(&ctr), &[0, 0, 0, 2]);
273+
ctr.increment_by_less_safe(blocks);
274+
assert_eq!(ctr32(&ctr), expected_final_counter);
275+
}
276+
}

0 commit comments

Comments
 (0)