Skip to content

Commit 4641aa8

Browse files
committed
test: extract more aes-ctr test cases
1 parent d6db2b5 commit 4641aa8

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

aes-ctr/src/lib.rs

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,10 @@ use aesni as aes;
7676
pub use crate::aes::{Aes128Ctr, Aes192Ctr, Aes256Ctr};
7777

7878
#[test]
79-
fn compare_to_openssl_with_over_64bit_nonce_and_counter() {
80-
use cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek};
81-
use core::fmt;
79+
fn compare_to_openssl_with_poc_values() {
8280
use hex_literal::hex;
8381
// values from https://github.com/RustCrypto/stream-ciphers/issues/12 poc
8482

85-
#[derive(PartialEq, Eq)]
86-
struct HexOnly<'a>(&'a [u8]);
87-
88-
impl<'a> fmt::Debug for HexOnly<'a> {
89-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
90-
self.0.iter().try_for_each(|b| write!(fmt, "{:02x}", b))
91-
}
92-
}
93-
9483
let key = hex!("0dc1 430e 6954 f687 d8d8 28fb 1a54 77df");
9584
let nonce = hex!("1aff ffff ffff ffff ffff ffff ffff ffff");
9685
let data = hex!(
@@ -113,14 +102,79 @@ fn compare_to_openssl_with_over_64bit_nonce_and_counter() {
113102
1fbd d4b2 6858"
114103
);
115104

105+
compare_scenario(&key, &data, &nonce, &openssl);
106+
}
107+
108+
#[test]
109+
fn compare_to_openssl_at_zero_nonce() {
110+
use hex_literal::hex;
111+
112+
let nonce = [0; 16];
113+
let expected = hex!("66e94bd4ef8a2c3b884cfa59ca342b2e58e2fccefa7e3061367f1d57a4e7455a0388dace60b6a392f328c2b971b2fe78f795aaab494b5923f7fd89ff948bc1e0");
114+
115+
// this should match the behaviour before the #75
116+
compare_scenario(&[0; 16], &[0; 4 * 16], &nonce, &expected);
117+
}
118+
119+
#[test]
120+
fn compare_to_openssl_near_64bit_le() {
121+
use hex_literal::hex;
122+
123+
let nonce = (u64::MAX as u128 - 1).to_le_bytes();
124+
let expected = hex!("0fc33b45e52ac8f00392805984e573c6e2a4ba8f764fa3fbe8b6e6e3cda6ecfff7ffe7a8bc8c8214384903c72e2d54fd20c10ba6f72ff0734fc4e545b7b1e585");
125+
126+
// this shouldn't wrap around as the nonce is treated as big endian input; this should match
127+
// behaviour before the #75
128+
compare_scenario(&[0; 16], &[0; 4 * 16], &nonce, &expected);
129+
}
130+
131+
#[test]
132+
fn compare_to_openssl_near_64bit_be() {
133+
use hex_literal::hex;
134+
135+
let nonce = (u64::MAX as u128 - 1).to_be_bytes();
136+
let expected = hex!("99c5f4ae0531eece7c33dab98d5e289d747cb9267e59fa9e4e615668db0909bc788bcd111ecf73d4e78d2e21bef55460daacdaf76b0cffc0fa1498a35ebe1dfc");
137+
138+
// changed in #75 as previously counter was 64-bit and only half of nonce was affected by it
139+
// wrapping around.
140+
compare_scenario(&[0; 16], &[0; 4 * 16], &nonce, &expected);
141+
}
142+
143+
#[test]
144+
fn compare_to_openssl_near_128bit_be() {
145+
use hex_literal::hex;
146+
147+
let nonce = (u128::MAX as u128 - 1).to_be_bytes();
148+
let expected = hex!("5c005e72c1418c44f569f2ea33ba54f33f5b8cc9ea855a0afa7347d23e8d664e66e94bd4ef8a2c3b884cfa59ca342b2e58e2fccefa7e3061367f1d57a4e7455a");
149+
150+
// changed in #75 for same reason as `compare_to_openssl_near_64bit_be`.
151+
compare_scenario(&[0; 16], &[0; 4 * 16], &nonce, &expected);
152+
}
153+
154+
/// Run aes-ctr against openssl generated next four blocks from the nonce.
155+
#[cfg(test)]
156+
fn compare_scenario(key: &[u8], data: &[u8], nonce: &[u8], expected: &[u8]) {
157+
use cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek};
158+
use core::fmt;
159+
160+
#[derive(PartialEq, Eq)]
161+
struct HexOnly<'a>(&'a [u8]);
162+
163+
impl<'a> fmt::Debug for HexOnly<'a> {
164+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
165+
self.0.iter().try_for_each(|b| write!(fmt, "{:02x}", b))
166+
}
167+
}
168+
169+
assert_eq!(expected.len(), data.len());
170+
116171
let mut cipher = Aes128Ctr::new_var(&key, &nonce).unwrap();
117-
let mut encrypted = data.to_vec();
118-
cipher.apply_keystream(&mut encrypted);
119172

120-
assert_eq!(HexOnly(&encrypted[..]), HexOnly(&openssl[..]));
173+
let mut encrypted = data.to_vec();
174+
cipher.apply_keystream(&mut encrypted[..]);
175+
assert_eq!(&encrypted[..], &expected[..]);
121176

122177
cipher.seek(0);
123178
cipher.apply_keystream(&mut encrypted[..]);
124-
125179
assert_eq!(&encrypted[..], &data[..]);
126180
}

0 commit comments

Comments
 (0)