Skip to content

Commit 31a76d3

Browse files
authored
Merge pull request #10468 from JeremiahM37/fenrir-wolfcrypt-api-hardening
wolfCrypt API hardening: input validation, key zeroization, hardware ports
2 parents 8d08ff8 + b235af7 commit 31a76d3

9 files changed

Lines changed: 76 additions & 7 deletions

File tree

tests/api/test_rc2.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,28 @@ int test_wc_Rc2Cbc_MonteCarlo(void)
284284
#endif
285285
return EXPECT_RESULT();
286286
}
287+
288+
/*
289+
* Testing function for wc_Rc2Free().
290+
*/
291+
int test_wc_Rc2Free(void)
292+
{
293+
EXPECT_DECLS;
294+
#ifdef WC_RC2
295+
Rc2 rc2;
296+
byte key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
297+
byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
298+
byte zero[sizeof(rc2)];
299+
300+
XMEMSET(&rc2, 0, sizeof(rc2));
301+
XMEMSET(zero, 0, sizeof(zero));
302+
303+
wc_Rc2Free(NULL);
304+
305+
ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32)sizeof(key), iv, 40), 0);
306+
ExpectIntNE(XMEMCMP(&rc2, zero, sizeof(rc2)), 0);
307+
wc_Rc2Free(&rc2);
308+
ExpectIntEQ(XMEMCMP(&rc2, zero, sizeof(rc2)), 0);
309+
#endif
310+
return EXPECT_RESULT();
311+
}

tests/api/test_rc2.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ int test_wc_Rc2SetIV(void);
2929
int test_wc_Rc2EcbEncryptDecrypt(void);
3030
int test_wc_Rc2CbcEncryptDecrypt(void);
3131
int test_wc_Rc2Cbc_MonteCarlo(void);
32+
int test_wc_Rc2Free(void);
3233

3334
#define TEST_RC2_DECLS \
3435
TEST_DECL_GROUP("rc2", test_wc_Rc2SetKey), \
3536
TEST_DECL_GROUP("rc2", test_wc_Rc2SetIV), \
3637
TEST_DECL_GROUP("rc2", test_wc_Rc2EcbEncryptDecrypt), \
3738
TEST_DECL_GROUP("rc2", test_wc_Rc2CbcEncryptDecrypt), \
38-
TEST_DECL_GROUP("rc2", test_wc_Rc2Cbc_MonteCarlo)
39+
TEST_DECL_GROUP("rc2", test_wc_Rc2Cbc_MonteCarlo), \
40+
TEST_DECL_GROUP("rc2", test_wc_Rc2Free)
3941

4042
#endif /* WOLFCRYPT_TEST_RC2_H */

wolfcrypt/src/curve25519.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ int wc_curve25519_make_pub_blind(int public_size, byte* pub, int private_size,
309309
if ((pub == NULL) || (priv == NULL)) {
310310
return ECC_BAD_ARG_E;
311311
}
312+
#ifndef FREESCALE_LTC_ECC
313+
if (rng == NULL) {
314+
return ECC_BAD_ARG_E;
315+
}
316+
#endif
312317

313318
/* check clamping */
314319
ret = curve25519_priv_clamp_check(priv);
@@ -420,6 +425,9 @@ int wc_curve25519_generic_blind(int public_size, byte* pub,
420425
}
421426
if ((pub == NULL) || (priv == NULL) || (basepoint == NULL))
422427
return ECC_BAD_ARG_E;
428+
if (rng == NULL) {
429+
return ECC_BAD_ARG_E;
430+
}
423431

424432
/* check clamping */
425433
ret = curve25519_priv_clamp_check(priv);

wolfcrypt/src/port/Renesas/renesas_tsip_rsa.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static int tsip_RsakeyImport(TsipUserCtx* tuc)
251251
*/
252252
int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
253253
{
254-
int ret;
254+
int ret = CRYPTOCB_UNAVAILABLE;
255255
int keySize;
256256
int type;
257257
tsip_rsa_byte_data_t plain, cipher;
@@ -321,6 +321,9 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
321321
*(info->pk.rsa.outLen) = plain.data_length;
322322
}
323323
}
324+
else {
325+
ret = CRYPTOCB_UNAVAILABLE;
326+
}
324327
tsip_hw_unlock();
325328
}
326329
}

wolfcrypt/src/port/nxp/casper_port.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ int casper_rsa_public_exptmod(
5252
int res;
5353
int sig_sz = inLen;
5454
int key_sz = mp_unsigned_bin_size(&key->n);
55-
word32 exp = 0;
55+
int exp_sz = mp_unsigned_bin_size(&key->e);
56+
uint8_t exp_buf[sizeof(uint32_t)];
57+
uint32_t exp = 0;
5658

5759
if (inLen > CASPER_MAX_BUF_SZ || *outLen > CASPER_MAX_BUF_SZ)
5860
return BAD_FUNC_ARG;
5961

62+
/* casper only accepts a 32-bit public exponent */
63+
if (exp_sz <= 0 || exp_sz > (int)sizeof(exp_buf))
64+
return BAD_FUNC_ARG;
65+
6066
/* casper requires little endian format for inputs/outputs */
6167
XMEMCPY(sig_buf, in, sig_sz);
6268
mp_reverse(sig_buf, sig_sz);
@@ -65,8 +71,13 @@ int casper_rsa_public_exptmod(
6571
return res;
6672
mp_reverse(key_buf, key_sz);
6773

68-
if ((res = mp_to_unsigned_bin(&key->e, (uint8_t *)&exp)) != MP_OKAY)
74+
XMEMSET(exp_buf, 0, sizeof(exp_buf));
75+
if ((res = mp_to_unsigned_bin(&key->e,
76+
exp_buf + sizeof(exp_buf) - exp_sz))
77+
!= MP_OKAY)
6978
return res;
79+
exp = ((uint32_t)exp_buf[0] << 24) | ((uint32_t)exp_buf[1] << 16) |
80+
((uint32_t)exp_buf[2] << 8) | ((uint32_t)exp_buf[3]);
7081

7182
CASPER_ModExp(CASPER, (void *)sig_buf, (void *)key_buf,
7283
key_sz / sizeof(uint32_t), exp, out_buf);

wolfcrypt/src/rc2.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,13 @@ int wc_Rc2CbcDecrypt(Rc2* rc2, byte* out, const byte* in, word32 sz)
348348
}
349349

350350

351+
void wc_Rc2Free(Rc2* rc2)
352+
{
353+
if (rc2 == NULL)
354+
return;
355+
ForceZero(rc2, sizeof(Rc2));
356+
}
357+
358+
351359
#endif /* WC_RC2 */
352360

wolfcrypt/src/wc_encrypt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ int wc_CryptKey(const char* password, int passwordSz, const byte* salt,
649649
else
650650
ret = wc_Rc2CbcDecrypt(&rc2, input, input, length);
651651
}
652-
ForceZero(&rc2, sizeof(Rc2));
652+
wc_Rc2Free(&rc2);
653653
break;
654654
}
655655
#endif

wolfcrypt/src/wolfentropy.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@ int wc_Entropy_GetRawEntropy(unsigned char* raw, int cnt)
495495
int ret = 0;
496496
int locked = 0;
497497

498+
if (raw == NULL || cnt <= 0) {
499+
return BAD_FUNC_ARG;
500+
}
501+
498502
#ifdef HAVE_FIPS
499503
if (!entropy_memuse_initialized) {
500504
ret = Entropy_Init();
@@ -809,10 +813,16 @@ static int Entropy_Condition(byte* output, word32 len, byte* noise,
809813
int wc_Entropy_Get(int bits, unsigned char* entropy, word32 len)
810814
{
811815
int ret = 0;
816+
int noise_len;
817+
static byte noise[MAX_NOISE_CNT];
818+
819+
if (bits <= 0 || (entropy == NULL && len > 0)) {
820+
return BAD_FUNC_ARG;
821+
}
822+
812823
/* Noise length is the number of 8 byte samples required to get the bits of
813824
* entropy requested. */
814-
int noise_len = (bits + ENTROPY_EXTRA) / ENTROPY_MIN;
815-
static byte noise[MAX_NOISE_CNT];
825+
noise_len = (bits + ENTROPY_EXTRA) / ENTROPY_MIN;
816826

817827
#ifdef HAVE_FIPS
818828
/* FIPS KATs, e.g. EccPrimitiveZ_KnownAnswerTest(), call wc_Entropy_Get()

wolfssl/wolfcrypt/rc2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ WOLFSSL_API int wc_Rc2CbcEncrypt(Rc2* rc2, byte* out,
6060
WOLFSSL_API int wc_Rc2CbcDecrypt(Rc2* rc2, byte* out,
6161
const byte* in, word32 sz);
6262

63+
WOLFSSL_API void wc_Rc2Free(Rc2* rc2);
64+
6365
#ifdef __cplusplus
6466
} /* extern "C" */
6567
#endif

0 commit comments

Comments
 (0)