From aaa6e892dab337ad1f4e9215b7850ff8c5d17d38 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Tue, 14 Jul 2020 11:00:31 -0700 Subject: [PATCH 1/6] Added unit tests for hash.c --- tests/api.c | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 219 insertions(+), 7 deletions(-) diff --git a/tests/api.c b/tests/api.c index 8e70e3d74..fe71e7314 100644 --- a/tests/api.c +++ b/tests/api.c @@ -9018,7 +9018,7 @@ static int test_wc_Sha3_512_Copy (void) /* * Unit test function for wc_Sha3_GetFlags() */ -static int test_wc_Sha3_GetFlags (void) +static int test_wc_Sha3_GetFlags (void) { int ret = 0; #if defined(WOLFSSL_SHA3) && \ @@ -9026,9 +9026,9 @@ static int test_wc_Sha3_GetFlags (void) wc_Sha3 sha3; word32 flags = 0; - + printf(testingFmt, "wc_Sha3_GetFlags()"); - + /* Initialize */ ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId); if (ret != 0) { @@ -9042,7 +9042,7 @@ static int test_wc_Sha3_GetFlags (void) ret = 0; } } - + wc_Sha3_224_Free(&sha3); printf(resultFmt, ret == 0 ? passed : failed); @@ -9199,7 +9199,7 @@ static int test_wc_Shake256_Final (void) /* * Testing wc_Shake256_Copy() */ -static int test_wc_Shake256_Copy (void) +static int test_wc_Shake256_Copy (void) { int ret = 0; #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256) @@ -9255,12 +9255,33 @@ static int test_wc_Shake256_Copy (void) } wc_Shake256_Free(&shake); printf(resultFmt, ret == 0 ? passed : failed); - + #endif return ret; } /* END test_wc_Shake256_Copy */ +/* + * Unit test function for wc_Shake256Hash() + */ +static int test_wc_Shake256Hash(void) +{ + int ret = 0; +#if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256) + const byte data[FOURK_BUF]; + word32 len = sizeof(data); + byte hash[WC_MD5_DIGEST_SIZE]; + word32 hashLen = sizeof(hash); + + printf(testingFmt, "wc_Shake256Hash()"); + + ret = wc_Shake256Hash(data, len, hash, hashLen); + + printf(resultFmt, ret == 0 ? passed : failed); + +#endif + return ret; +} /* END test_wc_Shake256Hash */ /* * unit test for wc_IdeaSetKey() @@ -22653,6 +22674,7 @@ static int test_wc_HashInit(void) /* For loop to test various arguments... */ for (i = 0; i < enumlen; i++) { + //printf("%d", i); /* check for bad args */ if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) { ret = 1; @@ -22677,6 +22699,193 @@ static int test_wc_HashInit(void) } return ret; } /* end of test_wc_HashInit */ +/* + * Unit test function for wc_HashSetFlags() + */ +static int test_wc_HashSetFlags(void) +{ + int ret = 0; +#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB) + wc_HashAlg hash; + word32 flags = 0; + int i, j; + //byte data[FOURK_BUF]; + //byte hashBuf[WC_MAX_DIGEST_SIZE]; + printf(testingFmt, "wc_HashSetFlags()"); + + + /* enum for holding supported algorithms, #ifndef's restrict if disabled */ + enum wc_HashType enumArray[] = { + #ifndef NO_MD5 + WC_HASH_TYPE_MD5, + #endif + #ifndef NO_SHA + WC_HASH_TYPE_SHA, + #endif + #ifdef WOLFSSL_SHA224 + WC_HASH_TYPE_SHA224, + #endif + #ifndef NO_SHA256 + WC_HASH_TYPE_SHA256, + #endif + #ifdef WOLFSSL_SHA384 + WC_HASH_TYPE_SHA384, + #endif + #ifdef WOLFSSL_SHA512 + WC_HASH_TYPE_SHA512, + #endif + #ifdef WOLFSSL_SHA3 + WC_HASH_TYPE_SHA3_224, + #endif + }; + enum wc_HashType notSupported[] = { + WC_HASH_TYPE_MD5_SHA, + WC_HASH_TYPE_MD2, + WC_HASH_TYPE_MD4, + WC_HASH_TYPE_BLAKE2B, + WC_HASH_TYPE_BLAKE2S, + WC_HASH_TYPE_NONE, + }; + + /* dynamically finds the length */ + int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType)); + + /* For loop to test various arguments... */ + for (i = 0; i < enumlen; i++) { + ret = wc_HashInit(&hash, enumArray[i]); + if (ret == 0) { + ret = wc_HashSetFlags(&hash, enumArray[i], flags); + } + if (ret == 0) { + if (flags & WC_HASH_FLAG_ISCOPY) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_HashSetFlags(NULL, enumArray[i], flags); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + + wc_HashFree(&hash, enumArray[i]); + if (ret != 0) { + break; + } + } + /* For loop to test not supported sites */ + int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType)); + for (j = 0; j < notSupportedLen; j++){ + + if (ret == 0) { + ret = wc_HashSetFlags(&hash, notSupported[j], flags); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + wc_HashFree(&hash, notSupported[j]); + if (ret != 0) { + break; + } + + } + + printf(resultFmt, ret == 0 ? passed : failed); + +#endif + return ret; +} /* END test_wc_HashSetFlags */ +/* + * Unit test function for wc_HashGetFlags() + */ +static int test_wc_HashGetFlags(void) +{ + int ret = 0; +#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB) + wc_HashAlg hash; + word32 flags = 0; + int i, j; + //byte data[FOURK_BUF]; + //byte hashBuf[WC_MAX_DIGEST_SIZE]; + printf(testingFmt, "wc_HashGetFlags()"); + + + /* enum for holding supported algorithms, #ifndef's restrict if disabled */ + enum wc_HashType enumArray[] = { + #ifndef NO_MD5 + WC_HASH_TYPE_MD5, + #endif + #ifndef NO_SHA + WC_HASH_TYPE_SHA, + #endif + #ifdef WOLFSSL_SHA224 + WC_HASH_TYPE_SHA224, + #endif + #ifndef NO_SHA256 + WC_HASH_TYPE_SHA256, + #endif + #ifdef WOLFSSL_SHA384 + WC_HASH_TYPE_SHA384, + #endif + #ifdef WOLFSSL_SHA512 + WC_HASH_TYPE_SHA512, + #endif + #ifdef WOLFSSL_SHA3 + WC_HASH_TYPE_SHA3_224, + #endif + }; + enum wc_HashType notSupported[] = { + WC_HASH_TYPE_MD5_SHA, + WC_HASH_TYPE_MD2, + WC_HASH_TYPE_MD4, + WC_HASH_TYPE_BLAKE2B, + WC_HASH_TYPE_BLAKE2S, + WC_HASH_TYPE_NONE, + }; + int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType)); + + /* For loop to test various arguments... */ + for (i = 0; i < enumlen; i++) { + ret = wc_HashInit(&hash, enumArray[i]); + if (ret == 0) { + ret = wc_HashGetFlags(&hash, enumArray[i], &flags); + } + if (ret == 0) { + if (flags & WC_HASH_FLAG_ISCOPY) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_HashGetFlags(NULL, enumArray[i], &flags); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + wc_HashFree(&hash, enumArray[i]); + if (ret != 0) { + break; + } + } + /* For loop to test not supported sites */ + int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType)); + for (j = 0; j < notSupportedLen; j++) { + if (ret == 0) { + ret = wc_HashGetFlags(&hash, notSupported[j], &flags); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + wc_HashFree(&hash, notSupported[j]); + if (ret != 0) { + break; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + +#endif + return ret; +} /* END test_wc_HashGetFlags */ /*----------------------------------------------------------------------------* | Compatibility Tests @@ -35109,11 +35318,12 @@ void ApiTest(void) AssertIntEQ(test_wc_Sha3_256_Copy(), 0); AssertIntEQ(test_wc_Sha3_384_Copy(), 0); AssertIntEQ(test_wc_Sha3_512_Copy(), 0); - AssertIntEQ(test_wc_Sha3_GetFlags(), 0); + AssertIntEQ(test_wc_Sha3_GetFlags(), 0); AssertIntEQ(test_wc_InitShake256(), 0); AssertIntEQ(testing_wc_Shake256_Update(), 0); AssertIntEQ(test_wc_Shake256_Final(), 0); AssertIntEQ(test_wc_Shake256_Copy(), 0); + AssertIntEQ(test_wc_Shake256Hash(), 0); AssertFalse(test_wc_Md5HmacSetKey()); AssertFalse(test_wc_Md5HmacUpdate()); @@ -35132,6 +35342,8 @@ void ApiTest(void) AssertFalse(test_wc_Sha384HmacFinal()); AssertIntEQ(test_wc_HashInit(), 0); + AssertIntEQ(test_wc_HashSetFlags(), 0); + AssertIntEQ(test_wc_HashGetFlags(), 0); AssertIntEQ(test_wc_InitCmac(), 0); AssertIntEQ(test_wc_CmacUpdate(), 0); From 379212acecc013a3d471e217b323c16399457c78 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Tue, 14 Jul 2020 12:28:43 -0700 Subject: [PATCH 2/6] Initialized variable data --- tests/api.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/api.c b/tests/api.c index fe71e7314..9c4a7bfba 100644 --- a/tests/api.c +++ b/tests/api.c @@ -9268,8 +9268,11 @@ static int test_wc_Shake256Hash(void) int ret = 0; #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256) - const byte data[FOURK_BUF]; - word32 len = sizeof(data); + const byte data[] = { /* Hello World */ + 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f, + 0x72,0x6c,0x64 + }; + word32 len = sizeof(data); byte hash[WC_MD5_DIGEST_SIZE]; word32 hashLen = sizeof(hash); From 9a07df9631d9c79f111891e1d359f0deb4b784b3 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 15 Jul 2020 08:11:01 -0700 Subject: [PATCH 3/6] Changed hash size to 144 for Shake256Hash --- tests/api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api.c b/tests/api.c index 9c4a7bfba..ecb52615d 100644 --- a/tests/api.c +++ b/tests/api.c @@ -9273,7 +9273,7 @@ static int test_wc_Shake256Hash(void) 0x72,0x6c,0x64 }; word32 len = sizeof(data); - byte hash[WC_MD5_DIGEST_SIZE]; + byte hash[144]; word32 hashLen = sizeof(hash); printf(testingFmt, "wc_Shake256Hash()"); From 6be76e84ec8b769c42a8abd858c549e85c208091 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 15 Jul 2020 08:11:01 -0700 Subject: [PATCH 4/6] Fixed formatting for Shake256Hash --- tests/api.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/api.c b/tests/api.c index ecb52615d..8eb975c73 100644 --- a/tests/api.c +++ b/tests/api.c @@ -9285,7 +9285,6 @@ static int test_wc_Shake256Hash(void) #endif return ret; } /* END test_wc_Shake256Hash */ - /* * unit test for wc_IdeaSetKey() */ From 2275b8965474e395f6c53e99fdd1ad8741511a06 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Thu, 16 Jul 2020 12:38:55 -0700 Subject: [PATCH 5/6] Removed unnecessary comments and added HashInit's and checked that they returned errors when they should --- tests/api.c | 61 +++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/tests/api.c b/tests/api.c index 8eb975c73..be693ac54 100644 --- a/tests/api.c +++ b/tests/api.c @@ -9271,8 +9271,8 @@ static int test_wc_Shake256Hash(void) const byte data[] = { /* Hello World */ 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f, 0x72,0x6c,0x64 - }; - word32 len = sizeof(data); + }; + word32 len = sizeof(data); byte hash[144]; word32 hashLen = sizeof(hash); @@ -22676,7 +22676,6 @@ static int test_wc_HashInit(void) /* For loop to test various arguments... */ for (i = 0; i < enumlen; i++) { - //printf("%d", i); /* check for bad args */ if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) { ret = 1; @@ -22711,8 +22710,6 @@ static int test_wc_HashSetFlags(void) wc_HashAlg hash; word32 flags = 0; int i, j; - //byte data[FOURK_BUF]; - //byte hashBuf[WC_MAX_DIGEST_SIZE]; printf(testingFmt, "wc_HashSetFlags()"); @@ -22771,23 +22768,26 @@ static int test_wc_HashSetFlags(void) } wc_HashFree(&hash, enumArray[i]); - if (ret != 0) { - break; - } + } - /* For loop to test not supported sites */ + /* For loop to test not supported cases */ int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType)); for (j = 0; j < notSupportedLen; j++){ - if (ret == 0) { - ret = wc_HashSetFlags(&hash, notSupported[j], flags); - if (ret == BAD_FUNC_ARG) { + ret = wc_HashInit(&hash, notSupported[j]); + if (ret == BAD_FUNC_ARG){ ret = 0; - } - } - wc_HashFree(&hash, notSupported[j]); - if (ret != 0) { - break; + if (ret == 0){ + ret = wc_HashSetFlags(&hash, notSupported[j], flags); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + } + } + ret = wc_HashFree(&hash, notSupported[j]); + if (ret == BAD_FUNC_ARG) { + ret = 0; } } @@ -22807,8 +22807,6 @@ static int test_wc_HashGetFlags(void) wc_HashAlg hash; word32 flags = 0; int i, j; - //byte data[FOURK_BUF]; - //byte hashBuf[WC_MAX_DIGEST_SIZE]; printf(testingFmt, "wc_HashGetFlags()"); @@ -22868,19 +22866,26 @@ static int test_wc_HashGetFlags(void) break; } } - /* For loop to test not supported sites */ + /* For loop to test not supported cases */ int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType)); - for (j = 0; j < notSupportedLen; j++) { + for (j = 0; j < notSupportedLen; j++){ if (ret == 0) { - ret = wc_HashGetFlags(&hash, notSupported[j], &flags); - if (ret == BAD_FUNC_ARG) { + ret = wc_HashInit(&hash, notSupported[j]); + if (ret == BAD_FUNC_ARG){ ret = 0; - } - } - wc_HashFree(&hash, notSupported[j]); - if (ret != 0) { - break; + if (ret == 0){ + ret = wc_HashGetFlags(&hash, notSupported[j], &flags); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + } + } + ret = wc_HashFree(&hash, notSupported[j]); + if (ret == BAD_FUNC_ARG) { + ret = 0; } + } printf(resultFmt, ret == 0 ? passed : failed); From 93c6e99aef6f637cdde6c586b5666e05156bd9a9 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Fri, 17 Jul 2020 08:45:39 -0700 Subject: [PATCH 6/6] Added a ret check --- tests/api.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/api.c b/tests/api.c index be693ac54..65cb4166a 100644 --- a/tests/api.c +++ b/tests/api.c @@ -22784,12 +22784,13 @@ static int test_wc_HashSetFlags(void) } } } - } - ret = wc_HashFree(&hash, notSupported[j]); - if (ret == BAD_FUNC_ARG) { - ret = 0; } - + if (ret == 0) { + ret = wc_HashFree(&hash, notSupported[j]); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } } printf(resultFmt, ret == 0 ? passed : failed); @@ -22880,12 +22881,13 @@ static int test_wc_HashGetFlags(void) } } } - } - ret = wc_HashFree(&hash, notSupported[j]); - if (ret == BAD_FUNC_ARG) { - ret = 0; } - + if (ret == 0) { + ret = wc_HashFree(&hash, notSupported[j]); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } } printf(resultFmt, ret == 0 ? passed : failed);