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
use block_cipher_trait::{BlockCipherVarKey, from_slice};
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: BlockCipherVarKey>(tests: &[BlockCipherTest]) {
let mut buf = GenericArray::default();
for test in tests {
let state = B::new(test.key);
let input = from_slice(test.input);
state.encrypt_block(input, &mut buf);
assert_eq!(test.output, &buf[..]);
}
for test in tests {
let state = B::new(test.key);
let output = from_slice(test.output);
state.decrypt_block(output, &mut buf);
assert_eq!(test.input, &buf[..]);
}
}
#[macro_export]
macro_rules! bench_block_cipher {
($cipher:path, $key:expr, $input:expr) => {
extern crate test;
extern crate block_cipher_trait;
extern crate generic_array;
use test::Bencher;
use block_cipher_trait::{BlockCipher, BlockCipherVarKey, from_slice};
use generic_array::GenericArray;
#[bench]
pub fn encrypt(bh: &mut Bencher) {
let state = <$cipher>::new($key);
let input = $input;
let mut output = GenericArray::new();
bh.iter(|| {
state.encrypt_block(&from_slice(input), &mut output);
});
bh.bytes = 8u64;
}
}
}