diff --git a/ctr/tests/mod.rs b/ctr/tests/mod.rs index 24c3d7ee..b2b3f0b2 100644 --- a/ctr/tests/mod.rs +++ b/ctr/tests/mod.rs @@ -33,3 +33,43 @@ fn test_from_cipher() { assert_eq!(&buf, &ciphertext); } } + +#[test] +fn compare_to_openssl_with_over_64bit_counter() { + use stream_cipher::{NewStreamCipher, SyncStreamCipher, SyncStreamCipherSeek}; + // values from https://github.com/RustCrypto/stream-ciphers/issues/12 poc + + let key = [ + 13, 193, 67, 14, 105, 84, 246, 135, 216, 216, 40, 251, 26, 84, 119, 223, + ]; + let nonce = [ + 26, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + ]; + let data = [ + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 7, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 202, 124, 216, 0, + ]; + let openssl = [ + 108, 253, 73, 159, 41, 43, 94, 79, 15, 121, 128, 186, 135, 246, 194, 87, 27, 222, 233, 216, + 2, 74, 106, 79, 70, 239, 105, 93, 125, 169, 59, 243, 171, 225, 15, 165, 102, 87, 79, 1, 31, + 125, 151, 72, 199, 184, 71, 14, 69, 200, 13, 5, 171, 26, 106, 86, 129, 55, 254, 219, 166, + 51, 34, 105, 154, 166, 12, 108, 239, 100, 153, 125, 229, 136, 86, 30, 233, 149, 169, 77, + 154, 25, 226, 107, 205, 53, 144, 233, 62, 225, 237, 218, 7, 246, 61, 146, 31, 189, 212, + 178, 104, 88, + ]; + + let mut cipher = Aes128Ctr::new_var(&key, &nonce).unwrap(); + let mut encrypted = data.to_vec(); + cipher.apply_keystream(&mut encrypted); + + assert_eq!(&encrypted[..], &openssl[..]); + + cipher.seek(0); + cipher.apply_keystream(&mut encrypted[..]); + + assert_eq!(&encrypted[..], &data[..]); +}