Skip to content

Commit 4f0836e

Browse files
authored
Merge pull request #9291 from JacobBarthelmeh/csharp
Fixes for Ed25519 raw key import with C# wrapper
2 parents 9872207 + 9debdda commit 4f0836e

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ private static void ed25519_test()
471471
IntPtr key = IntPtr.Zero;
472472
byte[] privKey;
473473
byte[] pubKey;
474+
uint pubKeySz;
474475

475476
Console.WriteLine("\nStarting ED25519 tests...");
476477

@@ -537,8 +538,33 @@ private static void ed25519_test()
537538
}
538539
Console.WriteLine("ED25519 Signature Verification test passed.");
539540

541+
/* test importing a raw public key */
542+
ret = wolfcrypt.Ed25519ExportPublic(key, pubKey, out pubKeySz);
543+
if (ret < 0 || pubKey == null) {
544+
throw new Exception("Ed25519ExportPublic failed");
545+
}
546+
Console.WriteLine("ED25519 Export Raw Public test passed.");
547+
548+
if (importedPubKey != IntPtr.Zero) {
549+
wolfcrypt.Ed25519FreeKey(importedPubKey);
550+
}
551+
ret = wolfcrypt.Ed25519ImportPublic(pubKey, pubKeySz, out importedPubKey);
552+
if (importedPubKey == IntPtr.Zero) {
553+
throw new Exception("Ed25519ImportPublic failed");
554+
}
555+
Console.WriteLine("ED25519 Import Raw Public test passed.");
556+
540557
/* Cleanup */
541-
if (key != IntPtr.Zero) wolfcrypt.Ed25519FreeKey(key);
558+
if (key != IntPtr.Zero) {
559+
wolfcrypt.Ed25519FreeKey(key);
560+
}
561+
if (importedPubKey != IntPtr.Zero) {
562+
wolfcrypt.Ed25519FreeKey(importedPubKey);
563+
}
564+
if (importedPrivKey != IntPtr.Zero) {
565+
wolfcrypt.Ed25519FreeKey(importedPrivKey);
566+
}
567+
542568
} /* END ed25519_test */
543569

544570
private static void curve25519_test()

wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ public static int Ed25519ImportPublic(byte[] inMsg, uint inLen, out IntPtr key)
22062206
try
22072207
{
22082208
/* Allocate memory */
2209-
key = Marshal.AllocHGlobal(ED25519_PUB_KEY_SIZE);
2209+
key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
22102210
if (key == IntPtr.Zero)
22112211
{
22122212
throw new OutOfMemoryException("Failed to allocate memory for the key.");
@@ -2222,20 +2222,26 @@ public static int Ed25519ImportPublic(byte[] inMsg, uint inLen, out IntPtr key)
22222222
ret = wc_ed25519_import_public(inMsgPtr, inLen, key);
22232223
if (ret != 0)
22242224
{
2225+
if (key != IntPtr.Zero) {
2226+
wc_ed25519_delete(key, IntPtr.Zero);
2227+
key = IntPtr.Zero;
2228+
}
22252229
return ret;
22262230
}
22272231
}
22282232
catch (Exception ex)
22292233
{
22302234
Console.WriteLine("Exception in EdImportPublic: " + ex.Message);
2231-
2235+
if (key != IntPtr.Zero) {
2236+
wc_ed25519_delete(key, IntPtr.Zero);
2237+
key = IntPtr.Zero;
2238+
}
22322239
return EXCEPTION_E;
22332240
}
22342241
finally
22352242
{
22362243
/* Cleanup */
22372244
if (inMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(inMsgPtr);
2238-
if (key != IntPtr.Zero) Marshal.FreeHGlobal(key);
22392245
}
22402246

22412247
return ret;
@@ -2415,7 +2421,7 @@ public static IntPtr Curve25519PrivateKeyDecode(byte[] input)
24152421

24162422
try
24172423
{
2418-
key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
2424+
key = wc_curve25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
24192425
if (key != IntPtr.Zero)
24202426
{
24212427
ret = wc_Ed25519PrivateKeyDecode(input, ref idx, key, (uint)input.Length);

0 commit comments

Comments
 (0)