| 1 |
|
| 2 |
|
| 3 |
pub use blobby; |
| 4 |
|
| 5 |
|
| 6 |
#[macro_export] |
| 7 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 8 |
macro_rules! new_test { |
| 9 |
($name:ident, $test_name:expr, $mac:ty) => { |
| 10 |
#[test] |
| 11 |
fn $name() { |
| 12 |
use crypto_mac::dev::blobby::Blob3Iterator; |
| 13 |
use crypto_mac::{Mac, NewMac}; |
| 14 |
|
| 15 |
fn run_test(key: &[u8], input: &[u8], tag: &[u8]) -> Option<&'static str> { |
| 16 |
let mut mac = <$mac as NewMac>::new_varkey(key).unwrap(); |
| 17 |
mac.update(input); |
| 18 |
let result = mac.finalize_reset(); |
| 19 |
if &result.into_bytes()[..] != tag { |
| 20 |
return Some("whole message"); |
| 21 |
} |
| 22 |
|
| 23 |
mac.update(input); |
| 24 |
if mac.verify(&tag).is_err() { |
| 25 |
return Some("after reset"); |
| 26 |
} |
| 27 |
|
| 28 |
let mut mac = <$mac as NewMac>::new_varkey(key).unwrap(); |
| 29 |
|
| 30 |
for i in 0..input.len() { |
| 31 |
mac.update(&input[i..i + 1]); |
| 32 |
} |
| 33 |
if let Err(_) = mac.verify(tag) { |
| 34 |
return Some("message byte-by-byte"); |
| 35 |
} |
| 36 |
None |
| 37 |
} |
| 38 |
|
| 39 |
let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
| 40 |
|
| 41 |
for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() { |
| 42 |
let [key, input, tag] = row.unwrap(); |
| 43 |
if let Some(desc) = run_test(key, input, tag) { |
| 44 |
panic!( |
| 45 |
"\n\ |
| 46 |
Failed test №{}: {}\n\ |
| 47 |
key:\t{:?}\n\ |
| 48 |
input:\t{:?}\n\ |
| 49 |
tag:\t{:?}\n", |
| 50 |
i, desc, key, input, tag, |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
#[macro_export] |
| 60 |
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))] |
| 61 |
macro_rules! bench { |
| 62 |
($name:ident, $engine:path, $bs:expr) => { |
| 63 |
#[bench] |
| 64 |
fn $name(b: &mut Bencher) { |
| 65 |
let key = Default::default(); |
| 66 |
let mut mac = <$engine>::new(&key); |
| 67 |
let data = [0; $bs]; |
| 68 |
|
| 69 |
b.iter(|| { |
| 70 |
mac.update(&data); |
| 71 |
}); |
| 72 |
|
| 73 |
b.bytes = $bs; |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
($engine:path) => { |
| 78 |
extern crate test; |
| 79 |
|
| 80 |
use crypto_mac::{Mac, NewMac}; |
| 81 |
use test::Bencher; |
| 82 |
|
| 83 |
$crate::bench!(bench1_10, $engine, 10); |
| 84 |
$crate::bench!(bench2_100, $engine, 100); |
| 85 |
$crate::bench!(bench3_1000, $engine, 1000); |
| 86 |
$crate::bench!(bench3_10000, $engine, 10000); |
| 87 |
}; |
| 88 |
} |