From 4753e1c32e664903254fcb0db5d61c3fac85e230 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 8 Oct 2024 10:37:45 -0700 Subject: [PATCH] Use `byte` for `isAllocated` bit-field. Cleanup some of the "heap" hint logic. --- wolfcrypt/src/aes.c | 10 ++++++---- wolfcrypt/src/curve25519.c | 4 ++-- wolfcrypt/src/ed25519.c | 4 ++-- wolfcrypt/src/hash.c | 3 ++- wolfcrypt/src/rsa.c | 6 +++--- wolfssl/wolfcrypt/curve25519.h | 3 +-- wolfssl/wolfcrypt/ed25519.h | 4 ++-- wolfssl/wolfcrypt/hash.h | 2 +- wolfssl/wolfcrypt/rsa.h | 2 +- 9 files changed, 20 insertions(+), 18 deletions(-) diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 25b7be1c1..72dbe696d 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -11448,16 +11448,18 @@ int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId) /* Free Aes from use with async hardware */ void wc_AesFree(Aes* aes) { - unsigned int isAllocated; + void* heap; + byte isAllocated; if (aes == NULL) { return; } + heap = aes->heap; isAllocated = aes->isAllocated; #ifdef WC_DEBUG_CIPHER_LIFECYCLE - (void)wc_debug_CipherLifecycleFree(&aes->CipherLifecycleTag, aes->heap, 1); + (void)wc_debug_CipherLifecycleFree(&aes->CipherLifecycleTag, heap, 1); #endif #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES) @@ -11495,7 +11497,7 @@ void wc_AesFree(Aes* aes) #endif #if defined(WOLFSSL_AESGCM_STREAM) && defined(WOLFSSL_SMALL_STACK) && \ !defined(WOLFSSL_AESNI) - XFREE(aes->streamData, aes->heap, DYNAMIC_TYPE_AES); + XFREE(aes->streamData, heap, DYNAMIC_TYPE_AES); aes->streamData = NULL; #endif @@ -11524,7 +11526,7 @@ void wc_AesFree(Aes* aes) #endif if (isAllocated) { - XFREE(aes, aes->heap, DYNAMIC_TYPE_AES); + XFREE(aes, heap, DYNAMIC_TYPE_AES); } } diff --git a/wolfcrypt/src/curve25519.c b/wolfcrypt/src/curve25519.c index 304fa3b95..db3205a04 100644 --- a/wolfcrypt/src/curve25519.c +++ b/wolfcrypt/src/curve25519.c @@ -707,14 +707,14 @@ int wc_curve25519_init(curve25519_key* key) /* Clean the memory of a key */ void wc_curve25519_free(curve25519_key* key) { - int isAllocated = 0; void* heap; + byte isAllocated = 0; if (key == NULL) return; - isAllocated = key->isAllocated; heap = key->heap; + isAllocated = key->isAllocated; #ifdef WOLFSSL_SE050 se050_curve25519_free_key(key); diff --git a/wolfcrypt/src/ed25519.c b/wolfcrypt/src/ed25519.c index 363ecc4aa..d4610d3d9 100644 --- a/wolfcrypt/src/ed25519.c +++ b/wolfcrypt/src/ed25519.c @@ -1023,14 +1023,14 @@ int wc_ed25519_init(ed25519_key* key) /* clear memory of key */ void wc_ed25519_free(ed25519_key* key) { - int isAllocated = 0; void* heap; + byte isAllocated = 0; if (key == NULL) return; - isAllocated = key->isAllocated; heap = key->heap; + isAllocated = key->isAllocated; #ifdef WOLFSSL_ED25519_PERSISTENT_SHA ed25519_hash_free(key, &key->sha); diff --git a/wolfcrypt/src/hash.c b/wolfcrypt/src/hash.c index 3e0c173c9..dc3521c11 100644 --- a/wolfcrypt/src/hash.c +++ b/wolfcrypt/src/hash.c @@ -1041,8 +1041,8 @@ int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out) int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type) { int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */ - int isAllocated = 0; void* heap = NULL; + byte isAllocated = 0; if (hash == NULL) return BAD_FUNC_ARG; @@ -1172,6 +1172,7 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type) if (isAllocated) { XFREE(hash, heap, DYNAMIC_TYPE_HASHES); + (void)heap; } return ret; diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index 29295716b..3cd4c324b 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -542,15 +542,15 @@ int wc_RsaGetKeyId(RsaKey* key, word32* keyId) int wc_FreeRsaKey(RsaKey* key) { int ret = 0; - int isAllocated = 0; void* heap; + byte isAllocated = 0; if (key == NULL) { return BAD_FUNC_ARG; } - isAllocated = key->isAllocated; heap = key->heap; + isAllocated = key->isAllocated; wc_RsaCleanup(key); @@ -587,7 +587,7 @@ int wc_FreeRsaKey(RsaKey* key) mp_clear(&key->n); #ifdef WOLFSSL_XILINX_CRYPT - XFREE(key->mod, key->heap, DYNAMIC_TYPE_KEY); + XFREE(key->mod, heap, DYNAMIC_TYPE_KEY); key->mod = NULL; #endif diff --git a/wolfssl/wolfcrypt/curve25519.h b/wolfssl/wolfcrypt/curve25519.h index 2d4d85173..d6240d626 100644 --- a/wolfssl/wolfcrypt/curve25519.h +++ b/wolfssl/wolfcrypt/curve25519.h @@ -99,8 +99,7 @@ struct curve25519_key { /* bit fields */ byte pubSet:1; byte privSet:1; - - unsigned int isAllocated:1; /* flag indicates if structure was allocated */ + byte isAllocated:1; /* flag indicates if structure was allocated */ }; enum { diff --git a/wolfssl/wolfcrypt/ed25519.h b/wolfssl/wolfcrypt/ed25519.h index b8b483ce2..763553ffa 100644 --- a/wolfssl/wolfcrypt/ed25519.h +++ b/wolfssl/wolfcrypt/ed25519.h @@ -106,10 +106,10 @@ struct ed25519_key { void *heap; #ifdef WOLFSSL_ED25519_PERSISTENT_SHA wc_Sha512 sha; - unsigned int sha_clean_flag : 1; + byte sha_clean_flag : 1; #endif /* flag indicates if structure was allocated */ - unsigned int isAllocated : 1; + byte isAllocated : 1; }; #ifndef WC_ED25519KEY_TYPE_DEFINED diff --git a/wolfssl/wolfcrypt/hash.h b/wolfssl/wolfcrypt/hash.h index 838d06665..0fe45bb13 100644 --- a/wolfssl/wolfcrypt/hash.h +++ b/wolfssl/wolfcrypt/hash.h @@ -125,7 +125,7 @@ typedef union { typedef struct { wc_Hashes alg; enum wc_HashType type; /* sanity check */ - unsigned int isAllocated:1; /* flag indicates if structure was allocated */ + byte isAllocated:1; /* flag indicates if structure was allocated */ } wc_HashAlg; #endif /* !NO_HASH_WRAPPER */ diff --git a/wolfssl/wolfcrypt/rsa.h b/wolfssl/wolfcrypt/rsa.h index 4cff68a43..0cf701dc9 100644 --- a/wolfssl/wolfcrypt/rsa.h +++ b/wolfssl/wolfcrypt/rsa.h @@ -269,7 +269,7 @@ struct RsaKey { #if defined(WOLFSSL_RENESAS_FSPSM) FSPSM_RSA_CTX ctx; #endif - unsigned int isAllocated:1; /* flag indicates if structure was allocated */ + byte isAllocated:1; /* flag indicates if structure was allocated */ }; #ifndef WC_RSAKEY_TYPE_DEFINED