Skip to content

Commit

Permalink
refactor wc_AesDelete, wc_curve25519_delete, wc_ed25519_delete, wc_Ha…
Browse files Browse the repository at this point in the history
…shDelete, and wc_DeleteRsaKey to take two arguments, the first a required pointer to the object, the second an optional pointer to the pointer to be zeroed upon successful deletion, for the benefit of calling from C# without unsafe code.

wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs: update for new calling conventions around wc_AesNew, wc_curve25519_new, wc_ed25519_new, wc_HashNew, and wc_NewRsaKey, and the corresponding delete functions.
  • Loading branch information
douzzer committed Oct 19, 2024
1 parent f44d120 commit 2c71a22
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 235 deletions.
17 changes: 9 additions & 8 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions wolfcrypt/src/curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
11 changes: 6 additions & 5 deletions wolfcrypt/src/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
11 changes: 6 additions & 5 deletions wolfcrypt/src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
11 changes: 6 additions & 5 deletions wolfcrypt/src/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Loading

0 comments on commit 2c71a22

Please sign in to comment.