Merge pull request #3133 from ethanlooney/13th_branch

Added unit tests for Hash.c - Fixed hash formatting errors
This commit is contained in:
Chris Conlon
2020-07-20 10:03:28 -06:00
committed by GitHub

View File

@ -9260,8 +9260,31 @@ static int test_wc_Shake256_Copy (void)
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[] = { /* Hello World */
0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
0x72,0x6c,0x64
};
word32 len = sizeof(data);
byte hash[144];
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()
*/
@ -22677,6 +22700,201 @@ 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;
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]);
}
/* 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_HashInit(&hash, notSupported[j]);
if (ret == BAD_FUNC_ARG){
ret = 0;
if (ret == 0){
ret = wc_HashSetFlags(&hash, notSupported[j], flags);
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);
#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;
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 cases */
int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
for (j = 0; j < notSupportedLen; j++){
if (ret == 0) {
ret = wc_HashInit(&hash, notSupported[j]);
if (ret == BAD_FUNC_ARG){
ret = 0;
if (ret == 0){
ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
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);
#endif
return ret;
} /* END test_wc_HashGetFlags */
/*----------------------------------------------------------------------------*
| Compatibility Tests
@ -35187,6 +35405,7 @@ void ApiTest(void)
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());
@ -35205,6 +35424,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);