From c14e2d5888f35259e8c02ffa50e4bee8762da42a Mon Sep 17 00:00:00 2001 From: jrblixt Date: Fri, 2 Jun 2017 10:55:04 -0600 Subject: [PATCH] Add Rabbit unit test functions. --- tests/api.c | 104 +++++++++++++++++++++++++++++++++++++++++ wolfcrypt/src/rabbit.c | 18 +++++-- 2 files changed, 117 insertions(+), 5 deletions(-) diff --git a/tests/api.c b/tests/api.c index 26aeeacfd..bc121dc80 100644 --- a/tests/api.c +++ b/tests/api.c @@ -87,6 +87,10 @@ #include #endif +#ifndef NO_RABBIT + #include +#endif + #ifdef OPENSSL_EXTRA #include #include @@ -5862,6 +5866,102 @@ static int test_wc_CamelliaCbcEncryptDecrypt (void) } /* END test_wc_CamelliaCbcEncryptDecrypt */ +/* + * Testing wc_RabbitSetKey() + */ +static int test_wc_RabbitSetKey (void) +{ +#ifndef NO_RABBIT + Rabbit rabbit; + int ret; + const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B" + "\xFE\x36\x3D\x2E\x29\x13\x28\x91"; + const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3"; + + printf(testingFmt, "wc_RabbitSetKey()"); + + ret = wc_RabbitSetKey(&rabbit, (byte*)key, (byte*)iv); + + /* Test bad args. */ + if (ret == 0) { + ret = wc_RabbitSetKey(NULL, (byte*)key, (byte*)iv); + if (ret == BAD_FUNC_ARG) { + ret = wc_RabbitSetKey(&rabbit, NULL, (byte*)iv); + } + if (ret == BAD_FUNC_ARG) { + ret = wc_RabbitSetKey(&rabbit, (byte*)key, NULL); + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + +#endif + return 0; + +} /* END test_wc_RabbitSetKey */ + +/* + * Test wc_RabbitProcess() + */ +static int test_wc_RabbitProcess (void) +{ +#ifndef NO_RABBIT + Rabbit enc, dec; + byte cipher[25]; + byte plain[25]; + int ret; + const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B" + "\xFE\x36\x3D\x2E\x29\x13\x28\x91"; + const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3"; + const char* input = "Everyone gets Friday off."; + unsigned long int inlen = XSTRLEN(input); + + /* Initialize stack variables. */ + XMEMSET(cipher, 0, sizeof(cipher)); + XMEMSET(plain, 0, sizeof(plain)); + + printf(testingFmt, "wc_RabbitProcess()"); + + ret = wc_RabbitSetKey(&enc, (byte*)key, (byte*)iv); + if (ret == 0) { + ret = wc_RabbitSetKey(&dec, (byte*)key, (byte*)iv); + } + if (ret == 0) { + ret = wc_RabbitProcess(&enc, cipher, (byte*)input, (word32)inlen); + } + if (ret == 0) { + ret = wc_RabbitProcess(&dec, plain, cipher, (word32)inlen); + if (ret != 0 || XMEMCMP(input, plain, inlen)) { + ret = SSL_FATAL_ERROR; + } else { + ret = 0; + } + } + /* Test bad args. */ + if (ret == 0) { + ret = wc_RabbitProcess(NULL, plain, cipher, (word32)inlen); + if (ret == BAD_FUNC_ARG) { + ret = wc_RabbitProcess(&dec, NULL, cipher, (word32)inlen); + } + if (ret == BAD_FUNC_ARG) { + ret = wc_RabbitProcess(&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_RabbitProcess */ + + + /*----------------------------------------------------------------------------* | Compatibility Tests *----------------------------------------------------------------------------*/ @@ -7156,6 +7256,10 @@ void ApiTest(void) AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0); AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0); + + AssertIntEQ(test_wc_RabbitSetKey(), 0); + AssertIntEQ(test_wc_RabbitProcess(), 0); + printf(" End API Tests\n"); } diff --git a/wolfcrypt/src/rabbit.c b/wolfcrypt/src/rabbit.c index 81258f66a..b7601eb04 100644 --- a/wolfcrypt/src/rabbit.c +++ b/wolfcrypt/src/rabbit.c @@ -88,7 +88,7 @@ static void RABBIT_next_state(RabbitCtx* ctx) ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5])); ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6])); ctx->carry = (ctx->c[7] < c_old[7]); - + /* Calculate the g-values */ for (i=0;i<8;i++) g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i])); @@ -116,7 +116,7 @@ static void wc_RabbitSetIV(Rabbit* ctx, const byte* inIv) XMEMCPY(iv, inIv, sizeof(iv)); else XMEMSET(iv, 0, sizeof(iv)); - + /* Generate four subvectors */ i0 = LITTLE32(iv[0]); i2 = LITTLE32(iv[1]); @@ -218,6 +218,10 @@ int wc_Rabbit_SetHeap(Rabbit* ctx, void* heap) /* Key setup */ int wc_RabbitSetKey(Rabbit* 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 @@ -286,11 +290,11 @@ static INLINE int DoProcess(Rabbit* ctx, byte* output, const byte* input, /* Generate 16 bytes of pseudo-random data */ tmp[0] = LITTLE32(ctx->workCtx.x[0] ^ (ctx->workCtx.x[5]>>16) ^ U32V(ctx->workCtx.x[3]<<16)); - tmp[1] = LITTLE32(ctx->workCtx.x[2] ^ + tmp[1] = LITTLE32(ctx->workCtx.x[2] ^ (ctx->workCtx.x[7]>>16) ^ U32V(ctx->workCtx.x[5]<<16)); - tmp[2] = LITTLE32(ctx->workCtx.x[4] ^ + tmp[2] = LITTLE32(ctx->workCtx.x[4] ^ (ctx->workCtx.x[1]>>16) ^ U32V(ctx->workCtx.x[7]<<16)); - tmp[3] = LITTLE32(ctx->workCtx.x[6] ^ + tmp[3] = LITTLE32(ctx->workCtx.x[6] ^ (ctx->workCtx.x[3]>>16) ^ U32V(ctx->workCtx.x[1]<<16)); /* Encrypt/decrypt the data */ @@ -305,6 +309,10 @@ static INLINE int DoProcess(Rabbit* ctx, byte* output, const byte* input, /* Encrypt/decrypt a message of any size */ int wc_RabbitProcess(Rabbit* 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