Skip to main content

bouncycastle_sha2/
lib.rs

1//! Implements SHA2 as per NIST FIPS 180-4.
2//!
3//! # Examples
4//! ## Hash
5//! Hash functionality is accessed via the [bouncycastle_core_interface::traits::Hash] trait,
6//! which is implemented by [SHA224], [SHA256], [SHA384] and [SHA512].
7//!
8//! The simplest usage is via the static functions.
9//! ```
10//! use bouncycastle_core_interface::traits::Hash;
11//!
12//! let data: &[u8] = b"Hello, world!";
13//! let output: Vec<u8> = sha2::SHA256::new().hash(data);
14//! ```
15//!
16//! More advanced usage will require creating a SHA3 or SHAKE object to hold state between successive calls,
17//! for example if input is received in chunks and not all available at the same time:
18//!
19//! ```
20//! use bouncycastle_core_interface::traits::Hash;
21//! use bouncycastle_sha2 as sha2;
22//!
23//! let data: &[u8] = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F
24//!                     \x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F
25//!                     \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F
26//!                     \x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
27//! let mut sha2 = sha2::SHA256::new();
28//!
29//! for chunk in data.chunks(16) {
30//!     sha2.do_update(chunk);
31//! }
32//!
33//! let output: Vec<u8> = sha2.do_final();
34//! ```
35
36#![forbid(unsafe_code)]
37#![allow(private_bounds)]
38
39mod sha256;
40mod sha512;
41
42pub use self::sha256::SHA256Internal;
43pub use self::sha512::Sha512Internal;
44use bouncycastle_core_interface::traits::{Algorithm, HashAlgParams, SecurityStrength};
45
46/*** String constants ***/
47pub const SHA224_NAME: &str = "SHA224";
48pub const SHA256_NAME: &str = "SHA256";
49pub const SHA384_NAME: &str = "SHA384";
50pub const SHA512_NAME: &str = "SHA512";
51
52/*** pub types ***/
53pub type SHA224 = SHA256Internal<SHA224Params>;
54pub type SHA256 = SHA256Internal<SHA256Params>;
55pub type SHA384 = Sha512Internal<SHA384Params>;
56pub type SHA512 = Sha512Internal<SHA512Params>;
57
58/*** Param traits ***/
59
60trait SHA2Params: HashAlgParams {}
61
62impl Algorithm for SHA224 {
63    const ALG_NAME: &'static str = SHA224_NAME;
64    const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_112bit;
65}
66impl HashAlgParams for SHA224 {
67    const OUTPUT_LEN: usize = 28;
68    const BLOCK_LEN: usize = 64;
69}
70pub struct SHA224Params;
71impl Algorithm for SHA224Params {
72    const ALG_NAME: &'static str = SHA224_NAME;
73    const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_112bit;
74}
75impl HashAlgParams for SHA224Params {
76    const OUTPUT_LEN: usize = 28;
77    const BLOCK_LEN: usize = 64;
78}
79impl SHA2Params for SHA224Params {}
80
81impl Algorithm for SHA256 {
82    const ALG_NAME: &'static str = SHA256_NAME;
83    const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_128bit;
84}
85impl HashAlgParams for SHA256 {
86    const OUTPUT_LEN: usize = 32;
87    const BLOCK_LEN: usize = 64;
88}
89pub struct SHA256Params;
90impl Algorithm for SHA256Params {
91    const ALG_NAME: &'static str = SHA256_NAME;
92    const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_128bit;
93}
94impl HashAlgParams for SHA256Params {
95    const OUTPUT_LEN: usize = 32;
96    const BLOCK_LEN: usize = 64;
97}
98impl SHA2Params for SHA256Params {}
99
100impl Algorithm for SHA384 {
101    const ALG_NAME: &'static str = SHA384_NAME;
102    const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_192bit;
103}
104impl HashAlgParams for SHA384 {
105    const OUTPUT_LEN: usize = 48;
106    const BLOCK_LEN: usize = 128;
107}
108pub struct SHA384Params;
109impl Algorithm for SHA384Params {
110    const ALG_NAME: &'static str = SHA384_NAME;
111    const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_192bit;
112}
113impl HashAlgParams for SHA384Params {
114    const OUTPUT_LEN: usize = 48;
115    const BLOCK_LEN: usize = 128;
116}
117impl SHA2Params for SHA384Params {}
118
119pub struct SHA512Params;
120impl Algorithm for SHA512 {
121    const ALG_NAME: &'static str = SHA512_NAME;
122    const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_256bit;
123}
124impl HashAlgParams for SHA512 {
125    const OUTPUT_LEN: usize = 64;
126    const BLOCK_LEN: usize = 128;
127}
128impl Algorithm for SHA512Params {
129    const ALG_NAME: &'static str = SHA512_NAME;
130    const MAX_SECURITY_STRENGTH: SecurityStrength = SecurityStrength::_256bit;
131}
132impl HashAlgParams for SHA512Params {
133    const OUTPUT_LEN: usize = 64;
134    const BLOCK_LEN: usize = 128;
135}
136impl SHA2Params for SHA512Params {}