Skip to content

Commit 996986d

Browse files
committed
refactor wc_AesDelete, wc_curve25519_delete, wc_ed25519_delete, wc_HashDelete, 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.
1 parent f44d120 commit 996986d

12 files changed

Lines changed: 155 additions & 233 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10542,7 +10542,7 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
1054210542
authTag, authTagSz, authIn, authInSz);
1054310543

1054410544
#ifdef WOLFSSL_SMALL_STACK
10545-
wc_AesDelete(&aes);
10545+
wc_AesDelete(aes, NULL);
1054610546
#else
1054710547
wc_AesFree(aes);
1054810548
#endif
@@ -10582,7 +10582,7 @@ int wc_GmacVerify(const byte* key, word32 keySz,
1058210582

1058310583
}
1058410584
#ifdef WOLFSSL_SMALL_STACK
10585-
wc_AesDelete(&aes);
10585+
wc_AesDelete(aes, NULL);
1058610586
#else
1058710587
wc_AesFree(aes);
1058810588
#endif
@@ -11318,13 +11318,14 @@ Aes* wc_AesNew(void* heap, int devId, int *result_code)
1131811318
return aes;
1131911319
}
1132011320

11321-
int wc_AesDelete(Aes** aes)
11321+
int wc_AesDelete(Aes *aes, Aes** aes_p)
1132211322
{
11323-
if ((aes == NULL) || (*aes == NULL))
11323+
if (aes == NULL)
1132411324
return BAD_FUNC_ARG;
11325-
wc_AesFree(*aes);
11326-
XFREE(*aes, (*aes)->heap, DYNAMIC_TYPE_AES);
11327-
*aes = NULL;
11325+
wc_AesFree(aes);
11326+
XFREE(aes, aes->heap, DYNAMIC_TYPE_AES);
11327+
if (aes_p != NULL)
11328+
*aes_p = NULL;
1132811329
return 0;
1132911330
}
1133011331
#endif /* !WC_NO_CONSTRUCTORS */
@@ -14028,7 +14029,7 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1402814029
}
1402914030

1403014031
#ifdef WOLFSSL_SMALL_STACK
14031-
wc_AesDelete(&aes);
14032+
wc_AesDelete(aes, NULL);
1403214033
#else
1403314034
wc_AesFree(aes);
1403414035
#endif

wolfcrypt/src/curve25519.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,13 @@ curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code)
678678
return key;
679679
}
680680

681-
int wc_curve25519_delete(curve25519_key** key) {
682-
if ((key == NULL) || (*key == NULL))
681+
int wc_curve25519_delete(curve25519_key* key, curve25519_key** key_p) {
682+
if (key == NULL)
683683
return BAD_FUNC_ARG;
684-
wc_curve25519_free(*key);
685-
XFREE(*key, (*key)->heap, DYNAMIC_TYPE_CURVE25519);
686-
*key = NULL;
684+
wc_curve25519_free(key);
685+
XFREE(key, key->heap, DYNAMIC_TYPE_CURVE25519);
686+
if (key_p != NULL)
687+
*key_p = NULL;
687688
return 0;
688689
}
689690
#endif /* !WC_NO_CONSTRUCTORS */

wolfcrypt/src/ed25519.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -991,12 +991,13 @@ ed25519_key* wc_ed25519_new(void* heap, int devId, int *result_code)
991991
return key;
992992
}
993993

994-
int wc_ed25519_delete(ed25519_key** key) {
995-
if ((key == NULL) || (*key == NULL))
994+
int wc_ed25519_delete(ed25519_key* key, ed25519_key** key_p) {
995+
if (key == NULL)
996996
return BAD_FUNC_ARG;
997-
wc_ed25519_free(*key);
998-
XFREE(*key, (*key)->heap, DYNAMIC_TYPE_ED25519);
999-
*key = NULL;
997+
wc_ed25519_free(key);
998+
XFREE(key, key->heap, DYNAMIC_TYPE_ED25519);
999+
if (key_p != NULL)
1000+
*key_p = NULL;
10001001
return 0;
10011002
}
10021003
#endif /* !WC_NO_CONSTRUCTORS */

wolfcrypt/src/hash.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,15 +710,16 @@ wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId,
710710
return hash;
711711
}
712712

713-
int wc_HashDelete(wc_HashAlg **hash) {
713+
int wc_HashDelete(wc_HashAlg *hash, wc_HashAlg **hash_p) {
714714
int ret;
715-
if ((hash == NULL) || (*hash == NULL))
715+
if (hash == NULL)
716716
return BAD_FUNC_ARG;
717-
ret = wc_HashFree(*hash, (*hash)->type);
717+
ret = wc_HashFree(hash, hash->type);
718718
if (ret < 0)
719719
return ret;
720-
XFREE(*hash, (*hash)->heap, DYNAMIC_TYPE_HASHES);
721-
*hash = NULL;
720+
XFREE(hash, hash->heap, DYNAMIC_TYPE_HASHES);
721+
if (hash_p != NULL)
722+
*hash_p = NULL;
722723
return 0;
723724
}
724725
#endif /* !WC_NO_CONSTRUCTORS */

wolfcrypt/src/rsa.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,14 @@ RsaKey* wc_NewRsaKey(void* heap, int devId, int *result_code)
176176
return key;
177177
}
178178

179-
int wc_DeleteRsaKey(RsaKey** key)
179+
int wc_DeleteRsaKey(RsaKey* key, RsaKey** key_p)
180180
{
181-
if ((key == NULL) || (*key == NULL))
181+
if (key == NULL)
182182
return BAD_FUNC_ARG;
183-
wc_FreeRsaKey(*key);
184-
XFREE(*key, (*key)->heap, DYNAMIC_TYPE_RSA);
185-
*key = NULL;
183+
wc_FreeRsaKey(key);
184+
XFREE(key, key->heap, DYNAMIC_TYPE_RSA);
185+
if (key_p != NULL)
186+
*key_p = NULL;
186187
return 0;
187188
}
188189
#endif /* !WC_NO_CONSTRUCTORS */

0 commit comments

Comments
 (0)