Skip to content

Commit 9b90ea8

Browse files
committed
src/x509.c: in wolfSSL_X509_get_ext_by_OBJ() and wolfSSL_X509_load_cert_crl_file(), add local protection from null derefs (fixes -Wnull-dereferences);
wolfcrypt/src/chacha.c and wolfssl/wolfcrypt/chacha.h: implement USE_ARM_CHACHA_SPEEDUP gate; wolfcrypt/src/kdf.c: in wc_SSH_KDF(), add early return if _HashInit() fails (fixes _HashFree() of uninited _hash); wolfcrypt/src/sha256.c: initialize sha256->W in ARMASM variant of wc_InitSha256_ex(), and pass sha256->heap to XMALLOC/XFREE consistently.
1 parent 097cd57 commit 9b90ea8

5 files changed

Lines changed: 39 additions & 22 deletions

File tree

src/x509.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,14 @@ int wolfSSL_X509_get_ext_by_OBJ(const WOLFSSL_X509 *x,
463463
lastpos++;
464464
if (lastpos < 0)
465465
lastpos = 0;
466-
for (; lastpos < wolfSSL_sk_num(sk); lastpos++)
467-
if (wolfSSL_OBJ_cmp(wolfSSL_sk_X509_EXTENSION_value(sk,
468-
lastpos)->obj, obj) == 0)
466+
for (; lastpos < wolfSSL_sk_num(sk); lastpos++) {
467+
const WOLFSSL_X509_EXTENSION *ext =
468+
wolfSSL_sk_X509_EXTENSION_value(sk, lastpos);
469+
if (ext == NULL)
470+
continue;
471+
if (wolfSSL_OBJ_cmp(ext->obj, obj) == 0)
469472
return lastpos;
473+
}
470474
return WOLFSSL_FATAL_ERROR;
471475
}
472476

@@ -8343,6 +8347,9 @@ int wolfSSL_X509_load_cert_crl_file(WOLFSSL_X509_LOOKUP *ctx,
83438347
for (i=0; i < num; i++) {
83448348
info_tmp = wolfSSL_sk_X509_INFO_value(info, i);
83458349

8350+
if (info_tmp == NULL)
8351+
continue;
8352+
83468353
if (info_tmp->x509) {
83478354
if (wolfSSL_X509_STORE_add_cert(ctx->store, info_tmp->x509) ==
83488355
WOLFSSL_SUCCESS) {

wolfcrypt/src/chacha.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Public domain.
115115
*/
116116
int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
117117
{
118-
#if !defined(WOLFSSL_ARMASM)
118+
#if !defined(USE_ARM_CHACHA_SPEEDUP)
119119
word32 temp[CHACHA_IV_WORDS];/* used for alignment of memory */
120120
#endif
121121

@@ -124,7 +124,7 @@ int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
124124

125125
ctx->left = 0; /* resets state */
126126

127-
#if !defined(WOLFSSL_ARMASM)
127+
#if !defined(USE_ARM_CHACHA_SPEEDUP)
128128
XMEMCPY(temp, inIv, CHACHA_IV_BYTES);
129129
/* block counter */
130130
ctx->X[CHACHA_MATRIX_CNT_IV+0] = counter;
@@ -141,7 +141,7 @@ int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
141141
return 0;
142142
}
143143

144-
#if !defined(WOLFSSL_ARMASM)
144+
#if !defined(USE_ARM_CHACHA_SPEEDUP)
145145
/* "expand 32-byte k" as unsigned 32 byte */
146146
static const word32 sigma[4] = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574};
147147
/* "expand 16-byte k" as unsigned 16 byte */
@@ -153,7 +153,7 @@ static const word32 tau[4] = {0x61707865, 0x3120646e, 0x79622d36, 0x6b206574};
153153
*/
154154
int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
155155
{
156-
#if !defined(WOLFSSL_ARMASM)
156+
#if !defined(USE_ARM_CHACHA_SPEEDUP)
157157
const word32* constants;
158158
const byte* k;
159159
#ifdef XSTREAM_ALIGN
@@ -167,7 +167,7 @@ int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
167167
if (keySz != (CHACHA_MAX_KEY_SZ/2) && keySz != CHACHA_MAX_KEY_SZ)
168168
return BAD_FUNC_ARG;
169169

170-
#if !defined(WOLFSSL_ARMASM)
170+
#if !defined(USE_ARM_CHACHA_SPEEDUP)
171171
#ifdef XSTREAM_ALIGN
172172
if ((wc_ptr_t)key % 4) {
173173
WOLFSSL_MSG("wc_ChachaSetKey unaligned key");
@@ -220,7 +220,7 @@ int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
220220
return 0;
221221
}
222222

223-
#if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
223+
#if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(USE_ARM_CHACHA_SPEEDUP)
224224
/**
225225
* Converts word into bytes with rotations having been done.
226226
*/
@@ -267,7 +267,7 @@ extern void chacha_encrypt_avx2(ChaCha* ctx, const byte* m, byte* c,
267267
#endif
268268

269269

270-
#if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
270+
#if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(USE_ARM_CHACHA_SPEEDUP)
271271
/**
272272
* Encrypt a stream of bytes
273273
*/
@@ -365,7 +365,7 @@ int wc_Chacha_Process(ChaCha* ctx, byte* output, const byte* input,
365365
chacha_encrypt_x64(ctx, input, output, msglen);
366366
return 0;
367367
}
368-
#elif defined(WOLFSSL_ARMASM)
368+
#elif defined(USE_ARM_CHACHA_SPEEDUP)
369369
/* Handle left over bytes from last block. */
370370
if ((msglen > 0) && (ctx->left > 0)) {
371371
byte* over = ((byte*)ctx->over) + CHACHA_CHUNK_BYTES - ctx->left;

wolfcrypt/src/kdf.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,10 @@ int wc_SSH_KDF(byte hashId, byte keyId, byte* key, word32 keySz,
797797
remainder = keySz % digestSz;
798798

799799
ret = _HashInit(enmhashId, &hash);
800-
if (ret == 0)
801-
ret = _HashUpdate(enmhashId, &hash, kSzFlat, LENGTH_SZ);
800+
if (ret != 0)
801+
return ret;
802+
803+
ret = _HashUpdate(enmhashId, &hash, kSzFlat, LENGTH_SZ);
802804
if (ret == 0 && kPad)
803805
ret = _HashUpdate(enmhashId, &hash, &pad, 1);
804806
if (ret == 0)

wolfcrypt/src/sha256.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,10 @@ int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
11051105
sha256->heap = heap;
11061106
(void)devId;
11071107

1108+
#ifdef WOLFSSL_SMALL_STACK_CACHE
1109+
sha256->W = NULL;
1110+
#endif
1111+
11081112
return ret;
11091113
}
11101114

@@ -1241,16 +1245,16 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
12411245
#if defined(WOLFSSL_SMALL_STACK_CACHE) && !defined(WOLFSSL_NO_MALLOC)
12421246
word32* W = sha256->W;
12431247
if (W == NULL) {
1244-
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
1245-
DYNAMIC_TYPE_DIGEST);
1248+
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
1249+
sha256->heap, DYNAMIC_TYPE_DIGEST);
12461250
if (W == NULL)
12471251
return MEMORY_E;
12481252
sha256->W = W;
12491253
}
12501254
#elif defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
12511255
word32* W;
1252-
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE, NULL,
1253-
DYNAMIC_TYPE_TMP_BUFFER);
1256+
W = (word32*)XMALLOC(sizeof(word32) * WC_SHA256_BLOCK_SIZE,
1257+
sha256->heap, DYNAMIC_TYPE_TMP_BUFFER);
12541258
if (W == NULL)
12551259
return MEMORY_E;
12561260
#else
@@ -1291,7 +1295,7 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
12911295
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SMALL_STACK_CACHE) &&\
12921296
!defined(WOLFSSL_NO_MALLOC)
12931297
ForceZero(W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
1294-
XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1298+
XFREE(W, sha256->heap, DYNAMIC_TYPE_TMP_BUFFER);
12951299
#endif
12961300
return 0;
12971301
}
@@ -2308,7 +2312,7 @@ static WC_INLINE int Transform_Sha256_Len(wc_Sha256* sha256, const byte* data,
23082312
#ifdef WOLFSSL_SMALL_STACK_CACHE
23092313
if (sha224->W != NULL) {
23102314
ForceZero(sha224->W, sizeof(word32) * WC_SHA224_BLOCK_SIZE);
2311-
XFREE(sha224->W, NULL, DYNAMIC_TYPE_DIGEST);
2315+
XFREE(sha224->W, sha224->heap, DYNAMIC_TYPE_DIGEST);
23122316
sha224->W = NULL;
23132317
}
23142318
#endif
@@ -2391,7 +2395,7 @@ void wc_Sha256Free(wc_Sha256* sha256)
23912395
#ifdef WOLFSSL_SMALL_STACK_CACHE
23922396
if (sha256->W != NULL) {
23932397
ForceZero(sha256->W, sizeof(word32) * WC_SHA256_BLOCK_SIZE);
2394-
XFREE(sha256->W, NULL, DYNAMIC_TYPE_DIGEST);
2398+
XFREE(sha256->W, sha256->heap, DYNAMIC_TYPE_DIGEST);
23952399
sha256->W = NULL;
23962400
}
23972401
#endif

wolfssl/wolfcrypt/chacha.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ Block counter is located at index 12.
6868
#define USE_INTEL_CHACHA_SPEEDUP
6969
#define HAVE_INTEL_AVX1
7070
#endif
71+
#elif defined(WOLFSSL_ARMASM)
72+
#ifndef NO_CHACHA_ASM
73+
#define USE_ARM_CHACHA_SPEEDUP
74+
#endif
7175
#endif
7276

7377
enum {
@@ -82,7 +86,7 @@ typedef struct ChaCha {
8286
byte extra[12];
8387
#endif
8488
word32 left; /* number of bytes leftover */
85-
#if defined(USE_INTEL_CHACHA_SPEEDUP) || defined(WOLFSSL_ARMASM) || \
89+
#if defined(USE_INTEL_CHACHA_SPEEDUP) || defined(USE_ARM_CHACHA_SPEEDUP) || \
8690
defined(WOLFSSL_RISCV_ASM)
8791
word32 over[CHACHA_CHUNK_WORDS];
8892
#endif
@@ -107,7 +111,7 @@ WOLFSSL_API int wc_XChacha_SetKey(ChaCha *ctx, const byte *key, word32 keySz,
107111
word32 counter);
108112
#endif
109113

110-
#if defined(WOLFSSL_ARMASM)
114+
#if defined(USE_ARM_CHACHA_SPEEDUP)
111115

112116
WOLFSSL_LOCAL void wc_chacha_setiv(word32* x, const byte* iv, word32 counter);
113117
WOLFSSL_LOCAL void wc_chacha_setkey(word32* x, const byte* key, word32 keySz);

0 commit comments

Comments
 (0)