diff --git a/wrapper/CSharp/user_settings.h b/wrapper/CSharp/user_settings.h
index a1d44aa5b2..c98d84cae3 100644
--- a/wrapper/CSharp/user_settings.h
+++ b/wrapper/CSharp/user_settings.h
@@ -44,6 +44,7 @@
#define NO_MULTIBYTE_PRINT
#define WOLFSSL_KEY_GEN /* RSA key gen */
#define WOLFSSL_ASN_TEMPLATE /* default */
+#define WOLFSSL_SHA3
#if 0
#define OPENSSL_EXTRA
#endif
diff --git a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
index c1c6a5fec3..c96f8ee862 100755
--- a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
+++ b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
@@ -560,20 +560,23 @@ private static void aes_gcm_test()
}
} /* END aes_gcm_test */
- private static void hash_test(wolfcrypt.wc_HashType hashType)
+ private static void hash_test(uint hashType)
{
IntPtr hash = IntPtr.Zero;
IntPtr heap = IntPtr.Zero;
int devId = wolfcrypt.INVALID_DEVID;
- Console.WriteLine($"\nStarting hash test for {hashType}...");
+ /* Get the enum name */
+ string hashTypeName = Enum.GetName(typeof(wolfcrypt.hashType), hashType);
+
+ Console.WriteLine($"\nStarting hash test for {hashTypeName}...");
/* Allocate new hash context */
Console.WriteLine("Testing hash context allocation...");
- hash = wolfcrypt.HashNew(heap, devId);
+ hash = wolfcrypt.HashNew(hashType, heap, devId);
if (hash == IntPtr.Zero)
{
- Console.WriteLine($"HashNew failed for {hashType}");
+ Console.WriteLine($"HashNew failed for {hashTypeName}");
return;
}
Console.WriteLine("Hash context allocation test passed.");
@@ -583,7 +586,7 @@ private static void hash_test(wolfcrypt.wc_HashType hashType)
int initResult = wolfcrypt.InitHash(hash, hashType);
if (initResult != 0)
{
- Console.WriteLine($"InitHash failed for {hashType}");
+ Console.WriteLine($"InitHash failed for {hashTypeName}");
wolfcrypt.HashFree(hash, hashType);
return;
}
@@ -595,7 +598,7 @@ private static void hash_test(wolfcrypt.wc_HashType hashType)
int updateResult = wolfcrypt.HashUpdate(hash, hashType, dataToHash);
if (updateResult != 0)
{
- Console.WriteLine($"HashUpdate failed for {hashType}");
+ Console.WriteLine($"HashUpdate failed for {hashTypeName}");
wolfcrypt.HashFree(hash, hashType);
return;
}
@@ -612,26 +615,23 @@ private static void hash_test(wolfcrypt.wc_HashType hashType)
return;
}
- Console.WriteLine($"Hash finalization test passed for {hashType}. Hash Length: {hashOutput.Length}");
+ Console.WriteLine($"Hash finalization test passed for {hashTypeName}. Hash Length: {hashOutput.Length}");
/* Output the hash result */
- Console.WriteLine($"Hash Output ({hashType}): {BitConverter.ToString(hashOutput).Replace("-", "")}");
+ Console.WriteLine($"Hash Output ({hashTypeName}): {BitConverter.ToString(hashOutput).Replace("-", "")}");
/* Cleanup */
Console.WriteLine("Testing hash cleanup...");
int freeResult = wolfcrypt.HashFree(hash, hashType);
if (freeResult != 0)
{
- Console.WriteLine($"HashFree failed for {hashType}");
+ Console.WriteLine($"HashFree failed for {hashTypeName}");
}
else
{
Console.WriteLine("Hash cleanup test passed.");
}
- }
-
-
-
+ } /* END hash_test */
public static void standard_log(int lvl, StringBuilder msg)
{
@@ -665,13 +665,14 @@ public static void Main(string[] args)
aes_gcm_test(); /* AES_GCM test */
- hash_test(wolfcrypt.wc_HashType.WC_HASH_TYPE_SHA256); /* HASH test */
- hash_test(wolfcrypt.wc_HashType.WC_HASH_TYPE_SHA512); /* HASH test */
- hash_test(wolfcrypt.wc_HashType.WC_HASH_TYPE_SHA3_256); /* HASH test */
+ hash_test((uint)wolfcrypt.hashType.WC_HASH_TYPE_SHA256); /* SHA-256 HASH test */
+ hash_test((uint)wolfcrypt.hashType.WC_HASH_TYPE_SHA384); /* SHA-384 HASH test */
+ hash_test((uint)wolfcrypt.hashType.WC_HASH_TYPE_SHA512); /* SHA-512 HASH test */
+ hash_test((uint)wolfcrypt.hashType.WC_HASH_TYPE_SHA3_256); /* SHA3_256 HASH test */
wolfcrypt.Cleanup();
- Console.WriteLine("All tests completed successfully.\n");
+ Console.WriteLine("\nAll tests completed successfully");
}
catch (Exception ex)
{
diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs
index 98f45b7a3d..bb8547499e 100755
--- a/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs
+++ b/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs
@@ -220,44 +220,17 @@ public class wolfcrypt
* HASH
*/
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
- private extern static IntPtr wc_HashNew(IntPtr heap, int devId);
+ private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
- private extern static int wc_HashInit(IntPtr hash, wc_HashType type);
+ private extern static int wc_HashInit(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
- private extern static int wc_HashUpdate(IntPtr hash, wc_HashType type, IntPtr data, uint dataSz);
+ private extern static int wc_HashUpdate(IntPtr hash, uint hashType, IntPtr data, uint dataSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
- private extern static int wc_HashFinal(IntPtr hash, wc_HashType type, IntPtr output);
+ private extern static int wc_HashFinal(IntPtr hash, uint hashType, IntPtr output);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
- private extern static int wc_HashFree(IntPtr hash, wc_HashType type);
+ private extern static int wc_HashFree(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
- private extern static int wc_HashGetDigestSize(wc_HashType hash_type);
-
- /* HASH type enum values */
- public enum wc_HashType
- {
- WC_HASH_TYPE_NONE = 15,
- WC_HASH_TYPE_MD2 = 16,
- WC_HASH_TYPE_MD4 = 17,
- WC_HASH_TYPE_MD5 = 0,
- WC_HASH_TYPE_SHA = 1, /* SHA-1 (not old SHA-0) */
- WC_HASH_TYPE_SHA224 = 8,
- WC_HASH_TYPE_SHA256 = 2,
- WC_HASH_TYPE_SHA384 = 5,
- WC_HASH_TYPE_SHA512 = 4,
- WC_HASH_TYPE_MD5_SHA = 18,
- WC_HASH_TYPE_SHA3_224 = 10,
- WC_HASH_TYPE_SHA3_256 = 11,
- WC_HASH_TYPE_SHA3_384 = 12,
- WC_HASH_TYPE_SHA3_512 = 13,
- WC_HASH_TYPE_BLAKE2B = 14,
- WC_HASH_TYPE_BLAKE2S = 19,
- WC_HASH_TYPE_MAX = WC_HASH_TYPE_BLAKE2S,
- }
-
-
-
- /* Specifically need SHA2-256, SHA2-384 and SHA3: */
- /* wc_HashInit, wc_HashUpdate, wc_HashFinal, wc_HashFree */
+ private extern static int wc_HashGetDigestSize(uint hashType);
/********************************
@@ -1729,8 +1702,8 @@ public static void Curve25519FreeKey(IntPtr key)
/***********************************************************************
- * RAW Curve25519
- **********************************************************************/
+ * RAW Curve25519
+ **********************************************************************/
///
/// Generate a shared secret using Curve25519
@@ -1906,7 +1879,6 @@ public static IntPtr AesNew(IntPtr heap, int devId)
///
/// AES-GCM context pointer.
/// The AES key (either 128, 192, or 256 bits).
- /// The size of the AES key in bytes (16, 24, or 32).
/// 0 on success, otherwise an error code.
public static int AesGcmSetKey(IntPtr aes, byte[] key)
{
@@ -2123,16 +2095,18 @@ public static void AesGcmFree(IntPtr aes)
///
/// Allocate and set up a new hash context with proper error handling
///
+ /// The type of hash (SHA-256, SHA-384, etc.)
/// Pointer to the heap for memory allocation (use IntPtr.Zero if not applicable)
/// Device ID (if applicable, otherwise use INVALID_DEVID)
/// Allocated hash context pointer or IntPtr.Zero on failure
- public static IntPtr HashNew(IntPtr heap, int devId)
+ public static IntPtr HashNew(uint hashType, IntPtr heap, int devId)
{
IntPtr hash = IntPtr.Zero;
try
{
- hash = wc_HashNew(heap, devId);
+ /* Allocate new hash */
+ hash = wc_HashNew(hashType, heap, devId);
if (hash == IntPtr.Zero)
{
throw new Exception("Failed to allocate new hash context.");
@@ -2149,13 +2123,17 @@ public static IntPtr HashNew(IntPtr heap, int devId)
/// Initialize the hash context for a specific hash type with proper error handling
///
/// Hash context pointer
+ /// The type of hash (SHA-256, SHA-384, etc.)
/// 0 on success, otherwise an error code
- public static int InitHash(IntPtr hash, wc_HashType hashType)
+ public static int InitHash(IntPtr hash, uint hashType)
{
int ret = -1;
try
{
- if (hash == IntPtr.Zero) throw new Exception("Hash context is null.");
+ /* Check hash */
+ if (hash == IntPtr.Zero)
+ throw new Exception("Hash context is null.");
+
ret = wc_HashInit(hash, hashType);
if (ret != 0)
{
@@ -2164,11 +2142,9 @@ public static int InitHash(IntPtr hash, wc_HashType hashType)
}
catch (Exception e)
{
+ /* Cleanup */
log(ERROR_LOG, "InitHash Exception: " + e.ToString());
- if (hash != IntPtr.Zero)
- {
- wc_HashFree(hash, hashType);
- }
+ if (hash != IntPtr.Zero) wc_HashFree(hash, hashType);
}
return ret;
}
@@ -2177,21 +2153,27 @@ public static int InitHash(IntPtr hash, wc_HashType hashType)
/// Update the hash with data
///
/// Hash context pointer
+ /// The type of hash
/// Byte array of the data to hash
/// 0 on success, otherwise an error code
- public static int HashUpdate(IntPtr hash, wc_HashType hashType, byte[] data)
+ public static int HashUpdate(IntPtr hash, uint hashType, byte[] data)
{
int ret = -1;
IntPtr dataPtr = IntPtr.Zero;
try
{
- if (hash == IntPtr.Zero) throw new Exception("Hash context is null.");
- if (data == null || data.Length == 0) throw new Exception("Invalid data array.");
+ /* Check parameters */
+ if (hash == IntPtr.Zero)
+ throw new Exception("Hash context is null.");
+ if (data == null || data.Length == 0)
+ throw new Exception("Invalid data array.");
+ /* Allocate memory */
dataPtr = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, dataPtr, data.Length);
+ /* Update hash */
ret = wc_HashUpdate(hash, hashType, dataPtr, (uint)data.Length);
if (ret != 0)
{
@@ -2204,6 +2186,7 @@ public static int HashUpdate(IntPtr hash, wc_HashType hashType, byte[] data)
}
finally
{
+ /* Cleanup */
if (dataPtr != IntPtr.Zero) Marshal.FreeHGlobal(dataPtr);
}
@@ -2214,20 +2197,28 @@ public static int HashUpdate(IntPtr hash, wc_HashType hashType, byte[] data)
/// Finalize the hash and output the result
///
/// Hash context pointer
+ /// The type of hash
/// Byte array where the hash output will be stored
/// 0 on success, otherwise an error code
- public static int HashFinal(IntPtr hash, wc_HashType hashType, out byte[] output)
+ public static int HashFinal(IntPtr hash, uint hashType, out byte[] output)
{
int ret = -1;
IntPtr outputPtr = IntPtr.Zero;
- int hashSize = wc_HashGetDigestSize(hashType);
- output = new byte[hashSize];
try
{
- if (hash == IntPtr.Zero) throw new Exception("Hash context is null.");
- if (hashSize <= 0) throw new Exception("Invalid hash size.");
+ /* Get hash size and initialize */
+ int hashSize = wc_HashGetDigestSize(hashType);
+ output = new byte[hashSize];
+
+ /* Check hash */
+ if (hash == IntPtr.Zero)
+ throw new Exception("Hash context is null.");
+ if (hashSize <= 0)
+ throw new Exception("Invalid hash size.");
+
+ /* Allocate memory */
outputPtr = Marshal.AllocHGlobal(hashSize);
ret = wc_HashFinal(hash, hashType, outputPtr);
@@ -2245,28 +2236,29 @@ public static int HashFinal(IntPtr hash, wc_HashType hashType, out byte[] output
}
finally
{
+ /* Cleanup */
if (outputPtr != IntPtr.Zero) Marshal.FreeHGlobal(outputPtr);
}
return ret;
}
-
///
/// Free the allocated hash context with proper error handling
///
/// Hash context pointer to be freed
+ /// The type of hash
/// 0 on success, otherwise an error code
- public static int HashFree(IntPtr hash, wc_HashType hashType)
+ public static int HashFree(IntPtr hash, uint hashType)
{
int ret = -1;
try
{
+ /* Check hash */
if (hash == IntPtr.Zero)
- {
throw new Exception("Hash context is null, cannot free.");
- }
+ /* Free hash */
ret = wc_HashFree(hash, hashType);
if (ret != 0)
{
@@ -2279,6 +2271,29 @@ public static int HashFree(IntPtr hash, wc_HashType hashType)
}
return ret;
}
+
+ ///
+ /// Hash type enum values
+ ///
+ public enum hashType
+ {
+ WC_HASH_TYPE_NONE = 0,
+ WC_HASH_TYPE_MD2 = 1,
+ WC_HASH_TYPE_MD4 = 2,
+ WC_HASH_TYPE_MD5 = 3,
+ WC_HASH_TYPE_SHA = 4, /* SHA-1 (not old SHA-0) */
+ WC_HASH_TYPE_SHA224 = 5,
+ WC_HASH_TYPE_SHA256 = 6,
+ WC_HASH_TYPE_SHA384 = 7,
+ WC_HASH_TYPE_SHA512 = 8,
+ WC_HASH_TYPE_MD5_SHA = 9,
+ WC_HASH_TYPE_SHA3_224 = 10,
+ WC_HASH_TYPE_SHA3_256 = 11,
+ WC_HASH_TYPE_SHA3_384 = 12,
+ WC_HASH_TYPE_SHA3_512 = 13,
+ WC_HASH_TYPE_BLAKE2B = 14,
+ WC_HASH_TYPE_BLAKE2S = 15,
+ }
/* END HASH */