Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow building with WOLFSSL_NO_MALLOC again #8053

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -11299,6 +11299,7 @@ int wc_AesCcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,

#endif /* HAVE_AESCCM */

#ifndef WOLFSSL_NO_MALLOC
Aes* wc_AesNew(void* heap, int devId)
{
Aes* aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_AES);
Expand All @@ -11313,6 +11314,7 @@ Aes* wc_AesNew(void* heap, int devId)
}
return aes;
}
#endif

/* Initialize Aes for use with async hardware */
int wc_AesInit(Aes* aes, void* heap, int devId)
Expand Down Expand Up @@ -11449,14 +11451,18 @@ int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId)
void wc_AesFree(Aes* aes)
{
void* heap;
#ifndef WOLFSSL_NO_MALLOC
byte isAllocated;
#endif

if (aes == NULL) {
return;
}

#ifndef WOLFSSL_NO_MALLOC
heap = aes->heap;
isAllocated = aes->isAllocated;
#endif

#ifdef WC_DEBUG_CIPHER_LIFECYCLE
(void)wc_debug_CipherLifecycleFree(&aes->CipherLifecycleTag, heap, 1);
Expand Down Expand Up @@ -11525,9 +11531,12 @@ void wc_AesFree(Aes* aes)
wc_MemZero_Check(aes, sizeof(Aes));
#endif

#ifndef WOLFSSL_NO_MALLOC
if (isAllocated) {
XFREE(aes, heap, DYNAMIC_TYPE_AES);
}
#endif
(void)heap;

}

Expand Down
12 changes: 10 additions & 2 deletions wolfcrypt/src/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@ int wc_ed25519ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
}
#endif /* HAVE_ED25519_VERIFY */

#ifndef WOLFSSL_NO_MALLOC
ed25519_key* wc_ed25519_new(void* heap, int devId)
{
ed25519_key* key = (ed25519_key*)XMALLOC(sizeof(ed25519_key), heap,
Expand All @@ -983,6 +984,7 @@ ed25519_key* wc_ed25519_new(void* heap, int devId)
}
return key;
}
#endif

/* initialize information and memory for key */
int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId)
Expand Down Expand Up @@ -1024,13 +1026,16 @@ int wc_ed25519_init(ed25519_key* key)
void wc_ed25519_free(ed25519_key* key)
{
void* heap;
#ifndef WOLFSSL_NO_MALLOC
byte isAllocated = 0;

#endif
if (key == NULL)
return;

#ifndef WOLFSSL_NO_MALLOC
heap = key->heap;
isAllocated = key->isAllocated;
#endif

#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
ed25519_hash_free(key, &key->sha);
Expand All @@ -1045,10 +1050,13 @@ void wc_ed25519_free(ed25519_key* key)
wc_MemZero_Check(key, sizeof(ed25519_key));
#endif

#ifndef WOLFSSL_NO_MALLOC
if (isAllocated) {
XFREE(key, heap, DYNAMIC_TYPE_ED25519);
(void)heap;
}
#endif
(void)heap;

}


Expand Down
14 changes: 12 additions & 2 deletions wolfcrypt/src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ int wc_Hash(enum wc_HashType hash_type, const byte* data,
NULL, INVALID_DEVID);
}

#ifndef WOLFSSL_NO_MALLOC
wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId)
{
wc_HashAlg* hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), heap,
Expand All @@ -701,6 +702,7 @@ wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId)
}
return hash;
}
#endif

int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
int devId)
Expand All @@ -710,7 +712,9 @@ int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
if (hash == NULL)
return BAD_FUNC_ARG;

#ifndef WOLFSSL_NO_MALLOC
hash->isAllocated = 0;
#endif
hash->type = type;

switch (type) {
Expand Down Expand Up @@ -1042,19 +1046,23 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
{
int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */
void* heap = NULL;
#ifndef WOLFSSL_NO_MALLOC
byte isAllocated = 0;

#endif
if (hash == NULL)
return BAD_FUNC_ARG;


#ifdef DEBUG_WOLFSSL
if (hash->type != type) {
WOLFSSL_MSG("Hash free type mismatch!");
return BAD_FUNC_ARG;
}
#endif

#ifndef WOLFSSL_NO_MALLOC
isAllocated = hash->isAllocated;
#endif

switch (type) {
case WC_HASH_TYPE_MD5:
Expand Down Expand Up @@ -1170,10 +1178,12 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
ret = BAD_FUNC_ARG;
};

#ifndef WOLFSSL_NO_MALLOC
if (isAllocated) {
XFREE(hash, heap, DYNAMIC_TYPE_HASHES);
(void)heap;
}
#endif
(void)heap;

return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion wolfssl/wolfcrypt/sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ struct wc_Sha {
#else
word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
#endif
void* heap;
#endif
void* heap;
#ifdef WOLFSSL_PIC32MZ_HASH
hashUpdCache cache; /* cache for updates */
#endif
Expand Down
2 changes: 1 addition & 1 deletion wolfssl/wolfcrypt/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ struct wc_Sha256 {
word32 buffLen; /* in bytes */
word32 loLen; /* length in bytes */
word32 hiLen; /* length in bytes */
void* heap;

#ifdef WC_C_DYNAMIC_FALLBACK
int sha_method;
#endif

#endif
void* heap;
#ifdef WOLFSSL_PIC32MZ_HASH
hashUpdCache cache; /* cache for updates */
#endif
Expand Down
1 change: 1 addition & 0 deletions wolfssl/wolfcrypt/sha512.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ struct wc_Sha512 {
cy_stc_crypto_sha_state_t hash_state;
cy_en_crypto_sha_mode_t sha_mode;
cy_stc_crypto_v2_sha512_buffers_t sha_buffers;
void* heap;
#else
word64 digest[WC_SHA512_DIGEST_SIZE / sizeof(word64)];
word64 buffer[WC_SHA512_BLOCK_SIZE / sizeof(word64)];
Expand Down
3 changes: 2 additions & 1 deletion wolfssl/wolfcrypt/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,8 @@ typedef struct w64wrapper {
WOLFSSL_API int wc_strncasecmp(const char *s1, const char *s2, size_t n);
#endif

#if !defined(XSTRDUP) && !defined(USE_WOLF_STRDUP)
#if !defined(XSTRDUP) && !defined(USE_WOLF_STRDUP) &&\
!defined (WOLFSSL_NO_MALLOC)
#define USE_WOLF_STRDUP
#endif
#ifdef USE_WOLF_STRDUP
Expand Down
Loading