/rust/registry/src/index.crates.io-6f17d22bba15001f/hmac-0.12.1/src/optim.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::{get_der_key, IPAD, OPAD}; |
2 | | use core::{fmt, slice}; |
3 | | #[cfg(feature = "reset")] |
4 | | use digest::Reset; |
5 | | use digest::{ |
6 | | block_buffer::Eager, |
7 | | core_api::{ |
8 | | AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreProxy, CoreWrapper, |
9 | | FixedOutputCore, OutputSizeUser, UpdateCore, |
10 | | }, |
11 | | crypto_common::{Key, KeySizeUser}, |
12 | | generic_array::typenum::{IsLess, Le, NonZero, U256}, |
13 | | HashMarker, InvalidLength, KeyInit, MacMarker, Output, |
14 | | }; |
15 | | |
16 | | /// Generic HMAC instance. |
17 | | pub type Hmac<D> = CoreWrapper<HmacCore<D>>; |
18 | | |
19 | | /// Generic core HMAC instance, which operates over blocks. |
20 | | pub struct HmacCore<D> |
21 | | where |
22 | | D: CoreProxy, |
23 | | D::Core: HashMarker |
24 | | + UpdateCore |
25 | | + FixedOutputCore |
26 | | + BufferKindUser<BufferKind = Eager> |
27 | | + Default |
28 | | + Clone, |
29 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
30 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
31 | | { |
32 | | digest: D::Core, |
33 | | opad_digest: D::Core, |
34 | | #[cfg(feature = "reset")] |
35 | | ipad_digest: D::Core, |
36 | | } |
37 | | |
38 | | impl<D> Clone for HmacCore<D> |
39 | | where |
40 | | D: CoreProxy, |
41 | | D::Core: HashMarker |
42 | | + UpdateCore |
43 | | + FixedOutputCore |
44 | | + BufferKindUser<BufferKind = Eager> |
45 | | + Default |
46 | | + Clone, |
47 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
48 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
49 | | { |
50 | 12.4k | fn clone(&self) -> Self { |
51 | 12.4k | Self { |
52 | 12.4k | digest: self.digest.clone(), |
53 | 12.4k | opad_digest: self.opad_digest.clone(), |
54 | 12.4k | #[cfg(feature = "reset")] |
55 | 12.4k | ipad_digest: self.ipad_digest.clone(), |
56 | 12.4k | } |
57 | 12.4k | } <hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as core::clone::Clone>::clone Line | Count | Source | 50 | 7.16k | fn clone(&self) -> Self { | 51 | 7.16k | Self { | 52 | 7.16k | digest: self.digest.clone(), | 53 | 7.16k | opad_digest: self.opad_digest.clone(), | 54 | 7.16k | #[cfg(feature = "reset")] | 55 | 7.16k | ipad_digest: self.ipad_digest.clone(), | 56 | 7.16k | } | 57 | 7.16k | } |
<hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as core::clone::Clone>::clone Line | Count | Source | 50 | 5.32k | fn clone(&self) -> Self { | 51 | 5.32k | Self { | 52 | 5.32k | digest: self.digest.clone(), | 53 | 5.32k | opad_digest: self.opad_digest.clone(), | 54 | 5.32k | #[cfg(feature = "reset")] | 55 | 5.32k | ipad_digest: self.ipad_digest.clone(), | 56 | 5.32k | } | 57 | 5.32k | } |
|
58 | | } |
59 | | |
60 | | impl<D> MacMarker for HmacCore<D> |
61 | | where |
62 | | D: CoreProxy, |
63 | | D::Core: HashMarker |
64 | | + UpdateCore |
65 | | + FixedOutputCore |
66 | | + BufferKindUser<BufferKind = Eager> |
67 | | + Default |
68 | | + Clone, |
69 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
70 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
71 | | { |
72 | | } |
73 | | |
74 | | impl<D> BufferKindUser for HmacCore<D> |
75 | | where |
76 | | D: CoreProxy, |
77 | | D::Core: HashMarker |
78 | | + UpdateCore |
79 | | + FixedOutputCore |
80 | | + BufferKindUser<BufferKind = Eager> |
81 | | + Default |
82 | | + Clone, |
83 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
84 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
85 | | { |
86 | | type BufferKind = Eager; |
87 | | } |
88 | | |
89 | | impl<D> KeySizeUser for HmacCore<D> |
90 | | where |
91 | | D: CoreProxy, |
92 | | D::Core: HashMarker |
93 | | + UpdateCore |
94 | | + FixedOutputCore |
95 | | + BufferKindUser<BufferKind = Eager> |
96 | | + Default |
97 | | + Clone, |
98 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
99 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
100 | | { |
101 | | type KeySize = <<D as CoreProxy>::Core as BlockSizeUser>::BlockSize; |
102 | | } |
103 | | |
104 | | impl<D> BlockSizeUser for HmacCore<D> |
105 | | where |
106 | | D: CoreProxy, |
107 | | D::Core: HashMarker |
108 | | + UpdateCore |
109 | | + FixedOutputCore |
110 | | + BufferKindUser<BufferKind = Eager> |
111 | | + Default |
112 | | + Clone, |
113 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
114 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
115 | | { |
116 | | type BlockSize = <<D as CoreProxy>::Core as BlockSizeUser>::BlockSize; |
117 | | } |
118 | | |
119 | | impl<D> OutputSizeUser for HmacCore<D> |
120 | | where |
121 | | D: CoreProxy, |
122 | | D::Core: HashMarker |
123 | | + UpdateCore |
124 | | + FixedOutputCore |
125 | | + BufferKindUser<BufferKind = Eager> |
126 | | + Default |
127 | | + Clone, |
128 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
129 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
130 | | { |
131 | | type OutputSize = <<D as CoreProxy>::Core as OutputSizeUser>::OutputSize; |
132 | | } |
133 | | |
134 | | impl<D> KeyInit for HmacCore<D> |
135 | | where |
136 | | D: CoreProxy, |
137 | | D::Core: HashMarker |
138 | | + UpdateCore |
139 | | + FixedOutputCore |
140 | | + BufferKindUser<BufferKind = Eager> |
141 | | + Default |
142 | | + Clone, |
143 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
144 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
145 | | { |
146 | | #[inline(always)] |
147 | | fn new(key: &Key<Self>) -> Self { |
148 | | Self::new_from_slice(key.as_slice()).unwrap() |
149 | | } |
150 | | |
151 | | #[inline(always)] |
152 | 9.36k | fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> { |
153 | 9.36k | let mut buf = get_der_key::<CoreWrapper<D::Core>>(key); |
154 | 599k | for b in buf.iter_mut() { |
155 | 599k | *b ^= IPAD; |
156 | 599k | } |
157 | 9.36k | let mut digest = D::Core::default(); |
158 | 9.36k | digest.update_blocks(slice::from_ref(&buf)); |
159 | | |
160 | 599k | for b in buf.iter_mut() { |
161 | 599k | *b ^= IPAD ^ OPAD; |
162 | 599k | } |
163 | | |
164 | 9.36k | let mut opad_digest = D::Core::default(); |
165 | 9.36k | opad_digest.update_blocks(slice::from_ref(&buf)); |
166 | 9.36k | |
167 | 9.36k | Ok(Self { |
168 | 9.36k | #[cfg(feature = "reset")] |
169 | 9.36k | ipad_digest: digest.clone(), |
170 | 9.36k | opad_digest, |
171 | 9.36k | digest, |
172 | 9.36k | }) |
173 | 9.36k | } <hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as crypto_common::KeyInit>::new_from_slice Line | Count | Source | 152 | 5.37k | fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> { | 153 | 5.37k | let mut buf = get_der_key::<CoreWrapper<D::Core>>(key); | 154 | 343k | for b in buf.iter_mut() { | 155 | 343k | *b ^= IPAD; | 156 | 343k | } | 157 | 5.37k | let mut digest = D::Core::default(); | 158 | 5.37k | digest.update_blocks(slice::from_ref(&buf)); | 159 | | | 160 | 343k | for b in buf.iter_mut() { | 161 | 343k | *b ^= IPAD ^ OPAD; | 162 | 343k | } | 163 | | | 164 | 5.37k | let mut opad_digest = D::Core::default(); | 165 | 5.37k | opad_digest.update_blocks(slice::from_ref(&buf)); | 166 | 5.37k | | 167 | 5.37k | Ok(Self { | 168 | 5.37k | #[cfg(feature = "reset")] | 169 | 5.37k | ipad_digest: digest.clone(), | 170 | 5.37k | opad_digest, | 171 | 5.37k | digest, | 172 | 5.37k | }) | 173 | 5.37k | } |
<hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as crypto_common::KeyInit>::new_from_slice Line | Count | Source | 152 | 3.99k | fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> { | 153 | 3.99k | let mut buf = get_der_key::<CoreWrapper<D::Core>>(key); | 154 | 255k | for b in buf.iter_mut() { | 155 | 255k | *b ^= IPAD; | 156 | 255k | } | 157 | 3.99k | let mut digest = D::Core::default(); | 158 | 3.99k | digest.update_blocks(slice::from_ref(&buf)); | 159 | | | 160 | 255k | for b in buf.iter_mut() { | 161 | 255k | *b ^= IPAD ^ OPAD; | 162 | 255k | } | 163 | | | 164 | 3.99k | let mut opad_digest = D::Core::default(); | 165 | 3.99k | opad_digest.update_blocks(slice::from_ref(&buf)); | 166 | 3.99k | | 167 | 3.99k | Ok(Self { | 168 | 3.99k | #[cfg(feature = "reset")] | 169 | 3.99k | ipad_digest: digest.clone(), | 170 | 3.99k | opad_digest, | 171 | 3.99k | digest, | 172 | 3.99k | }) | 173 | 3.99k | } |
|
174 | | } |
175 | | |
176 | | impl<D> UpdateCore for HmacCore<D> |
177 | | where |
178 | | D: CoreProxy, |
179 | | D::Core: HashMarker |
180 | | + UpdateCore |
181 | | + FixedOutputCore |
182 | | + BufferKindUser<BufferKind = Eager> |
183 | | + Default |
184 | | + Clone, |
185 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
186 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
187 | | { |
188 | | #[inline(always)] |
189 | 41 | fn update_blocks(&mut self, blocks: &[Block<Self>]) { |
190 | 41 | self.digest.update_blocks(blocks); |
191 | 41 | } <hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as digest::core_api::UpdateCore>::update_blocks Line | Count | Source | 189 | 20 | fn update_blocks(&mut self, blocks: &[Block<Self>]) { | 190 | 20 | self.digest.update_blocks(blocks); | 191 | 20 | } |
<hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as digest::core_api::UpdateCore>::update_blocks Line | Count | Source | 189 | 21 | fn update_blocks(&mut self, blocks: &[Block<Self>]) { | 190 | 21 | self.digest.update_blocks(blocks); | 191 | 21 | } |
|
192 | | } |
193 | | |
194 | | impl<D> FixedOutputCore for HmacCore<D> |
195 | | where |
196 | | D: CoreProxy, |
197 | | D::Core: HashMarker |
198 | | + UpdateCore |
199 | | + FixedOutputCore |
200 | | + BufferKindUser<BufferKind = Eager> |
201 | | + Default |
202 | | + Clone, |
203 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
204 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
205 | | { |
206 | | #[inline(always)] |
207 | 14.0k | fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { |
208 | 14.0k | let mut hash = Output::<D::Core>::default(); |
209 | 14.0k | self.digest.finalize_fixed_core(buffer, &mut hash); |
210 | 14.0k | // finalize_fixed_core should reset the buffer as well, but |
211 | 14.0k | // to be extra safe we reset it explicitly again. |
212 | 14.0k | buffer.reset(); |
213 | 14.0k | #[cfg(not(feature = "reset"))] |
214 | 14.0k | let h = &mut self.opad_digest; |
215 | 14.0k | #[cfg(feature = "reset")] |
216 | 14.0k | let mut h = self.opad_digest.clone(); |
217 | 14.0k | buffer.digest_blocks(&hash, |b| h.update_blocks(b)); Unexecuted instantiation: <hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as digest::core_api::FixedOutputCore>::finalize_fixed_core::{closure#0} Unexecuted instantiation: <hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as digest::core_api::FixedOutputCore>::finalize_fixed_core::{closure#0} |
218 | 14.0k | h.finalize_fixed_core(buffer, out); |
219 | 14.0k | } <hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as digest::core_api::FixedOutputCore>::finalize_fixed_core Line | Count | Source | 207 | 8.05k | fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { | 208 | 8.05k | let mut hash = Output::<D::Core>::default(); | 209 | 8.05k | self.digest.finalize_fixed_core(buffer, &mut hash); | 210 | 8.05k | // finalize_fixed_core should reset the buffer as well, but | 211 | 8.05k | // to be extra safe we reset it explicitly again. | 212 | 8.05k | buffer.reset(); | 213 | 8.05k | #[cfg(not(feature = "reset"))] | 214 | 8.05k | let h = &mut self.opad_digest; | 215 | 8.05k | #[cfg(feature = "reset")] | 216 | 8.05k | let mut h = self.opad_digest.clone(); | 217 | 8.05k | buffer.digest_blocks(&hash, |b| h.update_blocks(b)); | 218 | 8.05k | h.finalize_fixed_core(buffer, out); | 219 | 8.05k | } |
<hmac::optim::HmacCore<digest::core_api::wrapper::CoreWrapper<digest::core_api::ct_variable::CtVariableCoreWrapper<sha2::core_api::Sha256VarCore, typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UInt<typenum::uint::UTerm, typenum::bit::B1>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, typenum::bit::B0>, sha2::OidSha256>>> as digest::core_api::FixedOutputCore>::finalize_fixed_core Line | Count | Source | 207 | 5.99k | fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) { | 208 | 5.99k | let mut hash = Output::<D::Core>::default(); | 209 | 5.99k | self.digest.finalize_fixed_core(buffer, &mut hash); | 210 | 5.99k | // finalize_fixed_core should reset the buffer as well, but | 211 | 5.99k | // to be extra safe we reset it explicitly again. | 212 | 5.99k | buffer.reset(); | 213 | 5.99k | #[cfg(not(feature = "reset"))] | 214 | 5.99k | let h = &mut self.opad_digest; | 215 | 5.99k | #[cfg(feature = "reset")] | 216 | 5.99k | let mut h = self.opad_digest.clone(); | 217 | 5.99k | buffer.digest_blocks(&hash, |b| h.update_blocks(b)); | 218 | 5.99k | h.finalize_fixed_core(buffer, out); | 219 | 5.99k | } |
|
220 | | } |
221 | | |
222 | | #[cfg(feature = "reset")] |
223 | | #[cfg_attr(docsrs, doc(cfg(feature = "reset")))] |
224 | | impl<D> Reset for HmacCore<D> |
225 | | where |
226 | | D: CoreProxy, |
227 | | D::Core: HashMarker |
228 | | + UpdateCore |
229 | | + FixedOutputCore |
230 | | + BufferKindUser<BufferKind = Eager> |
231 | | + Default |
232 | | + Clone, |
233 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
234 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
235 | | { |
236 | | #[inline(always)] |
237 | | fn reset(&mut self) { |
238 | | self.digest = self.ipad_digest.clone(); |
239 | | } |
240 | | } |
241 | | |
242 | | impl<D> AlgorithmName for HmacCore<D> |
243 | | where |
244 | | D: CoreProxy, |
245 | | D::Core: HashMarker |
246 | | + AlgorithmName |
247 | | + UpdateCore |
248 | | + FixedOutputCore |
249 | | + BufferKindUser<BufferKind = Eager> |
250 | | + Default |
251 | | + Clone, |
252 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
253 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
254 | | { |
255 | | fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result { |
256 | | f.write_str("Hmac<")?; |
257 | | <D::Core as AlgorithmName>::write_alg_name(f)?; |
258 | | f.write_str(">") |
259 | | } |
260 | | } |
261 | | |
262 | | impl<D> fmt::Debug for HmacCore<D> |
263 | | where |
264 | | D: CoreProxy, |
265 | | D::Core: HashMarker |
266 | | + AlgorithmName |
267 | | + UpdateCore |
268 | | + FixedOutputCore |
269 | | + BufferKindUser<BufferKind = Eager> |
270 | | + Default |
271 | | + Clone, |
272 | | <D::Core as BlockSizeUser>::BlockSize: IsLess<U256>, |
273 | | Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero, |
274 | | { |
275 | | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
276 | | f.write_str("HmacCore<")?; |
277 | | <D::Core as AlgorithmName>::write_alg_name(f)?; |
278 | | f.write_str("> { ... }") |
279 | | } |
280 | | } |