| 1 |
use core::fmt; |
| 2 |
use digest::{ |
| 3 |
HashMarker, Output, |
| 4 |
array::Array, |
| 5 |
block_api::{ |
| 6 |
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, FixedOutputCore, |
| 7 |
OutputSizeUser, Reset, UpdateCore, |
| 8 |
}, |
| 9 |
common::hazmat::{DeserializeStateError, SerializableState, SerializedState}, |
| 10 |
typenum::{U20, U28, U64, Unsigned}, |
| 11 |
}; |
| 12 |
|
| 13 |
#[cfg(feature = "zeroize")] |
| 14 |
use digest::zeroize::{Zeroize, ZeroizeOnDrop}; |
| 15 |
|
| 16 |
use crate::consts::{H0, State}; |
| 17 |
|
| 18 |
/// SHA-1 compression function |
| 19 |
pub fn compress(state: &mut State, blocks: &[[u8; 64]]) { |
| 20 |
crate::compress::compress(state, blocks); |
| 21 |
} |
| 22 |
|
| 23 |
/// Core SHA-1 hasher state. |
| 24 |
#[derive(Clone)] |
| 25 |
pub struct Sha1Core { |
| 26 |
h: State, |
| 27 |
block_len: u64, |
| 28 |
} |
| 29 |
|
| 30 |
impl HashMarker for Sha1Core {} |
| 31 |
|
| 32 |
impl BlockSizeUser for Sha1Core { |
| 33 |
type BlockSize = U64; |
| 34 |
} |
| 35 |
|
| 36 |
impl BufferKindUser for Sha1Core { |
| 37 |
type BufferKind = Eager; |
| 38 |
} |
| 39 |
|
| 40 |
impl OutputSizeUser for Sha1Core { |
| 41 |
type OutputSize = U20; |
| 42 |
} |
| 43 |
|
| 44 |
impl UpdateCore for Sha1Core { |
| 45 |
#[inline] |
| 46 |
fn update_blocks(&mut self, blocks: &[Block<Self>]) { |
| 47 |
self.block_len += blocks.len() as u64; |
| 48 |
let blocks = Array::cast_slice_to_core(blocks); |
| 49 |
compress(&mut self.h, blocks); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl FixedOutputCore for Sha1Core { |
| 54 |
#[inline] |
| 55 |
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { |
| 56 |
let bs = Self::BlockSize::U64; |
| 57 |
let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len); |
| 58 |
|
| 59 |
let mut h = self.h; |
| 60 |
buffer.len64_padding_be(bit_len, |b| compress(&mut h, &[b.0])); |
| 61 |
for (chunk, v) in out.chunks_exact_mut(4).zip(h.iter()) { |
| 62 |
chunk.copy_from_slice(&v.to_be_bytes()); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl Default for Sha1Core { |
| 68 |
#[inline] |
| 69 |
fn default() -> Self { |
| 70 |
Self { |
| 71 |
h: H0, |
| 72 |
block_len: 0, |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl Reset for Sha1Core { |
| 78 |
#[inline] |
| 79 |
fn reset(&mut self) { |
| 80 |
*self = Default::default(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl AlgorithmName for Sha1Core { |
| 85 |
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 86 |
f.write_str("Sha1") |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl fmt::Debug for Sha1Core { |
| 91 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 92 |
f.write_str("Sha1Core { ... }") |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl Drop for Sha1Core { |
| 97 |
fn drop(&mut self) { |
| 98 |
#[cfg(feature = "zeroize")] |
| 99 |
{ |
| 100 |
self.h.zeroize(); |
| 101 |
self.block_len.zeroize(); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
#[cfg(feature = "zeroize")] |
| 107 |
impl ZeroizeOnDrop for Sha1Core {} |
| 108 |
|
| 109 |
impl SerializableState for Sha1Core { |
| 110 |
type SerializedStateSize = U28; |
| 111 |
|
| 112 |
fn serialize(&self) -> SerializedState<Self> { |
| 113 |
let mut serialized_h = SerializedState::<Self>::default(); |
| 114 |
|
| 115 |
for (val, chunk) in self.h.iter().zip(serialized_h.chunks_exact_mut(4)) { |
| 116 |
chunk.copy_from_slice(&val.to_le_bytes()); |
| 117 |
} |
| 118 |
|
| 119 |
serialized_h[20..].copy_from_slice(&self.block_len.to_le_bytes()); |
| 120 |
serialized_h |
| 121 |
} |
| 122 |
|
| 123 |
fn deserialize( |
| 124 |
serialized_state: &SerializedState<Self>, |
| 125 |
) -> Result<Self, DeserializeStateError> { |
| 126 |
let (serialized_h, serialized_block_len) = serialized_state.split::<U20>(); |
| 127 |
|
| 128 |
let mut h = State::default(); |
| 129 |
for (val, chunk) in h.iter_mut().zip(serialized_h.chunks_exact(4)) { |
| 130 |
*val = u32::from_le_bytes(chunk.try_into().unwrap()); |
| 131 |
} |
| 132 |
|
| 133 |
let block_len = u64::from_le_bytes(*serialized_block_len.as_ref()); |
| 134 |
|
| 135 |
Ok(Self { h, block_len }) |
| 136 |
} |
| 137 |
} |