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
use block_cipher_trait::NewVarKey;
use generic_array::GenericArray;
pub struct BlockCipherTest {
pub name: &'static str,
pub key: &'static [u8],
pub input: &'static [u8],
pub output: &'static [u8],
}
#[macro_export]
macro_rules! new_block_cipher_tests {
( $( $name:expr ),* ) => {
[$(
BlockCipherTest {
name: $name,
key: include_bytes!(concat!("data/", $name, ".key.bin")),
input: include_bytes!(concat!("data/", $name, ".input.bin")),
output: include_bytes!(concat!("data/", $name, ".output.bin")),
},
)*]
};
}
pub fn encrypt_decrypt<B: NewVarKey>(tests: &[BlockCipherTest]) {
for test in tests {
let state = B::new(test.key).unwrap();
let mut block = GenericArray::clone_from_slice(test.input);
state.encrypt_block(&mut block);
assert_eq!(test.output, block.as_slice());
}
for test in tests {
let state = B::new(test.key).unwrap();
let mut block = GenericArray::clone_from_slice(test.output);
state.decrypt_block(&mut block);
assert_eq!(test.input, block.as_slice());
}
}
#[macro_export]
macro_rules! bench_block_cipher {
($cipher:path, $key_len:expr) => {
extern crate test;
extern crate block_cipher_trait;
use test::Bencher;
use block_cipher_trait::{BlockCipher, NewVarKey};
#[bench]
pub fn encrypt(bh: &mut Bencher) {
let state = <$cipher>::new(&[1u8; $key_len]).unwrap();
let mut block = Default::default();
bh.iter(|| {
state.encrypt_block(&mut block);
test::black_box(&block);
});
bh.bytes = block.len() as u64;
}
#[bench]
pub fn decrypt(bh: &mut Bencher) {
let state = <$cipher>::new(&[1u8; $key_len]).unwrap();
let mut block = Default::default();
bh.iter(|| {
state.decrypt_block(&mut block);
test::black_box(&block);
});
bh.bytes = block.len() as u64;
}
}
}