Skip to content

Commit daef866

Browse files
authored
Merge pull request #8053 from danielinux/fix-no-malloc
Allow building with WOLFSSL_NO_MALLOC again
2 parents c49f571 + a3f6bab commit daef866

7 files changed

Lines changed: 36 additions & 7 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 9 additions & 0 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 WOLFSSL_NO_MALLOC
1130211303
Aes* wc_AesNew(void* heap, int devId)
1130311304
{
1130411305
Aes* aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_AES);
@@ -11313,6 +11314,7 @@ Aes* wc_AesNew(void* heap, int devId)
1131311314
}
1131411315
return aes;
1131511316
}
11317+
#endif
1131611318

1131711319
/* Initialize Aes for use with async hardware */
1131811320
int wc_AesInit(Aes* aes, void* heap, int devId)
@@ -11449,14 +11451,18 @@ int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId)
1144911451
void wc_AesFree(Aes* aes)
1145011452
{
1145111453
void* heap;
11454+
#ifndef WOLFSSL_NO_MALLOC
1145211455
byte isAllocated;
11456+
#endif
1145311457

1145411458
if (aes == NULL) {
1145511459
return;
1145611460
}
1145711461

11462+
#ifndef WOLFSSL_NO_MALLOC
1145811463
heap = aes->heap;
1145911464
isAllocated = aes->isAllocated;
11465+
#endif
1146011466

1146111467
#ifdef WC_DEBUG_CIPHER_LIFECYCLE
1146211468
(void)wc_debug_CipherLifecycleFree(&aes->CipherLifecycleTag, heap, 1);
@@ -11525,9 +11531,12 @@ void wc_AesFree(Aes* aes)
1152511531
wc_MemZero_Check(aes, sizeof(Aes));
1152611532
#endif
1152711533

11534+
#ifndef WOLFSSL_NO_MALLOC
1152811535
if (isAllocated) {
1152911536
XFREE(aes, heap, DYNAMIC_TYPE_AES);
1153011537
}
11538+
#endif
11539+
(void)heap;
1153111540

1153211541
}
1153311542

wolfcrypt/src/ed25519.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +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
971972
ed25519_key* wc_ed25519_new(void* heap, int devId)
972973
{
973974
ed25519_key* key = (ed25519_key*)XMALLOC(sizeof(ed25519_key), heap,
@@ -983,6 +984,7 @@ ed25519_key* wc_ed25519_new(void* heap, int devId)
983984
}
984985
return key;
985986
}
987+
#endif
986988

987989
/* initialize information and memory for key */
988990
int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId)
@@ -1024,13 +1026,16 @@ int wc_ed25519_init(ed25519_key* key)
10241026
void wc_ed25519_free(ed25519_key* key)
10251027
{
10261028
void* heap;
1029+
#ifndef WOLFSSL_NO_MALLOC
10271030
byte isAllocated = 0;
1028-
1031+
#endif
10291032
if (key == NULL)
10301033
return;
10311034

1035+
#ifndef WOLFSSL_NO_MALLOC
10321036
heap = key->heap;
10331037
isAllocated = key->isAllocated;
1038+
#endif
10341039

10351040
#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
10361041
ed25519_hash_free(key, &key->sha);
@@ -1045,10 +1050,13 @@ void wc_ed25519_free(ed25519_key* key)
10451050
wc_MemZero_Check(key, sizeof(ed25519_key));
10461051
#endif
10471052

1053+
#ifndef WOLFSSL_NO_MALLOC
10481054
if (isAllocated) {
10491055
XFREE(key, heap, DYNAMIC_TYPE_ED25519);
1050-
(void)heap;
10511056
}
1057+
#endif
1058+
(void)heap;
1059+
10521060
}
10531061

10541062

wolfcrypt/src/hash.c

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

689+
#ifndef WOLFSSL_NO_MALLOC
689690
wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId)
690691
{
691692
wc_HashAlg* hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), heap,
@@ -701,6 +702,7 @@ wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId)
701702
}
702703
return hash;
703704
}
705+
#endif
704706

705707
int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
706708
int devId)
@@ -710,7 +712,9 @@ int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
710712
if (hash == NULL)
711713
return BAD_FUNC_ARG;
712714

715+
#ifndef WOLFSSL_NO_MALLOC
713716
hash->isAllocated = 0;
717+
#endif
714718
hash->type = type;
715719

716720
switch (type) {
@@ -1042,19 +1046,23 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
10421046
{
10431047
int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */
10441048
void* heap = NULL;
1049+
#ifndef WOLFSSL_NO_MALLOC
10451050
byte isAllocated = 0;
1046-
1051+
#endif
10471052
if (hash == NULL)
10481053
return BAD_FUNC_ARG;
10491054

1055+
10501056
#ifdef DEBUG_WOLFSSL
10511057
if (hash->type != type) {
10521058
WOLFSSL_MSG("Hash free type mismatch!");
10531059
return BAD_FUNC_ARG;
10541060
}
10551061
#endif
10561062

1063+
#ifndef WOLFSSL_NO_MALLOC
10571064
isAllocated = hash->isAllocated;
1065+
#endif
10581066

10591067
switch (type) {
10601068
case WC_HASH_TYPE_MD5:
@@ -1170,10 +1178,12 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
11701178
ret = BAD_FUNC_ARG;
11711179
};
11721180

1181+
#ifndef WOLFSSL_NO_MALLOC
11731182
if (isAllocated) {
11741183
XFREE(hash, heap, DYNAMIC_TYPE_HASHES);
1175-
(void)heap;
11761184
}
1185+
#endif
1186+
(void)heap;
11771187

11781188
return ret;
11791189
}

wolfssl/wolfcrypt/sha.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ struct wc_Sha {
151151
#else
152152
word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
153153
#endif
154-
void* heap;
155154
#endif
155+
void* heap;
156156
#ifdef WOLFSSL_PIC32MZ_HASH
157157
hashUpdCache cache; /* cache for updates */
158158
#endif

wolfssl/wolfcrypt/sha256.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ struct wc_Sha256 {
194194
word32 buffLen; /* in bytes */
195195
word32 loLen; /* length in bytes */
196196
word32 hiLen; /* length in bytes */
197-
void* heap;
198197

199198
#ifdef WC_C_DYNAMIC_FALLBACK
200199
int sha_method;
201200
#endif
202201

203202
#endif
203+
void* heap;
204204
#ifdef WOLFSSL_PIC32MZ_HASH
205205
hashUpdCache cache; /* cache for updates */
206206
#endif

wolfssl/wolfcrypt/sha512.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ struct wc_Sha512 {
144144
cy_stc_crypto_sha_state_t hash_state;
145145
cy_en_crypto_sha_mode_t sha_mode;
146146
cy_stc_crypto_v2_sha512_buffers_t sha_buffers;
147+
void* heap;
147148
#else
148149
word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)];
149150
word64 buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64)];

wolfssl/wolfcrypt/types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ typedef struct w64wrapper {
943943
WOLFSSL_API int wc_strncasecmp(const char *s1, const char *s2, size_t n);
944944
#endif
945945

946-
#if !defined(XSTRDUP) && !defined(USE_WOLF_STRDUP)
946+
#if !defined(XSTRDUP) && !defined(USE_WOLF_STRDUP) &&\
947+
!defined (WOLFSSL_NO_MALLOC)
947948
#define USE_WOLF_STRDUP
948949
#endif
949950
#ifdef USE_WOLF_STRDUP

0 commit comments

Comments
 (0)