/src/wolfssl-sp-math-all-8bit/wolfcrypt/src/hash.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* hash.c |
2 | | * |
3 | | * Copyright (C) 2006-2022 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | |
23 | | #ifdef HAVE_CONFIG_H |
24 | | #include <config.h> |
25 | | #endif |
26 | | |
27 | | #include <wolfssl/wolfcrypt/settings.h> |
28 | | #include <wolfssl/wolfcrypt/logging.h> |
29 | | #include <wolfssl/wolfcrypt/error-crypt.h> |
30 | | #ifndef NO_ASN |
31 | | #include <wolfssl/wolfcrypt/asn.h> |
32 | | #endif |
33 | | |
34 | | #include <wolfssl/wolfcrypt/hash.h> |
35 | | #include <wolfssl/wolfcrypt/hmac.h> |
36 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
37 | | |
38 | | #ifdef NO_INLINE |
39 | | #include <wolfssl/wolfcrypt/misc.h> |
40 | | #else |
41 | | #define WOLFSSL_MISC_INCLUDED |
42 | | #include <wolfcrypt/src/misc.c> |
43 | | #endif |
44 | | |
45 | | |
46 | | #ifdef NO_ASN |
47 | | enum Hash_Sum { |
48 | | MD2h = 646, |
49 | | MD5h = 649, |
50 | | SHAh = 88, |
51 | | SHA224h = 417, |
52 | | SHA256h = 414, |
53 | | SHA384h = 415, |
54 | | SHA512h = 416, |
55 | | SHA512_224h = 418, |
56 | | SHA512_256h = 419, |
57 | | SHA3_224h = 420, |
58 | | SHA3_256h = 421, |
59 | | SHA3_384h = 422, |
60 | | SHA3_512h = 423, |
61 | | SHAKE128h = 424, |
62 | | SHAKE256h = 425 |
63 | | }; |
64 | | #endif /* !NO_ASN */ |
65 | | |
66 | | #if !defined(NO_PWDBASED) || !defined(NO_ASN) |
67 | | /* function converts int hash type to enum */ |
68 | | enum wc_HashType wc_HashTypeConvert(int hashType) |
69 | 0 | { |
70 | | /* Default to hash type none as error */ |
71 | 0 | enum wc_HashType eHashType = WC_HASH_TYPE_NONE; |
72 | | #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) |
73 | | /* original FIPSv1 and CAVP selftest require a mapping for unique hash |
74 | | type to wc_HashType */ |
75 | | switch (hashType) { |
76 | | #ifndef NO_MD5 |
77 | | case WC_MD5: |
78 | | eHashType = WC_HASH_TYPE_MD5; |
79 | | break; |
80 | | #endif /* !NO_MD5 */ |
81 | | #ifndef NO_SHA |
82 | | case WC_SHA: |
83 | | eHashType = WC_HASH_TYPE_SHA; |
84 | | break; |
85 | | #endif /* !NO_SHA */ |
86 | | |
87 | | #ifdef WOLFSSL_SHA224 |
88 | | case WC_SHA224: |
89 | | eHashType = WC_HASH_TYPE_SHA224; |
90 | | break; |
91 | | #endif /* WOLFSSL_SHA224 */ |
92 | | |
93 | | #ifndef NO_SHA256 |
94 | | case WC_SHA256: |
95 | | eHashType = WC_HASH_TYPE_SHA256; |
96 | | break; |
97 | | #endif /* !NO_SHA256 */ |
98 | | |
99 | | #ifdef WOLFSSL_SHA384 |
100 | | case WC_SHA384: |
101 | | eHashType = WC_HASH_TYPE_SHA384; |
102 | | break; |
103 | | #endif /* WOLFSSL_SHA384 */ |
104 | | #ifdef WOLFSSL_SHA512 |
105 | | case WC_SHA512: |
106 | | eHashType = WC_HASH_TYPE_SHA512; |
107 | | break; |
108 | | |
109 | | #endif /* WOLFSSL_SHA512 */ |
110 | | #ifdef WOLFSSL_SHA3 |
111 | | case WC_SHA3_224: |
112 | | eHashType = WC_HASH_TYPE_SHA3_224; |
113 | | break; |
114 | | case WC_SHA3_256: |
115 | | eHashType = WC_HASH_TYPE_SHA3_256; |
116 | | break; |
117 | | case WC_SHA3_384: |
118 | | eHashType = WC_HASH_TYPE_SHA3_384; |
119 | | break; |
120 | | case WC_SHA3_512: |
121 | | eHashType = WC_HASH_TYPE_SHA3_512; |
122 | | break; |
123 | | #endif /* WOLFSSL_SHA3 */ |
124 | | default: |
125 | | eHashType = WC_HASH_TYPE_NONE; |
126 | | break; |
127 | | } |
128 | | #else |
129 | | /* current master uses same unique types as wc_HashType */ |
130 | 0 | if (hashType > 0 && hashType <= WC_HASH_TYPE_MAX) { |
131 | 0 | eHashType = (enum wc_HashType)hashType; |
132 | 0 | } |
133 | 0 | #endif |
134 | 0 | return eHashType; |
135 | 0 | } |
136 | | #endif /* !NO_PWDBASED || !NO_ASN */ |
137 | | |
138 | | #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) |
139 | | |
140 | | int wc_HashGetOID(enum wc_HashType hash_type) |
141 | | { |
142 | | int oid = HASH_TYPE_E; /* Default to hash type error */ |
143 | | switch(hash_type) |
144 | | { |
145 | | case WC_HASH_TYPE_MD2: |
146 | | #ifdef WOLFSSL_MD2 |
147 | | oid = MD2h; |
148 | | #endif |
149 | | break; |
150 | | case WC_HASH_TYPE_MD5_SHA: |
151 | | case WC_HASH_TYPE_MD5: |
152 | | #ifndef NO_MD5 |
153 | | oid = MD5h; |
154 | | #endif |
155 | | break; |
156 | | case WC_HASH_TYPE_SHA: |
157 | | #ifndef NO_SHA |
158 | | oid = SHAh; |
159 | | #endif |
160 | | break; |
161 | | case WC_HASH_TYPE_SHA224: |
162 | | #ifdef WOLFSSL_SHA224 |
163 | | oid = SHA224h; |
164 | | #endif |
165 | | break; |
166 | | case WC_HASH_TYPE_SHA256: |
167 | | #ifndef NO_SHA256 |
168 | | oid = SHA256h; |
169 | | #endif |
170 | | break; |
171 | | case WC_HASH_TYPE_SHA384: |
172 | | #ifdef WOLFSSL_SHA384 |
173 | | oid = SHA384h; |
174 | | #endif |
175 | | break; |
176 | | case WC_HASH_TYPE_SHA512: |
177 | | #ifdef WOLFSSL_SHA512 |
178 | | oid = SHA512h; |
179 | | #endif |
180 | | break; |
181 | | #ifndef WOLFSSL_NOSHA512_224 |
182 | | case WC_HASH_TYPE_SHA512_224: |
183 | | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
184 | | oid = SHA512_224h; |
185 | | #endif |
186 | | break; |
187 | | #endif |
188 | | #ifndef WOLFSSL_NOSHA512_256 |
189 | | case WC_HASH_TYPE_SHA512_256: |
190 | | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
191 | | oid = SHA512_256h; |
192 | | #endif |
193 | | break; |
194 | | #endif |
195 | | case WC_HASH_TYPE_SHA3_224: |
196 | | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
197 | | oid = SHA3_224h; |
198 | | #endif |
199 | | break; |
200 | | case WC_HASH_TYPE_SHA3_256: |
201 | | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
202 | | oid = SHA3_256h; |
203 | | #endif |
204 | | break; |
205 | | case WC_HASH_TYPE_SHA3_384: |
206 | | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
207 | | oid = SHA3_384h; |
208 | | #endif |
209 | | break; |
210 | | case WC_HASH_TYPE_SHA3_512: |
211 | | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
212 | | oid = SHA3_512h; |
213 | | #endif |
214 | | break; |
215 | | #ifndef WOLFSSL_NO_SHAKE256 |
216 | | case WC_HASH_TYPE_SHAKE128: |
217 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128) |
218 | | oid = SHAKE128h; |
219 | | #endif |
220 | | break; |
221 | | case WC_HASH_TYPE_SHAKE256: |
222 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256) |
223 | | oid = SHAKE256h; |
224 | | #endif |
225 | | break; |
226 | | #endif |
227 | | |
228 | | /* Not Supported */ |
229 | | case WC_HASH_TYPE_MD4: |
230 | | case WC_HASH_TYPE_BLAKE2B: |
231 | | case WC_HASH_TYPE_BLAKE2S: |
232 | | case WC_HASH_TYPE_NONE: |
233 | | default: |
234 | | oid = BAD_FUNC_ARG; |
235 | | break; |
236 | | } |
237 | | return oid; |
238 | | } |
239 | | |
240 | | enum wc_HashType wc_OidGetHash(int oid) |
241 | 0 | { |
242 | 0 | enum wc_HashType hash_type = WC_HASH_TYPE_NONE; |
243 | 0 | switch (oid) |
244 | 0 | { |
245 | 0 | #ifdef WOLFSSL_MD2 |
246 | 0 | case MD2h: |
247 | 0 | hash_type = WC_HASH_TYPE_MD2; |
248 | 0 | break; |
249 | 0 | #endif |
250 | 0 | case MD5h: |
251 | 0 | #ifndef NO_MD5 |
252 | 0 | hash_type = WC_HASH_TYPE_MD5; |
253 | 0 | #endif |
254 | 0 | break; |
255 | 0 | case SHAh: |
256 | 0 | #ifndef NO_SHA |
257 | 0 | hash_type = WC_HASH_TYPE_SHA; |
258 | 0 | #endif |
259 | 0 | break; |
260 | 0 | case SHA224h: |
261 | 0 | #ifdef WOLFSSL_SHA224 |
262 | 0 | hash_type = WC_HASH_TYPE_SHA224; |
263 | 0 | #endif |
264 | 0 | break; |
265 | 0 | case SHA256h: |
266 | 0 | #ifndef NO_SHA256 |
267 | 0 | hash_type = WC_HASH_TYPE_SHA256; |
268 | 0 | #endif |
269 | 0 | break; |
270 | 0 | case SHA384h: |
271 | 0 | #ifdef WOLFSSL_SHA384 |
272 | 0 | hash_type = WC_HASH_TYPE_SHA384; |
273 | 0 | #endif |
274 | 0 | break; |
275 | 0 | case SHA512h: |
276 | 0 | #ifdef WOLFSSL_SHA512 |
277 | 0 | hash_type = WC_HASH_TYPE_SHA512; |
278 | 0 | #endif |
279 | 0 | break; |
280 | 0 | #ifdef WOLFSSL_SHA3 |
281 | 0 | case SHA3_224h: |
282 | 0 | hash_type = WC_HASH_TYPE_SHA3_224; |
283 | 0 | break; |
284 | 0 | case SHA3_256h: |
285 | 0 | hash_type = WC_HASH_TYPE_SHA3_256; |
286 | 0 | break; |
287 | 0 | case SHA3_384h: |
288 | 0 | hash_type = WC_HASH_TYPE_SHA3_384; |
289 | 0 | break; |
290 | 0 | case SHA3_512h: |
291 | 0 | hash_type = WC_HASH_TYPE_SHA3_512; |
292 | 0 | break; |
293 | 0 | #endif /* WOLFSSL_SHA3 */ |
294 | 0 | default: |
295 | 0 | break; |
296 | 0 | } |
297 | 0 | return hash_type; |
298 | 0 | } |
299 | | #endif /* !NO_ASN || !NO_DH || HAVE_ECC */ |
300 | | |
301 | | #ifndef NO_HASH_WRAPPER |
302 | | |
303 | | /* Get Hash digest size */ |
304 | | int wc_HashGetDigestSize(enum wc_HashType hash_type) |
305 | 35.5k | { |
306 | 35.5k | int dig_size = HASH_TYPE_E; /* Default to hash type error */ |
307 | 35.5k | switch(hash_type) |
308 | 35.5k | { |
309 | 12 | case WC_HASH_TYPE_MD2: |
310 | 12 | #ifdef WOLFSSL_MD2 |
311 | 12 | dig_size = MD2_DIGEST_SIZE; |
312 | 12 | #endif |
313 | 12 | break; |
314 | 8 | case WC_HASH_TYPE_MD4: |
315 | 8 | #ifndef NO_MD4 |
316 | 8 | dig_size = MD4_DIGEST_SIZE; |
317 | 8 | #endif |
318 | 8 | break; |
319 | 497 | case WC_HASH_TYPE_MD5: |
320 | 497 | #ifndef NO_MD5 |
321 | 497 | dig_size = WC_MD5_DIGEST_SIZE; |
322 | 497 | #endif |
323 | 497 | break; |
324 | 1.58k | case WC_HASH_TYPE_SHA: |
325 | 1.58k | #ifndef NO_SHA |
326 | 1.58k | dig_size = WC_SHA_DIGEST_SIZE; |
327 | 1.58k | #endif |
328 | 1.58k | break; |
329 | 1.41k | case WC_HASH_TYPE_SHA224: |
330 | 1.41k | #ifdef WOLFSSL_SHA224 |
331 | 1.41k | dig_size = WC_SHA224_DIGEST_SIZE; |
332 | 1.41k | #endif |
333 | 1.41k | break; |
334 | 18.1k | case WC_HASH_TYPE_SHA256: |
335 | 18.1k | #ifndef NO_SHA256 |
336 | 18.1k | dig_size = WC_SHA256_DIGEST_SIZE; |
337 | 18.1k | #endif |
338 | 18.1k | break; |
339 | 1.66k | case WC_HASH_TYPE_SHA384: |
340 | 1.66k | #ifdef WOLFSSL_SHA384 |
341 | 1.66k | dig_size = WC_SHA384_DIGEST_SIZE; |
342 | 1.66k | #endif |
343 | 1.66k | break; |
344 | 4.36k | case WC_HASH_TYPE_SHA512: |
345 | 4.36k | #ifdef WOLFSSL_SHA512 |
346 | 4.36k | dig_size = WC_SHA512_DIGEST_SIZE; |
347 | 4.36k | #endif |
348 | 4.36k | break; |
349 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
350 | 602 | case WC_HASH_TYPE_SHA512_224: |
351 | 602 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
352 | 602 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
353 | 602 | dig_size = WC_SHA512_224_DIGEST_SIZE; |
354 | 602 | #endif |
355 | 602 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
356 | 602 | break; |
357 | 0 | #endif |
358 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
359 | 411 | case WC_HASH_TYPE_SHA512_256: |
360 | 411 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
361 | 411 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
362 | 411 | dig_size = WC_SHA512_256_DIGEST_SIZE; |
363 | 411 | #endif |
364 | 411 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
365 | 411 | break; |
366 | 0 | #endif |
367 | 6.27k | case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */ |
368 | 6.27k | #if !defined(NO_MD5) && !defined(NO_SHA) |
369 | 6.27k | dig_size = (int)WC_MD5_DIGEST_SIZE + (int)WC_SHA_DIGEST_SIZE; |
370 | 6.27k | #endif |
371 | 6.27k | break; |
372 | | |
373 | 74 | case WC_HASH_TYPE_SHA3_224: |
374 | 74 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
375 | 74 | dig_size = WC_SHA3_224_DIGEST_SIZE; |
376 | 74 | #endif |
377 | 74 | break; |
378 | 59 | case WC_HASH_TYPE_SHA3_256: |
379 | 59 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
380 | 59 | dig_size = WC_SHA3_256_DIGEST_SIZE; |
381 | 59 | #endif |
382 | 59 | break; |
383 | 81 | case WC_HASH_TYPE_SHA3_384: |
384 | 81 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
385 | 81 | dig_size = WC_SHA3_384_DIGEST_SIZE; |
386 | 81 | #endif |
387 | 81 | break; |
388 | 192 | case WC_HASH_TYPE_SHA3_512: |
389 | 192 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
390 | 192 | dig_size = WC_SHA3_512_DIGEST_SIZE; |
391 | 192 | #endif |
392 | 192 | break; |
393 | 3 | case WC_HASH_TYPE_BLAKE2B: |
394 | 41 | case WC_HASH_TYPE_BLAKE2S: |
395 | 41 | #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S) |
396 | 41 | dig_size = BLAKE2S_OUTBYTES; |
397 | 41 | #endif |
398 | 41 | break; |
399 | | |
400 | | /* Not Supported */ |
401 | 0 | #ifndef WOLFSSL_NO_SHAKE256 |
402 | 1 | case WC_HASH_TYPE_SHAKE128: |
403 | 4 | case WC_HASH_TYPE_SHAKE256: |
404 | 4 | #endif |
405 | 37 | case WC_HASH_TYPE_NONE: |
406 | 37 | default: |
407 | 37 | dig_size = BAD_FUNC_ARG; |
408 | 37 | break; |
409 | 35.5k | } |
410 | 35.5k | return dig_size; |
411 | 35.5k | } |
412 | | |
413 | | |
414 | | /* Get Hash block size */ |
415 | | int wc_HashGetBlockSize(enum wc_HashType hash_type) |
416 | 63 | { |
417 | 63 | int block_size = HASH_TYPE_E; /* Default to hash type error */ |
418 | 63 | switch (hash_type) |
419 | 63 | { |
420 | 0 | case WC_HASH_TYPE_MD2: |
421 | 0 | #ifdef WOLFSSL_MD2 |
422 | 0 | block_size = MD2_BLOCK_SIZE; |
423 | 0 | #endif |
424 | 0 | break; |
425 | 0 | case WC_HASH_TYPE_MD4: |
426 | 0 | #ifndef NO_MD4 |
427 | 0 | block_size = MD4_BLOCK_SIZE; |
428 | 0 | #endif |
429 | 0 | break; |
430 | 0 | case WC_HASH_TYPE_MD5: |
431 | 0 | #ifndef NO_MD5 |
432 | 0 | block_size = WC_MD5_BLOCK_SIZE; |
433 | 0 | #endif |
434 | 0 | break; |
435 | 32 | case WC_HASH_TYPE_SHA: |
436 | 32 | #ifndef NO_SHA |
437 | 32 | block_size = WC_SHA_BLOCK_SIZE; |
438 | 32 | #endif |
439 | 32 | break; |
440 | 0 | case WC_HASH_TYPE_SHA224: |
441 | 0 | #ifdef WOLFSSL_SHA224 |
442 | 0 | block_size = WC_SHA224_BLOCK_SIZE; |
443 | 0 | #endif |
444 | 0 | break; |
445 | 19 | case WC_HASH_TYPE_SHA256: |
446 | 19 | #ifndef NO_SHA256 |
447 | 19 | block_size = WC_SHA256_BLOCK_SIZE; |
448 | 19 | #endif |
449 | 19 | break; |
450 | 12 | case WC_HASH_TYPE_SHA384: |
451 | 12 | #ifdef WOLFSSL_SHA384 |
452 | 12 | block_size = WC_SHA384_BLOCK_SIZE; |
453 | 12 | #endif |
454 | 12 | break; |
455 | 0 | case WC_HASH_TYPE_SHA512: |
456 | 0 | #ifdef WOLFSSL_SHA512 |
457 | 0 | block_size = WC_SHA512_BLOCK_SIZE; |
458 | 0 | #endif |
459 | 0 | break; |
460 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
461 | 0 | case WC_HASH_TYPE_SHA512_224: |
462 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
463 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
464 | 0 | block_size = WC_SHA512_224_BLOCK_SIZE; |
465 | 0 | #endif |
466 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
467 | 0 | break; |
468 | 0 | #endif |
469 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
470 | 0 | case WC_HASH_TYPE_SHA512_256: |
471 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
472 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
473 | 0 | block_size = WC_SHA512_256_BLOCK_SIZE; |
474 | 0 | #endif |
475 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
476 | 0 | break; |
477 | 0 | #endif |
478 | 0 | case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */ |
479 | 0 | #if !defined(NO_MD5) && !defined(NO_SHA) |
480 | 0 | block_size = (int)WC_MD5_BLOCK_SIZE + (int)WC_SHA_BLOCK_SIZE; |
481 | 0 | #endif |
482 | 0 | break; |
483 | | |
484 | 0 | case WC_HASH_TYPE_SHA3_224: |
485 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
486 | 0 | block_size = WC_SHA3_224_BLOCK_SIZE; |
487 | 0 | #endif |
488 | 0 | break; |
489 | 0 | case WC_HASH_TYPE_SHA3_256: |
490 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
491 | 0 | block_size = WC_SHA3_256_BLOCK_SIZE; |
492 | 0 | #endif |
493 | 0 | break; |
494 | 0 | case WC_HASH_TYPE_SHA3_384: |
495 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
496 | 0 | block_size = WC_SHA3_384_BLOCK_SIZE; |
497 | 0 | #endif |
498 | 0 | break; |
499 | 0 | case WC_HASH_TYPE_SHA3_512: |
500 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
501 | 0 | block_size = WC_SHA3_512_BLOCK_SIZE; |
502 | 0 | #endif |
503 | 0 | break; |
504 | 0 | case WC_HASH_TYPE_BLAKE2B: |
505 | 0 | case WC_HASH_TYPE_BLAKE2S: |
506 | 0 | #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S) |
507 | 0 | block_size = BLAKE2S_BLOCKBYTES; |
508 | 0 | #endif |
509 | 0 | break; |
510 | | |
511 | | /* Not Supported */ |
512 | 0 | #ifndef WOLFSSL_NO_SHAKE256 |
513 | 0 | case WC_HASH_TYPE_SHAKE128: |
514 | 0 | case WC_HASH_TYPE_SHAKE256: |
515 | 0 | #endif |
516 | 0 | case WC_HASH_TYPE_NONE: |
517 | 0 | default: |
518 | 0 | block_size = BAD_FUNC_ARG; |
519 | 0 | break; |
520 | 63 | } |
521 | 63 | return block_size; |
522 | 63 | } |
523 | | |
524 | | /* Generic Hashing Wrapper */ |
525 | | int wc_Hash(enum wc_HashType hash_type, const byte* data, |
526 | | word32 data_len, byte* hash, word32 hash_len) |
527 | 21.9k | { |
528 | 21.9k | int ret = HASH_TYPE_E; /* Default to hash type error */ |
529 | 21.9k | word32 dig_size; |
530 | | |
531 | | /* Validate hash buffer size */ |
532 | 21.9k | dig_size = wc_HashGetDigestSize(hash_type); |
533 | 21.9k | if (hash_len < dig_size) { |
534 | 0 | return BUFFER_E; |
535 | 0 | } |
536 | | |
537 | | /* Suppress possible unused arg if all hashing is disabled */ |
538 | 21.9k | (void)data; |
539 | 21.9k | (void)data_len; |
540 | 21.9k | (void)hash; |
541 | 21.9k | (void)hash_len; |
542 | | |
543 | 21.9k | switch(hash_type) |
544 | 21.9k | { |
545 | 212 | case WC_HASH_TYPE_MD5: |
546 | 212 | #ifndef NO_MD5 |
547 | 212 | ret = wc_Md5Hash(data, data_len, hash); |
548 | 212 | #endif |
549 | 212 | break; |
550 | 1.31k | case WC_HASH_TYPE_SHA: |
551 | 1.31k | #ifndef NO_SHA |
552 | 1.31k | ret = wc_ShaHash(data, data_len, hash); |
553 | 1.31k | #endif |
554 | 1.31k | break; |
555 | 1.12k | case WC_HASH_TYPE_SHA224: |
556 | 1.12k | #ifdef WOLFSSL_SHA224 |
557 | 1.12k | ret = wc_Sha224Hash(data, data_len, hash); |
558 | 1.12k | #endif |
559 | 1.12k | break; |
560 | 11.4k | case WC_HASH_TYPE_SHA256: |
561 | 11.4k | #ifndef NO_SHA256 |
562 | 11.4k | ret = wc_Sha256Hash(data, data_len, hash); |
563 | 11.4k | #endif |
564 | 11.4k | break; |
565 | 1.14k | case WC_HASH_TYPE_SHA384: |
566 | 1.14k | #ifdef WOLFSSL_SHA384 |
567 | 1.14k | ret = wc_Sha384Hash(data, data_len, hash); |
568 | 1.14k | #endif |
569 | 1.14k | break; |
570 | 2.64k | case WC_HASH_TYPE_SHA512: |
571 | 2.64k | #ifdef WOLFSSL_SHA512 |
572 | 2.64k | ret = wc_Sha512Hash(data, data_len, hash); |
573 | 2.64k | #endif |
574 | 2.64k | break; |
575 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
576 | 482 | case WC_HASH_TYPE_SHA512_224: |
577 | 482 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
578 | 482 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
579 | 482 | ret = wc_Sha512_224Hash(data, data_len, hash); |
580 | 482 | #endif |
581 | 482 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
582 | 482 | break; |
583 | 0 | #endif |
584 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
585 | 320 | case WC_HASH_TYPE_SHA512_256: |
586 | 320 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
587 | 320 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
588 | 320 | ret = wc_Sha512_256Hash(data, data_len, hash); |
589 | 320 | #endif |
590 | 320 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
591 | 320 | break; |
592 | 0 | #endif |
593 | 3.11k | case WC_HASH_TYPE_MD5_SHA: |
594 | 3.11k | #if !defined(NO_MD5) && !defined(NO_SHA) |
595 | 3.11k | ret = wc_Md5Hash(data, data_len, hash); |
596 | 3.11k | if (ret == 0) { |
597 | 3.11k | ret = wc_ShaHash(data, data_len, &hash[WC_MD5_DIGEST_SIZE]); |
598 | 3.11k | } |
599 | 3.11k | #endif |
600 | 3.11k | break; |
601 | | |
602 | 33 | case WC_HASH_TYPE_SHA3_224: |
603 | 33 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
604 | 33 | ret = wc_Sha3_224Hash(data, data_len, hash); |
605 | 33 | #endif |
606 | 33 | break; |
607 | 29 | case WC_HASH_TYPE_SHA3_256: |
608 | 29 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
609 | 29 | ret = wc_Sha3_256Hash(data, data_len, hash); |
610 | 29 | #endif |
611 | 29 | break; |
612 | 35 | case WC_HASH_TYPE_SHA3_384: |
613 | 35 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
614 | 35 | ret = wc_Sha3_384Hash(data, data_len, hash); |
615 | 35 | #endif |
616 | 35 | break; |
617 | 85 | case WC_HASH_TYPE_SHA3_512: |
618 | 85 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
619 | 85 | ret = wc_Sha3_512Hash(data, data_len, hash); |
620 | 85 | #endif |
621 | 85 | break; |
622 | | |
623 | | /* Not Supported */ |
624 | 0 | case WC_HASH_TYPE_MD2: |
625 | 0 | case WC_HASH_TYPE_MD4: |
626 | 1 | case WC_HASH_TYPE_BLAKE2B: |
627 | 2 | case WC_HASH_TYPE_BLAKE2S: |
628 | 2 | #ifndef WOLFSSL_NO_SHAKE256 |
629 | 2 | case WC_HASH_TYPE_SHAKE128: |
630 | 2 | case WC_HASH_TYPE_SHAKE256: |
631 | 2 | #endif |
632 | 2 | case WC_HASH_TYPE_NONE: |
633 | 2 | default: |
634 | 2 | ret = BAD_FUNC_ARG; |
635 | 2 | break; |
636 | 21.9k | } |
637 | 21.9k | return ret; |
638 | 21.9k | } |
639 | | |
640 | | int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap, |
641 | | int devId) |
642 | 63 | { |
643 | 63 | int ret = HASH_TYPE_E; /* Default to hash type error */ |
644 | | |
645 | 63 | if (hash == NULL) |
646 | 0 | return BAD_FUNC_ARG; |
647 | | |
648 | 63 | switch (type) { |
649 | 0 | case WC_HASH_TYPE_MD5: |
650 | 0 | #ifndef NO_MD5 |
651 | 0 | ret = wc_InitMd5_ex(&hash->md5, heap, devId); |
652 | 0 | #endif |
653 | 0 | break; |
654 | 32 | case WC_HASH_TYPE_SHA: |
655 | 32 | #ifndef NO_SHA |
656 | 32 | ret = wc_InitSha_ex(&hash->sha, heap, devId); |
657 | 32 | #endif |
658 | 32 | break; |
659 | 0 | case WC_HASH_TYPE_SHA224: |
660 | 0 | #ifdef WOLFSSL_SHA224 |
661 | 0 | ret = wc_InitSha224_ex(&hash->sha224, heap, devId); |
662 | 0 | #endif |
663 | 0 | break; |
664 | 19 | case WC_HASH_TYPE_SHA256: |
665 | 19 | #ifndef NO_SHA256 |
666 | 19 | ret = wc_InitSha256_ex(&hash->sha256, heap, devId); |
667 | 19 | #endif |
668 | 19 | break; |
669 | 12 | case WC_HASH_TYPE_SHA384: |
670 | 12 | #ifdef WOLFSSL_SHA384 |
671 | 12 | ret = wc_InitSha384_ex(&hash->sha384, heap, devId); |
672 | 12 | #endif |
673 | 12 | break; |
674 | 0 | case WC_HASH_TYPE_SHA512: |
675 | 0 | #ifdef WOLFSSL_SHA512 |
676 | 0 | ret = wc_InitSha512_ex(&hash->sha512, heap, devId); |
677 | 0 | #endif |
678 | 0 | break; |
679 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
680 | 0 | case WC_HASH_TYPE_SHA512_224: |
681 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
682 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
683 | 0 | ret = wc_InitSha512_224_ex(&hash->sha512, heap, devId); |
684 | 0 | #endif |
685 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
686 | 0 | break; |
687 | 0 | #endif |
688 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
689 | 0 | case WC_HASH_TYPE_SHA512_256: |
690 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
691 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
692 | 0 | ret = wc_InitSha512_256_ex(&hash->sha512, heap, devId); |
693 | 0 | #endif |
694 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
695 | 0 | break; |
696 | 0 | #endif |
697 | 0 | case WC_HASH_TYPE_SHA3_224: |
698 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
699 | 0 | ret = wc_InitSha3_224(&hash->sha3, heap, devId); |
700 | 0 | #endif |
701 | 0 | break; |
702 | 0 | case WC_HASH_TYPE_SHA3_256: |
703 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
704 | 0 | ret = wc_InitSha3_256(&hash->sha3, heap, devId); |
705 | 0 | #endif |
706 | 0 | break; |
707 | 0 | case WC_HASH_TYPE_SHA3_384: |
708 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
709 | 0 | ret = wc_InitSha3_384(&hash->sha3, heap, devId); |
710 | 0 | #endif |
711 | 0 | break; |
712 | 0 | case WC_HASH_TYPE_SHA3_512: |
713 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
714 | 0 | ret = wc_InitSha3_512(&hash->sha3, heap, devId); |
715 | 0 | #endif |
716 | 0 | break; |
717 | | |
718 | | /* not supported */ |
719 | 0 | case WC_HASH_TYPE_MD5_SHA: |
720 | 0 | case WC_HASH_TYPE_MD2: |
721 | 0 | case WC_HASH_TYPE_MD4: |
722 | 0 | case WC_HASH_TYPE_BLAKE2B: |
723 | 0 | case WC_HASH_TYPE_BLAKE2S: |
724 | 0 | #ifndef WOLFSSL_NO_SHAKE256 |
725 | 0 | case WC_HASH_TYPE_SHAKE128: |
726 | 0 | case WC_HASH_TYPE_SHAKE256: |
727 | 0 | #endif |
728 | 0 | case WC_HASH_TYPE_NONE: |
729 | 0 | default: |
730 | 0 | ret = BAD_FUNC_ARG; |
731 | 63 | }; |
732 | | |
733 | 63 | (void)heap; |
734 | 63 | (void)devId; |
735 | | |
736 | 63 | return ret; |
737 | 63 | } |
738 | | |
739 | | int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type) |
740 | 81.6k | { |
741 | 81.6k | return wc_HashInit_ex(hash, type, NULL, INVALID_DEVID); |
742 | 81.6k | } |
743 | | |
744 | | int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data, |
745 | | word32 dataSz) |
746 | 126 | { |
747 | 126 | int ret = HASH_TYPE_E; /* Default to hash type error */ |
748 | | |
749 | 126 | if (hash == NULL || (data == NULL && dataSz > 0)) |
750 | 0 | return BAD_FUNC_ARG; |
751 | | |
752 | 126 | switch (type) { |
753 | 0 | case WC_HASH_TYPE_MD5: |
754 | 0 | #ifndef NO_MD5 |
755 | 0 | ret = wc_Md5Update(&hash->md5, data, dataSz); |
756 | 0 | #endif |
757 | 0 | break; |
758 | 64 | case WC_HASH_TYPE_SHA: |
759 | 64 | #ifndef NO_SHA |
760 | 64 | ret = wc_ShaUpdate(&hash->sha, data, dataSz); |
761 | 64 | #endif |
762 | 64 | break; |
763 | 0 | case WC_HASH_TYPE_SHA224: |
764 | 0 | #ifdef WOLFSSL_SHA224 |
765 | 0 | ret = wc_Sha224Update(&hash->sha224, data, dataSz); |
766 | 0 | #endif |
767 | 0 | break; |
768 | 38 | case WC_HASH_TYPE_SHA256: |
769 | 38 | #ifndef NO_SHA256 |
770 | 38 | ret = wc_Sha256Update(&hash->sha256, data, dataSz); |
771 | 38 | #endif |
772 | 38 | break; |
773 | 24 | case WC_HASH_TYPE_SHA384: |
774 | 24 | #ifdef WOLFSSL_SHA384 |
775 | 24 | ret = wc_Sha384Update(&hash->sha384, data, dataSz); |
776 | 24 | #endif |
777 | 24 | break; |
778 | 0 | case WC_HASH_TYPE_SHA512: |
779 | 0 | #ifdef WOLFSSL_SHA512 |
780 | 0 | ret = wc_Sha512Update(&hash->sha512, data, dataSz); |
781 | 0 | #endif |
782 | 0 | break; |
783 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
784 | 0 | case WC_HASH_TYPE_SHA512_224: |
785 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
786 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
787 | 0 | ret = wc_Sha512_224Update(&hash->sha512, data, dataSz); |
788 | 0 | #endif |
789 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
790 | 0 | break; |
791 | 0 | #endif |
792 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
793 | 0 | case WC_HASH_TYPE_SHA512_256: |
794 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
795 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
796 | 0 | ret = wc_Sha512_256Update(&hash->sha512, data, dataSz); |
797 | 0 | #endif |
798 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
799 | 0 | break; |
800 | 0 | #endif |
801 | 0 | case WC_HASH_TYPE_SHA3_224: |
802 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
803 | 0 | ret = wc_Sha3_224_Update(&hash->sha3, data, dataSz); |
804 | 0 | #endif |
805 | 0 | break; |
806 | 0 | case WC_HASH_TYPE_SHA3_256: |
807 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
808 | 0 | ret = wc_Sha3_256_Update(&hash->sha3, data, dataSz); |
809 | 0 | #endif |
810 | 0 | break; |
811 | 0 | case WC_HASH_TYPE_SHA3_384: |
812 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
813 | 0 | ret = wc_Sha3_384_Update(&hash->sha3, data, dataSz); |
814 | 0 | #endif |
815 | 0 | break; |
816 | 0 | case WC_HASH_TYPE_SHA3_512: |
817 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
818 | 0 | ret = wc_Sha3_512_Update(&hash->sha3, data, dataSz); |
819 | 0 | #endif |
820 | 0 | break; |
821 | | |
822 | | /* not supported */ |
823 | 0 | case WC_HASH_TYPE_MD5_SHA: |
824 | 0 | case WC_HASH_TYPE_MD2: |
825 | 0 | case WC_HASH_TYPE_MD4: |
826 | 0 | case WC_HASH_TYPE_BLAKE2B: |
827 | 0 | case WC_HASH_TYPE_BLAKE2S: |
828 | 0 | #ifndef WOLFSSL_NO_SHAKE256 |
829 | 0 | case WC_HASH_TYPE_SHAKE128: |
830 | 0 | case WC_HASH_TYPE_SHAKE256: |
831 | 0 | #endif |
832 | 0 | case WC_HASH_TYPE_NONE: |
833 | 0 | default: |
834 | 0 | ret = BAD_FUNC_ARG; |
835 | 126 | }; |
836 | | |
837 | 126 | return ret; |
838 | 126 | } |
839 | | |
840 | | int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out) |
841 | 63 | { |
842 | 63 | int ret = HASH_TYPE_E; /* Default to hash type error */ |
843 | | |
844 | 63 | if (hash == NULL || out == NULL) |
845 | 0 | return BAD_FUNC_ARG; |
846 | | |
847 | 63 | switch (type) { |
848 | 0 | case WC_HASH_TYPE_MD5: |
849 | 0 | #ifndef NO_MD5 |
850 | 0 | ret = wc_Md5Final(&hash->md5, out); |
851 | 0 | #endif |
852 | 0 | break; |
853 | 32 | case WC_HASH_TYPE_SHA: |
854 | 32 | #ifndef NO_SHA |
855 | 32 | ret = wc_ShaFinal(&hash->sha, out); |
856 | 32 | #endif |
857 | 32 | break; |
858 | 0 | case WC_HASH_TYPE_SHA224: |
859 | 0 | #ifdef WOLFSSL_SHA224 |
860 | 0 | ret = wc_Sha224Final(&hash->sha224, out); |
861 | 0 | #endif |
862 | 0 | break; |
863 | 19 | case WC_HASH_TYPE_SHA256: |
864 | 19 | #ifndef NO_SHA256 |
865 | 19 | ret = wc_Sha256Final(&hash->sha256, out); |
866 | 19 | #endif |
867 | 19 | break; |
868 | 12 | case WC_HASH_TYPE_SHA384: |
869 | 12 | #ifdef WOLFSSL_SHA384 |
870 | 12 | ret = wc_Sha384Final(&hash->sha384, out); |
871 | 12 | #endif |
872 | 12 | break; |
873 | 0 | case WC_HASH_TYPE_SHA512: |
874 | 0 | #ifdef WOLFSSL_SHA512 |
875 | 0 | ret = wc_Sha512Final(&hash->sha512, out); |
876 | 0 | #endif |
877 | 0 | break; |
878 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
879 | 0 | case WC_HASH_TYPE_SHA512_224: |
880 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
881 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
882 | 0 | ret = wc_Sha512_224Final(&hash->sha512, out); |
883 | 0 | #endif |
884 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
885 | 0 | break; |
886 | 0 | #endif |
887 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
888 | 0 | case WC_HASH_TYPE_SHA512_256: |
889 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
890 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
891 | 0 | ret = wc_Sha512_256Final(&hash->sha512, out); |
892 | 0 | #endif |
893 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
894 | 0 | break; |
895 | 0 | #endif |
896 | 0 | case WC_HASH_TYPE_SHA3_224: |
897 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
898 | 0 | ret = wc_Sha3_224_Final(&hash->sha3, out); |
899 | 0 | #endif |
900 | 0 | break; |
901 | 0 | case WC_HASH_TYPE_SHA3_256: |
902 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
903 | 0 | ret = wc_Sha3_256_Final(&hash->sha3, out); |
904 | 0 | #endif |
905 | 0 | break; |
906 | 0 | case WC_HASH_TYPE_SHA3_384: |
907 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
908 | 0 | ret = wc_Sha3_384_Final(&hash->sha3, out); |
909 | 0 | #endif |
910 | 0 | break; |
911 | 0 | case WC_HASH_TYPE_SHA3_512: |
912 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
913 | 0 | ret = wc_Sha3_512_Final(&hash->sha3, out); |
914 | 0 | #endif |
915 | 0 | break; |
916 | | |
917 | | /* not supported */ |
918 | 0 | case WC_HASH_TYPE_MD5_SHA: |
919 | 0 | case WC_HASH_TYPE_MD2: |
920 | 0 | case WC_HASH_TYPE_MD4: |
921 | 0 | case WC_HASH_TYPE_BLAKE2B: |
922 | 0 | case WC_HASH_TYPE_BLAKE2S: |
923 | 0 | #ifndef WOLFSSL_NO_SHAKE256 |
924 | 0 | case WC_HASH_TYPE_SHAKE128: |
925 | 0 | case WC_HASH_TYPE_SHAKE256: |
926 | 0 | #endif |
927 | 0 | case WC_HASH_TYPE_NONE: |
928 | 0 | default: |
929 | 0 | ret = BAD_FUNC_ARG; |
930 | 63 | }; |
931 | | |
932 | 63 | return ret; |
933 | 63 | } |
934 | | |
935 | | int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type) |
936 | 63 | { |
937 | 63 | int ret = HASH_TYPE_E; /* Default to hash type error */ |
938 | | |
939 | 63 | if (hash == NULL) |
940 | 0 | return BAD_FUNC_ARG; |
941 | | |
942 | 63 | switch (type) { |
943 | 0 | case WC_HASH_TYPE_MD5: |
944 | 0 | #ifndef NO_MD5 |
945 | 0 | wc_Md5Free(&hash->md5); |
946 | 0 | ret = 0; |
947 | 0 | #endif |
948 | 0 | break; |
949 | 32 | case WC_HASH_TYPE_SHA: |
950 | 32 | #ifndef NO_SHA |
951 | 32 | wc_ShaFree(&hash->sha); |
952 | 32 | ret = 0; |
953 | 32 | #endif |
954 | 32 | break; |
955 | 0 | case WC_HASH_TYPE_SHA224: |
956 | 0 | #ifdef WOLFSSL_SHA224 |
957 | 0 | wc_Sha224Free(&hash->sha224); |
958 | 0 | ret = 0; |
959 | 0 | #endif |
960 | 0 | break; |
961 | 19 | case WC_HASH_TYPE_SHA256: |
962 | 19 | #ifndef NO_SHA256 |
963 | 19 | wc_Sha256Free(&hash->sha256); |
964 | 19 | ret = 0; |
965 | 19 | #endif |
966 | 19 | break; |
967 | 12 | case WC_HASH_TYPE_SHA384: |
968 | 12 | #ifdef WOLFSSL_SHA384 |
969 | 12 | wc_Sha384Free(&hash->sha384); |
970 | 12 | ret = 0; |
971 | 12 | #endif |
972 | 12 | break; |
973 | 0 | case WC_HASH_TYPE_SHA512: |
974 | 0 | #ifdef WOLFSSL_SHA512 |
975 | 0 | wc_Sha512Free(&hash->sha512); |
976 | 0 | ret = 0; |
977 | 0 | #endif |
978 | 0 | break; |
979 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
980 | 0 | case WC_HASH_TYPE_SHA512_224: |
981 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
982 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
983 | 0 | wc_Sha512_224Free(&hash->sha512); |
984 | 0 | ret = 0; |
985 | 0 | #endif |
986 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
987 | 0 | break; |
988 | 0 | #endif |
989 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
990 | 0 | case WC_HASH_TYPE_SHA512_256: |
991 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
992 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
993 | 0 | wc_Sha512_256Free(&hash->sha512); |
994 | 0 | ret = 0; |
995 | 0 | #endif |
996 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
997 | 0 | break; |
998 | 0 | #endif |
999 | 0 | case WC_HASH_TYPE_SHA3_224: |
1000 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
1001 | 0 | wc_Sha3_224_Free(&hash->sha3); |
1002 | 0 | ret = 0; |
1003 | 0 | #endif |
1004 | 0 | break; |
1005 | 0 | case WC_HASH_TYPE_SHA3_256: |
1006 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
1007 | 0 | wc_Sha3_256_Free(&hash->sha3); |
1008 | 0 | ret = 0; |
1009 | 0 | #endif |
1010 | 0 | break; |
1011 | 0 | case WC_HASH_TYPE_SHA3_384: |
1012 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
1013 | 0 | wc_Sha3_384_Free(&hash->sha3); |
1014 | 0 | ret = 0; |
1015 | 0 | #endif |
1016 | 0 | break; |
1017 | 0 | case WC_HASH_TYPE_SHA3_512: |
1018 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
1019 | 0 | wc_Sha3_512_Free(&hash->sha3); |
1020 | 0 | ret = 0; |
1021 | 0 | #endif |
1022 | 0 | break; |
1023 | | |
1024 | | /* not supported */ |
1025 | 0 | case WC_HASH_TYPE_MD5_SHA: |
1026 | 0 | case WC_HASH_TYPE_MD2: |
1027 | 0 | case WC_HASH_TYPE_MD4: |
1028 | 0 | case WC_HASH_TYPE_BLAKE2B: |
1029 | 0 | case WC_HASH_TYPE_BLAKE2S: |
1030 | 0 | #ifndef WOLFSSL_NO_SHAKE256 |
1031 | 0 | case WC_HASH_TYPE_SHAKE128: |
1032 | 0 | case WC_HASH_TYPE_SHAKE256: |
1033 | 0 | #endif |
1034 | 0 | case WC_HASH_TYPE_NONE: |
1035 | 0 | default: |
1036 | 0 | ret = BAD_FUNC_ARG; |
1037 | 63 | }; |
1038 | | |
1039 | 63 | return ret; |
1040 | 63 | } |
1041 | | |
1042 | | #ifdef WOLFSSL_HASH_FLAGS |
1043 | | int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type, word32 flags) |
1044 | 0 | { |
1045 | 0 | int ret = HASH_TYPE_E; /* Default to hash type error */ |
1046 | |
|
1047 | 0 | if (hash == NULL) |
1048 | 0 | return BAD_FUNC_ARG; |
1049 | | |
1050 | 0 | switch (type) { |
1051 | 0 | case WC_HASH_TYPE_MD5: |
1052 | 0 | #ifndef NO_MD5 |
1053 | 0 | ret = wc_Md5SetFlags(&hash->md5, flags); |
1054 | 0 | #endif |
1055 | 0 | break; |
1056 | 0 | case WC_HASH_TYPE_SHA: |
1057 | 0 | #ifndef NO_SHA |
1058 | 0 | ret = wc_ShaSetFlags(&hash->sha, flags); |
1059 | 0 | #endif |
1060 | 0 | break; |
1061 | 0 | case WC_HASH_TYPE_SHA224: |
1062 | 0 | #ifdef WOLFSSL_SHA224 |
1063 | 0 | ret = wc_Sha224SetFlags(&hash->sha224, flags); |
1064 | 0 | #endif |
1065 | 0 | break; |
1066 | 0 | case WC_HASH_TYPE_SHA256: |
1067 | 0 | #ifndef NO_SHA256 |
1068 | 0 | ret = wc_Sha256SetFlags(&hash->sha256, flags); |
1069 | 0 | #endif |
1070 | 0 | break; |
1071 | 0 | case WC_HASH_TYPE_SHA384: |
1072 | 0 | #ifdef WOLFSSL_SHA384 |
1073 | 0 | ret = wc_Sha384SetFlags(&hash->sha384, flags); |
1074 | 0 | #endif |
1075 | 0 | break; |
1076 | 0 | case WC_HASH_TYPE_SHA512: |
1077 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
1078 | 0 | case WC_HASH_TYPE_SHA512_224: |
1079 | 0 | #endif |
1080 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
1081 | 0 | case WC_HASH_TYPE_SHA512_256: |
1082 | 0 | #endif |
1083 | 0 | #ifdef WOLFSSL_SHA512 |
1084 | 0 | ret = wc_Sha512SetFlags(&hash->sha512, flags); |
1085 | 0 | #endif |
1086 | 0 | break; |
1087 | | |
1088 | 0 | case WC_HASH_TYPE_SHA3_224: |
1089 | 0 | case WC_HASH_TYPE_SHA3_256: |
1090 | 0 | case WC_HASH_TYPE_SHA3_384: |
1091 | 0 | case WC_HASH_TYPE_SHA3_512: |
1092 | 0 | #ifdef WOLFSSL_SHA3 |
1093 | 0 | ret = wc_Sha3_SetFlags(&hash->sha3, flags); |
1094 | 0 | #endif |
1095 | 0 | break; |
1096 | | |
1097 | | /* not supported */ |
1098 | 0 | case WC_HASH_TYPE_MD5_SHA: |
1099 | 0 | case WC_HASH_TYPE_MD2: |
1100 | 0 | case WC_HASH_TYPE_MD4: |
1101 | 0 | case WC_HASH_TYPE_BLAKE2B: |
1102 | 0 | case WC_HASH_TYPE_BLAKE2S: |
1103 | 0 | case WC_HASH_TYPE_NONE: |
1104 | 0 | #ifndef WOLFSSL_NO_SHAKE256 |
1105 | 0 | case WC_HASH_TYPE_SHAKE128: |
1106 | 0 | case WC_HASH_TYPE_SHAKE256: |
1107 | 0 | #endif |
1108 | 0 | default: |
1109 | 0 | ret = BAD_FUNC_ARG; |
1110 | 0 | }; |
1111 | |
|
1112 | 0 | return ret; |
1113 | 0 | } |
1114 | | int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, word32* flags) |
1115 | 0 | { |
1116 | 0 | int ret = HASH_TYPE_E; /* Default to hash type error */ |
1117 | |
|
1118 | 0 | if (hash == NULL) |
1119 | 0 | return BAD_FUNC_ARG; |
1120 | | |
1121 | 0 | switch (type) { |
1122 | 0 | case WC_HASH_TYPE_MD5: |
1123 | 0 | #ifndef NO_MD5 |
1124 | 0 | ret = wc_Md5GetFlags(&hash->md5, flags); |
1125 | 0 | #endif |
1126 | 0 | break; |
1127 | 0 | case WC_HASH_TYPE_SHA: |
1128 | 0 | #ifndef NO_SHA |
1129 | 0 | ret = wc_ShaGetFlags(&hash->sha, flags); |
1130 | 0 | #endif |
1131 | 0 | break; |
1132 | 0 | case WC_HASH_TYPE_SHA224: |
1133 | 0 | #ifdef WOLFSSL_SHA224 |
1134 | 0 | ret = wc_Sha224GetFlags(&hash->sha224, flags); |
1135 | 0 | #endif |
1136 | 0 | break; |
1137 | 0 | case WC_HASH_TYPE_SHA256: |
1138 | 0 | #ifndef NO_SHA256 |
1139 | 0 | ret = wc_Sha256GetFlags(&hash->sha256, flags); |
1140 | 0 | #endif |
1141 | 0 | break; |
1142 | 0 | case WC_HASH_TYPE_SHA384: |
1143 | 0 | #ifdef WOLFSSL_SHA384 |
1144 | 0 | ret = wc_Sha384GetFlags(&hash->sha384, flags); |
1145 | 0 | #endif |
1146 | 0 | break; |
1147 | 0 | case WC_HASH_TYPE_SHA512: |
1148 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
1149 | 0 | case WC_HASH_TYPE_SHA512_224: |
1150 | 0 | #endif |
1151 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
1152 | 0 | case WC_HASH_TYPE_SHA512_256: |
1153 | 0 | #endif |
1154 | 0 | #ifdef WOLFSSL_SHA512 |
1155 | 0 | ret = wc_Sha512GetFlags(&hash->sha512, flags); |
1156 | 0 | #endif |
1157 | 0 | break; |
1158 | | |
1159 | 0 | case WC_HASH_TYPE_SHA3_224: |
1160 | 0 | case WC_HASH_TYPE_SHA3_256: |
1161 | 0 | case WC_HASH_TYPE_SHA3_384: |
1162 | 0 | case WC_HASH_TYPE_SHA3_512: |
1163 | 0 | #ifdef WOLFSSL_SHA3 |
1164 | 0 | ret = wc_Sha3_GetFlags(&hash->sha3, flags); |
1165 | 0 | #endif |
1166 | 0 | break; |
1167 | | |
1168 | | /* not supported */ |
1169 | 0 | case WC_HASH_TYPE_MD5_SHA: |
1170 | 0 | case WC_HASH_TYPE_MD2: |
1171 | 0 | case WC_HASH_TYPE_MD4: |
1172 | 0 | case WC_HASH_TYPE_BLAKE2B: |
1173 | 0 | case WC_HASH_TYPE_BLAKE2S: |
1174 | 0 | #ifndef WOLFSSL_NO_SHAKE256 |
1175 | 0 | case WC_HASH_TYPE_SHAKE128: |
1176 | 0 | case WC_HASH_TYPE_SHAKE256: |
1177 | 0 | #endif |
1178 | 0 | case WC_HASH_TYPE_NONE: |
1179 | 0 | default: |
1180 | 0 | ret = BAD_FUNC_ARG; |
1181 | 0 | }; |
1182 | |
|
1183 | 0 | return ret; |
1184 | 0 | } |
1185 | | #endif /* WOLFSSL_HASH_FLAGS */ |
1186 | | |
1187 | | |
1188 | | #if !defined(WOLFSSL_TI_HASH) |
1189 | | |
1190 | | #if !defined(NO_MD5) |
1191 | | int wc_Md5Hash(const byte* data, word32 len, byte* hash) |
1192 | | { |
1193 | | int ret; |
1194 | | #ifdef WOLFSSL_SMALL_STACK |
1195 | | wc_Md5* md5; |
1196 | | #else |
1197 | | wc_Md5 md5[1]; |
1198 | | #endif |
1199 | | |
1200 | | #ifdef WOLFSSL_SMALL_STACK |
1201 | | md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1202 | | if (md5 == NULL) |
1203 | | return MEMORY_E; |
1204 | | #endif |
1205 | | |
1206 | | if ((ret = wc_InitMd5(md5)) != 0) { |
1207 | | WOLFSSL_MSG("InitMd5 failed"); |
1208 | | } |
1209 | | else { |
1210 | | if ((ret = wc_Md5Update(md5, data, len)) != 0) { |
1211 | | WOLFSSL_MSG("Md5Update failed"); |
1212 | | } |
1213 | | else if ((ret = wc_Md5Final(md5, hash)) != 0) { |
1214 | | WOLFSSL_MSG("Md5Final failed"); |
1215 | | } |
1216 | | wc_Md5Free(md5); |
1217 | | } |
1218 | | |
1219 | | #ifdef WOLFSSL_SMALL_STACK |
1220 | | XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1221 | | #endif |
1222 | | |
1223 | | return ret; |
1224 | | } |
1225 | | #endif /* !NO_MD5 */ |
1226 | | |
1227 | | #if !defined(NO_SHA) |
1228 | | int wc_ShaHash(const byte* data, word32 len, byte* hash) |
1229 | | { |
1230 | | int ret = 0; |
1231 | | #ifdef WOLFSSL_SMALL_STACK |
1232 | | wc_Sha* sha; |
1233 | | #else |
1234 | | wc_Sha sha[1]; |
1235 | | #endif |
1236 | | int devId = INVALID_DEVID; |
1237 | | |
1238 | | #ifdef WOLFSSL_SMALL_STACK |
1239 | | sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1240 | | if (sha == NULL) |
1241 | | return MEMORY_E; |
1242 | | #endif |
1243 | | |
1244 | | #ifdef WOLF_CRYPTO_CB |
1245 | | /* only use devId if its not an empty hash */ |
1246 | | if (data != NULL && len > 0) |
1247 | | devId = wc_CryptoCb_GetDevIdAtIndex(0); |
1248 | | #endif |
1249 | | |
1250 | | if ((ret = wc_InitSha_ex(sha, NULL, devId)) != 0) { |
1251 | | WOLFSSL_MSG("InitSha failed"); |
1252 | | } |
1253 | | else { |
1254 | | if ((ret = wc_ShaUpdate(sha, data, len)) != 0) { |
1255 | | WOLFSSL_MSG("ShaUpdate failed"); |
1256 | | } |
1257 | | else if ((ret = wc_ShaFinal(sha, hash)) != 0) { |
1258 | | WOLFSSL_MSG("ShaFinal failed"); |
1259 | | } |
1260 | | wc_ShaFree(sha); |
1261 | | } |
1262 | | |
1263 | | #ifdef WOLFSSL_SMALL_STACK |
1264 | | XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1265 | | #endif |
1266 | | |
1267 | | return ret; |
1268 | | } |
1269 | | #endif /* !NO_SHA */ |
1270 | | |
1271 | | #if defined(WOLFSSL_SHA224) |
1272 | | int wc_Sha224Hash(const byte* data, word32 len, byte* hash) |
1273 | | { |
1274 | | int ret = 0; |
1275 | | #ifdef WOLFSSL_SMALL_STACK |
1276 | | wc_Sha224* sha224; |
1277 | | #else |
1278 | | wc_Sha224 sha224[1]; |
1279 | | #endif |
1280 | | |
1281 | | #ifdef WOLFSSL_SMALL_STACK |
1282 | | sha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL, |
1283 | | DYNAMIC_TYPE_TMP_BUFFER); |
1284 | | if (sha224 == NULL) |
1285 | | return MEMORY_E; |
1286 | | #endif |
1287 | | |
1288 | | if ((ret = wc_InitSha224(sha224)) != 0) { |
1289 | | WOLFSSL_MSG("InitSha224 failed"); |
1290 | | } |
1291 | | else { |
1292 | | if ((ret = wc_Sha224Update(sha224, data, len)) != 0) { |
1293 | | WOLFSSL_MSG("Sha224Update failed"); |
1294 | | } |
1295 | | else if ((ret = wc_Sha224Final(sha224, hash)) != 0) { |
1296 | | WOLFSSL_MSG("Sha224Final failed"); |
1297 | | } |
1298 | | wc_Sha224Free(sha224); |
1299 | | } |
1300 | | |
1301 | | #ifdef WOLFSSL_SMALL_STACK |
1302 | | XFREE(sha224, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1303 | | #endif |
1304 | | |
1305 | | return ret; |
1306 | | } |
1307 | | #endif /* WOLFSSL_SHA224 */ |
1308 | | |
1309 | | #if !defined(NO_SHA256) |
1310 | | int wc_Sha256Hash(const byte* data, word32 len, byte* hash) |
1311 | | { |
1312 | | int ret = 0; |
1313 | | #ifdef WOLFSSL_SMALL_STACK |
1314 | | wc_Sha256* sha256; |
1315 | | #else |
1316 | | wc_Sha256 sha256[1]; |
1317 | | #endif |
1318 | | int devId = INVALID_DEVID; |
1319 | | |
1320 | | #ifdef WOLFSSL_SMALL_STACK |
1321 | | sha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL, |
1322 | | DYNAMIC_TYPE_TMP_BUFFER); |
1323 | | if (sha256 == NULL) |
1324 | | return MEMORY_E; |
1325 | | #endif |
1326 | | |
1327 | | #ifdef WOLF_CRYPTO_CB |
1328 | | /* only use devId if its not an empty hash */ |
1329 | | if (data != NULL && len > 0) |
1330 | | devId = wc_CryptoCb_GetDevIdAtIndex(0); |
1331 | | #endif |
1332 | | |
1333 | | if ((ret = wc_InitSha256_ex(sha256, NULL, devId)) != 0) { |
1334 | | WOLFSSL_MSG("InitSha256 failed"); |
1335 | | } |
1336 | | else { |
1337 | | if ((ret = wc_Sha256Update(sha256, data, len)) != 0) { |
1338 | | WOLFSSL_MSG("Sha256Update failed"); |
1339 | | } |
1340 | | else if ((ret = wc_Sha256Final(sha256, hash)) != 0) { |
1341 | | WOLFSSL_MSG("Sha256Final failed"); |
1342 | | } |
1343 | | wc_Sha256Free(sha256); |
1344 | | } |
1345 | | |
1346 | | |
1347 | | #ifdef WOLFSSL_SMALL_STACK |
1348 | | XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1349 | | #endif |
1350 | | |
1351 | | return ret; |
1352 | | } |
1353 | | #endif /* !NO_SHA256 */ |
1354 | | |
1355 | | #endif /* !defined(WOLFSSL_TI_HASH) */ |
1356 | | |
1357 | | |
1358 | | #if defined(WOLFSSL_SHA512) |
1359 | | int wc_Sha512Hash(const byte* data, word32 len, byte* hash) |
1360 | | { |
1361 | | int ret = 0; |
1362 | | #ifdef WOLFSSL_SMALL_STACK |
1363 | | wc_Sha512* sha512; |
1364 | | #else |
1365 | | wc_Sha512 sha512[1]; |
1366 | | #endif |
1367 | | |
1368 | | #ifdef WOLFSSL_SMALL_STACK |
1369 | | sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL, |
1370 | | DYNAMIC_TYPE_TMP_BUFFER); |
1371 | | if (sha512 == NULL) |
1372 | | return MEMORY_E; |
1373 | | #endif |
1374 | | |
1375 | | if ((ret = wc_InitSha512(sha512)) != 0) { |
1376 | | WOLFSSL_MSG("InitSha512 failed"); |
1377 | | } |
1378 | | else { |
1379 | | if ((ret = wc_Sha512Update(sha512, data, len)) != 0) { |
1380 | | WOLFSSL_MSG("Sha512Update failed"); |
1381 | | } |
1382 | | else if ((ret = wc_Sha512Final(sha512, hash)) != 0) { |
1383 | | WOLFSSL_MSG("Sha512Final failed"); |
1384 | | } |
1385 | | wc_Sha512Free(sha512); |
1386 | | } |
1387 | | |
1388 | | #ifdef WOLFSSL_SMALL_STACK |
1389 | | XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1390 | | #endif |
1391 | | |
1392 | | return ret; |
1393 | | } |
1394 | | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
1395 | | #ifndef WOLFSSL_NOSHA512_224 |
1396 | | int wc_Sha512_224Hash(const byte* data, word32 len, byte* hash) |
1397 | | { |
1398 | | int ret = 0; |
1399 | | #ifdef WOLFSSL_SMALL_STACK |
1400 | | wc_Sha512* sha512; |
1401 | | #else |
1402 | | wc_Sha512 sha512[1]; |
1403 | | #endif |
1404 | | |
1405 | | #ifdef WOLFSSL_SMALL_STACK |
1406 | | sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL, |
1407 | | DYNAMIC_TYPE_TMP_BUFFER); |
1408 | | if (sha512 == NULL) |
1409 | | return MEMORY_E; |
1410 | | #endif |
1411 | | |
1412 | | if ((ret = wc_InitSha512_224(sha512)) != 0) { |
1413 | | WOLFSSL_MSG("wc_InitSha512_224 failed"); |
1414 | | } |
1415 | | else { |
1416 | | if ((ret = wc_Sha512_224Update(sha512, data, len)) != 0) { |
1417 | | WOLFSSL_MSG("wc_Sha512_224_Update failed"); |
1418 | | } |
1419 | | else if ((ret = wc_Sha512_224Final(sha512, hash)) != 0) { |
1420 | | WOLFSSL_MSG("wc_Sha512_224_Final failed"); |
1421 | | } |
1422 | | wc_Sha512_224Free(sha512); |
1423 | | } |
1424 | | |
1425 | | #ifdef WOLFSSL_SMALL_STACK |
1426 | | XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1427 | | #endif |
1428 | | |
1429 | | return ret; |
1430 | | } |
1431 | | #endif /* !WOLFSSL_NOSHA512_224 */ |
1432 | | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
1433 | | |
1434 | | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
1435 | | #ifndef WOLFSSL_NOSHA512_256 |
1436 | | int wc_Sha512_256Hash(const byte* data, word32 len, byte* hash) |
1437 | | { |
1438 | | int ret = 0; |
1439 | | #ifdef WOLFSSL_SMALL_STACK |
1440 | | wc_Sha512* sha512; |
1441 | | #else |
1442 | | wc_Sha512 sha512[1]; |
1443 | | #endif |
1444 | | |
1445 | | #ifdef WOLFSSL_SMALL_STACK |
1446 | | sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL, |
1447 | | DYNAMIC_TYPE_TMP_BUFFER); |
1448 | | if (sha512 == NULL) |
1449 | | return MEMORY_E; |
1450 | | #endif |
1451 | | |
1452 | | if ((ret = wc_InitSha512_256(sha512)) != 0) { |
1453 | | WOLFSSL_MSG("wc_InitSha512_256 failed"); |
1454 | | } |
1455 | | else { |
1456 | | if ((ret = wc_Sha512_256Update(sha512, data, len)) != 0) { |
1457 | | WOLFSSL_MSG("wc_Sha512_256_Update failed"); |
1458 | | } |
1459 | | else if ((ret = wc_Sha512_256Final(sha512, hash)) != 0) { |
1460 | | WOLFSSL_MSG("wc_Sha512_256_Final failed"); |
1461 | | } |
1462 | | wc_Sha512_256Free(sha512); |
1463 | | } |
1464 | | |
1465 | | #ifdef WOLFSSL_SMALL_STACK |
1466 | | XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1467 | | #endif |
1468 | | |
1469 | | return ret; |
1470 | | } |
1471 | | #endif /* !WOLFSSL_NOSHA512_256 */ |
1472 | | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
1473 | | |
1474 | | #endif /* WOLFSSL_SHA512 */ |
1475 | | |
1476 | | #if defined(WOLFSSL_SHA384) |
1477 | | int wc_Sha384Hash(const byte* data, word32 len, byte* hash) |
1478 | | { |
1479 | | int ret = 0; |
1480 | | #ifdef WOLFSSL_SMALL_STACK |
1481 | | wc_Sha384* sha384; |
1482 | | #else |
1483 | | wc_Sha384 sha384[1]; |
1484 | | #endif |
1485 | | |
1486 | | #ifdef WOLFSSL_SMALL_STACK |
1487 | | sha384 = (wc_Sha384*)XMALLOC(sizeof(wc_Sha384), NULL, |
1488 | | DYNAMIC_TYPE_TMP_BUFFER); |
1489 | | if (sha384 == NULL) |
1490 | | return MEMORY_E; |
1491 | | #endif |
1492 | | |
1493 | | if ((ret = wc_InitSha384(sha384)) != 0) { |
1494 | | WOLFSSL_MSG("InitSha384 failed"); |
1495 | | } |
1496 | | else { |
1497 | | if ((ret = wc_Sha384Update(sha384, data, len)) != 0) { |
1498 | | WOLFSSL_MSG("Sha384Update failed"); |
1499 | | } |
1500 | | else if ((ret = wc_Sha384Final(sha384, hash)) != 0) { |
1501 | | WOLFSSL_MSG("Sha384Final failed"); |
1502 | | } |
1503 | | wc_Sha384Free(sha384); |
1504 | | } |
1505 | | |
1506 | | #ifdef WOLFSSL_SMALL_STACK |
1507 | | XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1508 | | #endif |
1509 | | |
1510 | | return ret; |
1511 | | } |
1512 | | #endif /* WOLFSSL_SHA384 */ |
1513 | | |
1514 | | #if defined(WOLFSSL_SHA3) |
1515 | | #if !defined(WOLFSSL_NOSHA3_224) |
1516 | | int wc_Sha3_224Hash(const byte* data, word32 len, byte* hash) |
1517 | | { |
1518 | | int ret = 0; |
1519 | | #ifdef WOLFSSL_SMALL_STACK |
1520 | | wc_Sha3* sha3; |
1521 | | #else |
1522 | | wc_Sha3 sha3[1]; |
1523 | | #endif |
1524 | | |
1525 | | #ifdef WOLFSSL_SMALL_STACK |
1526 | | sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL, |
1527 | | DYNAMIC_TYPE_TMP_BUFFER); |
1528 | | if (sha3 == NULL) |
1529 | | return MEMORY_E; |
1530 | | #endif |
1531 | | |
1532 | | if ((ret = wc_InitSha3_224(sha3, NULL, INVALID_DEVID)) != 0) { |
1533 | | WOLFSSL_MSG("InitSha3_224 failed"); |
1534 | | } |
1535 | | else { |
1536 | | if ((ret = wc_Sha3_224_Update(sha3, data, len)) != 0) { |
1537 | | WOLFSSL_MSG("Sha3_224_Update failed"); |
1538 | | } |
1539 | | else if ((ret = wc_Sha3_224_Final(sha3, hash)) != 0) { |
1540 | | WOLFSSL_MSG("Sha3_224_Final failed"); |
1541 | | } |
1542 | | wc_Sha3_224_Free(sha3); |
1543 | | } |
1544 | | |
1545 | | #ifdef WOLFSSL_SMALL_STACK |
1546 | | XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1547 | | #endif |
1548 | | |
1549 | | return ret; |
1550 | | } |
1551 | | #endif /* !WOLFSSL_NOSHA3_224 */ |
1552 | | |
1553 | | #if !defined(WOLFSSL_NOSHA3_256) |
1554 | | int wc_Sha3_256Hash(const byte* data, word32 len, byte* hash) |
1555 | | { |
1556 | | int ret = 0; |
1557 | | #ifdef WOLFSSL_SMALL_STACK |
1558 | | wc_Sha3* sha3; |
1559 | | #else |
1560 | | wc_Sha3 sha3[1]; |
1561 | | #endif |
1562 | | |
1563 | | #ifdef WOLFSSL_SMALL_STACK |
1564 | | sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL, |
1565 | | DYNAMIC_TYPE_TMP_BUFFER); |
1566 | | if (sha3 == NULL) |
1567 | | return MEMORY_E; |
1568 | | #endif |
1569 | | |
1570 | | if ((ret = wc_InitSha3_256(sha3, NULL, INVALID_DEVID)) != 0) { |
1571 | | WOLFSSL_MSG("InitSha3_256 failed"); |
1572 | | } |
1573 | | else { |
1574 | | if ((ret = wc_Sha3_256_Update(sha3, data, len)) != 0) { |
1575 | | WOLFSSL_MSG("Sha3_256_Update failed"); |
1576 | | } |
1577 | | else if ((ret = wc_Sha3_256_Final(sha3, hash)) != 0) { |
1578 | | WOLFSSL_MSG("Sha3_256_Final failed"); |
1579 | | } |
1580 | | wc_Sha3_256_Free(sha3); |
1581 | | } |
1582 | | |
1583 | | #ifdef WOLFSSL_SMALL_STACK |
1584 | | XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1585 | | #endif |
1586 | | |
1587 | | return ret; |
1588 | | } |
1589 | | #endif /* !WOLFSSL_NOSHA3_256 */ |
1590 | | |
1591 | | #if !defined(WOLFSSL_NOSHA3_384) |
1592 | | int wc_Sha3_384Hash(const byte* data, word32 len, byte* hash) |
1593 | | { |
1594 | | int ret = 0; |
1595 | | #ifdef WOLFSSL_SMALL_STACK |
1596 | | wc_Sha3* sha3; |
1597 | | #else |
1598 | | wc_Sha3 sha3[1]; |
1599 | | #endif |
1600 | | |
1601 | | #ifdef WOLFSSL_SMALL_STACK |
1602 | | sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL, |
1603 | | DYNAMIC_TYPE_TMP_BUFFER); |
1604 | | if (sha3 == NULL) |
1605 | | return MEMORY_E; |
1606 | | #endif |
1607 | | |
1608 | | if ((ret = wc_InitSha3_384(sha3, NULL, INVALID_DEVID)) != 0) { |
1609 | | WOLFSSL_MSG("InitSha3_384 failed"); |
1610 | | } |
1611 | | else { |
1612 | | if ((ret = wc_Sha3_384_Update(sha3, data, len)) != 0) { |
1613 | | WOLFSSL_MSG("Sha3_384_Update failed"); |
1614 | | } |
1615 | | else if ((ret = wc_Sha3_384_Final(sha3, hash)) != 0) { |
1616 | | WOLFSSL_MSG("Sha3_384_Final failed"); |
1617 | | } |
1618 | | wc_Sha3_384_Free(sha3); |
1619 | | } |
1620 | | |
1621 | | #ifdef WOLFSSL_SMALL_STACK |
1622 | | XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1623 | | #endif |
1624 | | |
1625 | | return ret; |
1626 | | } |
1627 | | #endif /* !WOLFSSL_NOSHA3_384 */ |
1628 | | |
1629 | | #if !defined(WOLFSSL_NOSHA3_512) |
1630 | | int wc_Sha3_512Hash(const byte* data, word32 len, byte* hash) |
1631 | | { |
1632 | | int ret = 0; |
1633 | | #ifdef WOLFSSL_SMALL_STACK |
1634 | | wc_Sha3* sha3; |
1635 | | #else |
1636 | | wc_Sha3 sha3[1]; |
1637 | | #endif |
1638 | | |
1639 | | #ifdef WOLFSSL_SMALL_STACK |
1640 | | sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL, |
1641 | | DYNAMIC_TYPE_TMP_BUFFER); |
1642 | | if (sha3 == NULL) |
1643 | | return MEMORY_E; |
1644 | | #endif |
1645 | | |
1646 | | if ((ret = wc_InitSha3_512(sha3, NULL, INVALID_DEVID)) != 0) { |
1647 | | WOLFSSL_MSG("InitSha3_512 failed"); |
1648 | | } |
1649 | | else { |
1650 | | if ((ret = wc_Sha3_512_Update(sha3, data, len)) != 0) { |
1651 | | WOLFSSL_MSG("Sha3_512_Update failed"); |
1652 | | } |
1653 | | else if ((ret = wc_Sha3_512_Final(sha3, hash)) != 0) { |
1654 | | WOLFSSL_MSG("Sha3_512_Final failed"); |
1655 | | } |
1656 | | wc_Sha3_512_Free(sha3); |
1657 | | } |
1658 | | |
1659 | | #ifdef WOLFSSL_SMALL_STACK |
1660 | | XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1661 | | #endif |
1662 | | |
1663 | | return ret; |
1664 | | } |
1665 | | #endif /* !WOLFSSL_NOSHA3_512 */ |
1666 | | |
1667 | | #ifdef WOLFSSL_SHAKE256 |
1668 | | int wc_Shake256Hash(const byte* data, word32 len, byte* hash, |
1669 | | word32 hashLen) |
1670 | 0 | { |
1671 | 0 | int ret = 0; |
1672 | 0 | #ifdef WOLFSSL_SMALL_STACK |
1673 | 0 | wc_Shake* shake; |
1674 | | #else |
1675 | | wc_Shake shake[1]; |
1676 | | #endif |
1677 | |
|
1678 | 0 | #ifdef WOLFSSL_SMALL_STACK |
1679 | 0 | shake = (wc_Shake*)XMALLOC(sizeof(wc_Shake), NULL, |
1680 | 0 | DYNAMIC_TYPE_TMP_BUFFER); |
1681 | 0 | if (shake == NULL) |
1682 | 0 | return MEMORY_E; |
1683 | 0 | #endif |
1684 | | |
1685 | 0 | if ((ret = wc_InitShake256(shake, NULL, INVALID_DEVID)) != 0) { |
1686 | 0 | WOLFSSL_MSG("InitShake256 failed"); |
1687 | 0 | } |
1688 | 0 | else { |
1689 | 0 | if ((ret = wc_Shake256_Update(shake, data, len)) != 0) { |
1690 | 0 | WOLFSSL_MSG("Shake256_Update failed"); |
1691 | 0 | } |
1692 | 0 | else if ((ret = wc_Shake256_Final(shake, hash, hashLen)) != 0) { |
1693 | 0 | WOLFSSL_MSG("Shake256_Final failed"); |
1694 | 0 | } |
1695 | 0 | wc_Shake256_Free(shake); |
1696 | 0 | } |
1697 | |
|
1698 | 0 | #ifdef WOLFSSL_SMALL_STACK |
1699 | 0 | XFREE(shake, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1700 | 0 | #endif |
1701 | |
|
1702 | 0 | return ret; |
1703 | 0 | } |
1704 | | #endif /* WOLFSSL_SHAKE_256 */ |
1705 | | #endif /* WOLFSSL_SHA3 */ |
1706 | | |
1707 | | #endif /* !NO_HASH_WRAPPER */ |
1708 | | |
1709 | | #ifdef WOLFSSL_HASH_KEEP |
1710 | | int _wc_Hash_Grow(byte** msg, word32* used, word32* len, const byte* in, |
1711 | | int inSz, void* heap) |
1712 | | { |
1713 | | if (*len < *used + inSz) { |
1714 | | if (*msg == NULL) { |
1715 | | *msg = (byte*)XMALLOC(*used + inSz, heap, DYNAMIC_TYPE_TMP_BUFFER); |
1716 | | } |
1717 | | else { |
1718 | | byte* pt = (byte*)XREALLOC(*msg, *used + inSz, heap, |
1719 | | DYNAMIC_TYPE_TMP_BUFFER); |
1720 | | if (pt == NULL) { |
1721 | | return MEMORY_E; |
1722 | | } |
1723 | | *msg = pt; |
1724 | | } |
1725 | | if (*msg == NULL) { |
1726 | | return MEMORY_E; |
1727 | | } |
1728 | | *len = *used + inSz; |
1729 | | } |
1730 | | XMEMCPY(*msg + *used, in, inSz); |
1731 | | *used += inSz; |
1732 | | return 0; |
1733 | | } |
1734 | | #endif /* WOLFSSL_HASH_KEEP */ |
1735 | | |