wolf_crypto/hash/
mod.rs

1//! Cryptographic Hash Algorithms
2
3/*
4 INFALLIBILITY COMMENTARY
5
6 ---- SHA3 / KECCAK FAMILY
7
8 -- InitSha3
9
10 Src: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha3.c#L620
11
12 Under all possible paths this returns 0.
13
14 -- Sha3Update
15
16 Src: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha3.c#L670
17
18 Under all possible paths this returns 0.
19
20 -- Sha3Final
21
22 Src: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha3.c#L745
23
24 Under all possible paths this returns 0.
25
26 -- wc_InitSha3
27
28 Src: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha3.c#L808
29
30 This will always return 0 unless WOLFSSL_ASYNC_CRYPT /\ WC_ASYNC_ENABLE_SHA3 are enabled, which
31 neither of these are. So this is also infallible.
32
33 This depends on the infallibility of InitSha3, which we have already pointed out, returns
34 zero under all possible paths.
35
36 -- wc_Sha3Update
37
38 Src: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha3.c#L840
39
40 This will always return zero unless WOLF_CRYPTO_CB \/ (WOLFSSL_ASYNC_CRYPT /\ WC_ASYNC_ENABLE_SHA3)
41 are enabled. Async crypt will never be enabled so this is ruled out. Though WOLF_CRYPTO_CB may
42 eventually be enabled via a feature flag. Currently, the upgrade guide, found in the workspace
43 root, requires that if this feature is being enabled that the introduced fallibility must be
44 handled in some way, shape, or form. The upgrade guide allows current implementations to make the
45 assumption that this is not to be enabled, and that methods made fallible due to the potential
46 enabling of this feature do not need to handle the potential error.
47
48 This is also fallible via the functions preconditions which are handled via the type system and
49 borrow checker. These being the sha3 instance being null, and the data being null if the length is
50 greater than zero.
51
52 This propagates any error encountered in Sha3Update, which we have already shown to be infallible,
53 regardless of WOLF_CRYPTO_CB.
54
55 So, this is currently to be considered infallible.
56
57 -- wc_Sha3Final
58
59 Src: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha3.c#L899
60
61 This carries the same dependencies in enabled features as the above wc_Sha3Update commentary.
62
63 This function is fallible via the preconditions, which are handled via the type system and
64 borrow checker. These being the sha3 instance being null, and the data being null if the length
65 is greater than zero.
66
67 This then depends on the infallibility of Sha3Final, and Sha3Init, which we have already shown
68 to be infallible.
69
70 -- wc_Sha3Copy
71
72 Src: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha3.c#L973
73
74 This is only fallible if the WOLFSSL_ASYNC_CRYPT /\ WC_ASYNC_ENABLE_SHA3 features are enabled,
75 which we have already discussed are not to be enabled as there's no practical advantage we have
76 for them.
77
78 They are OK for cheaper, old, processors where hardware acceleration with things such as AESNI
79 are shared between cores, as then they somewhat operate asynchronously at the hardware level,
80 but beyond this there's just no value, only complexity.
81
82 Then, there's the functions preconditions, which are handled via the type system and borrow
83 checker, these being the source or destination being null.
84
85 -- wc_Sha3_[224 | 256 | 384 | 512]_[Update | Final | Copy]
86
87 All the specific variants simply invoke the underlying associated wc_Sha3[Update | Final | Copy]
88 function, propagating any error encountered. We have already shown that these functions are
89 infallible.
90
91 -- Conclusion
92
93 All of wolfcrypt's Sha3 has been shown to be infallible under current conditions, related
94 functions (SHAKE), while out of scope of this particular module, are also infallible due to their
95 dependence on what we have already shown infallible. Their preconditions are all handled by the
96 type system under all cases.
97
98 ---- SHA2 FAMILY
99
100 Src: https://github.com/wolfSSL/wolfssl/blob/master/wolfcrypt/src/sha256.c
101
102 -- wc_InitSha[224 | 256 | 384 | 512 | 512_224 | 512_256]_ex
103
104 This function is infallible under standard configurations. The only potential points of
105 fallibility are:
106
107 1. If (WOLFSSL_ASYNC_CRYPT /\ WC_ASYNC_ENABLE_SHA256) is enabled, which as discussed in the SHA3
108    section, will not be enabled due to lack of practical advantage.
109 2. On IMXRT1170 processors with WOLFSSL_IMXRT1170_CAAM enabled, the wc_CAAM_HashInit call could
110    fail. Though this is discussed in the update checklist, and currently is considered infallible.
111
112 Given this, wc_InitSha256_ex is to be considered infallible for our purposes.
113
114 -- wc_Sha[224 | 256 | 384 | 512 | 512_224 | 512_256]Update
115
116 This function's infallibility depends on the same conditions as wc_Sha3Update:
117
118 It will always return zero unless (WOLF_CRYPTO_CB \/ (WOLFSSL_ASYNC_CRYPT /\ WC_ASYNC_ENABLE_SHA256))
119 are enabled. As discussed in the SHA3 section, async crypt will not be enabled, and WOLF_CRYPTO_CB,
120 while potentially enabled via a feature flag, is currently considered infallible per the upgrade
121 guide.
122
123 The function's preconditions (non-null sha256, len > 0 -> non-null data) are handled by the type
124 system and borrow checker.
125
126 The underlying Sha256Update function, which this calls, is infallible under all paths.
127
128 Therefore, wc_Sha256Update is to be considered infallible.
129
130 -- wc_Sha[224 | 256 | 384 | 512 | 512_224 | 512_256]Final
131
132 This function's infallibility analysis is similar to wc_Sha256Update. It depends on the same 
133 conditions regarding WOLF_CRYPTO_CB and async crypto, which we've established are currently 
134 considered infallible.
135
136 The function's preconditions (non-null sha256, non-null hash) are handled by the type system and 
137 borrow checker.
138
139 It calls Sha256Final, which is infallible under all paths, and InitSha256, which we've shown to be 
140 infallible.
141
142 Thus, wc_Sha256Final is to be considered infallible.
143
144 -- wc_Sha[224 | 256 | 384 | 512 | 512_224 | 512_256]GetHash
145
146 This function's infallibility depends on wc_Sha256Copy and wc_Sha256Final, both of which we have 
147 shown to be infallible. Therefore, wc_Sha256GetHash is also infallible.
148
149 -- wc_Sha[224 | 256 | 384 | 512 | 512_224 | 512_256]Copy
150
151 This function is infallible under standard configurations. The only potential points of fallibility
152 are:
153
154 1. If WOLFSSL_ASYNC_CRYPT /\ WC_ASYNC_ENABLE_SHA256 are enabled, which we've established will not 
155    be the case.
156 2. For PIC32MZ platforms with WOLFSSL_PIC32MZ_HASH enabled, this is discussed in the wolfcrypt-sys
157    update guidelines.
158 3. If HAVE_ARIA is enabled and MC_CopySession fails. This is covered under the WOLF_CRYPTO_CB case.
159
160 The function's preconditions (non-null src and dst) are handled by the type system and borrow 
161 checker.
162
163 Given these conditions, wc_Sha256Copy can be considered infallible for our purposes.
164
165 -- Conclusion
166
167 All of wolfcrypt's SHA2 functions have been shown to be infallible under current conditions and 
168 standard configurations. The potential sources of fallibility (async crypto, specific platform 
169 implementations, and certain cryptographic library integrations) are either not enabled or 
170 outlined in the upgrade checklist and are to currently be considered infallible.
171 
172 All preconditions are handled by the type system and borrow checker. Therefore, for the purposes of
173 this analysis, the SHA2 family can be considered infallible.
174 
175 END COMMENTARY
176 
177 Currently, this analysis is only acted on the HMAC implementation. Though, it is likely this 
178 analysis will be taken advantage of in the `hash` module in an upcoming update.
179*/
180#[macro_use]
181mod api_gen;
182
183hidden! {
184    pub mod sha224;
185    pub mod sha256;
186    pub mod sha384;
187    pub mod sha512;
188    pub mod sha512_256;
189    pub mod sha512_224;
190    pub mod sha3_224;
191    pub mod sha3_256;
192    pub mod sha3_384;
193    pub mod sha3_512;
194}
195
196#[macro_use]
197mod shake_api;
198
199hidden! {
200    pub mod shake128;
201    pub mod shake256;
202}
203
204pub use {
205    sha224::Sha224,
206    sha256::Sha256,
207    sha384::Sha384,
208    sha512::Sha512,
209    sha512_224::Sha512_224,
210    sha512_256::Sha512_256,
211    sha3_224::Sha3_224,
212    sha3_256::Sha3_256,
213    sha3_384::Sha3_384,
214    sha3_512::Sha3_512,
215    shake128::Shake128,
216    shake256::Shake256
217};
218
219non_fips! {
220    #[doc(hidden)]
221    pub mod ripemd_160;
222    pub use ripemd_160::RipeMd;
223
224    #[doc(hidden)]
225    pub mod md5;
226    #[doc(hidden)]
227    pub mod md4;
228
229    pub use md5::Md5;
230    pub use md4::Md4;
231
232    #[doc(hidden)]
233    pub mod sha;
234    pub use sha::Sha;
235
236    #[macro_use]
237    mod blake_api;
238
239    #[doc(hidden)]
240    pub mod blake2b;
241    pub use blake2b::Blake2b;
242
243    #[doc(hidden)]
244    pub mod blake2s;
245    pub use blake2s::Blake2s;
246}