diff --git a/mplabx/benchmark_main.c b/mplabx/benchmark_main.c index 5c4c8651e..c7a31b4be 100644 --- a/mplabx/benchmark_main.c +++ b/mplabx/benchmark_main.c @@ -53,7 +53,7 @@ void bench_md5(void); void bench_sha(void); void bench_sha256(void); void bench_sha512(void); -void bench_ripemd(void); +int bench_ripemd(void); void bench_rsa(void); void bench_rsaKeyGen(void); diff --git a/tests/api.c b/tests/api.c index ee04fce05..13b709e42 100644 --- a/tests/api.c +++ b/tests/api.c @@ -61,6 +61,9 @@ #ifdef WOLFSSL_SHA384 #include #endif +#ifdef WOLFSSL_RIPEMD + #include +#endif #ifdef OPENSSL_EXTRA #include @@ -2442,6 +2445,73 @@ static int test_wc_InitSha384 (void) return 0; } /* END test_wc_InitSha384 */ +/* + * Testing wc_InitSha224(); + */ +static int test_wc_InitSha224 (void) +{ +#ifdef WOLFSSL_SHA224 + Sha224 sha224; + int ret, flag; + + flag = 0; + + printf(testingFmt, "wc_InitSha224()"); + + /* Test good arg. */ + ret = wc_InitSha224(&sha224); + if (ret != 0) { + flag = SSL_FATAL_ERROR; + } + + /* Test bad arg. */ + if (!flag) { + ret = wc_InitSha224(NULL); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, flag == 0 ? passed : failed); + +#endif + return 0; +} /* END test_wc_InitSha224 */ + + +/* + * Testing wc_InitRipeMd() + */ +static int test_wc_InitRipeMd (void) +{ +#ifdef WOLFSSL_RIPEMD + RipeMd ripemd; + int ret, flag; + + flag = 0; + + printf(testingFmt, "wc_InitRipeMd()"); + + /* Test good arg. */ + ret = wc_InitRipeMd(&ripemd); + if (ret != 0) { + flag = SSL_FATAL_ERROR; + } + + /* Test bad arg. */ + if (!flag) { + ret = wc_InitRipeMd(NULL); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, flag == 0 ? passed : failed); + +#endif + return 0; + +} /* END test_wc_InitRipeMd */ /* * Testing wc_UpdateMd5() @@ -2859,6 +2929,109 @@ static int test_wc_Sha384Update (void) return 0; } /* END test_wc_Sha384Update */ +/* + * Testing wc_RipeMdUpdate() + */ +static int test_wc_RipeMdUpdate (void) +{ + +#ifdef WOLFSSL_RIPEMD + RipeMd ripemd; + byte hash[RIPEMD_DIGEST_SIZE]; + testVector a, b, c; + int ret, flag; + + flag = 0; + + ret = wc_InitRipeMd(&ripemd); + if (ret != 0) { + flag = ret; + } + + printf(testingFmt, "wc_RipeMdUpdate()"); + + /* Input */ + if (!flag) { + a.input = "a"; + a.inLen = XSTRLEN(a.input); + } + + if (!flag) { + ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen); + if (ret != 0) { + flag = ret; + } + } + + if (!flag) { + ret = wc_RipeMdFinal(&ripemd, hash); + if (ret != 0) { + flag = ret; + } + } + + /* Update input. */ + if (!flag) { + a.input = "abc"; + a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6" + "\xb0\x87\xf1\x5a\x0b\xfc"; + a.inLen = XSTRLEN(a.input); + a.outLen = XSTRLEN(a.output); + + ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen); + if (ret != 0) { + flag = ret; + } + } + + if (!flag) { + ret = wc_RipeMdFinal(&ripemd, hash); + if (ret != 0) { + flag = ret; + } + } + + if (!flag) { + if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) { + flag = SSL_FATAL_ERROR; + } + } + + /* Pass in bad values. */ + if (!flag) { + b.input = NULL; + b.inLen = 0; + + ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen); + if (ret != 0) { + flag = ret; + } + } + + if (!flag) { + c.input = NULL; + c.inLen = RIPEMD_DIGEST_SIZE; + + ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + if (!flag) { + ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, flag == 0 ? passed : failed); + +#endif + return 0; + +} /* END test_wc_RipeMdUdpate */ + /* * wc_Sha512Update() test. */ @@ -2903,11 +3076,11 @@ static int test_wc_Sha512Update (void) /* Update input. */ if (!flag) { a.input = "abc"; - a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31" - "\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3" - "\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe" - "\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5" - "\x4c\xa4\x9f"; + a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41" + "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b" + "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c" + "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a" + "\x9a\xc9\x4f\xa5\x4c\xa4\x9f"; a.inLen = XSTRLEN(a.input); a.outLen = XSTRLEN(a.output); @@ -2926,7 +3099,7 @@ static int test_wc_Sha512Update (void) if (!flag) { if (XMEMCMP(hash, a.output, SHA512_DIGEST_SIZE) != 0) { - flag = SSL_FAILURE; + flag = SSL_FATAL_ERROR; } } @@ -2966,6 +3139,109 @@ static int test_wc_Sha512Update (void) } /* END test_wc_Sha512Update */ +/* + * Unit test on wc_Sha224Update + */ +static int test_wc_Sha224Update (void) +{ +#ifdef WOLFSSL_SHA224 + Sha224 sha224; + byte hash[SHA224_DIGEST_SIZE]; + testVector a, b, c; + int ret, flag; + + flag = 0; + + ret = wc_InitSha224(&sha224); + if (ret != 0) { + flag = ret; + } + + printf(testingFmt, "wc_Sha224Update()"); + + /* Input. */ + if (!flag) { + a.input = "a"; + a.inLen = XSTRLEN(a.input); + } + + if (!flag) { + ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen); + if (ret != 0) { + flag = ret; + } + } + + if (!flag) { + ret = wc_Sha224Final(&sha224, hash); + if (ret != 0) { + flag = ret; + } + } + + /* Update input. */ + if (!flag) { + a.input = "abc"; + a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2" + "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7"; + a.inLen = XSTRLEN(a.input); + a.outLen = XSTRLEN(a.output); + + ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen); + if (ret != 0) { + flag = ret; + } + } + + if (!flag) { + ret = wc_Sha224Final(&sha224, hash); + if (ret != 0) { + flag = ret; + } + } + + if (!flag) { + if (XMEMCMP(hash, a.output, SHA224_DIGEST_SIZE) != 0) { + flag = SSL_FATAL_ERROR; + } + } + + /* Pass in bad values. */ + if (!flag) { + b.input = NULL; + b.inLen = 0; + + ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen); + if (ret != 0) { + flag = ret; + } + } + + if (!flag) { + c.input = NULL; + c.inLen = SHA224_DIGEST_SIZE; + + ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + if (!flag) { + ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + /* If not returned then the unit test passed test vectors. */ + printf(resultFmt, flag == 0 ? passed : failed); + +#endif + return 0; + +} /* END test_wc_Sha224Update */ + /* * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c */ @@ -3313,6 +3589,144 @@ static int test_wc_Sha384Final (void) } /* END test_wc_Sha384Final */ +/* + * Unit test function for wc_RipeMdFinal() + */ +static int test_wc_RipeMdFinal (void) +{ +#ifdef WOLFSSL_RIPEMD + RipeMd ripemd; + byte* hash_test[3]; + byte hash1[RIPEMD_DIGEST_SIZE]; + byte hash2[2*RIPEMD_DIGEST_SIZE]; + byte hash3[5*RIPEMD_DIGEST_SIZE]; + int times, i, ret, flag; + + flag = 0; + + /* Initialize */ + ret = wc_InitRipeMd(&ripemd); + if (ret != 0) { + flag = ret; + } + + if (!flag) { + hash_test[0] = hash1; + hash_test[1] = hash2; + hash_test[2] = hash3; + } + + times = sizeof(hash_test) / sizeof(byte*); + + /* Good test args. */ + printf(testingFmt, "wc_RipeMdFinal()"); + /* Testing oversized buffers. */ + for (i = 0; i < times; i++) { + if (!flag) { + ret = wc_RipeMdFinal(&ripemd, hash_test[i]); + if (ret != 0) { + flag = SSL_FATAL_ERROR; + } + } + } + + /* Test bad args. */ + if (!flag) { + ret = wc_RipeMdFinal(NULL, NULL); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + if (!flag) { + ret = wc_RipeMdFinal(NULL, hash1); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + if (!flag) { + ret = wc_RipeMdFinal(&ripemd, NULL); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, flag == 0 ? passed : failed); + +#endif + return 0; +} /* END test_wc_RipeMdFinal */ + +/* + * Unit test for wc_Sha224Final(); + */ +static int test_wc_Sha224Final (void) +{ +#ifdef WOLFSSL_SHA224 + Sha224 sha224; + byte* hash_test[3]; + byte hash1[SHA224_DIGEST_SIZE]; + byte hash2[2*SHA224_DIGEST_SIZE]; + byte hash3[5*SHA224_DIGEST_SIZE]; + int times, i, ret, flag; + + flag = 0; + + /* Initialize */ + ret = wc_InitSha224(&sha224); + if (ret) { + flag = ret; + } + + if (!flag) { + hash_test[0] = hash1; + hash_test[1] = hash2; + hash_test[2] = hash3; + } + + times = sizeof(hash_test) / sizeof(byte*); + + /* Good test args. */ + printf(testingFmt, "wc_sha224Final()"); + /* Testing oversized buffers. */ + for (i = 0; i < times; i++) { + if (!flag) { + ret = wc_Sha224Final(&sha224, hash_test[i]); + if (ret != 0) { + flag = SSL_FATAL_ERROR; + } + } + } + + /* Test bad args. */ + if (!flag) { + ret = wc_Sha224Final(NULL, NULL); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + if (!flag) { + ret = wc_Sha224Final(NULL, hash1); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + if (!flag) { + ret = wc_Sha224Final(&sha224, NULL); + if (ret != BAD_FUNC_ARG) { + flag = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, flag == 0 ? passed : failed); + +#endif + return 0; +} /* END test_wc_Sha224Final */ + /*----------------------------------------------------------------------------* | Compatibility Tests @@ -4566,6 +4980,12 @@ void ApiTest(void) AssertFalse(test_wc_InitSha384()); AssertFalse(test_wc_Sha384Update()); AssertFalse(test_wc_Sha384Final()); + AssertFalse(test_wc_InitSha224()); + AssertFalse(test_wc_Sha224Update()); + AssertFalse(test_wc_Sha224Final()); + AssertFalse(test_wc_InitRipeMd()); + AssertFalse(test_wc_RipeMdUpdate()); + AssertFalse(test_wc_RipeMdFinal()); printf(" End API Tests\n"); } diff --git a/tests/hash.c b/tests/hash.c index 0407af600..5dd0017be 100644 --- a/tests/hash.c +++ b/tests/hash.c @@ -590,6 +590,7 @@ int sha384_test() int ripemd_test(void) { RipeMd ripemd; + int ret; byte hash[RIPEMD_DIGEST_SIZE]; testVector a, b, c, d; @@ -626,12 +627,22 @@ int ripemd_test(void) test_ripemd[2] = c; test_ripemd[3] = d; - wc_InitRipeMd(&ripemd); + ret = wc_InitRipeMd(&ripemd); + if (ret) { + return ret; + } for (i = 0; i < times; ++i) { - wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input, - (word32)test_ripemd[i].inLen); - wc_RipeMdFinal(&ripemd, hash); + ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input, + (word32)test_ripemd[i].inLen); + if (ret) { + return ret; + } + + ret = wc_RipeMdFinal(&ripemd, hash); + if (ret) { + return ret; + } if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0) return -10 - i; diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 3ca221509..27e605c7a 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -208,7 +208,7 @@ void bench_sha224(int); void bench_sha256(int); void bench_sha384(int); void bench_sha512(int); -void bench_ripemd(void); +int bench_ripemd(void); void bench_cmac(void); void bench_scrypt(void); @@ -1039,7 +1039,7 @@ int benchmark_test(void *args) bench_stats_free(); - if (wolfCrypt_Cleanup() != 0) { + if (wolfCrypt_Cleanup() != 0) { printf("error with wolfCrypt_Cleanup\n"); } @@ -2085,24 +2085,36 @@ exit: #endif #ifdef WOLFSSL_RIPEMD -void bench_ripemd(void) +int bench_ripemd(void) { RipeMd hash; byte digest[RIPEMD_DIGEST_SIZE]; double start; - int i, count; + int i, count, ret; - wc_InitRipeMd(&hash); + ret = wc_InitRipeMd(&hash); + if (ret != 0) { + return ret; + } bench_stats_start(&count, &start); do { for (i = 0; i < numBlocks; i++) { - wc_RipeMdUpdate(&hash, bench_plain, BENCH_SIZE); + ret = wc_RipeMdUpdate(&hash, bench_plain, BENCH_SIZE); + if (ret != 0) { + return ret; + } } - wc_RipeMdFinal(&hash, digest); + ret = wc_RipeMdFinal(&hash, digest); + if (ret != 0) { + return ret; + } + count += i; } while (bench_stats_sym_check(start)); bench_stats_sym_finish("RIPEMD", 0, count, start); + + return 0; } #endif diff --git a/wolfcrypt/src/ripemd.c b/wolfcrypt/src/ripemd.c index 8cda86aef..0958ea477 100644 --- a/wolfcrypt/src/ripemd.c +++ b/wolfcrypt/src/ripemd.c @@ -37,10 +37,14 @@ #include #endif +#include - -void wc_InitRipeMd(RipeMd* ripemd) +int wc_InitRipeMd(RipeMd* ripemd) { + if (ripemd == NULL) { + return BAD_FUNC_ARG; + } + ripemd->digest[0] = 0x67452301L; ripemd->digest[1] = 0xEFCDAB89L; ripemd->digest[2] = 0x98BADCFEL; @@ -50,6 +54,8 @@ void wc_InitRipeMd(RipeMd* ripemd) ripemd->buffLen = 0; ripemd->loLen = 0; ripemd->hiLen = 0; + + return 0; } @@ -273,10 +279,16 @@ static INLINE void AddLength(RipeMd* ripemd, word32 len) } -void wc_RipeMdUpdate(RipeMd* ripemd, const byte* data, word32 len) +int wc_RipeMdUpdate(RipeMd* ripemd, const byte* data, word32 len) { /* do block size increments */ - byte* local = (byte*)ripemd->buffer; + byte* local; + + if (ripemd == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + + local = (byte*)ripemd->buffer; while (len) { word32 add = min(len, RIPEMD_BLOCK_SIZE - ripemd->buffLen); @@ -296,12 +308,19 @@ void wc_RipeMdUpdate(RipeMd* ripemd, const byte* data, word32 len) ripemd->buffLen = 0; } } + return 0; } -void wc_RipeMdFinal(RipeMd* ripemd, byte* hash) +int wc_RipeMdFinal(RipeMd* ripemd, byte* hash) { - byte* local = (byte*)ripemd->buffer; + byte* local; + + if (ripemd == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + + local = (byte*)ripemd->buffer; AddLength(ripemd, ripemd->buffLen); /* before adding pads */ @@ -340,7 +359,7 @@ void wc_RipeMdFinal(RipeMd* ripemd, byte* hash) #endif XMEMCPY(hash, ripemd->digest, RIPEMD_DIGEST_SIZE); - wc_InitRipeMd(ripemd); /* reset state */ + return wc_InitRipeMd(ripemd); /* reset state */ } diff --git a/wolfcrypt/src/sha256.c b/wolfcrypt/src/sha256.c index 268e30d2a..9783c7cab 100755 --- a/wolfcrypt/src/sha256.c +++ b/wolfcrypt/src/sha256.c @@ -1818,8 +1818,13 @@ static int Transform_AVX2(Sha256* sha256) #ifdef WOLFSSL_SHA224 static int InitSha224(Sha224* sha224) { + int ret = 0; + if (sha224 == NULL) { + return BAD_FUNC_ARG; + } + sha224->digest[0] = 0xc1059ed8; sha224->digest[1] = 0x367cd507; sha224->digest[2] = 0x3070dd17; @@ -1873,6 +1878,10 @@ static int Transform_AVX2(Sha256* sha256) { int ret; + if (sha224 == NULL || (data == NULL && len > 0)) { + return BAD_FUNC_ARG; + } + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224) if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) { #if defined(HAVE_INTEL_QA) @@ -1890,6 +1899,10 @@ static int Transform_AVX2(Sha256* sha256) { int ret; + if (sha224 == NULL || hash == NULL) { + return BAD_FUNC_ARG; + } + #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224) if (sha224->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA224) { #if defined(HAVE_INTEL_QA) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index fbed19234..db6ce9fbc 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -1390,6 +1390,7 @@ int sha_test(void) int ripemd_test(void) { RipeMd ripemd; + int ret; byte hash[RIPEMD_DIGEST_SIZE]; testVector a, b, c, d; @@ -1426,15 +1427,25 @@ int ripemd_test(void) test_ripemd[2] = c; test_ripemd[3] = d; - wc_InitRipeMd(&ripemd); + ret = wc_InitRipeMd(&ripemd); + if (ret != 0) { + return -1800; + } for (i = 0; i < times; ++i) { - wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input, - (word32)test_ripemd[i].inLen); - wc_RipeMdFinal(&ripemd, hash); + ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input, + (word32)test_ripemd[i].inLen); + if (ret != 0) { + return -1810 - i; + } + + ret = wc_RipeMdFinal(&ripemd, hash); + if (ret != 0) { + return -1820 - i; + } if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0) - return -1800 - i; + return -1830 - i; } return 0; diff --git a/wolfssl/wolfcrypt/ripemd.h b/wolfssl/wolfcrypt/ripemd.h index 5845d594c..65897abaa 100644 --- a/wolfssl/wolfcrypt/ripemd.h +++ b/wolfssl/wolfcrypt/ripemd.h @@ -51,9 +51,9 @@ typedef struct RipeMd { } RipeMd; -WOLFSSL_API void wc_InitRipeMd(RipeMd*); -WOLFSSL_API void wc_RipeMdUpdate(RipeMd*, const byte*, word32); -WOLFSSL_API void wc_RipeMdFinal(RipeMd*, byte*); +WOLFSSL_API int wc_InitRipeMd(RipeMd*); +WOLFSSL_API int wc_RipeMdUpdate(RipeMd*, const byte*, word32); +WOLFSSL_API int wc_RipeMdFinal(RipeMd*, byte*); #ifdef __cplusplus