diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 44dc31cb67..6a6b494035 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -10542,7 +10542,7 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz, authTag, authTagSz, authIn, authInSz); #ifdef WOLFSSL_SMALL_STACK - wc_AesDelete(&aes); + wc_AesDelete(aes, NULL); #else wc_AesFree(aes); #endif @@ -10582,7 +10582,7 @@ int wc_GmacVerify(const byte* key, word32 keySz, } #ifdef WOLFSSL_SMALL_STACK - wc_AesDelete(&aes); + wc_AesDelete(aes, NULL); #else wc_AesFree(aes); #endif @@ -11318,13 +11318,14 @@ Aes* wc_AesNew(void* heap, int devId, int *result_code) return aes; } -int wc_AesDelete(Aes** aes) +int wc_AesDelete(Aes *aes, Aes** aes_p) { - if ((aes == NULL) || (*aes == NULL)) + if (aes == NULL) return BAD_FUNC_ARG; - wc_AesFree(*aes); - XFREE(*aes, (*aes)->heap, DYNAMIC_TYPE_AES); - *aes = NULL; + wc_AesFree(aes); + XFREE(aes, aes->heap, DYNAMIC_TYPE_AES); + if (aes_p != NULL) + *aes_p = NULL; return 0; } #endif /* !WC_NO_CONSTRUCTORS */ @@ -14028,7 +14029,7 @@ static WARN_UNUSED_RESULT int AesSivCipher( } #ifdef WOLFSSL_SMALL_STACK - wc_AesDelete(&aes); + wc_AesDelete(aes, NULL); #else wc_AesFree(aes); #endif diff --git a/wolfcrypt/src/curve25519.c b/wolfcrypt/src/curve25519.c index f4c7c11aba..7641055b44 100644 --- a/wolfcrypt/src/curve25519.c +++ b/wolfcrypt/src/curve25519.c @@ -678,12 +678,13 @@ curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code) return key; } -int wc_curve25519_delete(curve25519_key** key) { - if ((key == NULL) || (*key == NULL)) +int wc_curve25519_delete(curve25519_key* key, curve25519_key** key_p) { + if (key == NULL) return BAD_FUNC_ARG; - wc_curve25519_free(*key); - XFREE(*key, (*key)->heap, DYNAMIC_TYPE_CURVE25519); - *key = NULL; + wc_curve25519_free(key); + XFREE(key, key->heap, DYNAMIC_TYPE_CURVE25519); + if (key_p != NULL) + *key_p = NULL; return 0; } #endif /* !WC_NO_CONSTRUCTORS */ diff --git a/wolfcrypt/src/ed25519.c b/wolfcrypt/src/ed25519.c index ce856b9011..09777dde76 100644 --- a/wolfcrypt/src/ed25519.c +++ b/wolfcrypt/src/ed25519.c @@ -991,12 +991,13 @@ ed25519_key* wc_ed25519_new(void* heap, int devId, int *result_code) return key; } -int wc_ed25519_delete(ed25519_key** key) { - if ((key == NULL) || (*key == NULL)) +int wc_ed25519_delete(ed25519_key* key, ed25519_key** key_p) { + if (key == NULL) return BAD_FUNC_ARG; - wc_ed25519_free(*key); - XFREE(*key, (*key)->heap, DYNAMIC_TYPE_ED25519); - *key = NULL; + wc_ed25519_free(key); + XFREE(key, key->heap, DYNAMIC_TYPE_ED25519); + if (key_p != NULL) + *key_p = NULL; return 0; } #endif /* !WC_NO_CONSTRUCTORS */ diff --git a/wolfcrypt/src/hash.c b/wolfcrypt/src/hash.c index f2eefad9ce..b16c47dcb1 100644 --- a/wolfcrypt/src/hash.c +++ b/wolfcrypt/src/hash.c @@ -710,15 +710,16 @@ wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId, return hash; } -int wc_HashDelete(wc_HashAlg **hash) { +int wc_HashDelete(wc_HashAlg *hash, wc_HashAlg **hash_p) { int ret; - if ((hash == NULL) || (*hash == NULL)) + if (hash == NULL) return BAD_FUNC_ARG; - ret = wc_HashFree(*hash, (*hash)->type); + ret = wc_HashFree(hash, hash->type); if (ret < 0) return ret; - XFREE(*hash, (*hash)->heap, DYNAMIC_TYPE_HASHES); - *hash = NULL; + XFREE(hash, hash->heap, DYNAMIC_TYPE_HASHES); + if (hash_p != NULL) + *hash_p = NULL; return 0; } #endif /* !WC_NO_CONSTRUCTORS */ diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index dd5f7f8be2..f5ed3d3533 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -176,13 +176,14 @@ RsaKey* wc_NewRsaKey(void* heap, int devId, int *result_code) return key; } -int wc_DeleteRsaKey(RsaKey** key) +int wc_DeleteRsaKey(RsaKey* key, RsaKey** key_p) { - if ((key == NULL) || (*key == NULL)) + if (key == NULL) return BAD_FUNC_ARG; - wc_FreeRsaKey(*key); - XFREE(*key, (*key)->heap, DYNAMIC_TYPE_RSA); - *key = NULL; + wc_FreeRsaKey(key); + XFREE(key, key->heap, DYNAMIC_TYPE_RSA); + if (key_p != NULL) + *key_p = NULL; return 0; } #endif /* !WC_NO_CONSTRUCTORS */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index b7f8131d2e..4c3c9d771d 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -938,7 +938,7 @@ static void myFipsCb(int ok, int err, const char* hash) #if defined(HAVE_FIPS) && FIPS_VERSION3_LT(6,0,0) && !defined(WC_NO_CONSTRUCTORS) #if !defined(NO_AES) -static WC_MAYBE_UNUSED Aes* wc_AesNew(void* heap, int devId, int *result_code) +static WC_MAYBE_UNUSED Aes* wc_AesNew(void* heap, int thisDevId, int *result_code) { int ret; Aes* aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_AES); @@ -946,7 +946,7 @@ static WC_MAYBE_UNUSED Aes* wc_AesNew(void* heap, int devId, int *result_code) ret = MEMORY_E; } else { - ret = wc_AesInit(aes, heap, devId); + ret = wc_AesInit(aes, heap, thisDevId); if (ret != 0) { XFREE(aes, heap, DYNAMIC_TYPE_AES); aes = NULL; @@ -958,27 +958,28 @@ static WC_MAYBE_UNUSED Aes* wc_AesNew(void* heap, int devId, int *result_code) return aes; } -static WC_MAYBE_UNUSED int wc_AesDelete(Aes** aes) +static WC_MAYBE_UNUSED int wc_AesDelete(Aes *aes, Aes** aes_p) { - if ((aes == NULL) || (*aes == NULL)) + if (aes == NULL) return BAD_FUNC_ARG; - wc_AesFree(*aes); - XFREE(*aes, (*aes)->heap, DYNAMIC_TYPE_AES); - *aes = NULL; + wc_AesFree(aes); + XFREE(aes, aes->heap, DYNAMIC_TYPE_AES); + if (aes_p != NULL) + *aes_p = NULL; return 0; } #endif /* !NO_AES */ #if !defined(NO_RSA) -static WC_MAYBE_UNUSED RsaKey* wc_NewRsaKey(void* heap, int devId, int *result_code) +static WC_MAYBE_UNUSED RsaKey* wc_NewRsaKey(void* heap, int thisDevId, int *result_code) { int ret; RsaKey* key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA); - if (key = NULL) { + if (key == NULL) { ret = MEMORY_E; } else { - ret = wc_InitRsaKey_ex(key, heap, devId); + ret = wc_InitRsaKey_ex(key, heap, thisDevId); if (ret != 0) { XFREE(key, heap, DYNAMIC_TYPE_RSA); key = NULL; @@ -990,120 +991,18 @@ static WC_MAYBE_UNUSED RsaKey* wc_NewRsaKey(void* heap, int devId, int *result_c return key; } -static WC_MAYBE_UNUSED int wc_DeleteRsaKey(RsaKey** key) +static WC_MAYBE_UNUSED int wc_DeleteRsaKey(RsaKey* key, RsaKey** key_p) { - if ((key == NULL) || (*key == NULL)) + if (key == NULL) return BAD_FUNC_ARG; - wc_FreeRsaKey(*key); - XFREE(*key, (*key)->heap, DYNAMIC_TYPE_RSA); - *key = NULL; + wc_FreeRsaKey(key); + XFREE(key, key->heap, DYNAMIC_TYPE_RSA); + if (key_p != NULL) + *key_p = NULL; return 0; } #endif /* !NO_RSA */ -#if !defined(NO_HASH_WRAPPER) -static WC_MAYBE_UNUSED wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId, - int *result_code) -{ - int ret; - wc_HashAlg* hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), heap, - DYNAMIC_TYPE_HASHES); - if (hash == NULL) { - ret = MEMORY_E; - } - else { - ret = wc_HashInit_ex(hash, type, heap, devId); - if (ret != 0) { - XFREE(hash, heap, DYNAMIC_TYPE_HASHES); - hash = NULL; - } - } - - if (result_code != NULL) - *result_code = ret; - - return hash; -} - -static WC_MAYBE_UNUSED int wc_HashDelete(wc_HashAlg **hash) { - int ret; - if ((hash == NULL) || (*hash == NULL)) - return BAD_FUNC_ARG; - ret = wc_HashFree(*hash, (*hash)->type); - if (ret < 0) - return ret; - XFREE(*hash, (*hash)->heap, DYNAMIC_TYPE_HASHES); - *hash = NULL; - return 0; -} -#endif /* !NO_HASH_WRAPPER */ - -#if defined(HAVE_CURVE25519) -static WC_MAYBE_UNUSED curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code) -{ - int ret; - curve25519_key* key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), heap, - DYNAMIC_TYPE_CURVE25519); - if (key == NULL) { - ret = MEMORY_E; - } - else { - ret = wc_curve25519_init_ex(key, heap, devId); - if (ret != 0) { - XFREE(key, heap, DYNAMIC_TYPE_CURVE25519); - key = NULL; - } - } - - if (result_code != NULL) - *result_code = ret; - - return key; -} - -static WC_MAYBE_UNUSED int wc_curve25519_delete(curve25519_key** key) { - if ((key == NULL) || (*key == NULL)) - return BAD_FUNC_ARG; - wc_curve25519_free(*key); - XFREE(*key, (*key)->heap, DYNAMIC_TYPE_CURVE25519); - *key = NULL; - return 0; -} -#endif /* HAVE_CURVE25519 */ - -#if defined(HAVE_ED25519) -static WC_MAYBE_UNUSED ed25519_key* wc_ed25519_new(void* heap, int devId, int *result_code) -{ - int ret; - ed25519_key* key = (ed25519_key*)XMALLOC(sizeof(ed25519_key), heap, - DYNAMIC_TYPE_ED25519); - if (key == NULL) { - ret = MEMORY_E; - } - else { - ret = wc_ed25519_init_ex(key, heap, devId); - if (ret != 0) { - XFREE(key, heap, DYNAMIC_TYPE_ED25519); - key = NULL; - } - } - - if (result_code != NULL) - *result_code = ret; - - return key; -} - -static WC_MAYBE_UNUSED int wc_ed25519_delete(ed25519_key** key) { - if ((key == NULL) || (*key == NULL)) - return BAD_FUNC_ARG; - wc_ed25519_free(*key); - XFREE(*key, (*key)->heap, DYNAMIC_TYPE_ED25519); - *key = NULL; - return 0; -} -#endif /* HAVE_ED25519 */ - #endif /* FIPS_VERSION3_LT(6,0,0) && !WC_NO_CONSTRUCTORS */ #ifdef WOLFSSL_STATIC_MEMORY @@ -6457,7 +6356,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void) #endif #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - (void)wc_HashDelete(&hash); + (void)wc_HashDelete(hash, &hash); #endif return 0; @@ -9680,14 +9579,14 @@ static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key, out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif #ifdef HAVE_AES_DECRYPT #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&dec); + wc_AesDelete(dec, &dec); #else wc_AesFree(dec); #endif @@ -10010,13 +9909,13 @@ static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key, out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif #ifdef HAVE_AES_DECRYPT #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&dec); + wc_AesDelete(dec, &dec); #else wc_AesFree(dec); #endif @@ -10272,13 +10171,13 @@ static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key, out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif #ifdef HAVE_AES_DECRYPT #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&dec); + wc_AesDelete(dec, &dec); #else wc_AesFree(dec); #endif @@ -10484,13 +10383,13 @@ static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key, out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif #ifdef HAVE_AES_DECRYPT #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&dec); + wc_AesDelete(dec, &dec); #else wc_AesFree(dec); #endif @@ -10629,7 +10528,7 @@ static wc_test_ret_t aes_key_size_test(void) out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&aes); + wc_AesDelete(aes, &aes); #else wc_AesFree(aes); #endif @@ -13670,13 +13569,13 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_ctr_test(void) out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif #ifdef HAVE_AES_DECRYPT #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&dec); + wc_AesDelete(dec, &dec); #else wc_AesFree(dec); #endif @@ -14246,13 +14145,13 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_cbc_test(void) out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif #ifdef HAVE_AES_DECRYPT #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&dec); + wc_AesDelete(dec, &dec); #else wc_AesFree(dec); #endif @@ -14324,8 +14223,8 @@ static wc_test_ret_t aes_ecb_direct_test(void) out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); - wc_AesDelete(&dec); + wc_AesDelete(enc, &enc); + wc_AesDelete(dec, &dec); #else wc_AesFree(enc); wc_AesFree(dec); @@ -14521,13 +14420,13 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void) out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif #ifdef HAVE_AES_DECRYPT #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&dec); + wc_AesDelete(dec, &dec); #else wc_AesFree(dec); #endif @@ -14728,13 +14627,13 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void) out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif #ifdef HAVE_AES_DECRYPT #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&dec); + wc_AesDelete(dec, &dec); #else wc_AesFree(dec); #endif @@ -14865,8 +14764,8 @@ static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); - wc_AesDelete(&dec); + wc_AesDelete(enc, &enc); + wc_AesDelete(dec, &dec); #else wc_AesFree(enc); wc_AesFree(dec); @@ -15802,8 +15701,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void) #endif #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); - wc_AesDelete(&dec); + wc_AesDelete(enc, &enc); + wc_AesDelete(dec, &dec); #else wc_AesFree(enc); wc_AesFree(dec); @@ -16026,7 +15925,7 @@ static wc_test_ret_t aesccm_256_test(void) #endif #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&aes); + wc_AesDelete(aes, &aes); #else wc_AesFree(aes); #endif @@ -16319,7 +16218,7 @@ static wc_test_ret_t aesccm_128_test(void) out: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_AesDelete(&enc); + wc_AesDelete(enc, &enc); #else wc_AesFree(enc); #endif @@ -22278,9 +22177,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void) exit_rsa: #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_DeleteRsaKey(&key); + wc_DeleteRsaKey(key, &key); #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN) - wc_DeleteRsaKey(&keypub); + wc_DeleteRsaKey(keypub, &keypub); #endif #ifdef WOLFSSL_TEST_CERT XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); @@ -35361,9 +35260,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void) /* clean up keys when done */ #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_curve25519_delete(&pubKey); - wc_curve25519_delete(&userB); - wc_curve25519_delete(&userA); + wc_curve25519_delete(pubKey, &pubKey); + wc_curve25519_delete(userB, &userB); + wc_curve25519_delete(userA, &userA); #else wc_curve25519_free(pubKey); wc_curve25519_free(userB); @@ -36544,7 +36443,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void) ERROR_OUT(WC_TEST_RET_ENC_NC, cleanup); #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_ed25519_delete(&key3); + wc_ed25519_delete(key3, &key3); #else wc_ed25519_free(key3); #endif @@ -36569,8 +36468,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void) /* clean up keys when done */ #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) - wc_ed25519_delete(&key); - wc_ed25519_delete(&key2); + wc_ed25519_delete(key, &key); + wc_ed25519_delete(key2, &key2); #else wc_ed25519_free(key); wc_ed25519_free(key2); diff --git a/wolfssl/wolfcrypt/aes.h b/wolfssl/wolfcrypt/aes.h index 6f1a313bf9..eaa0c47150 100644 --- a/wolfssl/wolfcrypt/aes.h +++ b/wolfssl/wolfcrypt/aes.h @@ -729,7 +729,7 @@ WOLFSSL_API int wc_AesInit_Label(Aes* aes, const char* label, void* heap, WOLFSSL_API void wc_AesFree(Aes* aes); #ifndef WC_NO_CONSTRUCTORS WOLFSSL_API Aes* wc_AesNew(void* heap, int devId, int *result_code); -WOLFSSL_API int wc_AesDelete(Aes** aes); +WOLFSSL_API int wc_AesDelete(Aes* aes, Aes** aes_p); #endif #ifdef WOLFSSL_AES_SIV diff --git a/wolfssl/wolfcrypt/curve25519.h b/wolfssl/wolfcrypt/curve25519.h index e2367e21b1..4d18c5678e 100644 --- a/wolfssl/wolfcrypt/curve25519.h +++ b/wolfssl/wolfcrypt/curve25519.h @@ -143,7 +143,7 @@ void wc_curve25519_free(curve25519_key* key); WOLFSSL_API curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code); WOLFSSL_API -int wc_curve25519_delete(curve25519_key** key); +int wc_curve25519_delete(curve25519_key* key, curve25519_key** key_p); #endif WOLFSSL_API diff --git a/wolfssl/wolfcrypt/ed25519.h b/wolfssl/wolfcrypt/ed25519.h index 5f017e31a0..8c660b2189 100644 --- a/wolfssl/wolfcrypt/ed25519.h +++ b/wolfssl/wolfcrypt/ed25519.h @@ -185,7 +185,7 @@ void wc_ed25519_free(ed25519_key* key); WOLFSSL_API ed25519_key* wc_ed25519_new(void* heap, int devId, int *result_code); WOLFSSL_API -int wc_ed25519_delete(ed25519_key** key); +int wc_ed25519_delete(ed25519_key* key, ed25519_key** key_p); #endif WOLFSSL_API diff --git a/wolfssl/wolfcrypt/hash.h b/wolfssl/wolfcrypt/hash.h index 2c3bd0363c..edbc949bcb 100644 --- a/wolfssl/wolfcrypt/hash.h +++ b/wolfssl/wolfcrypt/hash.h @@ -195,7 +195,7 @@ WOLFSSL_API int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type); #ifndef WC_NO_CONSTRUCTORS WOLFSSL_API wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId, int *result_code); -WOLFSSL_API int wc_HashDelete(wc_HashAlg **hash); +WOLFSSL_API int wc_HashDelete(wc_HashAlg *hash, wc_HashAlg **hash_p); #endif #ifdef WOLFSSL_HASH_FLAGS diff --git a/wolfssl/wolfcrypt/rsa.h b/wolfssl/wolfcrypt/rsa.h index 4b30e455e2..3f39d5b4df 100644 --- a/wolfssl/wolfcrypt/rsa.h +++ b/wolfssl/wolfcrypt/rsa.h @@ -297,7 +297,7 @@ WOLFSSL_API int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId); WOLFSSL_API int wc_FreeRsaKey(RsaKey* key); #ifndef WC_NO_CONSTRUCTORS WOLFSSL_API RsaKey* wc_NewRsaKey(void* heap, int devId, int *result_code); -WOLFSSL_API int wc_DeleteRsaKey(RsaKey** key); +WOLFSSL_API int wc_DeleteRsaKey(RsaKey* key, RsaKey** key_p); #endif #ifdef WOLF_PRIVATE_KEY_ID diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs index 2e5f30e938..223beafacc 100644 --- a/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs @@ -119,7 +119,9 @@ public class wolfcrypt * RSA */ [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr wc_NewRsaKey(IntPtr heap, int devId); + private static extern IntPtr wc_NewRsaKey(IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private static extern int wc_DeleteRsaKey(IntPtr key, IntPtr key_p); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_InitRsaKey(IntPtr key, IntPtr heap); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -153,7 +155,9 @@ public class wolfcrypt * ED25519 */ [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr wc_ed25519_new(IntPtr heap, int devId); + private static extern IntPtr wc_ed25519_new(IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private static extern int wc_ed25519_delete(IntPtr key, IntPtr key_p); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private static extern int wc_ed25519_init(IntPtr key); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -194,7 +198,9 @@ public class wolfcrypt * Curve25519 */ [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] - private static extern IntPtr wc_curve25519_new(IntPtr heap, int devId); + private static extern IntPtr wc_curve25519_new(IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private static extern int wc_curve25519_delete(IntPtr key, IntPtr key_p); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_curve25519_init(IntPtr key); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -235,7 +241,9 @@ public class wolfcrypt * AES-GCM */ [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] - private extern static IntPtr wc_AesNew(IntPtr heap, int devId); + private extern static IntPtr wc_AesNew(IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private extern static int wc_AesDelete(IntPtr aes, IntPtr aes_p); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_AesFree(IntPtr aes); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -254,7 +262,9 @@ public class wolfcrypt * HASH */ [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] - private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId); + private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private extern static int wc_HashDelete(IntPtr hash, IntPtr hash_p); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_HashInit(IntPtr hash, uint hashType); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -373,7 +383,7 @@ public static int Cleanup() /// Pointer to allocated WC_RNG or null public static IntPtr RandomNew() { - IntPtr rng; + IntPtr rng; try { @@ -386,7 +396,7 @@ public static IntPtr RandomNew() { log(ERROR_LOG, "random new exception " + e.ToString()); rng = IntPtr.Zero; - } + } return rng; } @@ -551,7 +561,7 @@ public static int EccSetRng(IntPtr key, IntPtr rng) public static IntPtr EccImportKey(byte[] keyASN1) { int ret; - IntPtr key = IntPtr.Zero; + IntPtr key = IntPtr.Zero; try { @@ -577,7 +587,7 @@ public static IntPtr EccImportKey(byte[] keyASN1) log(ERROR_LOG, "ECC import key exception " + e.ToString()); EccFreeKey(key); /* make sure its free'd */ key = IntPtr.Zero; - } + } return key; } @@ -713,7 +723,7 @@ public static int EccExportPrivateKeyToDer(IntPtr key, out byte[] derKey) { log(ERROR_LOG, "ECC export private exception " + e.ToString()); ret = EXCEPTION_E; - } + } return ret; } @@ -747,7 +757,7 @@ public static int EccExportPublicKeyToDer(IntPtr key, out byte[] derKey, bool in { log(ERROR_LOG, "ECC export public exception " + e.ToString()); ret = EXCEPTION_E; - } + } return ret; } @@ -1317,12 +1327,12 @@ public static IntPtr RsaMakeKey(IntPtr heap, int devId, int keysize, Int32 expon { int ret; IntPtr key = IntPtr.Zero; - IntPtr rng = IntPtr.Zero; + IntPtr rng = IntPtr.Zero; try { /* Allocate and init new RSA key structure */ - key = wc_NewRsaKey(heap, devId); + key = wc_NewRsaKey(heap, devId, IntPtr.Zero); if (key != IntPtr.Zero) { rng = RandomNew(); @@ -1348,7 +1358,7 @@ public static IntPtr RsaMakeKey(IntPtr heap, int devId, int keysize, Int32 expon if (rng != IntPtr.Zero) RandomFree(rng); if (key != IntPtr.Zero) RsaFreeKey(key); key = IntPtr.Zero; - } + } return key; } @@ -1366,11 +1376,11 @@ public static IntPtr RsaMakeKey(IntPtr heap, int devId, int keysize) public static IntPtr RsaImportKey(byte[] keyASN1) { int ret; - IntPtr key = IntPtr.Zero; + IntPtr key = IntPtr.Zero; try { - key = wc_NewRsaKey(IntPtr.Zero, INVALID_DEVID); + key = wc_NewRsaKey(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero); if (key != IntPtr.Zero) { IntPtr idx = Marshal.AllocHGlobal(sizeof(uint)); @@ -1392,7 +1402,7 @@ public static IntPtr RsaImportKey(byte[] keyASN1) log(ERROR_LOG, "RSA make key exception " + e.ToString()); RsaFreeKey(key); /* make sure its free'd */ key = IntPtr.Zero; - } + } return key; } @@ -1548,7 +1558,8 @@ public static void RsaFreeKey(IntPtr key) { if (key != IntPtr.Zero) { - wc_FreeRsaKey(key); + wc_DeleteRsaKey(key, IntPtr.Zero); + key = IntPtr.Zero; } } /* END RSA */ @@ -1578,7 +1589,7 @@ public static IntPtr Ed25519MakeKey(IntPtr heap, int devId) throw new Exception("Failed to create RNG."); } - key = wc_ed25519_new(heap, devId); + key = wc_ed25519_new(heap, devId, IntPtr.Zero); if (key != IntPtr.Zero) { ret = wc_ed25519_make_key(rng, 32, key); @@ -1595,7 +1606,7 @@ public static IntPtr Ed25519MakeKey(IntPtr heap, int devId) if (rng != IntPtr.Zero) RandomFree(rng); if (ret != 0) { - wc_ed25519_free(key); + wc_ed25519_delete(key, IntPtr.Zero); key = IntPtr.Zero; } } @@ -1611,7 +1622,7 @@ public static IntPtr Ed25519MakeKey(IntPtr heap, int devId) /// Private key used for signing /// 0 on success, otherwise an error code public static int Ed25519SignMsg(byte[] inMsg, out byte[] outMsg, IntPtr key) - { + { int ret; IntPtr inMsgPtr = Marshal.AllocHGlobal(inMsg.Length); IntPtr outMsgPtr = Marshal.AllocHGlobal(ED25519_SIG_SIZE); @@ -1633,7 +1644,7 @@ public static int Ed25519SignMsg(byte[] inMsg, out byte[] outMsg, IntPtr key) /* Clenup */ if (inMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(inMsgPtr); if (outMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(outMsgPtr); - } + } return ret; } @@ -1682,7 +1693,7 @@ public static int Ed25519VerifyMsg(byte[] sig, byte[] msg, IntPtr key) /* Cleanup */ if (sigPtr != IntPtr.Zero) Marshal.FreeHGlobal(sigPtr); if (msgPtr != IntPtr.Zero) Marshal.FreeHGlobal(msgPtr); - } + } return ret; } @@ -1700,7 +1711,7 @@ public static IntPtr Ed25519PrivateKeyDecode(byte[] input) try { - key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID); + key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero); if (key != IntPtr.Zero) { ret = wc_Ed25519PrivateKeyDecode(input, ref idx, key, (uint)input.Length); @@ -1734,7 +1745,7 @@ public static IntPtr Ed25519PublicKeyDecode(byte[] input) try { - key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID); + key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero); if (key != IntPtr.Zero) { ret = wc_Ed25519PublicKeyDecode(input, ref idx, key, (uint)input.Length); @@ -1878,7 +1889,8 @@ public static int Ed25519ExportPublicKeyToDer(IntPtr key, out byte[] pubKey, boo /// Key to be freed public static void Ed25519FreeKey(IntPtr key) { - wc_ed25519_free(key); + wc_ed25519_delete(key, IntPtr.Zero); + key = IntPtr.Zero; } /* END ED25519 */ @@ -2104,7 +2116,7 @@ public static IntPtr Curve25519MakeKey(IntPtr heap, int devId) throw new Exception("Failed to create RNG."); } - key = wc_curve25519_new(heap, devId); + key = wc_curve25519_new(heap, devId, IntPtr.Zero); if (key != IntPtr.Zero) { ret = wc_curve25519_make_key(rng, 32, key); @@ -2121,7 +2133,7 @@ public static IntPtr Curve25519MakeKey(IntPtr heap, int devId) if (rng != IntPtr.Zero) RandomFree(rng); if (ret != 0) { - wc_curve25519_free(key); + wc_curve25519_delete(key, IntPtr.Zero); key = IntPtr.Zero; } } @@ -2142,7 +2154,7 @@ public static IntPtr Curve25519PrivateKeyDecode(byte[] input) try { - key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID); + key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero); if (key != IntPtr.Zero) { ret = wc_Ed25519PrivateKeyDecode(input, ref idx, key, (uint)input.Length); @@ -2176,7 +2188,7 @@ public static IntPtr Curve25519PublicKeyDecode(byte[] input) try { - key = wc_curve25519_new(IntPtr.Zero, INVALID_DEVID); + key = wc_curve25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero); if (key != IntPtr.Zero) { ret = wc_Curve25519PublicKeyDecode(input, ref idx, key, (uint)input.Length); @@ -2280,7 +2292,8 @@ public static int Curve25519ExportPublicKeyToDer(IntPtr key, out byte[] derKey, /// Key to be freed public static void Curve25519FreeKey(IntPtr key) { - wc_curve25519_free(key); + wc_curve25519_delete(key, IntPtr.Zero); + key = IntPtr.Zero; } /* END Curve25519 */ @@ -2313,7 +2326,7 @@ public static int Curve25519SharedSecret(IntPtr privateKey, IntPtr publicKey, by { log(ERROR_LOG, "Curve25519 shared secret exception " + e.ToString()); ret = EXCEPTION_E; - } + } return ret; } @@ -2325,7 +2338,7 @@ public static int Curve25519SharedSecret(IntPtr privateKey, IntPtr publicKey, by /// Allocated Curve25519 key structure or null public static IntPtr Curve25519ImportPrivateKey(byte[] privateKey) { - IntPtr key = IntPtr.Zero; + IntPtr key = IntPtr.Zero; try { @@ -2343,7 +2356,7 @@ public static IntPtr Curve25519ImportPrivateKey(byte[] privateKey) log(ERROR_LOG, "Curve25519 import private key exception " + e.ToString()); if (key != IntPtr.Zero) Marshal.FreeHGlobal(key); key = IntPtr.Zero; - } + } return key; } @@ -2355,7 +2368,7 @@ public static IntPtr Curve25519ImportPrivateKey(byte[] privateKey) /// Allocated Curve25519 key structure or null public static IntPtr Curve25519ImportPublicKey(byte[] publicKey) { - IntPtr key = IntPtr.Zero; + IntPtr key = IntPtr.Zero; try { @@ -2373,7 +2386,7 @@ public static IntPtr Curve25519ImportPublicKey(byte[] publicKey) log(ERROR_LOG, "Curve25519 import public key exception " + e.ToString()); if (key != IntPtr.Zero) Marshal.FreeHGlobal(key); key = IntPtr.Zero; - } + } return key; } @@ -2449,7 +2462,7 @@ public static IntPtr AesNew(IntPtr heap, int devId) try { - aesPtr = wc_AesNew(heap, devId); + aesPtr = wc_AesNew(heap, devId, IntPtr.Zero); if (aesPtr == IntPtr.Zero) { @@ -2460,7 +2473,7 @@ public static IntPtr AesNew(IntPtr heap, int devId) catch (Exception e) { Console.WriteLine($"AES context creation failed: {e.Message}"); - } + } return aesPtr; } @@ -2529,7 +2542,7 @@ public static int AesGcmInit(IntPtr aes, byte[] key, byte[] iv) /* Cleanup */ if (keyPtr != IntPtr.Zero) Marshal.FreeHGlobal(keyPtr); if (ivPtr != IntPtr.Zero) Marshal.FreeHGlobal(ivPtr); - } + } return ret; } @@ -2596,7 +2609,7 @@ public static int AesGcmEncrypt(IntPtr aes, byte[] iv, byte[] plaintext, if (plaintextPtr != IntPtr.Zero) Marshal.FreeHGlobal(plaintextPtr); if (authTagPtr != IntPtr.Zero) Marshal.FreeHGlobal(authTagPtr); if (addAuthPtr != IntPtr.Zero) Marshal.FreeHGlobal(addAuthPtr); - } + } return ret; } @@ -2663,7 +2676,7 @@ public static int AesGcmDecrypt(IntPtr aes, byte[] iv, byte[] ciphertext, if (plaintextPtr != IntPtr.Zero) Marshal.FreeHGlobal(plaintextPtr); if (authTagPtr != IntPtr.Zero) Marshal.FreeHGlobal(authTagPtr); if (addAuthPtr != IntPtr.Zero) Marshal.FreeHGlobal(addAuthPtr); - } + } return ret; } @@ -2676,7 +2689,8 @@ public static void AesGcmFree(IntPtr aes) { if (aes != IntPtr.Zero) { - wc_AesFree(aes); + wc_AesDelete(aes, IntPtr.Zero); + aes = IntPtr.Zero; } } /* END AES-GCM */ @@ -2700,7 +2714,7 @@ public static IntPtr HashNew(uint hashType, IntPtr heap, int devId) try { /* Allocate new hash */ - hash = wc_HashNew(hashType, heap, devId); + hash = wc_HashNew(hashType, heap, devId, IntPtr.Zero); if (hash == IntPtr.Zero) { throw new Exception("Failed to allocate new hash context."); @@ -2709,7 +2723,7 @@ public static IntPtr HashNew(uint hashType, IntPtr heap, int devId) catch (Exception e) { log(ERROR_LOG, "HashNew Exception: " + e.ToString()); - } + } return hash; } @@ -2740,8 +2754,11 @@ public static int InitHash(IntPtr hash, uint hashType) { /* Cleanup */ log(ERROR_LOG, "InitHash Exception: " + e.ToString()); - if (hash != IntPtr.Zero) wc_HashFree(hash, hashType); - } + if (hash != IntPtr.Zero) { + wc_HashDelete(hash, IntPtr.Zero); + hash = IntPtr.Zero; + } + } return ret; } @@ -2856,7 +2873,8 @@ public static int HashFree(IntPtr hash, uint hashType) throw new Exception("Hash context is null, cannot free."); /* Free hash */ - ret = wc_HashFree(hash, hashType); + ret = wc_HashDelete(hash, IntPtr.Zero); + hash = IntPtr.Zero; if (ret != 0) { throw new Exception($"Failed to free hash context. Error code: {ret}"); @@ -2865,7 +2883,7 @@ public static int HashFree(IntPtr hash, uint hashType) catch (Exception e) { log(ERROR_LOG, "HashFree Exception: " + e.ToString()); - } + } return ret; }