Skip to content

Commit 984d16b

Browse files
committed
refactor wolfcrypt constructors:
add delete APIs, matching recently added wc_AesNew, wc_curve25519_new, wc_ed25519_new, wc_HashNew, and wc_NewRsaKey: * wc_AesDelete() * wc_HashDelete() * wc_DeleteRsaKey() * wc_curve25519_delete() * wc_ed25519_delete() * remove handling in corresponding preexisting free APIs for recently added .isAllocated member -- this restores preexisting semantics; * add WC_NO_CONSTRUCTORS gate, and auto-activate it when NO_WOLFSSL_MEMORY && WOLFSSL_NO_MALLOC (unless preempted by XMALLOC_USER or XMALLOC_OVERRIDE); * exclude recently added .isAllocated members from wolfcrypt structs when defined(WC_NO_CONSTRUCTORS); * adjust wolfcrypt/test/test.c for consistency with the above, and fix cleanup codes/dynamics in several tests.
1 parent 61b726f commit 984d16b

12 files changed

Lines changed: 284 additions & 232 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11299,6 +11299,7 @@ int wc_AesCcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
1129911299

1130011300
#endif /* HAVE_AESCCM */
1130111301

11302+
#ifndef WC_NO_CONSTRUCTORS
1130211303
Aes* wc_AesNew(void* heap, int devId)
1130311304
{
1130411305
Aes* aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_AES);
@@ -11314,6 +11315,17 @@ Aes* wc_AesNew(void* heap, int devId)
1131411315
return aes;
1131511316
}
1131611317

11318+
int wc_AesDelete(Aes** aes)
11319+
{
11320+
if ((aes == NULL) || (*aes == NULL))
11321+
return BAD_FUNC_ARG;
11322+
wc_AesFree(*aes);
11323+
XFREE(*aes, (*aes)->heap, DYNAMIC_TYPE_AES);
11324+
*aes = NULL;
11325+
return 0;
11326+
}
11327+
#endif /* !WC_NO_CONSTRUCTORS */
11328+
1131711329
/* Initialize Aes for use with async hardware */
1131811330
int wc_AesInit(Aes* aes, void* heap, int devId)
1131911331
{
@@ -11448,18 +11460,12 @@ int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId)
1144811460
/* Free Aes from use with async hardware */
1144911461
void wc_AesFree(Aes* aes)
1145011462
{
11451-
void* heap;
11452-
byte isAllocated;
11453-
1145411463
if (aes == NULL) {
1145511464
return;
1145611465
}
1145711466

11458-
heap = aes->heap;
11459-
isAllocated = aes->isAllocated;
11460-
1146111467
#ifdef WC_DEBUG_CIPHER_LIFECYCLE
11462-
(void)wc_debug_CipherLifecycleFree(&aes->CipherLifecycleTag, heap, 1);
11468+
(void)wc_debug_CipherLifecycleFree(&aes->CipherLifecycleTag, aes->heap, 1);
1146311469
#endif
1146411470

1146511471
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
@@ -11497,7 +11503,7 @@ void wc_AesFree(Aes* aes)
1149711503
#endif
1149811504
#if defined(WOLFSSL_AESGCM_STREAM) && defined(WOLFSSL_SMALL_STACK) && \
1149911505
!defined(WOLFSSL_AESNI)
11500-
XFREE(aes->streamData, heap, DYNAMIC_TYPE_AES);
11506+
XFREE(aes->streamData, aes->heap, DYNAMIC_TYPE_AES);
1150111507
aes->streamData = NULL;
1150211508
#endif
1150311509

@@ -11524,11 +11530,6 @@ void wc_AesFree(Aes* aes)
1152411530
#ifdef WOLFSSL_CHECK_MEM_ZERO
1152511531
wc_MemZero_Check(aes, sizeof(Aes));
1152611532
#endif
11527-
11528-
if (isAllocated) {
11529-
XFREE(aes, heap, DYNAMIC_TYPE_AES);
11530-
}
11531-
1153211533
}
1153311534

1153411535
int wc_AesGetKeySize(Aes* aes, word32* keySize)

wolfcrypt/src/curve25519.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ int wc_curve25519_import_private_ex(const byte* priv, word32 privSz,
655655

656656
#endif /* HAVE_CURVE25519_KEY_IMPORT */
657657

658+
#ifndef WC_NO_CONSTRUCTORS
658659
curve25519_key* wc_curve25519_new(void* heap, int devId)
659660
{
660661
curve25519_key* key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), heap,
@@ -671,6 +672,16 @@ curve25519_key* wc_curve25519_new(void* heap, int devId)
671672
return key;
672673
}
673674

675+
int wc_curve25519_delete(curve25519_key** key) {
676+
if ((key == NULL) || (*key == NULL))
677+
return BAD_FUNC_ARG;
678+
wc_curve25519_free(*key);
679+
XFREE(*key, (*key)->heap, DYNAMIC_TYPE_CURVE25519);
680+
*key = NULL;
681+
return 0;
682+
}
683+
#endif /* !WC_NO_CONSTRUCTORS */
684+
674685
int wc_curve25519_init_ex(curve25519_key* key, void* heap, int devId)
675686
{
676687
if (key == NULL)
@@ -707,15 +718,9 @@ int wc_curve25519_init(curve25519_key* key)
707718
/* Clean the memory of a key */
708719
void wc_curve25519_free(curve25519_key* key)
709720
{
710-
void* heap;
711-
byte isAllocated = 0;
712-
713721
if (key == NULL)
714722
return;
715723

716-
heap = key->heap;
717-
isAllocated = key->isAllocated;
718-
719724
#ifdef WOLFSSL_SE050
720725
se050_curve25519_free_key(key);
721726
#endif
@@ -729,11 +734,6 @@ void wc_curve25519_free(curve25519_key* key)
729734
#ifdef WOLFSSL_CHECK_MEM_ZERO
730735
wc_MemZero_Check(key, sizeof(curve25519_key));
731736
#endif
732-
733-
if (isAllocated) {
734-
XFREE(key, heap, DYNAMIC_TYPE_CURVE25519);
735-
(void)heap;
736-
}
737737
}
738738

739739
/* get key size */

wolfcrypt/src/ed25519.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ int wc_ed25519ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
968968
}
969969
#endif /* HAVE_ED25519_VERIFY */
970970

971-
#ifndef WOLFSSL_NO_MALLOC
971+
#ifndef WC_NO_CONSTRUCTORS
972972
ed25519_key* wc_ed25519_new(void* heap, int devId)
973973
{
974974
ed25519_key* key = (ed25519_key*)XMALLOC(sizeof(ed25519_key), heap,
@@ -984,7 +984,16 @@ ed25519_key* wc_ed25519_new(void* heap, int devId)
984984
}
985985
return key;
986986
}
987-
#endif
987+
988+
int wc_ed25519_delete(ed25519_key** key) {
989+
if ((key == NULL) || (*key == NULL))
990+
return BAD_FUNC_ARG;
991+
wc_ed25519_free(*key);
992+
XFREE(*key, (*key)->heap, DYNAMIC_TYPE_ED25519);
993+
*key = NULL;
994+
return 0;
995+
}
996+
#endif /* !WC_NO_CONSTRUCTORS */
988997

989998
/* initialize information and memory for key */
990999
int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId)
@@ -1025,15 +1034,9 @@ int wc_ed25519_init(ed25519_key* key)
10251034
/* clear memory of key */
10261035
void wc_ed25519_free(ed25519_key* key)
10271036
{
1028-
void* heap;
1029-
byte isAllocated = 0;
1030-
10311037
if (key == NULL)
10321038
return;
10331039

1034-
heap = key->heap;
1035-
isAllocated = key->isAllocated;
1036-
10371040
#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
10381041
ed25519_hash_free(key, &key->sha);
10391042
#endif
@@ -1046,12 +1049,6 @@ void wc_ed25519_free(ed25519_key* key)
10461049
#ifdef WOLFSSL_CHECK_MEM_ZERO
10471050
wc_MemZero_Check(key, sizeof(ed25519_key));
10481051
#endif
1049-
1050-
if (isAllocated) {
1051-
XFREE(key, heap, DYNAMIC_TYPE_ED25519);
1052-
(void)heap;
1053-
}
1054-
10551052
}
10561053

10571054

wolfcrypt/src/hash.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ int wc_Hash(enum wc_HashType hash_type, const byte* data,
686686
NULL, INVALID_DEVID);
687687
}
688688

689-
#ifndef WOLFSSL_NO_MALLOC
689+
#ifndef WC_NO_CONSTRUCTORS
690690
wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId)
691691
{
692692
wc_HashAlg* hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), heap,
@@ -702,7 +702,19 @@ wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId)
702702
}
703703
return hash;
704704
}
705-
#endif
705+
706+
int wc_HashDelete(wc_HashAlg **hash) {
707+
int ret;
708+
if ((hash == NULL) || (*hash == NULL))
709+
return BAD_FUNC_ARG;
710+
ret = wc_HashFree(*hash, (*hash)->type);
711+
if (ret < 0)
712+
return ret;
713+
XFREE(*hash, (*hash)->heap, DYNAMIC_TYPE_HASHES);
714+
*hash = NULL;
715+
return 0;
716+
}
717+
#endif /* !WC_NO_CONSTRUCTORS */
706718

707719
int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
708720
int devId)
@@ -712,9 +724,14 @@ int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
712724
if (hash == NULL)
713725
return BAD_FUNC_ARG;
714726

715-
hash->isAllocated = 0;
716727
hash->type = type;
717728

729+
#ifdef WC_NO_CONSTRUCTORS
730+
(void)heap;
731+
#else
732+
hash->heap = heap;
733+
#endif
734+
718735
switch (type) {
719736
case WC_HASH_TYPE_MD5:
720737
#ifndef NO_MD5
@@ -808,7 +825,6 @@ int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
808825
ret = BAD_FUNC_ARG;
809826
};
810827

811-
(void)heap;
812828
(void)devId;
813829

814830
return ret;
@@ -1043,8 +1059,6 @@ int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out)
10431059
int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
10441060
{
10451061
int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */
1046-
void* heap = NULL;
1047-
byte isAllocated = 0;
10481062

10491063
if (hash == NULL)
10501064
return BAD_FUNC_ARG;
@@ -1056,47 +1070,39 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
10561070
}
10571071
#endif
10581072

1059-
isAllocated = hash->isAllocated;
1060-
10611073
switch (type) {
10621074
case WC_HASH_TYPE_MD5:
10631075
#ifndef NO_MD5
1064-
heap = hash->alg.md5.heap;
10651076
wc_Md5Free(&hash->alg.md5);
10661077
ret = 0;
10671078
#endif
10681079
break;
10691080
case WC_HASH_TYPE_SHA:
10701081
#ifndef NO_SHA
1071-
heap = hash->alg.sha.heap;
10721082
wc_ShaFree(&hash->alg.sha);
10731083
ret = 0;
10741084
#endif
10751085
break;
10761086
case WC_HASH_TYPE_SHA224:
10771087
#ifdef WOLFSSL_SHA224
1078-
heap = hash->alg.sha224.heap;
10791088
wc_Sha224Free(&hash->alg.sha224);
10801089
ret = 0;
10811090
#endif
10821091
break;
10831092
case WC_HASH_TYPE_SHA256:
10841093
#ifndef NO_SHA256
1085-
heap = hash->alg.sha256.heap;
10861094
wc_Sha256Free(&hash->alg.sha256);
10871095
ret = 0;
10881096
#endif
10891097
break;
10901098
case WC_HASH_TYPE_SHA384:
10911099
#ifdef WOLFSSL_SHA384
1092-
heap = hash->alg.sha384.heap;
10931100
wc_Sha384Free(&hash->alg.sha384);
10941101
ret = 0;
10951102
#endif
10961103
break;
10971104
case WC_HASH_TYPE_SHA512:
10981105
#ifdef WOLFSSL_SHA512
1099-
heap = hash->alg.sha512.heap;
11001106
wc_Sha512Free(&hash->alg.sha512);
11011107
ret = 0;
11021108
#endif
@@ -1123,7 +1129,6 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
11231129
#endif
11241130
case WC_HASH_TYPE_SHA3_224:
11251131
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
1126-
heap = hash->alg.sha3.heap;
11271132
wc_Sha3_224_Free(&hash->alg.sha3);
11281133
ret = 0;
11291134
#endif
@@ -1149,7 +1154,6 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
11491154

11501155
#ifdef WOLFSSL_SM3
11511156
case WC_HASH_TYPE_SM3:
1152-
heap = hash->alg.sm3.heap;
11531157
wc_Sm3Free(&hash->alg.sm3);
11541158
ret = 0;
11551159
break;
@@ -1172,11 +1176,6 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
11721176
ret = BAD_FUNC_ARG;
11731177
};
11741178

1175-
if (isAllocated) {
1176-
XFREE(hash, heap, DYNAMIC_TYPE_HASHES);
1177-
(void)heap;
1178-
}
1179-
11801179
return ret;
11811180
}
11821181

wolfcrypt/src/rsa.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ static void wc_RsaCleanup(RsaKey* key)
154154
#endif
155155
}
156156

157+
#ifndef WC_NO_CONSTRUCTORS
157158
RsaKey* wc_NewRsaKey(void* heap, int devId)
158159
{
159160
RsaKey* key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA);
@@ -169,6 +170,17 @@ RsaKey* wc_NewRsaKey(void* heap, int devId)
169170
return key;
170171
}
171172

173+
int wc_DeleteRsaKey(RsaKey** key)
174+
{
175+
if ((key == NULL) || (*key == NULL))
176+
return BAD_FUNC_ARG;
177+
wc_FreeRsaKey(*key);
178+
XFREE(*key, (*key)->heap, DYNAMIC_TYPE_RSA);
179+
*key = NULL;
180+
return 0;
181+
}
182+
#endif /* !WC_NO_CONSTRUCTORS */
183+
172184
int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
173185
{
174186
int ret = 0;
@@ -542,16 +554,11 @@ int wc_RsaGetKeyId(RsaKey* key, word32* keyId)
542554
int wc_FreeRsaKey(RsaKey* key)
543555
{
544556
int ret = 0;
545-
void* heap;
546-
byte isAllocated = 0;
547557

548558
if (key == NULL) {
549559
return BAD_FUNC_ARG;
550560
}
551561

552-
heap = key->heap;
553-
isAllocated = key->isAllocated;
554-
555562
wc_RsaCleanup(key);
556563

557564
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
@@ -615,11 +622,6 @@ int wc_FreeRsaKey(RsaKey* key)
615622
wc_fspsm_RsaKeyFree(key);
616623
#endif
617624

618-
if (isAllocated) {
619-
XFREE(key, heap, DYNAMIC_TYPE_RSA);
620-
(void)heap;
621-
}
622-
623625
return ret;
624626
}
625627

0 commit comments

Comments
 (0)