From 7a642e2b78bddbb6dd2a722a05281a85ff12402b Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 15 Jul 2020 12:55:19 -0700 Subject: [PATCH 1/4] Added unit tests for Random.c --- tests/api.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/tests/api.c b/tests/api.c index 8e70e3d74..5b6e60845 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,7 +9255,7 @@ static int test_wc_Shake256_Copy (void) } wc_Shake256_Free(&shake); printf(resultFmt, ret == 0 ? passed : failed); - + #endif return ret; @@ -33358,6 +33358,58 @@ static int test_wc_RNG_GenerateBlock(void) return ret; } +/* + * Testing wc_InitRngNonce + */ +static int test_wc_InitRngNonce(void) +{ + int ret; + WC_RNG rng; + byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" + "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";; + word32 nonceSz = sizeof(nonce); + + + printf(testingFmt, "wc_InitRngNonce()"); + + ret = wc_InitRng(&rng); + + if (ret == 0){ + ret = wc_InitRngNonce(&rng, nonce, nonceSz); + } + + wc_FreeRng(&rng); + + printf(resultFmt, ret == 0 ? passed : failed); + + return ret; +}/* End test_wc_InitRngNonce*/ +/* + * Testing wc_InitRngNonce_ex + */ +static int test_wc_InitRngNonce_ex(void) +{ + int ret; + WC_RNG rng; + byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" + "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";; + word32 nonceSz = sizeof(nonce); + + + printf(testingFmt, "wc_InitRngNonce_ex()"); + + ret = wc_InitRng(&rng); + if (ret == 0){ + ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId); + } + + wc_FreeRng(&rng); + + printf(resultFmt, ret == 0 ? passed : failed); + + return ret; +}/*End test_wc_InitRngNonce_ex*/ + #endif static void test_wolfSSL_X509_CRL(void) @@ -35109,7 +35161,7 @@ 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); @@ -35228,6 +35280,8 @@ void ApiTest(void) AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0); #endif AssertIntEQ(test_wc_RNG_GenerateBlock(), 0); + AssertIntEQ(test_wc_InitRngNonce(), 0); + AssertIntEQ(test_wc_InitRngNonce_ex(), 0); #endif AssertIntEQ(test_wc_ed25519_make_key(), 0); From d54a51cd204ec20bbf64990140ddb30d457b559e Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 15 Jul 2020 13:56:12 -0700 Subject: [PATCH 2/4] Added if not defined wc_no_rng --- tests/api.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/api.c b/tests/api.c index 5b6e60845..22a8a796d 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33364,6 +33364,7 @@ static int test_wc_RNG_GenerateBlock(void) static int test_wc_InitRngNonce(void) { int ret; +#ifndef WC_NO_RNG WC_RNG rng; byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";; @@ -33381,7 +33382,7 @@ static int test_wc_InitRngNonce(void) wc_FreeRng(&rng); printf(resultFmt, ret == 0 ? passed : failed); - +#endif return ret; }/* End test_wc_InitRngNonce*/ /* @@ -33390,6 +33391,7 @@ static int test_wc_InitRngNonce(void) static int test_wc_InitRngNonce_ex(void) { int ret; +#ifndef WC_NO_RNG WC_RNG rng; byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";; @@ -33406,7 +33408,7 @@ static int test_wc_InitRngNonce_ex(void) wc_FreeRng(&rng); printf(resultFmt, ret == 0 ? passed : failed); - +#endif return ret; }/*End test_wc_InitRngNonce_ex*/ From 96e59118fcf32162fc2f8de014c11af8ba45f932 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Thu, 16 Jul 2020 15:26:10 -0700 Subject: [PATCH 3/4] Changed the if defined order and to include fips and selftest --- tests/api.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/api.c b/tests/api.c index 22a8a796d..1a056ccfe 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33358,13 +33358,15 @@ static int test_wc_RNG_GenerateBlock(void) return ret; } +#endif /* * Testing wc_InitRngNonce */ static int test_wc_InitRngNonce(void) { - int ret; -#ifndef WC_NO_RNG + int ret=0; +#if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \ + (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2)) WC_RNG rng; byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";; @@ -33390,8 +33392,9 @@ static int test_wc_InitRngNonce(void) */ static int test_wc_InitRngNonce_ex(void) { - int ret; -#ifndef WC_NO_RNG + int ret=0; +#if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \ + (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2)) WC_RNG rng; byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";; @@ -33412,7 +33415,7 @@ static int test_wc_InitRngNonce_ex(void) return ret; }/*End test_wc_InitRngNonce_ex*/ -#endif + static void test_wolfSSL_X509_CRL(void) { From ef7109922577b27c04210331fcad876e76f36c88 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Fri, 17 Jul 2020 10:34:38 -0700 Subject: [PATCH 4/4] Removed duplicate semicolon and deleted unneeded initrng --- tests/api.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tests/api.c b/tests/api.c index 1a056ccfe..004a64741 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33369,14 +33369,12 @@ static int test_wc_InitRngNonce(void) (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2)) WC_RNG rng; byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" - "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";; + "\x45\xAC\x13\x7A\xE1\x48\xAF\x16"; word32 nonceSz = sizeof(nonce); printf(testingFmt, "wc_InitRngNonce()"); - ret = wc_InitRng(&rng); - if (ret == 0){ ret = wc_InitRngNonce(&rng, nonce, nonceSz); } @@ -33397,13 +33395,11 @@ static int test_wc_InitRngNonce_ex(void) (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2)) WC_RNG rng; byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" - "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";; + "\x45\xAC\x13\x7A\xE1\x48\xAF\x16"; word32 nonceSz = sizeof(nonce); - printf(testingFmt, "wc_InitRngNonce_ex()"); - - ret = wc_InitRng(&rng); + if (ret == 0){ ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId); } @@ -35285,10 +35281,10 @@ void ApiTest(void) AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0); #endif AssertIntEQ(test_wc_RNG_GenerateBlock(), 0); + +#endif AssertIntEQ(test_wc_InitRngNonce(), 0); AssertIntEQ(test_wc_InitRngNonce_ex(), 0); -#endif - AssertIntEQ(test_wc_ed25519_make_key(), 0); AssertIntEQ(test_wc_ed25519_init(), 0); AssertIntEQ(test_wc_ed25519_sign_msg(), 0);