cryptoxide/hashing/sha2/
eng512.rs

1use crate::cryptoutil::{write_u32_be, write_u64v_be};
2
3pub(super) const STATE_LEN: usize = 8;
4pub(super) const BLOCK_LEN: usize = 16;
5pub(super) const BLOCK_LEN_BYTES: usize = BLOCK_LEN * core::mem::size_of::<u64>();
6
7use super::impl512::*;
8
9// A structure that represents that state of a digest computation for
10// the SHA-2 64 bits family of digest functions
11#[derive(Clone)]
12pub(super) struct Engine {
13    h: [u64; STATE_LEN],
14}
15
16impl Engine {
17    pub(super) const fn new(h: &[u64; STATE_LEN]) -> Self {
18        Self { h: *h }
19    }
20
21    pub(super) fn reset(&mut self, h: &[u64; STATE_LEN]) {
22        self.h = *h;
23    }
24
25    /// Process a block in bytes with the SHA-2 32bits algorithm.
26    pub fn blocks(&mut self, block: &[u8]) {
27        assert_eq!(block.len() % BLOCK_LEN_BYTES, 0);
28        digest_block(&mut self.h, block);
29    }
30
31    #[allow(dead_code)]
32    pub(super) fn output_224bits(&self, out: &mut [u8; 28]) {
33        write_u64v_be(&mut out[0..24], &self.h[0..3]);
34        write_u32_be(&mut out[24..28], (self.h[3] >> 32) as u32);
35    }
36
37    #[allow(dead_code)]
38    pub(super) fn output_256bits(&self, out: &mut [u8; 32]) {
39        write_u64v_be(out, &self.h[0..4]);
40    }
41
42    #[allow(dead_code)]
43    pub(super) fn output_384bits(&self, out: &mut [u8; 48]) {
44        write_u64v_be(out, &self.h[0..6])
45    }
46
47    #[allow(dead_code)]
48    pub(super) fn output_512bits(&self, out: &mut [u8; 64]) {
49        write_u64v_be(out, &self.h[0..8])
50    }
51
52    pub(super) fn output_224bits_at(&self, out: &mut [u8]) {
53        write_u64v_be(&mut out[0..24], &self.h[0..3]);
54        write_u32_be(&mut out[24..28], (self.h[3] >> 32) as u32);
55    }
56
57    pub(super) fn output_256bits_at(&self, out: &mut [u8]) {
58        write_u64v_be(out, &self.h[0..4]);
59    }
60
61    pub(super) fn output_384bits_at(&self, out: &mut [u8]) {
62        write_u64v_be(out, &self.h[0..6])
63    }
64
65    pub(super) fn output_512bits_at(&self, out: &mut [u8]) {
66        write_u64v_be(out, &self.h[0..8])
67    }
68}