Skip to content

Commit

Permalink
wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs: update for new calling co…
Browse files Browse the repository at this point in the history
…nventions 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 09d8ab5
Showing 1 changed file with 51 additions and 26 deletions.
77 changes: 51 additions & 26 deletions wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_InitRsaKey(IntPtr key, IntPtr heap);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
Expand Down Expand Up @@ -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);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_init(IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
Expand Down Expand Up @@ -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);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_init(IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
Expand Down Expand Up @@ -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);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesFree(IntPtr aes);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
Expand All @@ -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);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_HashInit(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
Expand Down Expand Up @@ -1322,7 +1332,7 @@ public static IntPtr RsaMakeKey(IntPtr heap, int devId, int keysize, Int32 expon
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();
Expand Down Expand Up @@ -1370,7 +1380,7 @@ public static IntPtr RsaImportKey(byte[] keyASN1)

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));
Expand Down Expand Up @@ -1548,7 +1558,9 @@ public static void RsaFreeKey(IntPtr key)
{
if (key != IntPtr.Zero)
{
wc_FreeRsaKey(key);
unsafe {
wc_DeleteRsaKey(&key);
}
}
}
/* END RSA */
Expand Down Expand Up @@ -1578,7 +1590,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);
Expand All @@ -1595,8 +1607,9 @@ public static IntPtr Ed25519MakeKey(IntPtr heap, int devId)
if (rng != IntPtr.Zero) RandomFree(rng);
if (ret != 0)
{
wc_ed25519_free(key);
key = IntPtr.Zero;
unsafe {
wc_ed25519_delete(&key);
}
}
}

Expand Down Expand Up @@ -1700,7 +1713,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);
Expand Down Expand Up @@ -1734,7 +1747,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);
Expand Down Expand Up @@ -1878,7 +1891,9 @@ public static int Ed25519ExportPublicKeyToDer(IntPtr key, out byte[] pubKey, boo
/// <param name="key">Key to be freed</param>
public static void Ed25519FreeKey(IntPtr key)
{
wc_ed25519_free(key);
unsafe {
wc_ed25519_delete(&key);
}
}
/* END ED25519 */

Expand Down Expand Up @@ -2104,7 +2119,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);
Expand All @@ -2121,8 +2136,9 @@ public static IntPtr Curve25519MakeKey(IntPtr heap, int devId)
if (rng != IntPtr.Zero) RandomFree(rng);
if (ret != 0)
{
wc_curve25519_free(key);
key = IntPtr.Zero;
unsafe {
wc_curve25519_delete(&key);
}
}
}

Expand All @@ -2142,7 +2158,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);
Expand Down Expand Up @@ -2176,7 +2192,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);
Expand Down Expand Up @@ -2280,7 +2296,9 @@ public static int Curve25519ExportPublicKeyToDer(IntPtr key, out byte[] derKey,
/// <param name="key">Key to be freed</param>
public static void Curve25519FreeKey(IntPtr key)
{
wc_curve25519_free(key);
unsafe {
wc_curve25519_delete(&key);
}
}
/* END Curve25519 */

Expand Down Expand Up @@ -2449,7 +2467,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)
{
Expand Down Expand Up @@ -2676,7 +2694,9 @@ public static void AesGcmFree(IntPtr aes)
{
if (aes != IntPtr.Zero)
{
wc_AesFree(aes);
unsafe {
wc_AesDelete(&aes);
}
}
}
/* END AES-GCM */
Expand All @@ -2700,7 +2720,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.");
Expand Down Expand Up @@ -2740,8 +2760,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) {
unsafe {
wc_HashDelete(&hash);
}
}

return ret;
}
Expand Down Expand Up @@ -2856,7 +2879,9 @@ public static int HashFree(IntPtr hash, uint hashType)
throw new Exception("Hash context is null, cannot free.");

/* Free hash */
ret = wc_HashFree(hash, hashType);
unsafe {
ret = wc_HashDelete(&hash);
}
if (ret != 0)
{
throw new Exception($"Failed to free hash context. Error code: {ret}");
Expand Down

0 comments on commit 09d8ab5

Please sign in to comment.