Debian Codegraph @

debian/rust-vendor/sm3/src/block_api.rs

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