Skip to main content

irox_tools/hash/
sha2.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2025 IROX Contributors
3//
4
5//!
6//! Bog standard implementation of SHA2 / RFC6234.
7//!
8
9#![allow(clippy::indexing_slicing)]
10
11pub use sha224_256::{SHA224, SHA256};
12pub use sha384_512::{SHA384, SHA512};
13
14/// Output size (bytes) for SHA224
15pub const SHA224_OUTPUT_SIZE: usize = 28;
16/// Output size (bytes) for SHA256
17pub const SHA256_OUTPUT_SIZE: usize = 32;
18/// Output size (bytes) for SHA384
19pub const SHA384_OUTPUT_SIZE: usize = 48;
20/// Output size (bytes) for SHA512
21pub const SHA512_OUTPUT_SIZE: usize = 64;
22
23macro_rules! CH {
24    ($x:expr,$y:expr,$z:expr) => {{
25        let x = $x;
26        let y = $y;
27        let z = $z;
28        (x.bitand(y)).bitxor(x.not().bitand(z))
29    }};
30}
31macro_rules! MAJ {
32    ($x:expr,$y:expr,$z:expr) => {{
33        let x = $x;
34        let y = $y;
35        let z = $z;
36        x.bitand(y).bitxor(x.bitand(z)).bitxor(y.bitand(z))
37    }};
38}
39macro_rules! sha2_impl {
40    ($name:ident, $typ: ident, $init: expr, $block_size: expr, $word_size: expr, $output_size: expr) => {
41        /// Standard implementation of
42        #[doc=stringify!($name)]
43        /// following RFC 6234
44        #[derive(Clone)]
45        pub struct $name {
46            alg: $typ<{ $block_size }, { $word_size }, { $output_size }>,
47        }
48        impl $name {
49            /// Creates a new empty instance of this algorithm
50            pub fn new() -> Self {
51                Self {
52                    alg: $typ::new($init),
53                }
54            }
55            /// Writes all the data in the slice to this algorithm
56            pub fn write(&mut self, bytes: &[u8]) {
57                self.alg.write(bytes)
58            }
59            /// Finishes this algorithm and returns the result
60            pub fn finish(self) -> [u8; $output_size] {
61                self.alg.finish()
62            }
63            /// One shot - hashes the data and returns the result.
64            pub fn hash(self, bytes: &[u8]) -> [u8; $output_size] {
65                self.alg.hash(bytes)
66            }
67        }
68        impl Default for $name {
69            fn default() -> Self {
70                $name::new()
71            }
72        }
73        impl crate::hash::HashDigest<{ $block_size }, { $output_size }> for $name {
74            fn write(&mut self, bytes: &[u8]) {
75                $name::write(self, bytes)
76            }
77
78            fn hash(self, bytes: &[u8]) -> [u8; $output_size] {
79                $name::hash(self, bytes)
80            }
81
82            fn finish(self) -> [u8; $output_size] {
83                $name::finish(self)
84            }
85            fn algorithm() -> HashAlgorithm {
86                HashAlgorithm::$name
87            }
88        }
89        impl MutBits for $name {
90            fn write_u8(&mut self, val: u8) -> Result<(), Error> {
91                self.write(&[val]);
92                Ok(())
93            }
94        }
95    };
96}
97
98/// Block size (bytes) for SHA224
99pub const SHA224_BLOCK_SIZE: usize = 64;
100pub const SHA224_WORD_SIZE: usize = 16;
101/// Block size (bytes) for SHA256
102pub const SHA256_BLOCK_SIZE: usize = 64;
103pub const SHA256_WORD_SIZE: usize = 16;
104mod sha224_256 {
105    use crate::buf::{ArrayBuf, U32ArrayBuf};
106    use crate::hash::HashAlgorithm;
107    use core::ops::{BitAnd, BitXor, Not};
108    use irox_bits::{Error, MutBits};
109
110    static KONSTANTS: [u32; 64] = [
111        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
112        0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
113        0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
114        0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
115        0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
116        0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
117        0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,
118        0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
119        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
120        0xc67178f2,
121    ];
122
123    macro_rules! BSIG0 {
124        ($x:expr) => {{
125            let x = $x;
126            x.rotate_right(2)
127                .bitxor(x.rotate_right(13))
128                .bitxor(x.rotate_right(22))
129        }};
130    }
131    macro_rules! BSIG1 {
132        ($x:expr) => {{
133            let x = $x;
134            x.rotate_right(6)
135                .bitxor(x.rotate_right(11))
136                .bitxor(x.rotate_right(25))
137        }};
138    }
139    macro_rules! SSIG0 {
140        ($x:expr) => {{
141            let x = $x;
142            x.rotate_right(7)
143                .bitxor(x.rotate_right(18))
144                .bitxor(x.wrapping_shr(3))
145        }};
146    }
147    macro_rules! SSIG1 {
148        ($x:expr) => {{
149            let x = $x;
150            x.rotate_right(17)
151                .bitxor(x.rotate_right(19))
152                .bitxor(x.wrapping_shr(10))
153        }};
154    }
155    const SHA224_INIT: [u32; 8] = [
156        0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7,
157        0xbefa4fa4,
158    ];
159    const SHA256_INIT: [u32; 8] = [
160        0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
161        0x5be0cd19,
162    ];
163
164    #[derive(Clone)]
165    pub struct LittleSha2<const BLOCK_SIZE: usize, const WORD_SIZE: usize, const OUTPUT_SIZE: usize> {
166        buf: U32ArrayBuf<16>,
167        written_length: u64,
168        h0: u32,
169        h1: u32,
170        h2: u32,
171        h3: u32,
172        h4: u32,
173        h5: u32,
174        h6: u32,
175        h7: u32,
176    }
177
178    impl<const BLOCK_SIZE: usize, const WORD_SIZE: usize, const OUTPUT_SIZE: usize>
179        LittleSha2<BLOCK_SIZE, WORD_SIZE, OUTPUT_SIZE>
180    {
181        pub fn new(init: [u32; 8]) -> Self {
182            let [h0, h1, h2, h3, h4, h5, h6, h7] = init;
183            Self {
184                buf: ArrayBuf::new(),
185                written_length: 0,
186                h0,
187                h1,
188                h2,
189                h3,
190                h4,
191                h5,
192                h6,
193                h7,
194            }
195        }
196
197        fn try_chomp(&mut self) {
198            if !self.buf.is_full() {
199                return;
200            }
201
202            let mut words = [0; 64];
203            words[0..16].copy_from_slice(&self.buf.take_be_buf());
204            for idx in 16..64 {
205                words[idx] = SSIG1!(words[idx - 2])
206                    .wrapping_add(words[idx - 7])
207                    .wrapping_add(SSIG0!(words[idx - 15]))
208                    .wrapping_add(words[idx - 16]);
209            }
210            let mut a = self.h0;
211            let mut b = self.h1;
212            let mut c = self.h2;
213            let mut d = self.h3;
214            let mut e = self.h4;
215            let mut f = self.h5;
216            let mut g = self.h6;
217            let mut h = self.h7;
218
219            for t in 0..64 {
220                let t1 = h
221                    .wrapping_add(BSIG1!(e))
222                    .wrapping_add(CH!(e, f, g))
223                    .wrapping_add(KONSTANTS[t])
224                    .wrapping_add(words[t]);
225                let t2 = BSIG0!(a).wrapping_add(MAJ!(a, b, c));
226                h = g;
227                g = f;
228                f = e;
229                e = d.wrapping_add(t1);
230                d = c;
231                c = b;
232                b = a;
233                a = t1.wrapping_add(t2);
234            }
235            self.h0 = self.h0.wrapping_add(a);
236            self.h1 = self.h1.wrapping_add(b);
237            self.h2 = self.h2.wrapping_add(c);
238            self.h3 = self.h3.wrapping_add(d);
239            self.h4 = self.h4.wrapping_add(e);
240            self.h5 = self.h5.wrapping_add(f);
241            self.h6 = self.h6.wrapping_add(g);
242            self.h7 = self.h7.wrapping_add(h);
243        }
244        pub fn finish(mut self) -> [u8; OUTPUT_SIZE] {
245            let mut modlen_bksize = (self.written_length & (BLOCK_SIZE - 1) as u64) as usize;
246            let mut pad: usize = 0;
247
248            let bitslen_minus_8 = BLOCK_SIZE - 8;
249
250            if modlen_bksize >= bitslen_minus_8 {
251                // append N bits/8 bytes;
252                pad += BLOCK_SIZE - modlen_bksize;
253                modlen_bksize = 0;
254            }
255            pad += bitslen_minus_8 - modlen_bksize;
256            let _ = self.buf.push_back(0x80);
257            pad -= 1;
258            for _ in 0..pad {
259                self.try_chomp();
260                let _ = self.buf.push_back(0);
261            }
262            let _ = self.buf.write_be_u64(self.written_length << 3);
263            self.try_chomp();
264
265            let mut out: [u8; OUTPUT_SIZE] = [0; OUTPUT_SIZE];
266            let mut v = out.as_mut_slice();
267            let _ = v.write_be_u32(self.h0);
268            let _ = v.write_be_u32(self.h1);
269            let _ = v.write_be_u32(self.h2);
270            let _ = v.write_be_u32(self.h3);
271            let _ = v.write_be_u32(self.h4);
272            let _ = v.write_be_u32(self.h5);
273            let _ = v.write_be_u32(self.h6);
274            let _ = v.write_be_u32(self.h7);
275            out
276        }
277        ///
278        /// Appends the bytes to the internal buffer.  NOTE: You must call 'finish' to get the final result.
279        pub fn write(&mut self, mut v: &[u8]) {
280            let align = self.buf.rem_align();
281            if align < 4 && align < v.len() {
282                let (a, b) = v.split_at(self.buf.rem_align());
283                v = b;
284                for val in a {
285                    let _ = self.buf.write_u8(*val);
286                    self.written_length += 1;
287                    if self.buf.is_full() {
288                        self.try_chomp();
289                    }
290                }
291            }
292            let mut chunks = v.chunks_exact(4);
293            for c in chunks.by_ref() {
294                let _ = self
295                    .buf
296                    .push_prim(u32::from_be_bytes(c.try_into().unwrap_or_default()));
297                self.written_length += 4;
298
299                self.try_chomp();
300            }
301            for b in chunks.remainder() {
302                let _ = self.buf.push_back(*b);
303                self.written_length += 1;
304                self.try_chomp();
305            }
306        }
307
308        ///
309        /// Hashes the provided bytes.
310        pub fn hash(mut self, bytes: &[u8]) -> [u8; OUTPUT_SIZE] {
311            self.write(bytes);
312            self.finish()
313        }
314    }
315
316    impl<const BLOCK_SIZE: usize, const WORD_SIZE: usize, const OUTPUT_SIZE: usize> MutBits
317        for LittleSha2<BLOCK_SIZE, WORD_SIZE, OUTPUT_SIZE>
318    {
319        fn write_u8(&mut self, val: u8) -> Result<(), Error> {
320            self.buf.write_u8(val)
321        }
322    }
323
324    sha2_impl!(
325        SHA224,
326        LittleSha2,
327        SHA224_INIT,
328        super::SHA224_BLOCK_SIZE,
329        super::SHA224_WORD_SIZE,
330        super::SHA224_OUTPUT_SIZE
331    );
332    sha2_impl!(
333        SHA256,
334        LittleSha2,
335        SHA256_INIT,
336        super::SHA256_BLOCK_SIZE,
337        super::SHA256_WORD_SIZE,
338        super::SHA256_OUTPUT_SIZE
339    );
340}
341
342/// Block size (bytes) for SHA384
343pub const SHA384_BLOCK_SIZE: usize = 128;
344pub const SHA384_WORD_SIZE: usize = 32;
345/// Block size (bytes) for SHA512
346pub const SHA512_BLOCK_SIZE: usize = 128;
347pub const SHA512_WORD_SIZE: usize = 32;
348mod sha384_512 {
349    use crate::buf::{ArrayBuf, U64ArrayBuf};
350    use crate::hash::HashAlgorithm;
351    use core::ops::{BitAnd, BitXor, Not};
352    use irox_bits::{Error, MutBits};
353
354    macro_rules! BSIG0 {
355        ($x:expr) => {{
356            let x = $x;
357            x.rotate_right(28)
358                .bitxor(x.rotate_right(34))
359                .bitxor(x.rotate_right(39))
360        }};
361    }
362    macro_rules! BSIG1 {
363        ($x:expr) => {{
364            let x = $x;
365            x.rotate_right(14)
366                .bitxor(x.rotate_right(18))
367                .bitxor(x.rotate_right(41))
368        }};
369    }
370    macro_rules! SSIG0 {
371        ($x:expr) => {{
372            let x = $x;
373            x.rotate_right(1)
374                .bitxor(x.rotate_right(8))
375                .bitxor(x.wrapping_shr(7))
376        }};
377    }
378    macro_rules! SSIG1 {
379        ($x:expr) => {{
380            let x = $x;
381            x.rotate_right(19)
382                .bitxor(x.rotate_right(61))
383                .bitxor(x.wrapping_shr(6))
384        }};
385    }
386
387    pub static KONSTANTS: [u64; 80] = [
388        0x428a2f98d728ae22,
389        0x7137449123ef65cd,
390        0xb5c0fbcfec4d3b2f,
391        0xe9b5dba58189dbbc,
392        0x3956c25bf348b538,
393        0x59f111f1b605d019,
394        0x923f82a4af194f9b,
395        0xab1c5ed5da6d8118,
396        0xd807aa98a3030242,
397        0x12835b0145706fbe,
398        0x243185be4ee4b28c,
399        0x550c7dc3d5ffb4e2,
400        0x72be5d74f27b896f,
401        0x80deb1fe3b1696b1,
402        0x9bdc06a725c71235,
403        0xc19bf174cf692694,
404        0xe49b69c19ef14ad2,
405        0xefbe4786384f25e3,
406        0x0fc19dc68b8cd5b5,
407        0x240ca1cc77ac9c65,
408        0x2de92c6f592b0275,
409        0x4a7484aa6ea6e483,
410        0x5cb0a9dcbd41fbd4,
411        0x76f988da831153b5,
412        0x983e5152ee66dfab,
413        0xa831c66d2db43210,
414        0xb00327c898fb213f,
415        0xbf597fc7beef0ee4,
416        0xc6e00bf33da88fc2,
417        0xd5a79147930aa725,
418        0x06ca6351e003826f,
419        0x142929670a0e6e70,
420        0x27b70a8546d22ffc,
421        0x2e1b21385c26c926,
422        0x4d2c6dfc5ac42aed,
423        0x53380d139d95b3df,
424        0x650a73548baf63de,
425        0x766a0abb3c77b2a8,
426        0x81c2c92e47edaee6,
427        0x92722c851482353b,
428        0xa2bfe8a14cf10364,
429        0xa81a664bbc423001,
430        0xc24b8b70d0f89791,
431        0xc76c51a30654be30,
432        0xd192e819d6ef5218,
433        0xd69906245565a910,
434        0xf40e35855771202a,
435        0x106aa07032bbd1b8,
436        0x19a4c116b8d2d0c8,
437        0x1e376c085141ab53,
438        0x2748774cdf8eeb99,
439        0x34b0bcb5e19b48a8,
440        0x391c0cb3c5c95a63,
441        0x4ed8aa4ae3418acb,
442        0x5b9cca4f7763e373,
443        0x682e6ff3d6b2b8a3,
444        0x748f82ee5defb2fc,
445        0x78a5636f43172f60,
446        0x84c87814a1f0ab72,
447        0x8cc702081a6439ec,
448        0x90befffa23631e28,
449        0xa4506cebde82bde9,
450        0xbef9a3f7b2c67915,
451        0xc67178f2e372532b,
452        0xca273eceea26619c,
453        0xd186b8c721c0c207,
454        0xeada7dd6cde0eb1e,
455        0xf57d4f7fee6ed178,
456        0x06f067aa72176fba,
457        0x0a637dc5a2c898a6,
458        0x113f9804bef90dae,
459        0x1b710b35131c471b,
460        0x28db77f523047d84,
461        0x32caab7b40c72493,
462        0x3c9ebe0a15c9bebc,
463        0x431d67c49c100d4c,
464        0x4cc5d4becb3e42b6,
465        0x597f299cfc657e2a,
466        0x5fcb6fab3ad6faec,
467        0x6c44198c4a475817,
468    ];
469
470    pub const SHA384_INIT: [u64; 8] = [
471        0xcbbb9d5dc1059ed8,
472        0x629a292a367cd507,
473        0x9159015a3070dd17,
474        0x152fecd8f70e5939,
475        0x67332667ffc00b31,
476        0x8eb44a8768581511,
477        0xdb0c2e0d64f98fa7,
478        0x47b5481dbefa4fa4,
479    ];
480    pub const SHA512_INIT: [u64; 8] = [
481        0x6a09e667f3bcc908,
482        0xbb67ae8584caa73b,
483        0x3c6ef372fe94f82b,
484        0xa54ff53a5f1d36f1,
485        0x510e527fade682d1,
486        0x9b05688c2b3e6c1f,
487        0x1f83d9abfb41bd6b,
488        0x5be0cd19137e2179,
489    ];
490
491    #[derive(Clone)]
492    pub struct BiggerSha2<const BLOCK_SIZE: usize, const WORD_SIZE: usize, const OUTPUT_SIZE: usize> {
493        buf: U64ArrayBuf<16>,
494        written_length: u128,
495        h0: u64,
496        h1: u64,
497        h2: u64,
498        h3: u64,
499        h4: u64,
500        h5: u64,
501        h6: u64,
502        h7: u64,
503    }
504
505    impl<const BLOCK_SIZE: usize, const WORD_SIZE: usize, const OUTPUT_SIZE: usize>
506        BiggerSha2<BLOCK_SIZE, WORD_SIZE, OUTPUT_SIZE>
507    {
508        pub fn new(init: [u64; 8]) -> Self {
509            let [h0, h1, h2, h3, h4, h5, h6, h7] = init;
510            Self {
511                buf: ArrayBuf::new(),
512                written_length: 0,
513                h0,
514                h1,
515                h2,
516                h3,
517                h4,
518                h5,
519                h6,
520                h7,
521            }
522        }
523
524        fn try_chomp(&mut self) {
525            if !self.buf.is_full() {
526                return;
527            }
528
529            let mut words = [0; 80];
530            words[0..16].copy_from_slice(&self.buf.take_be_buf());
531            for idx in 16..80 {
532                words[idx] = SSIG1!(words[idx - 2])
533                    .wrapping_add(words[idx - 7])
534                    .wrapping_add(SSIG0!(words[idx - 15]))
535                    .wrapping_add(words[idx - 16]);
536            }
537            let mut a = self.h0;
538            let mut b = self.h1;
539            let mut c = self.h2;
540            let mut d = self.h3;
541            let mut e = self.h4;
542            let mut f = self.h5;
543            let mut g = self.h6;
544            let mut h = self.h7;
545
546            for t in 0..80 {
547                let t1 = h
548                    .wrapping_add(BSIG1!(e))
549                    .wrapping_add(CH!(e, f, g))
550                    .wrapping_add(KONSTANTS[t])
551                    .wrapping_add(words[t]);
552                let t2 = BSIG0!(a).wrapping_add(MAJ!(a, b, c));
553                h = g;
554                g = f;
555                f = e;
556                e = d.wrapping_add(t1);
557                d = c;
558                c = b;
559                b = a;
560                a = t1.wrapping_add(t2);
561            }
562            self.h0 = self.h0.wrapping_add(a);
563            self.h1 = self.h1.wrapping_add(b);
564            self.h2 = self.h2.wrapping_add(c);
565            self.h3 = self.h3.wrapping_add(d);
566            self.h4 = self.h4.wrapping_add(e);
567            self.h5 = self.h5.wrapping_add(f);
568            self.h6 = self.h6.wrapping_add(g);
569            self.h7 = self.h7.wrapping_add(h);
570        }
571        pub fn finish(mut self) -> [u8; OUTPUT_SIZE] {
572            let mut modlen_bksize = (self.written_length & (BLOCK_SIZE - 1) as u128) as usize;
573            let mut pad: usize = 0;
574
575            let bitslen_minus_8 = BLOCK_SIZE - 16;
576
577            if modlen_bksize >= bitslen_minus_8 {
578                // append 64 bits/8 bytes;
579                pad += BLOCK_SIZE - modlen_bksize;
580                modlen_bksize = 0;
581            }
582            pad += bitslen_minus_8 - modlen_bksize;
583            let _ = self.buf.push_back(0x80);
584            pad -= 1;
585            for _ in 0..pad {
586                self.try_chomp();
587                let _ = self.buf.push_back(0);
588            }
589            let _ = self.buf.write_be_u128(self.written_length << 3);
590            self.try_chomp();
591
592            let mut out: [u8; OUTPUT_SIZE] = [0; OUTPUT_SIZE];
593            let mut v = out.as_mut_slice();
594            let _ = v.write_be_u64(self.h0);
595            let _ = v.write_be_u64(self.h1);
596            let _ = v.write_be_u64(self.h2);
597            let _ = v.write_be_u64(self.h3);
598            let _ = v.write_be_u64(self.h4);
599            let _ = v.write_be_u64(self.h5);
600            let _ = v.write_be_u64(self.h6);
601            let _ = v.write_be_u64(self.h7);
602            out
603        }
604        ///
605        /// Appends the bytes to the internal buffer.  NOTE: You must call 'finish' to get the final result.
606        pub fn write(&mut self, mut v: &[u8]) {
607            let align = self.buf.rem_align();
608            if align < 8 && align < v.len() {
609                let (a, b) = v.split_at(self.buf.rem_align());
610                v = b;
611                for val in a {
612                    let _ = self.buf.write_u8(*val);
613                    self.written_length += 1;
614                    if self.buf.is_full() {
615                        self.try_chomp();
616                    }
617                }
618            }
619            let mut chunks = v.chunks_exact(8);
620            for c in chunks.by_ref() {
621                let _ = self
622                    .buf
623                    .push_prim(u64::from_be_bytes(c.try_into().unwrap_or_default()));
624                self.written_length += 8;
625                self.try_chomp();
626            }
627            for b in chunks.remainder() {
628                let _ = self.buf.push_back(*b);
629                self.written_length += 1;
630                self.try_chomp();
631            }
632        }
633
634        ///
635        /// Hashes the provided bytes.
636        pub fn hash(mut self, bytes: &[u8]) -> [u8; OUTPUT_SIZE] {
637            self.write(bytes);
638            self.finish()
639        }
640    }
641
642    impl<const BLOCK_SIZE: usize, const WORD_SIZE: usize, const OUTPUT_SIZE: usize> MutBits
643        for BiggerSha2<BLOCK_SIZE, WORD_SIZE, OUTPUT_SIZE>
644    {
645        fn write_u8(&mut self, val: u8) -> Result<(), Error> {
646            self.buf.write_u8(val)
647        }
648    }
649    sha2_impl!(
650        SHA384,
651        BiggerSha2,
652        SHA384_INIT,
653        super::SHA384_BLOCK_SIZE,
654        super::SHA384_WORD_SIZE,
655        super::SHA384_OUTPUT_SIZE
656    );
657    sha2_impl!(
658        SHA512,
659        BiggerSha2,
660        SHA512_INIT,
661        super::SHA512_BLOCK_SIZE,
662        super::SHA512_WORD_SIZE,
663        super::SHA512_OUTPUT_SIZE
664    );
665}
666
667#[cfg(test)]
668mod test {
669    use crate::hash::sha2::{SHA224, SHA224_OUTPUT_SIZE, SHA256, SHA256_OUTPUT_SIZE};
670    use irox_bits::MutBits;
671
672    fn u32s_to_arr<const T: usize>(input: &[u32]) -> [u8; T] {
673        let mut out = [0u8; T];
674        let mut pos = out.as_mut_slice();
675        for v in input {
676            let _ = pos.write_all_bytes(&v.to_be_bytes());
677        }
678        out
679    }
680    macro_rules! to_arr {
681        ($out:expr, $($elem:expr) +) => {
682            u32s_to_arr::<$out>(&[
683                $($elem),+
684            ])
685        };
686    }
687    #[test]
688    pub fn sha224_wiki1() {
689        assert_eq_hex_slice!(
690            to_arr!(SHA224_OUTPUT_SIZE, 0xd14a028c 0x2a3a2bc9 0x476102bb 0x288234c4 0x15a2b01f 0x828ea62a 0xc5b3e42f),
691            SHA224::default().hash(&[])
692        );
693    }
694    #[test]
695    pub fn sha224_wiki2() {
696        assert_eq_hex_slice!(
697            to_arr!(SHA224_OUTPUT_SIZE, 0x730e109b 0xd7a8a32b 0x1cb9d9a0 0x9aa2325d 0x2430587d 0xdbc0c38b 0xad911525),
698            SHA224::default().hash("The quick brown fox jumps over the lazy dog".as_bytes())
699        );
700    }
701    #[test]
702    pub fn sha224_wiki3() {
703        assert_eq_hex_slice!(
704            to_arr!(SHA224_OUTPUT_SIZE, 0x619cba8e 0x8e05826e 0x9b8c519c 0x0a5c68f4 0xfb653e8a 0x3d8aa04b 0xb2c8cd4c),
705            SHA224::default().hash("The quick brown fox jumps over the lazy dog.".as_bytes())
706        );
707    }
708    #[test]
709    pub fn sha224_nist1() {
710        assert_eq_hex_slice!(
711            to_arr!(SHA224_OUTPUT_SIZE, 0xe33f9d75 0xe6ae1369 0xdbabf81b 0x96b4591a 0xe46bba30 0xb591a6b6 0xc62542b5),
712            SHA224::default().hash(&[0xff])
713        );
714    }
715    #[test]
716    pub fn sha224_nist2() {
717        assert_eq_hex_slice!(
718            to_arr!(SHA224_OUTPUT_SIZE, 0xfd19e746 0x90d29146 0x7ce59f07 0x7df31163 0x8f1c3a46 0xe510d0e4 0x9a67062d ),
719            SHA224::default().hash(&0xe5e09924u32.to_be_bytes())
720        );
721    }
722    #[test]
723    pub fn sha224_nist3() {
724        assert_eq_hex_slice!(
725            to_arr!(SHA224_OUTPUT_SIZE, 0x5c3e25b6 0x9d0ea26f 0x260cfae8 0x7e23759e 0x1eca9d1e 0xcc9fbf3c 0x62266804),
726            SHA224::default().hash(&[0u8; 56])
727        );
728    }
729    #[test]
730    pub fn sha224_nist4() {
731        assert_eq_hex_slice!(
732            to_arr!(SHA224_OUTPUT_SIZE, 0x3706197f 0x66890a41 0x779dc879 0x1670522e 0x136fafa2 0x48746857 0x15bd0a8a),
733            SHA224::default().hash(&[0x51u8; 1000])
734        );
735    }
736    #[test]
737    pub fn sha224_nist5() {
738        assert_eq_hex_slice!(
739            to_arr!(SHA224_OUTPUT_SIZE, 0xa8d0c66b 0x5c6fdfd8 0x36eb3c6d 0x04d32dfe 0x66c3b1f1 0x68b488bf 0x4c9c66ce),
740            SHA224::default().hash(&[0x41u8; 1000])
741        );
742    }
743    #[test]
744    pub fn sha224_nist6() {
745        assert_eq_hex_slice!(
746            to_arr!(SHA224_OUTPUT_SIZE, 0xcb00ecd0 0x3788bf6c 0x0908401e 0x0eb053ac 0x61f35e7e 0x20a2cfd7 0xbd96d640),
747            SHA224::default().hash(&[0x99u8; 1005])
748        );
749    }
750    #[test]
751    pub fn sha224_nist7() {
752        let mut sha = SHA224::new();
753        for _ in 0..1000000 {
754            sha.write(&[0]);
755        }
756        assert_eq_hex_slice!(
757            to_arr!(SHA224_OUTPUT_SIZE, 0x3a5d74b6 0x8f14f3a4 0xb2be9289 0xb8d37067 0x2d0b3d2f 0x53bc303c 0x59032df3),
758            sha.finish()
759        );
760    }
761    #[test]
762    #[cfg_attr(not(feature = "_toobig-tests"), ignore)]
763    pub fn sha224_nist8() {
764        let mut sha = SHA224::new();
765        for _ in 0..0x20000000 {
766            sha.write(&[0x41]);
767        }
768        assert_eq_hex_slice!(
769            to_arr!(SHA224_OUTPUT_SIZE, 0xc4250083 0xcf8230bf 0x21065b30 0x14baaaf9 0xf76fecef 0xc21f91cf 0x237dedc9),
770            sha.finish()
771        );
772    }
773
774    #[test]
775    #[cfg_attr(not(feature = "_toobig-tests"), ignore)]
776    pub fn sha224_nist9() {
777        let mut sha = SHA224::new();
778        for _ in 0..0x41000000 {
779            sha.write(&[0x0]);
780        }
781        assert_eq_hex_slice!(
782            to_arr!(SHA224_OUTPUT_SIZE, 0x014674ab 0xc5cb9801 0x99935695 0xaf22fab6 0x83748f42 0x61d4c649 0x2b77c543
783            ),
784            sha.finish()
785        );
786    }
787    #[test]
788    #[cfg_attr(not(feature = "_toobig-tests"), ignore)]
789    pub fn sha224_nist10() {
790        let mut sha = SHA224::new();
791        for _ in 0..0x6000003f {
792            sha.write(&[0x84]);
793        }
794        assert_eq_hex_slice!(
795            to_arr!(SHA224_OUTPUT_SIZE, 0xa654b50b 0x767a8323 0xc5b519f4 0x67d86698 0x37142881 0xdc7ad368 0xa7d5ef8f),
796            sha.finish()
797        );
798    }
799
800    #[test]
801    pub fn sha256_nist1() {
802        assert_eq_hex_slice!(
803            to_arr!(SHA256_OUTPUT_SIZE, 0x68325720 0xaabd7c82 0xf30f554b 0x313d0570 0xc95accbb 0x7dc4b5aa 0xe11204c0 0x8ffe732b),
804            SHA256::default().hash(&[0xbd])
805        );
806    }
807    #[test]
808    pub fn sha256_nist2() {
809        assert_eq_hex_slice!(
810            to_arr!(SHA256_OUTPUT_SIZE, 0x7abc22c0 0xae5af26c 0xe93dbb94 0x433a0e0b 0x2e119d01 0x4f8e7f65 0xbd56c61c 0xcccd9504),
811            SHA256::default().hash(&0xc98c8e55u32.to_be_bytes())
812        );
813    }
814    #[test]
815    pub fn sha256_nist3() {
816        assert_eq_hex_slice!(
817            to_arr!(SHA256_OUTPUT_SIZE, 0x02779466 0xcdec1638 0x11d07881 0x5c633f21 0x90141308 0x1449002f 0x24aa3e80 0xf0b88ef7),
818            SHA256::default().hash(&[0u8; 55])
819        );
820    }
821    #[test]
822    pub fn sha256_nist4() {
823        assert_eq_hex_slice!(
824            to_arr!(SHA256_OUTPUT_SIZE, 0xd4817aa5 0x497628e7 0xc77e6b60 0x6107042b 0xbba31308 0x88c5f47a 0x375e6179 0xbe789fbb),
825            SHA256::default().hash(&[0x0u8; 56])
826        );
827    }
828    #[test]
829    pub fn sha256_nist5() {
830        assert_eq_hex_slice!(
831            to_arr!(SHA256_OUTPUT_SIZE, 0x65a16cb7 0x861335d5 0xace3c607 0x18b5052e 0x44660726 0xda4cd13b 0xb745381b 0x235a1785),
832            SHA256::default().hash(&[0x0u8; 57])
833        );
834    }
835    #[test]
836    pub fn sha256_nist6() {
837        assert_eq_hex_slice!(
838            to_arr!(SHA256_OUTPUT_SIZE, 0xf5a5fd42 0xd16a2030 0x2798ef6e 0xd309979b 0x43003d23 0x20d9f0e8 0xea9831a9 0x2759fb4b),
839            SHA256::default().hash(&[0x0u8; 64])
840        );
841    }
842    #[test]
843    pub fn sha256_nist7() {
844        assert_eq_hex_slice!(
845            to_arr!(SHA256_OUTPUT_SIZE, 0x541b3e9d 0xaa09b20b 0xf85fa273 0xe5cbd3e8 0x0185aa4e 0xc298e765 0xdb87742b 0x70138a53),
846            SHA256::default().hash(&[0x0u8; 1000])
847        );
848    }
849    #[test]
850    pub fn sha256_nist8() {
851        assert_eq_hex_slice!(
852            to_arr!(SHA256_OUTPUT_SIZE, 0xc2e68682 0x3489ced2 0x017f6059 0xb8b23931 0x8b6364f6 0xdcd835d0 0xa519105a 0x1eadd6e4),
853            SHA256::default().hash(&[0x41u8; 1000])
854        );
855    }
856    #[test]
857    pub fn sha256_nist9() {
858        assert_eq_hex_slice!(
859            to_arr!(SHA256_OUTPUT_SIZE, 0xf4d62dde 0xc0f3dd90 0xea1380fa 0x16a5ff8d 0xc4c54b21 0x740650f2 0x4afc4120 0x903552b0),
860            SHA256::default().hash(&[0x55u8; 1005])
861        );
862    }
863    #[test]
864    #[cfg_attr(not(feature = "_toobig-tests"), ignore)]
865    pub fn sha256_nist10() {
866        let mut sha = SHA256::new();
867        for _ in 0..1000000 {
868            sha.write(&[0]);
869        }
870        assert_eq_hex_slice!(
871            to_arr!(SHA256_OUTPUT_SIZE, 0xd29751f2 0x649b32ff 0x572b5e0a 0x9f541ea6 0x60a50f94 0xff0beedf 0xb0b692b9 0x24cc8025),
872            sha.finish()
873        );
874    }
875    #[test]
876    #[cfg_attr(not(feature = "_toobig-tests"), ignore)]
877    pub fn sha256_nist11() {
878        let mut sha = SHA256::new();
879        for _ in 0..0x20000000 {
880            sha.write(&[0x5a]);
881        }
882        assert_eq_hex_slice!(
883            to_arr!(SHA256_OUTPUT_SIZE, 0x15a1868c 0x12cc5395 0x1e182344 0x277447cd 0x0979536b 0xadcc512a 0xd24c67e9 0xb2d4f3dd),
884            sha.finish()
885        );
886    }
887    #[test]
888    #[cfg_attr(not(feature = "_toobig-tests"), ignore)]
889    pub fn sha256_nist12() {
890        let mut sha = SHA256::new();
891        for _ in 0..0x41000000 {
892            sha.write(&[0x0]);
893        }
894        assert_eq_hex_slice!(
895            to_arr!(SHA256_OUTPUT_SIZE, 0x461c19a9 0x3bd4344f 0x9215f5ec 0x64357090 0x342bc66b 0x15a14831 0x7d276e31 0xcbc20b53),
896            sha.finish()
897        );
898    }
899    #[test]
900    #[cfg_attr(not(feature = "_toobig-tests"), ignore)]
901    pub fn sha256_nist13() {
902        let mut sha = SHA256::new();
903        for _ in 0..0x6000003e {
904            sha.write(&[0x42]);
905        }
906        assert_eq_hex_slice!(
907            to_arr!(SHA256_OUTPUT_SIZE, 0xc23ce8a7 0x895f4b21 0xec0daf37 0x920ac0a2 0x62a22004 0x5a03eb2d 0xfed48ef9 0xb05aabea),
908            sha.finish()
909        );
910    }
911}