|
| 1 | +use crate::{ |
| 2 | + long, short, |
| 3 | + utils::{read_u64_le, write_u64_le, xor_bytes}, |
| 4 | +}; |
| 5 | +use core::fmt; |
| 6 | +use digest::{ |
| 7 | + HashMarker, InvalidOutputSize, Output, |
| 8 | + core_api::{ |
| 9 | + AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, OutputSizeUser, |
| 10 | + TruncSide, UpdateCore, VariableOutputCore, |
| 11 | + }, |
| 12 | + crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState}, |
| 13 | + typenum::{U32, U64, U72, U128, U136, Unsigned}, |
| 14 | +}; |
| 15 | + |
| 16 | +#[cfg(feature = "zeroize")] |
| 17 | +use digest::zeroize::{Zeroize, ZeroizeOnDrop}; |
| 18 | + |
| 19 | +/// Lowest-level core hasher state of the short Kupyna variant. |
| 20 | +#[derive(Clone)] |
| 21 | +pub struct KupynaShortVarCore { |
| 22 | + state: [u64; short::COLS], |
| 23 | + blocks_len: u64, |
| 24 | +} |
| 25 | + |
| 26 | +impl HashMarker for KupynaShortVarCore {} |
| 27 | + |
| 28 | +impl BlockSizeUser for KupynaShortVarCore { |
| 29 | + type BlockSize = U64; |
| 30 | +} |
| 31 | + |
| 32 | +impl BufferKindUser for KupynaShortVarCore { |
| 33 | + type BufferKind = Eager; |
| 34 | +} |
| 35 | + |
| 36 | +impl UpdateCore for KupynaShortVarCore { |
| 37 | + #[inline] |
| 38 | + fn update_blocks(&mut self, blocks: &[Block<Self>]) { |
| 39 | + self.blocks_len += blocks.len() as u64; |
| 40 | + for block in blocks { |
| 41 | + short::compress(&mut self.state, block.as_ref()); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl OutputSizeUser for KupynaShortVarCore { |
| 47 | + type OutputSize = U32; |
| 48 | +} |
| 49 | + |
| 50 | +impl VariableOutputCore for KupynaShortVarCore { |
| 51 | + const TRUNC_SIDE: TruncSide = TruncSide::Right; |
| 52 | + |
| 53 | + #[inline] |
| 54 | + fn new(output_size: usize) -> Result<Self, InvalidOutputSize> { |
| 55 | + if output_size > Self::OutputSize::USIZE { |
| 56 | + return Err(InvalidOutputSize); |
| 57 | + } |
| 58 | + let mut state = [0; short::COLS]; |
| 59 | + state[0] = 0x40; |
| 60 | + state[0] <<= 56; |
| 61 | + let blocks_len = 0; |
| 62 | + Ok(Self { state, blocks_len }) |
| 63 | + } |
| 64 | + |
| 65 | + #[inline] |
| 66 | + fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { |
| 67 | + let block_size = Self::BlockSize::USIZE as u128; |
| 68 | + let msg_len_bytes = (self.blocks_len as u128) * block_size + (buffer.get_pos() as u128); |
| 69 | + let msg_len_bits = 8 * msg_len_bytes; |
| 70 | + |
| 71 | + buffer.digest_pad(0x80, &msg_len_bits.to_le_bytes()[0..12], |block| { |
| 72 | + short::compress(&mut self.state, block.as_ref()); |
| 73 | + }); |
| 74 | + |
| 75 | + let mut state_u8 = [0u8; 64]; |
| 76 | + for (src, dst) in self.state.iter().zip(state_u8.chunks_exact_mut(8)) { |
| 77 | + dst.copy_from_slice(&src.to_be_bytes()); |
| 78 | + } |
| 79 | + |
| 80 | + // Call t_xor_l with u8 array |
| 81 | + let t_xor_ult_processed_block = short::t_xor_l(state_u8); |
| 82 | + |
| 83 | + let result_u8 = xor_bytes(state_u8, t_xor_ult_processed_block); |
| 84 | + |
| 85 | + // Convert result back to u64s |
| 86 | + let mut res = [0u64; 8]; |
| 87 | + for (dst, src) in res.iter_mut().zip(result_u8.chunks_exact(8)) { |
| 88 | + *dst = u64::from_be_bytes(src.try_into().unwrap()); |
| 89 | + } |
| 90 | + let n = short::COLS / 2; |
| 91 | + for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) { |
| 92 | + chunk.copy_from_slice(&v.to_be_bytes()); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl AlgorithmName for KupynaShortVarCore { |
| 98 | + #[inline] |
| 99 | + fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 100 | + f.write_str("KupynaShort") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl fmt::Debug for KupynaShortVarCore { |
| 105 | + #[inline] |
| 106 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 107 | + f.write_str("KupynaShortVarCore { ... }") |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl Drop for KupynaShortVarCore { |
| 112 | + #[inline] |
| 113 | + fn drop(&mut self) { |
| 114 | + #[cfg(feature = "zeroize")] |
| 115 | + { |
| 116 | + self.state.zeroize(); |
| 117 | + self.blocks_len.zeroize(); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl SerializableState for KupynaShortVarCore { |
| 123 | + type SerializedStateSize = U72; |
| 124 | + |
| 125 | + #[inline] |
| 126 | + fn serialize(&self) -> SerializedState<Self> { |
| 127 | + let mut serialized_state = SerializedState::<Self>::default(); |
| 128 | + let (state_dst, len_dst) = serialized_state.split_at_mut(64); |
| 129 | + write_u64_le(&self.state, state_dst); |
| 130 | + len_dst.copy_from_slice(&self.blocks_len.to_le_bytes()); |
| 131 | + serialized_state |
| 132 | + } |
| 133 | + |
| 134 | + #[inline] |
| 135 | + fn deserialize( |
| 136 | + serialized_state: &SerializedState<Self>, |
| 137 | + ) -> Result<Self, DeserializeStateError> { |
| 138 | + let (serialized_state, serialized_block_len) = serialized_state.split::<U64>(); |
| 139 | + Ok(Self { |
| 140 | + state: read_u64_le(&serialized_state.0), |
| 141 | + blocks_len: u64::from_le_bytes(serialized_block_len.0), |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[cfg(feature = "zeroize")] |
| 147 | +impl ZeroizeOnDrop for KupynaShortVarCore {} |
| 148 | + |
| 149 | +/// Lowest-level core hasher state of the long Kupyna variant. |
| 150 | +#[derive(Clone)] |
| 151 | +pub struct KupynaLongVarCore { |
| 152 | + state: [u64; long::COLS], |
| 153 | + blocks_len: u64, |
| 154 | +} |
| 155 | + |
| 156 | +impl HashMarker for KupynaLongVarCore {} |
| 157 | + |
| 158 | +impl BlockSizeUser for KupynaLongVarCore { |
| 159 | + type BlockSize = U128; |
| 160 | +} |
| 161 | + |
| 162 | +impl BufferKindUser for KupynaLongVarCore { |
| 163 | + type BufferKind = Eager; |
| 164 | +} |
| 165 | + |
| 166 | +impl UpdateCore for KupynaLongVarCore { |
| 167 | + #[inline] |
| 168 | + fn update_blocks(&mut self, blocks: &[Block<Self>]) { |
| 169 | + self.blocks_len += blocks.len() as u64; |
| 170 | + for block in blocks { |
| 171 | + long::compress(&mut self.state, block.as_ref()); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +impl OutputSizeUser for KupynaLongVarCore { |
| 177 | + type OutputSize = U64; |
| 178 | +} |
| 179 | + |
| 180 | +impl VariableOutputCore for KupynaLongVarCore { |
| 181 | + const TRUNC_SIDE: TruncSide = TruncSide::Right; |
| 182 | + |
| 183 | + #[inline] |
| 184 | + fn new(output_size: usize) -> Result<Self, InvalidOutputSize> { |
| 185 | + let min_size = Self::OutputSize::USIZE / 2; |
| 186 | + let max_size = Self::OutputSize::USIZE; |
| 187 | + if output_size < min_size || output_size > max_size { |
| 188 | + return Err(InvalidOutputSize); |
| 189 | + } |
| 190 | + let mut state = [0; long::COLS]; |
| 191 | + state[0] = 0x80; |
| 192 | + state[0] <<= 56; |
| 193 | + let blocks_len = 0; |
| 194 | + Ok(Self { state, blocks_len }) |
| 195 | + } |
| 196 | + |
| 197 | + #[inline] |
| 198 | + fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { |
| 199 | + let block_size = Self::BlockSize::USIZE as u128; |
| 200 | + let msg_len_bytes = (self.blocks_len as u128) * block_size + (buffer.get_pos() as u128); |
| 201 | + let msg_len_bits = 8 * msg_len_bytes; |
| 202 | + |
| 203 | + buffer.digest_pad(0x80, &msg_len_bits.to_le_bytes()[0..12], |block| { |
| 204 | + long::compress(&mut self.state, block.as_ref()); |
| 205 | + }); |
| 206 | + |
| 207 | + let mut state_u8 = [0u8; 128]; |
| 208 | + for (src, dst) in self.state.iter().zip(state_u8.chunks_exact_mut(8)) { |
| 209 | + dst.copy_from_slice(&src.to_be_bytes()); |
| 210 | + } |
| 211 | + |
| 212 | + // Call t_xor_l with u8 array |
| 213 | + let t_xor_ult_processed_block = long::t_xor_l(state_u8); |
| 214 | + |
| 215 | + let result_u8 = xor_bytes(state_u8, t_xor_ult_processed_block); |
| 216 | + |
| 217 | + // Convert result back to u64s |
| 218 | + let mut res = [0u64; 16]; |
| 219 | + for (dst, src) in res.iter_mut().zip(result_u8.chunks_exact(8)) { |
| 220 | + *dst = u64::from_be_bytes(src.try_into().unwrap()); |
| 221 | + } |
| 222 | + let n = long::COLS / 2; |
| 223 | + for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) { |
| 224 | + chunk.copy_from_slice(&v.to_be_bytes()); |
| 225 | + } |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +impl AlgorithmName for KupynaLongVarCore { |
| 230 | + #[inline] |
| 231 | + fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 232 | + f.write_str("KupynaLong") |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +impl fmt::Debug for KupynaLongVarCore { |
| 237 | + #[inline] |
| 238 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 239 | + f.write_str("KupynaLongVarCore { ... }") |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +impl Drop for KupynaLongVarCore { |
| 244 | + #[inline] |
| 245 | + fn drop(&mut self) { |
| 246 | + #[cfg(feature = "zeroize")] |
| 247 | + { |
| 248 | + self.state.zeroize(); |
| 249 | + self.blocks_len.zeroize(); |
| 250 | + } |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +impl SerializableState for KupynaLongVarCore { |
| 255 | + type SerializedStateSize = U136; |
| 256 | + |
| 257 | + #[inline] |
| 258 | + fn serialize(&self) -> SerializedState<Self> { |
| 259 | + let mut serialized_state = SerializedState::<Self>::default(); |
| 260 | + let (state_dst, len_dst) = serialized_state.split_at_mut(128); |
| 261 | + write_u64_le(&self.state, state_dst); |
| 262 | + len_dst.copy_from_slice(&self.blocks_len.to_le_bytes()); |
| 263 | + serialized_state |
| 264 | + } |
| 265 | + |
| 266 | + #[inline] |
| 267 | + fn deserialize( |
| 268 | + serialized_state: &SerializedState<Self>, |
| 269 | + ) -> Result<Self, DeserializeStateError> { |
| 270 | + let (serialized_state, serialized_block_len) = serialized_state.split::<U128>(); |
| 271 | + Ok(Self { |
| 272 | + state: read_u64_le(&serialized_state.0), |
| 273 | + blocks_len: u64::from_le_bytes(serialized_block_len.0), |
| 274 | + }) |
| 275 | + } |
| 276 | +} |
| 277 | + |
| 278 | +#[cfg(feature = "zeroize")] |
| 279 | +impl ZeroizeOnDrop for KupynaLongVarCore {} |
0 commit comments