/src/wolfssl/wolfcrypt/src/hmac.c
Line | Count | Source |
1 | | /* hmac.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 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 | | /* |
24 | | * HMAC Build Options: |
25 | | * |
26 | | * NO_HMAC: Disable HMAC support entirely default: off |
27 | | * HAVE_HKDF: Enable HKDF (RFC 5869) key derivation default: off |
28 | | * WOLFSSL_HMAC_COPY_HASH: Copy hash state instead of re-init default: off |
29 | | * for HMAC operations (performance) |
30 | | * STM32_HMAC: STM32 hardware HMAC acceleration default: off |
31 | | * |
32 | | * Hardware Acceleration (HMAC-specific): |
33 | | * WC_ASYNC_ENABLE_HMAC: Enable async HMAC operations default: off |
34 | | * WOLFSSL_DEVCRYPTO_HMAC: /dev/crypto HMAC acceleration default: off |
35 | | * WOLFSSL_KCAPI_HMAC: Linux kernel crypto API for HMAC default: off |
36 | | */ |
37 | | |
38 | | #define WC_FIPS_LL_CRYPTO |
39 | | #define _WC_BUILDING_HMAC_C |
40 | | |
41 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
42 | | |
43 | | #ifndef NO_HMAC |
44 | | |
45 | | #if FIPS_VERSION3_GE(2,0,0) |
46 | | #if defined(USE_WINDOWS_API) && defined(_MSC_VER) |
47 | | #pragma code_seg(".fipsA$g") |
48 | | #pragma const_seg(".fipsB$g") |
49 | | #endif |
50 | | #endif |
51 | | |
52 | | #include <wolfssl/wolfcrypt/hmac.h> |
53 | | |
54 | | #ifdef WOLF_CRYPTO_CB |
55 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
56 | | |
57 | | /* Is this HMAC a cryptocb candidate? WOLF_CRYPTO_CB_FIND can map any id |
58 | | * (INVALID_DEVID included) to a device, so every HMAC qualifies there. */ |
59 | | #ifdef WOLF_CRYPTO_CB_FIND |
60 | | #define WC_HMAC_USE_CRYPTOCB(hmac) 1 |
61 | | #else |
62 | | #define WC_HMAC_USE_CRYPTOCB(hmac) ((hmac)->devId != INVALID_DEVID) |
63 | | #endif |
64 | | #endif |
65 | | |
66 | | #ifdef NO_INLINE |
67 | | #include <wolfssl/wolfcrypt/misc.h> |
68 | | #else |
69 | | #define WOLFSSL_MISC_INCLUDED |
70 | | #include <wolfcrypt/src/misc.c> |
71 | | #endif |
72 | | |
73 | | #ifdef WOLFSSL_KCAPI_HMAC |
74 | | #include <wolfssl/wolfcrypt/port/kcapi/kcapi_hmac.h> |
75 | | |
76 | | /* map the _Software calls used by kcapi_hmac.c */ |
77 | | #define wc_HmacSetKey wc_HmacSetKey_Software |
78 | | #define wc_HmacSetKey_ex wc_HmacSetKey_ex_Software |
79 | | #define wc_HmacUpdate wc_HmacUpdate_Software |
80 | | #define wc_HmacFinal wc_HmacFinal_Software |
81 | | #endif |
82 | | |
83 | | #if FIPS_VERSION3_GE(6,0,0) |
84 | | const unsigned int wolfCrypt_FIPS_hmac_ro_sanity[2] = |
85 | | { 0x1a2b3c4d, 0x00000008 }; |
86 | | int wolfCrypt_FIPS_HMAC_sanity(void) |
87 | | { |
88 | | return 0; |
89 | | } |
90 | | #endif |
91 | | |
92 | | int wc_HmacSizeByType(int type) |
93 | 0 | { |
94 | 0 | int ret; |
95 | |
|
96 | 0 | if (!(type == WC_MD5 || type == WC_SHA || |
97 | | #ifdef WOLFSSL_SM3 |
98 | | type == WC_SM3 || |
99 | | #endif |
100 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
101 | 0 | type == WC_SHA512_224 || |
102 | 0 | #endif |
103 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
104 | 0 | type == WC_SHA512_256 || |
105 | 0 | #endif |
106 | 0 | type == WC_SHA224 || type == WC_SHA256 || |
107 | 0 | type == WC_SHA384 || type == WC_SHA512 || |
108 | 0 | type == WC_SHA3_224 || type == WC_SHA3_256 || |
109 | 0 | type == WC_SHA3_384 || type == WC_SHA3_512)) { |
110 | 0 | return BAD_FUNC_ARG; |
111 | 0 | } |
112 | | |
113 | 0 | switch (type) { |
114 | | #ifndef NO_MD5 |
115 | | case WC_MD5: |
116 | | ret = WC_MD5_DIGEST_SIZE; |
117 | | break; |
118 | | #endif /* !NO_MD5 */ |
119 | | |
120 | 0 | #ifndef NO_SHA |
121 | 0 | case WC_SHA: |
122 | 0 | ret = WC_SHA_DIGEST_SIZE; |
123 | 0 | break; |
124 | 0 | #endif /* !NO_SHA */ |
125 | | |
126 | 0 | #ifdef WOLFSSL_SHA224 |
127 | 0 | case WC_SHA224: |
128 | 0 | ret = WC_SHA224_DIGEST_SIZE; |
129 | 0 | break; |
130 | 0 | #endif /* WOLFSSL_SHA224 */ |
131 | | |
132 | 0 | #ifndef NO_SHA256 |
133 | 0 | case WC_SHA256: |
134 | 0 | ret = WC_SHA256_DIGEST_SIZE; |
135 | 0 | break; |
136 | 0 | #endif /* !NO_SHA256 */ |
137 | | |
138 | 0 | #ifdef WOLFSSL_SHA384 |
139 | 0 | case WC_SHA384: |
140 | 0 | ret = WC_SHA384_DIGEST_SIZE; |
141 | 0 | break; |
142 | 0 | #endif /* WOLFSSL_SHA384 */ |
143 | 0 | #ifdef WOLFSSL_SHA512 |
144 | 0 | case WC_SHA512: |
145 | 0 | ret = WC_SHA512_DIGEST_SIZE; |
146 | 0 | break; |
147 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
148 | 0 | case WC_SHA512_224: |
149 | 0 | ret = WC_SHA512_224_DIGEST_SIZE; |
150 | 0 | break; |
151 | 0 | #endif |
152 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
153 | 0 | case WC_SHA512_256: |
154 | 0 | ret = WC_SHA512_256_DIGEST_SIZE; |
155 | 0 | break; |
156 | 0 | #endif |
157 | 0 | #endif /* WOLFSSL_SHA512 */ |
158 | | |
159 | 0 | #ifdef WOLFSSL_SHA3 |
160 | 0 | case WC_SHA3_224: |
161 | 0 | ret = WC_SHA3_224_DIGEST_SIZE; |
162 | 0 | break; |
163 | | |
164 | 0 | case WC_SHA3_256: |
165 | 0 | ret = WC_SHA3_256_DIGEST_SIZE; |
166 | 0 | break; |
167 | | |
168 | 0 | case WC_SHA3_384: |
169 | 0 | ret = WC_SHA3_384_DIGEST_SIZE; |
170 | 0 | break; |
171 | | |
172 | 0 | case WC_SHA3_512: |
173 | 0 | ret = WC_SHA3_512_DIGEST_SIZE; |
174 | 0 | break; |
175 | 0 | #endif /* WOLFSSL_SHA3 */ |
176 | | |
177 | | #ifdef WOLFSSL_SM3 |
178 | | case WC_SM3: |
179 | | ret = WC_SM3_DIGEST_SIZE; |
180 | | break; |
181 | | #endif |
182 | | |
183 | 0 | default: |
184 | 0 | ret = BAD_FUNC_ARG; |
185 | 0 | break; |
186 | 0 | } |
187 | | |
188 | 0 | return ret; |
189 | 0 | } |
190 | | |
191 | | static int HmacKeyInitHash(wc_HmacHash* hash, int type, void* heap, int devId) |
192 | 0 | { |
193 | 0 | int ret = 0; |
194 | |
|
195 | 0 | switch (type) { |
196 | | #ifndef NO_MD5 |
197 | | case WC_MD5: |
198 | | ret = wc_InitMd5_ex(&hash->md5, heap, devId); |
199 | | break; |
200 | | #endif /* !NO_MD5 */ |
201 | | |
202 | 0 | #ifndef NO_SHA |
203 | 0 | case WC_SHA: |
204 | 0 | ret = wc_InitSha_ex(&hash->sha, heap, devId); |
205 | 0 | break; |
206 | 0 | #endif /* !NO_SHA */ |
207 | | |
208 | 0 | #ifdef WOLFSSL_SHA224 |
209 | 0 | case WC_SHA224: |
210 | 0 | ret = wc_InitSha224_ex(&hash->sha224, heap, devId); |
211 | 0 | break; |
212 | 0 | #endif /* WOLFSSL_SHA224 */ |
213 | | |
214 | 0 | #ifndef NO_SHA256 |
215 | 0 | case WC_SHA256: |
216 | 0 | ret = wc_InitSha256_ex(&hash->sha256, heap, devId); |
217 | 0 | break; |
218 | 0 | #endif /* !NO_SHA256 */ |
219 | | |
220 | 0 | #ifdef WOLFSSL_SHA384 |
221 | 0 | case WC_SHA384: |
222 | 0 | ret = wc_InitSha384_ex(&hash->sha384, heap, devId); |
223 | 0 | break; |
224 | 0 | #endif /* WOLFSSL_SHA384 */ |
225 | 0 | #ifdef WOLFSSL_SHA512 |
226 | 0 | case WC_SHA512: |
227 | 0 | ret = wc_InitSha512_ex(&hash->sha512, heap, devId); |
228 | 0 | break; |
229 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
230 | 0 | case WC_SHA512_224: |
231 | 0 | ret = wc_InitSha512_224_ex(&hash->sha512, heap, devId); |
232 | 0 | break; |
233 | 0 | #endif |
234 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
235 | 0 | case WC_SHA512_256: |
236 | 0 | ret = wc_InitSha512_256_ex(&hash->sha512, heap, devId); |
237 | 0 | break; |
238 | 0 | #endif |
239 | 0 | #endif /* WOLFSSL_SHA512 */ |
240 | | |
241 | 0 | #ifdef WOLFSSL_SHA3 |
242 | 0 | #ifndef WOLFSSL_NOSHA3_224 |
243 | 0 | case WC_SHA3_224: |
244 | 0 | ret = wc_InitSha3_224(&hash->sha3, heap, devId); |
245 | 0 | break; |
246 | 0 | #endif |
247 | 0 | #ifndef WOLFSSL_NOSHA3_256 |
248 | 0 | case WC_SHA3_256: |
249 | 0 | ret = wc_InitSha3_256(&hash->sha3, heap, devId); |
250 | 0 | break; |
251 | 0 | #endif |
252 | 0 | #ifndef WOLFSSL_NOSHA3_384 |
253 | 0 | case WC_SHA3_384: |
254 | 0 | ret = wc_InitSha3_384(&hash->sha3, heap, devId); |
255 | 0 | break; |
256 | 0 | #endif |
257 | 0 | #ifndef WOLFSSL_NOSHA3_512 |
258 | 0 | case WC_SHA3_512: |
259 | 0 | ret = wc_InitSha3_512(&hash->sha3, heap, devId); |
260 | 0 | break; |
261 | 0 | #endif |
262 | 0 | #endif |
263 | | |
264 | | #ifdef WOLFSSL_SM3 |
265 | | case WC_SM3: |
266 | | ret = wc_InitSm3(&hash->sm3, heap, devId); |
267 | | break; |
268 | | #endif |
269 | | |
270 | 0 | default: |
271 | 0 | ret = BAD_FUNC_ARG; |
272 | 0 | break; |
273 | 0 | } |
274 | | |
275 | 0 | return ret; |
276 | 0 | } |
277 | | |
278 | | int _InitHmac(Hmac* hmac, int type, void* heap) |
279 | 0 | { |
280 | 0 | int ret; |
281 | | #ifdef WOLF_CRYPTO_CB |
282 | | int devId = hmac->devId; |
283 | | #else |
284 | 0 | int devId = INVALID_DEVID; |
285 | 0 | #endif |
286 | |
|
287 | 0 | ret = HmacKeyInitHash(&hmac->hash, type, heap, devId); |
288 | 0 | if (ret != 0) |
289 | 0 | return ret; |
290 | | |
291 | | /* default to NULL heap hint or test value */ |
292 | | #ifdef WOLFSSL_HEAP_TEST |
293 | | hmac->heap = (void*)WOLFSSL_HEAP_TEST; |
294 | | #else |
295 | 0 | hmac->heap = heap; |
296 | 0 | #endif /* WOLFSSL_HEAP_TEST */ |
297 | |
|
298 | 0 | return ret; |
299 | 0 | } |
300 | | |
301 | | static int HmacKeyCopyHash(byte macType, wc_HmacHash* src, wc_HmacHash* dst) |
302 | 0 | { |
303 | 0 | int ret = 0; |
304 | |
|
305 | 0 | switch (macType) { |
306 | | #ifndef NO_MD5 |
307 | | case WC_MD5: |
308 | | ret = wc_Md5Copy(&src->md5, &dst->md5); |
309 | | break; |
310 | | #endif /* !NO_MD5 */ |
311 | | |
312 | 0 | #ifndef NO_SHA |
313 | 0 | case WC_SHA: |
314 | 0 | ret = wc_ShaCopy(&src->sha, &dst->sha); |
315 | 0 | break; |
316 | 0 | #endif /* !NO_SHA */ |
317 | | |
318 | 0 | #ifdef WOLFSSL_SHA224 |
319 | 0 | case WC_SHA224: |
320 | 0 | ret = wc_Sha224Copy(&src->sha224, &dst->sha224); |
321 | 0 | break; |
322 | 0 | #endif /* WOLFSSL_SHA224 */ |
323 | 0 | #ifndef NO_SHA256 |
324 | 0 | case WC_SHA256: |
325 | 0 | ret = wc_Sha256Copy(&src->sha256, &dst->sha256); |
326 | 0 | break; |
327 | 0 | #endif /* !NO_SHA256 */ |
328 | | |
329 | 0 | #ifdef WOLFSSL_SHA384 |
330 | 0 | case WC_SHA384: |
331 | 0 | ret = wc_Sha384Copy(&src->sha384, &dst->sha384); |
332 | 0 | break; |
333 | 0 | #endif /* WOLFSSL_SHA384 */ |
334 | 0 | #ifdef WOLFSSL_SHA512 |
335 | 0 | case WC_SHA512: |
336 | 0 | ret = wc_Sha512Copy(&src->sha512, &dst->sha512); |
337 | 0 | break; |
338 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
339 | 0 | case WC_SHA512_224: |
340 | 0 | ret = wc_Sha512_224Copy(&src->sha512, &dst->sha512); |
341 | 0 | break; |
342 | 0 | #endif |
343 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
344 | 0 | case WC_SHA512_256: |
345 | 0 | ret = wc_Sha512_256Copy(&src->sha512, &dst->sha512); |
346 | 0 | break; |
347 | 0 | #endif |
348 | 0 | #endif /* WOLFSSL_SHA512 */ |
349 | | |
350 | 0 | #ifdef WOLFSSL_SHA3 |
351 | 0 | #ifndef WOLFSSL_NOSHA3_224 |
352 | 0 | case WC_SHA3_224: |
353 | 0 | ret = wc_Sha3_224_Copy(&src->sha3, &dst->sha3); |
354 | 0 | break; |
355 | 0 | #endif |
356 | 0 | #ifndef WOLFSSL_NOSHA3_256 |
357 | 0 | case WC_SHA3_256: |
358 | 0 | ret = wc_Sha3_256_Copy(&src->sha3, &dst->sha3); |
359 | 0 | break; |
360 | 0 | #endif |
361 | 0 | #ifndef WOLFSSL_NOSHA3_384 |
362 | 0 | case WC_SHA3_384: |
363 | 0 | ret = wc_Sha3_384_Copy(&src->sha3, &dst->sha3); |
364 | 0 | break; |
365 | 0 | #endif |
366 | 0 | #ifndef WOLFSSL_NOSHA3_512 |
367 | 0 | case WC_SHA3_512: |
368 | 0 | ret = wc_Sha3_512_Copy(&src->sha3, &dst->sha3); |
369 | 0 | break; |
370 | 0 | #endif |
371 | 0 | #endif /* WOLFSSL_SHA3 */ |
372 | | |
373 | | #ifdef WOLFSSL_SM3 |
374 | | case WC_SM3: |
375 | | ret = wc_Sm3Copy(&src->sm3, &dst->sm3); |
376 | | break; |
377 | | #endif |
378 | | |
379 | 0 | default: |
380 | 0 | ret = BAD_FUNC_ARG; |
381 | 0 | break; |
382 | 0 | } |
383 | | |
384 | 0 | return ret; |
385 | 0 | } |
386 | | |
387 | | /* Free a hash context of the given macType. Tolerates zeroed, |
388 | | * never-initialized contexts, matching wc_HmacFree()'s existing contract |
389 | | * (e.g. i_hash/o_hash on device-keyed paths). |
390 | | */ |
391 | | static void HmacKeyFreeHash(byte macType, wc_HmacHash* hash) |
392 | 0 | { |
393 | 0 | switch (macType) { |
394 | | #ifndef NO_MD5 |
395 | | case WC_MD5: |
396 | | wc_Md5Free(&hash->md5); |
397 | | break; |
398 | | #endif /* !NO_MD5 */ |
399 | | |
400 | 0 | #ifndef NO_SHA |
401 | 0 | case WC_SHA: |
402 | 0 | wc_ShaFree(&hash->sha); |
403 | 0 | break; |
404 | 0 | #endif /* !NO_SHA */ |
405 | | |
406 | 0 | #ifdef WOLFSSL_SHA224 |
407 | 0 | case WC_SHA224: |
408 | 0 | wc_Sha224Free(&hash->sha224); |
409 | 0 | break; |
410 | 0 | #endif /* WOLFSSL_SHA224 */ |
411 | 0 | #ifndef NO_SHA256 |
412 | 0 | case WC_SHA256: |
413 | 0 | wc_Sha256Free(&hash->sha256); |
414 | 0 | break; |
415 | 0 | #endif /* !NO_SHA256 */ |
416 | | |
417 | 0 | #ifdef WOLFSSL_SHA384 |
418 | 0 | case WC_SHA384: |
419 | 0 | wc_Sha384Free(&hash->sha384); |
420 | 0 | break; |
421 | 0 | #endif /* WOLFSSL_SHA384 */ |
422 | 0 | #ifdef WOLFSSL_SHA512 |
423 | 0 | case WC_SHA512: |
424 | 0 | wc_Sha512Free(&hash->sha512); |
425 | 0 | break; |
426 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
427 | 0 | case WC_SHA512_224: |
428 | 0 | wc_Sha512_224Free(&hash->sha512); |
429 | 0 | break; |
430 | 0 | #endif |
431 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
432 | 0 | case WC_SHA512_256: |
433 | 0 | wc_Sha512_256Free(&hash->sha512); |
434 | 0 | break; |
435 | 0 | #endif |
436 | 0 | #endif /* WOLFSSL_SHA512 */ |
437 | | |
438 | 0 | #ifdef WOLFSSL_SHA3 |
439 | 0 | #ifndef WOLFSSL_NOSHA3_224 |
440 | 0 | case WC_SHA3_224: |
441 | 0 | wc_Sha3_224_Free(&hash->sha3); |
442 | 0 | break; |
443 | 0 | #endif |
444 | 0 | #ifndef WOLFSSL_NOSHA3_256 |
445 | 0 | case WC_SHA3_256: |
446 | 0 | wc_Sha3_256_Free(&hash->sha3); |
447 | 0 | break; |
448 | 0 | #endif |
449 | 0 | #ifndef WOLFSSL_NOSHA3_384 |
450 | 0 | case WC_SHA3_384: |
451 | 0 | wc_Sha3_384_Free(&hash->sha3); |
452 | 0 | break; |
453 | 0 | #endif |
454 | 0 | #ifndef WOLFSSL_NOSHA3_512 |
455 | 0 | case WC_SHA3_512: |
456 | 0 | wc_Sha3_512_Free(&hash->sha3); |
457 | 0 | break; |
458 | 0 | #endif |
459 | 0 | #endif /* WOLFSSL_SHA3 */ |
460 | | |
461 | | #ifdef WOLFSSL_SM3 |
462 | | case WC_SM3: |
463 | | wc_Sm3Free(&hash->sm3); |
464 | | break; |
465 | | #endif |
466 | | |
467 | 0 | default: |
468 | 0 | break; |
469 | 0 | } |
470 | 0 | } |
471 | | |
472 | 0 | int wc_HmacCopy(Hmac* src, Hmac* dst) { |
473 | 0 | int ret; |
474 | 0 | int hashes_copied = 0; |
475 | |
|
476 | 0 | if ((src == NULL) || (dst == NULL)) |
477 | 0 | return BAD_FUNC_ARG; |
478 | 0 | #ifdef WC_CTX_IS_ALIGNED |
479 | 0 | if (!WC_CTX_IS_ALIGNED(dst, Hmac)) |
480 | 0 | return BAD_ALIGN_E; |
481 | 0 | #endif |
482 | | |
483 | 0 | XMEMCPY(dst, src, sizeof(*dst)); |
484 | | |
485 | | /* Zero the hash contexts after the shallow copy to prevent shared |
486 | | * sub-pointers (e.g., msg, W buffers) with src. Each corresponding hash |
487 | | * Copy call below then performs the proper deep copy. Under |
488 | | * WOLFSSL_HMAC_COPY_HASH, i_hash and o_hash own state of their own and |
489 | | * need the same treatment as hash (freeing both src and a shallow-aliased |
490 | | * dst would otherwise double-free their sub-objects). |
491 | | */ |
492 | 0 | XMEMSET(&dst->hash, 0, sizeof(wc_HmacHash)); |
493 | | #ifdef WOLFSSL_HMAC_COPY_HASH |
494 | | XMEMSET(&dst->i_hash, 0, sizeof(wc_HmacHash)); |
495 | | XMEMSET(&dst->o_hash, 0, sizeof(wc_HmacHash)); |
496 | | #endif |
497 | |
|
498 | 0 | ret = HmacKeyCopyHash(src->macType, &src->hash, &dst->hash); |
499 | 0 | if (ret == 0) |
500 | 0 | hashes_copied++; |
501 | | #ifdef WOLFSSL_HMAC_COPY_HASH |
502 | | if (ret == 0) { |
503 | | ret = HmacKeyCopyHash(src->macType, &src->i_hash, &dst->i_hash); |
504 | | if (ret == 0) |
505 | | hashes_copied++; |
506 | | } |
507 | | if (ret == 0) { |
508 | | ret = HmacKeyCopyHash(src->macType, &src->o_hash, &dst->o_hash); |
509 | | if (ret == 0) |
510 | | hashes_copied++; |
511 | | } |
512 | | #endif |
513 | |
|
514 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY) |
515 | | /* The shallow copy above left dst sharing any per-context state a device |
516 | | * hung off devCtx; let the device give dst its own copy. The struct and the |
517 | | * hash contexts are already copied, so the callback only fixes up devCtx. |
518 | | */ |
519 | | #ifndef WOLF_CRYPTO_CB_FIND |
520 | | if ((ret == 0) && (src->devId != INVALID_DEVID)) |
521 | | #else |
522 | | if (ret == 0) |
523 | | #endif |
524 | | { |
525 | | int cbRet = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HMAC, |
526 | | src->macType, (void*)src, (void*)dst); |
527 | | if (cbRet != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
528 | | ret = cbRet; |
529 | | } |
530 | | #endif |
531 | |
|
532 | 0 | if (ret != 0) { |
533 | | /* Release whichever deep copies succeeded before zeroing dst, lest |
534 | | * their owned sub-objects leak. |
535 | | */ |
536 | 0 | if (hashes_copied >= 1) |
537 | 0 | HmacKeyFreeHash(src->macType, &dst->hash); |
538 | | #ifdef WOLFSSL_HMAC_COPY_HASH |
539 | | if (hashes_copied >= 2) |
540 | | HmacKeyFreeHash(src->macType, &dst->i_hash); |
541 | | if (hashes_copied >= 3) |
542 | | HmacKeyFreeHash(src->macType, &dst->o_hash); |
543 | | #endif |
544 | 0 | ForceZero(dst, sizeof(*dst)); |
545 | 0 | } |
546 | 0 | return ret; |
547 | 0 | } |
548 | | |
549 | | static int HmacKeyHashUpdate(byte macType, wc_HmacHash* hash, byte* pad) |
550 | 0 | { |
551 | 0 | int ret = 0; |
552 | |
|
553 | 0 | switch (macType) { |
554 | | #ifndef NO_MD5 |
555 | | case WC_MD5: |
556 | | ret = wc_Md5Update(&hash->md5, pad, WC_MD5_BLOCK_SIZE); |
557 | | break; |
558 | | #endif /* !NO_MD5 */ |
559 | | |
560 | 0 | #ifndef NO_SHA |
561 | 0 | case WC_SHA: |
562 | 0 | ret = wc_ShaUpdate(&hash->sha, pad, WC_SHA_BLOCK_SIZE); |
563 | 0 | break; |
564 | 0 | #endif /* !NO_SHA */ |
565 | | |
566 | 0 | #ifdef WOLFSSL_SHA224 |
567 | 0 | case WC_SHA224: |
568 | 0 | ret = wc_Sha224Update(&hash->sha224, pad, WC_SHA224_BLOCK_SIZE); |
569 | 0 | break; |
570 | 0 | #endif /* WOLFSSL_SHA224 */ |
571 | 0 | #ifndef NO_SHA256 |
572 | 0 | case WC_SHA256: |
573 | 0 | ret = wc_Sha256Update(&hash->sha256, pad, WC_SHA256_BLOCK_SIZE); |
574 | 0 | break; |
575 | 0 | #endif /* !NO_SHA256 */ |
576 | | |
577 | 0 | #ifdef WOLFSSL_SHA384 |
578 | 0 | case WC_SHA384: |
579 | 0 | ret = wc_Sha384Update(&hash->sha384, pad, WC_SHA384_BLOCK_SIZE); |
580 | 0 | break; |
581 | 0 | #endif /* WOLFSSL_SHA384 */ |
582 | 0 | #ifdef WOLFSSL_SHA512 |
583 | 0 | case WC_SHA512: |
584 | 0 | ret = wc_Sha512Update(&hash->sha512, pad, WC_SHA512_BLOCK_SIZE); |
585 | 0 | break; |
586 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
587 | 0 | case WC_SHA512_224: |
588 | 0 | ret = wc_Sha512_224Update(&hash->sha512, pad, |
589 | 0 | WC_SHA512_224_BLOCK_SIZE); |
590 | 0 | break; |
591 | 0 | #endif |
592 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
593 | 0 | case WC_SHA512_256: |
594 | 0 | ret = wc_Sha512_256Update(&hash->sha512, pad, |
595 | 0 | WC_SHA512_256_BLOCK_SIZE); |
596 | 0 | break; |
597 | 0 | #endif |
598 | 0 | #endif /* WOLFSSL_SHA512 */ |
599 | | |
600 | 0 | #ifdef WOLFSSL_SHA3 |
601 | 0 | #ifndef WOLFSSL_NOSHA3_224 |
602 | 0 | case WC_SHA3_224: |
603 | 0 | ret = wc_Sha3_224_Update(&hash->sha3, pad, WC_SHA3_224_BLOCK_SIZE); |
604 | 0 | break; |
605 | 0 | #endif |
606 | 0 | #ifndef WOLFSSL_NOSHA3_256 |
607 | 0 | case WC_SHA3_256: |
608 | 0 | ret = wc_Sha3_256_Update(&hash->sha3, pad, WC_SHA3_256_BLOCK_SIZE); |
609 | 0 | break; |
610 | 0 | #endif |
611 | 0 | #ifndef WOLFSSL_NOSHA3_384 |
612 | 0 | case WC_SHA3_384: |
613 | 0 | ret = wc_Sha3_384_Update(&hash->sha3, pad, WC_SHA3_384_BLOCK_SIZE); |
614 | 0 | break; |
615 | 0 | #endif |
616 | 0 | #ifndef WOLFSSL_NOSHA3_512 |
617 | 0 | case WC_SHA3_512: |
618 | 0 | ret = wc_Sha3_512_Update(&hash->sha3, pad, WC_SHA3_512_BLOCK_SIZE); |
619 | 0 | break; |
620 | 0 | #endif |
621 | 0 | #endif /* WOLFSSL_SHA3 */ |
622 | | |
623 | | #ifdef WOLFSSL_SM3 |
624 | | case WC_SM3: |
625 | | ret = wc_Sm3Update(&hash->sm3, pad, WC_SM3_BLOCK_SIZE); |
626 | | break; |
627 | | #endif |
628 | | |
629 | 0 | default: |
630 | 0 | ret = BAD_FUNC_ARG; |
631 | 0 | break; |
632 | 0 | } |
633 | | |
634 | 0 | return ret; |
635 | 0 | } |
636 | | |
637 | | #ifdef WOLFSSL_HMAC_COPY_HASH |
638 | | int _HmacInitIOHashes(Hmac* hmac) |
639 | | { |
640 | | int ret; |
641 | | #ifdef WOLF_CRYPTO_CB |
642 | | int devId = hmac->devId; |
643 | | #else |
644 | | int devId = INVALID_DEVID; |
645 | | #endif |
646 | | |
647 | | ret = HmacKeyInitHash(&hmac->i_hash, hmac->macType, hmac->heap, devId); |
648 | | if (ret == 0) { |
649 | | ret = HmacKeyInitHash(&hmac->o_hash, hmac->macType, hmac->heap, devId); |
650 | | } |
651 | | if (ret == 0) { |
652 | | ret = HmacKeyHashUpdate(hmac->macType, &hmac->i_hash, |
653 | | (byte*)hmac->ipad); |
654 | | } |
655 | | if (ret == 0) { |
656 | | ret = HmacKeyHashUpdate(hmac->macType, &hmac->o_hash, |
657 | | (byte*)hmac->opad); |
658 | | } |
659 | | |
660 | | return ret; |
661 | | } |
662 | | #endif |
663 | | |
664 | | int wc_HmacSetKey_ex(Hmac* hmac, int type, const byte* key, word32 length, |
665 | | int allowFlag) |
666 | 0 | { |
667 | 0 | #ifndef WOLFSSL_MAXQ108X |
668 | 0 | byte* ip; |
669 | 0 | byte* op; |
670 | 0 | word32 hmac_block_size = 0; |
671 | 0 | #endif |
672 | 0 | int ret = 0; |
673 | 0 | void* heap = NULL; |
674 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_SETKEY) |
675 | | int cbRet; |
676 | | #endif |
677 | |
|
678 | 0 | if (hmac == NULL || (key == NULL && length != 0) || |
679 | 0 | !(type == WC_MD5 || type == WC_SHA || |
680 | | #ifdef WOLFSSL_SM3 |
681 | | type == WC_SM3 || |
682 | | #endif |
683 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
684 | 0 | type == WC_SHA512_224 || |
685 | 0 | #endif |
686 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
687 | 0 | type == WC_SHA512_256 || |
688 | 0 | #endif |
689 | 0 | type == WC_SHA224 || type == WC_SHA256 || |
690 | 0 | type == WC_SHA384 || type == WC_SHA512 || |
691 | 0 | type == WC_SHA3_224 || type == WC_SHA3_256 || |
692 | 0 | type == WC_SHA3_384 || type == WC_SHA3_512)) { |
693 | 0 | return BAD_FUNC_ARG; |
694 | 0 | } |
695 | | |
696 | | #if !defined(NO_MD5) && defined(HAVE_FIPS) |
697 | | if (type == WC_MD5) |
698 | | return BAD_FUNC_ARG; |
699 | | #endif |
700 | | |
701 | 0 | heap = hmac->heap; |
702 | 0 | #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0) |
703 | | /* if set key has already been run then make sure and free existing */ |
704 | | /* This is for async and PIC32MZ situations, and just normally OK, |
705 | | provided the user calls wc_HmacInit() first. That function is not |
706 | | available in FIPS builds. In current FIPS builds, the hashes are |
707 | | not allocating resources. */ |
708 | 0 | if (hmac->macType != WC_HASH_TYPE_NONE) { |
709 | | #ifdef WOLF_CRYPTO_CB |
710 | | int devId = hmac->devId; |
711 | | #endif |
712 | | #ifdef WOLF_PRIVATE_KEY_ID |
713 | | byte id[HMAC_MAX_ID_LEN]; |
714 | | int idLen = hmac->idLen; |
715 | | char label[HMAC_MAX_LABEL_LEN]; |
716 | | int labelLen = hmac->labelLen; |
717 | | |
718 | | XMEMCPY(id, hmac->id, sizeof(id)); |
719 | | XMEMCPY(label, hmac->label, sizeof(label)); |
720 | | #endif |
721 | 0 | wc_HmacFree(hmac); |
722 | | #ifdef WOLF_CRYPTO_CB |
723 | | hmac->devId = devId; |
724 | | #endif |
725 | | #ifdef WOLF_PRIVATE_KEY_ID |
726 | | XMEMCPY(hmac->id, id, sizeof(id)); |
727 | | hmac->idLen = idLen; |
728 | | XMEMCPY(hmac->label, label, sizeof(label)); |
729 | | hmac->labelLen = labelLen; |
730 | | #endif |
731 | 0 | } |
732 | 0 | #endif |
733 | |
|
734 | 0 | hmac->innerHashKeyed = 0; |
735 | 0 | hmac->macType = (byte)type; |
736 | | #ifdef WC_HMAC_HAVE_KEY_LOCATION |
737 | | hmac->keyLocation = WC_KEY_SW_STORED; |
738 | | #endif |
739 | |
|
740 | 0 | ret = _InitHmac(hmac, type, heap); |
741 | 0 | if (ret != 0) |
742 | 0 | return ret; |
743 | | |
744 | | /* Regarding the password length: |
745 | | * SP800-107r1 ss 5.3.2 states: "An HMAC key shall have a security strength |
746 | | * that meets or exceeds the security strength required to protect the data |
747 | | * over which the HMAC is computed" then refers to SP800-133 for HMAC keys |
748 | | * generation. |
749 | | * |
750 | | * SP800-133r2 ss 6.2.3 states: "When a key is generated from a password, |
751 | | * the entropy provided (and thus, the maximum security strength that can be |
752 | | * supported by the generated key) shall be considered to be zero unless the |
753 | | * password is generated using an approved RBG" |
754 | | * |
755 | | * wolfSSL Notes: The statement from SP800-133r2 applies to |
756 | | * all password lengths. Any human generated password is considered to have |
757 | | * 0 security strength regardless of length, there is no minimum length that |
758 | | * is OK or will provide any amount of security strength other than 0. If |
759 | | * a security strength is required users shall generate random passwords |
760 | | * using a FIPS approved RBG of sufficient length that any HMAC key |
761 | | * generated from that password can claim to inherit the needed security |
762 | | * strength from that input. |
763 | | */ |
764 | | |
765 | | /* In light of the above, Loosen past restriction that limited passwords to |
766 | | * no less than 14-bytes to allow for shorter Passwords. |
767 | | * User needs to pass true (non-zero) to override historical behavior that |
768 | | * prevented use of any password less than 14-bytes. ALL non-RBG generated |
769 | | * passwords shall inherit a security strength of zero |
770 | | * (no security strength) |
771 | | */ |
772 | 0 | if (!allowFlag) { |
773 | 0 | if (length < HMAC_FIPS_MIN_KEY) { |
774 | 0 | WOLFSSL_ERROR_VERBOSE(HMAC_MIN_KEYLEN_E); |
775 | 0 | return HMAC_MIN_KEYLEN_E; |
776 | 0 | } |
777 | 0 | } |
778 | | |
779 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_SETKEY) |
780 | | #ifndef WOLF_CRYPTO_CB_FIND |
781 | | if (hmac->devId != INVALID_DEVID) |
782 | | #endif |
783 | | { |
784 | | cbRet = wc_CryptoCb_SetKey(hmac->devId, |
785 | | WC_SETKEY_HMAC, hmac, (void*)key, length, NULL, 0, 0); |
786 | | if (cbRet != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
787 | | return cbRet; |
788 | | /* fall-through to software when unavailable */ |
789 | | } |
790 | | #endif |
791 | | |
792 | | #ifdef WOLF_CRYPTO_CB |
793 | | hmac->keyRaw = key; /* use buffer directly */ |
794 | | hmac->keyLen = (word16)length; |
795 | | #endif |
796 | | |
797 | | #ifdef WOLFSSL_MAXQ108X |
798 | | /* For MAXQ108x, nothing left to do. */ |
799 | | return 0; |
800 | | #else |
801 | | |
802 | | #if defined(STM32_HASH) && defined(STM32_HMAC) |
803 | | { |
804 | | word32 stmAlgo, stmBlockSize, stmDigestSize; |
805 | | /* Check if this hash type is supported by STM32 HMAC hardware */ |
806 | | if (wc_Stm32_Hmac_GetAlgoInfo(type, &stmAlgo, &stmBlockSize, |
807 | | &stmDigestSize) == 0) { |
808 | | /* Cache algo info for Update/Final */ |
809 | | hmac->stmAlgo = stmAlgo; |
810 | | hmac->stmBlockSize = stmBlockSize; |
811 | | hmac->stmDigestSize = stmDigestSize; |
812 | | |
813 | | /* Store raw key in ipad (unused in HW HMAC mode). |
814 | | * Pre-hash if longer than hash block size. */ |
815 | | if (length <= stmBlockSize) { |
816 | | if (key != NULL) { |
817 | | XMEMCPY(hmac->ipad, key, length); |
818 | | } |
819 | | hmac->stmKeyLen = length; |
820 | | } |
821 | | else { |
822 | | /* Pre-hash long key using stmCtx (re-initialized below) */ |
823 | | wc_Stm32_Hash_Init(&hmac->stmCtx); |
824 | | ret = wolfSSL_CryptHwMutexLock(); |
825 | | if (ret == 0) { |
826 | | ret = wc_Stm32_Hash_Update(&hmac->stmCtx, stmAlgo, |
827 | | key, length, stmBlockSize); |
828 | | if (ret == 0) { |
829 | | ret = wc_Stm32_Hash_Final(&hmac->stmCtx, stmAlgo, |
830 | | (byte*)hmac->ipad, stmDigestSize); |
831 | | } |
832 | | wolfSSL_CryptHwMutexUnLock(); |
833 | | } |
834 | | if (ret != 0) |
835 | | return ret; |
836 | | hmac->stmKeyLen = stmDigestSize; |
837 | | } |
838 | | |
839 | | /* HW HMAC Phase 1: feed key */ |
840 | | ret = wolfSSL_CryptHwMutexLock(); |
841 | | if (ret == 0) { |
842 | | ret = wc_Stm32_Hmac_SetKey(&hmac->stmCtx, type, |
843 | | (const byte*)hmac->ipad, hmac->stmKeyLen); |
844 | | wolfSSL_CryptHwMutexUnLock(); |
845 | | } |
846 | | if (ret == 0) { |
847 | | hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_DEV; |
848 | | } |
849 | | return ret; |
850 | | } |
851 | | /* Unsupported algo falls through to software */ |
852 | | } |
853 | | #endif /* STM32_HASH && STM32_HMAC */ |
854 | | |
855 | 0 | ip = (byte*)hmac->ipad; |
856 | 0 | op = (byte*)hmac->opad; |
857 | |
|
858 | 0 | switch (hmac->macType) { |
859 | | #ifndef NO_MD5 |
860 | | case WC_MD5: |
861 | | hmac_block_size = WC_MD5_BLOCK_SIZE; |
862 | | if (length <= WC_MD5_BLOCK_SIZE) { |
863 | | if (key != NULL) { |
864 | | XMEMCPY(ip, key, length); |
865 | | } |
866 | | } |
867 | | else { |
868 | | ret = wc_Md5Update(&hmac->hash.md5, key, length); |
869 | | if (ret != 0) |
870 | | break; |
871 | | ret = wc_Md5Final(&hmac->hash.md5, ip); |
872 | | if (ret != 0) |
873 | | break; |
874 | | length = WC_MD5_DIGEST_SIZE; |
875 | | } |
876 | | break; |
877 | | #endif /* !NO_MD5 */ |
878 | | |
879 | 0 | #ifndef NO_SHA |
880 | 0 | case WC_SHA: |
881 | 0 | hmac_block_size = WC_SHA_BLOCK_SIZE; |
882 | 0 | if (length <= WC_SHA_BLOCK_SIZE) { |
883 | 0 | if (key != NULL) { |
884 | 0 | XMEMCPY(ip, key, length); |
885 | 0 | } |
886 | 0 | } |
887 | 0 | else { |
888 | 0 | ret = wc_ShaUpdate(&hmac->hash.sha, key, length); |
889 | 0 | if (ret != 0) |
890 | 0 | break; |
891 | 0 | ret = wc_ShaFinal(&hmac->hash.sha, ip); |
892 | 0 | if (ret != 0) |
893 | 0 | break; |
894 | | |
895 | 0 | length = WC_SHA_DIGEST_SIZE; |
896 | 0 | } |
897 | 0 | break; |
898 | 0 | #endif /* !NO_SHA */ |
899 | | |
900 | 0 | #ifdef WOLFSSL_SHA224 |
901 | 0 | case WC_SHA224: |
902 | 0 | hmac_block_size = WC_SHA224_BLOCK_SIZE; |
903 | 0 | if (length <= WC_SHA224_BLOCK_SIZE) { |
904 | 0 | if (key != NULL) { |
905 | 0 | XMEMCPY(ip, key, length); |
906 | 0 | } |
907 | 0 | } |
908 | 0 | else { |
909 | 0 | ret = wc_Sha224Update(&hmac->hash.sha224, key, length); |
910 | 0 | if (ret != 0) |
911 | 0 | break; |
912 | 0 | ret = wc_Sha224Final(&hmac->hash.sha224, ip); |
913 | 0 | if (ret != 0) |
914 | 0 | break; |
915 | | |
916 | 0 | length = WC_SHA224_DIGEST_SIZE; |
917 | 0 | } |
918 | 0 | break; |
919 | 0 | #endif /* WOLFSSL_SHA224 */ |
920 | 0 | #ifndef NO_SHA256 |
921 | 0 | case WC_SHA256: |
922 | 0 | hmac_block_size = WC_SHA256_BLOCK_SIZE; |
923 | 0 | if (length <= WC_SHA256_BLOCK_SIZE) { |
924 | 0 | if (key != NULL) { |
925 | 0 | XMEMCPY(ip, key, length); |
926 | 0 | } |
927 | 0 | } |
928 | 0 | else { |
929 | 0 | ret = wc_Sha256Update(&hmac->hash.sha256, key, length); |
930 | 0 | if (ret != 0) |
931 | 0 | break; |
932 | 0 | ret = wc_Sha256Final(&hmac->hash.sha256, ip); |
933 | 0 | if (ret != 0) |
934 | 0 | break; |
935 | | |
936 | 0 | length = WC_SHA256_DIGEST_SIZE; |
937 | 0 | } |
938 | 0 | break; |
939 | 0 | #endif /* !NO_SHA256 */ |
940 | | |
941 | 0 | #ifdef WOLFSSL_SHA384 |
942 | 0 | case WC_SHA384: |
943 | 0 | hmac_block_size = WC_SHA384_BLOCK_SIZE; |
944 | 0 | if (length <= WC_SHA384_BLOCK_SIZE) { |
945 | 0 | if (key != NULL) { |
946 | 0 | XMEMCPY(ip, key, length); |
947 | 0 | } |
948 | 0 | } |
949 | 0 | else { |
950 | 0 | ret = wc_Sha384Update(&hmac->hash.sha384, key, length); |
951 | 0 | if (ret != 0) |
952 | 0 | break; |
953 | 0 | ret = wc_Sha384Final(&hmac->hash.sha384, ip); |
954 | 0 | if (ret != 0) |
955 | 0 | break; |
956 | | |
957 | 0 | length = WC_SHA384_DIGEST_SIZE; |
958 | 0 | } |
959 | 0 | break; |
960 | 0 | #endif /* WOLFSSL_SHA384 */ |
961 | 0 | #ifdef WOLFSSL_SHA512 |
962 | 0 | case WC_SHA512: |
963 | 0 | hmac_block_size = WC_SHA512_BLOCK_SIZE; |
964 | 0 | if (length <= WC_SHA512_BLOCK_SIZE) { |
965 | 0 | if (key != NULL) { |
966 | 0 | XMEMCPY(ip, key, length); |
967 | 0 | } |
968 | 0 | } |
969 | 0 | else { |
970 | 0 | ret = wc_Sha512Update(&hmac->hash.sha512, key, length); |
971 | 0 | if (ret != 0) |
972 | 0 | break; |
973 | 0 | ret = wc_Sha512Final(&hmac->hash.sha512, ip); |
974 | 0 | if (ret != 0) |
975 | 0 | break; |
976 | | |
977 | 0 | length = WC_SHA512_DIGEST_SIZE; |
978 | 0 | } |
979 | 0 | break; |
980 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
981 | 0 | case WC_SHA512_224: |
982 | 0 | hmac_block_size = WC_SHA512_224_BLOCK_SIZE; |
983 | 0 | if (length <= WC_SHA512_224_BLOCK_SIZE) { |
984 | 0 | if (key != NULL) { |
985 | 0 | XMEMCPY(ip, key, length); |
986 | 0 | } |
987 | 0 | } |
988 | 0 | else { |
989 | 0 | ret = wc_Sha512_224Update(&hmac->hash.sha512, key, length); |
990 | 0 | if (ret != 0) |
991 | 0 | break; |
992 | 0 | ret = wc_Sha512_224Final(&hmac->hash.sha512, ip); |
993 | 0 | if (ret != 0) |
994 | 0 | break; |
995 | | |
996 | 0 | length = WC_SHA512_224_DIGEST_SIZE; |
997 | 0 | } |
998 | 0 | break; |
999 | 0 | #endif |
1000 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
1001 | 0 | case WC_SHA512_256: |
1002 | 0 | hmac_block_size = WC_SHA512_256_BLOCK_SIZE; |
1003 | 0 | if (length <= WC_SHA512_256_BLOCK_SIZE) { |
1004 | 0 | if (key != NULL) { |
1005 | 0 | XMEMCPY(ip, key, length); |
1006 | 0 | } |
1007 | 0 | } |
1008 | 0 | else { |
1009 | 0 | ret = wc_Sha512_256Update(&hmac->hash.sha512, key, length); |
1010 | 0 | if (ret != 0) |
1011 | 0 | break; |
1012 | 0 | ret = wc_Sha512_256Final(&hmac->hash.sha512, ip); |
1013 | 0 | if (ret != 0) |
1014 | 0 | break; |
1015 | | |
1016 | 0 | length = WC_SHA512_256_DIGEST_SIZE; |
1017 | 0 | } |
1018 | 0 | break; |
1019 | 0 | #endif |
1020 | 0 | #endif /* WOLFSSL_SHA512 */ |
1021 | | |
1022 | 0 | #ifdef WOLFSSL_SHA3 |
1023 | 0 | #ifndef WOLFSSL_NOSHA3_224 |
1024 | 0 | case WC_SHA3_224: |
1025 | 0 | hmac_block_size = WC_SHA3_224_BLOCK_SIZE; |
1026 | 0 | if (length <= WC_SHA3_224_BLOCK_SIZE) { |
1027 | 0 | if (key != NULL) { |
1028 | 0 | XMEMCPY(ip, key, length); |
1029 | 0 | } |
1030 | 0 | } |
1031 | 0 | else { |
1032 | 0 | ret = wc_Sha3_224_Update(&hmac->hash.sha3, key, length); |
1033 | 0 | if (ret != 0) |
1034 | 0 | break; |
1035 | 0 | ret = wc_Sha3_224_Final(&hmac->hash.sha3, ip); |
1036 | 0 | if (ret != 0) |
1037 | 0 | break; |
1038 | | |
1039 | 0 | length = WC_SHA3_224_DIGEST_SIZE; |
1040 | 0 | } |
1041 | 0 | break; |
1042 | 0 | #endif |
1043 | 0 | #ifndef WOLFSSL_NOSHA3_256 |
1044 | 0 | case WC_SHA3_256: |
1045 | 0 | hmac_block_size = WC_SHA3_256_BLOCK_SIZE; |
1046 | 0 | if (length <= WC_SHA3_256_BLOCK_SIZE) { |
1047 | 0 | if (key != NULL) { |
1048 | 0 | XMEMCPY(ip, key, length); |
1049 | 0 | } |
1050 | 0 | } |
1051 | 0 | else { |
1052 | 0 | ret = wc_Sha3_256_Update(&hmac->hash.sha3, key, length); |
1053 | 0 | if (ret != 0) |
1054 | 0 | break; |
1055 | 0 | ret = wc_Sha3_256_Final(&hmac->hash.sha3, ip); |
1056 | 0 | if (ret != 0) |
1057 | 0 | break; |
1058 | | |
1059 | 0 | length = WC_SHA3_256_DIGEST_SIZE; |
1060 | 0 | } |
1061 | 0 | break; |
1062 | 0 | #endif |
1063 | 0 | #ifndef WOLFSSL_NOSHA3_384 |
1064 | 0 | case WC_SHA3_384: |
1065 | 0 | hmac_block_size = WC_SHA3_384_BLOCK_SIZE; |
1066 | 0 | if (length <= WC_SHA3_384_BLOCK_SIZE) { |
1067 | 0 | if (key != NULL) { |
1068 | 0 | XMEMCPY(ip, key, length); |
1069 | 0 | } |
1070 | 0 | } |
1071 | 0 | else { |
1072 | 0 | ret = wc_Sha3_384_Update(&hmac->hash.sha3, key, length); |
1073 | 0 | if (ret != 0) |
1074 | 0 | break; |
1075 | 0 | ret = wc_Sha3_384_Final(&hmac->hash.sha3, ip); |
1076 | 0 | if (ret != 0) |
1077 | 0 | break; |
1078 | | |
1079 | 0 | length = WC_SHA3_384_DIGEST_SIZE; |
1080 | 0 | } |
1081 | 0 | break; |
1082 | 0 | #endif |
1083 | 0 | #ifndef WOLFSSL_NOSHA3_512 |
1084 | 0 | case WC_SHA3_512: |
1085 | 0 | hmac_block_size = WC_SHA3_512_BLOCK_SIZE; |
1086 | 0 | if (length <= WC_SHA3_512_BLOCK_SIZE) { |
1087 | 0 | if (key != NULL) { |
1088 | 0 | XMEMCPY(ip, key, length); |
1089 | 0 | } |
1090 | 0 | } |
1091 | 0 | else { |
1092 | 0 | ret = wc_Sha3_512_Update(&hmac->hash.sha3, key, length); |
1093 | 0 | if (ret != 0) |
1094 | 0 | break; |
1095 | 0 | ret = wc_Sha3_512_Final(&hmac->hash.sha3, ip); |
1096 | 0 | if (ret != 0) |
1097 | 0 | break; |
1098 | | |
1099 | 0 | length = WC_SHA3_512_DIGEST_SIZE; |
1100 | 0 | } |
1101 | 0 | break; |
1102 | 0 | #endif |
1103 | 0 | #endif /* WOLFSSL_SHA3 */ |
1104 | | |
1105 | | #ifdef WOLFSSL_SM3 |
1106 | | case WC_SM3: |
1107 | | hmac_block_size = WC_SM3_BLOCK_SIZE; |
1108 | | if (length <= WC_SM3_BLOCK_SIZE) { |
1109 | | if (key != NULL) { |
1110 | | XMEMCPY(ip, key, length); |
1111 | | } |
1112 | | } |
1113 | | else { |
1114 | | ret = wc_Sm3Update(&hmac->hash.sm3, key, length); |
1115 | | if (ret != 0) |
1116 | | break; |
1117 | | ret = wc_Sm3Final(&hmac->hash.sm3, ip); |
1118 | | if (ret != 0) |
1119 | | break; |
1120 | | |
1121 | | length = WC_SM3_DIGEST_SIZE; |
1122 | | } |
1123 | | break; |
1124 | | #endif |
1125 | | |
1126 | 0 | default: |
1127 | 0 | return BAD_FUNC_ARG; |
1128 | 0 | } |
1129 | | |
1130 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) |
1131 | | if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) { |
1132 | | #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM) |
1133 | | #ifdef HAVE_INTEL_QA |
1134 | | if (IntelQaHmacGetType(hmac->macType, NULL) == 0) |
1135 | | #endif |
1136 | | { |
1137 | | if (length > hmac_block_size) |
1138 | | length = hmac_block_size; |
1139 | | /* update key length */ |
1140 | | hmac->keyLen = (word16)length; |
1141 | | |
1142 | | return ret; |
1143 | | } |
1144 | | /* no need to pad below */ |
1145 | | #endif |
1146 | | } |
1147 | | #endif |
1148 | | |
1149 | 0 | if (ret == 0) { |
1150 | 0 | word32 i; |
1151 | |
|
1152 | 0 | if (length < hmac_block_size) |
1153 | 0 | XMEMSET(ip + length, 0, hmac_block_size - length); |
1154 | |
|
1155 | 0 | for(i = 0; i < hmac_block_size; i++) { |
1156 | 0 | op[i] = (byte)(ip[i] ^ OPAD); |
1157 | 0 | ip[i] ^= IPAD; |
1158 | 0 | } |
1159 | 0 | } |
1160 | |
|
1161 | | #ifdef WOLFSSL_HMAC_COPY_HASH |
1162 | | if (ret == 0) { |
1163 | | ret = _HmacInitIOHashes(hmac); |
1164 | | } |
1165 | | #endif |
1166 | |
|
1167 | 0 | #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(6,0,0) |
1168 | 0 | if (ret != 0) { |
1169 | | #ifdef WOLF_CRYPTO_CB |
1170 | | int devId = hmac->devId; |
1171 | | #endif |
1172 | | /* The key entered the object; clear it (ISO/IEC 19790 7.9.7). */ |
1173 | 0 | wc_HmacFree(hmac); |
1174 | 0 | hmac->heap = heap; |
1175 | | #ifdef WOLF_CRYPTO_CB |
1176 | | hmac->devId = devId; |
1177 | | #endif |
1178 | 0 | } |
1179 | 0 | #endif |
1180 | |
|
1181 | 0 | return ret; |
1182 | 0 | #endif /* WOLFSSL_MAXQ108X */ |
1183 | 0 | } |
1184 | | |
1185 | | int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length) |
1186 | 0 | { |
1187 | 0 | int allowFlag; |
1188 | | #if defined(HAVE_FIPS) |
1189 | | allowFlag = 0; /* default false for FIPS cases */ |
1190 | | #else |
1191 | 0 | allowFlag = 1; /* default true for all non-FIPS cases */ |
1192 | 0 | #endif |
1193 | 0 | return wc_HmacSetKey_ex(hmac, type, key, length, allowFlag); |
1194 | 0 | } |
1195 | | |
1196 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_PRIVATE_KEY_ID) |
1197 | | /* A key named by id or label has no key bytes for software to use. */ |
1198 | | static int HmacKeyNotInSoftware(const Hmac* hmac) |
1199 | | { |
1200 | | return ((hmac->idLen > 0) || (hmac->labelLen > 0)) && |
1201 | | (hmac->keyRaw == NULL); |
1202 | | } |
1203 | | #endif |
1204 | | |
1205 | | #ifdef WC_HMAC_HAVE_KEY_LOCATION |
1206 | | /* Records where the key is kept, for wc_CryptoCb_SetKeyLocation(). |
1207 | | * WC_KEY_HW_STORED makes software Update and Final fail with MISSING_KEY. |
1208 | | * Returns 0, or BAD_FUNC_ARG for an unknown location. */ |
1209 | | int wc_HmacSetKeyLocation(Hmac* hmac, int location) |
1210 | | { |
1211 | | if (location != WC_KEY_SW_STORED && location != WC_KEY_HW_STORED) { |
1212 | | return BAD_FUNC_ARG; |
1213 | | } |
1214 | | hmac->keyLocation = (byte)location; |
1215 | | return 0; |
1216 | | } |
1217 | | #endif |
1218 | | |
1219 | | int wc_HmacUpdate(Hmac* hmac, const byte* msg, word32 length) |
1220 | 0 | { |
1221 | 0 | int ret = 0; |
1222 | |
|
1223 | 0 | if (hmac == NULL || (msg == NULL && length > 0)) { |
1224 | 0 | return BAD_FUNC_ARG; |
1225 | 0 | } |
1226 | 0 | if (length == 0) { |
1227 | 0 | return 0; /* nothing to do, return success */ |
1228 | 0 | } |
1229 | | |
1230 | | #ifdef WOLF_CRYPTO_CB |
1231 | | if (WC_HMAC_USE_CRYPTOCB(hmac)) { |
1232 | | ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, msg, length, NULL); |
1233 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1234 | | return ret; |
1235 | | /* fall-through when unavailable */ |
1236 | | } |
1237 | | #endif |
1238 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_PRIVATE_KEY_ID) |
1239 | | if (HmacKeyNotInSoftware(hmac)) |
1240 | | return MISSING_KEY; |
1241 | | #endif |
1242 | | #ifdef WC_HMAC_HAVE_KEY_LOCATION |
1243 | | /* The key is on the device only: software has no ipad/opad to use. */ |
1244 | | if (hmac->keyLocation == WC_KEY_HW_STORED) { |
1245 | | return MISSING_KEY; |
1246 | | } |
1247 | | #endif |
1248 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) |
1249 | | if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) { |
1250 | | #if defined(HAVE_CAVIUM) |
1251 | | return NitroxHmacUpdate(hmac, msg, length); |
1252 | | #elif defined(HAVE_INTEL_QA) |
1253 | | if (IntelQaHmacGetType(hmac->macType, NULL) == 0) { |
1254 | | return IntelQaHmac(&hmac->asyncDev, hmac->macType, |
1255 | | (byte*)hmac->ipad, hmac->keyLen, NULL, msg, length); |
1256 | | } |
1257 | | #endif |
1258 | | } |
1259 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
1260 | | |
1261 | | #if defined(STM32_HASH) && defined(STM32_HMAC) |
1262 | | if (hmac->innerHashKeyed == WC_HMAC_INNER_HASH_KEYED_DEV) { |
1263 | | ret = wolfSSL_CryptHwMutexLock(); |
1264 | | if (ret == 0) { |
1265 | | ret = wc_Stm32_Hmac_Update(&hmac->stmCtx, hmac->stmAlgo, |
1266 | | msg, length, hmac->stmBlockSize); |
1267 | | wolfSSL_CryptHwMutexUnLock(); |
1268 | | } |
1269 | | return ret; |
1270 | | } |
1271 | | #endif /* STM32_HASH && STM32_HMAC */ |
1272 | | |
1273 | 0 | if (!hmac->innerHashKeyed) { |
1274 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1275 | 0 | ret = HmacKeyHashUpdate(hmac->macType, &hmac->hash, (byte*)hmac->ipad); |
1276 | | #else |
1277 | | ret = HmacKeyCopyHash(hmac->macType, &hmac->i_hash, &hmac->hash); |
1278 | | #endif |
1279 | 0 | if (ret != 0) |
1280 | 0 | return ret; |
1281 | 0 | hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_SW; |
1282 | 0 | } |
1283 | | |
1284 | 0 | switch (hmac->macType) { |
1285 | | #ifndef NO_MD5 |
1286 | | case WC_MD5: |
1287 | | ret = wc_Md5Update(&hmac->hash.md5, msg, length); |
1288 | | break; |
1289 | | #endif /* !NO_MD5 */ |
1290 | | |
1291 | 0 | #ifndef NO_SHA |
1292 | 0 | case WC_SHA: |
1293 | 0 | ret = wc_ShaUpdate(&hmac->hash.sha, msg, length); |
1294 | 0 | break; |
1295 | 0 | #endif /* !NO_SHA */ |
1296 | | |
1297 | 0 | #ifdef WOLFSSL_SHA224 |
1298 | 0 | case WC_SHA224: |
1299 | 0 | ret = wc_Sha224Update(&hmac->hash.sha224, msg, length); |
1300 | 0 | break; |
1301 | 0 | #endif /* WOLFSSL_SHA224 */ |
1302 | | |
1303 | 0 | #ifndef NO_SHA256 |
1304 | 0 | case WC_SHA256: |
1305 | 0 | ret = wc_Sha256Update(&hmac->hash.sha256, msg, length); |
1306 | 0 | break; |
1307 | 0 | #endif /* !NO_SHA256 */ |
1308 | | |
1309 | 0 | #ifdef WOLFSSL_SHA384 |
1310 | 0 | case WC_SHA384: |
1311 | 0 | ret = wc_Sha384Update(&hmac->hash.sha384, msg, length); |
1312 | 0 | break; |
1313 | 0 | #endif /* WOLFSSL_SHA384 */ |
1314 | 0 | #ifdef WOLFSSL_SHA512 |
1315 | 0 | case WC_SHA512: |
1316 | 0 | ret = wc_Sha512Update(&hmac->hash.sha512, msg, length); |
1317 | 0 | break; |
1318 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
1319 | 0 | case WC_SHA512_224: |
1320 | 0 | ret = wc_Sha512_224Update(&hmac->hash.sha512, msg, length); |
1321 | 0 | break; |
1322 | 0 | #endif |
1323 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
1324 | 0 | case WC_SHA512_256: |
1325 | 0 | ret = wc_Sha512_256Update(&hmac->hash.sha512, msg, length); |
1326 | 0 | break; |
1327 | 0 | #endif |
1328 | 0 | #endif /* WOLFSSL_SHA512 */ |
1329 | | |
1330 | 0 | #ifdef WOLFSSL_SHA3 |
1331 | 0 | #ifndef WOLFSSL_NOSHA3_224 |
1332 | 0 | case WC_SHA3_224: |
1333 | 0 | ret = wc_Sha3_224_Update(&hmac->hash.sha3, msg, length); |
1334 | 0 | break; |
1335 | 0 | #endif |
1336 | 0 | #ifndef WOLFSSL_NOSHA3_256 |
1337 | 0 | case WC_SHA3_256: |
1338 | 0 | ret = wc_Sha3_256_Update(&hmac->hash.sha3, msg, length); |
1339 | 0 | break; |
1340 | 0 | #endif |
1341 | 0 | #ifndef WOLFSSL_NOSHA3_384 |
1342 | 0 | case WC_SHA3_384: |
1343 | 0 | ret = wc_Sha3_384_Update(&hmac->hash.sha3, msg, length); |
1344 | 0 | break; |
1345 | 0 | #endif |
1346 | 0 | #ifndef WOLFSSL_NOSHA3_512 |
1347 | 0 | case WC_SHA3_512: |
1348 | 0 | ret = wc_Sha3_512_Update(&hmac->hash.sha3, msg, length); |
1349 | 0 | break; |
1350 | 0 | #endif |
1351 | 0 | #endif /* WOLFSSL_SHA3 */ |
1352 | | |
1353 | | #ifdef WOLFSSL_SM3 |
1354 | | case WC_SM3: |
1355 | | ret = wc_Sm3Update(&hmac->hash.sm3, msg, length); |
1356 | | break; |
1357 | | #endif |
1358 | | |
1359 | 0 | default: |
1360 | 0 | ret = BAD_FUNC_ARG; |
1361 | 0 | break; |
1362 | 0 | } |
1363 | | |
1364 | 0 | return ret; |
1365 | 0 | } |
1366 | | |
1367 | | |
1368 | | int wc_HmacFinal(Hmac* hmac, byte* hash) |
1369 | 0 | { |
1370 | 0 | int ret; |
1371 | |
|
1372 | 0 | if (hmac == NULL || hash == NULL) { |
1373 | 0 | return BAD_FUNC_ARG; |
1374 | 0 | } |
1375 | | |
1376 | | #ifdef WOLF_CRYPTO_CB |
1377 | | if (WC_HMAC_USE_CRYPTOCB(hmac)) { |
1378 | | ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, NULL, 0, hash); |
1379 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1380 | | return ret; |
1381 | | /* fall-through when unavailable */ |
1382 | | } |
1383 | | #endif |
1384 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_PRIVATE_KEY_ID) |
1385 | | if (HmacKeyNotInSoftware(hmac)) |
1386 | | return MISSING_KEY; |
1387 | | #endif |
1388 | | #ifdef WC_HMAC_HAVE_KEY_LOCATION |
1389 | | /* The key is on the device only: software has no ipad/opad to use. */ |
1390 | | if (hmac->keyLocation == WC_KEY_HW_STORED) { |
1391 | | return MISSING_KEY; |
1392 | | } |
1393 | | #endif |
1394 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) |
1395 | | if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) { |
1396 | | int hashLen = wc_HmacSizeByType(hmac->macType); |
1397 | | if (hashLen <= 0) |
1398 | | return hashLen; |
1399 | | |
1400 | | #if defined(HAVE_CAVIUM) |
1401 | | return NitroxHmacFinal(hmac, hash, hashLen); |
1402 | | #elif defined(HAVE_INTEL_QA) |
1403 | | if (IntelQaHmacGetType(hmac->macType, NULL) == 0) { |
1404 | | return IntelQaHmac(&hmac->asyncDev, hmac->macType, |
1405 | | (byte*)hmac->ipad, hmac->keyLen, hash, NULL, hashLen); |
1406 | | } |
1407 | | #endif |
1408 | | } |
1409 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
1410 | | |
1411 | | #if defined(STM32_HASH) && defined(STM32_HMAC) |
1412 | | if (hmac->innerHashKeyed == WC_HMAC_INNER_HASH_KEYED_DEV) { |
1413 | | ret = wolfSSL_CryptHwMutexLock(); |
1414 | | if (ret == 0) { |
1415 | | ret = wc_Stm32_Hmac_Final(&hmac->stmCtx, hmac->stmAlgo, |
1416 | | (const byte*)hmac->ipad, hmac->stmKeyLen, hash, |
1417 | | hmac->stmDigestSize); |
1418 | | /* Re-run Phase 1 so HMAC is ready for next Update/Final cycle |
1419 | | * (needed for PRF/HKDF loops that reuse the same key) */ |
1420 | | if (ret == 0) { |
1421 | | ret = wc_Stm32_Hmac_SetKey(&hmac->stmCtx, hmac->macType, |
1422 | | (const byte*)hmac->ipad, hmac->stmKeyLen); |
1423 | | } |
1424 | | wolfSSL_CryptHwMutexUnLock(); |
1425 | | } |
1426 | | return ret; |
1427 | | } |
1428 | | #endif /* STM32_HASH && STM32_HMAC */ |
1429 | | |
1430 | 0 | if (!hmac->innerHashKeyed) { |
1431 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1432 | 0 | ret = HmacKeyHashUpdate(hmac->macType, &hmac->hash, (byte*)hmac->ipad); |
1433 | | #else |
1434 | | ret = HmacKeyCopyHash(hmac->macType, &hmac->i_hash, &hmac->hash); |
1435 | | #endif |
1436 | 0 | if (ret != 0) |
1437 | 0 | return ret; |
1438 | 0 | hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_SW; |
1439 | 0 | } |
1440 | | |
1441 | 0 | switch (hmac->macType) { |
1442 | | #ifndef NO_MD5 |
1443 | | case WC_MD5: |
1444 | | ret = wc_Md5Final(&hmac->hash.md5, (byte*)hmac->innerHash); |
1445 | | if (ret != 0) |
1446 | | break; |
1447 | | #ifndef WOLFSSL_HMAC_COPY_HASH |
1448 | | ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->opad, |
1449 | | WC_MD5_BLOCK_SIZE); |
1450 | | #else |
1451 | | ret = HmacKeyCopyHash(WC_MD5, &hmac->o_hash, &hmac->hash); |
1452 | | #endif |
1453 | | if (ret != 0) |
1454 | | break; |
1455 | | ret = wc_Md5Update(&hmac->hash.md5, (byte*)hmac->innerHash, |
1456 | | WC_MD5_DIGEST_SIZE); |
1457 | | if (ret != 0) |
1458 | | break; |
1459 | | ret = wc_Md5Final(&hmac->hash.md5, hash); |
1460 | | break; |
1461 | | #endif /* !NO_MD5 */ |
1462 | | |
1463 | 0 | #ifndef NO_SHA |
1464 | 0 | case WC_SHA: |
1465 | 0 | ret = wc_ShaFinal(&hmac->hash.sha, (byte*)hmac->innerHash); |
1466 | 0 | if (ret != 0) |
1467 | 0 | break; |
1468 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1469 | 0 | ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->opad, |
1470 | 0 | WC_SHA_BLOCK_SIZE); |
1471 | | #else |
1472 | | ret = HmacKeyCopyHash(WC_SHA, &hmac->o_hash, &hmac->hash); |
1473 | | #endif |
1474 | 0 | if (ret != 0) |
1475 | 0 | break; |
1476 | 0 | ret = wc_ShaUpdate(&hmac->hash.sha, (byte*)hmac->innerHash, |
1477 | 0 | WC_SHA_DIGEST_SIZE); |
1478 | 0 | if (ret != 0) |
1479 | 0 | break; |
1480 | 0 | ret = wc_ShaFinal(&hmac->hash.sha, hash); |
1481 | 0 | break; |
1482 | 0 | #endif /* !NO_SHA */ |
1483 | | |
1484 | 0 | #ifdef WOLFSSL_SHA224 |
1485 | 0 | case WC_SHA224: |
1486 | 0 | ret = wc_Sha224Final(&hmac->hash.sha224, (byte*)hmac->innerHash); |
1487 | 0 | if (ret != 0) |
1488 | 0 | break; |
1489 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1490 | 0 | ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->opad, |
1491 | 0 | WC_SHA224_BLOCK_SIZE); |
1492 | | #else |
1493 | | ret = HmacKeyCopyHash(WC_SHA224, &hmac->o_hash, &hmac->hash); |
1494 | | #endif |
1495 | 0 | if (ret != 0) |
1496 | 0 | break; |
1497 | 0 | ret = wc_Sha224Update(&hmac->hash.sha224, (byte*)hmac->innerHash, |
1498 | 0 | WC_SHA224_DIGEST_SIZE); |
1499 | 0 | if (ret != 0) |
1500 | 0 | break; |
1501 | 0 | ret = wc_Sha224Final(&hmac->hash.sha224, hash); |
1502 | 0 | if (ret != 0) |
1503 | 0 | break; |
1504 | 0 | break; |
1505 | 0 | #endif /* WOLFSSL_SHA224 */ |
1506 | 0 | #ifndef NO_SHA256 |
1507 | 0 | case WC_SHA256: |
1508 | 0 | ret = wc_Sha256Final(&hmac->hash.sha256, (byte*)hmac->innerHash); |
1509 | 0 | if (ret != 0) |
1510 | 0 | break; |
1511 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1512 | 0 | ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->opad, |
1513 | 0 | WC_SHA256_BLOCK_SIZE); |
1514 | | #else |
1515 | | ret = HmacKeyCopyHash(WC_SHA256, &hmac->o_hash, &hmac->hash); |
1516 | | #endif |
1517 | 0 | if (ret != 0) |
1518 | 0 | break; |
1519 | 0 | ret = wc_Sha256Update(&hmac->hash.sha256, (byte*)hmac->innerHash, |
1520 | 0 | WC_SHA256_DIGEST_SIZE); |
1521 | 0 | if (ret != 0) |
1522 | 0 | break; |
1523 | 0 | ret = wc_Sha256Final(&hmac->hash.sha256, hash); |
1524 | 0 | break; |
1525 | 0 | #endif /* !NO_SHA256 */ |
1526 | | |
1527 | 0 | #ifdef WOLFSSL_SHA384 |
1528 | 0 | case WC_SHA384: |
1529 | 0 | ret = wc_Sha384Final(&hmac->hash.sha384, (byte*)hmac->innerHash); |
1530 | 0 | if (ret != 0) |
1531 | 0 | break; |
1532 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1533 | 0 | ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->opad, |
1534 | 0 | WC_SHA384_BLOCK_SIZE); |
1535 | | #else |
1536 | | ret = HmacKeyCopyHash(WC_SHA384, &hmac->o_hash, &hmac->hash); |
1537 | | #endif |
1538 | 0 | if (ret != 0) |
1539 | 0 | break; |
1540 | 0 | ret = wc_Sha384Update(&hmac->hash.sha384, (byte*)hmac->innerHash, |
1541 | 0 | WC_SHA384_DIGEST_SIZE); |
1542 | 0 | if (ret != 0) |
1543 | 0 | break; |
1544 | 0 | ret = wc_Sha384Final(&hmac->hash.sha384, hash); |
1545 | 0 | break; |
1546 | 0 | #endif /* WOLFSSL_SHA384 */ |
1547 | 0 | #ifdef WOLFSSL_SHA512 |
1548 | 0 | case WC_SHA512: |
1549 | 0 | ret = wc_Sha512Final(&hmac->hash.sha512, (byte*)hmac->innerHash); |
1550 | 0 | if (ret != 0) |
1551 | 0 | break; |
1552 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1553 | 0 | ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->opad, |
1554 | 0 | WC_SHA512_BLOCK_SIZE); |
1555 | | #else |
1556 | | ret = HmacKeyCopyHash(WC_SHA512, &hmac->o_hash, &hmac->hash); |
1557 | | #endif |
1558 | 0 | if (ret != 0) |
1559 | 0 | break; |
1560 | 0 | ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->innerHash, |
1561 | 0 | WC_SHA512_DIGEST_SIZE); |
1562 | 0 | if (ret != 0) |
1563 | 0 | break; |
1564 | 0 | ret = wc_Sha512Final(&hmac->hash.sha512, hash); |
1565 | 0 | break; |
1566 | 0 | #ifndef WOLFSSL_NOSHA512_224 |
1567 | 0 | case WC_SHA512_224: |
1568 | 0 | ret = wc_Sha512_224Final(&hmac->hash.sha512, |
1569 | 0 | (byte*)hmac->innerHash); |
1570 | 0 | if (ret != 0) |
1571 | 0 | break; |
1572 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1573 | 0 | ret = wc_Sha512_224Update(&hmac->hash.sha512, (byte*)hmac->opad, |
1574 | 0 | WC_SHA512_224_BLOCK_SIZE); |
1575 | | #else |
1576 | | ret = HmacKeyCopyHash(WC_SHA512_224, &hmac->o_hash, &hmac->hash); |
1577 | | #endif |
1578 | 0 | if (ret != 0) |
1579 | 0 | break; |
1580 | 0 | ret = wc_Sha512_224Update(&hmac->hash.sha512, |
1581 | 0 | (byte*)hmac->innerHash, WC_SHA512_224_DIGEST_SIZE); |
1582 | 0 | if (ret != 0) |
1583 | 0 | break; |
1584 | 0 | ret = wc_Sha512_224Final(&hmac->hash.sha512, hash); |
1585 | 0 | break; |
1586 | 0 | #endif |
1587 | 0 | #ifndef WOLFSSL_NOSHA512_256 |
1588 | 0 | case WC_SHA512_256: |
1589 | 0 | ret = wc_Sha512_256Final(&hmac->hash.sha512, |
1590 | 0 | (byte*)hmac->innerHash); |
1591 | 0 | if (ret != 0) |
1592 | 0 | break; |
1593 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1594 | 0 | ret = wc_Sha512_256Update(&hmac->hash.sha512, (byte*)hmac->opad, |
1595 | 0 | WC_SHA512_256_BLOCK_SIZE); |
1596 | | #else |
1597 | | ret = HmacKeyCopyHash(WC_SHA512_256, &hmac->o_hash, &hmac->hash); |
1598 | | #endif |
1599 | 0 | if (ret != 0) |
1600 | 0 | break; |
1601 | 0 | ret = wc_Sha512_256Update(&hmac->hash.sha512, |
1602 | 0 | (byte*)hmac->innerHash, WC_SHA512_256_DIGEST_SIZE); |
1603 | 0 | if (ret != 0) |
1604 | 0 | break; |
1605 | 0 | ret = wc_Sha512_256Final(&hmac->hash.sha512, hash); |
1606 | 0 | break; |
1607 | 0 | #endif |
1608 | 0 | #endif /* WOLFSSL_SHA512 */ |
1609 | | |
1610 | 0 | #ifdef WOLFSSL_SHA3 |
1611 | 0 | #ifndef WOLFSSL_NOSHA3_224 |
1612 | 0 | case WC_SHA3_224: |
1613 | 0 | ret = wc_Sha3_224_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); |
1614 | 0 | if (ret != 0) |
1615 | 0 | break; |
1616 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1617 | 0 | ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->opad, |
1618 | 0 | WC_SHA3_224_BLOCK_SIZE); |
1619 | | #else |
1620 | | ret = HmacKeyCopyHash(WC_SHA3_224, &hmac->o_hash, &hmac->hash); |
1621 | | #endif |
1622 | 0 | if (ret != 0) |
1623 | 0 | break; |
1624 | 0 | ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, |
1625 | 0 | WC_SHA3_224_DIGEST_SIZE); |
1626 | 0 | if (ret != 0) |
1627 | 0 | break; |
1628 | 0 | ret = wc_Sha3_224_Final(&hmac->hash.sha3, hash); |
1629 | 0 | break; |
1630 | 0 | #endif |
1631 | 0 | #ifndef WOLFSSL_NOSHA3_256 |
1632 | 0 | case WC_SHA3_256: |
1633 | 0 | ret = wc_Sha3_256_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); |
1634 | 0 | if (ret != 0) |
1635 | 0 | break; |
1636 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1637 | 0 | ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->opad, |
1638 | 0 | WC_SHA3_256_BLOCK_SIZE); |
1639 | | #else |
1640 | | ret = HmacKeyCopyHash(WC_SHA3_256, &hmac->o_hash, &hmac->hash); |
1641 | | #endif |
1642 | 0 | if (ret != 0) |
1643 | 0 | break; |
1644 | 0 | ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, |
1645 | 0 | WC_SHA3_256_DIGEST_SIZE); |
1646 | 0 | if (ret != 0) |
1647 | 0 | break; |
1648 | 0 | ret = wc_Sha3_256_Final(&hmac->hash.sha3, hash); |
1649 | 0 | break; |
1650 | 0 | #endif |
1651 | 0 | #ifndef WOLFSSL_NOSHA3_384 |
1652 | 0 | case WC_SHA3_384: |
1653 | 0 | ret = wc_Sha3_384_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); |
1654 | 0 | if (ret != 0) |
1655 | 0 | break; |
1656 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1657 | 0 | ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->opad, |
1658 | 0 | WC_SHA3_384_BLOCK_SIZE); |
1659 | | #else |
1660 | | ret = HmacKeyCopyHash(WC_SHA3_384, &hmac->o_hash, &hmac->hash); |
1661 | | #endif |
1662 | 0 | if (ret != 0) |
1663 | 0 | break; |
1664 | 0 | ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, |
1665 | 0 | WC_SHA3_384_DIGEST_SIZE); |
1666 | 0 | if (ret != 0) |
1667 | 0 | break; |
1668 | 0 | ret = wc_Sha3_384_Final(&hmac->hash.sha3, hash); |
1669 | 0 | break; |
1670 | 0 | #endif |
1671 | 0 | #ifndef WOLFSSL_NOSHA3_512 |
1672 | 0 | case WC_SHA3_512: |
1673 | 0 | ret = wc_Sha3_512_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); |
1674 | 0 | if (ret != 0) |
1675 | 0 | break; |
1676 | 0 | #ifndef WOLFSSL_HMAC_COPY_HASH |
1677 | 0 | ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->opad, |
1678 | 0 | WC_SHA3_512_BLOCK_SIZE); |
1679 | | #else |
1680 | | ret = HmacKeyCopyHash(WC_SHA3_512, &hmac->o_hash, &hmac->hash); |
1681 | | #endif |
1682 | 0 | if (ret != 0) |
1683 | 0 | break; |
1684 | 0 | ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, |
1685 | 0 | WC_SHA3_512_DIGEST_SIZE); |
1686 | 0 | if (ret != 0) |
1687 | 0 | break; |
1688 | 0 | ret = wc_Sha3_512_Final(&hmac->hash.sha3, hash); |
1689 | 0 | break; |
1690 | 0 | #endif |
1691 | 0 | #endif /* WOLFSSL_SHA3 */ |
1692 | | |
1693 | | #ifdef WOLFSSL_SM3 |
1694 | | case WC_SM3: |
1695 | | ret = wc_Sm3Final(&hmac->hash.sm3, (byte*)hmac->innerHash); |
1696 | | if (ret != 0) |
1697 | | break; |
1698 | | #ifndef WOLFSSL_HMAC_COPY_HASH |
1699 | | ret = wc_Sm3Update(&hmac->hash.sm3, (byte*)hmac->opad, |
1700 | | WC_SM3_BLOCK_SIZE); |
1701 | | #else |
1702 | | ret = HmacKeyCopyHash(WC_SM3, &hmac->o_hash, &hmac->hash); |
1703 | | #endif |
1704 | | if (ret != 0) |
1705 | | break; |
1706 | | ret = wc_Sm3Update(&hmac->hash.sm3, (byte*)hmac->innerHash, |
1707 | | WC_SM3_DIGEST_SIZE); |
1708 | | if (ret != 0) |
1709 | | break; |
1710 | | ret = wc_Sm3Final(&hmac->hash.sm3, hash); |
1711 | | break; |
1712 | | #endif |
1713 | | |
1714 | 0 | default: |
1715 | 0 | ret = BAD_FUNC_ARG; |
1716 | 0 | break; |
1717 | 0 | } |
1718 | | |
1719 | 0 | if (ret == 0) { |
1720 | 0 | hmac->innerHashKeyed = 0; |
1721 | 0 | } |
1722 | |
|
1723 | 0 | return ret; |
1724 | 0 | } |
1725 | | |
1726 | | #ifdef WOLFSSL_KCAPI_HMAC |
1727 | | /* implemented in wolfcrypt/src/port/kcapi/kcapi_hmac.c */ |
1728 | | |
1729 | | /* unmap the _Software calls used by kcapi_hmac.c */ |
1730 | | #undef wc_HmacSetKey |
1731 | | #undef wc_HmacSetKey_ex |
1732 | | #undef wc_HmacUpdate |
1733 | | #undef wc_HmacFinal |
1734 | | |
1735 | | #else |
1736 | | /* Initialize Hmac for use with async device */ |
1737 | | int wc_HmacInit(Hmac* hmac, void* heap, int devId) |
1738 | 0 | { |
1739 | 0 | int ret = 0; |
1740 | |
|
1741 | 0 | if (hmac == NULL) |
1742 | 0 | return BAD_FUNC_ARG; |
1743 | 0 | #ifdef WC_CTX_IS_ALIGNED |
1744 | 0 | if (!WC_CTX_IS_ALIGNED(hmac, Hmac)) |
1745 | 0 | return BAD_ALIGN_E; |
1746 | 0 | #endif |
1747 | | |
1748 | 0 | XMEMSET(hmac, 0, sizeof(Hmac)); |
1749 | 0 | hmac->macType = WC_HASH_TYPE_NONE; |
1750 | 0 | hmac->heap = heap; |
1751 | | #ifdef WOLF_CRYPTO_CB |
1752 | | hmac->devId = devId; |
1753 | | hmac->devCtx = NULL; |
1754 | | #endif |
1755 | | #if defined(WOLFSSL_DEVCRYPTO_HMAC) |
1756 | | hmac->ctx.inited = 0; |
1757 | | hmac->ctx.cfd = -1; |
1758 | | #endif |
1759 | |
|
1760 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) |
1761 | | ret = wolfAsync_DevCtxInit(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC, |
1762 | | hmac->heap, devId); |
1763 | | #else |
1764 | 0 | (void)devId; |
1765 | 0 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
1766 | |
|
1767 | 0 | return ret; |
1768 | 0 | } |
1769 | | |
1770 | | #ifdef WOLF_PRIVATE_KEY_ID |
1771 | | int wc_HmacInit_Id(Hmac* hmac, unsigned char* id, int len, void* heap, |
1772 | | int devId) |
1773 | | { |
1774 | | int ret = 0; |
1775 | | |
1776 | | if (hmac == NULL || (id == NULL && len > 0)) |
1777 | | ret = BAD_FUNC_ARG; |
1778 | | if (ret == 0 && (len < 0 || len > HMAC_MAX_ID_LEN)) |
1779 | | ret = BUFFER_E; |
1780 | | |
1781 | | if (ret == 0) |
1782 | | ret = wc_HmacInit(hmac, heap, devId); |
1783 | | if (ret == 0 && id != NULL && len != 0) { |
1784 | | XMEMCPY(hmac->id, id, (size_t)len); |
1785 | | hmac->idLen = len; |
1786 | | } |
1787 | | |
1788 | | return ret; |
1789 | | } |
1790 | | |
1791 | | int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap, int devId) |
1792 | | { |
1793 | | int ret = 0; |
1794 | | int labelLen = 0; |
1795 | | |
1796 | | if (hmac == NULL || label == NULL) |
1797 | | ret = BAD_FUNC_ARG; |
1798 | | if (ret == 0) { |
1799 | | labelLen = (int)XSTRLEN(label); |
1800 | | if (labelLen == 0 || labelLen > HMAC_MAX_LABEL_LEN) |
1801 | | ret = BUFFER_E; |
1802 | | } |
1803 | | |
1804 | | if (ret == 0) |
1805 | | ret = wc_HmacInit(hmac, heap, devId); |
1806 | | if (ret == 0) { |
1807 | | XMEMCPY(hmac->label, label, (size_t)labelLen); |
1808 | | hmac->labelLen = labelLen; |
1809 | | } |
1810 | | |
1811 | | return ret; |
1812 | | } |
1813 | | #endif /* WOLF_PRIVATE_KEY_ID */ |
1814 | | |
1815 | | /* Free Hmac from use with async device */ |
1816 | | void wc_HmacFree(Hmac* hmac) |
1817 | 0 | { |
1818 | 0 | if (hmac == NULL) |
1819 | 0 | return; |
1820 | 0 | #ifdef WC_CTX_IS_ALIGNED |
1821 | 0 | if (!WC_CTX_IS_ALIGNED(hmac, Hmac)) { |
1822 | 0 | ForceZero(hmac, sizeof(*hmac)); |
1823 | 0 | return; |
1824 | 0 | } |
1825 | 0 | #endif |
1826 | | |
1827 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE) |
1828 | | /* Let a device release any per-context state it hung off devCtx directly. If |
1829 | | * a device handles it, devCtx is cleared and the finalize fallback below is |
1830 | | * skipped; otherwise this is a no-op and the fallback still runs. */ |
1831 | | #ifndef WOLF_CRYPTO_CB_FIND |
1832 | | if (hmac->devId != INVALID_DEVID && hmac->devCtx != NULL) |
1833 | | #else |
1834 | | if (hmac->devCtx != NULL) |
1835 | | #endif |
1836 | | { |
1837 | | (void)wc_CryptoCb_Free(hmac->devId, WC_ALGO_TYPE_HMAC, hmac->macType, 0, |
1838 | | (void*)hmac); |
1839 | | } |
1840 | | #endif |
1841 | | |
1842 | | #ifdef WOLF_CRYPTO_CB |
1843 | | /* cleanup when final was not called; devCtx is set only by a device, so |
1844 | | * under WOLF_CRYPTO_CB_FIND it alone marks an op still owned by one. */ |
1845 | | if (WC_HMAC_USE_CRYPTOCB(hmac) && hmac->devCtx != NULL) { |
1846 | | int ret; |
1847 | | byte finalHash[WC_HMAC_BLOCK_SIZE]; |
1848 | | ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, NULL, 0, |
1849 | | finalHash); |
1850 | | (void)ret; /* must ignore return code here */ |
1851 | | (void)finalHash; |
1852 | | } |
1853 | | #endif |
1854 | | |
1855 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) |
1856 | | wolfAsync_DevCtxFree(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC); |
1857 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
1858 | | |
1859 | 0 | HmacKeyFreeHash(hmac->macType, &hmac->hash); |
1860 | | #ifdef WOLFSSL_HMAC_COPY_HASH |
1861 | | HmacKeyFreeHash(hmac->macType, &hmac->i_hash); |
1862 | | HmacKeyFreeHash(hmac->macType, &hmac->o_hash); |
1863 | | #endif |
1864 | |
|
1865 | 0 | ForceZero(hmac, sizeof(*hmac)); |
1866 | 0 | } |
1867 | | #endif /* WOLFSSL_KCAPI_HMAC */ |
1868 | | |
1869 | | int wolfSSL_GetHmacMaxSize(void) |
1870 | 0 | { |
1871 | 0 | return WC_MAX_DIGEST_SIZE; |
1872 | 0 | } |
1873 | | |
1874 | | /* Wait out an async HMAC sub-op: the one-shot and HKDF callers cannot resume |
1875 | | * mid-chain. QAT/Cavium only (not covered by CI); a crypto callback pending |
1876 | | * must instead propagate so the caller can re-invoke. */ |
1877 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) && \ |
1878 | | (defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)) |
1879 | | /* QAT/Cavium only: WOLFSSL_ASYNC_REINVOKE is never defined for these |
1880 | | * backends (see internal.h), so an HMAC sub-op can only pend on the |
1881 | | * hardware device, never on a re-invokable crypto callback. Wait |
1882 | | * unconditionally: the callers cannot resume mid-chain. */ |
1883 | | #define HMAC_SUBOP_WAIT(ret, hmac) \ |
1884 | | do { \ |
1885 | | (ret) = wc_AsyncWait((ret), &(hmac)->asyncDev, \ |
1886 | | WC_ASYNC_FLAG_NONE); \ |
1887 | | } while (0) |
1888 | | #else |
1889 | | /* No HMAC device to wait on, or the pending must reach the caller |
1890 | | * (crypto callback re-invocation); hmac is unevaluated. */ |
1891 | 0 | #define HMAC_SUBOP_WAIT(ret, hmac) WC_DO_NOTHING |
1892 | | #endif |
1893 | | |
1894 | | /* One-shot HMAC for use with a crypto callback device. Without one, use |
1895 | | * wc_HmacGenerate(). |
1896 | | * |
1897 | | * The device is offered the whole operation in one call, with the key in the |
1898 | | * call. If it declines, the key is set and the device gets the update and |
1899 | | * final calls. |
1900 | | * |
1901 | | * With a NULL key, hmac must be initialized by the caller. A NULL key is only |
1902 | | * for a key on the device, which hmac then names, see wc_HmacInit_Id() and |
1903 | | * wc_HmacInit_Label(). If no device computes the tag, the call fails with |
1904 | | * MISSING_KEY. A NULL key is never used as a key of zero length. |
1905 | | * |
1906 | | * hmac Storage for the HMAC context. Initialized and freed here, unless |
1907 | | * key is NULL. Then the caller initializes it and frees it. |
1908 | | * type The hash algorithm type. |
1909 | | * out Buffer to hold the tag. |
1910 | | * outSz On input, the size of out (at least the digest size). On output, |
1911 | | * the digest size. |
1912 | | * in The message. |
1913 | | * inSz The size of the message. |
1914 | | * key The HMAC key, or NULL to use the key on the device. For a key of |
1915 | | * zero length, give a key pointer and a keySz of 0. |
1916 | | * keySz The size of the key. |
1917 | | * heap The heap hint. |
1918 | | * devId The device ID. |
1919 | | * returns 0 on success, BUFFER_E when out is too small, otherwise failure. |
1920 | | */ |
1921 | | int wc_HmacGenerate_ex(Hmac* hmac, int type, byte* out, word32* outSz, |
1922 | | const byte* in, word32 inSz, |
1923 | | const byte* key, word32 keySz, |
1924 | | void* heap, int devId) |
1925 | 0 | { |
1926 | 0 | int ret; |
1927 | 0 | word32 digestSz; |
1928 | | /* With no key, the caller initialized the context. */ |
1929 | 0 | int callerCtx = (key == NULL); |
1930 | | #ifdef WOLF_CRYPTO_CB |
1931 | | const byte noInput = 0; |
1932 | | #endif |
1933 | |
|
1934 | 0 | if (hmac == NULL || out == NULL || outSz == NULL || |
1935 | 0 | (in == NULL && inSz != 0) || (key == NULL && keySz != 0)) { |
1936 | 0 | return BAD_FUNC_ARG; |
1937 | 0 | } |
1938 | | |
1939 | 0 | ret = wc_HmacSizeByType(type); |
1940 | 0 | if (ret < 0) { |
1941 | 0 | return ret; |
1942 | 0 | } |
1943 | 0 | digestSz = (word32)ret; |
1944 | 0 | if (*outSz < digestSz) { |
1945 | 0 | return BUFFER_E; |
1946 | 0 | } |
1947 | | |
1948 | | #ifdef WOLF_CRYPTO_CB |
1949 | | /* The device ID of the call is used, not the one of the context. */ |
1950 | | hmac->devId = devId; |
1951 | | #ifndef WOLF_CRYPTO_CB_FIND |
1952 | | if (devId != INVALID_DEVID) |
1953 | | #endif |
1954 | | { |
1955 | | /* Offer the whole operation before the context is set up. The call |
1956 | | * always has an input pointer, so that it differs from a final. */ |
1957 | | ret = wc_CryptoCb_Hmac(hmac, type, key, keySz, |
1958 | | (in != NULL) ? in : &noInput, inSz, out); |
1959 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
1960 | | if (ret == 0) { |
1961 | | *outSz = digestSz; |
1962 | | } |
1963 | | return ret; |
1964 | | } |
1965 | | /* fall-through when unavailable */ |
1966 | | } |
1967 | | #endif |
1968 | | |
1969 | 0 | if (callerCtx) { |
1970 | | /* No device computed the tag. Without a key named by id or label, |
1971 | | * fail here so that an empty key is never set. */ |
1972 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_PRIVATE_KEY_ID) |
1973 | | if ((hmac->idLen <= 0) && (hmac->labelLen <= 0)) |
1974 | | #endif |
1975 | 0 | { |
1976 | 0 | return MISSING_KEY; |
1977 | 0 | } |
1978 | 0 | } |
1979 | | |
1980 | 0 | ret = 0; |
1981 | 0 | if (!callerCtx) { |
1982 | 0 | ret = wc_HmacInit(hmac, heap, devId); |
1983 | 0 | } |
1984 | 0 | if (ret == 0) { |
1985 | 0 | ret = wc_HmacSetKey(hmac, type, key, keySz); |
1986 | 0 | if (ret == 0) { |
1987 | 0 | ret = wc_HmacUpdate(hmac, in, inSz); |
1988 | 0 | HMAC_SUBOP_WAIT(ret, hmac); |
1989 | 0 | } |
1990 | 0 | if (ret == 0) { |
1991 | 0 | ret = wc_HmacFinal(hmac, out); |
1992 | 0 | HMAC_SUBOP_WAIT(ret, hmac); |
1993 | 0 | } |
1994 | | /* The caller frees a context that it initialized. */ |
1995 | 0 | if (!callerCtx) { |
1996 | 0 | wc_HmacFree(hmac); |
1997 | 0 | } |
1998 | 0 | } |
1999 | 0 | if (ret == 0) { |
2000 | 0 | *outSz = digestSz; |
2001 | 0 | } |
2002 | |
|
2003 | 0 | return ret; |
2004 | 0 | } |
2005 | | |
2006 | | int wc_HmacGenerate(int type, byte* out, word32* outSz, |
2007 | | const byte* in, word32 inSz, |
2008 | | const byte* key, word32 keySz) |
2009 | 0 | { |
2010 | 0 | int ret; |
2011 | 0 | const byte emptyKey = 0; |
2012 | 0 | WC_DECLARE_VAR(hmac, Hmac, 1, 0); |
2013 | | |
2014 | | /* No context from the caller here, so a NULL key is a key of zero |
2015 | | * length. */ |
2016 | 0 | if (key == NULL && keySz == 0) { |
2017 | 0 | key = &emptyKey; |
2018 | 0 | } |
2019 | |
|
2020 | 0 | WC_ALLOC_VAR_EX(hmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC, |
2021 | 0 | return MEMORY_E); |
2022 | |
|
2023 | 0 | ret = wc_HmacGenerate_ex(hmac, type, out, outSz, in, inSz, key, keySz, |
2024 | 0 | NULL, INVALID_DEVID); |
2025 | |
|
2026 | 0 | WC_FREE_VAR_EX(hmac, NULL, DYNAMIC_TYPE_HMAC); |
2027 | |
|
2028 | 0 | return ret; |
2029 | 0 | } |
2030 | | |
2031 | | /* One-shot HMAC verify for use with a crypto callback device. Without one, use |
2032 | | * wc_HmacVerify(). hmac and key are used as in wc_HmacGenerate_ex(): with a |
2033 | | * NULL key, hmac must be initialized by the caller. |
2034 | | * checkSz must be the digest size of type. |
2035 | | * |
2036 | | * returns 0 when the tag matches, MAC_CMP_FAILED_E when it does not, |
2037 | | * otherwise failure. |
2038 | | */ |
2039 | | int wc_HmacVerify_ex(Hmac* hmac, int type, const byte* check, word32 checkSz, |
2040 | | const byte* in, word32 inSz, |
2041 | | const byte* key, word32 keySz, |
2042 | | void* heap, int devId) |
2043 | 0 | { |
2044 | 0 | int ret; |
2045 | 0 | byte tag[WC_MAX_DIGEST_SIZE]; |
2046 | 0 | word32 tagSz = sizeof(tag); |
2047 | |
|
2048 | 0 | if (check == NULL) { |
2049 | 0 | return BAD_FUNC_ARG; |
2050 | 0 | } |
2051 | | |
2052 | 0 | ret = wc_HmacSizeByType(type); |
2053 | 0 | if (ret < 0) { |
2054 | 0 | return ret; |
2055 | 0 | } |
2056 | 0 | if (checkSz != (word32)ret) { |
2057 | 0 | return BAD_FUNC_ARG; |
2058 | 0 | } |
2059 | | |
2060 | 0 | XMEMSET(tag, 0, sizeof(tag)); |
2061 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2062 | | wc_MemZero_Add("wc_HmacVerify_ex tag", tag, sizeof(tag)); |
2063 | | #endif |
2064 | |
|
2065 | 0 | ret = wc_HmacGenerate_ex(hmac, type, tag, &tagSz, in, inSz, key, keySz, |
2066 | 0 | heap, devId); |
2067 | 0 | if (ret == 0 && ConstantCompare(check, tag, (int)checkSz) != 0) { |
2068 | 0 | ret = MAC_CMP_FAILED_E; |
2069 | 0 | } |
2070 | |
|
2071 | 0 | ForceZero(tag, sizeof(tag)); |
2072 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2073 | | wc_MemZero_Check(tag, sizeof(tag)); |
2074 | | #endif |
2075 | |
|
2076 | 0 | return ret; |
2077 | 0 | } |
2078 | | |
2079 | | int wc_HmacVerify(int type, const byte* check, word32 checkSz, |
2080 | | const byte* in, word32 inSz, |
2081 | | const byte* key, word32 keySz) |
2082 | 0 | { |
2083 | 0 | int ret; |
2084 | 0 | const byte emptyKey = 0; |
2085 | 0 | WC_DECLARE_VAR(hmac, Hmac, 1, 0); |
2086 | | |
2087 | | /* No context from the caller here, so a NULL key is a key of zero |
2088 | | * length. */ |
2089 | 0 | if (key == NULL && keySz == 0) { |
2090 | 0 | key = &emptyKey; |
2091 | 0 | } |
2092 | |
|
2093 | 0 | WC_ALLOC_VAR_EX(hmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC, |
2094 | 0 | return MEMORY_E); |
2095 | |
|
2096 | 0 | ret = wc_HmacVerify_ex(hmac, type, check, checkSz, in, inSz, key, keySz, |
2097 | 0 | NULL, INVALID_DEVID); |
2098 | |
|
2099 | 0 | WC_FREE_VAR_EX(hmac, NULL, DYNAMIC_TYPE_HMAC); |
2100 | |
|
2101 | 0 | return ret; |
2102 | 0 | } |
2103 | | |
2104 | | #ifdef HAVE_HKDF |
2105 | | |
2106 | | /* HMAC-KDF-Extract. |
2107 | | * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF). |
2108 | | * |
2109 | | * type The hash algorithm type. |
2110 | | * salt The optional salt value. |
2111 | | * saltSz The size of the salt. |
2112 | | * inKey The input keying material. |
2113 | | * inKeySz The size of the input keying material. |
2114 | | * out The pseudorandom key with the length that of the hash. |
2115 | | * returns 0 on success, otherwise failure. |
2116 | | */ |
2117 | | int wc_HKDF_Extract_ex(int type, const byte* salt, word32 saltSz, |
2118 | | const byte* inKey, word32 inKeySz, byte* out, void* heap, int devId) |
2119 | 0 | { |
2120 | 0 | byte tmp[WC_MAX_DIGEST_SIZE]; /* localSalt helper */ |
2121 | 0 | WC_DECLARE_VAR(myHmac, Hmac, 1, 0); |
2122 | 0 | int ret; |
2123 | 0 | const byte* localSalt; /* either points to user input or tmp */ |
2124 | 0 | word32 hashSz; |
2125 | |
|
2126 | 0 | if (out == NULL || (inKey == NULL && inKeySz > 0)) { |
2127 | 0 | return BAD_FUNC_ARG; |
2128 | 0 | } |
2129 | | |
2130 | | #ifdef WOLF_CRYPTO_CB |
2131 | | /* Try crypto callback first. Only CRYPTOCB_UNAVAILABLE falls back |
2132 | | * to software. WC_PENDING_E is returned as-is: the caller polls, |
2133 | | * re-invoking with identical arguments until it clears. */ |
2134 | | if (devId != INVALID_DEVID) { |
2135 | | ret = wc_CryptoCb_Hkdf_Extract(type, salt, saltSz, inKey, inKeySz, |
2136 | | out, devId); |
2137 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
2138 | | return ret; |
2139 | | } |
2140 | | #endif |
2141 | | |
2142 | 0 | ret = wc_HmacSizeByType(type); |
2143 | 0 | if (ret < 0) { |
2144 | 0 | return ret; |
2145 | 0 | } |
2146 | 0 | hashSz = (word32)ret; |
2147 | |
|
2148 | 0 | WC_ALLOC_VAR_EX(myHmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC, |
2149 | 0 | return MEMORY_E); |
2150 | |
|
2151 | 0 | localSalt = salt; |
2152 | 0 | if (localSalt == NULL) { |
2153 | 0 | XMEMSET(tmp, 0, hashSz); |
2154 | 0 | localSalt = tmp; |
2155 | 0 | saltSz = hashSz; |
2156 | 0 | } |
2157 | |
|
2158 | 0 | ret = wc_HmacInit(myHmac, heap, devId); |
2159 | 0 | if (ret == 0) { |
2160 | | #if FIPS_VERSION3_GE(6,0,0) |
2161 | | ret = wc_HmacSetKey_ex(myHmac, type, localSalt, saltSz, |
2162 | | FIPS_ALLOW_SHORT); |
2163 | | #else |
2164 | 0 | ret = wc_HmacSetKey(myHmac, type, localSalt, saltSz); |
2165 | 0 | #endif |
2166 | 0 | if (ret == 0) { |
2167 | 0 | ret = wc_HmacUpdate(myHmac, inKey, inKeySz); |
2168 | 0 | HMAC_SUBOP_WAIT(ret, myHmac); |
2169 | 0 | } |
2170 | 0 | if (ret == 0) { |
2171 | 0 | ret = wc_HmacFinal(myHmac, out); |
2172 | 0 | HMAC_SUBOP_WAIT(ret, myHmac); |
2173 | 0 | } |
2174 | 0 | wc_HmacFree(myHmac); |
2175 | 0 | } |
2176 | 0 | WC_FREE_VAR_EX(myHmac, NULL, DYNAMIC_TYPE_HMAC); |
2177 | |
|
2178 | 0 | return ret; |
2179 | 0 | } |
2180 | | |
2181 | | int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz, |
2182 | | const byte* inKey, word32 inKeySz, byte* out) |
2183 | 0 | { |
2184 | 0 | return wc_HKDF_Extract_ex(type, salt, saltSz, inKey, inKeySz, out, NULL, |
2185 | 0 | INVALID_DEVID); |
2186 | 0 | } |
2187 | | |
2188 | | /* HMAC-KDF-Expand. |
2189 | | * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF). |
2190 | | * |
2191 | | * type The hash algorithm type. |
2192 | | * inKey The input key. |
2193 | | * inKeySz The size of the input key. |
2194 | | * info The application specific information. |
2195 | | * infoSz The size of the application specific information. |
2196 | | * out The output keying material. |
2197 | | * returns 0 on success, otherwise failure. |
2198 | | */ |
2199 | | int wc_HKDF_Expand_ex(int type, const byte* inKey, word32 inKeySz, |
2200 | | const byte* info, word32 infoSz, byte* out, word32 outSz, |
2201 | | void* heap, int devId) |
2202 | 0 | { |
2203 | 0 | byte tmp[WC_MAX_DIGEST_SIZE]; |
2204 | 0 | WC_DECLARE_VAR(myHmac, Hmac, 1, 0); |
2205 | 0 | int ret = 0; |
2206 | 0 | word32 outIdx = 0; |
2207 | 0 | word32 hashSz; |
2208 | 0 | byte n = 0x1; |
2209 | |
|
2210 | 0 | if (out == NULL || (inKey == NULL && inKeySz > 0)) { |
2211 | 0 | return BAD_FUNC_ARG; |
2212 | 0 | } |
2213 | | |
2214 | 0 | ret = wc_HmacSizeByType(type); |
2215 | 0 | if (ret < 0) { |
2216 | 0 | return ret; |
2217 | 0 | } |
2218 | 0 | else if (ret == 0) |
2219 | 0 | return BAD_FUNC_ARG; |
2220 | | |
2221 | 0 | hashSz = (word32)ret; |
2222 | | |
2223 | | /* RFC 5869 states that the length of output keying material in |
2224 | | * octets must be L <= 255*HashLen or N = ceil(L/HashLen) */ |
2225 | 0 | if (outSz/hashSz + ((outSz % hashSz) != 0) > 255) |
2226 | 0 | return BAD_FUNC_ARG; |
2227 | | |
2228 | | #ifdef WOLF_CRYPTO_CB |
2229 | | /* Try crypto callback first. WC_PENDING_E is returned to the |
2230 | | * caller to poll, as in wc_HKDF_Extract_ex(). */ |
2231 | | if (devId != INVALID_DEVID) { |
2232 | | ret = wc_CryptoCb_Hkdf_Expand(type, inKey, inKeySz, info, infoSz, |
2233 | | out, outSz, devId); |
2234 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
2235 | | return ret; |
2236 | | } |
2237 | | #endif |
2238 | | |
2239 | 0 | WC_ALLOC_VAR_EX(myHmac, Hmac, 1, NULL, DYNAMIC_TYPE_HMAC, |
2240 | 0 | return MEMORY_E); |
2241 | |
|
2242 | 0 | ret = wc_HmacInit(myHmac, heap, devId); |
2243 | 0 | if (ret != 0) { |
2244 | 0 | WC_FREE_VAR_EX(myHmac, NULL, DYNAMIC_TYPE_HMAC); |
2245 | 0 | return ret; |
2246 | 0 | } |
2247 | | |
2248 | 0 | XMEMSET(tmp, 0, WC_MAX_DIGEST_SIZE); |
2249 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2250 | | wc_MemZero_Add("wc_HKDF_Expand_ex tmp", tmp, WC_MAX_DIGEST_SIZE); |
2251 | | #endif |
2252 | |
|
2253 | 0 | while (outIdx < outSz) { |
2254 | 0 | word32 tmpSz = (n == 1) ? 0 : hashSz; |
2255 | 0 | word32 left = outSz - outIdx; |
2256 | |
|
2257 | | #if FIPS_VERSION3_GE(6,0,0) |
2258 | | ret = wc_HmacSetKey_ex(myHmac, type, inKey, inKeySz, |
2259 | | FIPS_ALLOW_SHORT); |
2260 | | #else |
2261 | 0 | ret = wc_HmacSetKey(myHmac, type, inKey, inKeySz); |
2262 | 0 | #endif |
2263 | 0 | if (ret != 0) |
2264 | 0 | break; |
2265 | 0 | ret = wc_HmacUpdate(myHmac, tmp, tmpSz); |
2266 | 0 | HMAC_SUBOP_WAIT(ret, myHmac); |
2267 | 0 | if (ret != 0) |
2268 | 0 | break; |
2269 | 0 | ret = wc_HmacUpdate(myHmac, info, infoSz); |
2270 | 0 | HMAC_SUBOP_WAIT(ret, myHmac); |
2271 | 0 | if (ret != 0) |
2272 | 0 | break; |
2273 | 0 | ret = wc_HmacUpdate(myHmac, &n, 1); |
2274 | 0 | HMAC_SUBOP_WAIT(ret, myHmac); |
2275 | 0 | if (ret != 0) |
2276 | 0 | break; |
2277 | 0 | ret = wc_HmacFinal(myHmac, tmp); |
2278 | 0 | HMAC_SUBOP_WAIT(ret, myHmac); |
2279 | 0 | if (ret != 0) |
2280 | 0 | break; |
2281 | | |
2282 | 0 | left = min(left, hashSz); |
2283 | 0 | XMEMCPY(out+outIdx, tmp, left); |
2284 | |
|
2285 | 0 | outIdx += left; |
2286 | 0 | n++; |
2287 | 0 | } |
2288 | 0 | if (ret != 0) { |
2289 | | /* Partial OKM is not released (ISO/IEC 19790 7.9.7). */ |
2290 | 0 | ForceZero(out, outSz); |
2291 | 0 | } |
2292 | |
|
2293 | 0 | ForceZero(tmp, WC_MAX_DIGEST_SIZE); |
2294 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2295 | | wc_MemZero_Check(tmp, WC_MAX_DIGEST_SIZE); |
2296 | | #endif |
2297 | 0 | wc_HmacFree(myHmac); |
2298 | 0 | WC_FREE_VAR_EX(myHmac, NULL, DYNAMIC_TYPE_HMAC); |
2299 | |
|
2300 | 0 | return ret; |
2301 | 0 | } |
2302 | | |
2303 | | int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz, |
2304 | | const byte* info, word32 infoSz, byte* out, word32 outSz) |
2305 | 0 | { |
2306 | 0 | return wc_HKDF_Expand_ex(type, inKey, inKeySz, info, infoSz, out, outSz, |
2307 | 0 | NULL, INVALID_DEVID); |
2308 | 0 | } |
2309 | | |
2310 | | /* HMAC-KDF. |
2311 | | * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF). |
2312 | | * |
2313 | | * type The hash algorithm type. |
2314 | | * inKey The input keying material. |
2315 | | * inKeySz The size of the input keying material. |
2316 | | * salt The optional salt value. |
2317 | | * saltSz The size of the salt. |
2318 | | * info The application specific information. |
2319 | | * infoSz The size of the application specific information. |
2320 | | * out The output keying material. |
2321 | | * returns 0 on success, otherwise failure. |
2322 | | */ |
2323 | | int wc_HKDF_ex(int type, const byte* inKey, word32 inKeySz, |
2324 | | const byte* salt, word32 saltSz, const byte* info, |
2325 | | word32 infoSz, byte* out, word32 outSz, void* heap, |
2326 | | int devId) |
2327 | 0 | { |
2328 | 0 | byte prk[WC_MAX_DIGEST_SIZE]; |
2329 | 0 | word32 hashSz; |
2330 | 0 | int ret; |
2331 | |
|
2332 | 0 | (void)devId; /* suppress unused parameter warning */ |
2333 | |
|
2334 | | #ifdef WOLF_CRYPTO_CB |
2335 | | /* Try crypto callback first. WC_PENDING_E is returned to the |
2336 | | * caller to poll, as in wc_HKDF_Extract_ex(). */ |
2337 | | if (devId != INVALID_DEVID) { |
2338 | | ret = wc_CryptoCb_Hkdf(type, inKey, inKeySz, salt, saltSz, info, |
2339 | | infoSz, out, outSz, devId); |
2340 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
2341 | | return ret; |
2342 | | } |
2343 | | #endif |
2344 | |
|
2345 | 0 | ret = wc_HmacSizeByType(type); |
2346 | 0 | if (ret < 0) |
2347 | 0 | return ret; |
2348 | 0 | hashSz = (word32)ret; |
2349 | |
|
2350 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2351 | | XMEMSET(prk, 0, WC_MAX_DIGEST_SIZE); |
2352 | | wc_MemZero_Add("wc_HKDF_ex prk", prk, WC_MAX_DIGEST_SIZE); |
2353 | | #endif |
2354 | | /* Restartable, not resumable: the retry redoes extract, so a |
2355 | | * device that pends must serve the repeat from its result. */ |
2356 | 0 | ret = wc_HKDF_Extract_ex(type, salt, saltSz, inKey, inKeySz, prk, heap, |
2357 | 0 | devId); |
2358 | 0 | if (ret == 0) { |
2359 | 0 | ret = wc_HKDF_Expand_ex(type, prk, hashSz, info, infoSz, |
2360 | 0 | out, outSz, heap, devId); |
2361 | 0 | } |
2362 | 0 | ForceZero(prk, WC_MAX_DIGEST_SIZE); |
2363 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
2364 | | wc_MemZero_Check(prk, WC_MAX_DIGEST_SIZE); |
2365 | | #endif |
2366 | 0 | return ret; |
2367 | 0 | } |
2368 | | |
2369 | | int wc_HKDF(int type, const byte* inKey, word32 inKeySz, const byte* salt, |
2370 | | word32 saltSz, const byte* info, word32 infoSz, byte* out, |
2371 | | word32 outSz) |
2372 | 0 | { |
2373 | 0 | return wc_HKDF_ex(type, inKey, inKeySz, salt, saltSz, info, infoSz, out, |
2374 | 0 | outSz, NULL, INVALID_DEVID); |
2375 | 0 | } |
2376 | | |
2377 | | #endif /* HAVE_HKDF */ |
2378 | | |
2379 | | #undef HMAC_SUBOP_WAIT |
2380 | | |
2381 | | #endif /* NO_HMAC */ |