Skip to content

Commit f44d120

Browse files
committed
wolfssl/wolfcrypt/{aes.h,curve25519.h,ed25519.h,hash.h,rsa.h}: remove unneeded .isAllocated member from struct definitions, and add int *result_code argument to constructor prototypes;
wolfssl/wolfcrypt/aes.h: add Aes.streamData_sz; src/tls13.c: fix devId passed to wc_HmacInit() in CreateCookieExt() and TlsCheckCookie(); src/keys.c: in SetKeys(), call wc_HmacInit() on hmacs only if newly allocated; wolfcrypt/src/aes.c: * in wc_Gmac(), wc_GmacVerify(), and AesSivCipher(), use wc_AesNew() and wc_AesDelete(); * in wc_AesInit(), zero the object on entry, and remove superseded piecemeal initializations to zero; * in wc_AesFree(), zero aes->streamData, and zero the entire object as final cleanup; wolfcrypt/src/curve25519.c: in wc_curve25519_free(), zero the entire object rather than zeroing piecemeal; wolfcrypt/test/test.c: * add fallback implementations (for old FIPS) of wc_HashNew(), wc_HashDelete(), wc_curve25519_new(), wc_curve25519_delete(), wc_ed25519_new(), and wc_ed25519_delete(); * update constructor calls throughout for new semantics; * refactor ed25519_test() for proper cleanup and error encoding.
1 parent 984d16b commit f44d120

14 files changed

Lines changed: 375 additions & 287 deletions

File tree

src/keys.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3318,9 +3318,7 @@ int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
33183318
DYNAMIC_TYPE_CIPHER);
33193319
if (enc->hmac == NULL)
33203320
return MEMORY_E;
3321-
}
33223321

3323-
if (enc) {
33243322
if (wc_HmacInit(enc->hmac, heap, devId) != 0) {
33253323
WOLFSSL_MSG("HmacInit failed in SetKeys");
33263324
XFREE(enc->hmac, heap, DYNAMIC_TYPE_CIPHER);
@@ -3334,9 +3332,7 @@ int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
33343332
DYNAMIC_TYPE_CIPHER);
33353333
if (dec->hmac == NULL)
33363334
return MEMORY_E;
3337-
}
33383335

3339-
if (dec) {
33403336
if (wc_HmacInit(dec->hmac, heap, devId) != 0) {
33413337
WOLFSSL_MSG("HmacInit failed in SetKeys");
33423338
XFREE(dec->hmac, heap, DYNAMIC_TYPE_CIPHER);

src/tls13.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,7 +2534,6 @@ static int Tls13IntegrityOnly_Encrypt(WOLFSSL* ssl, byte* output,
25342534
/* Copy the input to output if not the same buffer */
25352535
if (ret == 0 && output != input)
25362536
XMEMCPY(output, input, sz);
2537-
25382537
return ret;
25392538
}
25402539
#endif
@@ -2930,7 +2929,6 @@ static int Tls13IntegrityOnly_Decrypt(WOLFSSL* ssl, byte* output,
29302929
/* Copy the input to output if not the same buffer */
29312930
if (ret == 0 && output != input)
29322931
XMEMCPY(output, input, sz);
2933-
29342932
return ret;
29352933
}
29362934
#endif
@@ -3612,7 +3610,7 @@ int CreateCookieExt(const WOLFSSL* ssl, byte* hash, word16 hashSz,
36123610
macSz = WC_SHA256_DIGEST_SIZE;
36133611
#endif /* NO_SHA256 */
36143612

3615-
ret = wc_HmacInit(&cookieHmac, ssl->heap, INVALID_DEVID);
3613+
ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId);
36163614
if (ret == 0) {
36173615
ret = wc_HmacSetKey(&cookieHmac, cookieType,
36183616
ssl->buffers.tls13CookieSecret.buffer,
@@ -6394,7 +6392,7 @@ int TlsCheckCookie(const WOLFSSL* ssl, const byte* cookie, word16 cookieSz)
63946392
return HRR_COOKIE_ERROR;
63956393
cookieSz -= macSz;
63966394

6397-
ret = wc_HmacInit(&cookieHmac, ssl->heap, INVALID_DEVID);
6395+
ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId);
63986396
if (ret == 0) {
63996397
ret = wc_HmacSetKey(&cookieHmac, cookieType,
64006398
ssl->buffers.tls13CookieSecret.buffer,

wolfcrypt/src/aes.c

Lines changed: 53 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -10026,7 +10026,8 @@ int wc_AesGcmInit(Aes* aes, const byte* key, word32 len, const byte* iv,
1002610026
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_AESNI)
1002710027
if ((ret == 0) && (aes->streamData == NULL)) {
1002810028
/* Allocate buffers for streaming. */
10029-
aes->streamData = (byte*)XMALLOC(5 * AES_BLOCK_SIZE, aes->heap,
10029+
aes->streamData_sz = 5 * AES_BLOCK_SIZE;
10030+
aes->streamData = (byte*)XMALLOC(aes->streamData_sz, aes->heap,
1003010031
DYNAMIC_TYPE_AES);
1003110032
if (aes->streamData == NULL) {
1003210033
ret = MEMORY_E;
@@ -10513,7 +10514,7 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
1051310514
byte* authTag, word32 authTagSz, WC_RNG* rng)
1051410515
{
1051510516
#ifdef WOLFSSL_SMALL_STACK
10516-
Aes *aes = NULL;
10517+
Aes *aes;
1051710518
#else
1051810519
Aes aes[1];
1051910520
#endif
@@ -10526,25 +10527,24 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
1052610527
}
1052710528

1052810529
#ifdef WOLFSSL_SMALL_STACK
10529-
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
10530-
DYNAMIC_TYPE_AES)) == NULL)
10531-
return MEMORY_E;
10530+
aes = wc_AesNew(NULL, INVALID_DEVID, &ret);
10531+
#else
10532+
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
1053210533
#endif
10534+
if (ret != 0)
10535+
return ret;
1053310536

10534-
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
10535-
if (ret == 0) {
10536-
ret = wc_AesGcmSetKey(aes, key, keySz);
10537-
if (ret == 0)
10538-
ret = wc_AesGcmSetIV(aes, ivSz, NULL, 0, rng);
10539-
if (ret == 0)
10540-
ret = wc_AesGcmEncrypt_ex(aes, NULL, NULL, 0, iv, ivSz,
10537+
ret = wc_AesGcmSetKey(aes, key, keySz);
10538+
if (ret == 0)
10539+
ret = wc_AesGcmSetIV(aes, ivSz, NULL, 0, rng);
10540+
if (ret == 0)
10541+
ret = wc_AesGcmEncrypt_ex(aes, NULL, NULL, 0, iv, ivSz,
1054110542
authTag, authTagSz, authIn, authInSz);
10542-
aes->isAllocated = 0;
10543-
wc_AesFree(aes);
10544-
}
10545-
ForceZero(aes, sizeof *aes);
10543+
1054610544
#ifdef WOLFSSL_SMALL_STACK
10547-
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
10545+
wc_AesDelete(&aes);
10546+
#else
10547+
wc_AesFree(aes);
1054810548
#endif
1054910549

1055010550
return ret;
@@ -10570,24 +10570,21 @@ int wc_GmacVerify(const byte* key, word32 keySz,
1057010570
}
1057110571

1057210572
#ifdef WOLFSSL_SMALL_STACK
10573-
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
10574-
DYNAMIC_TYPE_AES)) == NULL)
10575-
return MEMORY_E;
10576-
#endif
10577-
10573+
aes = wc_AesNew(NULL, INVALID_DEVID, &ret);
10574+
#else
1057810575
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
10576+
#endif
1057910577
if (ret == 0) {
1058010578
ret = wc_AesGcmSetKey(aes, key, keySz);
1058110579
if (ret == 0)
1058210580
ret = wc_AesGcmDecrypt(aes, NULL, NULL, 0, iv, ivSz,
1058310581
authTag, authTagSz, authIn, authInSz);
1058410582

10585-
aes->isAllocated = 0;
10586-
wc_AesFree(aes);
1058710583
}
10588-
ForceZero(aes, sizeof *aes);
1058910584
#ifdef WOLFSSL_SMALL_STACK
10590-
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
10585+
wc_AesDelete(&aes);
10586+
#else
10587+
wc_AesFree(aes);
1059110588
#endif
1059210589
#else
1059310590
(void)key;
@@ -11300,18 +11297,24 @@ int wc_AesCcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
1130011297
#endif /* HAVE_AESCCM */
1130111298

1130211299
#ifndef WC_NO_CONSTRUCTORS
11303-
Aes* wc_AesNew(void* heap, int devId)
11300+
Aes* wc_AesNew(void* heap, int devId, int *result_code)
1130411301
{
11302+
int ret;
1130511303
Aes* aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_AES);
11306-
if (aes != NULL) {
11307-
if (wc_AesInit(aes, heap, devId) != 0) {
11304+
if (aes == NULL) {
11305+
ret = MEMORY_E;
11306+
}
11307+
else {
11308+
ret = wc_AesInit(aes, heap, devId);
11309+
if (ret != 0) {
1130811310
XFREE(aes, heap, DYNAMIC_TYPE_AES);
1130911311
aes = NULL;
1131011312
}
11311-
else {
11312-
aes->isAllocated = 1;
11313-
}
1131411313
}
11314+
11315+
if (result_code != NULL)
11316+
*result_code = ret;
11317+
1131511318
return aes;
1131611319
}
1131711320

@@ -11326,26 +11329,20 @@ int wc_AesDelete(Aes** aes)
1132611329
}
1132711330
#endif /* !WC_NO_CONSTRUCTORS */
1132811331

11329-
/* Initialize Aes for use with async hardware */
11332+
/* Initialize Aes */
1133011333
int wc_AesInit(Aes* aes, void* heap, int devId)
1133111334
{
1133211335
int ret = 0;
1133311336

1133411337
if (aes == NULL)
1133511338
return BAD_FUNC_ARG;
1133611339

11337-
aes->isAllocated = 0;
11338-
aes->heap = heap;
11339-
aes->rounds = 0;
11340+
XMEMSET(aes, 0, sizeof(*aes));
1134011341

11341-
#ifdef WOLFSSL_AESNI
11342-
/* clear here for the benefit of wc_AesGcmInit(). */
11343-
aes->use_aesni = 0;
11344-
#endif
11342+
aes->heap = heap;
1134511343

1134611344
#ifdef WOLF_CRYPTO_CB
1134711345
aes->devId = devId;
11348-
aes->devCtx = NULL;
1134911346
#else
1135011347
(void)devId;
1135111348
#endif
@@ -11358,51 +11355,18 @@ int wc_AesInit(Aes* aes, void* heap, int devId)
1135811355
aes->alFd = WC_SOCK_NOTSET;
1135911356
aes->rdFd = WC_SOCK_NOTSET;
1136011357
#endif
11361-
#ifdef WOLFSSL_KCAPI_AES
11362-
aes->handle = NULL;
11363-
aes->init = 0;
11364-
#endif
1136511358
#if defined(WOLFSSL_DEVCRYPTO) && \
1136611359
(defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
1136711360
aes->ctx.cfd = -1;
1136811361
#endif
11369-
#if defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
11370-
XMEMSET(&aes->ctx, 0, sizeof(aes->ctx));
11371-
#endif
1137211362
#if defined(WOLFSSL_IMXRT_DCP)
1137311363
DCPAesInit(aes);
1137411364
#endif
1137511365

11376-
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
11377-
XMEMSET(&aes->maxq_ctx, 0, sizeof(aes->maxq_ctx));
11378-
#endif
11379-
11380-
#ifdef HAVE_AESGCM
11381-
#ifdef OPENSSL_EXTRA
11382-
XMEMSET(aes->gcm.aadH, 0, sizeof(aes->gcm.aadH));
11383-
aes->gcm.aadLen = 0;
11384-
#endif
11385-
#endif
11386-
11387-
#ifdef WOLFSSL_AESGCM_STREAM
11388-
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_AESNI)
11389-
aes->streamData = NULL;
11390-
#endif
11391-
aes->keylen = 0;
11392-
aes->nonceSz = 0;
11393-
aes->gcmKeySet = 0;
11394-
aes->nonceSet = 0;
11395-
aes->ctrSet = 0;
11396-
#endif
11397-
1139811366
#if defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_AES)
1139911367
ret = wc_psa_aes_init(aes);
1140011368
#endif
1140111369

11402-
#if defined(WOLFSSL_RENESAS_FSPSM)
11403-
XMEMSET(&aes->ctx, 0, sizeof(aes->ctx));
11404-
#endif
11405-
1140611370
#ifdef WC_DEBUG_CIPHER_LIFECYCLE
1140711371
if (ret == 0)
1140811372
ret = wc_debug_CipherLifecycleInit(&aes->CipherLifecycleTag, aes->heap);
@@ -11457,7 +11421,7 @@ int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId)
1145711421
}
1145811422
#endif
1145911423

11460-
/* Free Aes from use with async hardware */
11424+
/* Free Aes resources */
1146111425
void wc_AesFree(Aes* aes)
1146211426
{
1146311427
if (aes == NULL) {
@@ -11503,8 +11467,11 @@ void wc_AesFree(Aes* aes)
1150311467
#endif
1150411468
#if defined(WOLFSSL_AESGCM_STREAM) && defined(WOLFSSL_SMALL_STACK) && \
1150511469
!defined(WOLFSSL_AESNI)
11506-
XFREE(aes->streamData, aes->heap, DYNAMIC_TYPE_AES);
11507-
aes->streamData = NULL;
11470+
if (aes->streamData != NULL) {
11471+
ForceZero(aes->streamData, aes->streamData_sz);
11472+
XFREE(aes->streamData, aes->heap, DYNAMIC_TYPE_AES);
11473+
aes->streamData = NULL;
11474+
}
1150811475
#endif
1150911476

1151011477
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
@@ -11527,6 +11494,8 @@ void wc_AesFree(Aes* aes)
1152711494
wc_fspsm_Aesfree(aes);
1152811495
#endif
1152911496

11497+
ForceZero(aes, sizeof(Aes));
11498+
1153011499
#ifdef WOLFSSL_CHECK_MEM_ZERO
1153111500
wc_MemZero_Check(aes, sizeof(Aes));
1153211501
#endif
@@ -14018,29 +13987,17 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1401813987
}
1401913988
}
1402013989

14021-
#ifdef WOLFSSL_SMALL_STACK
14022-
if (ret == 0) {
14023-
aes = (Aes*)XMALLOC(sizeof(Aes), NULL, DYNAMIC_TYPE_AES);
14024-
if (aes == NULL) {
14025-
ret = MEMORY_E;
14026-
}
14027-
}
14028-
#endif
14029-
1403013990
if (ret == 0) {
13991+
#ifdef WOLFSSL_SMALL_STACK
13992+
aes = wc_AesNew(NULL, INVALID_DEVID, &ret);
13993+
#else
1403113994
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
13995+
#endif
1403213996
if (ret != 0) {
1403313997
WOLFSSL_MSG("Failed to initialized AES object.");
1403413998
}
1403513999
}
1403614000

14037-
#ifndef WOLFSSL_SMALL_STACK
14038-
/* make aes has heap hint and isAllocated initialized for cleanup below */
14039-
if (ret != 0) {
14040-
XMEMSET(aes, 0, sizeof(Aes));
14041-
}
14042-
#endif
14043-
1404414001
if (ret == 0 && dataSz > 0) {
1404514002
sivTmp[12] &= 0x7f;
1404614003
sivTmp[8] &= 0x7f;
@@ -14071,14 +14028,10 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1407114028
}
1407214029

1407314030
#ifdef WOLFSSL_SMALL_STACK
14074-
if (aes != NULL)
14031+
wc_AesDelete(&aes);
14032+
#else
14033+
wc_AesFree(aes);
1407514034
#endif
14076-
{
14077-
wc_AesFree(aes);
14078-
#ifdef WOLFSSL_SMALL_STACK
14079-
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
14080-
#endif
14081-
}
1408214035

1408314036
return ret;
1408414037
}

wolfcrypt/src/curve25519.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -656,19 +656,25 @@ int wc_curve25519_import_private_ex(const byte* priv, word32 privSz,
656656
#endif /* HAVE_CURVE25519_KEY_IMPORT */
657657

658658
#ifndef WC_NO_CONSTRUCTORS
659-
curve25519_key* wc_curve25519_new(void* heap, int devId)
659+
curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code)
660660
{
661+
int ret;
661662
curve25519_key* key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), heap,
662663
DYNAMIC_TYPE_CURVE25519);
663-
if (key != NULL) {
664-
if (wc_curve25519_init_ex(key, heap, devId) != 0) {
664+
if (key == NULL) {
665+
ret = MEMORY_E;
666+
}
667+
else {
668+
ret = wc_curve25519_init_ex(key, heap, devId);
669+
if (ret != 0) {
665670
XFREE(key, heap, DYNAMIC_TYPE_CURVE25519);
666671
key = NULL;
667672
}
668-
else {
669-
key->isAllocated = 1;
670-
}
671673
}
674+
675+
if (result_code != NULL)
676+
*result_code = ret;
677+
672678
return key;
673679
}
674680

@@ -725,11 +731,7 @@ void wc_curve25519_free(curve25519_key* key)
725731
se050_curve25519_free_key(key);
726732
#endif
727733

728-
key->dp = NULL;
729-
ForceZero(key->k, sizeof(key->k));
730-
XMEMSET(&key->p, 0, sizeof(key->p));
731-
key->pubSet = 0;
732-
key->privSet = 0;
734+
ForceZero(key, sizeof(*key));
733735

734736
#ifdef WOLFSSL_CHECK_MEM_ZERO
735737
wc_MemZero_Check(key, sizeof(curve25519_key));

0 commit comments

Comments
 (0)