Coverage Report

Created: 2025-12-31 07:08

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