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