| 1 |
//! Tests against a pseudo-hash. |
| 2 |
|
| 3 |
#![cfg(feature = "block-api")] |
| 4 |
|
| 5 |
mod block_api { |
| 6 |
use core::fmt; |
| 7 |
use digest::{ |
| 8 |
HashMarker, Output, OutputSizeUser, Reset, |
| 9 |
block_api::{ |
| 10 |
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, FixedOutputCore, |
| 11 |
UpdateCore, |
| 12 |
}, |
| 13 |
common::hazmat::{DeserializeStateError, SerializableState, SerializedState}, |
| 14 |
consts::U8, |
| 15 |
}; |
| 16 |
|
| 17 |
#[cfg(feature = "zeroize")] |
| 18 |
use zeroize::Zeroize; |
| 19 |
|
| 20 |
/// Core of primitive XOR hasher for testing purposes |
| 21 |
#[derive(Clone, Default, Debug)] |
| 22 |
pub struct FixedHashCore { |
| 23 |
state: u64, |
| 24 |
} |
| 25 |
|
| 26 |
impl AlgorithmName for FixedHashCore { |
| 27 |
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { |
| 28 |
f.write_str("FixedHash") |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl BlockSizeUser for FixedHashCore { |
| 33 |
type BlockSize = U8; |
| 34 |
} |
| 35 |
|
| 36 |
impl BufferKindUser for FixedHashCore { |
| 37 |
type BufferKind = block_buffer::Eager; |
| 38 |
} |
| 39 |
|
| 40 |
impl Reset for FixedHashCore { |
| 41 |
fn reset(&mut self) { |
| 42 |
self.state = 0; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl UpdateCore for FixedHashCore { |
| 47 |
fn update_blocks(&mut self, blocks: &[Block<Self>]) { |
| 48 |
for block in blocks { |
| 49 |
self.state ^= u64::from_le_bytes(block.0); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl HashMarker for FixedHashCore {} |
| 55 |
|
| 56 |
impl OutputSizeUser for FixedHashCore { |
| 57 |
type OutputSize = U8; |
| 58 |
} |
| 59 |
|
| 60 |
impl FixedOutputCore for FixedHashCore { |
| 61 |
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { |
| 62 |
let block = buffer.pad_with_zeros(); |
| 63 |
self.state ^= u64::from_le_bytes(block.0); |
| 64 |
out.copy_from_slice(&self.state.to_le_bytes()); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl SerializableState for FixedHashCore { |
| 69 |
type SerializedStateSize = U8; |
| 70 |
|
| 71 |
fn serialize(&self) -> SerializedState<Self> { |
| 72 |
self.state.to_le_bytes().into() |
| 73 |
} |
| 74 |
|
| 75 |
fn deserialize( |
| 76 |
serialized_state: &SerializedState<Self>, |
| 77 |
) -> Result<Self, DeserializeStateError> { |
| 78 |
Ok(Self { |
| 79 |
state: u64::from_le_bytes(serialized_state.0), |
| 80 |
}) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl Drop for FixedHashCore { |
| 85 |
fn drop(&mut self) { |
| 86 |
#[cfg(feature = "zeroize")] |
| 87 |
self.state.zeroize(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
#[cfg(feature = "zeroize")] |
| 92 |
impl zeroize::ZeroizeOnDrop for FixedHashCore {} |
| 93 |
} |
| 94 |
|
| 95 |
digest::buffer_fixed!( |
| 96 |
/// Primitive XOR hasher for testing purposes |
| 97 |
pub struct FixedHash(block_api::FixedHashCore); |
| 98 |
impl: BaseFixedTraits Default Clone HashMarker Reset FixedOutputReset; |
| 99 |
); |
| 100 |
digest::buffer_fixed!( |
| 101 |
/// Primitive XOR hasher for testing purposes |
| 102 |
pub struct FixedHashWithSer(block_api::FixedHashCore); |
| 103 |
impl: FixedHashTraits; |
| 104 |
); |
| 105 |
digest::buffer_fixed!( |
| 106 |
/// Primitive XOR hasher for testing purposes |
| 107 |
pub struct FixedHashWithOid(block_api::FixedHashCore); |
| 108 |
oid: "0.1.2.3.4.5"; |
| 109 |
impl: BaseFixedTraits Default Clone HashMarker Reset FixedOutputReset; |
| 110 |
); |
| 111 |
digest::buffer_fixed!( |
| 112 |
/// Primitive XOR hasher for testing purposes |
| 113 |
pub struct FixedHashWithOidSer(block_api::FixedHashCore); |
| 114 |
oid: "0.1.2.3.4.5"; |
| 115 |
impl: FixedHashTraits; |
| 116 |
); |
| 117 |
|
| 118 |
#[cfg(feature = "dev")] |
| 119 |
digest::hash_serialization_test!(fixed_hash_serialization, FixedHashWithSer,); |
| 120 |
|
| 121 |
#[cfg(feature = "zeroize")] |
| 122 |
/// check for `ZeroizeOnDrop` implementations |
| 123 |
const _: () = { |
| 124 |
const fn check_zeroize<T: zeroize::ZeroizeOnDrop>() {} |
| 125 |
check_zeroize::<FixedHashWithSer>(); |
| 126 |
check_zeroize::<FixedHashWithOidSer>(); |
| 127 |
}; |