Coverage Report

Created: 2025-11-16 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-sp-math/wolfcrypt/src/hmac.c
Line
Count
Source
1
/* hmac.c
2
 *
3
 * Copyright (C) 2006-2025 wolfSSL Inc.
4
 *
5
 * This file is part of wolfSSL.
6
 *
7
 * wolfSSL is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * wolfSSL is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20
 */
21
22
23
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
24
25
#ifndef NO_HMAC
26
27
#if FIPS_VERSION3_GE(2,0,0)
28
    /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
29
    #define FIPS_NO_WRAPPERS
30
31
    #ifdef USE_WINDOWS_API
32
        #pragma code_seg(".fipsA$g")
33
        #pragma const_seg(".fipsB$g")
34
    #endif
35
#endif
36
37
#include <wolfssl/wolfcrypt/hmac.h>
38
39
#ifdef WOLF_CRYPTO_CB
40
    #include <wolfssl/wolfcrypt/cryptocb.h>
41
#endif
42
43
#ifdef NO_INLINE
44
    #include <wolfssl/wolfcrypt/misc.h>
45
#else
46
    #define WOLFSSL_MISC_INCLUDED
47
    #include <wolfcrypt/src/misc.c>
48
#endif
49
50
#ifdef WOLFSSL_KCAPI_HMAC
51
    #include <wolfssl/wolfcrypt/port/kcapi/kcapi_hmac.h>
52
53
    /* map the _Software calls used by kcapi_hmac.c */
54
    #define wc_HmacSetKey  wc_HmacSetKey_Software
55
    #define wc_HmacUpdate  wc_HmacUpdate_Software
56
    #define wc_HmacFinal   wc_HmacFinal_Software
57
#endif
58
59
#if FIPS_VERSION3_GE(6,0,0)
60
    const unsigned int wolfCrypt_FIPS_hmac_ro_sanity[2] =
61
                                                     { 0x1a2b3c4d, 0x00000008 };
62
    int wolfCrypt_FIPS_HMAC_sanity(void)
63
    {
64
        return 0;
65
    }
66
#endif
67
68
int wc_HmacSizeByType(int type)
69
3.05k
{
70
3.05k
    int ret;
71
72
3.05k
    if (!(type == WC_MD5 || type == WC_SHA ||
73
2.37k
    #ifdef WOLFSSL_SM3
74
2.37k
            type == WC_SM3 ||
75
2.37k
    #endif
76
2.37k
            type == WC_SHA224 || type == WC_SHA256 ||
77
486
            type == WC_SHA384 || type == WC_SHA512 ||
78
18
            type == WC_SHA3_224 || type == WC_SHA3_256 ||
79
18
            type == WC_SHA3_384 || type == WC_SHA3_512)) {
80
18
        return BAD_FUNC_ARG;
81
18
    }
82
83
3.03k
    switch (type) {
84
0
    #ifndef NO_MD5
85
96
        case WC_MD5:
86
96
            ret = WC_MD5_DIGEST_SIZE;
87
96
            break;
88
0
    #endif /* !NO_MD5 */
89
90
0
    #ifndef NO_SHA
91
579
        case WC_SHA:
92
579
            ret = WC_SHA_DIGEST_SIZE;
93
579
            break;
94
0
    #endif /* !NO_SHA */
95
96
0
    #ifdef WOLFSSL_SHA224
97
261
        case WC_SHA224:
98
261
            ret = WC_SHA224_DIGEST_SIZE;
99
261
            break;
100
0
    #endif /* WOLFSSL_SHA224 */
101
102
0
    #ifndef NO_SHA256
103
1.62k
        case WC_SHA256:
104
1.62k
            ret = WC_SHA256_DIGEST_SIZE;
105
1.62k
            break;
106
0
    #endif /* !NO_SHA256 */
107
108
0
    #ifdef WOLFSSL_SHA384
109
213
        case WC_SHA384:
110
213
            ret = WC_SHA384_DIGEST_SIZE;
111
213
            break;
112
0
    #endif /* WOLFSSL_SHA384 */
113
0
    #ifdef WOLFSSL_SHA512
114
255
        case WC_SHA512:
115
255
            ret = WC_SHA512_DIGEST_SIZE;
116
255
            break;
117
0
    #endif /* WOLFSSL_SHA512 */
118
119
0
    #ifdef WOLFSSL_SHA3
120
0
        case WC_SHA3_224:
121
0
            ret = WC_SHA3_224_DIGEST_SIZE;
122
0
            break;
123
124
0
        case WC_SHA3_256:
125
0
            ret = WC_SHA3_256_DIGEST_SIZE;
126
0
            break;
127
128
0
        case WC_SHA3_384:
129
0
            ret = WC_SHA3_384_DIGEST_SIZE;
130
0
            break;
131
132
0
        case WC_SHA3_512:
133
0
            ret = WC_SHA3_512_DIGEST_SIZE;
134
0
            break;
135
0
    #endif /* WOLFSSL_SHA3 */
136
137
0
    #ifdef WOLFSSL_SM3
138
0
        case WC_SM3:
139
0
            ret = WC_SM3_DIGEST_SIZE;
140
0
            break;
141
0
    #endif
142
143
0
        default:
144
0
            ret = BAD_FUNC_ARG;
145
0
            break;
146
3.03k
    }
147
148
3.03k
    return ret;
149
3.03k
}
150
151
static int HmacKeyInitHash(wc_HmacHash* hash, int type, void* heap, int devId)
152
34.3k
{
153
34.3k
    int ret = 0;
154
155
34.3k
    switch (type) {
156
0
    #ifndef NO_MD5
157
1.99k
        case WC_MD5:
158
1.99k
            ret = wc_InitMd5_ex(&hash->md5, heap, devId);
159
1.99k
            break;
160
0
    #endif /* !NO_MD5 */
161
162
0
    #ifndef NO_SHA
163
6.16k
        case WC_SHA:
164
6.16k
            ret = wc_InitSha_ex(&hash->sha, heap, devId);
165
6.16k
            break;
166
0
    #endif /* !NO_SHA */
167
168
0
    #ifdef WOLFSSL_SHA224
169
5.91k
        case WC_SHA224:
170
5.91k
            ret = wc_InitSha224_ex(&hash->sha224, heap, devId);
171
5.91k
            break;
172
0
    #endif /* WOLFSSL_SHA224 */
173
174
0
    #ifndef NO_SHA256
175
8.26k
        case WC_SHA256:
176
8.26k
            ret = wc_InitSha256_ex(&hash->sha256, heap, devId);
177
8.26k
            break;
178
0
    #endif /* !NO_SHA256 */
179
180
0
    #ifdef WOLFSSL_SHA384
181
4.95k
        case WC_SHA384:
182
4.95k
            ret = wc_InitSha384_ex(&hash->sha384, heap, devId);
183
4.95k
            break;
184
0
    #endif /* WOLFSSL_SHA384 */
185
0
    #ifdef WOLFSSL_SHA512
186
7.09k
        case WC_SHA512:
187
7.09k
            ret = wc_InitSha512_ex(&hash->sha512, heap, devId);
188
7.09k
            break;
189
0
    #endif /* WOLFSSL_SHA512 */
190
191
0
    #ifdef WOLFSSL_SHA3
192
0
    #ifndef WOLFSSL_NOSHA3_224
193
0
        case WC_SHA3_224:
194
0
            ret = wc_InitSha3_224(&hash->sha3, heap, devId);
195
0
            break;
196
0
    #endif
197
0
    #ifndef WOLFSSL_NOSHA3_256
198
0
        case WC_SHA3_256:
199
0
            ret = wc_InitSha3_256(&hash->sha3, heap, devId);
200
0
            break;
201
0
    #endif
202
0
    #ifndef WOLFSSL_NOSHA3_384
203
0
        case WC_SHA3_384:
204
0
            ret = wc_InitSha3_384(&hash->sha3, heap, devId);
205
0
            break;
206
0
    #endif
207
0
    #ifndef WOLFSSL_NOSHA3_512
208
0
        case WC_SHA3_512:
209
0
            ret = wc_InitSha3_512(&hash->sha3, heap, devId);
210
0
            break;
211
0
    #endif
212
0
    #endif
213
214
0
    #ifdef WOLFSSL_SM3
215
0
        case WC_SM3:
216
0
            ret = wc_InitSm3(&hash->sm3, heap, devId);
217
0
            break;
218
0
    #endif
219
220
0
        default:
221
0
            ret = BAD_FUNC_ARG;
222
0
            break;
223
34.3k
    }
224
225
34.3k
    return ret;
226
34.3k
}
227
228
int _InitHmac(Hmac* hmac, int type, void* heap)
229
71.8k
{
230
71.8k
    int ret;
231
71.8k
#ifdef WOLF_CRYPTO_CB
232
71.8k
    int devId = hmac->devId;
233
#else
234
    int devId = INVALID_DEVID;
235
#endif
236
237
71.8k
    ret = HmacKeyInitHash(&hmac->hash, type, heap, devId);
238
71.8k
    if (ret != 0)
239
0
        return ret;
240
241
    /* default to NULL heap hint or test value */
242
#ifdef WOLFSSL_HEAP_TEST
243
    hmac->heap = (void*)WOLFSSL_HEAP_TEST;
244
#else
245
71.8k
    hmac->heap = heap;
246
71.8k
#endif /* WOLFSSL_HEAP_TEST */
247
248
71.8k
    return ret;
249
71.8k
}
250
251
#ifdef WOLFSSL_HMAC_COPY_HASH
252
static int HmacKeyCopyHash(byte macType, wc_HmacHash* src, wc_HmacHash* dst)
253
{
254
    int ret = 0;
255
256
    switch (macType) {
257
    #ifndef NO_MD5
258
        case WC_MD5:
259
            ret = wc_Md5Copy(&src->md5, &dst->md5);
260
            break;
261
    #endif /* !NO_MD5 */
262
263
    #ifndef NO_SHA
264
        case WC_SHA:
265
            ret = wc_ShaCopy(&src->sha, &dst->sha);
266
            break;
267
    #endif /* !NO_SHA */
268
269
    #ifdef WOLFSSL_SHA224
270
        case WC_SHA224:
271
            ret = wc_Sha224Copy(&src->sha224, &dst->sha224);
272
            break;
273
    #endif /* WOLFSSL_SHA224 */
274
    #ifndef NO_SHA256
275
        case WC_SHA256:
276
            ret = wc_Sha256Copy(&src->sha256, &dst->sha256);
277
            break;
278
    #endif /* !NO_SHA256 */
279
280
    #ifdef WOLFSSL_SHA384
281
        case WC_SHA384:
282
            ret = wc_Sha384Copy(&src->sha384, &dst->sha384);
283
            break;
284
    #endif /* WOLFSSL_SHA384 */
285
    #ifdef WOLFSSL_SHA512
286
        case WC_SHA512:
287
            ret = wc_Sha512Copy(&src->sha512, &dst->sha512);
288
            break;
289
    #endif /* WOLFSSL_SHA512 */
290
291
    #ifdef WOLFSSL_SHA3
292
    #ifndef WOLFSSL_NOSHA3_224
293
        case WC_SHA3_224:
294
            ret = wc_Sha3_224_Copy(&src->sha3, &dst->sha3);
295
            break;
296
    #endif
297
    #ifndef WOLFSSL_NOSHA3_256
298
        case WC_SHA3_256:
299
            ret = wc_Sha3_256_Copy(&src->sha3, &dst->sha3);
300
            break;
301
    #endif
302
    #ifndef WOLFSSL_NOSHA3_384
303
        case WC_SHA3_384:
304
            ret = wc_Sha3_384_Copy(&src->sha3, &dst->sha3);
305
            break;
306
    #endif
307
    #ifndef WOLFSSL_NOSHA3_512
308
        case WC_SHA3_512:
309
            ret = wc_Sha3_512_Copy(&src->sha3, &dst->sha3);
310
            break;
311
    #endif
312
    #endif /* WOLFSSL_SHA3 */
313
314
    #ifdef WOLFSSL_SM3
315
        case WC_SM3:
316
            ret = wc_Sm3Copy(&src->sm3, &dst->sm3);
317
            break;
318
    #endif
319
320
        default:
321
            break;
322
    }
323
324
    return ret;
325
}
326
#endif
327
328
static int HmacKeyHashUpdate(byte macType, wc_HmacHash* hash, byte* pad)
329
48.5k
{
330
48.5k
    int ret = 0;
331
332
48.5k
    switch (macType) {
333
0
    #ifndef NO_MD5
334
6.36k
        case WC_MD5:
335
6.36k
            ret = wc_Md5Update(&hash->md5, pad, WC_MD5_BLOCK_SIZE);
336
6.36k
            break;
337
0
    #endif /* !NO_MD5 */
338
339
0
    #ifndef NO_SHA
340
8.98k
        case WC_SHA:
341
8.98k
            ret = wc_ShaUpdate(&hash->sha, pad, WC_SHA_BLOCK_SIZE);
342
8.98k
            break;
343
0
    #endif /* !NO_SHA */
344
345
0
    #ifdef WOLFSSL_SHA224
346
6.71k
        case WC_SHA224:
347
6.71k
            ret = wc_Sha224Update(&hash->sha224, pad, WC_SHA224_BLOCK_SIZE);
348
6.71k
            break;
349
0
    #endif /* WOLFSSL_SHA224 */
350
0
    #ifndef NO_SHA256
351
12.5k
        case WC_SHA256:
352
12.5k
            ret = wc_Sha256Update(&hash->sha256, pad, WC_SHA256_BLOCK_SIZE);
353
12.5k
            break;
354
0
    #endif /* !NO_SHA256 */
355
356
0
    #ifdef WOLFSSL_SHA384
357
6.45k
        case WC_SHA384:
358
6.45k
            ret = wc_Sha384Update(&hash->sha384, pad, WC_SHA384_BLOCK_SIZE);
359
6.45k
            break;
360
0
    #endif /* WOLFSSL_SHA384 */
361
0
    #ifdef WOLFSSL_SHA512
362
7.51k
        case WC_SHA512:
363
7.51k
            ret = wc_Sha512Update(&hash->sha512, pad, WC_SHA512_BLOCK_SIZE);
364
7.51k
            break;
365
0
    #endif /* WOLFSSL_SHA512 */
366
367
0
    #ifdef WOLFSSL_SHA3
368
0
    #ifndef WOLFSSL_NOSHA3_224
369
0
        case WC_SHA3_224:
370
0
            ret = wc_Sha3_224_Update(&hash->sha3, pad, WC_SHA3_224_BLOCK_SIZE);
371
0
            break;
372
0
    #endif
373
0
    #ifndef WOLFSSL_NOSHA3_256
374
0
        case WC_SHA3_256:
375
0
            ret = wc_Sha3_256_Update(&hash->sha3, pad, WC_SHA3_256_BLOCK_SIZE);
376
0
            break;
377
0
    #endif
378
0
    #ifndef WOLFSSL_NOSHA3_384
379
0
        case WC_SHA3_384:
380
0
            ret = wc_Sha3_384_Update(&hash->sha3, pad, WC_SHA3_384_BLOCK_SIZE);
381
0
            break;
382
0
    #endif
383
0
    #ifndef WOLFSSL_NOSHA3_512
384
0
        case WC_SHA3_512:
385
0
            ret = wc_Sha3_512_Update(&hash->sha3, pad, WC_SHA3_512_BLOCK_SIZE);
386
0
            break;
387
0
    #endif
388
0
    #endif /* WOLFSSL_SHA3 */
389
390
0
    #ifdef WOLFSSL_SM3
391
0
        case WC_SM3:
392
0
            ret = wc_Sm3Update(&hash->sm3, pad, WC_SM3_BLOCK_SIZE);
393
0
            break;
394
0
    #endif
395
396
0
        default:
397
0
            break;
398
48.5k
    }
399
400
48.5k
    return ret;
401
48.5k
}
402
403
#ifdef WOLFSSL_HMAC_COPY_HASH
404
int _HmacInitIOHashes(Hmac* hmac)
405
{
406
    int ret;
407
#ifdef WOLF_CRYPTO_CB
408
    int devId = hmac->devId;
409
#else
410
    int devId = INVALID_DEVID;
411
#endif
412
413
    ret = HmacKeyInitHash(&hmac->i_hash, hmac->macType, hmac->heap, devId);
414
    if (ret == 0) {
415
        ret = HmacKeyInitHash(&hmac->o_hash, hmac->macType, hmac->heap, devId);
416
    }
417
    if (ret == 0) {
418
        ret = HmacKeyHashUpdate(hmac->macType, &hmac->i_hash,
419
            (byte*)hmac->ipad);
420
    }
421
    if (ret == 0) {
422
        ret = HmacKeyHashUpdate(hmac->macType, &hmac->o_hash,
423
            (byte*)hmac->opad);
424
    }
425
426
    return ret;
427
}
428
#endif
429
430
int wc_HmacSetKey_ex(Hmac* hmac, int type, const byte* key, word32 length,
431
                     int allowFlag)
432
34.4k
{
433
34.4k
#ifndef WOLFSSL_MAXQ108X
434
34.4k
    byte*  ip;
435
34.4k
    byte*  op;
436
34.4k
    word32 hmac_block_size = 0;
437
34.4k
#endif
438
34.4k
    int    ret = 0;
439
34.4k
    void*  heap = NULL;
440
441
34.4k
    if (hmac == NULL || (key == NULL && length != 0) ||
442
34.4k
       !(type == WC_MD5 || type == WC_SHA ||
443
26.2k
    #ifdef WOLFSSL_SM3
444
26.2k
            type == WC_SM3 ||
445
26.2k
    #endif
446
26.2k
            type == WC_SHA224 || type == WC_SHA256 ||
447
12.0k
            type == WC_SHA384 || type == WC_SHA512 ||
448
41
            type == WC_SHA3_224 || type == WC_SHA3_256 ||
449
41
            type == WC_SHA3_384 || type == WC_SHA3_512)) {
450
41
        return BAD_FUNC_ARG;
451
41
    }
452
453
34.3k
    heap = hmac->heap;
454
34.3k
#if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0)
455
    /* if set key has already been run then make sure and free existing */
456
    /* This is for async and PIC32MZ situations, and just normally OK,
457
       provided the user calls wc_HmacInit() first. That function is not
458
       available in FIPS builds. In current FIPS builds, the hashes are
459
       not allocating resources. */
460
34.3k
    if (hmac->macType != WC_HASH_TYPE_NONE) {
461
31.2k
    #ifdef WOLF_CRYPTO_CB
462
31.2k
        int devId = hmac->devId;
463
31.2k
    #endif
464
31.2k
        wc_HmacFree(hmac);
465
31.2k
    #ifdef WOLF_CRYPTO_CB
466
31.2k
        hmac->devId = devId;
467
31.2k
    #endif
468
31.2k
    }
469
34.3k
#endif
470
471
34.3k
    hmac->innerHashKeyed = 0;
472
34.3k
    hmac->macType = (byte)type;
473
474
34.3k
    ret = _InitHmac(hmac, type, heap);
475
34.3k
    if (ret != 0)
476
0
        return ret;
477
478
    /* Regarding the password length:
479
     * SP800-107r1 ss 5.3.2 states: "An HMAC key shall have a security strength
480
     * that meets or exceeds the security strength required to protect the data
481
     * over which the HMAC is computed" then refers to SP800-133 for HMAC keys
482
     * generation.
483
     *
484
     * SP800-133r2 ss 6.2.3 states: "When a key is generated from a password,
485
     * the entropy provided (and thus, the maximum security strength that can be
486
     * supported by the generated key) shall be considered to be zero unless the
487
     * password is generated using an approved RBG"
488
     *
489
     * wolfSSL Notes: The statement from SP800-133r2 applies to
490
     * all password lengths. Any human generated password is considered to have
491
     * 0 security strength regardless of length, there is no minimum length that
492
     * is OK or will provide any amount of security strength other than 0. If
493
     * a security strength is required users shall generate random passwords
494
     * using a FIPS approved RBG of sufficient length that any HMAC key
495
     * generated from that password can claim to inherit the needed security
496
     * strength from that input.
497
     */
498
499
    /* In light of the above, Loosen past restriction that limited passwords to
500
     * no less than 14-bytes to allow for shorter Passwords.
501
     * User needs to pass true (non-zero) to override historical behavior that
502
     * prevented use of any password less than 14-bytes. ALL non-RBG generated
503
     * passwords shall inherit a security strength of zero
504
     * (no security strength)
505
     */
506
34.3k
    if (!allowFlag) {
507
0
        if (length < HMAC_FIPS_MIN_KEY) {
508
0
            WOLFSSL_ERROR_VERBOSE(HMAC_MIN_KEYLEN_E);
509
0
            return HMAC_MIN_KEYLEN_E;
510
0
        }
511
0
    }
512
513
34.3k
#ifdef WOLF_CRYPTO_CB
514
34.3k
    hmac->keyRaw = key; /* use buffer directly */
515
34.3k
    hmac->keyLen = (word16)length;
516
34.3k
#endif
517
518
#ifdef WOLFSSL_MAXQ108X
519
    /* For MAXQ108x, nothing left to do. */
520
    return 0;
521
#else
522
523
34.3k
    ip = (byte*)hmac->ipad;
524
34.3k
    op = (byte*)hmac->opad;
525
526
34.3k
    switch (hmac->macType) {
527
0
    #ifndef NO_MD5
528
1.99k
        case WC_MD5:
529
1.99k
            hmac_block_size = WC_MD5_BLOCK_SIZE;
530
1.99k
            if (length <= WC_MD5_BLOCK_SIZE) {
531
1.94k
                if (key != NULL) {
532
1.94k
                    XMEMCPY(ip, key, length);
533
1.94k
                }
534
1.94k
            }
535
56
            else {
536
56
                ret = wc_Md5Update(&hmac->hash.md5, key, length);
537
56
                if (ret != 0)
538
0
                    break;
539
56
                ret = wc_Md5Final(&hmac->hash.md5, ip);
540
56
                if (ret != 0)
541
0
                    break;
542
56
                length = WC_MD5_DIGEST_SIZE;
543
56
            }
544
1.99k
            break;
545
1.99k
    #endif /* !NO_MD5 */
546
547
1.99k
    #ifndef NO_SHA
548
6.16k
        case WC_SHA:
549
6.16k
            hmac_block_size = WC_SHA_BLOCK_SIZE;
550
6.16k
            if (length <= WC_SHA_BLOCK_SIZE) {
551
6.13k
                if (key != NULL) {
552
6.13k
                    XMEMCPY(ip, key, length);
553
6.13k
                }
554
6.13k
            }
555
32
            else {
556
32
                ret = wc_ShaUpdate(&hmac->hash.sha, key, length);
557
32
                if (ret != 0)
558
0
                    break;
559
32
                ret = wc_ShaFinal(&hmac->hash.sha, ip);
560
32
                if (ret != 0)
561
0
                    break;
562
563
32
                length = WC_SHA_DIGEST_SIZE;
564
32
            }
565
6.16k
            break;
566
6.16k
    #endif /* !NO_SHA */
567
568
6.16k
    #ifdef WOLFSSL_SHA224
569
6.16k
        case WC_SHA224:
570
5.91k
            hmac_block_size = WC_SHA224_BLOCK_SIZE;
571
5.91k
            if (length <= WC_SHA224_BLOCK_SIZE) {
572
5.88k
                if (key != NULL) {
573
5.88k
                    XMEMCPY(ip, key, length);
574
5.88k
                }
575
5.88k
            }
576
32
            else {
577
32
                ret = wc_Sha224Update(&hmac->hash.sha224, key, length);
578
32
                if (ret != 0)
579
0
                    break;
580
32
                ret = wc_Sha224Final(&hmac->hash.sha224, ip);
581
32
                if (ret != 0)
582
0
                    break;
583
584
32
                length = WC_SHA224_DIGEST_SIZE;
585
32
            }
586
5.91k
            break;
587
5.91k
    #endif /* WOLFSSL_SHA224 */
588
5.91k
    #ifndef NO_SHA256
589
8.26k
        case WC_SHA256:
590
8.26k
            hmac_block_size = WC_SHA256_BLOCK_SIZE;
591
8.26k
            if (length <= WC_SHA256_BLOCK_SIZE) {
592
8.21k
                if (key != NULL) {
593
8.21k
                    XMEMCPY(ip, key, length);
594
8.21k
                }
595
8.21k
            }
596
49
            else {
597
49
                ret = wc_Sha256Update(&hmac->hash.sha256, key, length);
598
49
                if (ret != 0)
599
0
                    break;
600
49
                ret = wc_Sha256Final(&hmac->hash.sha256, ip);
601
49
                if (ret != 0)
602
0
                    break;
603
604
49
                length = WC_SHA256_DIGEST_SIZE;
605
49
            }
606
8.26k
            break;
607
8.26k
    #endif /* !NO_SHA256 */
608
609
8.26k
    #ifdef WOLFSSL_SHA384
610
8.26k
        case WC_SHA384:
611
4.95k
            hmac_block_size = WC_SHA384_BLOCK_SIZE;
612
4.95k
            if (length <= WC_SHA384_BLOCK_SIZE) {
613
4.92k
                if (key != NULL) {
614
4.92k
                    XMEMCPY(ip, key, length);
615
4.92k
                }
616
4.92k
            }
617
33
            else {
618
33
                ret = wc_Sha384Update(&hmac->hash.sha384, key, length);
619
33
                if (ret != 0)
620
0
                    break;
621
33
                ret = wc_Sha384Final(&hmac->hash.sha384, ip);
622
33
                if (ret != 0)
623
0
                    break;
624
625
33
                length = WC_SHA384_DIGEST_SIZE;
626
33
            }
627
4.95k
            break;
628
4.95k
    #endif /* WOLFSSL_SHA384 */
629
4.95k
    #ifdef WOLFSSL_SHA512
630
7.09k
        case WC_SHA512:
631
7.09k
            hmac_block_size = WC_SHA512_BLOCK_SIZE;
632
7.09k
            if (length <= WC_SHA512_BLOCK_SIZE) {
633
7.05k
                if (key != NULL) {
634
7.05k
                    XMEMCPY(ip, key, length);
635
7.05k
                }
636
7.05k
            }
637
31
            else {
638
31
                ret = wc_Sha512Update(&hmac->hash.sha512, key, length);
639
31
                if (ret != 0)
640
0
                    break;
641
31
                ret = wc_Sha512Final(&hmac->hash.sha512, ip);
642
31
                if (ret != 0)
643
0
                    break;
644
645
31
                length = WC_SHA512_DIGEST_SIZE;
646
31
            }
647
7.09k
            break;
648
7.09k
    #endif /* WOLFSSL_SHA512 */
649
650
7.09k
    #ifdef WOLFSSL_SHA3
651
7.09k
    #ifndef WOLFSSL_NOSHA3_224
652
7.09k
        case WC_SHA3_224:
653
0
            hmac_block_size = WC_SHA3_224_BLOCK_SIZE;
654
0
            if (length <= WC_SHA3_224_BLOCK_SIZE) {
655
0
                if (key != NULL) {
656
0
                    XMEMCPY(ip, key, length);
657
0
                }
658
0
            }
659
0
            else {
660
0
                ret = wc_Sha3_224_Update(&hmac->hash.sha3, key, length);
661
0
                if (ret != 0)
662
0
                    break;
663
0
                ret = wc_Sha3_224_Final(&hmac->hash.sha3, ip);
664
0
                if (ret != 0)
665
0
                    break;
666
667
0
                length = WC_SHA3_224_DIGEST_SIZE;
668
0
            }
669
0
            break;
670
0
    #endif
671
0
    #ifndef WOLFSSL_NOSHA3_256
672
0
        case WC_SHA3_256:
673
0
            hmac_block_size = WC_SHA3_256_BLOCK_SIZE;
674
0
            if (length <= WC_SHA3_256_BLOCK_SIZE) {
675
0
                if (key != NULL) {
676
0
                    XMEMCPY(ip, key, length);
677
0
                }
678
0
            }
679
0
            else {
680
0
                ret = wc_Sha3_256_Update(&hmac->hash.sha3, key, length);
681
0
                if (ret != 0)
682
0
                    break;
683
0
                ret = wc_Sha3_256_Final(&hmac->hash.sha3, ip);
684
0
                if (ret != 0)
685
0
                    break;
686
687
0
                length = WC_SHA3_256_DIGEST_SIZE;
688
0
            }
689
0
            break;
690
0
    #endif
691
0
    #ifndef WOLFSSL_NOSHA3_384
692
0
        case WC_SHA3_384:
693
0
            hmac_block_size = WC_SHA3_384_BLOCK_SIZE;
694
0
            if (length <= WC_SHA3_384_BLOCK_SIZE) {
695
0
                if (key != NULL) {
696
0
                    XMEMCPY(ip, key, length);
697
0
                }
698
0
            }
699
0
            else {
700
0
                ret = wc_Sha3_384_Update(&hmac->hash.sha3, key, length);
701
0
                if (ret != 0)
702
0
                    break;
703
0
                ret = wc_Sha3_384_Final(&hmac->hash.sha3, ip);
704
0
                if (ret != 0)
705
0
                    break;
706
707
0
                length = WC_SHA3_384_DIGEST_SIZE;
708
0
            }
709
0
            break;
710
0
    #endif
711
0
    #ifndef WOLFSSL_NOSHA3_512
712
0
        case WC_SHA3_512:
713
0
            hmac_block_size = WC_SHA3_512_BLOCK_SIZE;
714
0
            if (length <= WC_SHA3_512_BLOCK_SIZE) {
715
0
                if (key != NULL) {
716
0
                    XMEMCPY(ip, key, length);
717
0
                }
718
0
            }
719
0
            else {
720
0
                ret = wc_Sha3_512_Update(&hmac->hash.sha3, key, length);
721
0
                if (ret != 0)
722
0
                    break;
723
0
                ret = wc_Sha3_512_Final(&hmac->hash.sha3, ip);
724
0
                if (ret != 0)
725
0
                    break;
726
727
0
                length = WC_SHA3_512_DIGEST_SIZE;
728
0
            }
729
0
            break;
730
0
    #endif
731
0
    #endif /* WOLFSSL_SHA3 */
732
733
0
    #ifdef WOLFSSL_SM3
734
0
        case WC_SM3:
735
0
            hmac_block_size = WC_SM3_BLOCK_SIZE;
736
0
            if (length <= WC_SM3_BLOCK_SIZE) {
737
0
                if (key != NULL) {
738
0
                    XMEMCPY(ip, key, length);
739
0
                }
740
0
            }
741
0
            else {
742
0
                ret = wc_Sm3Update(&hmac->hash.sm3, key, length);
743
0
                if (ret != 0)
744
0
                    break;
745
0
                ret = wc_Sm3Final(&hmac->hash.sm3, ip);
746
0
                if (ret != 0)
747
0
                    break;
748
749
0
                length = WC_SM3_DIGEST_SIZE;
750
0
            }
751
0
            break;
752
0
    #endif
753
754
0
        default:
755
0
            return BAD_FUNC_ARG;
756
34.3k
    }
757
758
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
759
    if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
760
    #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
761
        #ifdef HAVE_INTEL_QA
762
        if (IntelQaHmacGetType(hmac->macType, NULL) == 0)
763
        #endif
764
        {
765
            if (length > hmac_block_size)
766
                length = hmac_block_size;
767
            /* update key length */
768
            hmac->keyLen = (word16)length;
769
770
            return ret;
771
        }
772
        /* no need to pad below */
773
    #endif
774
    }
775
#endif
776
777
34.3k
    if (ret == 0) {
778
34.3k
        word32 i;
779
780
34.3k
        if (length < hmac_block_size)
781
34.3k
            XMEMSET(ip + length, 0, hmac_block_size - length);
782
783
3.00M
        for(i = 0; i < hmac_block_size; i++) {
784
2.97M
            op[i] = (byte)(ip[i] ^ OPAD);
785
2.97M
            ip[i] ^= IPAD;
786
2.97M
        }
787
34.3k
    }
788
789
#ifdef WOLFSSL_HMAC_COPY_HASH
790
    if (ret == 0) {
791
        ret = _HmacInitIOHashes(hmac);
792
    }
793
#endif
794
795
34.3k
    return ret;
796
34.3k
#endif /* WOLFSSL_MAXQ108X */
797
34.3k
}
798
799
int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
800
71.9k
{
801
71.9k
    int allowFlag;
802
    #if defined(HAVE_FIPS)
803
        allowFlag = 0; /* default false for FIPS cases */
804
    #else
805
71.9k
        allowFlag = 1; /* default true for all non-FIPS cases */
806
71.9k
    #endif
807
71.9k
    return wc_HmacSetKey_ex(hmac, type, key, length, allowFlag);
808
71.9k
}
809
810
int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
811
146k
{
812
146k
    int ret = 0;
813
814
146k
    if (hmac == NULL || (msg == NULL && length > 0)) {
815
0
        return BAD_FUNC_ARG;
816
0
    }
817
146k
    if (length == 0) {
818
26.5k
        return 0; /* nothing to do, return success */
819
26.5k
    }
820
821
120k
#ifdef WOLF_CRYPTO_CB
822
120k
    if (hmac->devId != INVALID_DEVID) {
823
0
        ret = wc_CryptoCb_Hmac(hmac, hmac->macType, msg, length, NULL);
824
0
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
825
0
            return ret;
826
        /* fall-through when unavailable */
827
0
        ret = 0; /* reset error code */
828
0
    }
829
120k
#endif
830
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
831
    if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
832
    #if defined(HAVE_CAVIUM)
833
        return NitroxHmacUpdate(hmac, msg, length);
834
    #elif defined(HAVE_INTEL_QA)
835
        if (IntelQaHmacGetType(hmac->macType, NULL) == 0) {
836
            return IntelQaHmac(&hmac->asyncDev, hmac->macType,
837
                (byte*)hmac->ipad, hmac->keyLen, NULL, msg, length);
838
        }
839
    #endif
840
    }
841
#endif /* WOLFSSL_ASYNC_CRYPT */
842
843
120k
    if (!hmac->innerHashKeyed) {
844
48.5k
#ifndef WOLFSSL_HMAC_COPY_HASH
845
48.5k
        ret = HmacKeyHashUpdate(hmac->macType, &hmac->hash, (byte*)hmac->ipad);
846
#else
847
        ret = HmacKeyCopyHash(hmac->macType, &hmac->i_hash, &hmac->hash);
848
#endif
849
48.5k
        if (ret != 0)
850
16
            return ret;
851
48.5k
        hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_SW;
852
48.5k
    }
853
854
120k
    switch (hmac->macType) {
855
0
    #ifndef NO_MD5
856
18.9k
        case WC_MD5:
857
18.9k
            ret = wc_Md5Update(&hmac->hash.md5, msg, length);
858
18.9k
            break;
859
0
    #endif /* !NO_MD5 */
860
861
0
    #ifndef NO_SHA
862
22.4k
        case WC_SHA:
863
22.4k
            ret = wc_ShaUpdate(&hmac->hash.sha, msg, length);
864
22.4k
            break;
865
0
    #endif /* !NO_SHA */
866
867
0
    #ifdef WOLFSSL_SHA224
868
16.3k
        case WC_SHA224:
869
16.3k
            ret = wc_Sha224Update(&hmac->hash.sha224, msg, length);
870
16.3k
            break;
871
0
    #endif /* WOLFSSL_SHA224 */
872
873
0
    #ifndef NO_SHA256
874
26.6k
        case WC_SHA256:
875
26.6k
            ret = wc_Sha256Update(&hmac->hash.sha256, msg, length);
876
26.6k
            break;
877
0
    #endif /* !NO_SHA256 */
878
879
0
    #ifdef WOLFSSL_SHA384
880
15.6k
        case WC_SHA384:
881
15.6k
            ret = wc_Sha384Update(&hmac->hash.sha384, msg, length);
882
15.6k
            break;
883
0
    #endif /* WOLFSSL_SHA384 */
884
0
    #ifdef WOLFSSL_SHA512
885
20.1k
        case WC_SHA512:
886
20.1k
            ret = wc_Sha512Update(&hmac->hash.sha512, msg, length);
887
20.1k
            break;
888
0
    #endif /* WOLFSSL_SHA512 */
889
890
0
    #ifdef WOLFSSL_SHA3
891
0
    #ifndef WOLFSSL_NOSHA3_224
892
0
        case WC_SHA3_224:
893
0
            ret = wc_Sha3_224_Update(&hmac->hash.sha3, msg, length);
894
0
            break;
895
0
    #endif
896
0
    #ifndef WOLFSSL_NOSHA3_256
897
0
        case WC_SHA3_256:
898
0
            ret = wc_Sha3_256_Update(&hmac->hash.sha3, msg, length);
899
0
            break;
900
0
    #endif
901
0
    #ifndef WOLFSSL_NOSHA3_384
902
0
        case WC_SHA3_384:
903
0
            ret = wc_Sha3_384_Update(&hmac->hash.sha3, msg, length);
904
0
            break;
905
0
    #endif
906
0
    #ifndef WOLFSSL_NOSHA3_512
907
0
        case WC_SHA3_512:
908
0
            ret = wc_Sha3_512_Update(&hmac->hash.sha3, msg, length);
909
0
            break;
910
0
    #endif
911
0
    #endif /* WOLFSSL_SHA3 */
912
913
0
    #ifdef WOLFSSL_SM3
914
0
        case WC_SM3:
915
0
            ret = wc_Sm3Update(&hmac->hash.sm3, msg, length);
916
0
            break;
917
0
    #endif
918
919
0
        default:
920
0
            break;
921
120k
    }
922
923
120k
    return ret;
924
120k
}
925
926
927
int wc_HmacFinal(Hmac* hmac, byte* hash)
928
48.5k
{
929
48.5k
    int ret;
930
931
48.5k
    if (hmac == NULL || hash == NULL) {
932
0
        return BAD_FUNC_ARG;
933
0
    }
934
935
48.5k
#ifdef WOLF_CRYPTO_CB
936
48.5k
    if (hmac->devId != INVALID_DEVID) {
937
274
        ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, hash);
938
274
        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
939
0
            return ret;
940
        /* fall-through when unavailable */
941
274
    }
942
48.5k
#endif
943
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
944
    if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {
945
        int hashLen = wc_HmacSizeByType(hmac->macType);
946
        if (hashLen <= 0)
947
            return hashLen;
948
949
    #if defined(HAVE_CAVIUM)
950
        return NitroxHmacFinal(hmac, hash, hashLen);
951
    #elif defined(HAVE_INTEL_QA)
952
        if (IntelQaHmacGetType(hmac->macType, NULL) == 0) {
953
            return IntelQaHmac(&hmac->asyncDev, hmac->macType,
954
                (byte*)hmac->ipad, hmac->keyLen, hash, NULL, hashLen);
955
        }
956
    #endif
957
    }
958
#endif /* WOLFSSL_ASYNC_CRYPT */
959
960
48.5k
    if (!hmac->innerHashKeyed) {
961
14
#ifndef WOLFSSL_HMAC_COPY_HASH
962
14
        ret = HmacKeyHashUpdate(hmac->macType, &hmac->hash, (byte*)hmac->ipad);
963
#else
964
        ret = HmacKeyCopyHash(hmac->macType, &hmac->i_hash, &hmac->hash);
965
#endif
966
14
        if (ret != 0)
967
0
            return ret;
968
14
        hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_SW;
969
14
    }
970
971
48.5k
    switch (hmac->macType) {
972
0
    #ifndef NO_MD5
973
6.36k
        case WC_MD5:
974
6.36k
            ret = wc_Md5Final(&hmac->hash.md5, (byte*)hmac->innerHash);
975
6.36k
            if (ret != 0)
976
0
                break;
977
6.36k
       #ifndef WOLFSSL_HMAC_COPY_HASH
978
6.36k
            ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->opad,
979
6.36k
                                                             WC_MD5_BLOCK_SIZE);
980
       #else
981
            ret = HmacKeyCopyHash(WC_MD5, &hmac->o_hash, &hmac->hash);
982
       #endif
983
6.36k
            if (ret != 0)
984
0
                break;
985
6.36k
            ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->innerHash,
986
6.36k
                                                            WC_MD5_DIGEST_SIZE);
987
6.36k
            if (ret != 0)
988
0
                break;
989
6.36k
            ret = wc_Md5Final(&hmac->hash.md5, hash);
990
6.36k
            break;
991
0
    #endif /* !NO_MD5 */
992
993
0
    #ifndef NO_SHA
994
8.98k
        case WC_SHA:
995
8.98k
            ret = wc_ShaFinal(&hmac->hash.sha, (byte*)hmac->innerHash);
996
8.98k
            if (ret != 0)
997
0
                break;
998
8.98k
       #ifndef WOLFSSL_HMAC_COPY_HASH
999
8.98k
            ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->opad,
1000
8.98k
                                                             WC_SHA_BLOCK_SIZE);
1001
       #else
1002
            ret = HmacKeyCopyHash(WC_SHA, &hmac->o_hash, &hmac->hash);
1003
       #endif
1004
8.98k
            if (ret != 0)
1005
0
                break;
1006
8.98k
            ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->innerHash,
1007
8.98k
                                                            WC_SHA_DIGEST_SIZE);
1008
8.98k
            if (ret != 0)
1009
0
                break;
1010
8.98k
            ret = wc_ShaFinal(&hmac->hash.sha, hash);
1011
8.98k
            break;
1012
0
    #endif /* !NO_SHA */
1013
1014
0
    #ifdef WOLFSSL_SHA224
1015
6.71k
        case WC_SHA224:
1016
6.71k
            ret = wc_Sha224Final(&hmac->hash.sha224, (byte*)hmac->innerHash);
1017
6.71k
            if (ret != 0)
1018
0
                break;
1019
6.71k
       #ifndef WOLFSSL_HMAC_COPY_HASH
1020
6.71k
            ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->opad,
1021
6.71k
                                                          WC_SHA224_BLOCK_SIZE);
1022
       #else
1023
            ret = HmacKeyCopyHash(WC_SHA224, &hmac->o_hash, &hmac->hash);
1024
       #endif
1025
6.71k
            if (ret != 0)
1026
0
                break;
1027
6.71k
            ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->innerHash,
1028
6.71k
                                                         WC_SHA224_DIGEST_SIZE);
1029
6.71k
            if (ret != 0)
1030
0
                break;
1031
6.71k
            ret = wc_Sha224Final(&hmac->hash.sha224, hash);
1032
6.71k
            if (ret != 0)
1033
0
                break;
1034
6.71k
            break;
1035
6.71k
    #endif /* WOLFSSL_SHA224 */
1036
6.71k
    #ifndef NO_SHA256
1037
12.5k
        case WC_SHA256:
1038
12.5k
            ret = wc_Sha256Final(&hmac->hash.sha256, (byte*)hmac->innerHash);
1039
12.5k
            if (ret != 0)
1040
5
                break;
1041
12.5k
       #ifndef WOLFSSL_HMAC_COPY_HASH
1042
12.5k
            ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->opad,
1043
12.5k
                                                          WC_SHA256_BLOCK_SIZE);
1044
       #else
1045
            ret = HmacKeyCopyHash(WC_SHA256, &hmac->o_hash, &hmac->hash);
1046
       #endif
1047
12.5k
            if (ret != 0)
1048
11
                break;
1049
12.4k
            ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->innerHash,
1050
12.4k
                                                         WC_SHA256_DIGEST_SIZE);
1051
12.4k
            if (ret != 0)
1052
0
                break;
1053
12.4k
            ret = wc_Sha256Final(&hmac->hash.sha256, hash);
1054
12.4k
            break;
1055
0
    #endif /* !NO_SHA256 */
1056
1057
0
    #ifdef WOLFSSL_SHA384
1058
6.45k
        case WC_SHA384:
1059
6.45k
            ret = wc_Sha384Final(&hmac->hash.sha384, (byte*)hmac->innerHash);
1060
6.45k
            if (ret != 0)
1061
0
                break;
1062
6.45k
       #ifndef WOLFSSL_HMAC_COPY_HASH
1063
6.45k
            ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->opad,
1064
6.45k
                                                          WC_SHA384_BLOCK_SIZE);
1065
       #else
1066
            ret = HmacKeyCopyHash(WC_SHA384, &hmac->o_hash, &hmac->hash);
1067
       #endif
1068
6.45k
            if (ret != 0)
1069
0
                break;
1070
6.45k
            ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->innerHash,
1071
6.45k
                                                         WC_SHA384_DIGEST_SIZE);
1072
6.45k
            if (ret != 0)
1073
0
                break;
1074
6.45k
            ret = wc_Sha384Final(&hmac->hash.sha384, hash);
1075
6.45k
            break;
1076
0
    #endif /* WOLFSSL_SHA384 */
1077
0
    #ifdef WOLFSSL_SHA512
1078
7.51k
        case WC_SHA512:
1079
7.51k
            ret = wc_Sha512Final(&hmac->hash.sha512, (byte*)hmac->innerHash);
1080
7.51k
            if (ret != 0)
1081
0
                break;
1082
7.51k
       #ifndef WOLFSSL_HMAC_COPY_HASH
1083
7.51k
            ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->opad,
1084
7.51k
                                                          WC_SHA512_BLOCK_SIZE);
1085
       #else
1086
            ret = HmacKeyCopyHash(WC_SHA512, &hmac->o_hash, &hmac->hash);
1087
       #endif
1088
7.51k
            if (ret != 0)
1089
0
                break;
1090
7.51k
            ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->innerHash,
1091
7.51k
                                                         WC_SHA512_DIGEST_SIZE);
1092
7.51k
            if (ret != 0)
1093
0
                break;
1094
7.51k
            ret = wc_Sha512Final(&hmac->hash.sha512, hash);
1095
7.51k
            break;
1096
0
    #endif /* WOLFSSL_SHA512 */
1097
1098
0
    #ifdef WOLFSSL_SHA3
1099
0
    #ifndef WOLFSSL_NOSHA3_224
1100
0
        case WC_SHA3_224:
1101
0
            ret = wc_Sha3_224_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
1102
0
            if (ret != 0)
1103
0
                break;
1104
0
       #ifndef WOLFSSL_HMAC_COPY_HASH
1105
0
            ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->opad,
1106
0
                                                        WC_SHA3_224_BLOCK_SIZE);
1107
       #else
1108
            ret = HmacKeyCopyHash(WC_SHA3_224, &hmac->o_hash, &hmac->hash);
1109
       #endif
1110
0
            if (ret != 0)
1111
0
                break;
1112
0
            ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
1113
0
                                                       WC_SHA3_224_DIGEST_SIZE);
1114
0
            if (ret != 0)
1115
0
                break;
1116
0
            ret = wc_Sha3_224_Final(&hmac->hash.sha3, hash);
1117
0
            break;
1118
0
    #endif
1119
0
    #ifndef WOLFSSL_NOSHA3_256
1120
0
        case WC_SHA3_256:
1121
0
            ret = wc_Sha3_256_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
1122
0
            if (ret != 0)
1123
0
                break;
1124
0
       #ifndef WOLFSSL_HMAC_COPY_HASH
1125
0
            ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->opad,
1126
0
                                                        WC_SHA3_256_BLOCK_SIZE);
1127
       #else
1128
            ret = HmacKeyCopyHash(WC_SHA3_256, &hmac->o_hash, &hmac->hash);
1129
       #endif
1130
0
            if (ret != 0)
1131
0
                break;
1132
0
            ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
1133
0
                                                       WC_SHA3_256_DIGEST_SIZE);
1134
0
            if (ret != 0)
1135
0
                break;
1136
0
            ret = wc_Sha3_256_Final(&hmac->hash.sha3, hash);
1137
0
            break;
1138
0
    #endif
1139
0
    #ifndef WOLFSSL_NOSHA3_384
1140
0
        case WC_SHA3_384:
1141
0
            ret = wc_Sha3_384_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
1142
0
            if (ret != 0)
1143
0
                break;
1144
0
       #ifndef WOLFSSL_HMAC_COPY_HASH
1145
0
            ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->opad,
1146
0
                                                        WC_SHA3_384_BLOCK_SIZE);
1147
       #else
1148
            ret = HmacKeyCopyHash(WC_SHA3_384, &hmac->o_hash, &hmac->hash);
1149
       #endif
1150
0
            if (ret != 0)
1151
0
                break;
1152
0
            ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
1153
0
                                                       WC_SHA3_384_DIGEST_SIZE);
1154
0
            if (ret != 0)
1155
0
                break;
1156
0
            ret = wc_Sha3_384_Final(&hmac->hash.sha3, hash);
1157
0
            break;
1158
0
    #endif
1159
0
    #ifndef WOLFSSL_NOSHA3_512
1160
0
        case WC_SHA3_512:
1161
0
            ret = wc_Sha3_512_Final(&hmac->hash.sha3, (byte*)hmac->innerHash);
1162
0
            if (ret != 0)
1163
0
                break;
1164
0
       #ifndef WOLFSSL_HMAC_COPY_HASH
1165
0
            ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->opad,
1166
0
                                                        WC_SHA3_512_BLOCK_SIZE);
1167
       #else
1168
            ret = HmacKeyCopyHash(WC_SHA3_512, &hmac->o_hash, &hmac->hash);
1169
       #endif
1170
0
            if (ret != 0)
1171
0
                break;
1172
0
            ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->innerHash,
1173
0
                                                       WC_SHA3_512_DIGEST_SIZE);
1174
0
            if (ret != 0)
1175
0
                break;
1176
0
            ret = wc_Sha3_512_Final(&hmac->hash.sha3, hash);
1177
0
            break;
1178
0
    #endif
1179
0
    #endif /* WOLFSSL_SHA3 */
1180
1181
0
    #ifdef WOLFSSL_SM3
1182
0
        case WC_SM3:
1183
0
            ret = wc_Sm3Final(&hmac->hash.sm3, (byte*)hmac->innerHash);
1184
0
            if (ret != 0)
1185
0
                break;
1186
0
       #ifndef WOLFSSL_HMAC_COPY_HASH
1187
0
            ret = wc_Sm3Update(&hmac->hash.sm3, (byte*)hmac->opad,
1188
0
                                                             WC_SM3_BLOCK_SIZE);
1189
       #else
1190
            ret = HmacKeyCopyHash(WC_SM3, &hmac->o_hash, &hmac->hash);
1191
       #endif
1192
0
            if (ret != 0)
1193
0
                break;
1194
0
            ret = wc_Sm3Update(&hmac->hash.sm3, (byte*)hmac->innerHash,
1195
0
                                                            WC_SM3_DIGEST_SIZE);
1196
0
            if (ret != 0)
1197
0
                break;
1198
0
            ret = wc_Sm3Final(&hmac->hash.sm3, hash);
1199
0
            break;
1200
0
    #endif
1201
1202
0
        default:
1203
0
            ret = BAD_FUNC_ARG;
1204
0
            break;
1205
48.5k
    }
1206
1207
48.5k
    if (ret == 0) {
1208
48.5k
        hmac->innerHashKeyed = 0;
1209
48.5k
    }
1210
1211
48.5k
    return ret;
1212
48.5k
}
1213
1214
#ifdef WOLFSSL_KCAPI_HMAC
1215
    /* implemented in wolfcrypt/src/port/kcapi/kcapi_hmac.c */
1216
1217
    /* unmap the _Software calls used by kcapi_hmac.c */
1218
    #undef wc_HmacSetKey
1219
    #undef wc_HmacUpdate
1220
    #undef wc_HmacFinal
1221
1222
#else
1223
/* Initialize Hmac for use with async device */
1224
int wc_HmacInit(Hmac* hmac, void* heap, int devId)
1225
110k
{
1226
110k
    int ret = 0;
1227
1228
110k
    if (hmac == NULL)
1229
0
        return BAD_FUNC_ARG;
1230
1231
110k
    XMEMSET(hmac, 0, sizeof(Hmac));
1232
110k
    hmac->macType = WC_HASH_TYPE_NONE;
1233
110k
    hmac->heap = heap;
1234
110k
#ifdef WOLF_CRYPTO_CB
1235
110k
    hmac->devId = devId;
1236
110k
    hmac->devCtx = NULL;
1237
110k
#endif
1238
#if defined(WOLFSSL_DEVCRYPTO_HMAC)
1239
    hmac->ctx.cfd = -1;
1240
#endif
1241
1242
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
1243
    ret = wolfAsync_DevCtxInit(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC,
1244
                                                         hmac->heap, devId);
1245
#else
1246
110k
    (void)devId;
1247
110k
#endif /* WOLFSSL_ASYNC_CRYPT */
1248
1249
110k
    return ret;
1250
110k
}
1251
1252
#ifdef WOLF_PRIVATE_KEY_ID
1253
int  wc_HmacInit_Id(Hmac* hmac, unsigned char* id, int len, void* heap,
1254
                    int devId)
1255
0
{
1256
0
    int ret = 0;
1257
1258
0
    if (hmac == NULL)
1259
0
        ret = BAD_FUNC_ARG;
1260
0
    if (ret == 0 && (len < 0 || len > HMAC_MAX_ID_LEN))
1261
0
        ret = BUFFER_E;
1262
1263
0
    if (ret == 0)
1264
0
        ret = wc_HmacInit(hmac, heap, devId);
1265
0
    if (ret == 0) {
1266
0
        XMEMCPY(hmac->id, id, (size_t)len);
1267
0
        hmac->idLen = len;
1268
0
    }
1269
1270
0
    return ret;
1271
0
}
1272
1273
int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap, int devId)
1274
0
{
1275
0
    int ret = 0;
1276
0
    int labelLen = 0;
1277
1278
0
    if (hmac == NULL || label == NULL)
1279
0
        ret = BAD_FUNC_ARG;
1280
0
    if (ret == 0) {
1281
0
        labelLen = (int)XSTRLEN(label);
1282
0
        if (labelLen == 0 || labelLen > HMAC_MAX_LABEL_LEN)
1283
0
            ret = BUFFER_E;
1284
0
    }
1285
1286
0
    if (ret == 0)
1287
0
        ret  = wc_HmacInit(hmac, heap, devId);
1288
0
    if (ret == 0) {
1289
0
        XMEMCPY(hmac->label, label, (size_t)labelLen);
1290
0
        hmac->labelLen = labelLen;
1291
0
    }
1292
1293
0
    return ret;
1294
0
}
1295
#endif /* WOLF_PRIVATE_KEY_ID */
1296
1297
/* Free Hmac from use with async device */
1298
void wc_HmacFree(Hmac* hmac)
1299
34.8k
{
1300
34.8k
    if (hmac == NULL)
1301
0
        return;
1302
1303
34.8k
#ifdef WOLF_CRYPTO_CB
1304
    /* handle cleanup case where final is not called */
1305
34.8k
    if (hmac->devId != INVALID_DEVID && hmac->devCtx != NULL) {
1306
0
        int  ret;
1307
0
        byte finalHash[WC_HMAC_BLOCK_SIZE];
1308
0
        ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, finalHash);
1309
0
        (void)ret; /* must ignore return code here */
1310
0
        (void)finalHash;
1311
0
    }
1312
34.8k
#endif
1313
1314
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
1315
    wolfAsync_DevCtxFree(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC);
1316
#endif /* WOLFSSL_ASYNC_CRYPT */
1317
1318
34.8k
    switch (hmac->macType) {
1319
0
    #ifndef NO_MD5
1320
2.03k
        case WC_MD5:
1321
2.03k
            wc_Md5Free(&hmac->hash.md5);
1322
        #ifdef WOLFSSL_HMAC_COPY_HASH
1323
            wc_Md5Free(&hmac->i_hash.md5);
1324
            wc_Md5Free(&hmac->o_hash.md5);
1325
        #endif
1326
2.03k
            break;
1327
0
    #endif /* !NO_MD5 */
1328
1329
0
    #ifndef NO_SHA
1330
6.20k
        case WC_SHA:
1331
6.20k
            wc_ShaFree(&hmac->hash.sha);
1332
        #ifdef WOLFSSL_HMAC_COPY_HASH
1333
            wc_ShaFree(&hmac->i_hash.sha);
1334
            wc_ShaFree(&hmac->o_hash.sha);
1335
        #endif
1336
6.20k
            break;
1337
0
    #endif /* !NO_SHA */
1338
1339
0
    #ifdef WOLFSSL_SHA224
1340
5.94k
        case WC_SHA224:
1341
5.94k
            wc_Sha224Free(&hmac->hash.sha224);
1342
        #ifdef WOLFSSL_HMAC_COPY_HASH
1343
            wc_Sha224Free(&hmac->i_hash.sha224);
1344
            wc_Sha224Free(&hmac->o_hash.sha224);
1345
        #endif
1346
5.94k
            break;
1347
0
    #endif /* WOLFSSL_SHA224 */
1348
0
    #ifndef NO_SHA256
1349
8.31k
        case WC_SHA256:
1350
8.31k
            wc_Sha256Free(&hmac->hash.sha256);
1351
        #ifdef WOLFSSL_HMAC_COPY_HASH
1352
            wc_Sha256Free(&hmac->i_hash.sha256);
1353
            wc_Sha256Free(&hmac->o_hash.sha256);
1354
        #endif
1355
8.31k
            break;
1356
0
    #endif /* !NO_SHA256 */
1357
1358
0
    #ifdef WOLFSSL_SHA384
1359
4.97k
        case WC_SHA384:
1360
4.97k
            wc_Sha384Free(&hmac->hash.sha384);
1361
        #ifdef WOLFSSL_HMAC_COPY_HASH
1362
            wc_Sha384Free(&hmac->i_hash.sha384);
1363
            wc_Sha384Free(&hmac->o_hash.sha384);
1364
        #endif
1365
4.97k
            break;
1366
0
    #endif /* WOLFSSL_SHA384 */
1367
0
    #ifdef WOLFSSL_SHA512
1368
7.19k
        case WC_SHA512:
1369
7.19k
            wc_Sha512Free(&hmac->hash.sha512);
1370
        #ifdef WOLFSSL_HMAC_COPY_HASH
1371
            wc_Sha512Free(&hmac->i_hash.sha512);
1372
            wc_Sha512Free(&hmac->o_hash.sha512);
1373
        #endif
1374
7.19k
            break;
1375
0
    #endif /* WOLFSSL_SHA512 */
1376
1377
0
    #ifdef WOLFSSL_SHA3
1378
0
    #ifndef WOLFSSL_NOSHA3_224
1379
0
        case WC_SHA3_224:
1380
0
            wc_Sha3_224_Free(&hmac->hash.sha3);
1381
        #ifdef WOLFSSL_HMAC_COPY_HASH
1382
            wc_Sha3_224_Free(&hmac->i_hash.sha3);
1383
            wc_Sha3_224_Free(&hmac->o_hash.sha3);
1384
        #endif
1385
0
            break;
1386
0
    #endif
1387
0
    #ifndef WOLFSSL_NOSHA3_256
1388
0
        case WC_SHA3_256:
1389
0
            wc_Sha3_256_Free(&hmac->hash.sha3);
1390
        #ifdef WOLFSSL_HMAC_COPY_HASH
1391
            wc_Sha3_256_Free(&hmac->i_hash.sha3);
1392
            wc_Sha3_256_Free(&hmac->o_hash.sha3);
1393
        #endif
1394
0
            break;
1395
0
    #endif
1396
0
    #ifndef WOLFSSL_NOSHA3_384
1397
0
        case WC_SHA3_384:
1398
0
            wc_Sha3_384_Free(&hmac->hash.sha3);
1399
        #ifdef WOLFSSL_HMAC_COPY_HASH
1400
            wc_Sha3_384_Free(&hmac->i_hash.sha3);
1401
            wc_Sha3_384_Free(&hmac->o_hash.sha3);
1402
        #endif
1403
0
            break;
1404
0
    #endif
1405
0
    #ifndef WOLFSSL_NOSHA3_512
1406
0
        case WC_SHA3_512:
1407
0
            wc_Sha3_512_Free(&hmac->hash.sha3);
1408
        #ifdef WOLFSSL_HMAC_COPY_HASH
1409
            wc_Sha3_512_Free(&hmac->i_hash.sha3);
1410
            wc_Sha3_512_Free(&hmac->o_hash.sha3);
1411
        #endif
1412
0
            break;
1413
0
    #endif
1414
0
    #endif /* WOLFSSL_SHA3 */
1415
1416
0
    #ifdef WOLFSSL_SM3
1417
0
        case WC_SM3:
1418
0
            wc_Sm3Free(&hmac->hash.sm3);
1419
        #ifdef WOLFSSL_HMAC_COPY_HASH
1420
            wc_Sm3Free(&hmac->i_hash.sm3);
1421
            wc_Sm3Free(&hmac->o_hash.sm3);
1422
        #endif
1423
0
            break;
1424
0
    #endif
1425
1426
230
        default:
1427
230
            break;
1428
34.8k
    }
1429
1430
34.8k
    ForceZero(hmac, sizeof(*hmac));
1431
34.8k
}
1432
#endif /* WOLFSSL_KCAPI_HMAC */
1433
1434
int wolfSSL_GetHmacMaxSize(void)
1435
0
{
1436
0
    return WC_MAX_DIGEST_SIZE;
1437
0
}
1438
1439
#ifdef HAVE_HKDF
1440
    /* HMAC-KDF-Extract.
1441
     * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
1442
     *
1443
     * type     The hash algorithm type.
1444
     * salt     The optional salt value.
1445
     * saltSz   The size of the salt.
1446
     * inKey    The input keying material.
1447
     * inKeySz  The size of the input keying material.
1448
     * out      The pseudorandom key with the length that of the hash.
1449
     * returns 0 on success, otherwise failure.
1450
     */
1451
    int wc_HKDF_Extract_ex(int type, const byte* salt, word32 saltSz,
1452
        const byte* inKey, word32 inKeySz, byte* out, void* heap, int devId)
1453
5.82k
    {
1454
5.82k
        byte   tmp[WC_MAX_DIGEST_SIZE]; /* localSalt helper */
1455
5.82k
        WC_DECLARE_VAR(myHmac, Hmac, 1, 0);
1456
5.82k
        int    ret;
1457
5.82k
        const  byte* localSalt;  /* either points to user input or tmp */
1458
5.82k
        word32 hashSz;
1459
1460
5.82k
        ret = wc_HmacSizeByType(type);
1461
5.82k
        if (ret < 0) {
1462
0
            return ret;
1463
0
        }
1464
1465
5.82k
        WC_ALLOC_VAR_EX(myHmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC,
1466
5.82k
            return MEMORY_E);
1467
1468
5.80k
        hashSz = (word32)ret;
1469
5.80k
        localSalt = salt;
1470
5.80k
        if (localSalt == NULL) {
1471
4.40k
            XMEMSET(tmp, 0, hashSz);
1472
4.40k
            localSalt = tmp;
1473
4.40k
            saltSz    = hashSz;
1474
4.40k
        }
1475
1476
5.80k
        ret = wc_HmacInit(myHmac, heap, devId);
1477
5.80k
        if (ret == 0) {
1478
        #if FIPS_VERSION3_GE(6,0,0)
1479
            ret = wc_HmacSetKey_ex(myHmac, type, localSalt, saltSz,
1480
                                   FIPS_ALLOW_SHORT);
1481
        #else
1482
5.80k
            ret = wc_HmacSetKey(myHmac, type, localSalt, saltSz);
1483
5.80k
        #endif
1484
5.80k
            if (ret == 0)
1485
5.80k
                ret = wc_HmacUpdate(myHmac, inKey, inKeySz);
1486
5.80k
            if (ret == 0)
1487
5.79k
                ret = wc_HmacFinal(myHmac,  out);
1488
5.80k
            wc_HmacFree(myHmac);
1489
5.80k
        }
1490
5.80k
        WC_FREE_VAR_EX(myHmac, NULL, DYNAMIC_TYPE_HMAC);
1491
1492
5.80k
        return ret;
1493
5.82k
    }
1494
1495
    int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
1496
                        const byte* inKey, word32 inKeySz, byte* out)
1497
0
    {
1498
0
        return wc_HKDF_Extract_ex(type, salt, saltSz, inKey, inKeySz, out, NULL,
1499
0
            INVALID_DEVID);
1500
0
    }
1501
1502
    /* HMAC-KDF-Expand.
1503
     * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
1504
     *
1505
     * type     The hash algorithm type.
1506
     * inKey    The input key.
1507
     * inKeySz  The size of the input key.
1508
     * info     The application specific information.
1509
     * infoSz   The size of the application specific information.
1510
     * out      The output keying material.
1511
     * returns 0 on success, otherwise failure.
1512
     */
1513
    int wc_HKDF_Expand_ex(int type, const byte* inKey, word32 inKeySz,
1514
                       const byte* info, word32 infoSz, byte* out, word32 outSz,
1515
                       void* heap, int devId)
1516
13.7k
    {
1517
13.7k
        byte   tmp[WC_MAX_DIGEST_SIZE];
1518
13.7k
        WC_DECLARE_VAR(myHmac, Hmac, 1, 0);
1519
13.7k
        int    ret = 0;
1520
13.7k
        word32 outIdx = 0;
1521
13.7k
        word32 hashSz;
1522
13.7k
        byte   n = 0x1;
1523
1524
13.7k
        ret = wc_HmacSizeByType(type);
1525
13.7k
        if (ret < 0) {
1526
0
            return ret;
1527
0
        }
1528
13.7k
        hashSz = (word32)ret;
1529
1530
        /* RFC 5869 states that the length of output keying material in
1531
         * octets must be L <= 255*HashLen or N = ceil(L/HashLen) */
1532
1533
13.7k
        if (out == NULL || ((outSz/hashSz) + ((outSz % hashSz) != 0)) > 255) {
1534
197
            return BAD_FUNC_ARG;
1535
197
        }
1536
1537
13.5k
        WC_ALLOC_VAR_EX(myHmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC,
1538
13.5k
            return MEMORY_E);
1539
1540
13.5k
        ret = wc_HmacInit(myHmac, heap, devId);
1541
13.5k
        if (ret != 0) {
1542
0
        WC_FREE_VAR_EX(myHmac, NULL, DYNAMIC_TYPE_HMAC);
1543
0
            return ret;
1544
0
        }
1545
1546
13.5k
        XMEMSET(tmp, 0, WC_MAX_DIGEST_SIZE);
1547
1548
58.2k
        while (outIdx < outSz) {
1549
44.7k
            word32 tmpSz = (n == 1) ? 0 : hashSz;
1550
44.7k
            word32 left = outSz - outIdx;
1551
1552
        #if FIPS_VERSION3_GE(6,0,0)
1553
            ret = wc_HmacSetKey_ex(myHmac, type, inKey, inKeySz,
1554
                                   FIPS_ALLOW_SHORT);
1555
        #else
1556
44.7k
            ret = wc_HmacSetKey(myHmac, type, inKey, inKeySz);
1557
44.7k
        #endif
1558
44.7k
            if (ret != 0)
1559
0
                break;
1560
44.7k
            ret = wc_HmacUpdate(myHmac, tmp, tmpSz);
1561
44.7k
            if (ret != 0)
1562
2
                break;
1563
44.7k
            ret = wc_HmacUpdate(myHmac, info, infoSz);
1564
44.7k
            if (ret != 0)
1565
11
                break;
1566
44.7k
            ret = wc_HmacUpdate(myHmac, &n, 1);
1567
44.7k
            if (ret != 0)
1568
6
                break;
1569
44.7k
            ret = wc_HmacFinal(myHmac, tmp);
1570
44.7k
            if (ret != 0)
1571
44
                break;
1572
1573
44.7k
            left = min(left, hashSz);
1574
44.7k
            XMEMCPY(out+outIdx, tmp, left);
1575
1576
44.7k
            outIdx += left;
1577
44.7k
            n++;
1578
44.7k
        }
1579
1580
13.5k
        wc_HmacFree(myHmac);
1581
13.5k
        WC_FREE_VAR_EX(myHmac, NULL, DYNAMIC_TYPE_HMAC);
1582
1583
13.5k
        return ret;
1584
13.5k
    }
1585
1586
    int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
1587
                       const byte* info, word32 infoSz, byte* out, word32 outSz)
1588
0
    {
1589
0
        return wc_HKDF_Expand_ex(type, inKey, inKeySz, info, infoSz, out, outSz,
1590
0
            NULL, INVALID_DEVID);
1591
0
    }
1592
1593
    /* HMAC-KDF.
1594
     * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF).
1595
     *
1596
     * type     The hash algorithm type.
1597
     * inKey    The input keying material.
1598
     * inKeySz  The size of the input keying material.
1599
     * salt     The optional salt value.
1600
     * saltSz   The size of the salt.
1601
     * info     The application specific information.
1602
     * infoSz   The size of the application specific information.
1603
     * out      The output keying material.
1604
     * returns 0 on success, otherwise failure.
1605
     */
1606
    int wc_HKDF_ex(int type, const byte* inKey, word32 inKeySz,
1607
                   const byte* salt, word32 saltSz, const byte* info,
1608
                   word32 infoSz, byte* out, word32 outSz, void* heap,
1609
                   int devId)
1610
1.03k
    {
1611
1.03k
        byte   prk[WC_MAX_DIGEST_SIZE];
1612
1.03k
        word32 hashSz;
1613
1.03k
        int    ret;
1614
1615
1.03k
        (void)devId; /* suppress unused parameter warning */
1616
1617
1.03k
#ifdef WOLF_CRYPTO_CB
1618
        /* Try crypto callback first for complete operation */
1619
1.03k
        if (devId != INVALID_DEVID) {
1620
0
             ret = wc_CryptoCb_Hkdf(type, inKey, inKeySz, salt, saltSz, info,
1621
0
                                   infoSz, out, outSz, devId);
1622
0
            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1623
0
                return ret;
1624
0
        }
1625
1.03k
#endif
1626
1627
1.03k
        ret = wc_HmacSizeByType(type);
1628
1.03k
        if (ret < 0) {
1629
18
            return ret;
1630
18
        }
1631
1.01k
        hashSz = (word32)ret;
1632
1633
1.01k
        ret = wc_HKDF_Extract_ex(type, salt, saltSz, inKey, inKeySz, prk, heap,
1634
1.01k
                                 devId);
1635
1.01k
        if (ret != 0)
1636
15
            return ret;
1637
1638
1.00k
        return wc_HKDF_Expand_ex(type, prk, hashSz, info, infoSz, out, outSz,
1639
1.00k
                                 heap, devId);
1640
1.01k
    }
1641
1642
    int wc_HKDF(int type, const byte* inKey, word32 inKeySz, const byte* salt,
1643
                word32 saltSz, const byte* info, word32 infoSz, byte* out,
1644
                word32 outSz)
1645
1.03k
    {
1646
1.03k
        return wc_HKDF_ex(type, inKey, inKeySz, salt, saltSz, info, infoSz, out,
1647
1.03k
                          outSz, NULL, INVALID_DEVID);
1648
1.03k
    }
1649
1650
#endif /* HAVE_HKDF */
1651
1652
#endif /* NO_HMAC */