From 28dad8e395fef1a3ebeb202ceec159154bd65281 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Wed, 4 Sep 2024 16:20:15 -0700 Subject: [PATCH] 'wc_HashNew' and test case for hash --- wolfcrypt/src/hash.c | 31 +++++++ wolfcrypt/src/rsa.c | 2 +- wolfssl/wolfcrypt/hash.h | 2 + .../CSharp/wolfCrypt-Test/wolfCrypt-Test.cs | 83 ++++++++++++++++++- 4 files changed, 114 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/src/hash.c b/wolfcrypt/src/hash.c index db3a047998..d5988341d7 100644 --- a/wolfcrypt/src/hash.c +++ b/wolfcrypt/src/hash.c @@ -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) { @@ -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 @@ -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 @@ -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; @@ -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; } diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index 708a9cd7f2..35b2a88b0f 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -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; diff --git a/wolfssl/wolfcrypt/hash.h b/wolfssl/wolfcrypt/hash.h index 2f7de32d0d..b3dda6d09c 100644 --- a/wolfssl/wolfcrypt/hash.h +++ b/wolfssl/wolfcrypt/hash.h @@ -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 */ @@ -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); diff --git a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs index f74c83ca29..c1c6a5fec3 100755 --- a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs +++ b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs @@ -415,7 +415,7 @@ private static void curve25519_test() } else { - Console.WriteLine("Curve25519 shared secret match.\n"); + Console.WriteLine("Curve25519 shared secret match."); } /* Cleanup */ @@ -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; @@ -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) @@ -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); @@ -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");