Skip to content

Commit 0880f9e

Browse files
committed
Fixes Applied
1. Sign conversion warning fix (pwdbased.c) - Cast hLen to (word32) for ForceZero call 2. NULL checks before ForceZero (Copilot feedback) hpke.c (6 locations): - wc_HpkeEncap: NULL checks for dh and kemContext - wc_HpkeSetupBaseSender: NULL check for sharedSecret - wc_HpkeSealBase: NULL check for context - wc_HpkeDecap: NULL check for dh - wc_HpkeSetupBaseReceiver: NULL check for sharedSecret - wc_HpkeOpenBase: NULL check for context ecc.c (2 locations): - wc_ecc_encrypt_ex: NULL checks for sharedSecret and keys - wc_ecc_decrypt: NULL checks for sharedSecret and keys pwdbased.c (1 location): - wc_PBKDF2_ex: NULL check for buffer Not addressed (not real issues): - The INT_MAX overflow comments for evp.c and pkcs12.c are theoretical - digest sizes are always small (32-64 bytes), never close to INT_MAX Arduino CI failure: - Not related to this PR - it's a pre-existing issue with the external WiFiNINA library
1 parent 1461aaf commit 0880f9e

3 files changed

Lines changed: 36 additions & 12 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14484,8 +14484,12 @@ int wc_ecc_encrypt_ex(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
1448414484

1448514485
RESTORE_VECTOR_REGISTERS();
1448614486

14487-
ForceZero(sharedSecret, sharedSz);
14488-
ForceZero(keys, (word32)keysLen);
14487+
if (sharedSecret != NULL) {
14488+
ForceZero(sharedSecret, sharedSz);
14489+
}
14490+
if (keys != NULL) {
14491+
ForceZero(keys, (word32)keysLen);
14492+
}
1448914493
WC_FREE_VAR_EX(sharedSecret, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
1449014494
WC_FREE_VAR_EX(keys, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
1449114495

@@ -14884,8 +14888,12 @@ int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
1488414888
if (pubKey == peerKey)
1488514889
wc_ecc_free(peerKey);
1488614890
#endif
14887-
ForceZero(sharedSecret, sharedSz);
14888-
ForceZero(keys, (word32)keysLen);
14891+
if (sharedSecret != NULL) {
14892+
ForceZero(sharedSecret, sharedSz);
14893+
}
14894+
if (keys != NULL) {
14895+
ForceZero(keys, (word32)keysLen);
14896+
}
1488914897
#ifdef WOLFSSL_SMALL_STACK
1489014898
#ifndef WOLFSSL_ECIES_OLD
1489114899
XFREE(peerKey, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);

wolfcrypt/src/hpke.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,12 @@ static int wc_HpkeEncap(Hpke* hpke, void* ephemeralKey, void* receiverKey,
796796
hpke->Npk * 2, sharedSecret);
797797
}
798798

799-
ForceZero(dh, hpke->Ndh);
800-
ForceZero(kemContext, hpke->Npk * 2);
799+
if (dh != NULL) {
800+
ForceZero(dh, hpke->Ndh);
801+
}
802+
if (kemContext != NULL) {
803+
ForceZero(kemContext, hpke->Npk * 2);
804+
}
801805
WC_FREE_VAR_EX(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
802806
WC_FREE_VAR_EX(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
803807

@@ -829,7 +833,9 @@ static int wc_HpkeSetupBaseSender(Hpke* hpke, HpkeBaseContext* context,
829833
infoSz);
830834
}
831835

832-
ForceZero(sharedSecret, hpke->Nsecret);
836+
if (sharedSecret != NULL) {
837+
ForceZero(sharedSecret, hpke->Nsecret);
838+
}
833839
WC_FREE_VAR_EX(sharedSecret, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
834840

835841
return ret;
@@ -917,7 +923,9 @@ int wc_HpkeSealBase(Hpke* hpke, void* ephemeralKey, void* receiverKey,
917923

918924
PRIVATE_KEY_LOCK();
919925

920-
ForceZero(context, sizeof(HpkeBaseContext));
926+
if (context != NULL) {
927+
ForceZero(context, sizeof(HpkeBaseContext));
928+
}
921929
WC_FREE_VAR_EX(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
922930

923931
return ret;
@@ -1036,7 +1044,9 @@ static int wc_HpkeDecap(Hpke* hpke, void* receiverKey, const byte* pubKey,
10361044
hpke->Npk * 2, sharedSecret);
10371045
}
10381046

1039-
ForceZero(dh, hpke->Ndh);
1047+
if (dh != NULL) {
1048+
ForceZero(dh, hpke->Ndh);
1049+
}
10401050
WC_FREE_VAR_EX(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
10411051
WC_FREE_VAR_EX(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
10421052

@@ -1063,7 +1073,9 @@ static int wc_HpkeSetupBaseReceiver(Hpke* hpke, HpkeBaseContext* context,
10631073
infoSz);
10641074
}
10651075

1066-
ForceZero(sharedSecret, hpke->Nsecret);
1076+
if (sharedSecret != NULL) {
1077+
ForceZero(sharedSecret, hpke->Nsecret);
1078+
}
10671079
WC_FREE_VAR_EX(sharedSecret, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
10681080

10691081
return ret;
@@ -1150,7 +1162,9 @@ int wc_HpkeOpenBase(Hpke* hpke, void* receiverKey, const byte* pubKey,
11501162

11511163
PRIVATE_KEY_LOCK();
11521164

1153-
ForceZero(context, sizeof(HpkeBaseContext));
1165+
if (context != NULL) {
1166+
ForceZero(context, sizeof(HpkeBaseContext));
1167+
}
11541168
WC_FREE_VAR_EX(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
11551169

11561170
return ret;

wolfcrypt/src/pwdbased.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ int wc_PBKDF2_ex(byte* output, const byte* passwd, int pLen, const byte* salt,
296296
wc_HmacFree(hmac);
297297
}
298298

299-
ForceZero(buffer, hLen);
299+
if (buffer != NULL) {
300+
ForceZero(buffer, (word32)hLen);
301+
}
300302
WC_FREE_VAR_EX(buffer, heap, DYNAMIC_TYPE_TMP_BUFFER);
301303
WC_FREE_VAR_EX(hmac, heap, DYNAMIC_TYPE_HMAC);
302304

0 commit comments

Comments
 (0)