Skip to content

Commit

Permalink
Refactor / cleanup hash test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan Garske committed Sep 5, 2024
1 parent 28dad8e commit 6988bcb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 73 deletions.
1 change: 1 addition & 0 deletions wrapper/CSharp/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 18 additions & 17 deletions wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
127 changes: 71 additions & 56 deletions wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);


/********************************
Expand Down Expand Up @@ -1729,8 +1702,8 @@ public static void Curve25519FreeKey(IntPtr key)


/***********************************************************************
* RAW Curve25519
**********************************************************************/
* RAW Curve25519
**********************************************************************/

/// <summary>
/// Generate a shared secret using Curve25519
Expand Down Expand Up @@ -1906,7 +1879,6 @@ public static IntPtr AesNew(IntPtr heap, int devId)
/// </summary>
/// <param name="aes">AES-GCM context pointer.</param>
/// <param name="key">The AES key (either 128, 192, or 256 bits).</param>
/// <param name="len">The size of the AES key in bytes (16, 24, or 32).</param>
/// <returns>0 on success, otherwise an error code.</returns>
public static int AesGcmSetKey(IntPtr aes, byte[] key)
{
Expand Down Expand Up @@ -2123,16 +2095,18 @@ public static void AesGcmFree(IntPtr aes)
/// <summary>
/// Allocate and set up a new hash context with proper error handling
/// </summary>
/// <param name="hashType">The type of hash (SHA-256, SHA-384, etc.)</param>
/// <param name="heap">Pointer to the heap for memory allocation (use IntPtr.Zero if not applicable)</param>
/// <param name="devId">Device ID (if applicable, otherwise use INVALID_DEVID)</param>
/// <returns>Allocated hash context pointer or IntPtr.Zero on failure</returns>
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.");
Expand All @@ -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
/// </summary>
/// <param name="hash">Hash context pointer</param>
/// <param name="hashType">The type of hash (SHA-256, SHA-384, etc.)</param>
/// <returns>0 on success, otherwise an error code</returns>
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)
{
Expand All @@ -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;
}
Expand All @@ -2177,21 +2153,27 @@ public static int InitHash(IntPtr hash, wc_HashType hashType)
/// Update the hash with data
/// </summary>
/// <param name="hash">Hash context pointer</param>
/// <param name="hashType">The type of hash</param>
/// <param name="data">Byte array of the data to hash</param>
/// <returns>0 on success, otherwise an error code</returns>
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)
{
Expand All @@ -2204,6 +2186,7 @@ public static int HashUpdate(IntPtr hash, wc_HashType hashType, byte[] data)
}
finally
{
/* Cleanup */
if (dataPtr != IntPtr.Zero) Marshal.FreeHGlobal(dataPtr);
}

Expand All @@ -2214,20 +2197,28 @@ public static int HashUpdate(IntPtr hash, wc_HashType hashType, byte[] data)
/// Finalize the hash and output the result
/// </summary>
/// <param name="hash">Hash context pointer</param>
/// <param name="hashType">The type of hash</param>
/// <param name="output">Byte array where the hash output will be stored</param>
/// <returns>0 on success, otherwise an error code</returns>
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);
Expand All @@ -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;
}


/// <summary>
/// Free the allocated hash context with proper error handling
/// </summary>
/// <param name="hash">Hash context pointer to be freed</param>
/// <param name="hashType">The type of hash</param>
/// <returns>0 on success, otherwise an error code</returns>
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)
{
Expand All @@ -2279,6 +2271,29 @@ public static int HashFree(IntPtr hash, wc_HashType hashType)
}
return ret;
}

/// <summary>
/// Hash type enum values
/// </summary>
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 */


Expand Down

0 comments on commit 6988bcb

Please sign in to comment.