|
| 1 | +use ascon::State; |
| 2 | +use digest::{ |
| 3 | + CustomizedInit, HashMarker, OutputSizeUser, |
| 4 | + block_api::{ |
| 5 | + AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, ExtendableOutputCore, |
| 6 | + UpdateCore, |
| 7 | + }, |
| 8 | + common::hazmat::{DeserializeStateError, SerializableState, SerializedState}, |
| 9 | + consts::{U8, U32, U40}, |
| 10 | +}; |
| 11 | + |
| 12 | +use super::{AsconXofReaderCore, init_state}; |
| 13 | + |
| 14 | +/// Maximum allowed length of customization strings in bits. |
| 15 | +const MAX_CUSTOM_LEN: usize = 2048; |
| 16 | + |
| 17 | +const IV: u64 = 0x0000_0800_00CC_0004; |
| 18 | +const INIT_STATE: State = init_state(IV); |
| 19 | + |
| 20 | +/// Ascon-CXOF128 block-level hasher |
| 21 | +#[derive(Clone, Debug)] |
| 22 | +pub struct AsconCxof128Core { |
| 23 | + state: State, |
| 24 | +} |
| 25 | + |
| 26 | +impl Default for AsconCxof128Core { |
| 27 | + #[inline] |
| 28 | + fn default() -> Self { |
| 29 | + Self::new_customized(&[]) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl CustomizedInit for AsconCxof128Core { |
| 34 | + #[inline] |
| 35 | + fn new_customized(customization: &[u8]) -> Self { |
| 36 | + let bit_len = 8 * customization.len(); |
| 37 | + assert!(bit_len < MAX_CUSTOM_LEN); |
| 38 | + let mut state = INIT_STATE; |
| 39 | + |
| 40 | + state[0] ^= u64::try_from(bit_len).expect("bit_len is smaller than MAX_CUSTOM_LEN"); |
| 41 | + |
| 42 | + ascon::permute12(&mut state); |
| 43 | + |
| 44 | + let mut blocks = customization.chunks_exact(size_of::<u64>()); |
| 45 | + for block in &mut blocks { |
| 46 | + let block = block.try_into().expect("block has correct length"); |
| 47 | + state[0] ^= u64::from_le_bytes(block); |
| 48 | + ascon::permute12(&mut state); |
| 49 | + } |
| 50 | + |
| 51 | + let last_block = blocks.remainder(); |
| 52 | + let len = last_block.len(); |
| 53 | + |
| 54 | + let mut buf = [0u8; 8]; |
| 55 | + buf[..len].copy_from_slice(last_block); |
| 56 | + |
| 57 | + let pad = 1u64 << (8 * len); |
| 58 | + state[0] ^= u64::from_le_bytes(buf) ^ pad; |
| 59 | + |
| 60 | + ascon::permute12(&mut state); |
| 61 | + |
| 62 | + Self { state } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl HashMarker for AsconCxof128Core {} |
| 67 | + |
| 68 | +impl BlockSizeUser for AsconCxof128Core { |
| 69 | + type BlockSize = U8; |
| 70 | +} |
| 71 | + |
| 72 | +impl BufferKindUser for AsconCxof128Core { |
| 73 | + type BufferKind = Eager; |
| 74 | +} |
| 75 | + |
| 76 | +impl OutputSizeUser for AsconCxof128Core { |
| 77 | + type OutputSize = U32; |
| 78 | +} |
| 79 | + |
| 80 | +impl UpdateCore for AsconCxof128Core { |
| 81 | + #[inline] |
| 82 | + fn update_blocks(&mut self, blocks: &[Block<Self>]) { |
| 83 | + for block in blocks { |
| 84 | + self.state[0] ^= u64::from_le_bytes(block.0); |
| 85 | + ascon::permute12(&mut self.state); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl ExtendableOutputCore for AsconCxof128Core { |
| 91 | + type ReaderCore = AsconXofReaderCore; |
| 92 | + |
| 93 | + fn finalize_xof_core(&mut self, buffer: &mut Buffer<Self>) -> Self::ReaderCore { |
| 94 | + let len = buffer.get_pos(); |
| 95 | + let last_block = buffer.pad_with_zeros(); |
| 96 | + let pad = 1u64 << (8 * len); |
| 97 | + self.state[0] ^= u64::from_le_bytes(last_block.0) ^ pad; |
| 98 | + |
| 99 | + AsconXofReaderCore { state: self.state } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl AlgorithmName for AsconCxof128Core { |
| 104 | + #[inline] |
| 105 | + fn write_alg_name(f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 106 | + f.write_str("Ascon-CXOF128") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl SerializableState for AsconCxof128Core { |
| 111 | + type SerializedStateSize = U40; |
| 112 | + |
| 113 | + #[inline] |
| 114 | + fn serialize(&self) -> SerializedState<Self> { |
| 115 | + let mut res = SerializedState::<Self>::default(); |
| 116 | + let mut chunks = res.chunks_exact_mut(size_of::<u64>()); |
| 117 | + for (src, dst) in self.state.iter().zip(&mut chunks) { |
| 118 | + dst.copy_from_slice(&src.to_le_bytes()); |
| 119 | + } |
| 120 | + assert!(chunks.into_remainder().is_empty()); |
| 121 | + res |
| 122 | + } |
| 123 | + |
| 124 | + #[inline] |
| 125 | + fn deserialize( |
| 126 | + serialized_state: &SerializedState<Self>, |
| 127 | + ) -> Result<Self, DeserializeStateError> { |
| 128 | + let state = core::array::from_fn(|i| { |
| 129 | + let n = size_of::<u64>(); |
| 130 | + let chunk = &serialized_state[n * i..][..n]; |
| 131 | + u64::from_le_bytes(chunk.try_into().expect("chunk has correct length")) |
| 132 | + }); |
| 133 | + Ok(Self { state }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl Drop for AsconCxof128Core { |
| 138 | + #[inline] |
| 139 | + fn drop(&mut self) { |
| 140 | + #[cfg(feature = "zeroize")] |
| 141 | + { |
| 142 | + use digest::zeroize::Zeroize; |
| 143 | + self.state.zeroize() |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +#[cfg(feature = "zeroize")] |
| 149 | +impl digest::zeroize::ZeroizeOnDrop for AsconCxof128Core {} |
0 commit comments