Digest tests: add more tests

Add testing of MD2 and Md4.
Add more tests of functions in hash.c.
Reformat data to match what is output by PRINT_DATA macro.
This commit is contained in:
Sean Parkinson
2025-03-04 16:55:05 +10:00
parent 72d08a1a79
commit e7ef3ab606
20 changed files with 1842 additions and 717 deletions

View File

@ -2512,6 +2512,8 @@ if(WOLFSSL_EXAMPLES)
# Build unit tests
add_executable(unit_test
tests/api.c
tests/api/test_md2.c
tests/api/test_md4.c
tests/api/test_md5.c
tests/api/test_sha.c
tests/api/test_sha256.c

View File

@ -290,6 +290,8 @@
#include <tests/api/api.h>
/* Gather test declarations to include them in the testCases array */
#include <tests/api/test_md2.h>
#include <tests/api/test_md4.h>
#include <tests/api/test_md5.h>
#include <tests/api/test_sha.h>
#include <tests/api/test_sha256.h>
@ -88997,6 +88999,21 @@ TEST_CASE testCases[] = {
TEST_DECL(test_wc_LockMutex_ex),
/* Digests */
/* test_md2.c */
TEST_DECL(test_wc_InitMd2),
TEST_DECL(test_wc_Md2Update),
TEST_DECL(test_wc_Md2Final),
TEST_DECL(test_wc_Md2_KATs),
TEST_DECL(test_wc_Md2_other),
TEST_DECL(test_wc_Md2Hash),
/* test_md4.c */
TEST_DECL(test_wc_InitMd4),
TEST_DECL(test_wc_Md4Update),
TEST_DECL(test_wc_Md4Final),
TEST_DECL(test_wc_Md4_KATs),
TEST_DECL(test_wc_Md4_other),
/* test_md5.c */
TEST_DECL(test_wc_InitMd5),
TEST_DECL(test_wc_Md5Update),
@ -89145,7 +89162,6 @@ TEST_CASE testCases[] = {
TEST_DECL(test_wc_Sm3Copy),
TEST_DECL(test_wc_Sm3GetHash),
TEST_DECL(test_wc_Sm3_Flags),
TEST_DECL(test_wc_Sm3Hash),
/* test_ripemd.c */
TEST_DECL(test_wc_InitRipeMd),
@ -89156,8 +89172,17 @@ TEST_CASE testCases[] = {
/* test_hash.c */
TEST_DECL(test_wc_HashInit),
TEST_DECL(test_wc_HashUpdate),
TEST_DECL(test_wc_HashFinal),
TEST_DECL(test_wc_HashNewDelete),
TEST_DECL(test_wc_HashGetDigestSize),
TEST_DECL(test_wc_HashGetBlockSize),
TEST_DECL(test_wc_Hash),
TEST_DECL(test_wc_HashSetFlags),
TEST_DECL(test_wc_HashGetFlags),
TEST_DECL(test_wc_Hash_Algs),
TEST_DECL(test_wc_HashGetOID),
TEST_DECL(test_wc_OidGetHash),
/* HMAC */
TEST_DECL(test_wc_Md5HmacSetKey),

View File

@ -4,6 +4,8 @@
if BUILD_TESTS
# Digests
tests_unit_test_SOURCES += tests/api/test_md2.c
tests_unit_test_SOURCES += tests/api/test_md4.c
tests_unit_test_SOURCES += tests/api/test_md5.c
tests_unit_test_SOURCES += tests/api/test_sha.c
tests_unit_test_SOURCES += tests/api/test_sha256.c
@ -37,6 +39,8 @@ tests_unit_test_SOURCES += tests/api/test_ocsp.c
endif
EXTRA_DIST += tests/api/api.h
EXTRA_DIST += tests/api/test_md2.h
EXTRA_DIST += tests/api/test_md4.h
EXTRA_DIST += tests/api/test_md5.h
EXTRA_DIST += tests/api/test_sha.h
EXTRA_DIST += tests/api/test_sha256.h

View File

@ -395,7 +395,7 @@ int test_wc_Blake2sFinal(void)
EXPECT_DECLS;
#ifdef HAVE_BLAKE2S
Blake2s blake;
byte hash[WC_BLAKE2B_DIGEST_SIZE];
byte hash[WC_BLAKE2S_DIGEST_SIZE];
/* Initialize */
ExpectIntEQ(wc_InitBlake2s(&blake, WC_BLAKE2S_DIGEST_SIZE), 0);
@ -407,7 +407,7 @@ int test_wc_Blake2sFinal(void)
ExpectIntEQ(wc_Blake2sFinal(NULL, hash, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Test good args. */
ExpectIntEQ(wc_Blake2sFinal(&blake, hash, WC_BLAKE2B_DIGEST_SIZE), 0);
ExpectIntEQ(wc_Blake2sFinal(&blake, hash, WC_BLAKE2S_DIGEST_SIZE), 0);
#endif
return EXPECT_RESULT();
}

View File

@ -68,6 +68,17 @@ do { \
\
wc_##name##Free(NULL)
#define DIGEST_INIT_ONLY_TEST(type, name) \
do { \
type dgst; \
\
/* Test bad arg. */ \
wc_Init##name(NULL); \
\
/* Test good arg. */ \
wc_Init##name(&dgst); \
} while (0)
#define DIGEST_UPDATE_TEST(type, name) \
type dgst; \
\
@ -79,6 +90,8 @@ do { \
ExpectIntEQ(wc_##name##Update(&dgst, NULL, 1), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Update(NULL, NULL, 0), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Update(NULL, (byte*)"a", 1), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
ExpectIntEQ(wc_##name##Update(&dgst, NULL, 0), 0); \
@ -98,6 +111,8 @@ do { \
ExpectIntEQ(wc_##name##_Update(&dgst, NULL, 1), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##_Update(NULL, NULL, 0), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##_Update(NULL, (byte*)"a", 1), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
ExpectIntEQ(wc_##name##_Update(&dgst, NULL, 0), 0); \
@ -106,6 +121,20 @@ do { \
wc_##name##_Free(&dgst); \
} while (0)
#define DIGEST_UPDATE_ONLY_TEST(type, name) \
type dgst; \
\
wc_Init##name(&dgst); \
\
/* Pass in bad values. */ \
wc_##name##Update(NULL, NULL, 1); \
wc_##name##Update(&dgst, NULL, 1); \
wc_##name##Update(NULL, NULL, 0); \
wc_##name##Update(NULL, (byte*)"a", 1); \
\
wc_##name##Update(&dgst, NULL, 0); \
wc_##name##Update(&dgst, (byte*)"a", 1)
#define DIGEST_FINAL_TEST(type, name, upper) \
type dgst; \
byte hash[WC_##upper##_DIGEST_SIZE]; \
@ -165,6 +194,21 @@ do { \
wc_##name##_Free(&dgst); \
} while (0)
#define DIGEST_FINAL_ONLY_TEST(type, name, upper) \
type dgst; \
byte hash[WC_##upper##_DIGEST_SIZE]; \
\
/* Initialize */ \
wc_Init##name(&dgst); \
\
/* Test bad args. */ \
wc_##name##Final(NULL, NULL); \
wc_##name##Final(&dgst, NULL); \
wc_##name##Final(NULL, hash); \
\
/* Test good args. */ \
wc_##name##Final(&dgst, hash); \
#define DIGEST_FINAL_RAW_TEST(type, name, upper, hashStr) \
type dgst; \
byte hash[WC_##upper##_DIGEST_SIZE]; \
@ -241,6 +285,23 @@ do { \
\
wc_##name##_Free(&dgst)
#define DIGEST_KATS_ONLY_TEST(name, upper) \
do { \
(void)i; \
\
/* Initialize */ \
wc_Init##name(&dgst); \
\
for (i = 0; i < upper##_KAT_CNT; i++) { \
/* Do KAT. */ \
wc_##name##Update(&dgst, (byte*)dgst_kat[i].input, \
(word32)dgst_kat[i].inLen); \
wc_##name##Final(&dgst, hash); \
ExpectBufEQ(hash, (byte*)dgst_kat[i].output, \
WC_##upper##_DIGEST_SIZE); \
} \
} while (0)
#define DIGEST_OTHER_TEST(type, name, upper, hashStr) \
type dgst; \
byte hash[WC_##upper##_DIGEST_SIZE + 1]; \
@ -367,6 +428,46 @@ do { \
wc_##name##_Free(&dgst); \
} while (0)
#define DIGEST_OTHER_ONLY_TEST(type, name, upper, hashStr) \
do { \
type dgst; \
byte hash[WC_##upper##_DIGEST_SIZE + 1]; \
byte data[WC_##upper##_DIGEST_SIZE * 8 + 1]; \
int dataLen = WC_##upper##_DIGEST_SIZE * 8; \
const char* expHash = hashStr; \
int i; \
int j; \
\
XMEMSET(data, 0xa5, sizeof(data)); \
\
/* Initialize */ \
wc_Init##name(&dgst); \
\
/* Unaligned input and output buffer. */ \
wc_##name##Update(&dgst, data + 1, dataLen); \
wc_##name##Final(&dgst, hash + 1); \
ExpectBufEQ(hash + 1, (byte*)expHash, WC_##upper##_DIGEST_SIZE); \
\
/* Test that empty updates work. */ \
wc_##name##Update(&dgst, NULL, 0); \
wc_##name##Update(&dgst, (byte*)"", 0); \
wc_##name##Update(&dgst, data, dataLen); \
wc_##name##Final(&dgst, hash); \
ExpectBufEQ(hash, (byte*)expHash, WC_##upper##_DIGEST_SIZE); \
\
/* Ensure chunking works. */ \
for (i = 1; i < dataLen; i++) { \
for (j = 0; j < dataLen; j += i) { \
int len = dataLen - j; \
if (i < len) \
len = i; \
wc_##name##Update(&dgst, data + j, len); \
} \
wc_##name##Final(&dgst, hash); \
ExpectBufEQ(hash, (byte*)expHash, WC_##upper##_DIGEST_SIZE); \
} \
} while (0)
#define DIGEST_COPY_TEST(type, name, upper, emptyHashStr, abcHashStr) \
type src; \
type dst; \
@ -541,10 +642,12 @@ do { \
ExpectIntEQ(wc_Init##name(&dgst), 0); \
\
/* Test bad args. */ \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(NULL, (byte*)abc##name##Data), \
BAD_FUNC_ARG); \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
ExpectIntEQ(wc_##name##Transform(&dgst, (byte*)abc##name##Data), 0); \
ExpectBufEQ((byte*)dgst.digest, (byte*)abcHash, WC_##upper##_DIGEST_SIZE); \
@ -564,10 +667,12 @@ do { \
ExpectIntEQ(wc_Init##name(&dgst), 0); \
\
/* Test bad args. */ \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(NULL, (byte*)abc##name##Data), \
BAD_FUNC_ARG); \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
ExpectIntEQ(wc_##name##Transform(&dgst, (byte*)abcData), 0); \
ExpectIntEQ(wc_##name##FinalRaw(&dgst, hash), 0); \
@ -587,10 +692,12 @@ do { \
ExpectIntEQ(wc_Init##name(&dgst), 0); \
\
/* Test bad args. */ \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(NULL, (byte*)abc##name##Data), \
BAD_FUNC_ARG); \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
ByteReverseWords((word32*)abc##name##DataBE, (word32*)abc##name##Data, \
WC_##upper##_BLOCK_SIZE); \
@ -613,10 +720,12 @@ do { \
ExpectIntEQ(wc_Init##name(&dgst), 0); \
\
/* Test bad args. */ \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(NULL, (byte*)abc##name##Data), \
BAD_FUNC_ARG); \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
ByteReverseWords((word32*)abc##name##DataBE, (word32*)abc##name##Data, \
WC_##upper##_BLOCK_SIZE); \
@ -641,10 +750,12 @@ do { \
ExpectIntEQ(wc_Init##name(&dgst), 0); \
\
/* Test bad args. */ \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), BAD_FUNC_ARG); \
ExpectIntEQ(wc_##name##Transform(NULL, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(&dgst, NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Transform(NULL, (byte*)abc##name##Data), \
BAD_FUNC_ARG); \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
ExpectIntEQ(wc_##name##Transform(&dgst, (byte*)abcData), 0); \
ExpectIntEQ(wc_##name##FinalRaw(&dgst, hash), 0); \
@ -714,3 +825,62 @@ do { \
wc_##inst##_Free(&dgst_copy); \
wc_##inst##_Free(&dgst)
#define DIGEST_HASH_TEST(name, upper) \
do { \
byte data[WC_##upper##_BLOCK_SIZE]; \
byte hash[WC_##upper##_DIGEST_SIZE]; \
\
XMEMSET(data, 0xa5, sizeof(data)); \
\
/* Invalid parameters. */ \
ExpectIntEQ(wc_##name##Hash(NULL, sizeof(data), hash), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Hash(data, sizeof(data), NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Hash_ex(NULL, sizeof(data), hash, HEAP_HINT, \
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Hash_ex(data, sizeof(data), NULL, HEAP_HINT, \
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
/* Valid parameters. */ \
ExpectIntEQ(wc_##name##Hash(data, sizeof(data), hash), 0); \
ExpectIntEQ(wc_##name##Hash_ex(data, sizeof(data), hash, HEAP_HINT, \
INVALID_DEVID), 0); \
} while (0)
#define DIGEST_COUNT_HASH_TEST(name, upper) \
do { \
byte data[WC_##upper##_COUNT * 8]; \
byte hash[WC_##upper##_COUNT * 8]; \
\
XMEMSET(data, 0xa5, sizeof(data)); \
\
/* Invalid parameters. */ \
ExpectIntEQ(wc_##name##Hash(NULL, sizeof(data), hash), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Hash(data, sizeof(data), NULL), \
WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Hash_ex(NULL, sizeof(data), hash, HEAP_HINT, \
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
ExpectIntEQ(wc_##name##Hash_ex(data, sizeof(data), NULL, HEAP_HINT, \
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); \
\
/* Valid parameters. */ \
ExpectIntEQ(wc_##name##Hash(data, sizeof(data), hash), 0); \
ExpectIntEQ(wc_##name##Hash_ex(data, sizeof(data), hash, HEAP_HINT, \
INVALID_DEVID), 0); \
} while (0)
#define DIGEST_HASH_ONLY_TEST(name, upper) \
byte data[WC_##upper##_BLOCK_SIZE]; \
byte hash[WC_##upper##_DIGEST_SIZE]; \
\
XMEMSET(data, 0xa5, sizeof(data)); \
\
/* Invalid parameters. */ \
ExpectIntEQ(wc_##name##Hash(NULL, sizeof(data), hash), 0); \
ExpectIntEQ(wc_##name##Hash(data, sizeof(data), NULL), 0); \
\
/* Valid parameters. */ \
ExpectIntEQ(wc_##name##Hash(data, sizeof(data), hash), 0)

View File

@ -40,119 +40,549 @@
#include <tests/unit.h>
#include <tests/api/api.h>
#include <tests/api/test_hash.h>
#include <tests/api/test_digest.h>
#ifndef NO_HASH_WRAPPER
/* enum for holding supported algorithms, #ifndef's restrict if disabled */
static const enum wc_HashType supportedHash[] = {
#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,
#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
#ifndef WOLFSSL_NOSHA512_224
WC_HASH_TYPE_SHA512_224,
#endif
#ifndef WOLFSSL_NOSHA512_256
WC_HASH_TYPE_SHA512_256,
#endif
#endif
#endif
#ifdef WOLFSSL_SHA3
WC_HASH_TYPE_SHA3_224,
WC_HASH_TYPE_SHA3_256,
WC_HASH_TYPE_SHA3_384,
WC_HASH_TYPE_SHA3_512,
#endif
#ifdef WOLFSSL_SM3
WC_HASH_TYPE_SM3,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int supportedHashLen = (sizeof(supportedHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType notCompiledHash[] = {
#ifdef NO_MD5
WC_HASH_TYPE_MD5,
#endif
#ifdef NO_SHA
WC_HASH_TYPE_SHA,
#endif
#ifndef WOLFSSL_SHA224
WC_HASH_TYPE_SHA224,
#endif
#ifdef NO_SHA256
WC_HASH_TYPE_SHA256,
#endif
#ifndef WOLFSSL_SHA384
WC_HASH_TYPE_SHA384,
#endif
#ifndef WOLFSSL_SHA512
WC_HASH_TYPE_SHA512,
#endif
#ifndef WOLFSSL_SHA3
WC_HASH_TYPE_SHA3_224,
WC_HASH_TYPE_SHA3_256,
WC_HASH_TYPE_SHA3_384,
WC_HASH_TYPE_SHA3_512,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int notCompiledHashLen = (sizeof(notCompiledHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType notSupportedHash[] = {
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
WC_HASH_TYPE_SHAKE128,
#endif
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
WC_HASH_TYPE_SHAKE256,
#endif
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
};
static const int notSupportedHashLen = (sizeof(notSupportedHash) /
sizeof(enum wc_HashType));
static const enum wc_HashType sizeSupportedHash[] = {
#if !defined(NO_MD5) && !defined(NO_SHA)
WC_HASH_TYPE_MD5_SHA,
#endif
#ifdef WOLFSSL_MD2
WC_HASH_TYPE_MD2,
#endif
#ifndef NO_MD4
WC_HASH_TYPE_MD4,
#endif
#if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
WC_HASH_TYPE_BLAKE2B,
WC_HASH_TYPE_BLAKE2S,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int sizeSupportedHashLen = (sizeof(sizeSupportedHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType sizeNotCompiledHash[] = {
#if defined(NO_MD5) || defined(NO_SHA)
WC_HASH_TYPE_MD5_SHA,
#endif
#ifndef WOLFSSL_MD2
WC_HASH_TYPE_MD2,
#endif
#ifdef NO_MD4
WC_HASH_TYPE_MD4,
#endif
#if !defined(HAVE_BLAKE2) && !defined(HAVE_BLAKE2S)
WC_HASH_TYPE_BLAKE2B,
WC_HASH_TYPE_BLAKE2S,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int sizeNotCompiledHashLen = (sizeof(sizeNotCompiledHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType sizeNotSupportedHash[] = {
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
WC_HASH_TYPE_SHAKE128,
#endif
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
WC_HASH_TYPE_SHAKE256,
#endif
WC_HASH_TYPE_NONE
};
static const int sizeNotSupportedHashLen = (sizeof(sizeNotSupportedHash) /
sizeof(enum wc_HashType));
#endif /* NO_HASH_WRAPPER */
int test_wc_HashInit(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
wc_HashAlg hash;
int i; /* 0 indicates tests passed, 1 indicates failure */
wc_HashAlg hash;
/* 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
};
/* dynamically finds the length */
int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
/* For loop to test various arguments... */
for (i = 0; i < enumlen; i++) {
/* check for bad args */
ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
wc_HashFree(&hash, enumArray[i]);
for (i = 0; i < supportedHashLen; i++) {
/* check for null ptr */
ExpectIntEQ(wc_HashInit(NULL, enumArray[i]),
ExpectIntEQ(wc_HashInit(NULL, supportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit_ex(NULL, supportedHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
wc_HashFree(&hash, supportedHash[i]);
ExpectIntEQ(wc_HashInit_ex(&hash, supportedHash[i], HEAP_HINT,
INVALID_DEVID), 0);
wc_HashFree(&hash, supportedHash[i]);
wc_HashFree(NULL, supportedHash[i]);
} /* end of for loop */
for (i = 0; i < notCompiledHashLen; i++) {
/* check for null ptr */
ExpectIntEQ(wc_HashInit(NULL, notCompiledHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit_ex(NULL, notCompiledHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashInit_ex(&hash, notCompiledHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(HASH_TYPE_E));
wc_HashFree(NULL, notCompiledHash[i]);
}
for (i = 0; i < notSupportedHashLen; i++) {
/* check for null ptr */
ExpectIntEQ(wc_HashInit(NULL, supportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit_ex(NULL, supportedHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, notSupportedHash[i]);
ExpectIntEQ(wc_HashInit_ex(&hash, notSupportedHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, notSupportedHash[i]);
wc_HashFree(NULL, notSupportedHash[i]);
} /* end of for loop */
#endif
return EXPECT_RESULT();
} /* end of test_wc_HashInit */
int test_wc_HashUpdate(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
wc_HashAlg hash;
int i; /* 0 indicates tests passed, 1 indicates failure */
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
/* Invalid parameters */
ExpectIntEQ(wc_HashUpdate(NULL, supportedHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, supportedHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, supportedHash[i], NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, supportedHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, supportedHash[i], NULL, 0), 0);
ExpectIntEQ(wc_HashUpdate(&hash, supportedHash[i], (byte*)"a", 1), 0);
wc_HashFree(&hash, supportedHash[i]);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
/* Invalid parameters */
ExpectIntEQ(wc_HashUpdate(NULL, notCompiledHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notCompiledHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, notCompiledHash[i], NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, notCompiledHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notCompiledHash[i], NULL, 0),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashUpdate(&hash, notCompiledHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(HASH_TYPE_E));
wc_HashFree(&hash, notCompiledHash[i]);
}
for (i = 0; i < notSupportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Invalid parameters */
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, notSupportedHash[i]);
}
#if defined(DEBUG_WOLFSSL) && !defined(NO_SHA256) && defined(WOLFSSL_SHA512)
ExpectIntEQ(wc_HashInit(&hash, WC_HASH_TYPE_SHA256), 0);
ExpectIntEQ(wc_HashUpdate(&hash, WC_HASH_TYPE_SHA512, NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, WC_HASH_TYPE_SHA512, (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#endif
#endif
return EXPECT_RESULT();
}
int test_wc_HashFinal(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
wc_HashAlg hash;
byte digest[WC_MAX_DIGEST_SIZE];
int i; /* 0 indicates tests passed, 1 indicates failure */
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
/* Invalid parameters */
ExpectIntEQ(wc_HashFinal(NULL, supportedHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, supportedHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(NULL, supportedHash[i], digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, supportedHash[i], digest), 0);
wc_HashFree(&hash, supportedHash[i]);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
/* Invalid parameters */
ExpectIntEQ(wc_HashFinal(NULL, notCompiledHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, notCompiledHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(NULL, notCompiledHash[i], digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, notCompiledHash[i], digest),
WC_NO_ERR_TRACE(HASH_TYPE_E));
wc_HashFree(&hash, notCompiledHash[i]);
}
for (i = 0; i < notSupportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Invalid parameters */
ExpectIntEQ(wc_HashFinal(NULL, notSupportedHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, notSupportedHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(NULL, notSupportedHash[i], digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, notSupportedHash[i], digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, notSupportedHash[i]);
}
#if defined(DEBUG_WOLFSSL) && !defined(NO_SHA256) && defined(WOLFSSL_SHA512)
ExpectIntEQ(wc_HashInit(&hash, WC_HASH_TYPE_SHA256), 0);
ExpectIntEQ(wc_HashFinal(&hash, WC_HASH_TYPE_SHA512, digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#endif
#endif
return EXPECT_RESULT();
}
int test_wc_HashNewDelete(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && !defined(WC_NO_CONSTRUCTORS)
wc_HashAlg* hash;
byte digest[WC_MAX_DIGEST_SIZE];
int ret;
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectNotNull(hash = wc_HashNew(supportedHash[i], HEAP_HINT,
INVALID_DEVID, &ret));
ExpectIntEQ(ret, 0);
ExpectIntEQ(wc_HashUpdate(hash, supportedHash[i], (byte*)"a", 1), 0);
ExpectIntEQ(wc_HashFinal(hash, supportedHash[i], digest), 0);
ExpectIntEQ(wc_HashDelete(hash, &hash), 0);
ExpectNull(hash);
ExpectNotNull(hash = wc_HashNew(supportedHash[i], HEAP_HINT,
INVALID_DEVID, &ret));
ExpectIntEQ(ret, 0);
ExpectIntEQ(wc_HashDelete(hash, NULL), 0);
ExpectIntEQ(wc_HashDelete(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectNull(wc_HashNew(notCompiledHash[i], HEAP_HINT, INVALID_DEVID,
&ret));
ExpectIntEQ(ret, WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < notSupportedHashLen; i++) {
ExpectNull(wc_HashNew(notSupportedHash[i], HEAP_HINT, INVALID_DEVID,
&ret));
ExpectIntEQ(ret, WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
int test_wc_HashGetDigestSize(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectIntGT(wc_HashGetDigestSize(supportedHash[i]), 0);
}
for (i = 0; i < sizeSupportedHashLen; i++) {
ExpectIntGT(wc_HashGetDigestSize(sizeSupportedHash[i]), 0);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetDigestSize(notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetDigestSize(sizeNotCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotSupportedHashLen; i++) {
ExpectIntEQ(wc_HashGetDigestSize(sizeNotSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
int test_wc_HashGetBlockSize(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectIntGT(wc_HashGetBlockSize(supportedHash[i]), 0);
}
for (i = 0; i < sizeSupportedHashLen; i++) {
ExpectIntGT(wc_HashGetBlockSize(sizeSupportedHash[i]), 0);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetBlockSize(notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetBlockSize(sizeNotCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotSupportedHashLen; i++) {
ExpectIntEQ(wc_HashGetBlockSize(sizeNotSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
int test_wc_Hash(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && !defined(WC_NO_CONSTRUCTORS)
byte digest[WC_MAX_DIGEST_SIZE];
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_Hash(supportedHash[i], (byte*)"a", 1,
digest, sizeof(digest)), 0);
ExpectIntEQ(wc_Hash_ex(supportedHash[i], (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID), 0);
}
#if !defined(NO_MD5) && !defined(NO_SHA)
ExpectIntEQ(wc_Hash(WC_HASH_TYPE_MD5_SHA, (byte*)"a", 1,
digest, sizeof(digest)), 0);
ExpectIntEQ(wc_Hash_ex(WC_HASH_TYPE_MD5_SHA, (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID), 0);
#endif
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_Hash(notCompiledHash[i], (byte*)"a", 1,
digest, sizeof(digest)), WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_Hash_ex(notCompiledHash[i], (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotCompiledHashLen; i++) {
ExpectIntEQ(wc_Hash(sizeNotCompiledHash[i], (byte*)"a", 1,
digest, sizeof(digest)), WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_Hash_ex(sizeNotCompiledHash[i], (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotSupportedHashLen; i++) {
if (notSupportedHash[i] == WC_HASH_TYPE_MD5_SHA) {
/* Algorithm only supported with wc_Hash() and wc_Hash_ex(). */
continue;
}
ExpectIntEQ(wc_Hash(sizeNotSupportedHash[i], (byte*)"a", 1,
digest, sizeof(digest)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Hash_ex(sizeNotSupportedHash[i], (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
/*
* Unit test function for wc_HashSetFlags()
*/
int test_wc_HashSetFlags(void)
{
EXPECT_DECLS;
#ifdef WOLFSSL_HASH_FLAGS
#if !defined(NO_HASH_WRAPPER) && defined(WOLFSSL_HASH_FLAGS)
wc_HashAlg hash;
word32 flags = 0;
int i, j;
int notSupportedLen;
int i;
/* 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++) {
ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
ExpectIntEQ(wc_HashSetFlags(&hash, supportedHash[i], flags), 0);
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags),
ExpectIntEQ(wc_HashSetFlags(NULL, supportedHash[i], flags),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, enumArray[i]);
wc_HashFree(&hash, supportedHash[i]);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashSetFlags(&hash, notCompiledHash[i], flags),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashFree(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
/* For loop to test not supported cases */
notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
for (j = 0; j < notSupportedLen; j++) {
ExpectIntEQ(wc_HashInit(&hash, notSupported[j]),
for (i = 0; i < notSupportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
ExpectIntEQ(wc_HashSetFlags(&hash, notSupportedHash[i], flags),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFree(&hash, notSupported[j]),
ExpectIntEQ(wc_HashFree(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
@ -165,66 +595,230 @@ int test_wc_HashSetFlags(void)
int test_wc_HashGetFlags(void)
{
EXPECT_DECLS;
#ifdef WOLFSSL_HASH_FLAGS
#if !defined(NO_HASH_WRAPPER) && defined(WOLFSSL_HASH_FLAGS)
wc_HashAlg hash;
word32 flags = 0;
int i, j;
/* 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));
int notSupportedLen;
int i;
/* For loop to test various arguments... */
for (i = 0; i < enumlen; i++) {
ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
ExpectIntEQ(wc_HashGetFlags(&hash, supportedHash[i], &flags), 0);
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags),
ExpectIntEQ(wc_HashGetFlags(NULL, supportedHash[i], &flags),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, enumArray[i]);
wc_HashFree(&hash, supportedHash[i]);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashGetFlags(&hash, notCompiledHash[i], &flags),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashFree(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
/* For loop to test not supported cases */
notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
for (j = 0; j < notSupportedLen; j++) {
ExpectIntEQ(wc_HashInit(&hash, notSupported[j]),
for (i = 0; i < notSupportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
ExpectIntEQ(wc_HashGetFlags(&hash, notSupportedHash[i], &flags),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFree(&hash, notSupported[j]),
ExpectIntEQ(wc_HashFree(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
} /* END test_wc_HashGetFlags */
int test_wc_Hash_Algs(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
#ifndef NO_MD5
DIGEST_HASH_TEST(Md5, MD5);
#endif
#ifndef NO_SHA
DIGEST_HASH_TEST(Sha, SHA);
#endif
#ifdef WOLFSSL_SHA224
DIGEST_HASH_TEST(Sha224, SHA224);
#endif
#ifndef NO_SHA256
DIGEST_HASH_TEST(Sha256, SHA256);
#endif
#ifdef WOLFSSL_SHA384
DIGEST_HASH_TEST(Sha384, SHA384);
#endif
#ifdef WOLFSSL_SHA512
DIGEST_HASH_TEST(Sha512, SHA512);
#ifndef WOLFSSL_NOSHA512_224
DIGEST_HASH_TEST(Sha512_224, SHA512_224);
#endif
#ifndef WOLFSSL_NOSHA512_256
DIGEST_HASH_TEST(Sha512_256, SHA512_256);
#endif
#endif /* WOLFSSL_SHA512 */
#ifdef WOLFSSL_SHA3
DIGEST_COUNT_HASH_TEST(Sha3_224, SHA3_224);
DIGEST_COUNT_HASH_TEST(Sha3_256, SHA3_256);
DIGEST_COUNT_HASH_TEST(Sha3_384, SHA3_384);
DIGEST_COUNT_HASH_TEST(Sha3_512, SHA3_512);
#endif
#ifdef WOLFSSL_SM3
DIGEST_HASH_TEST(Sm3, SM3);
#endif
#endif /* !NO_HASH_WRAPPER */
return EXPECT_RESULT();
}
int test_wc_HashGetOID(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && (!defined(NO_ASN) || !defined(NO_DH) || \
defined(HAVE_ECC))
static const enum wc_HashType oidOnlySupportedHash[] = {
#ifdef WOLFSSL_MD2
WC_HASH_TYPE_MD2,
#endif
#ifndef NO_MD5
WC_HASH_TYPE_MD5_SHA,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int oidOnlySupportedHashLen = (sizeof(oidOnlySupportedHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType oidOnlyNotCompiledHash[] = {
#ifndef WOLFSSL_MD2
WC_HASH_TYPE_MD2,
#endif
#ifdef NO_MD5
WC_HASH_TYPE_MD5_SHA,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int oidOnlyNotCompiledHashLen =
(sizeof(oidOnlyNotCompiledHash) / sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType oidNotSupportedHash[] = {
WC_HASH_TYPE_MD4,
WC_HASH_TYPE_BLAKE2B,
WC_HASH_TYPE_BLAKE2S,
WC_HASH_TYPE_NONE
};
static const int oidNotSupportedHashLen = (sizeof(oidNotSupportedHash) /
sizeof(enum wc_HashType));
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectIntGT(wc_HashGetOID(supportedHash[i]), 0);
}
for (i = 0; i < oidOnlySupportedHashLen; i++) {
ExpectIntGT(wc_HashGetOID(oidOnlySupportedHash[i]), 0);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetOID(notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < oidOnlyNotCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetOID(oidOnlyNotCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < oidNotSupportedHashLen; i++) {
ExpectIntEQ(wc_HashGetOID(oidNotSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
int test_wc_OidGetHash(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && !defined(NO_ASN)
static const int sumSupportedHash[] = {
#ifdef WOLFSSL_MD2
MD2h,
#endif
#ifndef NO_MD5
MD5h,
#endif
#ifndef NO_SHA
SHAh,
#endif
#ifdef WOLFSSL_SHA224
SHA224h,
#endif
#ifndef NO_SHA256
SHA256h,
#endif
#ifdef WOLFSSL_SHA384
SHA384h,
#endif
#ifdef WOLFSSL_SHA512
SHA512h,
#endif
#ifdef WOLFSSL_SHA3
SHA3_224h,
SHA3_256h,
SHA3_384h,
SHA3_512h,
#endif
#ifdef WOLFSSL_SM3
SM3h,
#endif
0 /* Dummy value to ensure list is non-zero. */
};
static const int sumSupportedHashLen = (sizeof(sumSupportedHash) /
sizeof(enum wc_HashType)) - 1;
static const int sumNotSupportedHash[] = {
MD4h,
#ifdef NO_MD5
MD5h,
#endif
#ifdef NO_SHA
SHAh,
#endif
#ifndef WOLFSSL_SHA224
SHA224h,
#endif
#ifdef NO_SHA256
SHA256h,
#endif
#ifndef WOLFSSL_SHA384
SHA384h,
#endif
#ifndef WOLFSSL_SHA512
SHA512h,
#endif
#ifndef WOLFSSL_SHA3
SHA3_224h,
SHA3_256h,
SHA3_384h,
SHA3_512h,
#endif
#ifndef WOLFSSL_SM3
SM3h,
#endif
0
};
static const int sumNotSupportedHashLen = (sizeof(sumNotSupportedHash) /
sizeof(enum wc_HashType));
int i;
enum wc_HashType hash;
for (i = 0; i < sumSupportedHashLen; i++) {
hash = wc_OidGetHash(sumSupportedHash[i]);
ExpectTrue(hash != WC_HASH_TYPE_NONE);
}
for (i = 0; i < sumNotSupportedHashLen; i++) {
hash = wc_OidGetHash(sumNotSupportedHash[i]);
ExpectTrue(hash == WC_HASH_TYPE_NONE);
}
#endif
return EXPECT_RESULT();
}

View File

@ -23,7 +23,16 @@
#define WOLFCRYPT_TEST_HASH_H
int test_wc_HashInit(void);
int test_wc_HashUpdate(void);
int test_wc_HashFinal(void);
int test_wc_HashNewDelete(void);
int test_wc_HashGetDigestSize(void);
int test_wc_HashGetBlockSize(void);
int test_wc_Hash(void);
int test_wc_HashSetFlags(void);
int test_wc_HashGetFlags(void);
int test_wc_Hash_Algs(void);
int test_wc_HashGetOID(void);
int test_wc_OidGetHash(void);
#endif /* WOLFCRYPT_TEST_HASH_H */

135
tests/api/test_md2.c Normal file
View File

@ -0,0 +1,135 @@
/* test_md2.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
#include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/wolfcrypt/md2.h>
#include <wolfssl/wolfcrypt/types.h>
#include <tests/unit.h>
#include <tests/api/api.h>
#include <tests/api/test_md2.h>
#include <tests/api/test_digest.h>
/* Unit test for wc_InitMd2() and wc_InitMd2_ex() */
int test_wc_InitMd2(void)
{
EXPECT_SUCCESS_DECLS;
#ifdef WOLFSSL_MD2
DIGEST_INIT_ONLY_TEST(wc_Md2, Md2);
#endif
return EXPECT_RESULT();
}
/* Unit test for wc_UpdateMd2() */
int test_wc_Md2Update(void)
{
EXPECT_SUCCESS_DECLS;
#ifdef WOLFSSL_MD2
DIGEST_UPDATE_ONLY_TEST(wc_Md2, Md2);
#endif
return EXPECT_RESULT();
}
/* Unit test for wc_Md2Final() */
int test_wc_Md2Final(void)
{
EXPECT_SUCCESS_DECLS;
#ifdef WOLFSSL_MD2
DIGEST_FINAL_ONLY_TEST(wc_Md2, Md2, MD2);
#endif
return EXPECT_RESULT();
}
#define MD2_KAT_CNT 7
int test_wc_Md2_KATs(void)
{
EXPECT_DECLS;
#ifdef WOLFSSL_MD2
DIGEST_KATS_TEST_VARS(wc_Md2, MD2);
/* From RFC 1321. */
DIGEST_KATS_ADD("", 0,
"\x83\x50\xe5\xa3\xe2\x4c\x15\x3d"
"\xf2\x27\x5c\x9f\x80\x69\x27\x73" );
DIGEST_KATS_ADD("a", 1,
"\x32\xec\x01\xec\x4a\x6d\xac\x72"
"\xc0\xab\x96\xfb\x34\xc0\xb5\xd1");
DIGEST_KATS_ADD("abc", 3,
"\xda\x85\x3b\x0d\x3f\x88\xd9\x9b"
"\x30\x28\x3a\x69\xe6\xde\xd6\xbb");
DIGEST_KATS_ADD("message digest", 14,
"\xab\x4f\x49\x6b\xfb\x2a\x53\x0b"
"\x21\x9f\xf3\x30\x31\xfe\x06\xb0");
DIGEST_KATS_ADD("abcdefghijklmnopqrstuvwxyz", 26,
"\x4e\x8d\xdf\xf3\x65\x02\x92\xab"
"\x5a\x41\x08\xc3\xaa\x47\x94\x0b");
DIGEST_KATS_ADD("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
"0123456789", 62,
"\xda\x33\xde\xf2\xa4\x2d\xf1\x39"
"\x75\x35\x28\x46\xc3\x03\x38\xcd");
DIGEST_KATS_ADD("1234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890", 80,
"\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d"
"\xc9\x80\x6c\x3c\x66\xf3\xef\xd8");
DIGEST_KATS_ONLY_TEST(Md2, MD2);
#endif
return EXPECT_RESULT();
}
int test_wc_Md2_other(void)
{
EXPECT_DECLS;
#ifdef WOLFSSL_MD2
DIGEST_OTHER_ONLY_TEST(wc_Md2, Md2, MD2,
"\xa3\x0c\xa1\xdd\xfa\xd0\x7c\x97"
"\x58\xfd\xe2\x53\xf0\xa1\xb0\x6d");
#endif
return EXPECT_RESULT();
}
/*
* Testing wc_Md2Hash()
*/
int test_wc_Md2Hash(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_MD2)
DIGEST_HASH_ONLY_TEST(Md2, MD2);
#endif
return EXPECT_RESULT();
} /* END test_wc_Sm3Hash */

32
tests/api/test_md2.h Normal file
View File

@ -0,0 +1,32 @@
/* test_md2.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_MD2_H
#define WOLFCRYPT_TEST_MD2_H
int test_wc_InitMd2(void);
int test_wc_Md2Update(void);
int test_wc_Md2Final(void);
int test_wc_Md2_KATs(void);
int test_wc_Md2_other(void);
int test_wc_Md2Hash(void);
#endif /* WOLFCRYPT_TEST_MD2_H */

123
tests/api/test_md4.c Normal file
View File

@ -0,0 +1,123 @@
/* test_md4.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if !defined(WOLFSSL_USER_SETTINGS) && !defined(WOLFSSL_NO_OPTIONS_H)
#include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/wolfcrypt/md4.h>
#include <wolfssl/wolfcrypt/types.h>
#include <tests/unit.h>
#include <tests/api/api.h>
#include <tests/api/test_md4.h>
#include <tests/api/test_digest.h>
/* Unit test for wc_InitMd4() and wc_InitMd4_ex() */
int test_wc_InitMd4(void)
{
EXPECT_SUCCESS_DECLS;
#ifndef NO_MD4
DIGEST_INIT_ONLY_TEST(wc_Md4, Md4);
#endif
return EXPECT_RESULT();
}
/* Unit test for wc_UpdateMd4() */
int test_wc_Md4Update(void)
{
EXPECT_SUCCESS_DECLS;
#ifndef NO_MD4
DIGEST_UPDATE_ONLY_TEST(wc_Md4, Md4);
#endif
return EXPECT_RESULT();
}
/* Unit test for wc_Md4Final() */
int test_wc_Md4Final(void)
{
EXPECT_SUCCESS_DECLS;
#ifndef NO_MD4
DIGEST_FINAL_ONLY_TEST(wc_Md4, Md4, MD4);
#endif
return EXPECT_RESULT();
}
#define MD4_KAT_CNT 7
int test_wc_Md4_KATs(void)
{
EXPECT_DECLS;
#ifndef NO_MD4
DIGEST_KATS_TEST_VARS(wc_Md4, MD4);
/* From RFC 1321. */
DIGEST_KATS_ADD("", 0,
"\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31"
"\xb7\x3c\x59\xd7\xe0\xc0\x89\xc0");
DIGEST_KATS_ADD("a", 1,
"\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46"
"\x24\x5e\x05\xfb\xdb\xd6\xfb\x24");
DIGEST_KATS_ADD("abc", 3,
"\xa4\x48\x01\x7a\xaf\x21\xd8\x52"
"\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d");
DIGEST_KATS_ADD("message digest", 14,
"\xd9\x13\x0a\x81\x64\x54\x9f\xe8"
"\x18\x87\x48\x06\xe1\xc7\x01\x4b");
DIGEST_KATS_ADD("abcdefghijklmnopqrstuvwxyz", 26,
"\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd"
"\xee\xa8\xed\x63\xdf\x41\x2d\xa9");
DIGEST_KATS_ADD("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
"0123456789", 62,
"\x04\x3f\x85\x82\xf2\x41\xdb\x35"
"\x1c\xe6\x27\xe1\x53\xe7\xf0\xe4");
DIGEST_KATS_ADD("1234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890", 80,
"\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19"
"\x9c\x3e\x7b\x16\x4f\xcc\x05\x36");
DIGEST_KATS_ONLY_TEST(Md4, MD4);
#endif
return EXPECT_RESULT();
}
int test_wc_Md4_other(void)
{
EXPECT_DECLS;
#ifndef NO_MD4
DIGEST_OTHER_ONLY_TEST(wc_Md4, Md4, MD4,
"\x1b\x60\x7d\x08\x57\x0c\xf1\x52"
"\xbb\x44\x55\x97\x73\x26\x95\x6d");
#endif
return EXPECT_RESULT();
}

31
tests/api/test_md4.h Normal file
View File

@ -0,0 +1,31 @@
/* test_md2.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_MD4_H
#define WOLFCRYPT_TEST_MD4_H
int test_wc_InitMd4(void);
int test_wc_Md4Update(void);
int test_wc_Md4Final(void);
int test_wc_Md4_KATs(void);
int test_wc_Md4_other(void);
#endif /* WOLFCRYPT_TEST_MD4_H */

View File

@ -423,25 +423,3 @@ int test_wc_Sm3_Flags(void)
return EXPECT_RESULT();
}
/*
* Testing wc_Sm3Hash()
*/
int test_wc_Sm3Hash(void)
{
EXPECT_DECLS;
#if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
byte data[WC_SM3_BLOCK_SIZE];
byte hash[WC_SM3_DIGEST_SIZE];
/* Invalid parameters. */
ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Valid parameters. */
ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
#endif
return EXPECT_RESULT();
} /* END test_wc_Sm3Hash */

View File

@ -31,6 +31,5 @@ int test_wc_Sm3_other(void);
int test_wc_Sm3Copy(void);
int test_wc_Sm3GetHash(void);
int test_wc_Sm3_Flags(void);
int test_wc_Sm3Hash(void);
#endif /* WOLFCRYPT_TEST_SM3_H */

View File

@ -141,6 +141,8 @@
#define EXPECT_DECLS \
int _ret = TEST_SKIPPED, _fail_codepoint_id = TEST_FAIL
#define EXPECT_SUCCESS_DECLS \
int _ret = TEST_SUCCESS, _fail_codepoint_id = TEST_SUCCESS
#define EXPECT_DECLS_NO_MSGS(fail_codepoint_offset) \
int _ret = TEST_SKIPPED_NO_MSGS, \
_fail_codepoint_id = (fail_codepoint_offset)

View File

@ -42,6 +42,9 @@
void wc_InitMd2(wc_Md2* md2)
{
if (md2 == NULL)
return;
XMEMSET(md2->X, 0, WC_MD2_X_SIZE);
XMEMSET(md2->C, 0, WC_MD2_BLOCK_SIZE);
XMEMSET(md2->buffer, 0, WC_MD2_BLOCK_SIZE);
@ -73,6 +76,9 @@ void wc_Md2Update(wc_Md2* md2, const byte* data, word32 len)
31, 26, 219, 153, 141, 51, 159, 17, 131, 20
};
if (md2 == NULL || (data == NULL && len != 0))
return;
while (len) {
word32 L = (WC_MD2_PAD_SIZE - md2->count) < len ?
(WC_MD2_PAD_SIZE - md2->count) : len;
@ -117,9 +123,13 @@ void wc_Md2Update(wc_Md2* md2, const byte* data, word32 len)
void wc_Md2Final(wc_Md2* md2, byte* hash)
{
byte padding[WC_MD2_BLOCK_SIZE];
word32 padLen = WC_MD2_PAD_SIZE - md2->count;
word32 padLen;
word32 i;
if (md2 == NULL || hash == NULL)
return;
padLen = WC_MD2_PAD_SIZE - md2->count;
for (i = 0; i < padLen; i++)
padding[i] = (byte)padLen;

View File

@ -39,6 +39,9 @@
void wc_InitMd4(wc_Md4* md4)
{
if (md4 == NULL)
return;
md4->digest[0] = 0x67452301L;
md4->digest[1] = 0xefcdab89L;
md4->digest[2] = 0x98badcfeL;
@ -141,8 +144,12 @@ static WC_INLINE void AddLength(wc_Md4* md4, word32 len)
void wc_Md4Update(wc_Md4* md4, const byte* data, word32 len)
{
/* do block size increments */
byte* local = (byte*)md4->buffer;
byte* local;
if (md4 == NULL || (data == NULL && len != 0))
return;
local = (byte*)md4->buffer;
while (len) {
word32 add = min(len, WC_MD4_BLOCK_SIZE - md4->buffLen);
XMEMCPY(&local[md4->buffLen], data, add);
@ -165,8 +172,12 @@ void wc_Md4Update(wc_Md4* md4, const byte* data, word32 len)
void wc_Md4Final(wc_Md4* md4, byte* hash)
{
byte* local = (byte*)md4->buffer;
byte* local;
if (md4 == NULL || hash == NULL)
return;
local = (byte*)md4->buffer;
AddLength(md4, md4->buffLen); /* before adding pads */
local[md4->buffLen++] = 0x80; /* add 1 */