| 1 |
//! Development-related functionality |
| 2 |
|
| 3 |
#![allow(clippy::missing_errors_doc)] |
| 4 |
|
| 5 |
pub use blobby; |
| 6 |
|
| 7 |
mod fixed; |
| 8 |
#[cfg(feature = "mac")] |
| 9 |
mod mac; |
| 10 |
mod rng; |
| 11 |
mod xof; |
| 12 |
|
| 13 |
pub use fixed::*; |
| 14 |
#[cfg(feature = "mac")] |
| 15 |
pub use mac::*; |
| 16 |
pub use xof::*; |
| 17 |
|
| 18 |
/// Test vector for hash functions |
| 19 |
#[derive(Debug, Clone, Copy)] |
| 20 |
pub struct TestVector { |
| 21 |
/// Input data |
| 22 |
pub input: &'static [u8], |
| 23 |
/// Output hash |
| 24 |
pub output: &'static [u8], |
| 25 |
} |
| 26 |
|
| 27 |
/// Define hash function test |
| 28 |
#[macro_export] |
| 29 |
macro_rules! new_test { |
| 30 |
($name:ident, $hasher:ty, $test_fn:ident $(,)?) => { |
| 31 |
#[test] |
| 32 |
fn $name() { |
| 33 |
use $crate::dev::TestVector; |
| 34 |
|
| 35 |
$crate::dev::blobby::parse_into_structs!( |
| 36 |
include_bytes!(concat!("data/", stringify!($name), ".blb")); |
| 37 |
static TEST_VECTORS: &[TestVector { input, output }]; |
| 38 |
); |
| 39 |
|
| 40 |
for (i, tv) in TEST_VECTORS.iter().enumerate() { |
| 41 |
if let Err(reason) = $test_fn::<$hasher>(tv) { |
| 42 |
panic!( |
| 43 |
"\n\ |
| 44 |
Failed test #{i}:\n\ |
| 45 |
reason:\t{reason}\n\ |
| 46 |
test vector:\t{tv:?}\n" |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
}; |
| 52 |
} |
| 53 |
|
| 54 |
/// Define hash function serialization test |
| 55 |
#[macro_export] |
| 56 |
macro_rules! hash_serialization_test { |
| 57 |
($name:ident, $hasher:ty $(,)?) => { |
| 58 |
#[test] |
| 59 |
fn $name() { |
| 60 |
use digest::{ |
| 61 |
Digest, |
| 62 |
common::{BlockSizeUser, hazmat::SerializableState}, |
| 63 |
typenum::Unsigned, |
| 64 |
}; |
| 65 |
|
| 66 |
let mut h = <$hasher>::new(); |
| 67 |
|
| 68 |
h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]); |
| 69 |
|
| 70 |
let serialized_state = h.serialize(); |
| 71 |
let expected = include_bytes!(concat!("data/", stringify!($name), ".bin")); |
| 72 |
assert_eq!(serialized_state.as_slice(), expected); |
| 73 |
|
| 74 |
let mut h = <$hasher>::deserialize(&serialized_state).unwrap(); |
| 75 |
|
| 76 |
h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]); |
| 77 |
let output1 = h.finalize(); |
| 78 |
|
| 79 |
let mut h = <$hasher>::new(); |
| 80 |
h.update(&[0x13; 2 * (<$hasher as BlockSizeUser>::BlockSize::USIZE + 1)]); |
| 81 |
let output2 = h.finalize(); |
| 82 |
|
| 83 |
assert_eq!(output1, output2); |
| 84 |
} |
| 85 |
}; |
| 86 |
} |
| 87 |
|
| 88 |
/// Define hash function serialization test |
| 89 |
#[macro_export] |
| 90 |
macro_rules! hash_rt_outsize_serialization_test { |
| 91 |
($name:ident, $hasher:ty, $expected_serialized_state:expr) => { |
| 92 |
#[test] |
| 93 |
fn $name() { |
| 94 |
use digest::{ |
| 95 |
Digest, Update, VariableOutput, |
| 96 |
common::{BlockSizeUser, hazmat::SerializableState}, |
| 97 |
typenum::Unsigned, |
| 98 |
}; |
| 99 |
const HASH_OUTPUT_SIZE: usize = <$hasher>::MAX_OUTPUT_SIZE - 1; |
| 100 |
|
| 101 |
let mut h = <$hasher>::new(HASH_OUTPUT_SIZE).unwrap(); |
| 102 |
|
| 103 |
h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]); |
| 104 |
|
| 105 |
let serialized_state = h.serialize(); |
| 106 |
assert_eq!(serialized_state.as_slice(), $expected_serialized_state); |
| 107 |
|
| 108 |
let mut h = <$hasher>::deserialize(&serialized_state).unwrap(); |
| 109 |
|
| 110 |
h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]); |
| 111 |
let mut output1 = [0; HASH_OUTPUT_SIZE]; |
| 112 |
h.finalize_variable(&mut output1).unwrap(); |
| 113 |
|
| 114 |
let mut h = <$hasher>::new(HASH_OUTPUT_SIZE).unwrap(); |
| 115 |
h.update(&[0x13; 2 * (<$hasher as BlockSizeUser>::BlockSize::USIZE + 1)]); |
| 116 |
let mut output2 = [0; HASH_OUTPUT_SIZE]; |
| 117 |
h.finalize_variable(&mut output2).unwrap(); |
| 118 |
|
| 119 |
assert_eq!(output1, output2); |
| 120 |
} |
| 121 |
}; |
| 122 |
} |
| 123 |
|
| 124 |
/// Define [`Update`][crate::Update] impl benchmark |
| 125 |
#[macro_export] |
| 126 |
macro_rules! bench_update { |
| 127 |
( |
| 128 |
$init:expr; |
| 129 |
$($name:ident $bs:expr;)* |
| 130 |
) => { |
| 131 |
$( |
| 132 |
#[bench] |
| 133 |
fn $name(b: &mut Bencher) { |
| 134 |
let mut d = $init; |
| 135 |
let data = [0; $bs]; |
| 136 |
|
| 137 |
b.iter(|| { |
| 138 |
digest::Update::update(&mut d, &data[..]); |
| 139 |
}); |
| 140 |
|
| 141 |
b.bytes = $bs; |
| 142 |
} |
| 143 |
)* |
| 144 |
}; |
| 145 |
} |
| 146 |
|
| 147 |
/// Feed ~1 MiB of pseudorandom data to an updatable state. |
| 148 |
pub fn feed_rand_16mib<D: crate::Update>(d: &mut D) { |
| 149 |
let buf = &mut [0u8; 1024]; |
| 150 |
let mut rng = rng::RNG; |
| 151 |
let n = 16 * (1 << 20) / buf.len(); |
| 152 |
for _ in 0..n { |
| 153 |
rng.fill(buf); |
| 154 |
d.update(buf); |
| 155 |
// additional byte, so size of fed data |
| 156 |
// will not be multiple of block size |
| 157 |
d.update(&[42]); |
| 158 |
} |
| 159 |
} |