Skip to content

Commit 3c8af83

Browse files
committed
wc: ForceZero only sensitive fields in HMAC and SHA-2
1 parent 614940a commit 3c8af83

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

wolfcrypt/src/hmac.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,9 @@ void wc_HmacFree(Hmac* hmac)
14061406
HmacFreeHash(hmac->macType, &hmac->o_hash);
14071407
#endif
14081408

1409-
ForceZero(hmac, sizeof(*hmac));
1409+
ForceZero(hmac->ipad, sizeof(hmac->ipad));
1410+
ForceZero(hmac->opad, sizeof(hmac->opad));
1411+
ForceZero(hmac->innerHash, sizeof(hmac->innerHash));
14101412
}
14111413
#endif /* WOLFSSL_KCAPI_HMAC */
14121414

wolfcrypt/src/sha256.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,19 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
23472347
#if defined(PSOC6_HASH_SHA2)
23482348
wc_Psoc6_Sha_Free();
23492349
#endif
2350+
#if !defined(FREESCALE_LTC_SHA) && \
2351+
!(defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)) && \
2352+
!defined(STM32_HASH_SHA2) && \
2353+
!defined(WOLFSSL_SILABS_SE_ACCEL) && \
2354+
!defined(WOLFSSL_IMXRT_DCP) && \
2355+
!defined(PSOC6_HASH_SHA2)
2356+
/* PSA compiles out the free function completely */
2357+
ForceZero(sha224->buffer, sizeof(sha224->buffer));
2358+
if (sha224->hiLen != 0 || sha224->loLen != 0)
2359+
ForceZero(sha224->digest, sizeof(sha224->digest));
2360+
#else
23502361
ForceZero(sha224, sizeof(*sha224));
2362+
#endif
23512363
}
23522364
#endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */
23532365
#endif /* WOLFSSL_SHA224 */
@@ -2494,7 +2506,19 @@ void wc_Sha256Free(wc_Sha256* sha256)
24942506
wc_Psoc6_Sha_Free();
24952507
#endif
24962508

2509+
#if !defined(FREESCALE_LTC_SHA) && \
2510+
!(defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH)) && \
2511+
!defined(STM32_HASH_SHA2) && \
2512+
!defined(WOLFSSL_SILABS_SE_ACCEL) && \
2513+
!defined(WOLFSSL_IMXRT_DCP) && \
2514+
!defined(PSOC6_HASH_SHA2)
2515+
/* PSA compiles out the free function completely */
2516+
ForceZero(sha256->buffer, sizeof(sha256->buffer));
2517+
if (sha256->hiLen != 0 || sha256->loLen != 0)
2518+
ForceZero(sha256->digest, sizeof(sha256->digest));
2519+
#else
24972520
ForceZero(sha256, sizeof(*sha256));
2521+
#endif
24982522
} /* wc_Sha256Free */
24992523

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

wolfcrypt/src/sha512.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,13 @@ void wc_Sha512Free(wc_Sha512* sha512)
16881688
wc_Psoc6_Sha_Free();
16891689
#endif
16901690

1691+
#if !defined(PSOC6_HASH_SHA2)
1692+
ForceZero(sha512->buffer, sizeof(sha512->buffer));
1693+
if (!(sha512->hiLen == 0 && sha512->loLen == 0))
1694+
ForceZero(sha512->digest, sizeof(sha512->digest));
1695+
#else
16911696
ForceZero(sha512, sizeof(*sha512));
1697+
#endif
16921698
}
16931699
#endif
16941700

@@ -2176,7 +2182,13 @@ void wc_Sha384Free(wc_Sha384* sha384)
21762182
wc_MXC_TPU_SHA_Free(&(sha384->mxcCtx));
21772183
#endif
21782184

2185+
#if !defined(PSOC6_HASH_SHA2)
2186+
ForceZero(sha384->buffer, sizeof(sha384->buffer));
2187+
if (!(sha384->hiLen == 0 && sha384->loLen == 0))
2188+
ForceZero(sha384->digest, sizeof(sha384->digest));
2189+
#else
21792190
ForceZero(sha384, sizeof(*sha384));
2191+
#endif
21802192
}
21812193

21822194
#endif

0 commit comments

Comments
 (0)