From a5e7c91e8442644b8ea9b8a36b15c233fd0322c7 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 23 Aug 2024 12:02:08 -0700 Subject: [PATCH] Fixes for tests. Got CSharp wolfCrypy-test passing with mono. --- .github/workflows/win-csharp-test.yml | 9 ++++----- tests/api.c | 6 +++--- wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs | 16 ++++++++-------- wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs | 10 ++++------ 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/.github/workflows/win-csharp-test.yml b/.github/workflows/win-csharp-test.yml index 4005fb7f39..1e3d07bcb4 100644 --- a/.github/workflows/win-csharp-test.yml +++ b/.github/workflows/win-csharp-test.yml @@ -49,10 +49,9 @@ jobs: run: msbuild /m /p:PlatformToolset=v142 /p:Platform=${{env.BUILD_PLATFORM}} /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}} - name: Run wolfCrypt test - working-directory: ${{env.GITHUB_WORKSPACE}} - run: wolfssl\wrapper\CSharp\Debug\x64\wolfCrypt-test.exe + working-directory: ${{env.GITHUB_WORKSPACE}}wolfssl\wrapper\CSharp\Debug\x64\ + run: wolfCrypt-test.exe - name: Run wolfSSL client/server example - working-directory: ${{env.GITHUB_WORKSPACE}} - run: | - wolfssl\wrapper\CSharp\Debug\x64\wolfSSL-TLS-Server.exe && sleep 1 & wolfssl\wrapper\CSharp\Debug\x64\wolfSSL-TLS-Client.exe + working-directory: ${{env.GITHUB_WORKSPACE}}wolfssl\wrapper\CSharp\Debug\x64\ + run: wolfSSL-TLS-Server.exe && sleep 1 & wolfSSL-TLS-Client.exe diff --git a/tests/api.c b/tests/api.c index 68b1d4c361..91a866c2ae 100644 --- a/tests/api.c +++ b/tests/api.c @@ -27135,7 +27135,7 @@ static int test_wc_EccPrivateKeyToDer(void) byte output[ONEK_BUF]; ecc_key eccKey; WC_RNG rng; - word32 inLen; + word32 inLen = 0; word32 outLen = 0; int ret; @@ -27151,11 +27151,11 @@ static int test_wc_EccPrivateKeyToDer(void) #endif ExpectIntEQ(ret, 0); - inLen = (word32)sizeof(output); /* Bad Cases */ ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG); ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG); - ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E); + inLen = wc_EccPrivateKeyToDer(&eccKey, NULL, 0); + ExpectIntGT(inLen, 0); ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG); /* Good Case */ ExpectIntGT(outLen = (word32)wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0); diff --git a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs index b48f3cefb6..067f9c999e 100755 --- a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs +++ b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs @@ -172,12 +172,11 @@ private static void ecc_test(string hashAlgorithm, int keySize) private static void rsa_test(string hashAlgorithm, int keySize) { IntPtr key = IntPtr.Zero; - - Console.WriteLine("\nStarting RSA tests..."); - IntPtr heap = IntPtr.Zero; int devId = wolfcrypt.INVALID_DEVID; + Console.WriteLine("\nStarting RSA" + keySize + " test for " + hashAlgorithm + "..."); + /* Generate RSA Key Pair */ Console.WriteLine("Testing RSA Key Generation..."); key = wolfcrypt.RsaMakeKey(heap, devId, keySize); @@ -273,13 +272,13 @@ private static void ed25519_test() Console.WriteLine("Testing ED25519 Key Export and Import..."); /* Export Private */ ret = wolfcrypt.Ed25519ExportKeyToDer(key, out privKey); - if (ret != 0 || privKey == null) + if (ret < 0 || privKey == null) { throw new Exception("Ed25519ExportKeyToDer failed"); } /* Export Public */ - ret =wolfcrypt.Ed25519ExportPublicKeyToDer(key, out pubKey, true); - if (ret != 0 || pubKey == null) + ret = wolfcrypt.Ed25519ExportPublicKeyToDer(key, out pubKey, true); + if (ret < 0 || pubKey == null) { throw new Exception("Ed25519ExportKeyToDer failed"); } @@ -357,7 +356,7 @@ private static void curve25519_test() /* Export Public Key B to DER format */ Console.WriteLine("Exporting Public Key B to DER format..."); ret = wolfcrypt.Curve25519ExportPublicKeyToDer(keyB, out derKey, true); - if (ret != 0 || derKey == null) + if (ret < 0 || derKey == null) { throw new Exception("Curve25519ExportPublicKeyToDer failed"); } @@ -384,7 +383,7 @@ private static void curve25519_test() /* Export Public Key A to DER format */ Console.WriteLine("Exporting Public Key A to DER format..."); ret = wolfcrypt.Curve25519ExportPublicKeyToDer(keyA, out derKey, true); - if (ret != 0 || derKey == null) + if (ret < 0 || derKey == null) { throw new Exception("Curve25519ExportPublicKeyToDer failed"); } @@ -462,6 +461,7 @@ public static void Main(string[] args) catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); + Environment.Exit(-1); } } } diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs index f32909a586..b32f22f2db 100755 --- a/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs @@ -244,9 +244,6 @@ private static void log(int lvl, string msg) internal_log(lvl, ptr); } - [DllImport("kernel32.dll", EntryPoint = "RtlFillMemory", SetLastError = false)] - private extern static void RtlFillMemory(IntPtr destination, uint length, byte fill); - /******************************** * Enum types from wolfSSL library */ @@ -475,7 +472,7 @@ public static IntPtr EccImportKey(byte[] keyASN1) { IntPtr idx = Marshal.AllocHGlobal(sizeof(uint)); IntPtr keydata = Marshal.AllocHGlobal(keyASN1.Length); - RtlFillMemory(idx, sizeof(uint), 0); /* zero init */ + Marshal.WriteInt32(idx, 0); Marshal.Copy(keyASN1, 0, keydata, keyASN1.Length); ret = wc_EccPrivateKeyDecode(keydata, idx, key, Convert.ToUInt32(keyASN1.Length)); if (ret != 0) @@ -489,7 +486,7 @@ public static IntPtr EccImportKey(byte[] keyASN1) } catch (Exception e) { - log(ERROR_LOG, "ECC make key exception " + e.ToString()); + log(ERROR_LOG, "ECC import key exception " + e.ToString()); EccFreeKey(key); /* make sure its free'd */ key = IntPtr.Zero; } @@ -519,6 +516,7 @@ public static int EccSign(IntPtr key, byte[] hash, byte[] signature) sigPtr = Marshal.AllocHGlobal(signature.Length); sigLen = Marshal.AllocHGlobal(sizeof(uint)); + Marshal.WriteInt32(sigLen, signature.Length); Marshal.Copy(hash, 0, hashPtr, hash.Length); ret = wc_ecc_sign_hash(hashPtr, Convert.ToUInt32(hash.Length), sigPtr, sigLen, rng, key); @@ -782,7 +780,7 @@ public static IntPtr RsaImportKey(byte[] keyASN1) { IntPtr idx = Marshal.AllocHGlobal(sizeof(uint)); IntPtr keydata = Marshal.AllocHGlobal(keyASN1.Length); - RtlFillMemory(idx, sizeof(uint), 0); /* zero init */ + Marshal.WriteInt32(idx, 0); Marshal.Copy(keyASN1, 0, keydata, keyASN1.Length); ret = wc_RsaPrivateKeyDecode(keydata, idx, key, Convert.ToUInt32(keyASN1.Length)); if (ret != 0)