Skip to content

Commit 344e166

Browse files
committed
wolfcrypt/src/{hmac.c,sha256.c,sha512.c,kdf.c}: ForceZero() smallstack buffers before freeing them, and ForceZero() the Hmac, wc_Sha512, wc_Sha384, wc_Sha256, and wc_Sha224 structures at the end of their respective freeing routines. also, remove superseded ForceZero() calls in wc_HKDF_Expand(), wc_SSH_KDF(), and wc_HKDF_Extract().
1 parent 5540449 commit 344e166

4 files changed

Lines changed: 26 additions & 11 deletions

File tree

wolfcrypt/src/hmac.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,8 @@ void wc_HmacFree(Hmac* hmac)
11731173
default:
11741174
break;
11751175
}
1176+
1177+
ForceZero(hmac, sizeof(*hmac));
11761178
}
11771179
#endif /* WOLFSSL_KCAPI_HMAC */
11781180

@@ -1233,7 +1235,6 @@ int wolfSSL_GetHmacMaxSize(void)
12331235
ret = wc_HmacUpdate(myHmac, inKey, inKeySz);
12341236
if (ret == 0)
12351237
ret = wc_HmacFinal(myHmac, out);
1236-
ForceZero(myHmac, sizeof(Hmac));
12371238
wc_HmacFree(myHmac);
12381239
}
12391240
#ifdef WOLFSSL_SMALL_STACK
@@ -1325,7 +1326,6 @@ int wolfSSL_GetHmacMaxSize(void)
13251326
n++;
13261327
}
13271328

1328-
ForceZero(myHmac, sizeof(Hmac));
13291329
wc_HmacFree(myHmac);
13301330
#ifdef WOLFSSL_SMALL_STACK
13311331
XFREE(myHmac, NULL, DYNAMIC_TYPE_HMAC);

wolfcrypt/src/kdf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,6 @@ int wc_SSH_KDF(byte hashId, byte keyId, byte* key, word32 keySz,
863863
}
864864
}
865865

866-
ForceZero(&hash, sizeof(hash));
867866
_HashFree(enmhashId, &hash);
868867

869868
return ret;

wolfcrypt/src/sha256.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ static int InitSha256(wc_Sha256* sha256)
931931
}
932932

933933
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE)
934+
ForceZero(W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
934935
XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
935936
#endif
936937
return 0;
@@ -1690,10 +1691,11 @@ static int InitSha256(wc_Sha256* sha256)
16901691
return;
16911692

16921693
#ifdef WOLFSSL_SMALL_STACK_CACHE
1693-
if (sha224->W != NULL) {
1694-
XFREE(sha224->W, NULL, DYNAMIC_TYPE_DIGEST);
1695-
sha224->W = NULL;
1696-
}
1694+
if (sha224->W != NULL) {
1695+
ForceZero(sha224->W, sizeof(word32) * WC_SHA224_BLOCK_SIZE);
1696+
XFREE(sha224->W, NULL, DYNAMIC_TYPE_DIGEST);
1697+
sha224->W = NULL;
1698+
}
16971699
#endif
16981700

16991701
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
@@ -1707,11 +1709,13 @@ static int InitSha256(wc_Sha256* sha256)
17071709
KcapiHashFree(&sha224->kcapi);
17081710
#endif
17091711
#if defined(WOLFSSL_RENESAS_RX64_HASH)
1710-
if (sha224->msg != NULL) {
1711-
XFREE(sha224->msg, sha224->heap, DYNAMIC_TYPE_TMP_BUFFER);
1712-
sha224->msg = NULL;
1713-
}
1712+
if (sha224->msg != NULL) {
1713+
ForceZero(sha224->msg, sha224->len);
1714+
XFREE(sha224->msg, sha224->heap, DYNAMIC_TYPE_TMP_BUFFER);
1715+
sha224->msg = NULL;
1716+
}
17141717
#endif
1718+
ForceZero(sha224, sizeof(*sha224));
17151719
}
17161720
#endif /* WOLFSSL_SHA224 */
17171721
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
@@ -1737,6 +1741,7 @@ void wc_Sha256Free(wc_Sha256* sha256)
17371741

17381742
#ifdef WOLFSSL_SMALL_STACK_CACHE
17391743
if (sha256->W != NULL) {
1744+
ForceZero(sha256->W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
17401745
XFREE(sha256->W, NULL, DYNAMIC_TYPE_DIGEST);
17411746
sha256->W = NULL;
17421747
}
@@ -1772,6 +1777,7 @@ void wc_Sha256Free(wc_Sha256* sha256)
17721777
defined(WOLFSSL_HASH_KEEP)
17731778

17741779
if (sha256->msg != NULL) {
1780+
ForceZero(sha256->msg, sha256->len);
17751781
XFREE(sha256->msg, sha256->heap, DYNAMIC_TYPE_TMP_BUFFER);
17761782
sha256->msg = NULL;
17771783
}
@@ -1813,6 +1819,7 @@ void wc_Sha256Free(wc_Sha256* sha256)
18131819
ESP_LOGV(TAG, "Hardware unlock not needed in wc_Sha256Free.");
18141820
}
18151821
#endif
1822+
ForceZero(sha256, sizeof(*sha256));
18161823
}
18171824

18181825
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */

wolfcrypt/src/sha512.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ void wc_Sha512Free(wc_Sha512* sha512)
11261126

11271127
#ifdef WOLFSSL_SMALL_STACK_CACHE
11281128
if (sha512->W != NULL) {
1129+
ForceZero(sha512->W, sizeof(word64) * 16);
11291130
XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
11301131
sha512->W = NULL;
11311132
}
@@ -1137,6 +1138,7 @@ void wc_Sha512Free(wc_Sha512* sha512)
11371138

11381139
#if defined(WOLFSSL_HASH_KEEP)
11391140
if (sha512->msg != NULL) {
1141+
ForceZero(sha512->msg, sha512->len);
11401142
XFREE(sha512->msg, sha512->heap, DYNAMIC_TYPE_TMP_BUFFER);
11411143
sha512->msg = NULL;
11421144
}
@@ -1145,6 +1147,8 @@ void wc_Sha512Free(wc_Sha512* sha512)
11451147
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
11461148
wolfAsync_DevCtxFree(&sha512->asyncDev, WOLFSSL_ASYNC_MARKER_SHA512);
11471149
#endif /* WOLFSSL_ASYNC_CRYPT */
1150+
1151+
ForceZero(sha512, sizeof(*sha512));
11481152
}
11491153
#if (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) \
11501154
&& !defined(WOLFSSL_KCAPI_HASH)
@@ -1197,6 +1201,7 @@ int wc_Sha512Transform(wc_Sha512* sha, const unsigned char* data)
11971201

11981202
XMEMCPY(sha->buffer, buffer, WC_SHA512_BLOCK_SIZE);
11991203
#ifdef WOLFSSL_SMALL_STACK
1204+
ForceZero(buffer, WC_SHA512_BLOCK_SIZE);
12001205
XFREE(buffer, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
12011206
#endif
12021207
return ret;
@@ -1446,6 +1451,7 @@ void wc_Sha384Free(wc_Sha384* sha384)
14461451

14471452
#ifdef WOLFSSL_SMALL_STACK_CACHE
14481453
if (sha384->W != NULL) {
1454+
ForceZero(sha384->W, sizeof(word64) * 16);
14491455
XFREE(sha384->W, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
14501456
sha384->W = NULL;
14511457
}
@@ -1457,6 +1463,7 @@ void wc_Sha384Free(wc_Sha384* sha384)
14571463

14581464
#if defined(WOLFSSL_HASH_KEEP)
14591465
if (sha384->msg != NULL) {
1466+
ForceZero(sha384->msg, sha384->len);
14601467
XFREE(sha384->msg, sha384->heap, DYNAMIC_TYPE_TMP_BUFFER);
14611468
sha384->msg = NULL;
14621469
}
@@ -1476,6 +1483,8 @@ void wc_Sha384Free(wc_Sha384* sha384)
14761483
sha384->hSession = NULL;
14771484
}
14781485
#endif
1486+
1487+
ForceZero(sha384, sizeof(*sha384));
14791488
}
14801489

14811490
#endif /* WOLFSSL_SHA384 */

0 commit comments

Comments
 (0)