@@ -1054,6 +1054,14 @@ int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen,
10541054 return BAD_FUNC_ARG;
10551055 }
10561056
1057+ #ifdef WOLFSSL_CHECK_MEM_ZERO
1058+ /* Poison and register firstExpand before it is written so that any path
1059+ * below (all of which funnel through cleanup) is covered. */
1060+ XMEMSET(firstExpand, 0xff, sizeof(firstExpand));
1061+ wc_MemZero_Add("Tls13_Exporter firstExpand", firstExpand,
1062+ sizeof(firstExpand));
1063+ #endif
1064+
10571065 /* Derive-Secret(Secret, label, "") */
10581066 ret = Tls13HKDFExpandLabel(ssl, firstExpand, hashLen,
10591067 ssl->arrays->exporterSecret, hashLen,
@@ -1076,6 +1084,9 @@ int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen,
10761084 * Hash(context_value); wipe both before the stack frame is reclaimed. */
10771085 ForceZero(firstExpand, sizeof(firstExpand));
10781086 ForceZero(hashOut, sizeof(hashOut));
1087+ #ifdef WOLFSSL_CHECK_MEM_ZERO
1088+ wc_MemZero_Check(firstExpand, sizeof(firstExpand));
1089+ #endif
10791090 return ret;
10801091}
10811092#endif
@@ -1533,6 +1544,11 @@ int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
15331544 WC_ALLOC_VAR_EX(key_dig, byte, MAX_PRF_DIG, ssl->heap,
15341545 DYNAMIC_TYPE_DIGEST, return MEMORY_E);
15351546
1547+ #ifdef WOLFSSL_CHECK_MEM_ZERO
1548+ XMEMSET(key_dig, 0xff, MAX_PRF_DIG);
1549+ wc_MemZero_Add("DeriveTls13Keys key_dig", key_dig, MAX_PRF_DIG);
1550+ #endif
1551+
15361552 if (side == ENCRYPT_AND_DECRYPT_SIDE) {
15371553 provision = PROVISION_CLIENT_SERVER;
15381554 }
@@ -1717,7 +1733,9 @@ int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
17171733#endif /* WOLFSSL_DTLS13 */
17181734
17191735end:
1720- ForceZero(key_dig, (word32)i);
1736+ /* Zero the whole key_dig buffer (not just the i bytes derived) so no
1737+ * key-schedule material can linger in the unused tail. */
1738+ ForceZero(key_dig, MAX_PRF_DIG);
17211739#ifdef WOLFSSL_SMALL_STACK
17221740 XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
17231741#elif defined(WOLFSSL_CHECK_MEM_ZERO)
@@ -6523,6 +6541,13 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
65236541
65246542 (void)suite;
65256543
6544+ #ifdef WOLFSSL_CHECK_MEM_ZERO
6545+ /* Poison and register binderKey up front; every exit below (including the
6546+ * error paths) funnels through the cleanup label which zeroes it. */
6547+ XMEMSET(binderKey, 0xff, sizeof(binderKey));
6548+ wc_MemZero_Add("DoPreSharedKeys binderKey", binderKey, sizeof(binderKey));
6549+ #endif
6550+
65266551 ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
65276552 if (ext == NULL) {
65286553 WOLFSSL_MSG("No pre shared extension keys found");
@@ -6739,6 +6764,9 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
67396764cleanup:
67406765 ForceZero(binderKey, sizeof(binderKey));
67416766 ForceZero(binder, sizeof(binder));
6767+ #ifdef WOLFSSL_CHECK_MEM_ZERO
6768+ wc_MemZero_Check(binderKey, sizeof(binderKey));
6769+ #endif
67426770 WOLFSSL_LEAVE("DoPreSharedKeys", ret);
67436771
67446772 return ret;
0 commit comments