/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sha1-0.11.0/src/block_api.rs
Line | Count | Source |
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 | 0 | pub fn compress(state: &mut State, blocks: &[[u8; 64]]) { |
20 | 0 | crate::compress::compress(state, blocks); |
21 | 0 | } |
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 | 0 | fn update_blocks(&mut self, blocks: &[Block<Self>]) { |
47 | 0 | self.block_len += blocks.len() as u64; |
48 | 0 | let blocks = Array::cast_slice_to_core(blocks); |
49 | 0 | compress(&mut self.h, blocks); |
50 | 0 | } Unexecuted instantiation: <sha1::block_api::Sha1Core as digest::block_api::UpdateCore>::update_blocks Unexecuted instantiation: <sha1::block_api::Sha1Core as digest::block_api::UpdateCore>::update_blocks |
51 | | } |
52 | | |
53 | | impl FixedOutputCore for Sha1Core { |
54 | | #[inline] |
55 | 0 | fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { |
56 | 0 | let bs = Self::BlockSize::U64; |
57 | 0 | let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len); |
58 | | |
59 | 0 | let mut h = self.h; |
60 | 0 | buffer.len64_padding_be(bit_len, |b| compress(&mut h, &[b.0])); Unexecuted instantiation: <sha1::block_api::Sha1Core as digest::block_api::FixedOutputCore>::finalize_fixed_core::{closure#0}Unexecuted instantiation: <sha1::block_api::Sha1Core as digest::block_api::FixedOutputCore>::finalize_fixed_core::{closure#0} |
61 | 0 | for (chunk, v) in out.chunks_exact_mut(4).zip(h.iter()) { |
62 | 0 | chunk.copy_from_slice(&v.to_be_bytes()); |
63 | 0 | } |
64 | 0 | } Unexecuted instantiation: <sha1::block_api::Sha1Core as digest::block_api::FixedOutputCore>::finalize_fixed_core Unexecuted instantiation: <sha1::block_api::Sha1Core as digest::block_api::FixedOutputCore>::finalize_fixed_core |
65 | | } |
66 | | |
67 | | impl Default for Sha1Core { |
68 | | #[inline] |
69 | 0 | fn default() -> Self { |
70 | 0 | Self { |
71 | 0 | h: H0, |
72 | 0 | block_len: 0, |
73 | 0 | } |
74 | 0 | } Unexecuted instantiation: <sha1::block_api::Sha1Core as core::default::Default>::default Unexecuted instantiation: <sha1::block_api::Sha1Core as core::default::Default>::default |
75 | | } |
76 | | |
77 | | impl Reset for Sha1Core { |
78 | | #[inline] |
79 | 0 | fn reset(&mut self) { |
80 | 0 | *self = Default::default(); |
81 | 0 | } |
82 | | } |
83 | | |
84 | | impl AlgorithmName for Sha1Core { |
85 | 0 | fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { |
86 | 0 | f.write_str("Sha1") |
87 | 0 | } |
88 | | } |
89 | | |
90 | | impl fmt::Debug for Sha1Core { |
91 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
92 | 0 | f.write_str("Sha1Core { ... }") |
93 | 0 | } |
94 | | } |
95 | | |
96 | | impl Drop for Sha1Core { |
97 | 0 | fn drop(&mut self) { |
98 | | #[cfg(feature = "zeroize")] |
99 | | { |
100 | | self.h.zeroize(); |
101 | | self.block_len.zeroize(); |
102 | | } |
103 | 0 | } |
104 | | } |
105 | | |
106 | | #[cfg(feature = "zeroize")] |
107 | | impl ZeroizeOnDrop for Sha1Core {} |
108 | | |
109 | | impl SerializableState for Sha1Core { |
110 | | type SerializedStateSize = U28; |
111 | | |
112 | 0 | fn serialize(&self) -> SerializedState<Self> { |
113 | 0 | let mut serialized_h = SerializedState::<Self>::default(); |
114 | | |
115 | 0 | for (val, chunk) in self.h.iter().zip(serialized_h.chunks_exact_mut(4)) { |
116 | 0 | chunk.copy_from_slice(&val.to_le_bytes()); |
117 | 0 | } |
118 | | |
119 | 0 | serialized_h[20..].copy_from_slice(&self.block_len.to_le_bytes()); |
120 | 0 | serialized_h |
121 | 0 | } |
122 | | |
123 | 0 | fn deserialize( |
124 | 0 | serialized_state: &SerializedState<Self>, |
125 | 0 | ) -> Result<Self, DeserializeStateError> { |
126 | 0 | let (serialized_h, serialized_block_len) = serialized_state.split::<U20>(); |
127 | | |
128 | 0 | let mut h = State::default(); |
129 | 0 | for (val, chunk) in h.iter_mut().zip(serialized_h.chunks_exact(4)) { |
130 | 0 | *val = u32::from_le_bytes(chunk.try_into().unwrap()); |
131 | 0 | } |
132 | | |
133 | 0 | let block_len = u64::from_le_bytes(*serialized_block_len.as_ref()); |
134 | | |
135 | 0 | Ok(Self { h, block_len }) |
136 | 0 | } |
137 | | } |