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