From 8e37de61e03602403c13f0fabd3a9b99224c0240 Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 23 Aug 2024 10:11:41 -0700 Subject: [PATCH] Setup the CSharp wrapper to have its own wolfSSL project and user_settings.h. Revert IDE/WIN. Execute CI test applications. --- .github/workflows/win-csharp-test.yml | 9 + IDE/WIN/user_settings.h | 10 - wolfcrypt/src/asn.c | 20 +- wrapper/CSharp/include.am | 2 + wrapper/CSharp/user_settings.h | 132 ++++ .../CSharp/wolfCrypt-Test/wolfCrypt-Test.cs | 603 +++++++++--------- .../wolfCrypt-Test/wolfCrypt-Test.csproj | 10 +- .../wolfSSL-DTLS-PSK-Server.csproj | 8 +- .../wolfSSL-DTLS-Server.csproj | 8 +- .../wolfSSL-Example-IOCallbacks.csproj | 8 +- .../wolfSSL-TLS-Client.csproj | 8 +- .../wolfSSL-TLS-PSK-Client.csproj | 8 +- .../wolfSSL-TLS-PSK-Server.csproj | 8 +- .../wolfSSL-TLS-Server.csproj | 8 +- .../wolfSSL-TLS-ServerThreaded.csproj | 8 +- wrapper/CSharp/wolfSSL_CSharp.sln | 141 ++-- .../wolfSSL_CSharp/wolfSSL_CSharp.csproj | 8 +- wrapper/CSharp/wolfssl.vcxproj | 456 +++++++++++++ 18 files changed, 975 insertions(+), 480 deletions(-) create mode 100644 wrapper/CSharp/user_settings.h create mode 100644 wrapper/CSharp/wolfssl.vcxproj diff --git a/.github/workflows/win-csharp-test.yml b/.github/workflows/win-csharp-test.yml index 2651b35a96..4005fb7f39 100644 --- a/.github/workflows/win-csharp-test.yml +++ b/.github/workflows/win-csharp-test.yml @@ -47,3 +47,12 @@ jobs: # Add additional options to the MSBuild command line here (like platform or verbosity level). # See https://docs.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference 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 + + - 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 diff --git a/IDE/WIN/user_settings.h b/IDE/WIN/user_settings.h index bbed832e47..a1011abf8c 100644 --- a/IDE/WIN/user_settings.h +++ b/IDE/WIN/user_settings.h @@ -17,16 +17,6 @@ #define WOLFSSL_SEND_HRR_COOKIE #define WOLFSSL_DTLS_CID -/* Added for CSHarp wrapper */ -#define WOLFSSL_KEY_GEN -#define WOLFSSL_ASN_TEMPLATE /* default */ -#define HAVE_ED25519 -#define HAVE_CURVE25519 -#define WOLFSSL_SHA512 - -/* optional debug logging */ -#define DEBUG_WOLFSSL - /* Configurations */ #if defined(HAVE_FIPS) /* FIPS */ diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 53ffc9b76a..a0b211907a 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -22552,7 +22552,7 @@ static int DecodeCertReq(DecodedCert* cert, int* criticalExt) { DECL_ASNGETDATA(dataASN, certReqASN_Length); int ret = 0; - byte version; + byte version = 0; word32 idx; CALLOC_ASNGETDATA(dataASN, certReqASN_Length, ret, cert->heap); @@ -27698,7 +27698,7 @@ static int SetCertificatePolicies(byte *output, byte oid[MAX_OID_SZ]; word32 oidSz; word32 sz = 0; - int piSz; + int piSz = 0; if ((input == NULL) || (nb_certpol > MAX_CERTPOL_NB)) { ret = BAD_FUNC_ARG; @@ -30231,8 +30231,8 @@ int AddSignature(byte* buf, int bodySz, const byte* sig, int sigSz, return (int)(idx + seqSz); #else DECL_ASNSETDATA(dataASN, sigASN_Length); - word32 seqSz; - int sz; + word32 seqSz = 0; + int sz = 0; int ret = 0; CALLOC_ASNSETDATA(dataASN, sigASN_Length, ret, NULL); @@ -34813,6 +34813,7 @@ int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 *inLen, /* Write a Private ecc key, including public to DER format, * length on success else < 0 */ +/* Note: use wc_EccKeyDerSize to get length only */ WOLFSSL_ABI int wc_EccKeyToDer(ecc_key* key, byte* output, word32 inLen) { @@ -34824,10 +34825,7 @@ int wc_EccKeyToDer(ecc_key* key, byte* output, word32 inLen) int wc_EccKeyDerSize(ecc_key* key, int pub) { word32 sz = 0; - int ret; - - ret = wc_BuildEccKeyDer(key, NULL, &sz, pub, 1); - + int ret = wc_BuildEccKeyDer(key, NULL, &sz, pub, 1); if (ret != WC_NO_ERR_TRACE(LENGTH_ONLY_E)) { return ret; } @@ -34838,7 +34836,11 @@ int wc_EccKeyDerSize(ecc_key* key, int pub) * length on success else < 0 */ int wc_EccPrivateKeyToDer(ecc_key* key, byte* output, word32 inLen) { - return wc_BuildEccKeyDer(key, output, &inLen, 0, 1); + int ret = wc_BuildEccKeyDer(key, output, &inLen, 0, 1); + if (ret == WC_NO_ERR_TRACE(LENGTH_ONLY_E)) { + return (int)inLen; + } + return ret; } #ifdef HAVE_PKCS8 diff --git a/wrapper/CSharp/include.am b/wrapper/CSharp/include.am index 175f41fe75..b730816d82 100644 --- a/wrapper/CSharp/include.am +++ b/wrapper/CSharp/include.am @@ -26,6 +26,8 @@ EXTRA_DIST+= wrapper/CSharp/wolfSSL-Example-IOCallbacks/Properties/AssemblyInfo. EXTRA_DIST+= wrapper/CSharp/wolfSSL-Example-IOCallbacks/wolfSSL-Example-IOCallbacks.cs EXTRA_DIST+= wrapper/CSharp/wolfSSL-Example-IOCallbacks/wolfSSL-Example-IOCallbacks.csproj EXTRA_DIST+= wrapper/CSharp/wolfSSL_CSharp.sln +EXTRA_DIST+= wrapper/CSharp/user_settings.h +EXTRA_DIST+= wrapper/CSharp/wolfssl.vcxproj EXTRA_DIST+= wrapper/CSharp/wolfSSL_CSharp/Properties/AssemblyInfo.cs EXTRA_DIST+= wrapper/CSharp/wolfSSL_CSharp/Properties/Resources.Designer.cs EXTRA_DIST+= wrapper/CSharp/wolfSSL_CSharp/Properties/Resources.resx diff --git a/wrapper/CSharp/user_settings.h b/wrapper/CSharp/user_settings.h new file mode 100644 index 0000000000..a1d44aa5b2 --- /dev/null +++ b/wrapper/CSharp/user_settings.h @@ -0,0 +1,132 @@ +/* user_settings.h + * + * Copyright (C) 2006-2024 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* These are the build settings used by the Visual Studio CSharp wrapper */ + +#ifndef _WIN_CSHARP_USER_SETTINGS_H_ +#define _WIN_CSHARP_USER_SETTINGS_H_ + +/* Features */ +#define NO_OLD_TLS +#define WOLFSSL_TLS13 +#define WOLFSSL_DTLS +#define WOLFSSL_DTLS13 +#define WOLFSSL_SEND_HRR_COOKIE +#define WOLFSSL_DTLS_CID +#define HAVE_EXTENDED_MASTER +#define HAVE_SECURE_RENEGOTIATION +#define HAVE_SUPPORTED_CURVES +#define HAVE_TLS_EXTENSIONS +#define WOLFSSL_CERT_EXT +#define WOLFSSL_CERT_REQ +#define WOLFSSL_CERT_GEN +#define HAVE_ENCRYPT_THEN_MAC +#define HAVE_ECC_ENCRYPT +#define WOLFSSL_PUBLIC_MP +#define NO_MULTIBYTE_PRINT +#define WOLFSSL_KEY_GEN /* RSA key gen */ +#define WOLFSSL_ASN_TEMPLATE /* default */ +#if 0 + #define OPENSSL_EXTRA +#endif + +#define HAVE_CRL +#if 0 + /* start thread that can monitor CRL directory */ + #define HAVE_CRL_MONITOR +#endif + +/* Algorithms */ +#define HAVE_ED25519 +#define HAVE_CURVE25519 + +#define HAVE_AESGCM +#define WOLFSSL_AESGCM_STREAM +#define WOLFSSL_SHA384 +#define WOLFSSL_SHA512 + +#define HAVE_HKDF + +#undef NO_DH +#define HAVE_FFDHE_4096 + +#undef NO_RSA +#define WC_RSA_PSS +#define WOLFSSL_PSS_LONG_SALT +#define WC_RSA_BLINDING + +#define HAVE_ECC +#define ECC_SHAMIR +#define ECC_TIMING_RESISTANT +#define HAVE_COMP_KEY + +/* Disable features */ +#define NO_PSK + +/* Disable Algorithms */ +#define NO_DES3 +#define NO_DSA +#define NO_RC4 +#define NO_MD4 +#define NO_MD5 +#define NO_SHA + +/* Math */ + +/* Single Precision Support for RSA/DH 1024/2048/3072 and + * ECC P-256/P-384 */ +#define WOLFSSL_HAVE_SP_ECC +#define WOLFSSL_HAVE_SP_DH +#define WOLFSSL_HAVE_SP_RSA + +/* Optional Performance Speedups */ +#if 0 + #ifdef _WIN64 + /* Assembly speedups for SP math */ + #define WOLFSSL_SP_X86_64_ASM + + /* Support for RDSEED instruction */ + #define HAVE_INTEL_RDSEED + + /* AESNI on x64 */ + #define WOLFSSL_AESNI + + /* Intel ASM */ + #define USE_INTEL_SPEEDUP + #define WOLFSSL_X86_64_BUILD + + /* Old versions of MASM compiler do not recognize newer + * instructions. */ + #if 0 + #define NO_AVX2_SUPPORT + #define NO_MOVBE_SUPPORT + #endif + #endif +#endif + +/* Debug logging */ +#if 1 + #define DEBUG_WOLFSSL +#else + /* #define NO_ERROR_STRINGS */ +#endif + +#endif /* !_WIN_CSHARP_USER_SETTINGS_H_ */ diff --git a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs index c33e83cb44..b48f3cefb6 100755 --- a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs +++ b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs @@ -23,6 +23,7 @@ using System; using System.Linq; +using System.Text; using System.Security.Cryptography; using wolfSSL.CSharp; @@ -33,6 +34,8 @@ private static void random_test() int ret, i, zeroCount = 0; Byte[] data = new Byte[128]; + Console.WriteLine("\nStarting RNG test"); + /* Random Test */ ret = wolfcrypt.Random(data, data.Length); if (ret == 0) @@ -67,290 +70,260 @@ private static void ecc_test(string hashAlgorithm, int keySize) IntPtr PubKey = IntPtr.Zero; IntPtr key = IntPtr.Zero; - try - { - Console.WriteLine("Starting ECC tests..."); + Console.WriteLine("\nStarting ECC" + (keySize*8) + " test for " + hashAlgorithm + "..."); - /* Generate ECC Key Pair */ - Console.WriteLine("Testing ECC Key Generation..."); - key = wolfcrypt.EccMakeKey(keySize); - if (key == IntPtr.Zero) - { - throw new Exception("EccMakeKey failed"); - } - Console.WriteLine("ECC Key Generation test passed."); - - /* Export and Import Key */ - Console.WriteLine("Testing ECC Key Export and Import..."); - byte[] privateKeyDer; - ret = wolfcrypt.ExportPrivateKeyToDer(key, out privateKeyDer); - if (ret != 0) { - throw new Exception("ExportPrivateKeyToDer ailed"); - } - byte[] publicKeyDer; - ret = wolfcrypt.ExportPublicKeyToDer(key, out publicKeyDer, true); - if (ret != 0) { - throw new Exception("ExportPublicKeyToDer ailed"); - } - PrivKey = wolfcrypt.EccImportKey(privateKeyDer); - if (PrivKey == IntPtr.Zero) - { - throw new Exception("EccImportKey Private failed"); - } + /* Generate ECC Key Pair */ + Console.WriteLine("Testing ECC Key Generation..."); + key = wolfcrypt.EccMakeKey(keySize); + if (key == IntPtr.Zero) + { + throw new Exception("EccMakeKey failed"); + } + Console.WriteLine("ECC Key Generation test passed."); + + /* Export and Import Key */ + Console.WriteLine("Testing ECC Key Export and Import..."); + byte[] privateKeyDer; + ret = wolfcrypt.ExportPrivateKeyToDer(key, out privateKeyDer); + if (ret < 0) { + throw new Exception("ExportPrivateKeyToDer failed"); + } + byte[] publicKeyDer; + ret = wolfcrypt.ExportPublicKeyToDer(key, out publicKeyDer, true); + if (ret < 0) { + throw new Exception("ExportPublicKeyToDer failed"); + } + PrivKey = wolfcrypt.EccImportKey(privateKeyDer); + if (PrivKey == IntPtr.Zero) + { + throw new Exception("EccImportKey Private failed"); + } - PubKey = wolfcrypt.ImportPublicKeyFromDer(publicKeyDer); - if (PubKey == IntPtr.Zero) - { - throw new Exception("ImportPublicKeyFromDer Public failed"); - } + PubKey = wolfcrypt.ImportPublicKeyFromDer(publicKeyDer); + if (PubKey == IntPtr.Zero) + { + throw new Exception("ImportPublicKeyFromDer Public failed"); + } - Console.WriteLine("ECC Key Export and Import test passed."); + Console.WriteLine("ECC Key Export and Import test passed."); - /* Generate hash based on selected algorithm */ - byte[] dataToHash = System.Text.Encoding.UTF8.GetBytes("This is some data to hash"); - byte[] hash; + /* Generate hash based on selected algorithm */ + byte[] dataToHash = System.Text.Encoding.UTF8.GetBytes("This is some data to hash"); + byte[] hash; - switch (hashAlgorithm.ToUpper()) - { - case "SHA256": - using (SHA256 sha256 = SHA256.Create()) - { - hash = sha256.ComputeHash(dataToHash); - } - break; - - case "SHA384": - using (SHA384 sha384 = SHA384.Create()) - { - hash = sha384.ComputeHash(dataToHash); - } - break; - - case "SHA512": - using (SHA512 sha512 = SHA512.Create()) - { - hash = sha512.ComputeHash(dataToHash); - } - break; - - default: - throw new Exception("Unsupported hash algorithm"); - } + switch (hashAlgorithm.ToUpper()) + { + case "SHA256": + using (SHA256 sha256 = SHA256.Create()) + { + hash = sha256.ComputeHash(dataToHash); + } + break; - Console.WriteLine($"{hashAlgorithm} hash generated."); + case "SHA384": + using (SHA384 sha384 = SHA384.Create()) + { + hash = sha384.ComputeHash(dataToHash); + } + break; - /* Sign Data */ - Console.WriteLine("Testing ECC Signature Creation..."); - byte[] signature = new byte[wolfcrypt.ECC_MAX_SIG_SIZE]; - int signLength = wolfcrypt.EccSign(PrivKey, hash, signature); - if (signLength <= 0) - { - throw new Exception("EccSign failed"); - } + case "SHA512": + using (SHA512 sha512 = SHA512.Create()) + { + hash = sha512.ComputeHash(dataToHash); + } + break; - byte[] actualSignature = new byte[signLength]; - Array.Copy(signature, 0, actualSignature, 0, signLength); + default: + throw new Exception("Unsupported hash algorithm"); + } - Console.WriteLine($"ECC Signature Creation test passed. Signature Length: {signLength}"); + Console.WriteLine($"{hashAlgorithm} hash generated."); - /* Verify Signature */ - Console.WriteLine("Testing ECC Signature Verification..."); - int verifyResult = wolfcrypt.EccVerify(PubKey, actualSignature, hash); - if (verifyResult != 0) - { - throw new Exception("EccVerify failed"); - } - Console.WriteLine("ECC Signature Verification test passed."); - } - catch (Exception ex) + /* Sign Data */ + Console.WriteLine("Testing ECC Signature Creation..."); + byte[] signature = new byte[wolfcrypt.ECC_MAX_SIG_SIZE]; + int signLength = wolfcrypt.EccSign(PrivKey, hash, signature); + if (signLength <= 0) { - Console.WriteLine($"ECC test failed: {ex.Message}"); + throw new Exception("EccSign failed"); } - finally + + byte[] actualSignature = new byte[signLength]; + Array.Copy(signature, 0, actualSignature, 0, signLength); + + Console.WriteLine($"ECC Signature Creation test passed. Signature Length: {signLength}"); + + /* Verify Signature */ + Console.WriteLine("Testing ECC Signature Verification..."); + int verifyResult = wolfcrypt.EccVerify(PubKey, actualSignature, hash); + if (verifyResult != 0) { - /* Cleanup */ - if (key != IntPtr.Zero) wolfcrypt.EccFreeKey(key); - if (PubKey != IntPtr.Zero) wolfcrypt.EccFreeKey(PubKey); - if (PrivKey != IntPtr.Zero) wolfcrypt.EccFreeKey(PrivKey); + throw new Exception("EccVerify failed"); } - Console.WriteLine("ECC test completed successfully.\n"); + Console.WriteLine("ECC Signature Verification test passed."); + + /* Cleanup */ + if (key != IntPtr.Zero) wolfcrypt.EccFreeKey(key); + if (PubKey != IntPtr.Zero) wolfcrypt.EccFreeKey(PubKey); + if (PrivKey != IntPtr.Zero) wolfcrypt.EccFreeKey(PrivKey); } /* END ecc_test */ private static void rsa_test(string hashAlgorithm, int keySize) { IntPtr key = IntPtr.Zero; - try - { - Console.WriteLine("Starting RSA tests..."); + Console.WriteLine("\nStarting RSA tests..."); - IntPtr heap = IntPtr.Zero; - int devId = wolfcrypt.INVALID_DEVID; + IntPtr heap = IntPtr.Zero; + int devId = wolfcrypt.INVALID_DEVID; - /* Generate RSA Key Pair */ - Console.WriteLine("Testing RSA Key Generation..."); - key = wolfcrypt.RsaMakeKey(heap, devId, keySize); - if (key == IntPtr.Zero) - { - throw new Exception("RsaMakeKey failed"); - } - Console.WriteLine("RSA Key Generation test passed."); + /* Generate RSA Key Pair */ + Console.WriteLine("Testing RSA Key Generation..."); + key = wolfcrypt.RsaMakeKey(heap, devId, keySize); + if (key == IntPtr.Zero) + { + throw new Exception("RsaMakeKey failed"); + } + Console.WriteLine("RSA Key Generation test passed."); - /* Generate hash based on selected algorithm */ - byte[] dataToHash = System.Text.Encoding.UTF8.GetBytes("This is some data to hash"); - byte[] hash; + /* Generate hash based on selected algorithm */ + byte[] dataToHash = System.Text.Encoding.UTF8.GetBytes("This is some data to hash"); + byte[] hash; - switch (hashAlgorithm.ToUpper()) - { - case "SHA256": - using (SHA256 sha256 = SHA256.Create()) - { - hash = sha256.ComputeHash(dataToHash); - } - break; - - case "SHA384": - using (SHA384 sha384 = SHA384.Create()) - { - hash = sha384.ComputeHash(dataToHash); - } - break; - - case "SHA512": - using (SHA512 sha512 = SHA512.Create()) - { - hash = sha512.ComputeHash(dataToHash); - } - break; - - default: - throw new Exception("Unsupported hash algorithm"); - } + switch (hashAlgorithm.ToUpper()) + { + case "SHA256": + using (SHA256 sha256 = SHA256.Create()) + { + hash = sha256.ComputeHash(dataToHash); + } + break; - Console.WriteLine($"{hashAlgorithm} hash generated."); + case "SHA384": + using (SHA384 sha384 = SHA384.Create()) + { + hash = sha384.ComputeHash(dataToHash); + } + break; - /* Sign Data */ - Console.WriteLine("Testing RSA Signature Creation..."); - byte[] signature = new byte[keySize / 8]; - int signLength = wolfcrypt.RsaSignSSL(key, hash, signature); - if (signLength <= 0) - { - throw new Exception("RsaSignSSL failed"); - } + case "SHA512": + using (SHA512 sha512 = SHA512.Create()) + { + hash = sha512.ComputeHash(dataToHash); + } + break; - byte[] actualSignature = new byte[signLength]; - Array.Copy(signature, 0, actualSignature, 0, signLength); + default: + throw new Exception("Unsupported hash algorithm"); + } - Console.WriteLine($"RSA Signature Creation test passed. Signature Length: {signLength}"); + Console.WriteLine($"{hashAlgorithm} hash generated."); - /* Verify Signature */ - Console.WriteLine("Testing RSA Signature Verification..."); - int verifyResult = wolfcrypt.RsaVerifySSL(key, actualSignature, hash); - if (verifyResult != 0) - { - throw new Exception("RsaVerifySSL failed"); - } - Console.WriteLine("RSA Signature Verification test passed."); - } - catch (Exception ex) + /* Sign Data */ + Console.WriteLine("Testing RSA Signature Creation..."); + byte[] signature = new byte[keySize / 8]; + int signLength = wolfcrypt.RsaSignSSL(key, hash, signature); + if (signLength <= 0) { - Console.WriteLine($"RSA test failed: {ex.Message}"); + throw new Exception("RsaSignSSL failed"); } - finally + + byte[] actualSignature = new byte[signLength]; + Array.Copy(signature, 0, actualSignature, 0, signLength); + + Console.WriteLine($"RSA Signature Creation test passed. Signature Length: {signLength}"); + + /* Verify Signature */ + Console.WriteLine("Testing RSA Signature Verification..."); + int verifyResult = wolfcrypt.RsaVerifySSL(key, actualSignature, hash); + if (verifyResult != 0) { - /* Cleanup */ - if (key != IntPtr.Zero) wolfcrypt.RsaFreeKey(key); + throw new Exception("RsaVerifySSL failed"); } - Console.WriteLine("RSA test completed successfully.\n"); + Console.WriteLine("RSA Signature Verification test passed."); + + /* Cleanup */ + if (key != IntPtr.Zero) wolfcrypt.RsaFreeKey(key); } /* END rsa_test */ private static void ed25519_test() { + int ret; IntPtr key = IntPtr.Zero; byte[] privKey; byte[] pubKey; - int ret; - try - { - Console.WriteLine("Starting ED25519 tests..."); + Console.WriteLine("\nStarting ED25519 tests..."); - IntPtr heap = IntPtr.Zero; - int devId = wolfcrypt.INVALID_DEVID; + IntPtr heap = IntPtr.Zero; + int devId = wolfcrypt.INVALID_DEVID; - /* Generate ED25519 Key Pair */ - Console.WriteLine("Testing ED25519 Key Generation..."); - key = wolfcrypt.Ed25519MakeKey(heap, devId); - if (key == IntPtr.Zero) - { - throw new Exception("Ed25519MakeKey failed"); - } - - Console.WriteLine("ED25519 Key Generation test passed."); - - /* Export and Import Key */ - Console.WriteLine("Testing ED25519 Key Export and Import..."); - /* Export Private */ - ret = wolfcrypt.Ed25519ExportKeyToDer(key, out privKey); - if (ret != 0 || privKey == null) - { - throw new Exception("Ed25519ExportKeyToDer failed"); - } - /* Export Public */ - ret =wolfcrypt.Ed25519ExportPublicKeyToDer(key, out pubKey, true); - if (ret != 0 || pubKey == null) - { - throw new Exception("Ed25519ExportKeyToDer failed"); - } - /* Import Private */ - IntPtr importedPrivKey = wolfcrypt.Ed25519PrivateKeyDecode(privKey); - if (importedPrivKey == IntPtr.Zero) - { - throw new Exception("Ed25519PrivateKeyDecode failed"); - } - /* Import Public */ - IntPtr importedPubKey = wolfcrypt.Ed25519PublicKeyDecode(pubKey); - if (importedPubKey == IntPtr.Zero) - { - throw new Exception("Ed25519PublicKeyDecode failed"); - } + /* Generate ED25519 Key Pair */ + Console.WriteLine("Testing ED25519 Key Generation..."); + key = wolfcrypt.Ed25519MakeKey(heap, devId); + if (key == IntPtr.Zero) + { + throw new Exception("Ed25519MakeKey failed"); + } - Console.WriteLine("ED25519 Key Export and Import test passed."); + Console.WriteLine("ED25519 Key Generation test passed."); - /* Generate a hash */ - byte[] dataToHash = System.Text.Encoding.UTF8.GetBytes("This is some data to hash"); + /* Export and Import Key */ + Console.WriteLine("Testing ED25519 Key Export and Import..."); + /* Export Private */ + ret = wolfcrypt.Ed25519ExportKeyToDer(key, out privKey); + if (ret != 0 || privKey == null) + { + throw new Exception("Ed25519ExportKeyToDer failed"); + } + /* Export Public */ + ret =wolfcrypt.Ed25519ExportPublicKeyToDer(key, out pubKey, true); + if (ret != 0 || pubKey == null) + { + throw new Exception("Ed25519ExportKeyToDer failed"); + } + /* Import Private */ + IntPtr importedPrivKey = wolfcrypt.Ed25519PrivateKeyDecode(privKey); + if (importedPrivKey == IntPtr.Zero) + { + throw new Exception("Ed25519PrivateKeyDecode failed"); + } + /* Import Public */ + IntPtr importedPubKey = wolfcrypt.Ed25519PublicKeyDecode(pubKey); + if (importedPubKey == IntPtr.Zero) + { + throw new Exception("Ed25519PublicKeyDecode failed"); + } - /* Sign Data */ - Console.WriteLine("Testing ED25519 Signature Creation..."); - byte[] signature; + Console.WriteLine("ED25519 Key Export and Import test passed."); - ret = wolfcrypt.Ed25519SignMsg(dataToHash, out signature, key); - if (ret != 0) - { - throw new Exception("Ed25519SignMsg failed"); - } + /* Generate a hash */ + byte[] dataToHash = System.Text.Encoding.UTF8.GetBytes("This is some data to hash"); - Console.WriteLine($"ED25519 Signature Creation test passed. Signature Length: {signature.Length}"); + /* Sign Data */ + Console.WriteLine("Testing ED25519 Signature Creation..."); + byte[] signature; - /* Verify Signature */ - Console.WriteLine("Testing ED25519 Signature Verification..."); - ret = wolfcrypt.Ed25519VerifyMsg(signature, dataToHash, key); - if (ret != 0) - { - throw new Exception("Ed25519VerifyMsg failed"); - } - Console.WriteLine("ED25519 Signature Verification test passed."); - } - catch (Exception ex) + ret = wolfcrypt.Ed25519SignMsg(dataToHash, out signature, key); + if (ret != 0) { - Console.WriteLine($"ED25519 test failed: {ex.Message}"); + throw new Exception("Ed25519SignMsg failed"); } - finally + + Console.WriteLine($"ED25519 Signature Creation test passed. Signature Length: {signature.Length}"); + + /* Verify Signature */ + Console.WriteLine("Testing ED25519 Signature Verification..."); + ret = wolfcrypt.Ed25519VerifyMsg(signature, dataToHash, key); + if (ret != 0) { - /* Cleanup */ - if (key != IntPtr.Zero) wolfcrypt.Ed25519FreeKey(key); + throw new Exception("Ed25519VerifyMsg failed"); } - Console.WriteLine("ED25519 test completed successfully.\n"); + Console.WriteLine("ED25519 Signature Verification test passed."); + + /* Cleanup */ + if (key != IntPtr.Zero) wolfcrypt.Ed25519FreeKey(key); } /* END ed25519_test */ private static void curve25519_test() @@ -362,106 +335,101 @@ private static void curve25519_test() IntPtr publicKeyB = IntPtr.Zero; byte[] derKey; - try - { - Console.WriteLine("Starting Curve25519 test..."); - - /* Generate Key Pair A */ - Console.WriteLine("Generating Key Pair A..."); - keyA = wolfcrypt.Curve25519MakeKey(IntPtr.Zero, 0); - if (keyA == IntPtr.Zero) - { - throw new Exception("Failed to generate key pair A."); - } + Console.WriteLine("\nStarting Curve25519 test..."); - /* Generate Key Pair B */ - Console.WriteLine("Generating Key Pair B..."); - keyB = wolfcrypt.Curve25519MakeKey(IntPtr.Zero, 0); - if (keyB == IntPtr.Zero) - { - throw new Exception("Failed to generate key pair B."); - } - Console.WriteLine("Curve25519 Key generation test passed."); + /* Generate Key Pair A */ + Console.WriteLine("Generating Key Pair A..."); + keyA = wolfcrypt.Curve25519MakeKey(IntPtr.Zero, 0); + if (keyA == IntPtr.Zero) + { + throw new Exception("Failed to generate key pair A."); + } - /* 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) - { - throw new Exception("Curve25519ExportPublicKeyToDer failed"); - } + /* Generate Key Pair B */ + Console.WriteLine("Generating Key Pair B..."); + keyB = wolfcrypt.Curve25519MakeKey(IntPtr.Zero, 0); + if (keyB == IntPtr.Zero) + { + throw new Exception("Failed to generate key pair B."); + } + Console.WriteLine("Curve25519 Key generation test passed."); - /* Decode Public Key B from DER format */ - Console.WriteLine("Decoding Public Key B from DER format..."); - publicKeyB = wolfcrypt.Curve25519PublicKeyDecode(derKey); - if (publicKeyB == IntPtr.Zero) - { - throw new Exception("Failed to decode public key B from DER format."); - } - Console.WriteLine("Curve25519 Export and Import test passed."); + /* 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) + { + throw new Exception("Curve25519ExportPublicKeyToDer failed"); + } - /* Compute Shared Secret using Private Key A and Public Key B */ - Console.WriteLine("Computing Shared Secret using Private Key A and Public Key B..."); - byte[] sharedSecretA = new byte[wolfcrypt.ED25519_KEY_SIZE]; - int retA = wolfcrypt.Curve25519SharedSecret(keyA, publicKeyB, sharedSecretA); - if (retA != 0) - { - throw new Exception("Failed to compute shared secret A. Error code: " + retA); - } - Console.WriteLine("Curve25519 shared secret created using private Key A."); + /* Decode Public Key B from DER format */ + Console.WriteLine("Decoding Public Key B from DER format..."); + publicKeyB = wolfcrypt.Curve25519PublicKeyDecode(derKey); + if (publicKeyB == IntPtr.Zero) + { + throw new Exception("Failed to decode public key B from DER format."); + } + Console.WriteLine("Curve25519 Export and Import test passed."); - /* 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) - { - throw new Exception("Curve25519ExportPublicKeyToDer failed"); - } + /* Compute Shared Secret using Private Key A and Public Key B */ + Console.WriteLine("Computing Shared Secret using Private Key A and Public Key B..."); + byte[] sharedSecretA = new byte[wolfcrypt.ED25519_KEY_SIZE]; + int retA = wolfcrypt.Curve25519SharedSecret(keyA, publicKeyB, sharedSecretA); + if (retA != 0) + { + throw new Exception("Failed to compute shared secret A. Error code: " + retA); + } + Console.WriteLine("Curve25519 shared secret created using private Key A."); - /* Decode Public Key A from DER format */ - Console.WriteLine("Decoding Public Key A from DER format..."); - publicKeyA = wolfcrypt.Curve25519PublicKeyDecode(derKey); - if (publicKeyA == IntPtr.Zero) - { - throw new Exception("Failed to decode public key A from DER format."); - } + /* 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) + { + throw new Exception("Curve25519ExportPublicKeyToDer failed"); + } - /* Compute Shared Secret using Private Key B and Public Key A */ - Console.WriteLine("Computing Shared Secret using Private Key B and Public Key A..."); - byte[] sharedSecretB = new byte[wolfcrypt.ED25519_KEY_SIZE]; - int retB = wolfcrypt.Curve25519SharedSecret(keyB, publicKeyA, sharedSecretB); - if (retB != 0) - { - throw new Exception("Failed to compute shared secret B. Error code: " + retB); - } - Console.WriteLine("Curve25519 shared secret created using private Key B."); + /* Decode Public Key A from DER format */ + Console.WriteLine("Decoding Public Key A from DER format..."); + publicKeyA = wolfcrypt.Curve25519PublicKeyDecode(derKey); + if (publicKeyA == IntPtr.Zero) + { + throw new Exception("Failed to decode public key A from DER format."); + } - /* Compare Shared Secrets */ - Console.WriteLine("Comparing Shared Secrets..."); - if (!wolfcrypt.ByteArrayVerify(sharedSecretA, sharedSecretB)) - { - throw new Exception("Shared secrets do not match."); - } - else - { - Console.WriteLine("Curve25519 shared secret match."); - } + /* Compute Shared Secret using Private Key B and Public Key A */ + Console.WriteLine("Computing Shared Secret using Private Key B and Public Key A..."); + byte[] sharedSecretB = new byte[wolfcrypt.ED25519_KEY_SIZE]; + int retB = wolfcrypt.Curve25519SharedSecret(keyB, publicKeyA, sharedSecretB); + if (retB != 0) + { + throw new Exception("Failed to compute shared secret B. Error code: " + retB); } - catch (Exception ex) + Console.WriteLine("Curve25519 shared secret created using private Key B."); + + /* Compare Shared Secrets */ + Console.WriteLine("Comparing Shared Secrets..."); + if (!wolfcrypt.ByteArrayVerify(sharedSecretA, sharedSecretB)) { - Console.WriteLine($"Curve25519 test failed: {ex.Message}"); + throw new Exception("Shared secrets do not match."); } - finally + else { - /* Cleanup */ - if (keyA != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(keyA); - if (keyB != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(keyB); - if (publicKeyA != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(publicKeyA); - if (publicKeyB != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(publicKeyB); + Console.WriteLine("Curve25519 shared secret match."); } - Console.WriteLine("Curve25519 test completed successfully.\n"); + + /* Cleanup */ + if (keyA != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(keyA); + if (keyB != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(keyB); + if (publicKeyA != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(publicKeyA); + if (publicKeyB != IntPtr.Zero) wolfcrypt.Curve25519FreeKey(publicKeyB); } /* END curve25519_test */ + public static void standard_log(int lvl, StringBuilder msg) + { + Console.WriteLine(msg); + } + public static void Main(string[] args) { try @@ -470,6 +438,9 @@ public static void Main(string[] args) wolfcrypt.Init(); + /* setup logging to stdout */ + wolfcrypt.SetLogging(standard_log); + random_test(); ecc_test("SHA256", 32); /* Uses SHA-256 (32 byte hash) */ diff --git a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.csproj b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.csproj index ba166edc22..647d7ce7bd 100755 --- a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.csproj +++ b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.csproj @@ -32,7 +32,7 @@ true full false - ..\DLL Debug\ + $(SolutionDir)$(Configuration)\$(Platform)\ DEBUG;TRACE prompt 3 @@ -41,7 +41,7 @@ AnyCPU pdbonly true - ..\DLL Release\ + $(SolutionDir)$(Configuration)\$(Platform)\ TRACE prompt 4 @@ -51,7 +51,7 @@ true - ..\x64\DLL Debug\ + $(SolutionDir)$(Configuration)\$(Platform)\ DEBUG;TRACE 4 full @@ -61,7 +61,7 @@ true - ..\x64\DLL Release\ + $(SolutionDir)$(Configuration)\$(Platform)\ TRACE true pdbonly @@ -113,7 +113,7 @@ -