| 1 |
use crate::ExtendableOutputReset; |
| 2 |
use core::fmt::Debug; |
| 3 |
|
| 4 |
/// Resettable XOF test |
| 5 |
pub fn xof_reset_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str> |
| 6 |
where |
| 7 |
D: ExtendableOutputReset + Default + Debug + Clone, |
| 8 |
{ |
| 9 |
let mut hasher = D::default(); |
| 10 |
let mut buf = [0u8; 1024]; |
| 11 |
let buf = &mut buf[..output.len()]; |
| 12 |
// Test that it works when accepting the message all at once |
| 13 |
hasher.update(input); |
| 14 |
let mut hasher2 = hasher.clone(); |
| 15 |
hasher.finalize_xof_into(buf); |
| 16 |
if buf != output { |
| 17 |
return Some("whole message"); |
| 18 |
} |
| 19 |
buf.iter_mut().for_each(|b| *b = 0); |
| 20 |
|
| 21 |
// Test if reset works correctly |
| 22 |
hasher2.reset(); |
| 23 |
hasher2.update(input); |
| 24 |
hasher2.finalize_xof_reset_into(buf); |
| 25 |
if buf != output { |
| 26 |
return Some("whole message after reset"); |
| 27 |
} |
| 28 |
buf.iter_mut().for_each(|b| *b = 0); |
| 29 |
|
| 30 |
// Test that it works when accepting the message in chunks |
| 31 |
for n in 1..core::cmp::min(17, input.len()) { |
| 32 |
let mut hasher = D::default(); |
| 33 |
for chunk in input.chunks(n) { |
| 34 |
hasher.update(chunk); |
| 35 |
hasher2.update(chunk); |
| 36 |
} |
| 37 |
hasher.finalize_xof_into(buf); |
| 38 |
if buf != output { |
| 39 |
return Some("message in chunks"); |
| 40 |
} |
| 41 |
buf.iter_mut().for_each(|b| *b = 0); |
| 42 |
|
| 43 |
hasher2.finalize_xof_reset_into(buf); |
| 44 |
if buf != output { |
| 45 |
return Some("message in chunks"); |
| 46 |
} |
| 47 |
buf.iter_mut().for_each(|b| *b = 0); |
| 48 |
} |
| 49 |
|
| 50 |
None |
| 51 |
} |