/src/wolfssl/wolfcrypt/src/hash.c
Line | Count | Source (jump to first uncovered line) |
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 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 | | #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 | 0 | { |
44 | | /* Default to hash type none as error */ |
45 | 0 | 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 | 0 | if (hashType > 0 && hashType <= WC_HASH_TYPE_MAX) { |
110 | 0 | eHashType = (enum wc_HashType)hashType; |
111 | 0 | } |
112 | 0 | #endif |
113 | 0 | return eHashType; |
114 | 0 | } |
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 | 0 | { |
121 | 0 | int oid = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */ |
122 | 0 | switch(hash_type) |
123 | 0 | { |
124 | 0 | case WC_HASH_TYPE_MD2: |
125 | | #ifdef WOLFSSL_MD2 |
126 | | oid = MD2h; |
127 | | #endif |
128 | 0 | break; |
129 | 0 | case WC_HASH_TYPE_MD5_SHA: |
130 | 0 | case WC_HASH_TYPE_MD5: |
131 | 0 | #ifndef NO_MD5 |
132 | 0 | oid = MD5h; |
133 | 0 | #endif |
134 | 0 | break; |
135 | 0 | case WC_HASH_TYPE_SHA: |
136 | 0 | #ifndef NO_SHA |
137 | 0 | oid = SHAh; |
138 | 0 | #endif |
139 | 0 | break; |
140 | 0 | case WC_HASH_TYPE_SHA224: |
141 | 0 | #ifdef WOLFSSL_SHA224 |
142 | 0 | oid = SHA224h; |
143 | 0 | #endif |
144 | 0 | break; |
145 | 0 | case WC_HASH_TYPE_SHA256: |
146 | 0 | #ifndef NO_SHA256 |
147 | 0 | oid = SHA256h; |
148 | 0 | #endif |
149 | 0 | break; |
150 | 0 | case WC_HASH_TYPE_SHA384: |
151 | 0 | #ifdef WOLFSSL_SHA384 |
152 | 0 | oid = SHA384h; |
153 | 0 | #endif |
154 | 0 | break; |
155 | 0 | case WC_HASH_TYPE_SHA512: |
156 | 0 | #ifdef WOLFSSL_SHA512 |
157 | 0 | oid = SHA512h; |
158 | 0 | #endif |
159 | 0 | break; |
160 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
161 | 0 | case WC_HASH_TYPE_SHA512_224: |
162 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
163 | 0 | oid = SHA512_224h; |
164 | 0 | #endif |
165 | 0 | break; |
166 | 0 | #endif |
167 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
168 | 0 | case WC_HASH_TYPE_SHA512_256: |
169 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
170 | 0 | oid = SHA512_256h; |
171 | 0 | #endif |
172 | 0 | break; |
173 | 0 | #endif |
174 | 0 | case WC_HASH_TYPE_SHA3_224: |
175 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
176 | 0 | oid = SHA3_224h; |
177 | 0 | #endif |
178 | 0 | break; |
179 | 0 | case WC_HASH_TYPE_SHA3_256: |
180 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
181 | 0 | oid = SHA3_256h; |
182 | 0 | #endif |
183 | 0 | break; |
184 | 0 | case WC_HASH_TYPE_SHA3_384: |
185 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
186 | 0 | oid = SHA3_384h; |
187 | 0 | #endif |
188 | 0 | break; |
189 | 0 | case WC_HASH_TYPE_SHA3_512: |
190 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
191 | 0 | oid = SHA3_512h; |
192 | 0 | #endif |
193 | 0 | 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 | 0 | case WC_HASH_TYPE_MD4: |
212 | 0 | case WC_HASH_TYPE_BLAKE2B: |
213 | 0 | case WC_HASH_TYPE_BLAKE2S: |
214 | 0 | case WC_HASH_TYPE_NONE: |
215 | 0 | default: |
216 | 0 | oid = BAD_FUNC_ARG; |
217 | 0 | break; |
218 | 0 | } |
219 | 0 | return oid; |
220 | 0 | } |
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 | | #ifdef WOLFSSL_MD2 |
228 | | case MD2h: |
229 | | hash_type = WC_HASH_TYPE_MD2; |
230 | | break; |
231 | | #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 | | #ifdef WOLFSSL_SM3 |
277 | | case SM3h: |
278 | | hash_type = WC_HASH_TYPE_SM3; |
279 | | break; |
280 | | #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 | 0 | { |
293 | 0 | int dig_size = WC_NO_ERR_TRACE(HASH_TYPE_E); |
294 | 0 | switch(hash_type) |
295 | 0 | { |
296 | 0 | case WC_HASH_TYPE_MD2: |
297 | | #ifdef WOLFSSL_MD2 |
298 | | dig_size = WC_MD2_DIGEST_SIZE; |
299 | | #endif |
300 | 0 | break; |
301 | 0 | case WC_HASH_TYPE_MD4: |
302 | | #ifndef NO_MD4 |
303 | | dig_size = WC_MD4_DIGEST_SIZE; |
304 | | #endif |
305 | 0 | break; |
306 | 0 | case WC_HASH_TYPE_MD5: |
307 | 0 | #ifndef NO_MD5 |
308 | 0 | dig_size = WC_MD5_DIGEST_SIZE; |
309 | 0 | #endif |
310 | 0 | break; |
311 | 0 | case WC_HASH_TYPE_SHA: |
312 | 0 | #ifndef NO_SHA |
313 | 0 | dig_size = WC_SHA_DIGEST_SIZE; |
314 | 0 | #endif |
315 | 0 | break; |
316 | 0 | case WC_HASH_TYPE_SHA224: |
317 | 0 | #ifdef WOLFSSL_SHA224 |
318 | 0 | dig_size = WC_SHA224_DIGEST_SIZE; |
319 | 0 | #endif |
320 | 0 | break; |
321 | 0 | case WC_HASH_TYPE_SHA256: |
322 | 0 | #ifndef NO_SHA256 |
323 | 0 | dig_size = WC_SHA256_DIGEST_SIZE; |
324 | 0 | #endif |
325 | 0 | break; |
326 | 0 | case WC_HASH_TYPE_SHA384: |
327 | 0 | #ifdef WOLFSSL_SHA384 |
328 | 0 | dig_size = WC_SHA384_DIGEST_SIZE; |
329 | 0 | #endif |
330 | 0 | break; |
331 | 0 | case WC_HASH_TYPE_SHA512: |
332 | 0 | #ifdef WOLFSSL_SHA512 |
333 | 0 | dig_size = WC_SHA512_DIGEST_SIZE; |
334 | 0 | #endif |
335 | 0 | 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 | | #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S) |
383 | | dig_size = BLAKE2S_OUTBYTES; |
384 | | #endif |
385 | 0 | break; |
386 | | |
387 | | #ifdef WOLFSSL_SM3 |
388 | | case WC_HASH_TYPE_SM3: |
389 | | dig_size = WC_SM3_DIGEST_SIZE; |
390 | | break; |
391 | | #endif |
392 | | |
393 | | /* Not Supported */ |
394 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128) |
395 | | case WC_HASH_TYPE_SHAKE128: |
396 | | #endif |
397 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256) |
398 | | case WC_HASH_TYPE_SHAKE256: |
399 | | #endif |
400 | 0 | case WC_HASH_TYPE_NONE: |
401 | 0 | default: |
402 | 0 | dig_size = BAD_FUNC_ARG; |
403 | 0 | break; |
404 | 0 | } |
405 | 0 | return dig_size; |
406 | 0 | } |
407 | | |
408 | | |
409 | | /* Get Hash block size */ |
410 | | int wc_HashGetBlockSize(enum wc_HashType hash_type) |
411 | 0 | { |
412 | 0 | int block_size = WC_NO_ERR_TRACE(HASH_TYPE_E); |
413 | 0 | switch (hash_type) |
414 | 0 | { |
415 | 0 | case WC_HASH_TYPE_MD2: |
416 | | #ifdef WOLFSSL_MD2 |
417 | | block_size = WC_MD2_BLOCK_SIZE; |
418 | | #endif |
419 | 0 | break; |
420 | 0 | case WC_HASH_TYPE_MD4: |
421 | | #ifndef NO_MD4 |
422 | | block_size = WC_MD4_BLOCK_SIZE; |
423 | | #endif |
424 | 0 | break; |
425 | 0 | case WC_HASH_TYPE_MD5: |
426 | 0 | #ifndef NO_MD5 |
427 | 0 | block_size = WC_MD5_BLOCK_SIZE; |
428 | 0 | #endif |
429 | 0 | break; |
430 | 0 | case WC_HASH_TYPE_SHA: |
431 | 0 | #ifndef NO_SHA |
432 | 0 | block_size = WC_SHA_BLOCK_SIZE; |
433 | 0 | #endif |
434 | 0 | break; |
435 | 0 | case WC_HASH_TYPE_SHA224: |
436 | 0 | #ifdef WOLFSSL_SHA224 |
437 | 0 | block_size = WC_SHA224_BLOCK_SIZE; |
438 | 0 | #endif |
439 | 0 | break; |
440 | 0 | case WC_HASH_TYPE_SHA256: |
441 | 0 | #ifndef NO_SHA256 |
442 | 0 | block_size = WC_SHA256_BLOCK_SIZE; |
443 | 0 | #endif |
444 | 0 | break; |
445 | 0 | case WC_HASH_TYPE_SHA384: |
446 | 0 | #ifdef WOLFSSL_SHA384 |
447 | 0 | block_size = WC_SHA384_BLOCK_SIZE; |
448 | 0 | #endif |
449 | 0 | break; |
450 | 0 | case WC_HASH_TYPE_SHA512: |
451 | 0 | #ifdef WOLFSSL_SHA512 |
452 | 0 | block_size = WC_SHA512_BLOCK_SIZE; |
453 | 0 | #endif |
454 | 0 | break; |
455 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
456 | 0 | case WC_HASH_TYPE_SHA512_224: |
457 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
458 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
459 | 0 | block_size = WC_SHA512_224_BLOCK_SIZE; |
460 | 0 | #endif |
461 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
462 | 0 | break; |
463 | 0 | #endif |
464 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
465 | 0 | case WC_HASH_TYPE_SHA512_256: |
466 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
467 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
468 | 0 | block_size = WC_SHA512_256_BLOCK_SIZE; |
469 | 0 | #endif |
470 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
471 | 0 | break; |
472 | 0 | #endif |
473 | 0 | case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */ |
474 | 0 | #if !defined(NO_MD5) && !defined(NO_SHA) |
475 | 0 | block_size = (int)WC_MD5_BLOCK_SIZE + (int)WC_SHA_BLOCK_SIZE; |
476 | 0 | #endif |
477 | 0 | break; |
478 | | |
479 | 0 | case WC_HASH_TYPE_SHA3_224: |
480 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
481 | 0 | block_size = WC_SHA3_224_BLOCK_SIZE; |
482 | 0 | #endif |
483 | 0 | break; |
484 | 0 | case WC_HASH_TYPE_SHA3_256: |
485 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
486 | 0 | block_size = WC_SHA3_256_BLOCK_SIZE; |
487 | 0 | #endif |
488 | 0 | break; |
489 | 0 | case WC_HASH_TYPE_SHA3_384: |
490 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
491 | 0 | block_size = WC_SHA3_384_BLOCK_SIZE; |
492 | 0 | #endif |
493 | 0 | break; |
494 | 0 | case WC_HASH_TYPE_SHA3_512: |
495 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
496 | 0 | block_size = WC_SHA3_512_BLOCK_SIZE; |
497 | 0 | #endif |
498 | 0 | break; |
499 | 0 | case WC_HASH_TYPE_BLAKE2B: |
500 | 0 | case WC_HASH_TYPE_BLAKE2S: |
501 | | #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S) |
502 | | block_size = BLAKE2S_BLOCKBYTES; |
503 | | #endif |
504 | 0 | 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 | 0 | case WC_HASH_TYPE_NONE: |
520 | 0 | default: |
521 | 0 | block_size = BAD_FUNC_ARG; |
522 | 0 | break; |
523 | 0 | } |
524 | 0 | return block_size; |
525 | 0 | } |
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 | | #ifdef WOLFSSL_SM3 |
632 | | case WC_HASH_TYPE_SM3: |
633 | | ret = wc_Sm3Hash_ex(data, data_len, hash, heap, devId); |
634 | | break; |
635 | | #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 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128) |
643 | | case WC_HASH_TYPE_SHAKE128: |
644 | | #endif |
645 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256) |
646 | | case WC_HASH_TYPE_SHAKE256: |
647 | | #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 | 0 | { |
658 | 0 | return wc_Hash_ex(hash_type, data, data_len, hash, hash_len, |
659 | 0 | NULL, INVALID_DEVID); |
660 | 0 | } |
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 | 0 | { |
703 | 0 | int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */ |
704 | |
|
705 | 0 | if (hash == NULL) |
706 | 0 | return BAD_FUNC_ARG; |
707 | | |
708 | 0 | hash->type = type; |
709 | |
|
710 | | #ifdef WC_NO_CONSTRUCTORS |
711 | | (void)heap; |
712 | | #else |
713 | 0 | hash->heap = heap; |
714 | 0 | #endif |
715 | |
|
716 | 0 | switch (type) { |
717 | 0 | case WC_HASH_TYPE_MD5: |
718 | 0 | #ifndef NO_MD5 |
719 | 0 | ret = wc_InitMd5_ex(&hash->alg.md5, heap, devId); |
720 | 0 | #endif |
721 | 0 | break; |
722 | 0 | case WC_HASH_TYPE_SHA: |
723 | 0 | #ifndef NO_SHA |
724 | 0 | ret = wc_InitSha_ex(&hash->alg.sha, heap, devId); |
725 | 0 | #endif |
726 | 0 | break; |
727 | 0 | case WC_HASH_TYPE_SHA224: |
728 | 0 | #ifdef WOLFSSL_SHA224 |
729 | 0 | ret = wc_InitSha224_ex(&hash->alg.sha224, heap, devId); |
730 | 0 | #endif |
731 | 0 | break; |
732 | 0 | case WC_HASH_TYPE_SHA256: |
733 | 0 | #ifndef NO_SHA256 |
734 | 0 | ret = wc_InitSha256_ex(&hash->alg.sha256, heap, devId); |
735 | 0 | #endif |
736 | 0 | break; |
737 | 0 | case WC_HASH_TYPE_SHA384: |
738 | 0 | #ifdef WOLFSSL_SHA384 |
739 | 0 | ret = wc_InitSha384_ex(&hash->alg.sha384, heap, devId); |
740 | 0 | #endif |
741 | 0 | break; |
742 | 0 | case WC_HASH_TYPE_SHA512: |
743 | 0 | #ifdef WOLFSSL_SHA512 |
744 | 0 | ret = wc_InitSha512_ex(&hash->alg.sha512, heap, devId); |
745 | 0 | #endif |
746 | 0 | break; |
747 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
748 | 0 | case WC_HASH_TYPE_SHA512_224: |
749 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
750 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
751 | 0 | ret = wc_InitSha512_224_ex(&hash->alg.sha512, heap, devId); |
752 | 0 | #endif |
753 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
754 | 0 | break; |
755 | 0 | #endif |
756 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
757 | 0 | case WC_HASH_TYPE_SHA512_256: |
758 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
759 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
760 | 0 | ret = wc_InitSha512_256_ex(&hash->alg.sha512, heap, devId); |
761 | 0 | #endif |
762 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
763 | 0 | break; |
764 | 0 | #endif |
765 | 0 | case WC_HASH_TYPE_SHA3_224: |
766 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
767 | 0 | ret = wc_InitSha3_224(&hash->alg.sha3, heap, devId); |
768 | 0 | #endif |
769 | 0 | break; |
770 | 0 | case WC_HASH_TYPE_SHA3_256: |
771 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
772 | 0 | ret = wc_InitSha3_256(&hash->alg.sha3, heap, devId); |
773 | 0 | #endif |
774 | 0 | break; |
775 | 0 | case WC_HASH_TYPE_SHA3_384: |
776 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
777 | 0 | ret = wc_InitSha3_384(&hash->alg.sha3, heap, devId); |
778 | 0 | #endif |
779 | 0 | break; |
780 | 0 | case WC_HASH_TYPE_SHA3_512: |
781 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
782 | 0 | ret = wc_InitSha3_512(&hash->alg.sha3, heap, devId); |
783 | 0 | #endif |
784 | 0 | 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 | 0 | case WC_HASH_TYPE_MD5_SHA: |
794 | 0 | case WC_HASH_TYPE_MD2: |
795 | 0 | case WC_HASH_TYPE_MD4: |
796 | 0 | case WC_HASH_TYPE_BLAKE2B: |
797 | 0 | 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 | 0 | case WC_HASH_TYPE_NONE: |
805 | 0 | default: |
806 | 0 | ret = BAD_FUNC_ARG; |
807 | 0 | }; |
808 | |
|
809 | 0 | (void)devId; |
810 | |
|
811 | 0 | return ret; |
812 | 0 | } |
813 | | |
814 | | int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type) |
815 | 0 | { |
816 | 0 | return wc_HashInit_ex(hash, type, NULL, INVALID_DEVID); |
817 | 0 | } |
818 | | |
819 | | int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data, |
820 | | word32 dataSz) |
821 | 0 | { |
822 | 0 | int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */ |
823 | |
|
824 | 0 | if (hash == NULL || (data == NULL && dataSz > 0)) |
825 | 0 | 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 | 0 | switch (type) { |
835 | 0 | case WC_HASH_TYPE_MD5: |
836 | 0 | #ifndef NO_MD5 |
837 | 0 | ret = wc_Md5Update(&hash->alg.md5, data, dataSz); |
838 | 0 | #endif |
839 | 0 | break; |
840 | 0 | case WC_HASH_TYPE_SHA: |
841 | 0 | #ifndef NO_SHA |
842 | 0 | ret = wc_ShaUpdate(&hash->alg.sha, data, dataSz); |
843 | 0 | #endif |
844 | 0 | break; |
845 | 0 | case WC_HASH_TYPE_SHA224: |
846 | 0 | #ifdef WOLFSSL_SHA224 |
847 | 0 | ret = wc_Sha224Update(&hash->alg.sha224, data, dataSz); |
848 | 0 | #endif |
849 | 0 | break; |
850 | 0 | case WC_HASH_TYPE_SHA256: |
851 | 0 | #ifndef NO_SHA256 |
852 | 0 | ret = wc_Sha256Update(&hash->alg.sha256, data, dataSz); |
853 | 0 | #endif |
854 | 0 | break; |
855 | 0 | case WC_HASH_TYPE_SHA384: |
856 | 0 | #ifdef WOLFSSL_SHA384 |
857 | 0 | ret = wc_Sha384Update(&hash->alg.sha384, data, dataSz); |
858 | 0 | #endif |
859 | 0 | break; |
860 | 0 | case WC_HASH_TYPE_SHA512: |
861 | 0 | #ifdef WOLFSSL_SHA512 |
862 | 0 | ret = wc_Sha512Update(&hash->alg.sha512, data, dataSz); |
863 | 0 | #endif |
864 | 0 | break; |
865 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
866 | 0 | case WC_HASH_TYPE_SHA512_224: |
867 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
868 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
869 | 0 | ret = wc_Sha512_224Update(&hash->alg.sha512, data, dataSz); |
870 | 0 | #endif |
871 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
872 | 0 | break; |
873 | 0 | #endif |
874 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
875 | 0 | case WC_HASH_TYPE_SHA512_256: |
876 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
877 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
878 | 0 | ret = wc_Sha512_256Update(&hash->alg.sha512, data, dataSz); |
879 | 0 | #endif |
880 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
881 | 0 | break; |
882 | 0 | #endif |
883 | 0 | case WC_HASH_TYPE_SHA3_224: |
884 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
885 | 0 | ret = wc_Sha3_224_Update(&hash->alg.sha3, data, dataSz); |
886 | 0 | #endif |
887 | 0 | break; |
888 | 0 | case WC_HASH_TYPE_SHA3_256: |
889 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
890 | 0 | ret = wc_Sha3_256_Update(&hash->alg.sha3, data, dataSz); |
891 | 0 | #endif |
892 | 0 | break; |
893 | 0 | case WC_HASH_TYPE_SHA3_384: |
894 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
895 | 0 | ret = wc_Sha3_384_Update(&hash->alg.sha3, data, dataSz); |
896 | 0 | #endif |
897 | 0 | break; |
898 | 0 | case WC_HASH_TYPE_SHA3_512: |
899 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
900 | 0 | ret = wc_Sha3_512_Update(&hash->alg.sha3, data, dataSz); |
901 | 0 | #endif |
902 | 0 | 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 | 0 | case WC_HASH_TYPE_MD5_SHA: |
912 | 0 | case WC_HASH_TYPE_MD2: |
913 | 0 | case WC_HASH_TYPE_MD4: |
914 | 0 | case WC_HASH_TYPE_BLAKE2B: |
915 | 0 | 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 | 0 | case WC_HASH_TYPE_NONE: |
923 | 0 | default: |
924 | 0 | ret = BAD_FUNC_ARG; |
925 | 0 | }; |
926 | |
|
927 | 0 | return ret; |
928 | 0 | } |
929 | | |
930 | | int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out) |
931 | 0 | { |
932 | 0 | int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */ |
933 | |
|
934 | 0 | if (hash == NULL || out == NULL) |
935 | 0 | 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 | 0 | switch (type) { |
945 | 0 | case WC_HASH_TYPE_MD5: |
946 | 0 | #ifndef NO_MD5 |
947 | 0 | ret = wc_Md5Final(&hash->alg.md5, out); |
948 | 0 | #endif |
949 | 0 | break; |
950 | 0 | case WC_HASH_TYPE_SHA: |
951 | 0 | #ifndef NO_SHA |
952 | 0 | ret = wc_ShaFinal(&hash->alg.sha, out); |
953 | 0 | #endif |
954 | 0 | break; |
955 | 0 | case WC_HASH_TYPE_SHA224: |
956 | 0 | #ifdef WOLFSSL_SHA224 |
957 | 0 | ret = wc_Sha224Final(&hash->alg.sha224, out); |
958 | 0 | #endif |
959 | 0 | break; |
960 | 0 | case WC_HASH_TYPE_SHA256: |
961 | 0 | #ifndef NO_SHA256 |
962 | 0 | ret = wc_Sha256Final(&hash->alg.sha256, out); |
963 | 0 | #endif |
964 | 0 | break; |
965 | 0 | case WC_HASH_TYPE_SHA384: |
966 | 0 | #ifdef WOLFSSL_SHA384 |
967 | 0 | ret = wc_Sha384Final(&hash->alg.sha384, out); |
968 | 0 | #endif |
969 | 0 | break; |
970 | 0 | case WC_HASH_TYPE_SHA512: |
971 | 0 | #ifdef WOLFSSL_SHA512 |
972 | 0 | ret = wc_Sha512Final(&hash->alg.sha512, out); |
973 | 0 | #endif |
974 | 0 | break; |
975 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
976 | 0 | case WC_HASH_TYPE_SHA512_224: |
977 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
978 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
979 | 0 | ret = wc_Sha512_224Final(&hash->alg.sha512, out); |
980 | 0 | #endif |
981 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
982 | 0 | break; |
983 | 0 | #endif |
984 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
985 | 0 | case WC_HASH_TYPE_SHA512_256: |
986 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
987 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
988 | 0 | ret = wc_Sha512_256Final(&hash->alg.sha512, out); |
989 | 0 | #endif |
990 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
991 | 0 | break; |
992 | 0 | #endif |
993 | 0 | case WC_HASH_TYPE_SHA3_224: |
994 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
995 | 0 | ret = wc_Sha3_224_Final(&hash->alg.sha3, out); |
996 | 0 | #endif |
997 | 0 | break; |
998 | 0 | case WC_HASH_TYPE_SHA3_256: |
999 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
1000 | 0 | ret = wc_Sha3_256_Final(&hash->alg.sha3, out); |
1001 | 0 | #endif |
1002 | 0 | break; |
1003 | 0 | case WC_HASH_TYPE_SHA3_384: |
1004 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
1005 | 0 | ret = wc_Sha3_384_Final(&hash->alg.sha3, out); |
1006 | 0 | #endif |
1007 | 0 | break; |
1008 | 0 | case WC_HASH_TYPE_SHA3_512: |
1009 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
1010 | 0 | ret = wc_Sha3_512_Final(&hash->alg.sha3, out); |
1011 | 0 | #endif |
1012 | 0 | 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 | 0 | case WC_HASH_TYPE_MD5_SHA: |
1022 | 0 | case WC_HASH_TYPE_MD2: |
1023 | 0 | case WC_HASH_TYPE_MD4: |
1024 | 0 | case WC_HASH_TYPE_BLAKE2B: |
1025 | 0 | 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 | 0 | case WC_HASH_TYPE_NONE: |
1033 | 0 | default: |
1034 | 0 | ret = BAD_FUNC_ARG; |
1035 | 0 | }; |
1036 | |
|
1037 | 0 | return ret; |
1038 | 0 | } |
1039 | | |
1040 | | int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type) |
1041 | 0 | { |
1042 | 0 | int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */ |
1043 | |
|
1044 | 0 | if (hash == NULL) |
1045 | 0 | 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 | 0 | switch (type) { |
1055 | 0 | case WC_HASH_TYPE_MD5: |
1056 | 0 | #ifndef NO_MD5 |
1057 | 0 | wc_Md5Free(&hash->alg.md5); |
1058 | 0 | ret = 0; |
1059 | 0 | #endif |
1060 | 0 | break; |
1061 | 0 | case WC_HASH_TYPE_SHA: |
1062 | 0 | #ifndef NO_SHA |
1063 | 0 | wc_ShaFree(&hash->alg.sha); |
1064 | 0 | ret = 0; |
1065 | 0 | #endif |
1066 | 0 | break; |
1067 | 0 | case WC_HASH_TYPE_SHA224: |
1068 | 0 | #ifdef WOLFSSL_SHA224 |
1069 | 0 | wc_Sha224Free(&hash->alg.sha224); |
1070 | 0 | ret = 0; |
1071 | 0 | #endif |
1072 | 0 | break; |
1073 | 0 | case WC_HASH_TYPE_SHA256: |
1074 | 0 | #ifndef NO_SHA256 |
1075 | 0 | wc_Sha256Free(&hash->alg.sha256); |
1076 | 0 | ret = 0; |
1077 | 0 | #endif |
1078 | 0 | break; |
1079 | 0 | case WC_HASH_TYPE_SHA384: |
1080 | 0 | #ifdef WOLFSSL_SHA384 |
1081 | 0 | wc_Sha384Free(&hash->alg.sha384); |
1082 | 0 | ret = 0; |
1083 | 0 | #endif |
1084 | 0 | break; |
1085 | 0 | case WC_HASH_TYPE_SHA512: |
1086 | 0 | #ifdef WOLFSSL_SHA512 |
1087 | 0 | wc_Sha512Free(&hash->alg.sha512); |
1088 | 0 | ret = 0; |
1089 | 0 | #endif |
1090 | 0 | break; |
1091 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
1092 | 0 | case WC_HASH_TYPE_SHA512_224: |
1093 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
1094 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) |
1095 | 0 | wc_Sha512_224Free(&hash->alg.sha512); |
1096 | 0 | ret = 0; |
1097 | 0 | #endif |
1098 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
1099 | 0 | break; |
1100 | 0 | #endif |
1101 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
1102 | 0 | case WC_HASH_TYPE_SHA512_256: |
1103 | 0 | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
1104 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) |
1105 | 0 | wc_Sha512_256Free(&hash->alg.sha512); |
1106 | 0 | ret = 0; |
1107 | 0 | #endif |
1108 | 0 | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
1109 | 0 | break; |
1110 | 0 | #endif |
1111 | 0 | case WC_HASH_TYPE_SHA3_224: |
1112 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224) |
1113 | 0 | wc_Sha3_224_Free(&hash->alg.sha3); |
1114 | 0 | ret = 0; |
1115 | 0 | #endif |
1116 | 0 | break; |
1117 | 0 | case WC_HASH_TYPE_SHA3_256: |
1118 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256) |
1119 | 0 | wc_Sha3_256_Free(&hash->alg.sha3); |
1120 | 0 | ret = 0; |
1121 | 0 | #endif |
1122 | 0 | break; |
1123 | 0 | case WC_HASH_TYPE_SHA3_384: |
1124 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384) |
1125 | 0 | wc_Sha3_384_Free(&hash->alg.sha3); |
1126 | 0 | ret = 0; |
1127 | 0 | #endif |
1128 | 0 | break; |
1129 | 0 | case WC_HASH_TYPE_SHA3_512: |
1130 | 0 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) |
1131 | 0 | wc_Sha3_512_Free(&hash->alg.sha3); |
1132 | 0 | ret = 0; |
1133 | 0 | #endif |
1134 | 0 | 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 | 0 | case WC_HASH_TYPE_MD5_SHA: |
1145 | 0 | case WC_HASH_TYPE_MD2: |
1146 | 0 | case WC_HASH_TYPE_MD4: |
1147 | 0 | case WC_HASH_TYPE_BLAKE2B: |
1148 | 0 | 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 | 0 | case WC_HASH_TYPE_NONE: |
1156 | 0 | default: |
1157 | 0 | ret = BAD_FUNC_ARG; |
1158 | 0 | }; |
1159 | |
|
1160 | 0 | return ret; |
1161 | 0 | } |
1162 | | |
1163 | | #ifdef WOLFSSL_HASH_FLAGS |
1164 | | int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type, word32 flags) |
1165 | | { |
1166 | | int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */ |
1167 | | |
1168 | | if (hash == NULL) |
1169 | | return BAD_FUNC_ARG; |
1170 | | |
1171 | | switch (type) { |
1172 | | case WC_HASH_TYPE_MD5: |
1173 | | #ifndef NO_MD5 |
1174 | | ret = wc_Md5SetFlags(&hash->alg.md5, flags); |
1175 | | #endif |
1176 | | break; |
1177 | | case WC_HASH_TYPE_SHA: |
1178 | | #ifndef NO_SHA |
1179 | | ret = wc_ShaSetFlags(&hash->alg.sha, flags); |
1180 | | #endif |
1181 | | break; |
1182 | | case WC_HASH_TYPE_SHA224: |
1183 | | #ifdef WOLFSSL_SHA224 |
1184 | | ret = wc_Sha224SetFlags(&hash->alg.sha224, flags); |
1185 | | #endif |
1186 | | break; |
1187 | | case WC_HASH_TYPE_SHA256: |
1188 | | #ifndef NO_SHA256 |
1189 | | ret = wc_Sha256SetFlags(&hash->alg.sha256, flags); |
1190 | | #endif |
1191 | | break; |
1192 | | case WC_HASH_TYPE_SHA384: |
1193 | | #ifdef WOLFSSL_SHA384 |
1194 | | ret = wc_Sha384SetFlags(&hash->alg.sha384, flags); |
1195 | | #endif |
1196 | | break; |
1197 | | case WC_HASH_TYPE_SHA512: |
1198 | | #ifndef WOLFSSL_NOSHA512_224 |
1199 | | case WC_HASH_TYPE_SHA512_224: |
1200 | | #endif |
1201 | | #ifndef WOLFSSL_NOSHA512_256 |
1202 | | case WC_HASH_TYPE_SHA512_256: |
1203 | | #endif |
1204 | | #ifdef WOLFSSL_SHA512 |
1205 | | ret = wc_Sha512SetFlags(&hash->alg.sha512, flags); |
1206 | | #endif |
1207 | | break; |
1208 | | |
1209 | | case WC_HASH_TYPE_SHA3_224: |
1210 | | case WC_HASH_TYPE_SHA3_256: |
1211 | | case WC_HASH_TYPE_SHA3_384: |
1212 | | case WC_HASH_TYPE_SHA3_512: |
1213 | | #ifdef WOLFSSL_SHA3 |
1214 | | ret = wc_Sha3_SetFlags(&hash->alg.sha3, flags); |
1215 | | #endif |
1216 | | break; |
1217 | | |
1218 | | #ifdef WOLFSSL_SM3 |
1219 | | case WC_HASH_TYPE_SM3: |
1220 | | ret = wc_Sm3SetFlags(&hash->alg.sm3, flags); |
1221 | | break; |
1222 | | #endif |
1223 | | |
1224 | | /* not supported */ |
1225 | | case WC_HASH_TYPE_MD5_SHA: |
1226 | | case WC_HASH_TYPE_MD2: |
1227 | | case WC_HASH_TYPE_MD4: |
1228 | | case WC_HASH_TYPE_BLAKE2B: |
1229 | | case WC_HASH_TYPE_BLAKE2S: |
1230 | | case WC_HASH_TYPE_NONE: |
1231 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128) |
1232 | | case WC_HASH_TYPE_SHAKE128: |
1233 | | #endif |
1234 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256) |
1235 | | case WC_HASH_TYPE_SHAKE256: |
1236 | | #endif |
1237 | | default: |
1238 | | ret = BAD_FUNC_ARG; |
1239 | | }; |
1240 | | |
1241 | | return ret; |
1242 | | } |
1243 | | int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, word32* flags) |
1244 | | { |
1245 | | int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */ |
1246 | | |
1247 | | if (hash == NULL) |
1248 | | return BAD_FUNC_ARG; |
1249 | | |
1250 | | switch (type) { |
1251 | | case WC_HASH_TYPE_MD5: |
1252 | | #ifndef NO_MD5 |
1253 | | ret = wc_Md5GetFlags(&hash->alg.md5, flags); |
1254 | | #endif |
1255 | | break; |
1256 | | case WC_HASH_TYPE_SHA: |
1257 | | #ifndef NO_SHA |
1258 | | ret = wc_ShaGetFlags(&hash->alg.sha, flags); |
1259 | | #endif |
1260 | | break; |
1261 | | case WC_HASH_TYPE_SHA224: |
1262 | | #ifdef WOLFSSL_SHA224 |
1263 | | ret = wc_Sha224GetFlags(&hash->alg.sha224, flags); |
1264 | | #endif |
1265 | | break; |
1266 | | case WC_HASH_TYPE_SHA256: |
1267 | | #ifndef NO_SHA256 |
1268 | | ret = wc_Sha256GetFlags(&hash->alg.sha256, flags); |
1269 | | #endif |
1270 | | break; |
1271 | | case WC_HASH_TYPE_SHA384: |
1272 | | #ifdef WOLFSSL_SHA384 |
1273 | | ret = wc_Sha384GetFlags(&hash->alg.sha384, flags); |
1274 | | #endif |
1275 | | break; |
1276 | | case WC_HASH_TYPE_SHA512: |
1277 | | #ifndef WOLFSSL_NOSHA512_224 |
1278 | | case WC_HASH_TYPE_SHA512_224: |
1279 | | #endif |
1280 | | #ifndef WOLFSSL_NOSHA512_256 |
1281 | | case WC_HASH_TYPE_SHA512_256: |
1282 | | #endif |
1283 | | #ifdef WOLFSSL_SHA512 |
1284 | | ret = wc_Sha512GetFlags(&hash->alg.sha512, flags); |
1285 | | #endif |
1286 | | break; |
1287 | | |
1288 | | case WC_HASH_TYPE_SHA3_224: |
1289 | | case WC_HASH_TYPE_SHA3_256: |
1290 | | case WC_HASH_TYPE_SHA3_384: |
1291 | | case WC_HASH_TYPE_SHA3_512: |
1292 | | #ifdef WOLFSSL_SHA3 |
1293 | | ret = wc_Sha3_GetFlags(&hash->alg.sha3, flags); |
1294 | | #endif |
1295 | | break; |
1296 | | |
1297 | | #ifdef WOLFSSL_SM3 |
1298 | | case WC_HASH_TYPE_SM3: |
1299 | | ret = wc_Sm3GetFlags(&hash->alg.sm3, flags); |
1300 | | break; |
1301 | | #endif |
1302 | | |
1303 | | /* not supported */ |
1304 | | case WC_HASH_TYPE_MD5_SHA: |
1305 | | case WC_HASH_TYPE_MD2: |
1306 | | case WC_HASH_TYPE_MD4: |
1307 | | case WC_HASH_TYPE_BLAKE2B: |
1308 | | case WC_HASH_TYPE_BLAKE2S: |
1309 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128) |
1310 | | case WC_HASH_TYPE_SHAKE128: |
1311 | | #endif |
1312 | | #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256) |
1313 | | case WC_HASH_TYPE_SHAKE256: |
1314 | | #endif |
1315 | | case WC_HASH_TYPE_NONE: |
1316 | | default: |
1317 | | ret = BAD_FUNC_ARG; |
1318 | | }; |
1319 | | |
1320 | | return ret; |
1321 | | } |
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 | 0 | { |
1331 | 0 | int ret; |
1332 | | #ifdef WOLFSSL_SMALL_STACK |
1333 | | wc_Md5* md5; |
1334 | | #else |
1335 | 0 | wc_Md5 md5[1]; |
1336 | 0 | #endif |
1337 | |
|
1338 | | #ifdef WOLFSSL_SMALL_STACK |
1339 | | md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1340 | | if (md5 == NULL) |
1341 | | return MEMORY_E; |
1342 | | #endif |
1343 | |
|
1344 | 0 | if ((ret = wc_InitMd5_ex(md5, heap, devId)) != 0) { |
1345 | 0 | WOLFSSL_MSG("InitMd5 failed"); |
1346 | 0 | } |
1347 | 0 | else { |
1348 | 0 | if ((ret = wc_Md5Update(md5, data, len)) != 0) { |
1349 | 0 | WOLFSSL_MSG("Md5Update failed"); |
1350 | 0 | } |
1351 | 0 | else if ((ret = wc_Md5Final(md5, hash)) != 0) { |
1352 | 0 | WOLFSSL_MSG("Md5Final failed"); |
1353 | 0 | } |
1354 | 0 | wc_Md5Free(md5); |
1355 | 0 | } |
1356 | |
|
1357 | | #ifdef WOLFSSL_SMALL_STACK |
1358 | | XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1359 | | #endif |
1360 | |
|
1361 | 0 | return ret; |
1362 | 0 | } |
1363 | | int wc_Md5Hash(const byte* data, word32 len, byte* hash) |
1364 | 0 | { |
1365 | 0 | int devId = INVALID_DEVID; |
1366 | | #ifdef WOLF_CRYPTO_CB |
1367 | | /* find devId if its not an empty hash */ |
1368 | | if (data != NULL && len > 0) { |
1369 | | devId = wc_CryptoCb_DefaultDevID(); |
1370 | | } |
1371 | | #endif |
1372 | 0 | return wc_Md5Hash_ex(data, len, hash, NULL, devId); |
1373 | 0 | } |
1374 | | #endif /* !NO_MD5 */ |
1375 | | |
1376 | | #if !defined(NO_SHA) |
1377 | | int wc_ShaHash_ex(const byte* data, word32 len, byte* hash, |
1378 | | void* heap, int devId) |
1379 | 0 | { |
1380 | 0 | int ret = 0; |
1381 | | #ifdef WOLFSSL_SMALL_STACK |
1382 | | wc_Sha* sha; |
1383 | | #else |
1384 | 0 | wc_Sha sha[1]; |
1385 | 0 | #endif |
1386 | |
|
1387 | | #ifdef WOLFSSL_SMALL_STACK |
1388 | | sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1389 | | if (sha == NULL) |
1390 | | return MEMORY_E; |
1391 | | #endif |
1392 | |
|
1393 | 0 | if ((ret = wc_InitSha_ex(sha, heap, devId)) != 0) { |
1394 | 0 | WOLFSSL_MSG("InitSha failed"); |
1395 | 0 | } |
1396 | 0 | else { |
1397 | 0 | if ((ret = wc_ShaUpdate(sha, data, len)) != 0) { |
1398 | 0 | WOLFSSL_MSG("ShaUpdate failed"); |
1399 | 0 | } |
1400 | 0 | else if ((ret = wc_ShaFinal(sha, hash)) != 0) { |
1401 | 0 | WOLFSSL_MSG("ShaFinal failed"); |
1402 | 0 | } |
1403 | 0 | wc_ShaFree(sha); |
1404 | 0 | } |
1405 | |
|
1406 | | #ifdef WOLFSSL_SMALL_STACK |
1407 | | XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1408 | | #endif |
1409 | |
|
1410 | 0 | return ret; |
1411 | 0 | } |
1412 | | int wc_ShaHash(const byte* data, word32 len, byte* hash) |
1413 | 0 | { |
1414 | 0 | int devId = INVALID_DEVID; |
1415 | | #ifdef WOLF_CRYPTO_CB |
1416 | | /* find devId if its not an empty hash */ |
1417 | | if (data != NULL && len > 0) { |
1418 | | devId = wc_CryptoCb_DefaultDevID(); |
1419 | | } |
1420 | | #endif |
1421 | 0 | return wc_ShaHash_ex(data, len, hash, NULL, devId); |
1422 | 0 | } |
1423 | | #endif /* !NO_SHA */ |
1424 | | |
1425 | | #if defined(WOLFSSL_SHA224) |
1426 | | int wc_Sha224Hash_ex(const byte* data, word32 len, byte* hash, |
1427 | | void* heap, int devId) |
1428 | 0 | { |
1429 | 0 | int ret = 0; |
1430 | | #ifdef WOLFSSL_SMALL_STACK |
1431 | | wc_Sha224* sha224; |
1432 | | #else |
1433 | 0 | wc_Sha224 sha224[1]; |
1434 | 0 | #endif |
1435 | |
|
1436 | | #ifdef WOLFSSL_SMALL_STACK |
1437 | | sha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL, |
1438 | | DYNAMIC_TYPE_TMP_BUFFER); |
1439 | | if (sha224 == NULL) |
1440 | | return MEMORY_E; |
1441 | | #endif |
1442 | |
|
1443 | 0 | if ((ret = wc_InitSha224_ex(sha224, heap, devId)) != 0) { |
1444 | 0 | WOLFSSL_MSG("InitSha224 failed"); |
1445 | 0 | } |
1446 | 0 | else { |
1447 | 0 | if ((ret = wc_Sha224Update(sha224, data, len)) != 0) { |
1448 | 0 | WOLFSSL_MSG("Sha224Update failed"); |
1449 | 0 | } |
1450 | 0 | else if ((ret = wc_Sha224Final(sha224, hash)) != 0) { |
1451 | 0 | WOLFSSL_MSG("Sha224Final failed"); |
1452 | 0 | } |
1453 | 0 | wc_Sha224Free(sha224); |
1454 | 0 | } |
1455 | |
|
1456 | | #ifdef WOLFSSL_SMALL_STACK |
1457 | | XFREE(sha224, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1458 | | #endif |
1459 | |
|
1460 | 0 | return ret; |
1461 | 0 | } |
1462 | | int wc_Sha224Hash(const byte* data, word32 len, byte* hash) |
1463 | 0 | { |
1464 | 0 | int devId = INVALID_DEVID; |
1465 | | #ifdef WOLF_CRYPTO_CB |
1466 | | /* find devId if its not an empty hash */ |
1467 | | if (data != NULL && len > 0) { |
1468 | | devId = wc_CryptoCb_DefaultDevID(); |
1469 | | } |
1470 | | #endif |
1471 | 0 | return wc_Sha224Hash_ex(data, len, hash, NULL, devId); |
1472 | 0 | } |
1473 | | #endif /* WOLFSSL_SHA224 */ |
1474 | | |
1475 | | #if !defined(NO_SHA256) |
1476 | | int wc_Sha256Hash_ex(const byte* data, word32 len, byte* hash, |
1477 | | void* heap, int devId) |
1478 | 0 | { |
1479 | 0 | int ret = 0; |
1480 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1481 | | wc_Sha256* sha256; |
1482 | | #else |
1483 | 0 | wc_Sha256 sha256[1]; |
1484 | 0 | #endif |
1485 | |
|
1486 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1487 | | sha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL, |
1488 | | DYNAMIC_TYPE_TMP_BUFFER); |
1489 | | if (sha256 == NULL) |
1490 | | return MEMORY_E; |
1491 | | #endif |
1492 | |
|
1493 | 0 | if ((ret = wc_InitSha256_ex(sha256, heap, devId)) != 0) { |
1494 | 0 | WOLFSSL_MSG("InitSha256 failed"); |
1495 | 0 | } |
1496 | 0 | else { |
1497 | 0 | if ((ret = wc_Sha256Update(sha256, data, len)) != 0) { |
1498 | 0 | WOLFSSL_MSG("Sha256Update failed"); |
1499 | 0 | } |
1500 | 0 | else if ((ret = wc_Sha256Final(sha256, hash)) != 0) { |
1501 | 0 | WOLFSSL_MSG("Sha256Final failed"); |
1502 | 0 | } |
1503 | 0 | wc_Sha256Free(sha256); |
1504 | 0 | } |
1505 | | |
1506 | |
|
1507 | | #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) |
1508 | | XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1509 | | #endif |
1510 | |
|
1511 | 0 | return ret; |
1512 | 0 | } |
1513 | | int wc_Sha256Hash(const byte* data, word32 len, byte* hash) |
1514 | 0 | { |
1515 | 0 | int devId = INVALID_DEVID; |
1516 | | #ifdef WOLF_CRYPTO_CB |
1517 | | /* find devId if its not an empty hash */ |
1518 | | if (data != NULL && len > 0) { |
1519 | | devId = wc_CryptoCb_DefaultDevID(); |
1520 | | } |
1521 | | #endif |
1522 | 0 | return wc_Sha256Hash_ex(data, len, hash, NULL, devId); |
1523 | 0 | } |
1524 | | #endif /* !NO_SHA256 */ |
1525 | | |
1526 | | #endif /* !defined(WOLFSSL_TI_HASH) */ |
1527 | | |
1528 | | |
1529 | | #if defined(WOLFSSL_SHA512) |
1530 | | int wc_Sha512Hash_ex(const byte* data, word32 len, byte* hash, |
1531 | | void* heap, int devId) |
1532 | 0 | { |
1533 | 0 | int ret = 0; |
1534 | | #ifdef WOLFSSL_SMALL_STACK |
1535 | | wc_Sha512* sha512; |
1536 | | #else |
1537 | 0 | wc_Sha512 sha512[1]; |
1538 | 0 | #endif |
1539 | |
|
1540 | | #ifdef WOLFSSL_SMALL_STACK |
1541 | | sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL, |
1542 | | DYNAMIC_TYPE_TMP_BUFFER); |
1543 | | if (sha512 == NULL) |
1544 | | return MEMORY_E; |
1545 | | #endif |
1546 | |
|
1547 | 0 | if ((ret = wc_InitSha512_ex(sha512, heap, devId)) != 0) { |
1548 | 0 | WOLFSSL_MSG("InitSha512 failed"); |
1549 | 0 | } |
1550 | 0 | else { |
1551 | 0 | if ((ret = wc_Sha512Update(sha512, data, len)) != 0) { |
1552 | 0 | WOLFSSL_MSG("Sha512Update failed"); |
1553 | 0 | } |
1554 | 0 | else if ((ret = wc_Sha512Final(sha512, hash)) != 0) { |
1555 | 0 | WOLFSSL_MSG("Sha512Final failed"); |
1556 | 0 | } |
1557 | 0 | wc_Sha512Free(sha512); |
1558 | 0 | } |
1559 | |
|
1560 | | #ifdef WOLFSSL_SMALL_STACK |
1561 | | XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1562 | | #endif |
1563 | |
|
1564 | 0 | return ret; |
1565 | 0 | } |
1566 | | int wc_Sha512Hash(const byte* data, word32 len, byte* hash) |
1567 | 0 | { |
1568 | 0 | int devId = INVALID_DEVID; |
1569 | | #ifdef WOLF_CRYPTO_CB |
1570 | | /* find devId if its not an empty hash */ |
1571 | | if (data != NULL && len > 0) { |
1572 | | devId = wc_CryptoCb_DefaultDevID(); |
1573 | | } |
1574 | | #endif |
1575 | 0 | return wc_Sha512Hash_ex(data, len, hash, NULL, devId); |
1576 | 0 | } |
1577 | | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
1578 | | #ifndef WOLFSSL_NOSHA512_224 |
1579 | | int wc_Sha512_224Hash_ex(const byte* data, word32 len, byte* hash, |
1580 | | void* heap, int devId) |
1581 | 0 | { |
1582 | 0 | int ret = 0; |
1583 | | #ifdef WOLFSSL_SMALL_STACK |
1584 | | wc_Sha512* sha512; |
1585 | | #else |
1586 | 0 | wc_Sha512 sha512[1]; |
1587 | 0 | #endif |
1588 | |
|
1589 | | #ifdef WOLFSSL_SMALL_STACK |
1590 | | sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL, |
1591 | | DYNAMIC_TYPE_TMP_BUFFER); |
1592 | | if (sha512 == NULL) |
1593 | | return MEMORY_E; |
1594 | | #endif |
1595 | |
|
1596 | 0 | if ((ret = wc_InitSha512_224_ex(sha512, heap, devId)) != 0) { |
1597 | 0 | WOLFSSL_MSG("wc_InitSha512_224 failed"); |
1598 | 0 | } |
1599 | 0 | else { |
1600 | 0 | if ((ret = wc_Sha512_224Update(sha512, data, len)) != 0) { |
1601 | 0 | WOLFSSL_MSG("wc_Sha512_224_Update failed"); |
1602 | 0 | } |
1603 | 0 | else if ((ret = wc_Sha512_224Final(sha512, hash)) != 0) { |
1604 | 0 | WOLFSSL_MSG("wc_Sha512_224_Final failed"); |
1605 | 0 | } |
1606 | 0 | wc_Sha512_224Free(sha512); |
1607 | 0 | } |
1608 | |
|
1609 | | #ifdef WOLFSSL_SMALL_STACK |
1610 | | XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1611 | | #endif |
1612 | |
|
1613 | 0 | return ret; |
1614 | 0 | } |
1615 | | int wc_Sha512_224Hash(const byte* data, word32 len, byte* hash) |
1616 | 0 | { |
1617 | 0 | int devId = INVALID_DEVID; |
1618 | | #ifdef WOLF_CRYPTO_CB |
1619 | | /* find devId if its not an empty hash */ |
1620 | | if (data != NULL && len > 0) { |
1621 | | devId = wc_CryptoCb_DefaultDevID(); |
1622 | | } |
1623 | | #endif |
1624 | 0 | return wc_Sha512_224Hash_ex(data, len, hash, NULL, devId); |
1625 | 0 | } |
1626 | | #endif /* !WOLFSSL_NOSHA512_224 */ |
1627 | | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
1628 | | |
1629 | | #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) |
1630 | | #ifndef WOLFSSL_NOSHA512_256 |
1631 | | int wc_Sha512_256Hash_ex(const byte* data, word32 len, byte* hash, |
1632 | | void* heap, int devId) |
1633 | 0 | { |
1634 | 0 | int ret = 0; |
1635 | | #ifdef WOLFSSL_SMALL_STACK |
1636 | | wc_Sha512* sha512; |
1637 | | #else |
1638 | 0 | wc_Sha512 sha512[1]; |
1639 | 0 | #endif |
1640 | |
|
1641 | | #ifdef WOLFSSL_SMALL_STACK |
1642 | | sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL, |
1643 | | DYNAMIC_TYPE_TMP_BUFFER); |
1644 | | if (sha512 == NULL) |
1645 | | return MEMORY_E; |
1646 | | #endif |
1647 | |
|
1648 | 0 | if ((ret = wc_InitSha512_256_ex(sha512, heap, devId)) != 0) { |
1649 | 0 | WOLFSSL_MSG("wc_InitSha512_256 failed"); |
1650 | 0 | } |
1651 | 0 | else { |
1652 | 0 | if ((ret = wc_Sha512_256Update(sha512, data, len)) != 0) { |
1653 | 0 | WOLFSSL_MSG("wc_Sha512_256_Update failed"); |
1654 | 0 | } |
1655 | 0 | else if ((ret = wc_Sha512_256Final(sha512, hash)) != 0) { |
1656 | 0 | WOLFSSL_MSG("wc_Sha512_256_Final failed"); |
1657 | 0 | } |
1658 | 0 | wc_Sha512_256Free(sha512); |
1659 | 0 | } |
1660 | |
|
1661 | | #ifdef WOLFSSL_SMALL_STACK |
1662 | | XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1663 | | #endif |
1664 | |
|
1665 | 0 | return ret; |
1666 | 0 | } |
1667 | | int wc_Sha512_256Hash(const byte* data, word32 len, byte* hash) |
1668 | 0 | { |
1669 | 0 | int devId = INVALID_DEVID; |
1670 | | #ifdef WOLF_CRYPTO_CB |
1671 | | /* find devId if its not an empty hash */ |
1672 | | if (data != NULL && len > 0) { |
1673 | | devId = wc_CryptoCb_DefaultDevID(); |
1674 | | } |
1675 | | #endif |
1676 | 0 | return wc_Sha512_256Hash_ex(data, len, hash, NULL, devId); |
1677 | 0 | } |
1678 | | #endif /* !WOLFSSL_NOSHA512_256 */ |
1679 | | #endif /* !HAVE_FIPS && !HAVE_SELFTEST */ |
1680 | | |
1681 | | #endif /* WOLFSSL_SHA512 */ |
1682 | | |
1683 | | #if defined(WOLFSSL_SHA384) |
1684 | | int wc_Sha384Hash_ex(const byte* data, word32 len, byte* hash, |
1685 | | void* heap, int devId) |
1686 | 0 | { |
1687 | 0 | int ret = 0; |
1688 | | #ifdef WOLFSSL_SMALL_STACK |
1689 | | wc_Sha384* sha384; |
1690 | | #else |
1691 | 0 | wc_Sha384 sha384[1]; |
1692 | 0 | #endif |
1693 | |
|
1694 | | #ifdef WOLFSSL_SMALL_STACK |
1695 | | sha384 = (wc_Sha384*)XMALLOC(sizeof(wc_Sha384), NULL, |
1696 | | DYNAMIC_TYPE_TMP_BUFFER); |
1697 | | if (sha384 == NULL) |
1698 | | return MEMORY_E; |
1699 | | #endif |
1700 | |
|
1701 | 0 | if ((ret = wc_InitSha384_ex(sha384, heap, devId)) != 0) { |
1702 | 0 | WOLFSSL_MSG("InitSha384 failed"); |
1703 | 0 | } |
1704 | 0 | else { |
1705 | 0 | if ((ret = wc_Sha384Update(sha384, data, len)) != 0) { |
1706 | 0 | WOLFSSL_MSG("Sha384Update failed"); |
1707 | 0 | } |
1708 | 0 | else if ((ret = wc_Sha384Final(sha384, hash)) != 0) { |
1709 | 0 | WOLFSSL_MSG("Sha384Final failed"); |
1710 | 0 | } |
1711 | 0 | wc_Sha384Free(sha384); |
1712 | 0 | } |
1713 | |
|
1714 | | #ifdef WOLFSSL_SMALL_STACK |
1715 | | XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1716 | | #endif |
1717 | |
|
1718 | 0 | return ret; |
1719 | 0 | } |
1720 | | int wc_Sha384Hash(const byte* data, word32 len, byte* hash) |
1721 | 0 | { |
1722 | 0 | int devId = INVALID_DEVID; |
1723 | | #ifdef WOLF_CRYPTO_CB |
1724 | | /* find devId if its not an empty hash */ |
1725 | | if (data != NULL && len > 0) { |
1726 | | devId = wc_CryptoCb_DefaultDevID(); |
1727 | | } |
1728 | | #endif |
1729 | 0 | return wc_Sha384Hash_ex(data, len, hash, NULL, devId); |
1730 | 0 | } |
1731 | | #endif /* WOLFSSL_SHA384 */ |
1732 | | |
1733 | | #if defined(WOLFSSL_SHA3) |
1734 | | #if !defined(WOLFSSL_NOSHA3_224) |
1735 | | int wc_Sha3_224Hash_ex(const byte* data, word32 len, byte* hash, |
1736 | | void* heap, int devId) |
1737 | 0 | { |
1738 | 0 | int ret = 0; |
1739 | | #ifdef WOLFSSL_SMALL_STACK |
1740 | | wc_Sha3* sha3; |
1741 | | #else |
1742 | 0 | wc_Sha3 sha3[1]; |
1743 | 0 | #endif |
1744 | |
|
1745 | | #ifdef WOLFSSL_SMALL_STACK |
1746 | | sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL, |
1747 | | DYNAMIC_TYPE_TMP_BUFFER); |
1748 | | if (sha3 == NULL) |
1749 | | return MEMORY_E; |
1750 | | #endif |
1751 | |
|
1752 | 0 | if ((ret = wc_InitSha3_224(sha3, heap, devId)) != 0) { |
1753 | 0 | WOLFSSL_MSG("InitSha3_224 failed"); |
1754 | 0 | } |
1755 | 0 | else { |
1756 | 0 | if ((ret = wc_Sha3_224_Update(sha3, data, len)) != 0) { |
1757 | 0 | WOLFSSL_MSG("Sha3_224_Update failed"); |
1758 | 0 | } |
1759 | 0 | else if ((ret = wc_Sha3_224_Final(sha3, hash)) != 0) { |
1760 | 0 | WOLFSSL_MSG("Sha3_224_Final failed"); |
1761 | 0 | } |
1762 | 0 | wc_Sha3_224_Free(sha3); |
1763 | 0 | } |
1764 | |
|
1765 | | #ifdef WOLFSSL_SMALL_STACK |
1766 | | XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1767 | | #endif |
1768 | |
|
1769 | 0 | return ret; |
1770 | 0 | } |
1771 | | int wc_Sha3_224Hash(const byte* data, word32 len, byte* hash) |
1772 | 0 | { |
1773 | 0 | int devId = INVALID_DEVID; |
1774 | | #ifdef WOLF_CRYPTO_CB |
1775 | | /* find devId if its not an empty hash */ |
1776 | | if (data != NULL && len > 0) { |
1777 | | devId = wc_CryptoCb_DefaultDevID(); |
1778 | | } |
1779 | | #endif |
1780 | 0 | return wc_Sha3_224Hash_ex(data, len, hash, NULL, devId); |
1781 | 0 | } |
1782 | | #endif /* !WOLFSSL_NOSHA3_224 */ |
1783 | | |
1784 | | #if !defined(WOLFSSL_NOSHA3_256) |
1785 | | int wc_Sha3_256Hash_ex(const byte* data, word32 len, byte* hash, |
1786 | | void* heap, int devId) |
1787 | 0 | { |
1788 | 0 | int ret = 0; |
1789 | | #ifdef WOLFSSL_SMALL_STACK |
1790 | | wc_Sha3* sha3; |
1791 | | #else |
1792 | 0 | wc_Sha3 sha3[1]; |
1793 | 0 | #endif |
1794 | |
|
1795 | | #ifdef WOLFSSL_SMALL_STACK |
1796 | | sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL, |
1797 | | DYNAMIC_TYPE_TMP_BUFFER); |
1798 | | if (sha3 == NULL) |
1799 | | return MEMORY_E; |
1800 | | #endif |
1801 | |
|
1802 | 0 | if ((ret = wc_InitSha3_256(sha3, heap, devId)) != 0) { |
1803 | 0 | WOLFSSL_MSG("InitSha3_256 failed"); |
1804 | 0 | } |
1805 | 0 | else { |
1806 | 0 | if ((ret = wc_Sha3_256_Update(sha3, data, len)) != 0) { |
1807 | 0 | WOLFSSL_MSG("Sha3_256_Update failed"); |
1808 | 0 | } |
1809 | 0 | else if ((ret = wc_Sha3_256_Final(sha3, hash)) != 0) { |
1810 | 0 | WOLFSSL_MSG("Sha3_256_Final failed"); |
1811 | 0 | } |
1812 | 0 | wc_Sha3_256_Free(sha3); |
1813 | 0 | } |
1814 | |
|
1815 | | #ifdef WOLFSSL_SMALL_STACK |
1816 | | XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1817 | | #endif |
1818 | |
|
1819 | 0 | return ret; |
1820 | 0 | } |
1821 | | int wc_Sha3_256Hash(const byte* data, word32 len, byte* hash) |
1822 | 0 | { |
1823 | 0 | int devId = INVALID_DEVID; |
1824 | | #ifdef WOLF_CRYPTO_CB |
1825 | | /* find devId if its not an empty hash */ |
1826 | | if (data != NULL && len > 0) { |
1827 | | devId = wc_CryptoCb_DefaultDevID(); |
1828 | | } |
1829 | | #endif |
1830 | 0 | return wc_Sha3_256Hash_ex(data, len, hash, NULL, devId); |
1831 | 0 | } |
1832 | | #endif /* !WOLFSSL_NOSHA3_256 */ |
1833 | | |
1834 | | #if !defined(WOLFSSL_NOSHA3_384) |
1835 | | int wc_Sha3_384Hash_ex(const byte* data, word32 len, byte* hash, |
1836 | | void* heap, int devId) |
1837 | 0 | { |
1838 | 0 | int ret = 0; |
1839 | | #ifdef WOLFSSL_SMALL_STACK |
1840 | | wc_Sha3* sha3; |
1841 | | #else |
1842 | 0 | wc_Sha3 sha3[1]; |
1843 | 0 | #endif |
1844 | |
|
1845 | | #ifdef WOLFSSL_SMALL_STACK |
1846 | | sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL, |
1847 | | DYNAMIC_TYPE_TMP_BUFFER); |
1848 | | if (sha3 == NULL) |
1849 | | return MEMORY_E; |
1850 | | #endif |
1851 | |
|
1852 | 0 | if ((ret = wc_InitSha3_384(sha3, heap, devId)) != 0) { |
1853 | 0 | WOLFSSL_MSG("InitSha3_384 failed"); |
1854 | 0 | } |
1855 | 0 | else { |
1856 | 0 | if ((ret = wc_Sha3_384_Update(sha3, data, len)) != 0) { |
1857 | 0 | WOLFSSL_MSG("Sha3_384_Update failed"); |
1858 | 0 | } |
1859 | 0 | else if ((ret = wc_Sha3_384_Final(sha3, hash)) != 0) { |
1860 | 0 | WOLFSSL_MSG("Sha3_384_Final failed"); |
1861 | 0 | } |
1862 | 0 | wc_Sha3_384_Free(sha3); |
1863 | 0 | } |
1864 | |
|
1865 | | #ifdef WOLFSSL_SMALL_STACK |
1866 | | XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1867 | | #endif |
1868 | |
|
1869 | 0 | return ret; |
1870 | 0 | } |
1871 | | int wc_Sha3_384Hash(const byte* data, word32 len, byte* hash) |
1872 | 0 | { |
1873 | 0 | int devId = INVALID_DEVID; |
1874 | | #ifdef WOLF_CRYPTO_CB |
1875 | | /* find devId if its not an empty hash */ |
1876 | | if (data != NULL && len > 0) { |
1877 | | devId = wc_CryptoCb_DefaultDevID(); |
1878 | | } |
1879 | | #endif |
1880 | 0 | return wc_Sha3_384Hash_ex(data, len, hash, NULL, devId); |
1881 | 0 | } |
1882 | | #endif /* !WOLFSSL_NOSHA3_384 */ |
1883 | | |
1884 | | #if !defined(WOLFSSL_NOSHA3_512) |
1885 | | int wc_Sha3_512Hash_ex(const byte* data, word32 len, byte* hash, |
1886 | | void* heap, int devId) |
1887 | 0 | { |
1888 | 0 | int ret = 0; |
1889 | | #ifdef WOLFSSL_SMALL_STACK |
1890 | | wc_Sha3* sha3; |
1891 | | #else |
1892 | 0 | wc_Sha3 sha3[1]; |
1893 | 0 | #endif |
1894 | |
|
1895 | | #ifdef WOLFSSL_SMALL_STACK |
1896 | | sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL, |
1897 | | DYNAMIC_TYPE_TMP_BUFFER); |
1898 | | if (sha3 == NULL) |
1899 | | return MEMORY_E; |
1900 | | #endif |
1901 | |
|
1902 | 0 | if ((ret = wc_InitSha3_512(sha3, heap, devId)) != 0) { |
1903 | 0 | WOLFSSL_MSG("InitSha3_512 failed"); |
1904 | 0 | } |
1905 | 0 | else { |
1906 | 0 | if ((ret = wc_Sha3_512_Update(sha3, data, len)) != 0) { |
1907 | 0 | WOLFSSL_MSG("Sha3_512_Update failed"); |
1908 | 0 | } |
1909 | 0 | else if ((ret = wc_Sha3_512_Final(sha3, hash)) != 0) { |
1910 | 0 | WOLFSSL_MSG("Sha3_512_Final failed"); |
1911 | 0 | } |
1912 | 0 | wc_Sha3_512_Free(sha3); |
1913 | 0 | } |
1914 | |
|
1915 | | #ifdef WOLFSSL_SMALL_STACK |
1916 | | XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1917 | | #endif |
1918 | |
|
1919 | 0 | return ret; |
1920 | 0 | } |
1921 | | int wc_Sha3_512Hash(const byte* data, word32 len, byte* hash) |
1922 | 0 | { |
1923 | 0 | int devId = INVALID_DEVID; |
1924 | | #ifdef WOLF_CRYPTO_CB |
1925 | | /* find devId if its not an empty hash */ |
1926 | | if (data != NULL && len > 0) { |
1927 | | devId = wc_CryptoCb_DefaultDevID(); |
1928 | | } |
1929 | | #endif |
1930 | 0 | return wc_Sha3_512Hash_ex(data, len, hash, NULL, devId); |
1931 | 0 | } |
1932 | | #endif /* !WOLFSSL_NOSHA3_512 */ |
1933 | | |
1934 | | #ifdef WOLFSSL_SHAKE128 |
1935 | | int wc_Shake128Hash_ex(const byte* data, word32 len, byte* hash, |
1936 | | word32 hashLen, void* heap, int devId) |
1937 | | { |
1938 | | int ret = 0; |
1939 | | #ifdef WOLFSSL_SMALL_STACK |
1940 | | wc_Shake* shake; |
1941 | | #else |
1942 | | wc_Shake shake[1]; |
1943 | | #endif |
1944 | | |
1945 | | #ifdef WOLFSSL_SMALL_STACK |
1946 | | shake = (wc_Shake*)XMALLOC(sizeof(wc_Shake), NULL, |
1947 | | DYNAMIC_TYPE_TMP_BUFFER); |
1948 | | if (shake == NULL) |
1949 | | return MEMORY_E; |
1950 | | #endif |
1951 | | |
1952 | | if ((ret = wc_InitShake128(shake, heap, devId)) != 0) { |
1953 | | WOLFSSL_MSG("InitShake128 failed"); |
1954 | | } |
1955 | | else { |
1956 | | if ((ret = wc_Shake128_Update(shake, data, len)) != 0) { |
1957 | | WOLFSSL_MSG("Shake128_Update failed"); |
1958 | | } |
1959 | | else if ((ret = wc_Shake128_Final(shake, hash, hashLen)) != 0) { |
1960 | | WOLFSSL_MSG("Shake128_Final failed"); |
1961 | | } |
1962 | | wc_Shake128_Free(shake); |
1963 | | } |
1964 | | |
1965 | | #ifdef WOLFSSL_SMALL_STACK |
1966 | | XFREE(shake, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1967 | | #endif |
1968 | | |
1969 | | return ret; |
1970 | | } |
1971 | | int wc_Shake128Hash(const byte* data, word32 len, byte* hash, |
1972 | | word32 hashLen) |
1973 | | { |
1974 | | int devId = INVALID_DEVID; |
1975 | | #ifdef WOLF_CRYPTO_CB |
1976 | | /* find devId if its not an empty hash */ |
1977 | | if (data != NULL && len > 0) { |
1978 | | devId = wc_CryptoCb_DefaultDevID(); |
1979 | | } |
1980 | | #endif |
1981 | | return wc_Shake128Hash_ex(data, len, hash, hashLen, |
1982 | | NULL, devId); |
1983 | | } |
1984 | | #endif /* WOLFSSL_SHAKE_128 */ |
1985 | | |
1986 | | #ifdef WOLFSSL_SHAKE256 |
1987 | | int wc_Shake256Hash_ex(const byte* data, word32 len, byte* hash, |
1988 | | word32 hashLen, void* heap, int devId) |
1989 | | { |
1990 | | int ret = 0; |
1991 | | #ifdef WOLFSSL_SMALL_STACK |
1992 | | wc_Shake* shake; |
1993 | | #else |
1994 | | wc_Shake shake[1]; |
1995 | | #endif |
1996 | | |
1997 | | #ifdef WOLFSSL_SMALL_STACK |
1998 | | shake = (wc_Shake*)XMALLOC(sizeof(wc_Shake), NULL, |
1999 | | DYNAMIC_TYPE_TMP_BUFFER); |
2000 | | if (shake == NULL) |
2001 | | return MEMORY_E; |
2002 | | #endif |
2003 | | |
2004 | | if ((ret = wc_InitShake256(shake, heap, devId)) != 0) { |
2005 | | WOLFSSL_MSG("InitShake256 failed"); |
2006 | | } |
2007 | | else { |
2008 | | if ((ret = wc_Shake256_Update(shake, data, len)) != 0) { |
2009 | | WOLFSSL_MSG("Shake256_Update failed"); |
2010 | | } |
2011 | | else if ((ret = wc_Shake256_Final(shake, hash, hashLen)) != 0) { |
2012 | | WOLFSSL_MSG("Shake256_Final failed"); |
2013 | | } |
2014 | | wc_Shake256_Free(shake); |
2015 | | } |
2016 | | |
2017 | | #ifdef WOLFSSL_SMALL_STACK |
2018 | | XFREE(shake, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
2019 | | #endif |
2020 | | |
2021 | | return ret; |
2022 | | } |
2023 | | int wc_Shake256Hash(const byte* data, word32 len, byte* hash, |
2024 | | word32 hashLen) |
2025 | | { |
2026 | | int devId = INVALID_DEVID; |
2027 | | #ifdef WOLF_CRYPTO_CB |
2028 | | /* find devId if its not an empty hash */ |
2029 | | if (data != NULL && len > 0) { |
2030 | | devId = wc_CryptoCb_DefaultDevID(); |
2031 | | } |
2032 | | #endif |
2033 | | return wc_Shake256Hash_ex(data, len, hash, hashLen, |
2034 | | NULL, devId); |
2035 | | } |
2036 | | #endif /* WOLFSSL_SHAKE_256 */ |
2037 | | #endif /* WOLFSSL_SHA3 */ |
2038 | | |
2039 | | #ifdef WOLFSSL_SM3 |
2040 | | int wc_Sm3Hash_ex(const byte* data, word32 len, byte* hash, |
2041 | | void* heap, int devId) |
2042 | | { |
2043 | | int ret = 0; |
2044 | | #ifdef WOLFSSL_SMALL_STACK |
2045 | | wc_Sm3* sm3; |
2046 | | #else |
2047 | | wc_Sm3 sm3[1]; |
2048 | | #endif |
2049 | | |
2050 | | #ifdef WOLFSSL_SMALL_STACK |
2051 | | sm3 = (wc_Sm3*)XMALLOC(sizeof(wc_Sm3), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
2052 | | if (sm3 == NULL) |
2053 | | return MEMORY_E; |
2054 | | #endif |
2055 | | |
2056 | | if ((ret = wc_InitSm3(sm3, heap, devId)) != 0) { |
2057 | | WOLFSSL_MSG("InitSm3 failed"); |
2058 | | } |
2059 | | else { |
2060 | | if ((ret = wc_Sm3Update(sm3, data, len)) != 0) { |
2061 | | WOLFSSL_MSG("Sm3Update failed"); |
2062 | | } |
2063 | | else if ((ret = wc_Sm3Final(sm3, hash)) != 0) { |
2064 | | WOLFSSL_MSG("Sm3Final failed"); |
2065 | | } |
2066 | | wc_Sm3Free(sm3); |
2067 | | } |
2068 | | |
2069 | | #ifdef WOLFSSL_SMALL_STACK |
2070 | | XFREE(sm3, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
2071 | | #endif |
2072 | | |
2073 | | return ret; |
2074 | | } |
2075 | | int wc_Sm3Hash(const byte* data, word32 len, byte* hash) |
2076 | | { |
2077 | | int devId = INVALID_DEVID; |
2078 | | #ifdef WOLF_CRYPTO_CB |
2079 | | /* find devId if its not an empty hash */ |
2080 | | if (data != NULL && len > 0) { |
2081 | | devId = wc_CryptoCb_DefaultDevID(); |
2082 | | } |
2083 | | #endif |
2084 | | return wc_Sm3Hash_ex(data, len, hash, NULL, devId); |
2085 | | } |
2086 | | #endif /* !WOLFSSL_NOSHA3_224 */ |
2087 | | |
2088 | | #endif /* !NO_HASH_WRAPPER */ |
2089 | | |
2090 | | #ifdef WOLFSSL_HASH_KEEP |
2091 | | int _wc_Hash_Grow(byte** msg, word32* used, word32* len, const byte* in, |
2092 | | int inSz, void* heap) |
2093 | | { |
2094 | | if (*len < *used + inSz) { |
2095 | | if (*msg == NULL) { |
2096 | | *msg = (byte*)XMALLOC(*used + inSz, heap, DYNAMIC_TYPE_TMP_BUFFER); |
2097 | | } |
2098 | | else { |
2099 | | byte* pt = (byte*)XREALLOC(*msg, *used + inSz, heap, |
2100 | | DYNAMIC_TYPE_TMP_BUFFER); |
2101 | | if (pt == NULL) { |
2102 | | return MEMORY_E; |
2103 | | } |
2104 | | *msg = pt; |
2105 | | } |
2106 | | if (*msg == NULL) { |
2107 | | return MEMORY_E; |
2108 | | } |
2109 | | *len = *used + inSz; |
2110 | | } |
2111 | | XMEMCPY(*msg + *used, in, inSz); |
2112 | | *used += inSz; |
2113 | | return 0; |
2114 | | } |
2115 | | #endif /* WOLFSSL_HASH_KEEP */ |
2116 | | |