Coverage Report

Created: 2023-06-07 06:14

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