From f3a42793b2c4fd223a3e313ee270d8c269a1cf03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Thu, 11 Jan 2024 21:57:53 +0300 Subject: [PATCH] Simplify tiger --- tiger/src/lib.rs | 146 ++++++++++++----------------------------------- 1 file changed, 36 insertions(+), 110 deletions(-) diff --git a/tiger/src/lib.rs b/tiger/src/lib.rs index 2747800a..da7e3def 100644 --- a/tiger/src/lib.rs +++ b/tiger/src/lib.rs @@ -37,29 +37,31 @@ const S0: State = [ /// Core Tiger hasher state. #[derive(Clone)] -pub struct TigerCore { +pub struct TigerCore { block_len: u64, state: State, } /// Tiger hasher state. -pub type Tiger = CoreWrapper; +pub type Tiger = CoreWrapper>; +/// Tiger2 hasher state. +pub type Tiger2 = CoreWrapper>; -impl HashMarker for TigerCore {} +impl HashMarker for TigerCore {} -impl BlockSizeUser for TigerCore { +impl BlockSizeUser for TigerCore { type BlockSize = U64; } -impl BufferKindUser for TigerCore { +impl BufferKindUser for TigerCore { type BufferKind = Eager; } -impl OutputSizeUser for TigerCore { +impl OutputSizeUser for TigerCore { type OutputSize = U24; } -impl UpdateCore for TigerCore { +impl UpdateCore for TigerCore { #[inline] fn update_blocks(&mut self, blocks: &[Block]) { self.block_len += blocks.len() as u64; @@ -69,143 +71,67 @@ impl UpdateCore for TigerCore { } } -impl FixedOutputCore for TigerCore { +impl FixedOutputCore for TigerCore { #[inline] fn finalize_fixed_core(&mut self, buffer: &mut Buffer, out: &mut Output) { let bs = Self::BlockSize::U64; let pos = buffer.get_pos() as u64; let bit_len = 8 * (pos + bs * self.block_len); - buffer.digest_pad(1, &bit_len.to_le_bytes(), |b| { - compress(&mut self.state, b.as_ref()) - }); - for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) { - chunk.copy_from_slice(&v.to_le_bytes()); - } - } -} - -impl Default for TigerCore { - fn default() -> Self { - Self { - block_len: 0, - state: S0, - } - } -} - -impl Reset for TigerCore { - fn reset(&mut self) { - *self = Default::default(); - } -} - -impl AlgorithmName for TigerCore { - fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("Tiger") - } -} - -impl fmt::Debug for TigerCore { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("TigerCore { ... }") - } -} - -impl Drop for TigerCore { - fn drop(&mut self) { - #[cfg(feature = "zeroize")] - { - self.state.zeroize(); - self.block_len.zeroize(); + if IS_VER2 { + buffer.len64_padding_le(bit_len, |b| compress(&mut self.state, b.as_ref())); + } else { + buffer.digest_pad(1, &bit_len.to_le_bytes(), |b| { + compress(&mut self.state, b.as_ref()) + }); } - } -} - -#[cfg(feature = "zeroize")] -impl ZeroizeOnDrop for TigerCore {} -/// Core Tiger2 hasher state. -#[derive(Clone)] -pub struct Tiger2Core { - block_len: u64, - state: State, -} - -/// Tiger2 hasher state. -pub type Tiger2 = CoreWrapper; - -impl HashMarker for Tiger2Core {} - -impl BlockSizeUser for Tiger2Core { - type BlockSize = U64; -} - -impl BufferKindUser for Tiger2Core { - type BufferKind = Eager; -} - -impl OutputSizeUser for Tiger2Core { - type OutputSize = U24; -} - -impl UpdateCore for Tiger2Core { - #[inline] - fn update_blocks(&mut self, blocks: &[Block]) { - self.block_len += blocks.len() as u64; - for block in blocks { - compress(&mut self.state, block.as_ref()); - } - } -} - -impl FixedOutputCore for Tiger2Core { - #[inline] - fn finalize_fixed_core(&mut self, buffer: &mut Buffer, out: &mut Output) { - let bs = Self::BlockSize::U64; - let pos = buffer.get_pos() as u64; - let bit_len = 8 * (pos + bs * self.block_len); - - buffer.len64_padding_le(bit_len, |b| compress(&mut self.state, b.as_ref())); for (chunk, v) in out.chunks_exact_mut(8).zip(self.state.iter()) { chunk.copy_from_slice(&v.to_le_bytes()); } } } -impl Default for Tiger2Core { +impl Default for TigerCore { + #[inline] fn default() -> Self { Self { block_len: 0, - state: [ - 0x0123_4567_89AB_CDEF, - 0xFEDC_BA98_7654_3210, - 0xF096_A5B4_C3B2_E187, - ], + state: S0, } } } -impl Reset for Tiger2Core { +impl Reset for TigerCore { #[inline] fn reset(&mut self) { *self = Default::default(); } } -impl AlgorithmName for Tiger2Core { +impl AlgorithmName for TigerCore { + #[inline] fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("Tiger2") + if IS_VER2 { + f.write_str("Tiger2") + } else { + f.write_str("Tiger") + } } } -impl fmt::Debug for Tiger2Core { +impl fmt::Debug for TigerCore { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("Tiger2Core { ... }") + if IS_VER2 { + f.write_str("Tiger2Core { ... }") + } else { + f.write_str("TigerCore { ... }") + } } } -impl Drop for Tiger2Core { +impl Drop for TigerCore { + #[inline] fn drop(&mut self) { #[cfg(feature = "zeroize")] { @@ -216,4 +142,4 @@ impl Drop for Tiger2Core { } #[cfg(feature = "zeroize")] -impl ZeroizeOnDrop for Tiger2Core {} +impl ZeroizeOnDrop for TigerCore {}