1use self::{
22 dynstate::DynState,
23 sha2::{SHA256_BLOCK_LEN, SHA512_BLOCK_LEN},
24};
25use crate::{
26 bits::{BitLength, FromByteLen as _},
27 cpu, debug, error,
28 polyfill::{self, slice, sliceutil},
29};
30use core::num::Wrapping;
31
32pub(crate) use self::finish_error::FinishError;
33
34mod dynstate;
35mod sha1;
36mod sha2;
37
38#[derive(Clone)]
39pub(crate) struct BlockContext {
40 state: DynState,
41
42 completed_bytes: u64,
46
47 pub algorithm: &'static Algorithm,
49}
50
51impl BlockContext {
52 pub(crate) fn new(algorithm: &'static Algorithm) -> Self {
53 Self {
54 state: algorithm.initial_state.clone(),
55 completed_bytes: 0,
56 algorithm,
57 }
58 }
59
60 pub(crate) fn update<'i>(&mut self, input: &'i [u8], cpu_features: cpu::Features) -> &'i [u8] {
63 let (completed_bytes, leftover) = self.block_data_order(input, cpu_features);
64 self.completed_bytes = self
69 .completed_bytes
70 .saturating_add(polyfill::u64_from_usize(completed_bytes));
71 leftover
72 }
73
74 pub(crate) fn try_finish(
80 mut self,
81 block: &mut [u8; MAX_BLOCK_LEN],
82 num_pending: usize,
83 cpu_features: cpu::Features,
84 ) -> Result<Digest, FinishError> {
85 let completed_bits = self
86 .completed_bytes
87 .checked_add(polyfill::u64_from_usize(num_pending))
88 .ok_or_else(|| {
89 InputTooLongError::new(self.completed_bytes)
91 })
92 .and_then(BitLength::from_byte_len)
93 .map_err(FinishError::input_too_long)?;
94
95 let block_len = self.algorithm.block_len();
96 let block = &mut block[..block_len];
97
98 let padding = match block.get_mut(num_pending..) {
99 Some([separator, padding @ ..]) => {
100 *separator = 0x80;
101 padding
102 }
103 unreachable => {
105 return Err(FinishError::pending_not_a_partial_block(
106 unreachable.as_deref(),
107 ));
108 }
109 };
110
111 let padding = match padding
112 .len()
113 .checked_sub(self.algorithm.block_len.len_len())
114 {
115 Some(_) => padding,
116 None => {
117 padding.fill(0);
118 let (completed_bytes, leftover) = self.block_data_order(block, cpu_features);
119 debug_assert_eq!((completed_bytes, leftover.len()), (block_len, 0));
120 &mut block[..]
123 }
124 };
125
126 let (to_zero, len) = padding.split_at_mut(padding.len() - 8);
127 to_zero.fill(0);
128 len.copy_from_slice(&completed_bits.to_be_bytes());
129
130 let (completed_bytes, leftover) = self.block_data_order(block, cpu_features);
131 debug_assert_eq!((completed_bytes, leftover.len()), (block_len, 0));
132
133 Ok(Digest {
134 algorithm: self.algorithm,
135 value: self.state.format_output(),
136 })
137 }
138
139 #[must_use]
140 fn block_data_order<'d>(
141 &mut self,
142 data: &'d [u8],
143 cpu_features: cpu::Features,
144 ) -> (usize, &'d [u8]) {
145 (self.algorithm.block_data_order)(&mut self.state, data, cpu_features)
146 }
147}
148
149pub(crate) type InputTooLongError = error::InputTooLongError<u64>;
150
151cold_exhaustive_error! {
152 enum finish_error::FinishError {
153 input_too_long => InputTooLong(InputTooLongError),
154 pending_not_a_partial_block_inner => PendingNotAPartialBlock(usize),
155 }
156}
157
158impl FinishError {
159 #[cold]
160 #[inline(never)]
161 fn pending_not_a_partial_block(padding: Option<&[u8]>) -> Self {
162 match padding {
163 None => Self::pending_not_a_partial_block_inner(0),
164 Some(padding) => Self::pending_not_a_partial_block_inner(padding.len()),
165 }
166 }
167}
168
169#[derive(Clone)]
187pub struct Context {
188 block: BlockContext,
189 pending: [u8; MAX_BLOCK_LEN],
191
192 num_pending: usize,
194}
195
196impl Context {
197 pub fn new(algorithm: &'static Algorithm) -> Self {
199 Self {
200 block: BlockContext::new(algorithm),
201 pending: [0u8; MAX_BLOCK_LEN],
202 num_pending: 0,
203 }
204 }
205
206 pub(crate) fn clone_from(block: &BlockContext) -> Self {
207 Self {
208 block: block.clone(),
209 pending: [0u8; MAX_BLOCK_LEN],
210 num_pending: 0,
211 }
212 }
213
214 pub fn update(&mut self, data: &[u8]) {
216 let cpu_features = cpu::features();
217
218 let block_len = self.block.algorithm.block_len();
219 let buffer = &mut self.pending[..block_len];
220
221 let to_digest = if self.num_pending == 0 {
222 data
223 } else {
224 let buffer_to_fill = match buffer.get_mut(self.num_pending..) {
225 Some(buffer_to_fill) => buffer_to_fill,
226 None => {
227 unreachable!();
229 }
230 };
231 sliceutil::overwrite_at_start(buffer_to_fill, data);
232 match slice::split_at_checked(data, buffer_to_fill.len()) {
233 Some((just_copied, to_digest)) => {
234 debug_assert_eq!(buffer_to_fill.len(), just_copied.len());
235 debug_assert_eq!(self.num_pending + just_copied.len(), block_len);
236 let leftover = self.block.update(buffer, cpu_features);
237 debug_assert_eq!(leftover.len(), 0);
238 self.num_pending = 0;
239 to_digest
240 }
241 None => {
242 self.num_pending += data.len();
243 debug_assert!(self.num_pending < block_len);
245 return;
246 }
247 }
248 };
249
250 let leftover = self.block.update(to_digest, cpu_features);
251 sliceutil::overwrite_at_start(buffer, leftover);
252 self.num_pending = leftover.len();
253 debug_assert!(self.num_pending < block_len);
254 }
255
256 pub fn finish(self) -> Digest {
261 let cpu = cpu::features();
262 self.try_finish(cpu)
263 .map_err(error::erase::<InputTooLongError>)
264 .unwrap()
265 }
266
267 pub(crate) fn try_finish(
268 mut self,
269 cpu_features: cpu::Features,
270 ) -> Result<Digest, InputTooLongError> {
271 self.block
272 .try_finish(&mut self.pending, self.num_pending, cpu_features)
273 .map_err(|err| match err {
274 FinishError::InputTooLong(i) => i,
275 FinishError::PendingNotAPartialBlock(_) => {
276 unreachable!()
278 }
279 })
280 }
281
282 #[inline(always)]
284 pub fn algorithm(&self) -> &'static Algorithm {
285 self.block.algorithm
286 }
287}
288
289pub fn digest(algorithm: &'static Algorithm, data: &[u8]) -> Digest {
291 let cpu = cpu::features();
292 Digest::compute_from(algorithm, data, cpu)
293 .map_err(error::erase::<InputTooLongError>)
294 .unwrap()
295}
296
297#[derive(Clone, Copy)]
301pub struct Digest {
302 value: Output,
303 algorithm: &'static Algorithm,
304}
305
306impl Digest {
307 pub(crate) fn compute_from(
308 algorithm: &'static Algorithm,
309 data: &[u8],
310 cpu: cpu::Features,
311 ) -> Result<Self, InputTooLongError> {
312 let mut ctx = Context::new(algorithm);
313 ctx.update(data);
314 ctx.try_finish(cpu)
315 }
316
317 #[inline(always)]
319 pub fn algorithm(&self) -> &'static Algorithm {
320 self.algorithm
321 }
322}
323
324impl AsRef<[u8]> for Digest {
325 #[inline(always)]
326 fn as_ref(&self) -> &[u8] {
327 &self.value.0[..self.algorithm.output_len()]
328 }
329}
330
331impl core::fmt::Debug for Digest {
332 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
333 write!(fmt, "{:?}:", self.algorithm)?;
334 debug::write_hex_bytes(fmt, self.as_ref())
335 }
336}
337
338pub struct Algorithm {
340 output_len: OutputLen,
341 chaining_len: usize,
342 block_len: BlockLen,
343
344 block_data_order: for<'d> fn(
348 state: &mut DynState,
349 data: &'d [u8],
350 cpu_features: cpu::Features,
351 ) -> (usize, &'d [u8]),
352
353 initial_state: DynState,
354
355 id: AlgorithmID,
356}
357
358#[derive(Debug, Eq, PartialEq)]
359enum AlgorithmID {
360 SHA1,
361 SHA256,
362 SHA384,
363 SHA512,
364 SHA512_256,
365}
366
367impl PartialEq for Algorithm {
368 fn eq(&self, other: &Self) -> bool {
369 self.id == other.id
370 }
371}
372
373impl Eq for Algorithm {}
374
375derive_debug_via_id!(Algorithm);
376
377impl Algorithm {
378 pub fn block_len(&self) -> usize {
380 self.block_len.into()
381 }
382
383 pub fn chaining_len(&self) -> usize {
391 self.chaining_len
392 }
393
394 pub fn output_len(&self) -> usize {
396 self.output_len.into()
397 }
398}
399
400pub static SHA1_FOR_LEGACY_USE_ONLY: Algorithm = Algorithm {
404 output_len: sha1::OUTPUT_LEN,
405 chaining_len: sha1::CHAINING_LEN,
406 block_len: sha1::BLOCK_LEN,
407 block_data_order: dynstate::sha1_block_data_order,
408 initial_state: DynState::new32([
409 Wrapping(0x67452301u32),
410 Wrapping(0xefcdab89u32),
411 Wrapping(0x98badcfeu32),
412 Wrapping(0x10325476u32),
413 Wrapping(0xc3d2e1f0u32),
414 Wrapping(0),
415 Wrapping(0),
416 Wrapping(0),
417 ]),
418 id: AlgorithmID::SHA1,
419};
420
421pub static SHA256: Algorithm = Algorithm {
425 output_len: OutputLen::_256,
426 chaining_len: SHA256_OUTPUT_LEN,
427 block_len: SHA256_BLOCK_LEN,
428 block_data_order: dynstate::sha256_block_data_order,
429 initial_state: DynState::new32([
430 Wrapping(0x6a09e667u32),
431 Wrapping(0xbb67ae85u32),
432 Wrapping(0x3c6ef372u32),
433 Wrapping(0xa54ff53au32),
434 Wrapping(0x510e527fu32),
435 Wrapping(0x9b05688cu32),
436 Wrapping(0x1f83d9abu32),
437 Wrapping(0x5be0cd19u32),
438 ]),
439 id: AlgorithmID::SHA256,
440};
441
442pub static SHA384: Algorithm = Algorithm {
446 output_len: OutputLen::_384,
447 chaining_len: SHA512_OUTPUT_LEN,
448 block_len: SHA512_BLOCK_LEN,
449 block_data_order: dynstate::sha512_block_data_order,
450 initial_state: DynState::new64([
451 Wrapping(0xcbbb9d5dc1059ed8),
452 Wrapping(0x629a292a367cd507),
453 Wrapping(0x9159015a3070dd17),
454 Wrapping(0x152fecd8f70e5939),
455 Wrapping(0x67332667ffc00b31),
456 Wrapping(0x8eb44a8768581511),
457 Wrapping(0xdb0c2e0d64f98fa7),
458 Wrapping(0x47b5481dbefa4fa4),
459 ]),
460 id: AlgorithmID::SHA384,
461};
462
463pub static SHA512: Algorithm = Algorithm {
467 output_len: OutputLen::_512,
468 chaining_len: SHA512_OUTPUT_LEN,
469 block_len: SHA512_BLOCK_LEN,
470 block_data_order: dynstate::sha512_block_data_order,
471 initial_state: DynState::new64([
472 Wrapping(0x6a09e667f3bcc908),
473 Wrapping(0xbb67ae8584caa73b),
474 Wrapping(0x3c6ef372fe94f82b),
475 Wrapping(0xa54ff53a5f1d36f1),
476 Wrapping(0x510e527fade682d1),
477 Wrapping(0x9b05688c2b3e6c1f),
478 Wrapping(0x1f83d9abfb41bd6b),
479 Wrapping(0x5be0cd19137e2179),
480 ]),
481 id: AlgorithmID::SHA512,
482};
483
484pub static SHA512_256: Algorithm = Algorithm {
492 output_len: OutputLen::_256,
493 chaining_len: SHA512_OUTPUT_LEN,
494 block_len: SHA512_BLOCK_LEN,
495 block_data_order: dynstate::sha512_block_data_order,
496 initial_state: DynState::new64([
497 Wrapping(0x22312194fc2bf72c),
498 Wrapping(0x9f555fa3c84c64c2),
499 Wrapping(0x2393b86b6f53b151),
500 Wrapping(0x963877195940eabd),
501 Wrapping(0x96283ee2a88effe3),
502 Wrapping(0xbe5e1e2553863992),
503 Wrapping(0x2b0199fc2c85b8aa),
504 Wrapping(0x0eb72ddc81c52ca2),
505 ]),
506 id: AlgorithmID::SHA512_256,
507};
508
509#[derive(Clone, Copy)]
510struct Output([u8; MAX_OUTPUT_LEN]);
511
512pub const MAX_BLOCK_LEN: usize = BlockLen::MAX.into();
515
516pub const MAX_OUTPUT_LEN: usize = OutputLen::MAX.into();
519
520pub const MAX_CHAINING_LEN: usize = MAX_OUTPUT_LEN;
523
524#[inline]
525fn format_output<T, F, const N: usize>(input: [Wrapping<T>; sha2::CHAINING_WORDS], f: F) -> Output
526where
527 F: Fn(T) -> [u8; N],
528 T: Copy,
529{
530 let mut output = Output([0; MAX_OUTPUT_LEN]);
531 output
532 .0
533 .chunks_mut(N)
534 .zip(input.iter().copied().map(|Wrapping(w)| f(w)))
535 .for_each(|(o, i)| {
536 o.copy_from_slice(&i);
537 });
538 output
539}
540
541pub const SHA1_OUTPUT_LEN: usize = sha1::OUTPUT_LEN.into();
543
544pub const SHA256_OUTPUT_LEN: usize = OutputLen::_256.into();
546
547pub const SHA384_OUTPUT_LEN: usize = OutputLen::_384.into();
549
550pub const SHA512_OUTPUT_LEN: usize = OutputLen::_512.into();
552
553pub const SHA512_256_OUTPUT_LEN: usize = OutputLen::_256.into();
555
556#[derive(Clone, Copy)]
557enum BlockLen {
558 _512 = 512 / 8,
559 _1024 = 1024 / 8, }
561
562impl BlockLen {
563 const MAX: Self = Self::_1024;
564 #[inline(always)]
565 const fn into(self) -> usize {
566 self as usize
567 }
568
569 #[inline(always)]
570 const fn len_len(self) -> usize {
571 let len_len = match self {
572 BlockLen::_512 => LenLen::_64,
573 BlockLen::_1024 => LenLen::_128,
574 };
575 len_len as usize
576 }
577}
578
579#[derive(Clone, Copy)]
580enum LenLen {
581 _64 = 64 / 8,
582 _128 = 128 / 8,
583}
584
585#[derive(Clone, Copy)]
586enum OutputLen {
587 _160 = 160 / 8,
588 _256 = 256 / 8,
589 _384 = 384 / 8,
590 _512 = 512 / 8, }
592
593impl OutputLen {
594 const MAX: Self = Self::_512;
595
596 #[inline(always)]
597 const fn into(self) -> usize {
598 self as usize
599 }
600}
601
602#[cfg(test)]
603mod tests {
604 mod max_input {
605 extern crate alloc;
606 use super::super::super::digest;
607 use crate::polyfill::u64_from_usize;
608 use alloc::vec;
609
610 macro_rules! max_input_tests {
611 ( $algorithm_name:ident ) => {
612 mod $algorithm_name {
613 use super::super::super::super::digest;
614
615 #[test]
616 fn max_input_test() {
617 super::max_input_test(&digest::$algorithm_name);
618 }
619
620 #[test]
621 #[should_panic]
622 fn too_long_input_test_block() {
623 super::too_long_input_test_block(&digest::$algorithm_name);
624 }
625
626 #[test]
627 #[should_panic]
628 fn too_long_input_test_byte() {
629 super::too_long_input_test_byte(&digest::$algorithm_name);
630 }
631 }
632 };
633 }
634
635 fn max_input_test(alg: &'static digest::Algorithm) {
636 let mut context = nearly_full_context(alg);
637 let next_input = vec![0u8; alg.block_len() - 1];
638 context.update(&next_input);
639 let _ = context.finish(); }
641
642 fn too_long_input_test_block(alg: &'static digest::Algorithm) {
643 let mut context = nearly_full_context(alg);
644 let next_input = vec![0u8; alg.block_len()];
645 context.update(&next_input);
646 let _ = context.finish(); }
648
649 fn too_long_input_test_byte(alg: &'static digest::Algorithm) {
650 let mut context = nearly_full_context(alg);
651 let next_input = vec![0u8; alg.block_len() - 1];
652 context.update(&next_input);
653 context.update(&[0]);
654 let _ = context.finish(); }
656
657 fn nearly_full_context(alg: &'static digest::Algorithm) -> digest::Context {
658 let max_bytes = 1u64 << (64 - 3);
662 let max_blocks = max_bytes / u64_from_usize(alg.block_len());
663 let completed_bytes = (max_blocks - 1) * u64_from_usize(alg.block_len());
664 digest::Context {
665 block: digest::BlockContext {
666 state: alg.initial_state.clone(),
667 completed_bytes,
668 algorithm: alg,
669 },
670 pending: [0u8; digest::MAX_BLOCK_LEN],
671 num_pending: 0,
672 }
673 }
674
675 max_input_tests!(SHA1_FOR_LEGACY_USE_ONLY);
676 max_input_tests!(SHA256);
677 max_input_tests!(SHA384);
678 max_input_tests!(SHA512);
679 }
680}