forked from RustCrypto/stream-ciphers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
75 lines (63 loc) · 2.91 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
extern crate aes;
extern crate blobby;
extern crate ctr;
#[macro_use]
extern crate stream_cipher;
use aes::block_cipher_trait::generic_array::GenericArray;
use aes::block_cipher_trait::BlockCipher;
use stream_cipher::SyncStreamCipher;
type Aes128Ctr = ctr::Ctr128<aes::Aes128>;
type Aes256Ctr = ctr::Ctr128<aes::Aes256>;
new_sync_test!(aes128_ctr_core, Aes128Ctr, "aes128-ctr");
new_seek_test!(aes128_ctr_seek, Aes128Ctr, "aes128-ctr");
new_sync_test!(aes256_ctr_core, Aes256Ctr, "aes256-ctr");
new_seek_test!(aes256_ctr_seek, Aes256Ctr, "aes256-ctr");
#[test]
fn test_from_cipher() {
let data = include_bytes!("data/aes128-ctr.blb");
for row in blobby::Blob4Iterator::new(data).unwrap() {
let key = row[0];
let iv = GenericArray::from_slice(row[1]);
let plaintext = row[2];
let ciphertext = row[3];
let block_cipher = aes::Aes128::new_varkey(key).unwrap();
let mut cipher = ctr::Ctr128::from_cipher(block_cipher, iv);
let mut buf = plaintext.to_vec();
cipher.apply_keystream(&mut buf);
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[..]);
}