From d2245b9614dff052e88de41f2ea4f9091a30e6dc Mon Sep 17 00:00:00 2001 From: jrblixt Date: Mon, 5 Jun 2017 11:49:33 -0600 Subject: [PATCH] Unit test functions for HC128. --- tests/api.c | 95 +++++++++++++++++++++++++++++++++++++++++++ wolfcrypt/src/hc128.c | 8 ++++ 2 files changed, 103 insertions(+) diff --git a/tests/api.c b/tests/api.c index c126b44ef..e1a7f147e 100644 --- a/tests/api.c +++ b/tests/api.c @@ -118,6 +118,10 @@ #include #endif +#ifdef HAVE_HC128 + #include +#endif + #ifdef OPENSSL_EXTRA #include #include @@ -8195,6 +8199,95 @@ static int test_wc_AesCcmEncryptDecrypt (void) +/* + * Test wc_Hc128_SetKey() + */ +static int test_wc_Hc128_SetKey (void) +{ +#ifdef HAVE_HC128 + HC128 ctx; + const char* key = "\x80\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00"; + const char* iv = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE" + "\x45\xAC\x13\x7A\xE1\x48\xAF\x16"; + int ret; + + printf(testingFmt, "c_Hc128_SetKey()"); + ret = wc_Hc128_SetKey(&ctx, (byte*)key, (byte*)iv); + /* Test bad args. */ + if (ret == 0) { + ret = wc_Hc128_SetKey(NULL, (byte*)key, (byte*)iv); + if (ret == BAD_FUNC_ARG) { + ret = wc_Hc128_SetKey(&ctx, NULL, (byte*)iv); + } + if (ret == BAD_FUNC_ARG) { + ret = wc_Hc128_SetKey(&ctx, (byte*)key, NULL); + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + + +#endif + return 0; + +} /* END test_wc_Hc128_SetKey */ + +/* + * Testing wc_Hc128_Process() + */ +static int test_wc_Hc128_Process (void) +{ +#ifdef HAVE_HC128 + HC128 enc; + HC128 dec; + const char* key = "\x0F\x62\xB5\x08\x5B\xAE\x01\x54" + "\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"; + const char* input = "Encrypt Hc128, and then Decrypt."; + size_t inlen = XSTRLEN(input); + byte cipher[inlen]; + byte plain[inlen]; + int ret; + + printf(testingFmt, "wc_Hc128_Process()"); + ret = wc_Hc128_SetKey(&enc, (byte*)key, NULL); + if (ret == 0) { + ret = wc_Hc128_SetKey(&dec, (byte*)key, NULL); + } + if (ret == 0) { + ret = wc_Hc128_Process(&enc, cipher, (byte*)input, (word32)inlen); + if (ret == 0) { + ret = wc_Hc128_Process(&dec, plain, cipher, (word32)inlen); + } + } + + /* Bad args. */ + if (ret == 0) { + ret = wc_Hc128_Process(NULL, plain, cipher, (word32)inlen); + if (ret == BAD_FUNC_ARG) { + ret = wc_Hc128_Process(&dec, NULL, cipher, (word32)inlen); + } + if (ret == BAD_FUNC_ARG) { + ret = wc_Hc128_Process(&dec, plain, NULL, (word32)inlen); + } + if (ret == BAD_FUNC_ARG) { + ret = 0; + } else { + ret = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + + #endif + return 0; + +} /* END test_wc_Hc128_Process */ + + + + + /*----------------------------------------------------------------------------* | Compatibility Tests *----------------------------------------------------------------------------*/ @@ -9708,6 +9801,8 @@ void ApiTest(void) AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0); AssertIntEQ(test_wc_AesCcmSetKey(), 0); AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0); + AssertIntEQ(test_wc_Hc128_SetKey(), 0); + AssertIntEQ(test_wc_Hc128_Process(), 0); printf(" End API Tests\n"); } diff --git a/wolfcrypt/src/hc128.c b/wolfcrypt/src/hc128.c index 286327c11..11fe27468 100644 --- a/wolfcrypt/src/hc128.c +++ b/wolfcrypt/src/hc128.c @@ -305,6 +305,10 @@ int wc_Hc128_SetHeap(HC128* ctx, void* heap) /* Key setup */ int wc_Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv) { + if (ctx == NULL || key == NULL) { + return BAD_FUNC_ARG; + } + #ifdef XSTREAM_ALIGN /* default heap to NULL or heap test value */ #ifdef WOLFSSL_HEAP_TEST @@ -384,6 +388,10 @@ static INLINE int DoProcess(HC128* ctx, byte* output, const byte* input, /* Encrypt/decrypt a message of any size */ int wc_Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen) { + if (ctx == NULL || output == NULL || input == NULL) { + return BAD_FUNC_ARG; + } + #ifdef XSTREAM_ALIGN if ((wolfssl_word)input % 4 || (wolfssl_word)output % 4) { #ifndef NO_WOLFSSL_ALLOC_ALIGN