-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathasync_stream.rs
More file actions
91 lines (78 loc) · 2.49 KB
/
async_stream.rs
File metadata and controls
91 lines (78 loc) · 2.49 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Tests for inherent methods
use aes::*;
use cfb_mode::{BufDecryptor, BufEncryptor, Decryptor, Encryptor};
use cipher::{KeyInit, KeyIvInit};
#[test]
fn aes128_cfb_async_test() {
type Enc = Encryptor<Aes128>;
type Dec = Decryptor<Aes128>;
let key = [42; 16];
let iv = [24; 16];
let mut pt = [0u8; 101];
for (i, b) in pt.iter_mut().enumerate() {
*b = u8::try_from(i % 11).unwrap();
}
let enc = Enc::new_from_slices(&key, &iv).unwrap();
let mut ct = pt;
enc.encrypt(&mut ct);
for i in 1..100 {
let enc = Enc::new_from_slices(&key, &iv).unwrap();
let mut t = pt;
let t = &mut t[..i];
enc.encrypt(t);
assert_eq!(t, &ct[..i]);
let dec = Dec::new_from_slices(&key, &iv).unwrap();
dec.decrypt(t);
assert_eq!(t, &pt[..i]);
}
}
#[test]
fn aes128_cfb_buffered_test() {
type Enc = Encryptor<Aes128>;
type BufEnc = BufEncryptor<Aes128>;
type BufDec = BufDecryptor<Aes128>;
let key = [42; 16];
let iv = [24; 16];
let mut pt = [0u8; 101];
for (i, b) in pt.iter_mut().enumerate() {
*b = u8::try_from(i % 11).unwrap();
}
// unbuffered
let enc = Enc::new_from_slices(&key, &iv).unwrap();
let mut ct = pt;
enc.encrypt(&mut ct);
// buffered
for i in 1..100 {
let mut buf_enc = BufEnc::new_from_slices(&key, &iv).unwrap();
let mut ct2 = pt;
for chunk in ct2.chunks_mut(i) {
buf_enc.encrypt(chunk);
}
assert_eq!(ct2, ct);
let mut buf_dec = BufDec::new_from_slices(&key, &iv).unwrap();
for chunk in ct2.chunks_mut(i) {
buf_dec.decrypt(chunk);
}
assert_eq!(ct2, pt);
}
// buffered with restore
for i in 1..100 {
let mut buf_enc = BufEnc::new_from_slices(&key, &iv).unwrap();
let mut ct2 = pt;
for chunk in ct2.chunks_mut(i) {
let (iv, pos) = buf_enc.get_state();
let cipher = Aes128::new_from_slice(&key).unwrap();
buf_enc = BufEnc::from_state(cipher, iv, pos);
buf_enc.encrypt(chunk);
}
assert_eq!(ct2, ct);
let mut buf_dec = BufDec::new_from_slices(&key, &iv).unwrap();
for chunk in ct2.chunks_mut(i) {
let (iv, pos) = buf_dec.get_state();
let cipher = Aes128::new_from_slice(&key).unwrap();
buf_dec = BufDec::from_state(cipher, iv, pos);
buf_dec.decrypt(chunk);
}
assert_eq!(ct2, pt);
}
}