From f447fe22b04782f39e6766411c1999ba87683a04 Mon Sep 17 00:00:00 2001 From: MJSPollard Date: Mon, 21 May 2018 10:55:56 -0600 Subject: [PATCH 1/3] added Poly1305SetKey Unit Test --- tests/api.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/api.c b/tests/api.c index 11bcbded3..70b5a58bd 100644 --- a/tests/api.c +++ b/tests/api.c @@ -193,6 +193,11 @@ #ifdef HAVE_CHACHA #include #endif + +#ifdef HAVE_POLY1305 + #include +#endif + #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) #include #endif @@ -7402,6 +7407,7 @@ static int test_wc_Des3_SetKey (void) return ret; } /* END test_wc_Des3_SetKey */ + /* * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt @@ -7642,6 +7648,45 @@ static int test_wc_Chacha_SetKey (void) return ret; } /* END test_wc_Chacha_SetKey */ +/* + * unit test for wc_Poly1305SetKey() + */ +static int test_wc_Poly1305SetKey(void) +{ + int ret = 0; + +#ifdef HAVE_POLY1305 + Poly1305 ctx; + const byte key[] = + { + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 + }; + + printf(testingFmt, "wc_Poly1305_SetKey()"); + + ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte))); + /* Test bad args. */ + if (ret == 0) { + ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte))); + if (ret == BAD_FUNC_ARG) { + ret = wc_Poly1305SetKey(&ctx, key, 18); + } + if (ret == BAD_FUNC_ARG) { + ret = 0; + } else { + ret = WOLFSSL_FATAL_ERROR; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + +#endif + return ret; +} /* END test_wc_Poly1305_SetKey() */ + /* * Testing wc_Chacha_Process() */ @@ -18774,6 +18819,7 @@ void ApiTest(void) AssertIntEQ(test_wc_Chacha_SetKey(), 0); AssertIntEQ(test_wc_Chacha_Process(), 0); AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0); + AssertIntEQ(test_wc_Poly1305SetKey(), 0); AssertIntEQ(test_wc_CamelliaSetKey(), 0); AssertIntEQ(test_wc_CamelliaSetIV(), 0); @@ -18802,6 +18848,7 @@ void ApiTest(void) AssertIntEQ(test_wc_MakeRsaKey(), 0); AssertIntEQ(test_wc_SetKeyUsage (), 0); + AssertIntEQ(test_wc_RsaKeyToDer(), 0); AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0); AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0); From 2b49f69f1b51302c30fd8f0d6ca130e2dd0e500d Mon Sep 17 00:00:00 2001 From: MJSPollard Date: Mon, 21 May 2018 12:44:59 -0600 Subject: [PATCH 2/3] updated unit test --- tests/api.c | 3 +++ wolfcrypt/src/poly1305.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/api.c b/tests/api.c index 70b5a58bd..dc33129a9 100644 --- a/tests/api.c +++ b/tests/api.c @@ -7671,6 +7671,9 @@ static int test_wc_Poly1305SetKey(void) /* Test bad args. */ if (ret == 0) { ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte))); + if(ret == BAD_FUNC_ARG) { + ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte))); + } if (ret == BAD_FUNC_ARG) { ret = wc_Poly1305SetKey(&ctx, key, 18); } diff --git a/wolfcrypt/src/poly1305.c b/wolfcrypt/src/poly1305.c index 4fcc712f0..e54d7cf98 100644 --- a/wolfcrypt/src/poly1305.c +++ b/wolfcrypt/src/poly1305.c @@ -1222,7 +1222,7 @@ int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz) printf("\n"); #endif - if (keySz != 32 || ctx == NULL) + if (keySz != 32 || ctx == NULL || key == NULL) return BAD_FUNC_ARG; #ifdef USE_INTEL_SPEEDUP From fb247a5d8dfa24e15d309e7d27324bae04bead12 Mon Sep 17 00:00:00 2001 From: MJSPollard Date: Mon, 21 May 2018 13:59:15 -0600 Subject: [PATCH 3/3] added null check for key --- wolfcrypt/src/poly1305.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/poly1305.c b/wolfcrypt/src/poly1305.c index e54d7cf98..adf3dbf80 100644 --- a/wolfcrypt/src/poly1305.c +++ b/wolfcrypt/src/poly1305.c @@ -1211,6 +1211,9 @@ int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz) word64 t0,t1; #endif + if (key == NULL) + return BAD_FUNC_ARG; + #ifdef CHACHA_AEAD_TEST word32 k; printf("Poly key used:\n"); @@ -1222,7 +1225,7 @@ int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz) printf("\n"); #endif - if (keySz != 32 || ctx == NULL || key == NULL) + if (keySz != 32 || ctx == NULL) return BAD_FUNC_ARG; #ifdef USE_INTEL_SPEEDUP