| 1 |
//! Development-related functionality |
| 2 |
|
| 3 |
pub use blobby; |
| 4 |
|
| 5 |
/// Define block cipher test |
| 6 |
#[macro_export] |
| 7 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 8 |
macro_rules! block_cipher_test { |
| 9 |
($name:ident, $test_name:expr, $cipher:ty $(,)?) => { |
| 10 |
#[test] |
| 11 |
fn $name() { |
| 12 |
use cipher::{ |
| 13 |
blobby::Blob3Iterator, generic_array::GenericArray, typenum::Unsigned, |
| 14 |
BlockDecryptMut, BlockEncryptMut, BlockSizeUser, KeyInit, |
| 15 |
}; |
| 16 |
|
| 17 |
fn run_test(key: &[u8], pt: &[u8], ct: &[u8]) -> bool { |
| 18 |
let mut state = <$cipher as KeyInit>::new_from_slice(key).unwrap(); |
| 19 |
|
| 20 |
let mut block = GenericArray::clone_from_slice(pt); |
| 21 |
state.encrypt_block_mut(&mut block); |
| 22 |
if ct != block.as_slice() { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
state.decrypt_block_mut(&mut block); |
| 27 |
if pt != block.as_slice() { |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
true |
| 32 |
} |
| 33 |
|
| 34 |
fn run_par_test(key: &[u8], pt: &[u8]) -> bool { |
| 35 |
type Block = cipher::Block<$cipher>; |
| 36 |
|
| 37 |
let mut state = <$cipher as KeyInit>::new_from_slice(key).unwrap(); |
| 38 |
|
| 39 |
let block = Block::clone_from_slice(pt); |
| 40 |
let mut blocks1 = vec![block; 101]; |
| 41 |
for (i, b) in blocks1.iter_mut().enumerate() { |
| 42 |
*b = block; |
| 43 |
b[0] = b[0].wrapping_add(i as u8); |
| 44 |
} |
| 45 |
let mut blocks2 = blocks1.clone(); |
| 46 |
|
| 47 |
// check that `encrypt_blocks` and `encrypt_block` |
| 48 |
// result in the same ciphertext |
| 49 |
state.encrypt_blocks_mut(&mut blocks1); |
| 50 |
for b in blocks2.iter_mut() { |
| 51 |
state.encrypt_block_mut(b); |
| 52 |
} |
| 53 |
if blocks1 != blocks2 { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// check that `encrypt_blocks` and `encrypt_block` |
| 58 |
// result in the same plaintext |
| 59 |
state.decrypt_blocks_mut(&mut blocks1); |
| 60 |
for b in blocks2.iter_mut() { |
| 61 |
state.decrypt_block_mut(b); |
| 62 |
} |
| 63 |
if blocks1 != blocks2 { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
true |
| 68 |
} |
| 69 |
|
| 70 |
let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
| 71 |
for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() { |
| 72 |
let [key, pt, ct] = row.unwrap(); |
| 73 |
if !run_test(key, pt, ct) { |
| 74 |
panic!( |
| 75 |
"\n\ |
| 76 |
Failed test №{}\n\ |
| 77 |
key:\t{:?}\n\ |
| 78 |
plaintext:\t{:?}\n\ |
| 79 |
ciphertext:\t{:?}\n", |
| 80 |
i, key, pt, ct, |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
// test parallel blocks encryption/decryption |
| 85 |
if !run_par_test(key, pt) { |
| 86 |
panic!( |
| 87 |
"\n\ |
| 88 |
Failed parallel test №{}\n\ |
| 89 |
key:\t{:?}\n\ |
| 90 |
plaintext:\t{:?}\n\ |
| 91 |
ciphertext:\t{:?}\n", |
| 92 |
i, key, pt, ct, |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
}; |
| 98 |
} |
| 99 |
|
| 100 |
/// Define block mode encryption test |
| 101 |
#[macro_export] |
| 102 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 103 |
macro_rules! block_mode_enc_test { |
| 104 |
($name:ident, $test_name:expr, $cipher:ty $(,)?) => { |
| 105 |
#[test] |
| 106 |
fn $name() { |
| 107 |
use cipher::{ |
| 108 |
blobby::Blob4Iterator, generic_array::GenericArray, inout::InOutBuf, |
| 109 |
typenum::Unsigned, BlockEncryptMut, BlockSizeUser, KeyIvInit, |
| 110 |
}; |
| 111 |
|
| 112 |
fn run_test(key: &[u8], iv: &[u8], pt: &[u8], ct: &[u8]) -> bool { |
| 113 |
assert_eq!(pt.len(), ct.len()); |
| 114 |
// test block-by-block processing |
| 115 |
let mut state = <$cipher as KeyIvInit>::new_from_slices(key, iv).unwrap(); |
| 116 |
|
| 117 |
let mut out = vec![0u8; ct.len()]; |
| 118 |
let mut buf = InOutBuf::new(pt, &mut out).unwrap(); |
| 119 |
let (blocks, tail) = buf.reborrow().into_chunks(); |
| 120 |
assert_eq!(tail.len(), 0); |
| 121 |
for block in blocks { |
| 122 |
state.encrypt_block_inout_mut(block); |
| 123 |
} |
| 124 |
if buf.get_out() != ct { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
// test multi-block processing |
| 129 |
let mut state = <$cipher as KeyIvInit>::new_from_slices(key, iv).unwrap(); |
| 130 |
buf.get_out().iter_mut().for_each(|b| *b = 0); |
| 131 |
let (blocks, _) = buf.reborrow().into_chunks(); |
| 132 |
state.encrypt_blocks_inout_mut(blocks); |
| 133 |
if buf.get_out() != ct { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
true |
| 138 |
} |
| 139 |
|
| 140 |
let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
| 141 |
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() { |
| 142 |
let [key, iv, pt, ct] = row.unwrap(); |
| 143 |
if !run_test(key, iv, pt, ct) { |
| 144 |
panic!( |
| 145 |
"\n\ |
| 146 |
Failed test №{}\n\ |
| 147 |
key:\t{:?}\n\ |
| 148 |
iv:\t{:?}\n\ |
| 149 |
plaintext:\t{:?}\n\ |
| 150 |
ciphertext:\t{:?}\n", |
| 151 |
i, key, iv, pt, ct, |
| 152 |
); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
}; |
| 157 |
} |
| 158 |
|
| 159 |
/// Define block mode decryption test |
| 160 |
#[macro_export] |
| 161 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 162 |
macro_rules! block_mode_dec_test { |
| 163 |
($name:ident, $test_name:expr, $cipher:ty $(,)?) => { |
| 164 |
#[test] |
| 165 |
fn $name() { |
| 166 |
use cipher::{ |
| 167 |
blobby::Blob4Iterator, generic_array::GenericArray, inout::InOutBuf, |
| 168 |
typenum::Unsigned, BlockDecryptMut, BlockSizeUser, KeyIvInit, |
| 169 |
}; |
| 170 |
|
| 171 |
fn run_test(key: &[u8], iv: &[u8], pt: &[u8], ct: &[u8]) -> bool { |
| 172 |
assert_eq!(pt.len(), ct.len()); |
| 173 |
// test block-by-block processing |
| 174 |
let mut state = <$cipher as KeyIvInit>::new_from_slices(key, iv).unwrap(); |
| 175 |
|
| 176 |
let mut out = vec![0u8; pt.len()]; |
| 177 |
let mut buf = InOutBuf::new(ct, &mut out).unwrap(); |
| 178 |
let (blocks, tail) = buf.reborrow().into_chunks(); |
| 179 |
assert_eq!(tail.len(), 0); |
| 180 |
for block in blocks { |
| 181 |
state.decrypt_block_inout_mut(block); |
| 182 |
} |
| 183 |
if buf.get_out() != pt { |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
// test multi-block processing |
| 188 |
let mut state = <$cipher as KeyIvInit>::new_from_slices(key, iv).unwrap(); |
| 189 |
buf.get_out().iter_mut().for_each(|b| *b = 0); |
| 190 |
let (blocks, _) = buf.reborrow().into_chunks(); |
| 191 |
state.decrypt_blocks_inout_mut(blocks); |
| 192 |
if buf.get_out() != pt { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
true |
| 197 |
} |
| 198 |
|
| 199 |
let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
| 200 |
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() { |
| 201 |
let [key, iv, pt, ct] = row.unwrap(); |
| 202 |
if !run_test(key, iv, pt, ct) { |
| 203 |
panic!( |
| 204 |
"\n\ |
| 205 |
Failed test №{}\n\ |
| 206 |
key:\t{:?}\n\ |
| 207 |
iv:\t{:?}\n\ |
| 208 |
plaintext:\t{:?}\n\ |
| 209 |
ciphertext:\t{:?}\n", |
| 210 |
i, key, iv, pt, ct, |
| 211 |
); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
}; |
| 216 |
} |
| 217 |
|
| 218 |
/// Define `IvState` test |
| 219 |
#[macro_export] |
| 220 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 221 |
macro_rules! iv_state_test { |
| 222 |
($name:ident, $cipher:ty, encrypt $(,)?) => { |
| 223 |
$crate::iv_state_test!($name, $cipher, encrypt_blocks_mut); |
| 224 |
}; |
| 225 |
($name:ident, $cipher:ty, decrypt $(,)?) => { |
| 226 |
$crate::iv_state_test!($name, $cipher, decrypt_blocks_mut); |
| 227 |
}; |
| 228 |
($name:ident, $cipher:ty, apply_ks $(,)?) => { |
| 229 |
$crate::iv_state_test!($name, $cipher, apply_keystream_blocks); |
| 230 |
}; |
| 231 |
($name:ident, $cipher:ty, $method:ident $(,)?) => { |
| 232 |
#[test] |
| 233 |
fn $name() { |
| 234 |
use cipher::*; |
| 235 |
|
| 236 |
let mut blocks = [Block::<$cipher>::default(); 32]; |
| 237 |
|
| 238 |
for (i, block) in blocks.iter_mut().enumerate() { |
| 239 |
for (j, b) in block.iter_mut().enumerate() { |
| 240 |
*b = (i + j) as u8; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
let mut key = Key::<$cipher>::default(); |
| 245 |
let mut iv = Iv::<$cipher>::default(); |
| 246 |
key.iter_mut().for_each(|b| *b = 0x42); |
| 247 |
iv.iter_mut().for_each(|b| *b = 0x24); |
| 248 |
|
| 249 |
let mut cipher = <$cipher>::new(&key, &iv); |
| 250 |
let mut target = blocks.clone(); |
| 251 |
cipher.$method(&mut target); |
| 252 |
|
| 253 |
for i in 0..32 { |
| 254 |
let mut blocks = blocks.clone(); |
| 255 |
let (b1, b2) = blocks.split_at_mut(i); |
| 256 |
let mut cipher1 = <$cipher>::new(&key, &iv); |
| 257 |
cipher1.$method(b1); |
| 258 |
let temp_iv = cipher1.iv_state(); |
| 259 |
let mut cipher2 = <$cipher>::new(&key, &temp_iv); |
| 260 |
cipher2.$method(b2); |
| 261 |
assert_eq!(blocks, target); |
| 262 |
} |
| 263 |
} |
| 264 |
}; |
| 265 |
} |
| 266 |
|
| 267 |
/// Define block encryptor benchmark |
| 268 |
#[macro_export] |
| 269 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 270 |
macro_rules! block_encryptor_bench { |
| 271 |
(Key: $cipher:ty, $block_name:ident, $blocks_name:ident $(,)? ) => { |
| 272 |
$crate::block_encryptor_bench!( |
| 273 |
{ |
| 274 |
use $crate::KeyInit; |
| 275 |
let key = test::black_box(Default::default()); |
| 276 |
<$cipher>::new(&key) |
| 277 |
}, |
| 278 |
$cipher, |
| 279 |
$block_name, |
| 280 |
$blocks_name, |
| 281 |
); |
| 282 |
}; |
| 283 |
(KeyIv: $cipher:ty, $block_name:ident, $blocks_name:ident $(,)? ) => { |
| 284 |
$crate::block_encryptor_bench!( |
| 285 |
{ |
| 286 |
use $crate::KeyIvInit; |
| 287 |
let key = test::black_box(Default::default()); |
| 288 |
let iv = test::black_box(Default::default()); |
| 289 |
<$cipher>::new(&key, &iv) |
| 290 |
}, |
| 291 |
$cipher, |
| 292 |
$block_name, |
| 293 |
$blocks_name, |
| 294 |
); |
| 295 |
}; |
| 296 |
($init:block, $cipher:ty, $block_name:ident, $blocks_name:ident $(,)? ) => { |
| 297 |
#[bench] |
| 298 |
pub fn $block_name(bh: &mut test::Bencher) { |
| 299 |
use cipher::BlockEncryptMut; |
| 300 |
|
| 301 |
let mut cipher = $init; |
| 302 |
let mut blocks = vec![Default::default(); 1024]; |
| 303 |
|
| 304 |
bh.iter(|| { |
| 305 |
for block in blocks.iter_mut() { |
| 306 |
cipher.encrypt_block_mut(block); |
| 307 |
} |
| 308 |
test::black_box(&blocks); |
| 309 |
}); |
| 310 |
bh.bytes = (blocks.len() * blocks[0].len()) as u64; |
| 311 |
} |
| 312 |
|
| 313 |
#[bench] |
| 314 |
pub fn $blocks_name(bh: &mut test::Bencher) { |
| 315 |
use cipher::BlockEncryptMut; |
| 316 |
|
| 317 |
let mut cipher = $init; |
| 318 |
let mut blocks = vec![Default::default(); 1024]; |
| 319 |
|
| 320 |
bh.iter(|| { |
| 321 |
cipher.encrypt_blocks_mut(&mut blocks); |
| 322 |
test::black_box(&blocks); |
| 323 |
}); |
| 324 |
bh.bytes = (blocks.len() * blocks[0].len()) as u64; |
| 325 |
} |
| 326 |
}; |
| 327 |
} |
| 328 |
|
| 329 |
/// Define block decryptor benchmark |
| 330 |
#[macro_export] |
| 331 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 332 |
macro_rules! block_decryptor_bench { |
| 333 |
(Key: $cipher:ty, $block_name:ident, $blocks_name:ident $(,)? ) => { |
| 334 |
$crate::block_decryptor_bench!( |
| 335 |
{ |
| 336 |
use $crate::KeyInit; |
| 337 |
let key = test::black_box(Default::default()); |
| 338 |
<$cipher>::new(&key) |
| 339 |
}, |
| 340 |
$cipher, |
| 341 |
$block_name, |
| 342 |
$blocks_name, |
| 343 |
); |
| 344 |
}; |
| 345 |
(KeyIv: $cipher:ty, $block_name:ident, $blocks_name:ident $(,)? ) => { |
| 346 |
$crate::block_decryptor_bench!( |
| 347 |
{ |
| 348 |
use $crate::KeyIvInit; |
| 349 |
let key = test::black_box(Default::default()); |
| 350 |
let iv = test::black_box(Default::default()); |
| 351 |
<$cipher>::new(&key, &iv) |
| 352 |
}, |
| 353 |
$cipher, |
| 354 |
$block_name, |
| 355 |
$blocks_name, |
| 356 |
); |
| 357 |
}; |
| 358 |
($init:block, $cipher:ty, $block_name:ident, $blocks_name:ident $(,)? ) => { |
| 359 |
#[bench] |
| 360 |
pub fn $block_name(bh: &mut test::Bencher) { |
| 361 |
use cipher::BlockDecryptMut; |
| 362 |
|
| 363 |
let mut cipher = $init; |
| 364 |
let mut blocks = vec![Default::default(); 1024]; |
| 365 |
|
| 366 |
bh.iter(|| { |
| 367 |
for block in blocks.iter_mut() { |
| 368 |
cipher.decrypt_block_mut(block); |
| 369 |
} |
| 370 |
test::black_box(&blocks); |
| 371 |
}); |
| 372 |
bh.bytes = (blocks.len() * blocks[0].len()) as u64; |
| 373 |
} |
| 374 |
|
| 375 |
#[bench] |
| 376 |
pub fn $blocks_name(bh: &mut test::Bencher) { |
| 377 |
use cipher::BlockDecryptMut; |
| 378 |
|
| 379 |
let mut cipher = $init; |
| 380 |
let mut blocks = vec![Default::default(); 1024]; |
| 381 |
|
| 382 |
bh.iter(|| { |
| 383 |
cipher.decrypt_blocks_mut(&mut blocks); |
| 384 |
test::black_box(&blocks); |
| 385 |
}); |
| 386 |
bh.bytes = (blocks.len() * blocks[0].len()) as u64; |
| 387 |
} |
| 388 |
}; |
| 389 |
} |