Skip to content

Commit 614940a

Browse files
committed
hmac: refactor out hmac->hash clean-up
1 parent 2c0c28d commit 614940a

1 file changed

Lines changed: 42 additions & 76 deletions

File tree

wolfcrypt/src/hmac.c

Lines changed: 42 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,138 +1307,104 @@ int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap, int devId)
13071307
}
13081308
#endif /* WOLF_PRIVATE_KEY_ID */
13091309

1310-
/* Free Hmac from use with async device */
1311-
void wc_HmacFree(Hmac* hmac)
1310+
/* Free hash state. */
1311+
static void HmacFreeHash(int macType, wc_HmacHash* hash)
13121312
{
1313-
if (hmac == NULL)
1314-
return;
1315-
1316-
#ifdef WOLF_CRYPTO_CB
1317-
/* handle cleanup case where final is not called */
1318-
if (hmac->devId != INVALID_DEVID && hmac->devCtx != NULL) {
1319-
int ret;
1320-
byte finalHash[WC_HMAC_BLOCK_SIZE];
1321-
ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, finalHash);
1322-
(void)ret; /* must ignore return code here */
1323-
(void)finalHash;
1324-
}
1325-
#endif
1326-
1327-
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
1328-
wolfAsync_DevCtxFree(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC);
1329-
#endif /* WOLFSSL_ASYNC_CRYPT */
1330-
1331-
switch (hmac->macType) {
1313+
switch (macType) {
13321314
#ifndef NO_MD5
13331315
case WC_MD5:
1334-
wc_Md5Free(&hmac->hash.md5);
1335-
#ifdef WOLFSSL_HMAC_COPY_HASH
1336-
wc_Md5Free(&hmac->i_hash.md5);
1337-
wc_Md5Free(&hmac->o_hash.md5);
1338-
#endif
1316+
wc_Md5Free(&hash->md5);
13391317
break;
13401318
#endif /* !NO_MD5 */
13411319

13421320
#ifndef NO_SHA
13431321
case WC_SHA:
1344-
wc_ShaFree(&hmac->hash.sha);
1345-
#ifdef WOLFSSL_HMAC_COPY_HASH
1346-
wc_ShaFree(&hmac->i_hash.sha);
1347-
wc_ShaFree(&hmac->o_hash.sha);
1348-
#endif
1322+
wc_ShaFree(&hash->sha);
13491323
break;
13501324
#endif /* !NO_SHA */
13511325

13521326
#ifdef WOLFSSL_SHA224
13531327
case WC_SHA224:
1354-
wc_Sha224Free(&hmac->hash.sha224);
1355-
#ifdef WOLFSSL_HMAC_COPY_HASH
1356-
wc_Sha224Free(&hmac->i_hash.sha224);
1357-
wc_Sha224Free(&hmac->o_hash.sha224);
1358-
#endif
1328+
wc_Sha224Free(&hash->sha224);
13591329
break;
13601330
#endif /* WOLFSSL_SHA224 */
13611331
#ifndef NO_SHA256
13621332
case WC_SHA256:
1363-
wc_Sha256Free(&hmac->hash.sha256);
1364-
#ifdef WOLFSSL_HMAC_COPY_HASH
1365-
wc_Sha256Free(&hmac->i_hash.sha256);
1366-
wc_Sha256Free(&hmac->o_hash.sha256);
1367-
#endif
1333+
wc_Sha256Free(&hash->sha256);
13681334
break;
13691335
#endif /* !NO_SHA256 */
13701336

13711337
#ifdef WOLFSSL_SHA384
13721338
case WC_SHA384:
1373-
wc_Sha384Free(&hmac->hash.sha384);
1374-
#ifdef WOLFSSL_HMAC_COPY_HASH
1375-
wc_Sha384Free(&hmac->i_hash.sha384);
1376-
wc_Sha384Free(&hmac->o_hash.sha384);
1377-
#endif
1339+
wc_Sha384Free(&hash->sha384);
13781340
break;
13791341
#endif /* WOLFSSL_SHA384 */
13801342
#ifdef WOLFSSL_SHA512
13811343
case WC_SHA512:
1382-
wc_Sha512Free(&hmac->hash.sha512);
1383-
#ifdef WOLFSSL_HMAC_COPY_HASH
1384-
wc_Sha512Free(&hmac->i_hash.sha512);
1385-
wc_Sha512Free(&hmac->o_hash.sha512);
1386-
#endif
1344+
wc_Sha512Free(&hash->sha512);
13871345
break;
13881346
#endif /* WOLFSSL_SHA512 */
13891347

13901348
#ifdef WOLFSSL_SHA3
13911349
#ifndef WOLFSSL_NOSHA3_224
13921350
case WC_SHA3_224:
1393-
wc_Sha3_224_Free(&hmac->hash.sha3);
1394-
#ifdef WOLFSSL_HMAC_COPY_HASH
1395-
wc_Sha3_224_Free(&hmac->i_hash.sha3);
1396-
wc_Sha3_224_Free(&hmac->o_hash.sha3);
1397-
#endif
1351+
wc_Sha3_224_Free(&hash->sha3);
13981352
break;
13991353
#endif
14001354
#ifndef WOLFSSL_NOSHA3_256
14011355
case WC_SHA3_256:
1402-
wc_Sha3_256_Free(&hmac->hash.sha3);
1403-
#ifdef WOLFSSL_HMAC_COPY_HASH
1404-
wc_Sha3_256_Free(&hmac->i_hash.sha3);
1405-
wc_Sha3_256_Free(&hmac->o_hash.sha3);
1406-
#endif
1356+
wc_Sha3_256_Free(&hash->sha3);
14071357
break;
14081358
#endif
14091359
#ifndef WOLFSSL_NOSHA3_384
14101360
case WC_SHA3_384:
1411-
wc_Sha3_384_Free(&hmac->hash.sha3);
1412-
#ifdef WOLFSSL_HMAC_COPY_HASH
1413-
wc_Sha3_384_Free(&hmac->i_hash.sha3);
1414-
wc_Sha3_384_Free(&hmac->o_hash.sha3);
1415-
#endif
1361+
wc_Sha3_384_Free(&hash->sha3);
14161362
break;
14171363
#endif
14181364
#ifndef WOLFSSL_NOSHA3_512
14191365
case WC_SHA3_512:
1420-
wc_Sha3_512_Free(&hmac->hash.sha3);
1421-
#ifdef WOLFSSL_HMAC_COPY_HASH
1422-
wc_Sha3_512_Free(&hmac->i_hash.sha3);
1423-
wc_Sha3_512_Free(&hmac->o_hash.sha3);
1424-
#endif
1366+
wc_Sha3_512_Free(&hash->sha3);
14251367
break;
14261368
#endif
14271369
#endif /* WOLFSSL_SHA3 */
14281370

14291371
#ifdef WOLFSSL_SM3
14301372
case WC_SM3:
1431-
wc_Sm3Free(&hmac->hash.sm3);
1432-
#ifdef WOLFSSL_HMAC_COPY_HASH
1433-
wc_Sm3Free(&hmac->i_hash.sm3);
1434-
wc_Sm3Free(&hmac->o_hash.sm3);
1435-
#endif
1373+
wc_Sm3Free(&hash->sm3);
14361374
break;
14371375
#endif
14381376

14391377
default:
14401378
break;
14411379
}
1380+
}
1381+
1382+
/* Free Hmac from use with async device */
1383+
void wc_HmacFree(Hmac* hmac)
1384+
{
1385+
if (hmac == NULL)
1386+
return;
1387+
1388+
#ifdef WOLF_CRYPTO_CB
1389+
/* handle cleanup case where final is not called */
1390+
if (hmac->devId != INVALID_DEVID && hmac->devCtx != NULL) {
1391+
int ret;
1392+
byte finalHash[WC_HMAC_BLOCK_SIZE];
1393+
ret = wc_CryptoCb_Hmac(hmac, hmac->macType, NULL, 0, finalHash);
1394+
(void)ret; /* must ignore return code here */
1395+
(void)finalHash;
1396+
}
1397+
#endif
1398+
1399+
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
1400+
wolfAsync_DevCtxFree(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC);
1401+
#endif /* WOLFSSL_ASYNC_CRYPT */
1402+
1403+
HmacFreeHash(hmac->macType, &hmac->hash);
1404+
#ifdef WOLFSSL_HMAC_COPY_HASH
1405+
HmacFreeHash(hmac->macType, &hmac->i_hash);
1406+
HmacFreeHash(hmac->macType, &hmac->o_hash);
1407+
#endif
14421408

14431409
ForceZero(hmac, sizeof(*hmac));
14441410
}

0 commit comments

Comments
 (0)