Skip to content

Commit

Permalink
'wc_HashNew' and test case for hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan Garske committed Sep 4, 2024
1 parent d9513eb commit 28dad8e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
31 changes: 31 additions & 0 deletions wolfcrypt/src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,22 @@ int wc_Hash(enum wc_HashType hash_type, const byte* data,
NULL, INVALID_DEVID);
}

wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId)
{
wc_HashAlg* hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), heap,
DYNAMIC_TYPE_HASHES);
if (hash != NULL) {
if (wc_HashInit_ex(hash, type, heap, devId) != 0) {
XFREE(hash, heap, DYNAMIC_TYPE_HASHES);
hash = NULL;
}
else {
hash->isAllocated = 1;
}
}
return hash;
}

int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
int devId)
{
Expand Down Expand Up @@ -1008,43 +1024,53 @@ int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out)
int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
{
int ret = WC_NO_ERR_TRACE(HASH_TYPE_E); /* Default to hash type error */
int isAllocated = 0;
void* heap = NULL;

if (hash == NULL)
return BAD_FUNC_ARG;

isAllocated = hash->isAllocated;

switch (type) {
case WC_HASH_TYPE_MD5:
#ifndef NO_MD5
heap = hash->md5.heap;
wc_Md5Free(&hash->md5);
ret = 0;
#endif
break;
case WC_HASH_TYPE_SHA:
#ifndef NO_SHA
heap = hash->sha.heap;
wc_ShaFree(&hash->sha);
ret = 0;
#endif
break;
case WC_HASH_TYPE_SHA224:
#ifdef WOLFSSL_SHA224
heap = hash->sha224.heap;
wc_Sha224Free(&hash->sha224);
ret = 0;
#endif
break;
case WC_HASH_TYPE_SHA256:
#ifndef NO_SHA256
heap = hash->sha256.heap;
wc_Sha256Free(&hash->sha256);
ret = 0;
#endif
break;
case WC_HASH_TYPE_SHA384:
#ifdef WOLFSSL_SHA384
heap = hash->sha384.heap;
wc_Sha384Free(&hash->sha384);
ret = 0;
#endif
break;
case WC_HASH_TYPE_SHA512:
#ifdef WOLFSSL_SHA512
heap = hash->sha512.heap;
wc_Sha512Free(&hash->sha512);
ret = 0;
#endif
Expand All @@ -1071,6 +1097,7 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
#endif
case WC_HASH_TYPE_SHA3_224:
#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
heap = hash->sha3.heap;
wc_Sha3_224_Free(&hash->sha3);
ret = 0;
#endif
Expand All @@ -1096,6 +1123,7 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)

#ifdef WOLFSSL_SM3
case WC_HASH_TYPE_SM3:
heap = hash->sm3.heap;
wc_Sm3Free(&hash->sm3);
ret = 0;
break;
Expand All @@ -1118,6 +1146,9 @@ int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
ret = BAD_FUNC_ARG;
};

if (isAllocated && heap)
XFREE(hash, heap, DYNAMIC_TYPE_HASHES);

return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion wolfcrypt/src/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ RsaKey* wc_NewRsaKey(void* heap, int devId)

int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
{
int ret = 0;
int ret = 0;

if (key == NULL) {
return BAD_FUNC_ARG;
Expand Down
2 changes: 2 additions & 0 deletions wolfssl/wolfcrypt/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ typedef union {
#ifdef WOLFSSL_SM3
wc_Sm3 sm3;
#endif
unsigned int isAllocated:1; /* flag indicates if structure was allocated */
} wc_HashAlg;
#endif /* !NO_HASH_WRAPPER */

Expand Down Expand Up @@ -175,6 +176,7 @@ WOLFSSL_API int wc_Hash_ex(enum wc_HashType hash_type,
byte* hash, word32 hash_len, void* heap, int devId);

/* generic hash operation wrappers */
WOLFSSL_API wc_HashAlg* wc_HashNew(enum wc_HashType type, void* heap, int devId);
WOLFSSL_API int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type,
void* heap, int devId);
WOLFSSL_API int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type);
Expand Down
83 changes: 80 additions & 3 deletions wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private static void curve25519_test()
}
else
{
Console.WriteLine("Curve25519 shared secret match.\n");
Console.WriteLine("Curve25519 shared secret match.");
}

/* Cleanup */
Expand All @@ -439,7 +439,7 @@ private static void aes_gcm_test()

try
{
Console.WriteLine("Starting AES-GCM tests...");
Console.WriteLine("\nStarting AES-GCM tests...");

IntPtr heap = IntPtr.Zero;
int devId = wolfcrypt.INVALID_DEVID;
Expand Down Expand Up @@ -543,7 +543,7 @@ private static void aes_gcm_test()
{
throw new Exception("Decryption failed: decrypted data does not match original plaintext.");
}
Console.WriteLine("AES-GCM Decryption test passed.\n");
Console.WriteLine("AES-GCM Decryption test passed.");

}
catch (Exception ex)
Expand All @@ -558,8 +558,81 @@ private static void aes_gcm_test()
wolfcrypt.AesGcmFree(aes);
}
}
} /* END aes_gcm_test */

private static void hash_test(wolfcrypt.wc_HashType hashType)
{
IntPtr hash = IntPtr.Zero;
IntPtr heap = IntPtr.Zero;
int devId = wolfcrypt.INVALID_DEVID;

Console.WriteLine($"\nStarting hash test for {hashType}...");

/* Allocate new hash context */
Console.WriteLine("Testing hash context allocation...");
hash = wolfcrypt.HashNew(heap, devId);
if (hash == IntPtr.Zero)
{
Console.WriteLine($"HashNew failed for {hashType}");
return;
}
Console.WriteLine("Hash context allocation test passed.");

/* Initialize the hash context with the specified hash type */
Console.WriteLine("Testing hash initialization...");
int initResult = wolfcrypt.InitHash(hash, hashType);
if (initResult != 0)
{
Console.WriteLine($"InitHash failed for {hashType}");
wolfcrypt.HashFree(hash, hashType);
return;
}
Console.WriteLine("Hash initialization test passed.");

/* Update the hash with data */
byte[] dataToHash = Encoding.UTF8.GetBytes("This is some data to hash");
Console.WriteLine("Testing hash update...");
int updateResult = wolfcrypt.HashUpdate(hash, hashType, dataToHash);
if (updateResult != 0)
{
Console.WriteLine($"HashUpdate failed for {hashType}");
wolfcrypt.HashFree(hash, hashType);
return;
}
Console.WriteLine("Hash update test passed.");

/* Finalize the hash and get the result */
Console.WriteLine("Testing hash finalization...");
byte[] hashOutput;
int finalResult = wolfcrypt.HashFinal(hash, hashType, out hashOutput);
if (finalResult != 0)
{
Console.WriteLine($"HashFinal failed for {hashType}");
wolfcrypt.HashFree(hash, hashType);
return;
}

Console.WriteLine($"Hash finalization test passed for {hashType}. Hash Length: {hashOutput.Length}");

/* Output the hash result */
Console.WriteLine($"Hash Output ({hashType}): {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}");
}
else
{
Console.WriteLine("Hash cleanup test passed.");
}
}




public static void standard_log(int lvl, StringBuilder msg)
{
Console.WriteLine(msg);
Expand Down Expand Up @@ -592,6 +665,10 @@ 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 */

wolfcrypt.Cleanup();

Console.WriteLine("All tests completed successfully.\n");
Expand Down

0 comments on commit 28dad8e

Please sign in to comment.