/src/wolfssl-normal-math/wolfcrypt/src/cmac.c
Line | Count | Source |
1 | | /* cmac.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 | | #define _WC_BUILDING_CMAC_C |
23 | | |
24 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
25 | | |
26 | | #ifdef WOLFSSL_QNX_CAAM |
27 | | #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h> |
28 | | #endif |
29 | | #if defined(WOLFSSL_HASH_KEEP) |
30 | | #include <wolfssl/wolfcrypt/hash.h> |
31 | | #endif |
32 | | |
33 | | #if defined(WOLFSSL_CMAC) |
34 | | |
35 | | #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) |
36 | | /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ |
37 | | #define FIPS_NO_WRAPPERS |
38 | | |
39 | | #ifdef USE_WINDOWS_API |
40 | | #pragma code_seg(".fipsA$c") |
41 | | #pragma const_seg(".fipsB$c") |
42 | | #endif |
43 | | #endif |
44 | | |
45 | | #ifdef NO_INLINE |
46 | | #include <wolfssl/wolfcrypt/misc.h> |
47 | | #else |
48 | | #define WOLFSSL_MISC_INCLUDED |
49 | | #include <wolfcrypt/src/misc.c> |
50 | | #endif |
51 | | |
52 | | #include <wolfssl/wolfcrypt/aes.h> |
53 | | #include <wolfssl/wolfcrypt/cmac.h> |
54 | | |
55 | | #ifdef WOLF_CRYPTO_CB |
56 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
57 | | #endif |
58 | | |
59 | | #if FIPS_VERSION3_GE(6,0,0) |
60 | | const unsigned int wolfCrypt_FIPS_cmac_ro_sanity[2] = |
61 | | { 0x1a2b3c4d, 0x00000003 }; |
62 | | int wolfCrypt_FIPS_CMAC_sanity(void) |
63 | | { |
64 | | return 0; |
65 | | } |
66 | | #endif |
67 | | |
68 | | #ifdef WOLFSSL_HASH_KEEP |
69 | | /* Some hardware have issues with update, this function stores the data to be |
70 | | * hashed into an array. Once ready, the Final operation is called on all of the |
71 | | * data to be hashed at once. |
72 | | * returns 0 on success |
73 | | */ |
74 | | int wc_CMAC_Grow(Cmac* cmac, const byte* in, int inSz) |
75 | | { |
76 | | if ((cmac == NULL) || (in == NULL && inSz != 0)) |
77 | | return BAD_FUNC_ARG; |
78 | | return _wc_Hash_Grow(&cmac->msg, &cmac->used, &cmac->len, in, inSz, cmac->aes.heap); |
79 | | } |
80 | | #endif /* WOLFSSL_HASH_KEEP */ |
81 | | |
82 | | #if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) |
83 | | /* Used by AES-SIV. See aes.c. */ |
84 | | void ShiftAndXorRb(byte* out, byte* in) |
85 | 230 | { |
86 | 230 | int i, j, xorRb; |
87 | 230 | int mask = 0, last = 0; |
88 | 230 | byte Rb = 0x87; |
89 | | |
90 | 230 | xorRb = (in[0] & 0x80) != 0; |
91 | | |
92 | 3.91k | for (i = 1, j = WC_AES_BLOCK_SIZE - 1; i <= WC_AES_BLOCK_SIZE; i++, j--) { |
93 | 3.68k | last = (in[j] & 0x80) ? 1 : 0; |
94 | 3.68k | out[j] = (byte)((in[j] << 1) | mask); |
95 | 3.68k | mask = last; |
96 | 3.68k | if (xorRb) { |
97 | 1.44k | out[j] ^= Rb; |
98 | 1.44k | Rb = 0; |
99 | 1.44k | } |
100 | 3.68k | } |
101 | 230 | } |
102 | | #endif /* !NO_AES && WOLFSSL_AES_DIRECT */ |
103 | | |
104 | 115 | #define CMAC_AES_INIT_PLAIN 0 |
105 | | #ifdef WOLF_PRIVATE_KEY_ID |
106 | 230 | #define CMAC_AES_INIT_ID 1 |
107 | 230 | #define CMAC_AES_INIT_LABEL 2 |
108 | | #endif |
109 | | |
110 | | |
111 | | static int _InitCmac_common(Cmac* cmac, const byte* key, word32 keySz, |
112 | | int type, void* unused, void* heap, int devId, |
113 | | int aesInitType, unsigned char* id, int idLen, |
114 | | const char* label) |
115 | 115 | { |
116 | 115 | int ret = 0; |
117 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT) |
118 | | byte useSW = 0; |
119 | | #endif |
120 | | |
121 | 115 | if (cmac == NULL || type != WC_CMAC_AES) { |
122 | 0 | return BAD_FUNC_ARG; |
123 | 0 | } |
124 | | |
125 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT) |
126 | | /* save if we should use SW crypt, restore after memset */ |
127 | | useSW = cmac->useSWCrypt; |
128 | | #endif |
129 | 115 | XMEMSET(cmac, 0, sizeof(Cmac)); |
130 | | |
131 | | /* Stash heap so the cryptocb can read it from the zeroed cmac. */ |
132 | 115 | #ifndef NO_AES |
133 | 115 | cmac->aes.heap = heap; |
134 | | #else |
135 | | cmac->heap = heap; |
136 | | #endif |
137 | | |
138 | | /* Store id/label on the Cmac struct so the crypto callback can |
139 | | * inspect them to determine the hardware key slot. */ |
140 | 115 | #ifdef WOLF_PRIVATE_KEY_ID |
141 | 115 | cmac->aesInitType = aesInitType; |
142 | 115 | if (aesInitType == CMAC_AES_INIT_ID && id != NULL && idLen > 0) { |
143 | 0 | if (idLen > (int)sizeof(cmac->id)) { |
144 | 0 | return BAD_FUNC_ARG; |
145 | 0 | } |
146 | 0 | XMEMCPY(cmac->id, id, (word32)idLen); |
147 | 0 | cmac->idLen = idLen; |
148 | 0 | } |
149 | 115 | else if (aesInitType == CMAC_AES_INIT_LABEL && label != NULL) { |
150 | 0 | int labelLen = (int)XSTRLEN(label); |
151 | 0 | if (labelLen > 0 && labelLen < (int)sizeof(cmac->label)) { |
152 | 0 | XMEMCPY(cmac->label, label, (word32)labelLen); |
153 | 0 | cmac->labelLen = labelLen; |
154 | 0 | } |
155 | 0 | } |
156 | 115 | #endif |
157 | 115 | (void)aesInitType; |
158 | 115 | (void)id; |
159 | 115 | (void)idLen; |
160 | 115 | (void)label; |
161 | | |
162 | 115 | #ifdef WOLF_CRYPTO_CB |
163 | | /* Set devId regardless of value (invalid or not) */ |
164 | 115 | cmac->devId = devId; |
165 | 115 | #ifndef WOLF_CRYPTO_CB_FIND |
166 | 115 | if (devId != INVALID_DEVID) |
167 | 0 | #endif |
168 | 0 | { |
169 | 0 | cmac->devCtx = NULL; |
170 | |
|
171 | 0 | ret = wc_CryptoCb_Cmac(cmac, key, keySz, NULL, 0, NULL, NULL, |
172 | 0 | type, unused); |
173 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { |
174 | 0 | return ret; |
175 | 0 | } |
176 | | /* fall-through when unavailable, reset ret for software path */ |
177 | 0 | ret = 0; |
178 | 0 | (void)ret; |
179 | 0 | } |
180 | | #else |
181 | | (void)devId; |
182 | | #endif |
183 | 115 | (void)unused; |
184 | 115 | (void)heap; |
185 | | |
186 | 115 | if (key == NULL || keySz == 0) { |
187 | 0 | return BAD_FUNC_ARG; |
188 | 0 | } |
189 | | |
190 | 115 | switch (type) { |
191 | 0 | #if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) |
192 | 115 | case WC_CMAC_AES: |
193 | 115 | cmac->type = WC_CMAC_AES; |
194 | 115 | switch (aesInitType) { |
195 | 0 | #ifdef WOLF_PRIVATE_KEY_ID |
196 | 0 | case CMAC_AES_INIT_ID: |
197 | 0 | if (id == NULL || idLen == 0 || label != NULL) { |
198 | 0 | ret = BAD_FUNC_ARG; |
199 | 0 | } |
200 | 0 | else { |
201 | 0 | ret = wc_AesInit_Id(&cmac->aes, id, idLen, heap, devId); |
202 | 0 | } |
203 | 0 | break; |
204 | 0 | case CMAC_AES_INIT_LABEL: |
205 | 0 | if (label == NULL || id != NULL || idLen != 0) { |
206 | 0 | ret = BAD_FUNC_ARG; |
207 | 0 | } |
208 | 0 | else { |
209 | 0 | ret = wc_AesInit_Label(&cmac->aes, label, heap, devId); |
210 | 0 | } |
211 | 0 | break; |
212 | 0 | #endif |
213 | 115 | default: |
214 | 115 | if (id != NULL || idLen != 0 || label != NULL) { |
215 | 0 | ret = BAD_FUNC_ARG; |
216 | 0 | } |
217 | 115 | else { |
218 | 115 | ret = wc_AesInit(&cmac->aes, heap, devId); |
219 | 115 | } |
220 | 115 | break; |
221 | 115 | } |
222 | 115 | if (ret != 0) { |
223 | 0 | return ret; |
224 | 0 | } |
225 | | |
226 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT) |
227 | | cmac->useSWCrypt = useSW; |
228 | | if (cmac->useSWCrypt == 1) { |
229 | | cmac->aes.useSWCrypt = 1; |
230 | | } |
231 | | #endif |
232 | | |
233 | 115 | if (ret == 0) { |
234 | 115 | ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION); |
235 | 115 | } |
236 | | |
237 | 115 | if (ret == 0) { |
238 | 115 | byte l[WC_AES_BLOCK_SIZE]; |
239 | | |
240 | 115 | XMEMSET(l, 0, WC_AES_BLOCK_SIZE); |
241 | 115 | #ifndef HAVE_SELFTEST |
242 | 115 | ret = wc_AesEncryptDirect(&cmac->aes, l, l); |
243 | 115 | if (ret == 0) { |
244 | 115 | ShiftAndXorRb(cmac->k1, l); |
245 | 115 | ShiftAndXorRb(cmac->k2, cmac->k1); |
246 | 115 | ForceZero(l, WC_AES_BLOCK_SIZE); |
247 | 115 | } |
248 | | #else |
249 | | wc_AesEncryptDirect(&cmac->aes, l, l); |
250 | | ShiftAndXorRb(cmac->k1, l); |
251 | | ShiftAndXorRb(cmac->k2, cmac->k1); |
252 | | ForceZero(l, WC_AES_BLOCK_SIZE); |
253 | | #endif |
254 | 115 | } |
255 | 115 | break; |
256 | 0 | #endif /* !NO_AES && WOLFSSL_AES_DIRECT */ |
257 | 0 | default: |
258 | 0 | return BAD_FUNC_ARG; |
259 | 115 | } |
260 | | |
261 | 115 | return ret; |
262 | 115 | } |
263 | | |
264 | | |
265 | | /* returns 0 on success */ |
266 | | int wc_InitCmac_ex(Cmac* cmac, const byte* key, word32 keySz, |
267 | | int type, void* unused, void* heap, int devId) |
268 | 115 | { |
269 | 115 | return _InitCmac_common(cmac, key, keySz, type, unused, heap, devId, |
270 | 115 | CMAC_AES_INIT_PLAIN, NULL, 0, NULL); |
271 | 115 | } |
272 | | |
273 | | |
274 | | int wc_InitCmac(Cmac* cmac, const byte* key, word32 keySz, |
275 | | int type, void* unused) |
276 | 115 | { |
277 | | #ifdef WOLFSSL_QNX_CAAM |
278 | | int devId = WOLFSSL_CAAM_DEVID; |
279 | | #else |
280 | 115 | int devId = INVALID_DEVID; |
281 | 115 | #endif |
282 | 115 | return wc_InitCmac_ex(cmac, key, keySz, type, unused, NULL, devId); |
283 | 115 | } |
284 | | |
285 | | |
286 | | #ifdef WOLF_PRIVATE_KEY_ID |
287 | | /* returns 0 on success */ |
288 | | int wc_InitCmac_Id(Cmac* cmac, const byte* key, word32 keySz, |
289 | | int type, void* unused, unsigned char* id, int len, |
290 | | void* heap, int devId) |
291 | 0 | { |
292 | 0 | return _InitCmac_common(cmac, key, keySz, type, unused, heap, devId, |
293 | 0 | CMAC_AES_INIT_ID, id, len, NULL); |
294 | 0 | } |
295 | | |
296 | | |
297 | | /* returns 0 on success */ |
298 | | int wc_InitCmac_Label(Cmac* cmac, const byte* key, word32 keySz, |
299 | | int type, void* unused, const char* label, |
300 | | void* heap, int devId) |
301 | 0 | { |
302 | 0 | return _InitCmac_common(cmac, key, keySz, type, unused, heap, devId, |
303 | 0 | CMAC_AES_INIT_LABEL, NULL, 0, label); |
304 | 0 | } |
305 | | #endif /* WOLF_PRIVATE_KEY_ID */ |
306 | | |
307 | | |
308 | | int wc_CmacUpdate(Cmac* cmac, const byte* in, word32 inSz) |
309 | 1.54k | { |
310 | 1.54k | int ret = 0; |
311 | | |
312 | 1.54k | if ((cmac == NULL) || (in == NULL && inSz != 0)) { |
313 | 0 | return BAD_FUNC_ARG; |
314 | 0 | } |
315 | | |
316 | 1.54k | #ifdef WOLF_CRYPTO_CB |
317 | 1.54k | #ifndef WOLF_CRYPTO_CB_FIND |
318 | 1.54k | if (cmac->devId != INVALID_DEVID) |
319 | 0 | #endif |
320 | 0 | { |
321 | 0 | ret = wc_CryptoCb_Cmac(cmac, NULL, 0, in, inSz, |
322 | 0 | NULL, NULL, (int)cmac->type, NULL); |
323 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
324 | 0 | return ret; |
325 | | /* fall-through when unavailable */ |
326 | 0 | } |
327 | 1.54k | #endif |
328 | | |
329 | | /* Clear CRYPTOCB_UNAVAILABLE return code */ |
330 | 1.54k | ret = 0; |
331 | | |
332 | 1.54k | switch (cmac->type) { |
333 | 0 | #if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) |
334 | 1.54k | case WC_CMAC_AES: |
335 | 1.54k | { |
336 | | #ifdef HAVE_SELFTEST |
337 | | while ((ret == 0) && (inSz != 0)) { |
338 | | word32 add = min(inSz, WC_AES_BLOCK_SIZE - cmac->bufferSz); |
339 | | XMEMCPY(&cmac->buffer[cmac->bufferSz], in, add); |
340 | | |
341 | | cmac->bufferSz += add; |
342 | | in += add; |
343 | | inSz -= add; |
344 | | |
345 | | if (cmac->bufferSz == WC_AES_BLOCK_SIZE && inSz != 0) { |
346 | | xorbuf(cmac->buffer, cmac->digest, WC_AES_BLOCK_SIZE); |
347 | | wc_AesEncryptDirect(&cmac->aes, cmac->digest, |
348 | | cmac->buffer); |
349 | | cmac->totalSz += WC_AES_BLOCK_SIZE; |
350 | | cmac->bufferSz = 0; |
351 | | } |
352 | | } |
353 | | #else |
354 | 1.54k | (void)ret; |
355 | 1.54k | ret = wc_local_CmacUpdateAes(cmac, in, inSz); |
356 | 1.54k | #endif |
357 | 1.54k | }; break; |
358 | 0 | #endif /* !NO_AES && WOLFSSL_AES_DIRECT */ |
359 | 0 | default: |
360 | 0 | ret = BAD_FUNC_ARG; |
361 | 1.54k | } |
362 | 1.54k | return ret; |
363 | 1.54k | } |
364 | | |
365 | | int wc_CmacFree(Cmac* cmac) |
366 | 115 | { |
367 | 115 | if (cmac == NULL) |
368 | 0 | return BAD_FUNC_ARG; |
369 | | #if defined(WOLFSSL_HASH_KEEP) |
370 | | /* TODO: msg is leaked if wc_CmacFinal() is not called |
371 | | * e.g. when multiple calls to wc_CmacUpdate() and one fails but |
372 | | * wc_CmacFinal() not called. */ |
373 | | XFREE(cmac->msg, cmac->aes.heap, DYNAMIC_TYPE_TMP_BUFFER); |
374 | | #endif |
375 | 115 | switch (cmac->type) { |
376 | 0 | #if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) |
377 | 115 | case WC_CMAC_AES: |
378 | 115 | wc_AesFree(&cmac->aes); |
379 | 115 | break; |
380 | 0 | #endif /* !NO_AES && WOLFSSL_AES_DIRECT */ |
381 | 0 | default: |
382 | | /* Nothing to do */ |
383 | 0 | (void)cmac; |
384 | 115 | } |
385 | 115 | ForceZero(cmac, sizeof(Cmac)); |
386 | 115 | return 0; |
387 | 115 | } |
388 | | |
389 | | int wc_CmacFinalNoFree(Cmac* cmac, byte* out, word32* outSz) |
390 | 115 | { |
391 | 115 | int ret = 0; |
392 | | |
393 | 115 | if (cmac == NULL || out == NULL || outSz == NULL) { |
394 | 0 | return BAD_FUNC_ARG; |
395 | 0 | } |
396 | 115 | if (*outSz < WC_CMAC_TAG_MIN_SZ || *outSz > WC_CMAC_TAG_MAX_SZ) { |
397 | 0 | return BUFFER_E; |
398 | 0 | } |
399 | | |
400 | 115 | #ifdef WOLF_CRYPTO_CB |
401 | 115 | #ifndef WOLF_CRYPTO_CB_FIND |
402 | 115 | if (cmac->devId != INVALID_DEVID) |
403 | 0 | #endif |
404 | 0 | { |
405 | 0 | ret = wc_CryptoCb_Cmac(cmac, NULL, 0, NULL, 0, out, outSz, |
406 | 0 | (int)cmac->type, NULL); |
407 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
408 | 0 | return ret; |
409 | | |
410 | | /* Clear CRYPTOCB_UNAVAILABLE return code */ |
411 | 0 | ret = 0; |
412 | | |
413 | | /* fall-through when unavailable */ |
414 | 0 | } |
415 | 115 | #endif |
416 | 115 | if (ret == 0) { |
417 | 115 | switch (cmac->type) { |
418 | 0 | #if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) |
419 | 115 | case WC_CMAC_AES: |
420 | 115 | { |
421 | 115 | const byte* subKey; |
422 | 115 | word32 remainder; |
423 | | |
424 | 115 | if (cmac->bufferSz == WC_AES_BLOCK_SIZE) { |
425 | 0 | subKey = cmac->k1; |
426 | 0 | } |
427 | 115 | else { |
428 | | /* ensure we will have a valid remainder value */ |
429 | 115 | if (cmac->bufferSz > WC_AES_BLOCK_SIZE) { |
430 | 0 | ret = BAD_STATE_E; |
431 | 0 | break; |
432 | 0 | } |
433 | 115 | remainder = WC_AES_BLOCK_SIZE - cmac->bufferSz; |
434 | | |
435 | 115 | if (remainder == 0) { |
436 | 0 | remainder = WC_AES_BLOCK_SIZE; |
437 | 0 | } |
438 | 115 | if (remainder > 1) { |
439 | 80 | XMEMSET(cmac->buffer + WC_AES_BLOCK_SIZE - remainder, 0, |
440 | 80 | remainder); |
441 | 80 | } |
442 | | |
443 | 115 | cmac->buffer[WC_AES_BLOCK_SIZE - remainder] = 0x80; |
444 | 115 | subKey = cmac->k2; |
445 | 115 | } |
446 | 115 | xorbuf(cmac->buffer, cmac->digest, WC_AES_BLOCK_SIZE); |
447 | 115 | xorbuf(cmac->buffer, subKey, WC_AES_BLOCK_SIZE); |
448 | 115 | #ifndef HAVE_SELFTEST |
449 | 115 | ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer); |
450 | 115 | if (ret == 0) { |
451 | 115 | XMEMCPY(out, cmac->digest, *outSz); |
452 | 115 | } |
453 | | #else |
454 | | wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer); |
455 | | XMEMCPY(out, cmac->digest, *outSz); |
456 | | #endif |
457 | 115 | }; break; |
458 | 0 | #endif /* !NO_AES && WOLFSSL_AES_DIRECT */ |
459 | 0 | default: |
460 | 0 | ret = BAD_FUNC_ARG; |
461 | 115 | } |
462 | 115 | } |
463 | 115 | return ret; |
464 | 115 | } |
465 | | |
466 | | int wc_CmacFinal(Cmac* cmac, byte* out, word32* outSz) |
467 | 115 | { |
468 | 115 | int ret = 0; |
469 | | |
470 | 115 | if (cmac == NULL) |
471 | 0 | return BAD_FUNC_ARG; |
472 | 115 | ret = wc_CmacFinalNoFree(cmac, out, outSz); |
473 | 115 | (void)wc_CmacFree(cmac); |
474 | 115 | return ret; |
475 | 115 | } |
476 | | |
477 | | #if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) |
478 | | int wc_AesCmacGenerate_ex(Cmac* cmac, |
479 | | byte* out, word32* outSz, |
480 | | const byte* in, word32 inSz, |
481 | | const byte* key, word32 keySz, |
482 | | void* heap, int devId) |
483 | 0 | { |
484 | 0 | int ret = 0; |
485 | |
|
486 | 0 | if (cmac == NULL) { |
487 | 0 | return BAD_FUNC_ARG; |
488 | 0 | } |
489 | | |
490 | 0 | #ifdef WOLF_CRYPTO_CB |
491 | | /* Set devId regardless of value (invalid or not) */ |
492 | 0 | cmac->devId = devId; |
493 | 0 | #ifndef WOLF_CRYPTO_CB_FIND |
494 | 0 | if (devId != INVALID_DEVID) |
495 | 0 | #endif |
496 | 0 | { |
497 | 0 | ret = wc_CryptoCb_Cmac(cmac, key, keySz, in, inSz, out, outSz, |
498 | 0 | WC_CMAC_AES, NULL); |
499 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
500 | 0 | return ret; |
501 | | |
502 | | /* Clear CRYPTOCB_UNAVAILABLE return code */ |
503 | 0 | ret = 0; |
504 | | |
505 | | /* fall-through when unavailable */ |
506 | 0 | } |
507 | 0 | #endif |
508 | | |
509 | 0 | if ( ((out == NULL) && (outSz != NULL) && (*outSz > 0)) |
510 | 0 | || (in == NULL && inSz > 0) |
511 | 0 | || (key == NULL && keySz > 0)) { |
512 | 0 | return BAD_FUNC_ARG; |
513 | 0 | } |
514 | | |
515 | | /* Init step is optional */ |
516 | 0 | if (key != NULL) { |
517 | 0 | ret = wc_InitCmac_ex(cmac, key, keySz, WC_CMAC_AES, NULL, heap, devId); |
518 | 0 | } |
519 | 0 | if (ret == 0) { |
520 | 0 | ret = wc_CmacUpdate(cmac, in, inSz); |
521 | | /* Ensure we are freed and zeroed if not calling wc_CmacFinal */ |
522 | 0 | if (ret != 0) { |
523 | 0 | (void)wc_CmacFree(cmac); |
524 | 0 | } |
525 | 0 | } |
526 | 0 | if (ret == 0) { |
527 | 0 | ret = wc_CmacFinal(cmac, out, outSz); |
528 | 0 | } |
529 | |
|
530 | 0 | return ret; |
531 | 0 | } |
532 | | |
533 | | |
534 | | int wc_AesCmacGenerate(byte* out, word32* outSz, |
535 | | const byte* in, word32 inSz, |
536 | | const byte* key, word32 keySz) |
537 | 0 | { |
538 | 0 | int ret = 0; |
539 | 0 | WC_DECLARE_VAR(cmac, Cmac, 1, 0); |
540 | |
|
541 | 0 | if (out == NULL || (in == NULL && inSz > 0) || key == NULL || keySz == 0) { |
542 | 0 | return BAD_FUNC_ARG; |
543 | 0 | } |
544 | | |
545 | 0 | #ifdef WOLFSSL_SMALL_STACK |
546 | 0 | if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, NULL, |
547 | 0 | DYNAMIC_TYPE_CMAC)) == NULL) { |
548 | 0 | return MEMORY_E; |
549 | 0 | } |
550 | 0 | #endif |
551 | | |
552 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
553 | | XMEMSET(((unsigned char *)cmac) + sizeof(Aes), 0xff, |
554 | | sizeof(Cmac) - sizeof(Aes)); |
555 | | /* Aes part is checked by wc_AesFree. */ |
556 | | wc_MemZero_Add("wc_AesCmacGenerate_ex cmac", |
557 | | ((unsigned char *)cmac) + sizeof(Aes), sizeof(Cmac) - sizeof(Aes)); |
558 | | #endif |
559 | | |
560 | 0 | ret = wc_AesCmacGenerate_ex(cmac, |
561 | 0 | out, outSz, |
562 | 0 | in, inSz, |
563 | 0 | key, keySz, |
564 | 0 | NULL, |
565 | 0 | INVALID_DEVID); |
566 | | |
567 | |
|
568 | 0 | #ifdef WOLFSSL_SMALL_STACK |
569 | 0 | XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC); |
570 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
571 | | wc_MemZero_Check(cmac, sizeof(Cmac)); |
572 | | #endif |
573 | |
|
574 | 0 | return ret; |
575 | 0 | } |
576 | | |
577 | | |
578 | | int wc_AesCmacVerify_ex(Cmac* cmac, |
579 | | const byte* check, word32 checkSz, |
580 | | const byte* in, word32 inSz, |
581 | | const byte* key, word32 keySz, |
582 | | void* heap, int devId) |
583 | 0 | { |
584 | 0 | int ret = 0; |
585 | 0 | byte a[WC_AES_BLOCK_SIZE]; |
586 | 0 | word32 aSz; |
587 | 0 | int compareRet; |
588 | |
|
589 | 0 | if (cmac == NULL || check == NULL || checkSz < WC_CMAC_TAG_MIN_SZ || |
590 | 0 | checkSz > WC_AES_BLOCK_SIZE || (in == NULL && inSz != 0)) { |
591 | 0 | return BAD_FUNC_ARG; |
592 | 0 | } |
593 | | |
594 | 0 | aSz = checkSz; |
595 | 0 | XMEMSET(a, 0, sizeof(a)); |
596 | 0 | ret = wc_AesCmacGenerate_ex(cmac, |
597 | 0 | a, &aSz, |
598 | 0 | in, inSz, |
599 | 0 | key, keySz, |
600 | 0 | heap, |
601 | 0 | devId); |
602 | | /* aSz is passed by reference to wc_AesCmacGenerate_ex, which on the |
603 | | * WOLF_CRYPTO_CB path forwards it to a user-supplied callback that may |
604 | | * write back any value. Reject anything that does not match the user |
605 | | * provided length. */ |
606 | 0 | if (ret == 0 && aSz != checkSz) { |
607 | 0 | ret = BAD_STATE_E; |
608 | 0 | } |
609 | 0 | if (ret == 0) { |
610 | 0 | compareRet = ConstantCompare(check, a, (int)aSz); |
611 | 0 | ret = compareRet ? MAC_CMP_FAILED_E : 0; |
612 | 0 | } |
613 | |
|
614 | 0 | return ret; |
615 | 0 | } |
616 | | |
617 | | |
618 | | int wc_AesCmacVerify(const byte* check, word32 checkSz, |
619 | | const byte* in, word32 inSz, |
620 | | const byte* key, word32 keySz) |
621 | 0 | { |
622 | 0 | int ret = 0; |
623 | 0 | WC_DECLARE_VAR(cmac, Cmac, 1, 0); |
624 | |
|
625 | 0 | if (check == NULL || checkSz < WC_CMAC_TAG_MIN_SZ || |
626 | 0 | checkSz > WC_AES_BLOCK_SIZE || (in == NULL && inSz > 0) || |
627 | 0 | key == NULL || keySz == 0) { |
628 | 0 | return BAD_FUNC_ARG; |
629 | 0 | } |
630 | | |
631 | 0 | #ifdef WOLFSSL_SMALL_STACK |
632 | 0 | if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, NULL, |
633 | 0 | DYNAMIC_TYPE_CMAC)) == NULL) { |
634 | 0 | return MEMORY_E; |
635 | 0 | } |
636 | 0 | #endif |
637 | | |
638 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
639 | | XMEMSET(((unsigned char *)cmac) + sizeof(Aes), 0xff, |
640 | | sizeof(Cmac) - sizeof(Aes)); |
641 | | /* Aes part is checked by wc_AesFree. */ |
642 | | wc_MemZero_Add("wc_AesCmacGenerate_ex cmac", |
643 | | ((unsigned char *)cmac) + sizeof(Aes), sizeof(Cmac) - sizeof(Aes)); |
644 | | #endif |
645 | | |
646 | 0 | ret = wc_AesCmacVerify_ex(cmac, |
647 | 0 | check, checkSz, |
648 | 0 | in, inSz, |
649 | 0 | key, keySz, |
650 | 0 | NULL, |
651 | 0 | INVALID_DEVID); |
652 | |
|
653 | 0 | #ifdef WOLFSSL_SMALL_STACK |
654 | 0 | XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC); |
655 | | #elif defined(WOLFSSL_CHECK_MEM_ZERO) |
656 | | wc_MemZero_Check(cmac, sizeof(Cmac)); |
657 | | #endif |
658 | |
|
659 | 0 | return ret; |
660 | 0 | } |
661 | | #endif /* !NO_AES && WOLFSSL_AES_DIRECT */ |
662 | | |
663 | | #endif /* WOLFSSL_CMAC */ |