Skip to content

Commit

Permalink
Fixes for tests. Got CSharp wolfCrypy-test passing with mono.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarske committed Aug 23, 2024
1 parent 8e37de6 commit a5e7c91
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/win-csharp-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand Down Expand Up @@ -462,6 +461,7 @@ public static void Main(string[] args)
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
Environment.Exit(-1);
}
}
}
10 changes: 4 additions & 6 deletions wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a5e7c91

Please sign in to comment.