Skip to content

Commit 5277556

Browse files
Merge pull request #10264 from JeremiahM37/fenrir-issues-5
Harden wolfCrypt input validation and zeroization
2 parents 2ba4d7e + d5312ba commit 5277556

10 files changed

Lines changed: 35 additions & 10 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10062,8 +10062,11 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
1006210062
int ret;
1006310063

1006410064
/* argument checks */
10065-
if (aes == NULL || authTagSz > WC_AES_BLOCK_SIZE || ivSz == 0 ||
10066-
((authTagSz > 0) && (authTag == NULL)) ||
10065+
/* If sz is non-zero, both in and out must be set; if sz is 0, in and
10066+
* out are don't cares (GMAC case), matching wc_AesGcmDecrypt. */
10067+
if (aes == NULL || iv == NULL || ivSz == 0 ||
10068+
(sz != 0 && (in == NULL || out == NULL)) ||
10069+
authTag == NULL || authTagSz > WC_AES_BLOCK_SIZE ||
1006710070
((authInSz > 0) && (authIn == NULL)))
1006810071
{
1006910072
return BAD_FUNC_ARG;
@@ -17140,7 +17143,8 @@ int wc_AesEaxEncryptFinal(AesEax* eax, byte* authTag, word32 authTagSz)
1714017143
int ret;
1714117144
word32 i;
1714217145

17143-
if (eax == NULL || authTag == NULL || authTagSz > WC_AES_BLOCK_SIZE) {
17146+
if (eax == NULL || authTag == NULL || authTagSz == 0 ||
17147+
authTagSz > WC_AES_BLOCK_SIZE) {
1714417148
return BAD_FUNC_ARG;
1714517149
}
1714617150

@@ -17197,7 +17201,8 @@ int wc_AesEaxDecryptFinal(AesEax* eax,
1719717201
byte authTag[WC_AES_BLOCK_SIZE];
1719817202
#endif
1719917203

17200-
if (eax == NULL || authIn == NULL || authInSz > WC_AES_BLOCK_SIZE) {
17204+
if (eax == NULL || authIn == NULL || authInSz == 0 ||
17205+
authInSz > WC_AES_BLOCK_SIZE) {
1720117206
return BAD_FUNC_ARG;
1720217207
}
1720317208

wolfcrypt/src/arc4.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525

2626
#include <wolfssl/wolfcrypt/arc4.h>
2727

28+
#ifdef NO_INLINE
29+
#include <wolfssl/wolfcrypt/misc.h>
30+
#else
31+
#define WOLFSSL_MISC_INCLUDED
32+
#include <wolfcrypt/src/misc.c>
33+
#endif
34+
2835

2936
int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
3037
{
@@ -137,6 +144,10 @@ void wc_Arc4Free(Arc4* arc4)
137144
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
138145
wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4);
139146
#endif /* WOLFSSL_ASYNC_CRYPT */
147+
148+
ForceZero(arc4->state, sizeof(arc4->state));
149+
arc4->x = 0;
150+
arc4->y = 0;
140151
}
141152

142153
#endif /* NO_RC4 */

wolfcrypt/src/camellia.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ int wc_CamelliaSetKey(wc_Camellia* cam, const byte* key, word32 len, const byte*
15211521
{
15221522
int ret = 0;
15231523

1524-
if (cam == NULL) return BAD_FUNC_ARG;
1524+
if (cam == NULL || key == NULL) return BAD_FUNC_ARG;
15251525

15261526
XMEMSET(cam->key, 0, WC_CAMELLIA_TABLE_BYTE_LEN);
15271527

wolfcrypt/src/compress.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ int wc_Compress_ex(byte* out, word32 outSz, const byte* in, word32 inSz,
8181
z_stream stream;
8282
int result = 0;
8383

84+
if (out == NULL || in == NULL)
85+
return BAD_FUNC_ARG;
86+
8487
stream.next_in = (Bytef*)in;
8588
stream.avail_in = (uInt)inSz;
8689
#ifdef MAXSEG_64K
@@ -149,6 +152,9 @@ int wc_DeCompress_ex(byte* out, word32 outSz, const byte* in, word32 inSz,
149152
z_stream stream;
150153
int result = 0;
151154

155+
if (out == NULL || in == NULL)
156+
return BAD_FUNC_ARG;
157+
152158
stream.next_in = (Bytef*)in;
153159
stream.avail_in = (uInt)inSz;
154160
/* Check for source > 64K on 16-bit machine: */

wolfcrypt/src/des3.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,7 @@ void wc_Des3Free(Des3* des3)
20022002
(defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES))
20032003
ForceZero(des3->devKey, sizeof(des3->devKey));
20042004
#endif
2005+
ForceZero(des3, sizeof(Des3));
20052006
#ifdef WOLFSSL_CHECK_MEM_ZERO
20062007
wc_MemZero_Check(des3, sizeof(Des3));
20072008
#endif

wolfcrypt/src/dsa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ int wc_MakeDsaKey(WC_RNG *rng, DsaKey *dsa)
174174
SAVE_VECTOR_REGISTERS(;);
175175

176176
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
177-
if ((tmpQ = (mp_int *)XMALLOC(sizeof(*tmpQ), NULL,
178-
DYNAMIC_TYPE_WOLF_BIGINT)) == NULL)
177+
if ((tmpQ = (mp_int *)XMALLOC(sizeof(*tmpQ), dsa->heap,
178+
DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
179179
err = MEMORY_E;
180180
else
181181
err = MP_OKAY;

wolfcrypt/src/hmac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ int wc_HmacInit_Id(Hmac* hmac, unsigned char* id, int len, void* heap,
13951395

13961396
if (ret == 0)
13971397
ret = wc_HmacInit(hmac, heap, devId);
1398-
if (ret == 0) {
1398+
if (ret == 0 && id != NULL && len != 0) {
13991399
XMEMCPY(hmac->id, id, (size_t)len);
14001400
hmac->idLen = len;
14011401
}

wolfcrypt/src/rng_bank.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ WOLFSSL_API int wc_rng_bank_init(
5252

5353
#ifdef WC_RNG_BANK_STATIC
5454
if (n_rngs > WC_RNG_BANK_STATIC_SIZE)
55-
return BAD_LENGTH_E;
55+
ret = BAD_LENGTH_E;
5656
#else
5757
ctx->rngs = (struct wc_rng_bank_inst *)
5858
XMALLOC(sizeof(*ctx->rngs) * (size_t)n_rngs,

wolfcrypt/src/wc_lms.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,8 @@ int wc_LmsKey_ExportPubRaw(const LmsKey* key, byte* out, word32* outLen)
11591159
int ret = 0;
11601160

11611161
/* Validate parameters. */
1162-
if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
1162+
if ((key == NULL) || (out == NULL) || (outLen == NULL) ||
1163+
(key->params == NULL)) {
11631164
ret = BAD_FUNC_ARG;
11641165
}
11651166
/* Check size of out is sufficient. */

wolfcrypt/src/wc_lms_impl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,7 @@ static int wc_hss_next_subtree_inc(LmsState* state, HssPrivKey* priv_key,
28552855
q64_hi = cq64_hi;
28562856
}
28572857

2858+
ForceZero(tmp_priv, sizeof(tmp_priv));
28582859
return ret;
28592860
}
28602861

0 commit comments

Comments
 (0)