| 1 |
use core::{convert::TryInto, fmt::Debug}; |
| 2 |
use digest::ExtendableOutput; |
| 3 |
|
| 4 |
pub(crate) fn turbo_shake_test<D, F>( |
| 5 |
input: &[u8], |
| 6 |
output: &[u8], |
| 7 |
truncate_output: usize, |
| 8 |
new: F, |
| 9 |
) -> Option<&'static str> |
| 10 |
where |
| 11 |
D: ExtendableOutput + Debug + Clone, |
| 12 |
F: Fn() -> D, |
| 13 |
{ |
| 14 |
let mut hasher = new(); |
| 15 |
let mut buf = [0u8; 16 * 1024]; |
| 16 |
let buf = &mut buf[..truncate_output + output.len()]; |
| 17 |
// Test that it works when accepting the message all at once |
| 18 |
hasher.update(input); |
| 19 |
let mut hasher2 = hasher.clone(); |
| 20 |
hasher.finalize_xof_into(buf); |
| 21 |
if &buf[truncate_output..] != output { |
| 22 |
return Some("whole message"); |
| 23 |
} |
| 24 |
buf.iter_mut().for_each(|b| *b = 0); |
| 25 |
|
| 26 |
// Test that it works when accepting the message in chunks |
| 27 |
for n in 1..core::cmp::min(17, input.len()) { |
| 28 |
let mut hasher = new(); |
| 29 |
for chunk in input.chunks(n) { |
| 30 |
hasher.update(chunk); |
| 31 |
hasher2.update(chunk); |
| 32 |
} |
| 33 |
hasher.finalize_xof_into(buf); |
| 34 |
if &buf[truncate_output..] != output { |
| 35 |
return Some("message in chunks"); |
| 36 |
} |
| 37 |
buf.iter_mut().for_each(|b| *b = 0); |
| 38 |
} |
| 39 |
|
| 40 |
None |
| 41 |
} |
| 42 |
|
| 43 |
macro_rules! new_turbo_shake_test { |
| 44 |
($name:ident, $test_name:expr, $hasher:ty, $hasher_core:ty, $test_func:ident $(,)?) => { |
| 45 |
#[test] |
| 46 |
fn $name() { |
| 47 |
use digest::dev::blobby::Blob5Iterator; |
| 48 |
let data = include_bytes!(concat!("data/", $test_name, ".blb")); |
| 49 |
|
| 50 |
for (i, row) in Blob5Iterator::new(data).unwrap().enumerate() { |
| 51 |
let [domain_separation, input, input_pattern_length, output, truncate_output] = |
| 52 |
row.unwrap(); |
| 53 |
|
| 54 |
let input = if (input_pattern_length.len() == 0) { |
| 55 |
input.to_vec() |
| 56 |
} else if (input.len() == 0) { |
| 57 |
let pattern_length = |
| 58 |
u64::from_be_bytes(input_pattern_length.try_into().unwrap()); |
| 59 |
let mut input = Vec::<u8>::new(); |
| 60 |
for value in 0..pattern_length { |
| 61 |
input.push((value % 0xFB).try_into().unwrap()); |
| 62 |
} |
| 63 |
input |
| 64 |
} else { |
| 65 |
panic!( |
| 66 |
"\ |
| 67 |
failed to read tests data\n\ |
| 68 |
input:\t{:02X?}\n\ |
| 69 |
input_pattern_length:\t{:02X?}\n", |
| 70 |
input, input_pattern_length, |
| 71 |
); |
| 72 |
}; |
| 73 |
|
| 74 |
if let Some(desc) = $test_func( |
| 75 |
&input, |
| 76 |
output, |
| 77 |
u64::from_be_bytes(truncate_output.try_into().unwrap()) |
| 78 |
.try_into() |
| 79 |
.unwrap(), |
| 80 |
|| <$hasher>::from_core(<$hasher_core>::new(domain_separation[0])), |
| 81 |
) { |
| 82 |
panic!( |
| 83 |
"\n\ |
| 84 |
Failed test №{}: {}\n\ |
| 85 |
input:\t{:02X?}\n\ |
| 86 |
output:\t{:02X?}\n", |
| 87 |
i, desc, &input, output, |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
}; |
| 93 |
} |
| 94 |
|
| 95 |
new_turbo_shake_test!( |
| 96 |
turboshake128, |
| 97 |
"turboshake128", |
| 98 |
sha3::TurboShake128, |
| 99 |
sha3::TurboShake128Core, |
| 100 |
turbo_shake_test, |
| 101 |
); |
| 102 |
new_turbo_shake_test!( |
| 103 |
turboshake256, |
| 104 |
"turboshake256", |
| 105 |
sha3::TurboShake256, |
| 106 |
sha3::TurboShake256Core, |
| 107 |
turbo_shake_test, |
| 108 |
); |