Skip to content

Commit 06d9993

Browse files
1 parent 0cecccd commit 06d9993

13 files changed

Lines changed: 89 additions & 57 deletions

File tree

tests/api/test_aes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3727,7 +3727,8 @@ int test_wc_AesGcmNonStdNonce(void)
37273727
* and cannot exercise the GHASH-based counter derivation. */
37283728
#if !defined(NO_AES) && defined(HAVE_AESGCM) && \
37293729
!defined(HAVE_FIPS) && \
3730-
!defined(WOLFSSL_AFALG) && !defined(WOLFSSL_KCAPI)
3730+
!defined(WOLFSSL_AFALG) && !defined(WOLFSSL_KCAPI) && \
3731+
!defined(WOLFSSL_DEVCRYPTO_AES)
37313732

37323733
/* ------------------------------------------------------------------
37333734
* Section 1: 1-byte IV, AES-128

wolfcrypt/src/des3.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,10 +1571,14 @@
15711571

15721572
/* rotate left and right halves independently */
15731573
for (j = 0; j < 48; j++) { /* select bits individually */
1574-
if (pcr[pc2[j] - 1]) { /* check bit that goes to ks[j] */
1575-
l= j % 6; /* mask it in if it's there */
1576-
ks[j/6] |= (byte)(bytebit[l] >> 2);
1577-
}
1574+
byte bit;
1575+
byte mask;
1576+
bit =
1577+
(byte)(pcr[pc2[j] - 1]); /* all pcr values are either 0 or 1 */
1578+
mask = (byte)(0 - bit); /* mask is either 0xFF or 0x00 */
1579+
ks[j/6] |=
1580+
(byte)((bytebit[j % 6] >> 2) & mask); /* only set to bytebit value
1581+
if bit == 1*/
15781582
}
15791583

15801584
/* Now convert to odd/even interleaved form for use in F */

wolfcrypt/src/dsa.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,9 @@ int wc_DsaVerify_ex(const byte* digest, word32 digestSz, const byte* sig,
11211121
if (digest == NULL || sig == NULL || key == NULL || answer == NULL)
11221122
return BAD_FUNC_ARG;
11231123

1124+
/* assign default value so we return 0 on error */
1125+
*answer = 0;
1126+
11241127
/* Note the min allowed digestSz here is WC_SHA_DIGEST_SIZE, not
11251128
* WC_MIN_DIGEST_SIZE, to allow verify-only legacy DSA operations, as
11261129
* expressly allowed under FIPS 186-5, FIPS 140-3, and SP 800-131A.

wolfcrypt/src/ecc.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,6 @@ ECC Curve Sizes:
254254
#include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
255255
#endif
256256

257-
#if defined(WOLFSSL_CAAM)
258-
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
259-
#endif
260-
261257
#if defined(WOLFSSL_KCAPI_ECC)
262258
#include <wolfssl/wolfcrypt/port/kcapi/kcapi_ecc.h>
263259
#endif
@@ -10043,7 +10039,7 @@ static int _ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
1004310039
/* store byte point type */
1004410040
out[0] = ECC_POINT_UNCOMP;
1004510041

10046-
if (caamReadPartition((CAAM_ADDRESS)key->securePubKey, out+1, keySz*2) != 0)
10042+
if (caamReadPartition(key->securePubKey, out+1, keySz*2) != 0)
1004710043
return WC_HW_E;
1004810044

1004910045
*outLen = 1 + 2*keySz;
@@ -11636,15 +11632,15 @@ static int _ecc_import_private_key_ex(const byte* priv, word32 privSz,
1163611632
}
1163711633

1163811634
key->partNum = part;
11639-
key->blackKey = (word32)vaddr;
11635+
key->blackKey = vaddr;
1164011636
if (caamWriteToPartition(vaddr, priv, privSz) != 0)
1164111637
return WC_HW_E;
1164211638

1164311639
if (pub != NULL) {
1164411640
/* +1 to account for x963 compressed bit */
1164511641
if (caamWriteToPartition(vaddr + privSz, pub + 1, pubSz - 1) != 0)
1164611642
return WC_HW_E;
11647-
key->securePubKey = (word32)vaddr + privSz;
11643+
key->securePubKey = vaddr + privSz;
1164811644
}
1164911645
}
1165011646
else {

wolfcrypt/src/port/Espressif/esp_crt_bundle/esp_crt_bundle.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -983,14 +983,12 @@ static CB_INLINE int wolfssl_ssl_conf_verify_cb_no_signer(int preverify,
983983
/* Clean up and exit */
984984
if ((_crt_found == 0) && (bundle_cert != NULL)) {
985985
ESP_LOGW(TAG, "Cert not found, free bundle_cert");
986+
/* this_subject and this_issuer are apart of bundle_cert and will be
987+
* freed here*/
986988
wolfSSL_X509_free(bundle_cert);
987989
bundle_cert = NULL;
988-
/* this_subject and this_issuer are pointers into cert used.
989-
* Don't free if the cert was found. */
990-
wolfSSL_X509_NAME_free(this_subject);
991-
this_subject = NULL;
992-
wolfSSL_X509_NAME_free(this_issuer);
993990
this_issuer = NULL;
991+
this_subject = NULL;
994992
}
995993

996994
/* We don't clean up the store_cert and x509 as we are in a callback,

wolfcrypt/src/port/Renesas/renesas_fspsm_aes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ int wc_fspsm_AesGcmEncrypt(struct Aes* aes, byte* out,
415415
XFREE(plainBuf, aes->heap, DYNAMIC_TYPE_AES);
416416
XFREE(cipherBuf, aes->heap, DYNAMIC_TYPE_AES);
417417
XFREE(aTagBuf, aes->heap, DYNAMIC_TYPE_AES);
418+
wc_fspsm_hw_unlock();
418419
return MEMORY_E;
419420
}
420421

wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ static int FSPSM_HashFinal(wolfssl_FSPSM_Hash* hash, byte* out, word32 outSz)
418418
#endif
419419
wc_fspsm_hw_lock();
420420

421-
if (Init(&handle) == FSP_SUCCESS) {
421+
if ((ret = Init(&handle)) == FSP_SUCCESS) {
422422
ret = Update(&handle, (uint8_t*)hash->msg, hash->used);
423423
if (ret == FSP_SUCCESS) {
424424
ret = Final(&handle, out, (uint32_t*)&sz);

wolfcrypt/src/port/devcrypto/devcrypto_aes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
110110
const word32 max_key_len = (AES_MAX_KEY_SIZE / 8);
111111
#endif
112112

113-
if (aes == NULL ||
113+
if (aes == NULL || userKey == NULL ||
114114
!((keylen == 16) || (keylen == 24) || (keylen == 32))) {
115115
return BAD_FUNC_ARG;
116116
}

wolfcrypt/src/port/devcrypto/devcrypto_hash.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,14 @@ int wc_Sha256Update(wc_Sha256* sha, const byte* in, word32 sz)
135135
#ifdef WOLFSSL_DEVCRYPTO_HASH_KEEP
136136
/* keep full message to hash at end instead of incremental updates */
137137
if (sha->len < sha->used + sz) {
138-
if (sha->msg == NULL) {
139-
sha->msg = (byte*)XMALLOC(sha->used + sz, sha->heap,
140-
DYNAMIC_TYPE_TMP_BUFFER);
141-
} else {
142-
byte* pt = (byte*)XREALLOC(sha->msg, sha->used + sz, sha->heap,
143-
DYNAMIC_TYPE_TMP_BUFFER);
144-
if (pt == NULL) {
145-
return MEMORY_E;
146-
}
147-
sha->msg = pt;
148-
}
149-
if (sha->msg == NULL) {
138+
byte* pt = (byte*)XREALLOC(sha->msg, sha->used + sz, sha->heap,
139+
DYNAMIC_TYPE_TMP_BUFFER);
140+
if (pt == NULL) {
150141
return MEMORY_E;
151142
}
143+
144+
sha->msg = pt;
145+
152146
sha->len = sha->used + sz;
153147
}
154148
XMEMCPY(sha->msg + sha->used, in, sz);
@@ -180,7 +174,8 @@ int wc_Sha256Final(wc_Sha256* sha, byte* hash)
180174
#endif
181175
ret = GetDigest(sha, CRYPTO_SHA2_256, hash);
182176
if (ret != 0) {
183-
return ret;
177+
wc_Sha256Free(sha);
178+
return ret;
184179
}
185180

186181
wc_Sha256Free(sha);
@@ -190,6 +185,7 @@ int wc_Sha256Final(wc_Sha256* sha, byte* hash)
190185

191186
int wc_Sha256GetHash(wc_Sha256* sha, byte* hash)
192187
{
188+
193189
if (sha == NULL || hash == NULL) {
194190
return BAD_FUNC_ARG;
195191
}
@@ -198,9 +194,15 @@ int wc_Sha256GetHash(wc_Sha256* sha, byte* hash)
198194
{
199195
int ret;
200196
wc_Sha256 cpy;
201-
wc_Sha256Copy(sha, &cpy);
202-
203-
if ((ret = HashUpdate(&cpy, CRYPTO_SHA2_256, cpy.msg, cpy.used)) == 0) {
197+
XMEMSET(&cpy, 0, sizeof(cpy)); /* ZII */
198+
/* mark as having no /dev/crypto session yet so the wc_Sha256Free()
199+
* in wc_Sha256Copy() does not close fd 0 (cfd == -1 is the
200+
* "no session" sentinel, matching wc_AesInit()) */
201+
cpy.ctx.cfd = -1;
202+
ret = wc_Sha256Copy(sha, &cpy);
203+
204+
if (ret == 0 &&
205+
(ret = HashUpdate(&cpy, CRYPTO_SHA2_256, cpy.msg, cpy.used)) == 0) {
204206
/* help static analysis tools out */
205207
XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
206208
ret = GetDigest(&cpy, CRYPTO_SHA2_256, hash);
@@ -219,22 +221,30 @@ int wc_Sha256GetHash(wc_Sha256* sha, byte* hash)
219221

220222
int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
221223
{
224+
222225
if (src == NULL || dst == NULL) {
223226
return BAD_FUNC_ARG;
224227
}
225228

226-
wc_InitSha256_ex(dst, src->heap, 0);
227229
#ifdef WOLFSSL_DEVCRYPTO_HASH_KEEP
230+
wc_Sha256Free(dst);
231+
wc_InitSha256_ex(dst, src->heap, 0);
228232
dst->len = src->len;
229233
dst->used = src->used;
230234
dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
231235
if (dst->msg == NULL) {
232236
return MEMORY_E;
233237
}
234238
XMEMCPY(dst->msg, src->msg, src->len);
235-
#endif
236239

237240
return 0;
241+
#else
242+
(void)src;
243+
(void)dst;
244+
245+
WOLFSSL_MSG("Compile with WOLFSSL_DEVCRYPTO_HASH_KEEP for this feature");
246+
return NOT_COMPILED_IN;
247+
#endif
238248
}
239249

240250
#endif /* !NO_SHA256 */

wolfcrypt/src/random.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,29 @@ static int sha512DrbgDisabled = 0;
380380
static wolfSSL_Mutex drbgStateMutex
381381
WOLFSSL_MUTEX_INITIALIZER_CLAUSE(drbgStateMutex);
382382
#ifndef WOLFSSL_MUTEX_INITIALIZER
383+
#ifdef WOLFSSL_ATOMIC_OPS
384+
static wolfSSL_Atomic_Int drbgStateMutex_inited = WOLFSSL_ATOMIC_INITIALIZER(0);
385+
#else
383386
static int drbgStateMutex_inited = 0;
384387
#endif
388+
#endif
385389
#endif /* !SINGLE_THREADED */
386390

387391
int wc_DrbgState_MutexInit(void)
388392
{
389393
#ifndef SINGLE_THREADED
390394
#ifndef WOLFSSL_MUTEX_INITIALIZER
391-
if (!drbgStateMutex_inited) {
395+
int expected = 0;
396+
/* Check if mutex is not inited and set it to true before init.
397+
* This means that the mutex is marked as init before it actually is.
398+
* Necessary to ensure that two threads don't init at the same time.*/
399+
if (wolfSSL_Atomic_Int_CompareExchange(&drbgStateMutex_inited,
400+
&expected, 1)) {
392401
int ret = wc_InitMutex(&drbgStateMutex);
393-
if (ret != 0)
402+
if (ret != 0) {
403+
(void)wolfSSL_Atomic_Int_Exchange(&drbgStateMutex_inited, 0);
394404
return ret;
395-
drbgStateMutex_inited = 1;
405+
}
396406
}
397407
#endif
398408
#endif
@@ -3716,9 +3726,11 @@ static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz)
37163726

37173727
for (; (sz / sizeof(word64)) > 0; sz -= sizeof(word64),
37183728
output += sizeof(word64)) {
3719-
ret = IntelRDseed64_r((word64*)output);
3729+
word64 rndTmpLocal;
3730+
ret = IntelRDseed64_r(&rndTmpLocal);
37203731
if (ret != 0)
37213732
return ret;
3733+
writeUnalignedWord64(output, rndTmpLocal);
37223734
}
37233735
if (sz == 0)
37243736
return 0;

0 commit comments

Comments
 (0)