Skip to content

Commit 7592241

Browse files
authored
Merge pull request #8007 from billphipps/fix_cmac_cryptocb
Update to separate CMAC and AES conditional compiles. Correct update.
2 parents 72711b4 + 60dbe38 commit 7592241

3 files changed

Lines changed: 173 additions & 85 deletions

File tree

wolfcrypt/src/cmac.c

Lines changed: 122 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <wolfssl/wolfcrypt/hash.h>
3333
#endif
3434

35-
#if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
35+
#if defined(WOLFSSL_CMAC)
3636

3737
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
3838
/* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
@@ -80,7 +80,7 @@ int wc_CMAC_Grow(Cmac* cmac, const byte* in, int inSz)
8080
}
8181
#endif /* WOLFSSL_HASH_KEEP */
8282

83-
83+
#if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
8484
/* Used by AES-SIV. See aes.c. */
8585
void ShiftAndXorRb(byte* out, byte* in)
8686
{
@@ -100,6 +100,7 @@ void ShiftAndXorRb(byte* out, byte* in)
100100
}
101101
}
102102
}
103+
#endif /* !NO_AES && WOLFSSL_AES_DIRECT */
103104

104105
/* returns 0 on success */
105106
int wc_InitCmac_ex(Cmac* cmac, const byte* key, word32 keySz,
@@ -146,30 +147,40 @@ int wc_InitCmac_ex(Cmac* cmac, const byte* key, word32 keySz,
146147
return BAD_FUNC_ARG;
147148
}
148149

149-
ret = wc_AesInit(&cmac->aes, heap, devId);
150-
151-
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
152-
cmac->useSWCrypt = useSW;
153-
if (cmac->useSWCrypt == 1) {
154-
cmac->aes.useSWCrypt = 1;
155-
}
156-
#endif
150+
switch (type) {
151+
#if !defined (NO_AES) && defined(WOLFSSL_AES_DIRECT)
152+
case WC_CMAC_AES:
153+
cmac->type = WC_CMAC_AES;
154+
ret = wc_AesInit(&cmac->aes, heap, devId);
157155

158-
if (ret == 0) {
159-
ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION);
160-
}
156+
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
157+
cmac->useSWCrypt = useSW;
158+
if (cmac->useSWCrypt == 1) {
159+
cmac->aes.useSWCrypt = 1;
160+
}
161+
#endif
161162

162-
if (ret == 0) {
163-
byte l[AES_BLOCK_SIZE];
163+
if (ret == 0) {
164+
ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION);
165+
}
164166

165-
XMEMSET(l, 0, AES_BLOCK_SIZE);
166-
ret = wc_AesEncryptDirect(&cmac->aes, l, l);
167167
if (ret == 0) {
168-
ShiftAndXorRb(cmac->k1, l);
169-
ShiftAndXorRb(cmac->k2, cmac->k1);
170-
ForceZero(l, AES_BLOCK_SIZE);
168+
byte l[AES_BLOCK_SIZE];
169+
170+
XMEMSET(l, 0, AES_BLOCK_SIZE);
171+
ret = wc_AesEncryptDirect(&cmac->aes, l, l);
172+
if (ret == 0) {
173+
ShiftAndXorRb(cmac->k1, l);
174+
ShiftAndXorRb(cmac->k2, cmac->k1);
175+
ForceZero(l, AES_BLOCK_SIZE);
176+
}
171177
}
178+
break;
179+
#endif /* !NO_AES && WOLFSSL_AES_DIRECT */
180+
default:
181+
return BAD_FUNC_ARG;
172182
}
183+
173184
return ret;
174185
}
175186

@@ -201,7 +212,7 @@ int wc_CmacUpdate(Cmac* cmac, const byte* in, word32 inSz)
201212
#endif
202213
{
203214
ret = wc_CryptoCb_Cmac(cmac, NULL, 0, in, inSz,
204-
NULL, NULL, 0, NULL);
215+
NULL, NULL, cmac->type, NULL);
205216
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
206217
return ret;
207218
/* fall-through when unavailable */
@@ -211,26 +222,35 @@ int wc_CmacUpdate(Cmac* cmac, const byte* in, word32 inSz)
211222
/* Clear CRYPTOCB_UNAVAILABLE return code */
212223
ret = 0;
213224

214-
while ((ret == 0) && (inSz != 0)) {
215-
word32 add = min(inSz, AES_BLOCK_SIZE - cmac->bufferSz);
216-
XMEMCPY(&cmac->buffer[cmac->bufferSz], in, add);
217-
218-
cmac->bufferSz += add;
219-
in += add;
220-
inSz -= add;
221-
222-
if (cmac->bufferSz == AES_BLOCK_SIZE && inSz != 0) {
223-
if (cmac->totalSz != 0) {
224-
xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
225-
}
226-
ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
227-
if (ret == 0) {
228-
cmac->totalSz += AES_BLOCK_SIZE;
229-
cmac->bufferSz = 0;
225+
switch (cmac->type) {
226+
#if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
227+
case WC_CMAC_AES:
228+
{
229+
while ((ret == 0) && (inSz != 0)) {
230+
word32 add = min(inSz, AES_BLOCK_SIZE - cmac->bufferSz);
231+
XMEMCPY(&cmac->buffer[cmac->bufferSz], in, add);
232+
233+
cmac->bufferSz += add;
234+
in += add;
235+
inSz -= add;
236+
237+
if (cmac->bufferSz == AES_BLOCK_SIZE && inSz != 0) {
238+
if (cmac->totalSz != 0) {
239+
xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
240+
}
241+
ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest,
242+
cmac->buffer);
243+
if (ret == 0) {
244+
cmac->totalSz += AES_BLOCK_SIZE;
245+
cmac->bufferSz = 0;
246+
}
230247
}
231248
}
249+
}; break;
250+
#endif /* !NO_AES && WOLFSSL_AES_DIRECT */
251+
default:
252+
ret = BAD_FUNC_ARG;
232253
}
233-
234254
return ret;
235255
}
236256

@@ -244,16 +264,23 @@ int wc_CmacFree(Cmac* cmac)
244264
* wc_CmacFinal() not called. */
245265
XFREE(cmac->msg, cmac->heap, DYNAMIC_TYPE_TMP_BUFFER);
246266
#endif
247-
wc_AesFree(&cmac->aes);
267+
switch (cmac->type) {
268+
#if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
269+
case WC_CMAC_AES:
270+
wc_AesFree(&cmac->aes);
271+
break;
272+
#endif /* !NO_AES && WOLFSSL_AES_DIRECT */
273+
default:
274+
/* Nothing to do */
275+
(void)cmac;
276+
}
248277
ForceZero(cmac, sizeof(Cmac));
249278
return 0;
250279
}
251280

252281
int wc_CmacFinalNoFree(Cmac* cmac, byte* out, word32* outSz)
253282
{
254283
int ret = 0;
255-
const byte* subKey;
256-
word32 remainder;
257284

258285
if (cmac == NULL || out == NULL || outSz == NULL) {
259286
return BAD_FUNC_ARG;
@@ -267,44 +294,64 @@ int wc_CmacFinalNoFree(Cmac* cmac, byte* out, word32* outSz)
267294
if (cmac->devId != INVALID_DEVID)
268295
#endif
269296
{
270-
ret = wc_CryptoCb_Cmac(cmac, NULL, 0, NULL, 0, out, outSz, 0, NULL);
297+
ret = wc_CryptoCb_Cmac(cmac, NULL, 0, NULL, 0, out, outSz, cmac->type,
298+
NULL);
271299
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
272300
return ret;
273-
/* fall-through when unavailable */
274-
}
275-
#endif
276-
277-
if (cmac->bufferSz == AES_BLOCK_SIZE) {
278-
subKey = cmac->k1;
279-
}
280-
else {
281-
/* ensure we will have a valid remainder value */
282-
if (cmac->bufferSz > AES_BLOCK_SIZE) {
283-
return BAD_STATE_E;
284-
}
285-
remainder = AES_BLOCK_SIZE - cmac->bufferSz;
286301

287-
if (remainder == 0) {
288-
remainder = AES_BLOCK_SIZE;
289-
}
290-
if (remainder > 1) {
291-
XMEMSET(cmac->buffer + AES_BLOCK_SIZE - remainder, 0, remainder);
292-
}
302+
/* Clear CRYPTOCB_UNAVAILABLE return code */
303+
ret = 0;
293304

294-
cmac->buffer[AES_BLOCK_SIZE - remainder] = 0x80;
295-
subKey = cmac->k2;
305+
/* fall-through when unavailable */
296306
}
297-
xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
298-
xorbuf(cmac->buffer, subKey, AES_BLOCK_SIZE);
299-
ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
307+
#endif
300308
if (ret == 0) {
301-
XMEMCPY(out, cmac->digest, *outSz);
309+
switch (cmac->type) {
310+
#if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
311+
case WC_CMAC_AES:
312+
{
313+
const byte* subKey;
314+
word32 remainder;
315+
316+
if (cmac->bufferSz == AES_BLOCK_SIZE) {
317+
subKey = cmac->k1;
318+
}
319+
else {
320+
/* ensure we will have a valid remainder value */
321+
if (cmac->bufferSz > AES_BLOCK_SIZE) {
322+
ret = BAD_STATE_E;
323+
break;
324+
}
325+
remainder = AES_BLOCK_SIZE - cmac->bufferSz;
326+
327+
if (remainder == 0) {
328+
remainder = AES_BLOCK_SIZE;
329+
}
330+
if (remainder > 1) {
331+
XMEMSET(cmac->buffer + AES_BLOCK_SIZE - remainder, 0,
332+
remainder);
333+
}
334+
335+
cmac->buffer[AES_BLOCK_SIZE - remainder] = 0x80;
336+
subKey = cmac->k2;
337+
}
338+
xorbuf(cmac->buffer, cmac->digest, AES_BLOCK_SIZE);
339+
xorbuf(cmac->buffer, subKey, AES_BLOCK_SIZE);
340+
ret = wc_AesEncryptDirect(&cmac->aes, cmac->digest, cmac->buffer);
341+
if (ret == 0) {
342+
XMEMCPY(out, cmac->digest, *outSz);
343+
}
344+
}; break;
345+
#endif /* !NO_AES && WOLFSSL_AES_DIRECT */
346+
default:
347+
ret = BAD_FUNC_ARG;
348+
}
302349
}
303-
304-
return 0;
350+
return ret;
305351
}
306352

307-
int wc_CmacFinal(Cmac* cmac, byte* out, word32* outSz) {
353+
int wc_CmacFinal(Cmac* cmac, byte* out, word32* outSz)
354+
{
308355
int ret = 0;
309356

310357
if (cmac == NULL)
@@ -314,7 +361,7 @@ int wc_CmacFinal(Cmac* cmac, byte* out, word32* outSz) {
314361
return ret;
315362
}
316363

317-
364+
#if !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
318365
int wc_AesCmacGenerate_ex(Cmac* cmac,
319366
byte* out, word32* outSz,
320367
const byte* in, word32 inSz,
@@ -334,8 +381,6 @@ int wc_AesCmacGenerate_ex(Cmac* cmac,
334381
if (devId != INVALID_DEVID)
335382
#endif
336383
{
337-
cmac->devCtx = NULL;
338-
339384
ret = wc_CryptoCb_Cmac(cmac, key, keySz, in, inSz, out, outSz,
340385
WC_CMAC_AES, NULL);
341386
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
@@ -432,7 +477,8 @@ int wc_AesCmacVerify_ex(Cmac* cmac,
432477
word32 aSz = sizeof(a);
433478
int compareRet;
434479

435-
if (cmac == NULL || check == NULL || checkSz == 0 || (in == NULL && inSz != 0)) {
480+
if (cmac == NULL || check == NULL || checkSz == 0 ||
481+
(in == NULL && inSz != 0)) {
436482
return BAD_FUNC_ARG;
437483
}
438484

@@ -498,5 +544,6 @@ int wc_AesCmacVerify(const byte* check, word32 checkSz,
498544

499545
return ret;
500546
}
547+
#endif /* !NO_AES && WOLFSSL_AES_DIRECT */
501548

502-
#endif /* WOLFSSL_CMAC && NO_AES && WOLFSSL_AES_DIRECT */
549+
#endif /* WOLFSSL_CMAC */

0 commit comments

Comments
 (0)