| 1 |
//! Development-related functionality |
| 2 |
|
| 3 |
/// Test core functionality of synchronous stream cipher |
| 4 |
#[macro_export] |
| 5 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 6 |
macro_rules! stream_cipher_test { |
| 7 |
($name:ident, $test_name:expr, $cipher:ty $(,)?) => { |
| 8 |
#[test] |
| 9 |
fn $name() { |
| 10 |
use cipher::generic_array::GenericArray; |
| 11 |
use cipher::{blobby::Blob4Iterator, KeyIvInit, StreamCipher}; |
| 12 |
|
| 13 |
let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
| 14 |
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() { |
| 15 |
let [key, iv, pt, ct] = row.unwrap(); |
| 16 |
|
| 17 |
for chunk_n in 1..256 { |
| 18 |
let mut mode = <$cipher>::new_from_slices(key, iv).unwrap(); |
| 19 |
let mut pt = pt.to_vec(); |
| 20 |
for chunk in pt.chunks_mut(chunk_n) { |
| 21 |
mode.apply_keystream(chunk); |
| 22 |
} |
| 23 |
if pt != &ct[..] { |
| 24 |
panic!( |
| 25 |
"Failed main test №{}, chunk size: {}\n\ |
| 26 |
key:\t{:?}\n\ |
| 27 |
iv:\t{:?}\n\ |
| 28 |
plaintext:\t{:?}\n\ |
| 29 |
ciphertext:\t{:?}\n", |
| 30 |
i, chunk_n, key, iv, pt, ct, |
| 31 |
); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
/// Test stream synchronous stream cipher seeking capabilities |
| 40 |
#[macro_export] |
| 41 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 42 |
macro_rules! stream_cipher_seek_test { |
| 43 |
($name:ident, $cipher:ty) => { |
| 44 |
#[test] |
| 45 |
fn $name() { |
| 46 |
use cipher::generic_array::GenericArray; |
| 47 |
use cipher::{KeyIvInit, StreamCipher, StreamCipherSeek}; |
| 48 |
|
| 49 |
fn get_cipher() -> $cipher { |
| 50 |
<$cipher>::new(&Default::default(), &Default::default()) |
| 51 |
} |
| 52 |
|
| 53 |
const MAX_SEEK: usize = 512; |
| 54 |
|
| 55 |
let mut ct = [0u8; MAX_SEEK]; |
| 56 |
get_cipher().apply_keystream(&mut ct[..]); |
| 57 |
|
| 58 |
for n in 0..MAX_SEEK { |
| 59 |
let mut cipher = get_cipher(); |
| 60 |
assert_eq!(cipher.current_pos::<usize>(), 0); |
| 61 |
cipher.seek(n); |
| 62 |
assert_eq!(cipher.current_pos::<usize>(), n); |
| 63 |
let mut buf = [0u8; MAX_SEEK]; |
| 64 |
cipher.apply_keystream(&mut buf[n..]); |
| 65 |
assert_eq!(cipher.current_pos::<usize>(), MAX_SEEK); |
| 66 |
assert_eq!(&buf[n..], &ct[n..]); |
| 67 |
} |
| 68 |
|
| 69 |
const MAX_CHUNK: usize = 128; |
| 70 |
const MAX_LEN: usize = 1024; |
| 71 |
|
| 72 |
let mut buf = [0u8; MAX_CHUNK]; |
| 73 |
let mut cipher = get_cipher(); |
| 74 |
assert_eq!(cipher.current_pos::<usize>(), 0); |
| 75 |
cipher.apply_keystream(&mut []); |
| 76 |
assert_eq!(cipher.current_pos::<usize>(), 0); |
| 77 |
for n in 1..MAX_CHUNK { |
| 78 |
assert_eq!(cipher.current_pos::<usize>(), 0); |
| 79 |
for m in 1.. { |
| 80 |
cipher.apply_keystream(&mut buf[..n]); |
| 81 |
assert_eq!(cipher.current_pos::<usize>(), n * m); |
| 82 |
if n * m > MAX_LEN { |
| 83 |
break; |
| 84 |
} |
| 85 |
} |
| 86 |
cipher.seek(0); |
| 87 |
} |
| 88 |
} |
| 89 |
}; |
| 90 |
} |
| 91 |
|
| 92 |
/// Create stream cipher benchmarks |
| 93 |
#[macro_export] |
| 94 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 95 |
macro_rules! stream_cipher_bench { |
| 96 |
( |
| 97 |
$cipher:ty; |
| 98 |
$($name:ident $bs:expr;)* |
| 99 |
) => { |
| 100 |
$crate::stream_cipher_bench!( |
| 101 |
Init: { |
| 102 |
use $crate::KeyIvInit; |
| 103 |
let key = test::black_box(Default::default()); |
| 104 |
let iv = test::black_box(Default::default()); |
| 105 |
<$cipher>::new(&key, &iv) |
| 106 |
}; |
| 107 |
$($name $bs;)* |
| 108 |
); |
| 109 |
}; |
| 110 |
( |
| 111 |
Key: $cipher:ty; |
| 112 |
$($name:ident $bs:expr;)* |
| 113 |
) => { |
| 114 |
$crate::stream_cipher_bench!( |
| 115 |
Init: { |
| 116 |
use $crate::KeyInit; |
| 117 |
let key = test::black_box(Default::default()); |
| 118 |
let iv = test::black_box(Default::default()); |
| 119 |
<$cipher>::new(&key, &iv) |
| 120 |
}; |
| 121 |
$($name $bs;)* |
| 122 |
); |
| 123 |
}; |
| 124 |
( |
| 125 |
Init: $init:expr; |
| 126 |
$($name:ident $bs:expr;)* |
| 127 |
) => { |
| 128 |
$( |
| 129 |
#[bench] |
| 130 |
fn $name(b: &mut test::Bencher) { |
| 131 |
use $crate::StreamCipher; |
| 132 |
|
| 133 |
let mut cipher = $init; |
| 134 |
let mut buf = vec![0; $bs]; |
| 135 |
|
| 136 |
b.iter(|| { |
| 137 |
cipher.apply_keystream(&mut buf); |
| 138 |
test::black_box(&buf); |
| 139 |
}); |
| 140 |
|
| 141 |
b.bytes = $bs; |
| 142 |
} |
| 143 |
)* |
| 144 |
}; |
| 145 |
} |