From 6a2824f19979e07a1041e81dd2f4fe3e297fa53a Mon Sep 17 00:00:00 2001 From: jrblixt Date: Wed, 31 May 2017 14:58:01 -0600 Subject: [PATCH] Add Camellia unit test functions.. --- src/internal.c | 4 +- tests/api.c | 297 ++++++++++++++++++++++++++++++++ wolfcrypt/benchmark/benchmark.c | 8 +- wolfcrypt/src/camellia.c | 50 ++++-- wolfcrypt/test/test.c | 28 +-- wolfssl/wolfcrypt/camellia.h | 8 +- 6 files changed, 361 insertions(+), 34 deletions(-) diff --git a/src/internal.c b/src/internal.c index 41532de90..cbe7915e8 100755 --- a/src/internal.c +++ b/src/internal.c @@ -10200,7 +10200,7 @@ static INLINE int EncryptDo(WOLFSSL* ssl, byte* out, const byte* input, #ifdef HAVE_CAMELLIA case wolfssl_camellia: - wc_CamelliaCbcEncrypt(ssl->encrypt.cam, out, input, sz); + ret = wc_CamelliaCbcEncrypt(ssl->encrypt.cam, out, input, sz); break; #endif @@ -10435,7 +10435,7 @@ static INLINE int DecryptDo(WOLFSSL* ssl, byte* plain, const byte* input, #ifdef HAVE_CAMELLIA case wolfssl_camellia: - wc_CamelliaCbcDecrypt(ssl->decrypt.cam, plain, input, sz); + ret = wc_CamelliaCbcDecrypt(ssl->decrypt.cam, plain, input, sz); break; #endif diff --git a/tests/api.c b/tests/api.c index 621e3f2bf..26aeeacfd 100644 --- a/tests/api.c +++ b/tests/api.c @@ -83,6 +83,10 @@ #include #endif +#ifdef HAVE_CAMELLIA + #include +#endif + #ifdef OPENSSL_EXTRA #include #include @@ -5571,6 +5575,293 @@ static int test_wc_ChaCha20Poly1305_aead (void) +/* + * testing wc_CamelliaSetKey + */ +static int test_wc_CamelliaSetKey (void) +{ +#ifdef HAVE_CAMELLIA + Camellia camellia; + /*128-bit key*/ + static const byte key16[] = + { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 + }; + /* 192-bit key */ + static const byte key24[] = + { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 + }; + /* 256-bit key */ + static const byte key32[] = + { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff + }; + static const byte iv[] = + { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F + }; + int ret; + + printf(testingFmt, "wc_CamelliaSetKey()"); + + ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv); + if (ret == 0) { + ret = wc_CamelliaSetKey(&camellia, key16, + (word32)sizeof(key16), NULL); + if (ret == 0) { + ret = wc_CamelliaSetKey(&camellia, key24, + (word32)sizeof(key24), iv); + } + if (ret == 0) { + ret = wc_CamelliaSetKey(&camellia, key24, + (word32)sizeof(key24), NULL); + } + if (ret == 0) { + ret = wc_CamelliaSetKey(&camellia, key32, + (word32)sizeof(key32), iv); + } + if (ret == 0) { + ret = wc_CamelliaSetKey(&camellia, key32, + (word32)sizeof(key32), NULL); + } + } + /* Bad args. */ + if (ret == 0) { + ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv); + if (ret != BAD_FUNC_ARG) { + ret = SSL_FATAL_ERROR; + } else { + ret = 0; + } + } /* END bad args. */ + + printf(resultFmt, ret == 0 ? passed : failed); + +#endif + return 0; + +} /* END test_wc_CammeliaSetKey */ + +/* + * Testing wc_CamelliaSetIV() + */ +static int test_wc_CamelliaSetIV (void) +{ +#ifdef HAVE_CAMELLIA + Camellia camellia; + static const byte iv[] = + { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F + }; + int ret; + + printf(testingFmt, "wc_CamelliaSetIV()"); + + ret = wc_CamelliaSetIV(&camellia, iv); + if (ret == 0) { + ret = wc_CamelliaSetIV(&camellia, NULL); + } + /* Bad args. */ + if (ret == 0) { + ret = wc_CamelliaSetIV(NULL, NULL); + if (ret != BAD_FUNC_ARG) { + ret = SSL_FATAL_ERROR; + } else { + ret = 0; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + +#endif + return 0; +} /*END test_wc_CamelliaSetIV*/ + +/* + * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect + */ +static int test_wc_CamelliaEncryptDecryptDirect (void) +{ +#ifdef HAVE_CAMELLIA + Camellia camellia; + static const byte key24[] = + { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 + }; + static const byte iv[] = + { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F + }; + static const byte plainT[] = + { + 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, + 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A + }; + byte enc[sizeof(plainT)]; + byte dec[sizeof(enc)]; + int camE = SSL_FATAL_ERROR; + int camD = SSL_FATAL_ERROR; + int ret; + + /*Init stack variables.*/ + XMEMSET(enc, 0, 16); + XMEMSET(enc, 0, 16); + + ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv); + if (ret == 0) { + ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT); + if (ret == 0) { + ret = wc_CamelliaDecryptDirect(&camellia, dec, enc); + if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) { + ret = SSL_FATAL_ERROR; + } + } + } + printf(testingFmt, "wc_CamelliaEncryptDirect()"); + /* Pass bad args. */ + if (ret == 0) { + camE = wc_CamelliaEncryptDirect(NULL, enc, plainT); + if (camE == BAD_FUNC_ARG) { + camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT); + } + if (camE == BAD_FUNC_ARG) { + camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL); + } + if (camE == BAD_FUNC_ARG) { + camE = 0; + } else { + camE = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, camE == 0 ? passed : failed); + printf(testingFmt, "wc_CamelliaDecryptDirect()"); + + if (ret == 0) { + camD = wc_CamelliaDecryptDirect(NULL, dec, enc); + if (camD == BAD_FUNC_ARG) { + camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc); + } + if (camD == BAD_FUNC_ARG) { + camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL); + } + if (camD == BAD_FUNC_ARG) { + camD = 0; + } else { + camD = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, camD == 0 ? passed : failed); + +#endif + return 0; + +} /* END test-wc_CamelliaEncryptDecryptDirect */ + +/* + * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt + */ +static int test_wc_CamelliaCbcEncryptDecrypt (void) +{ +#ifdef HAVE_CAMELLIA + Camellia camellia; + static const byte key24[] = + { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 + }; + static const byte plainT[] = + { + 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, + 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A + }; + byte enc[CAMELLIA_BLOCK_SIZE]; + byte dec[CAMELLIA_BLOCK_SIZE]; + int camCbcE = SSL_FATAL_ERROR; + int camCbcD = SSL_FATAL_ERROR; + int ret; + + /* Init stack variables. */ + XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE); + XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE); + + ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL); + if (ret == 0) { + ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE); + if (ret != 0) { + ret = SSL_FATAL_ERROR; + } + } + if (ret == 0) { + ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL); + if (ret == 0) { + ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE); + if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) { + ret = SSL_FATAL_ERROR; + } + } + } + + printf(testingFmt, "wc_CamelliaCbcEncrypt"); + /* Pass in bad args. */ + if (ret == 0) { + camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE); + if (camCbcE == BAD_FUNC_ARG) { + camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT, + CAMELLIA_BLOCK_SIZE); + } + if (camCbcE == BAD_FUNC_ARG) { + camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL, + CAMELLIA_BLOCK_SIZE); + } + if (camCbcE == BAD_FUNC_ARG) { + camCbcE = 0; + } else { + camCbcE = SSL_FATAL_ERROR; + } + } + + printf(resultFmt, camCbcE == 0 ? passed : failed); + printf(testingFmt, "wc_CamelliaCbcDecrypt()"); + + if (ret == 0) { + camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE); + if (camCbcD == BAD_FUNC_ARG) { + camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc, + CAMELLIA_BLOCK_SIZE); + } + if (camCbcD == BAD_FUNC_ARG) { + camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL, + CAMELLIA_BLOCK_SIZE); + } + if (camCbcD == BAD_FUNC_ARG) { + camCbcD = 0; + } else { + camCbcD = SSL_FATAL_ERROR; + } + } /* END bad args. */ + + printf(resultFmt, camCbcD == 0 ? passed : failed); + +#endif + return 0; + +} /* END test_wc_CamelliaCbcEncryptDecrypt */ + /*----------------------------------------------------------------------------* | Compatibility Tests *----------------------------------------------------------------------------*/ @@ -6859,6 +7150,12 @@ void ApiTest(void) AssertIntEQ(test_wc_Chacha_SetKey(), 0); AssertIntEQ(test_wc_Chacha_Process(), 0); AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0); + + AssertIntEQ(test_wc_CamelliaSetKey(), 0); + AssertIntEQ(test_wc_CamelliaSetIV(), 0); + AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0); + AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0); + printf(" End API Tests\n"); } diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 37cca7474..08d022781 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -1416,8 +1416,12 @@ void bench_camellia(void) bench_stats_start(&count, &start); do { for (i = 0; i < numBlocks; i++) { - wc_CamelliaCbcEncrypt(&cam, bench_plain, bench_cipher, - BENCH_SIZE); + ret = wc_CamelliaCbcEncrypt(&cam, bench_plain, bench_cipher, + BENCH_SIZE); + if (ret <= 0) { + printf("CamelliaCbcEncrypt failed: %d\n", ret); + return; + } } count += i; } while (bench_stats_sym_check(start)); diff --git a/wolfcrypt/src/camellia.c b/wolfcrypt/src/camellia.c index 95609371d..5f8dd07a7 100644 --- a/wolfcrypt/src/camellia.c +++ b/wolfcrypt/src/camellia.c @@ -48,7 +48,7 @@ /* - * Algorithm Specification + * Algorithm Specification * http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html */ @@ -1017,7 +1017,7 @@ static int camellia_setup256(const unsigned char *key, u32 *subkey) CamelliaSubkeyR(30) = CamelliaSubkeyL(30) ^ dw, CamelliaSubkeyL(30) = dw; dw = CamelliaSubkeyL(31) ^ CamelliaSubkeyR(31), dw = CAMELLIA_RL8(dw); CamelliaSubkeyR(31) = CamelliaSubkeyL(31) ^ dw,CamelliaSubkeyL(31) = dw; - + #ifdef WOLFSSL_SMALL_STACK XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(subR, NULL, DYNAMIC_TYPE_TMP_BUFFER); @@ -1134,14 +1134,14 @@ static void camellia_encrypt128(const u32 *subkey, u32 *io) io[1] = io[3]; io[2] = t0; io[3] = t1; - + return; } static void camellia_decrypt128(const u32 *subkey, u32 *io) { u32 il,ir,t0,t1; /* temporary variables */ - + /* pre whitening but absorb kw2*/ io[0] ^= CamelliaSubkeyL(24); io[1] ^= CamelliaSubkeyR(24); @@ -1352,7 +1352,7 @@ static void camellia_decrypt256(const u32 *subkey, u32 *io) /* pre whitening but absorb kw2*/ io[0] ^= CamelliaSubkeyL(32); io[1] ^= CamelliaSubkeyR(32); - + /* main iteration */ CAMELLIA_ROUNDSM(io[0],io[1], CamelliaSubkeyL(31),CamelliaSubkeyR(31), @@ -1464,9 +1464,9 @@ static void camellia_decrypt256(const u32 *subkey, u32 *io) * API for compatibility */ -static void Camellia_EncryptBlock(const int keyBitLength, - const unsigned char *plaintext, - const KEY_TABLE_TYPE keyTable, +static void Camellia_EncryptBlock(const int keyBitLength, + const unsigned char *plaintext, + const KEY_TABLE_TYPE keyTable, unsigned char *ciphertext) { u32 tmp[4]; @@ -1495,9 +1495,9 @@ static void Camellia_EncryptBlock(const int keyBitLength, PUTU32(ciphertext + 12, tmp[3]); } -static void Camellia_DecryptBlock(const int keyBitLength, - const unsigned char *ciphertext, - const KEY_TABLE_TYPE keyTable, +static void Camellia_DecryptBlock(const int keyBitLength, + const unsigned char *ciphertext, + const KEY_TABLE_TYPE keyTable, unsigned char *plaintext) { u32 tmp[4]; @@ -1574,20 +1574,33 @@ int wc_CamelliaSetIV(Camellia* cam, const byte* iv) } -void wc_CamelliaEncryptDirect(Camellia* cam, byte* out, const byte* in) +int wc_CamelliaEncryptDirect(Camellia* cam, byte* out, const byte* in) { + if (cam == NULL || out == NULL || in == NULL) { + return BAD_FUNC_ARG; + } Camellia_EncryptBlock(cam->keySz, in, cam->key, out); + + return 0; } -void wc_CamelliaDecryptDirect(Camellia* cam, byte* out, const byte* in) +int wc_CamelliaDecryptDirect(Camellia* cam, byte* out, const byte* in) { + if (cam == NULL || out == NULL || in == NULL) { + return BAD_FUNC_ARG; + } Camellia_DecryptBlock(cam->keySz, in, cam->key, out); + + return 0; } -void wc_CamelliaCbcEncrypt(Camellia* cam, byte* out, const byte* in, word32 sz) +int wc_CamelliaCbcEncrypt(Camellia* cam, byte* out, const byte* in, word32 sz) { + if (cam == NULL || out == NULL || in == NULL) { + return BAD_FUNC_ARG; + } word32 blocks = sz / CAMELLIA_BLOCK_SIZE; while (blocks--) { @@ -1599,11 +1612,16 @@ void wc_CamelliaCbcEncrypt(Camellia* cam, byte* out, const byte* in, word32 sz) out += CAMELLIA_BLOCK_SIZE; in += CAMELLIA_BLOCK_SIZE; } + + return 0; } -void wc_CamelliaCbcDecrypt(Camellia* cam, byte* out, const byte* in, word32 sz) +int wc_CamelliaCbcDecrypt(Camellia* cam, byte* out, const byte* in, word32 sz) { + if (cam == NULL || out == NULL || in == NULL) { + return BAD_FUNC_ARG; + } word32 blocks = sz / CAMELLIA_BLOCK_SIZE; while (blocks--) { @@ -1615,6 +1633,8 @@ void wc_CamelliaCbcDecrypt(Camellia* cam, byte* out, const byte* in, word32 sz) out += CAMELLIA_BLOCK_SIZE; in += CAMELLIA_BLOCK_SIZE; } + + return 0; } diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 597d5ac19..a72dff337 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -5075,7 +5075,7 @@ int camellia_test(void) byte out[CAMELLIA_BLOCK_SIZE]; Camellia cam; - int i, testsSz; + int i, testsSz, ret; const test_vector_t testVectors[] = { {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114}, @@ -5100,25 +5100,31 @@ int camellia_test(void) switch (testVectors[i].type) { case CAM_ECB_ENC: - wc_CamelliaEncryptDirect(&cam, out, testVectors[i].plaintext); - if (XMEMCMP(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE)) + ret = wc_CamelliaEncryptDirect(&cam, out, + testVectors[i].plaintext); + if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext, + CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_ECB_DEC: - wc_CamelliaDecryptDirect(&cam, out, testVectors[i].ciphertext); - if (XMEMCMP(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE)) + ret = wc_CamelliaDecryptDirect(&cam, out, + testVectors[i].ciphertext); + if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext, + CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_CBC_ENC: - wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext, + ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE); - if (XMEMCMP(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE)) + if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext, + CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; case CAM_CBC_DEC: - wc_CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext, + ret = wc_CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE); - if (XMEMCMP(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE)) + if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext, + CAMELLIA_BLOCK_SIZE)) return testVectors[i].errorCode; break; default: @@ -5127,8 +5133,8 @@ int camellia_test(void) } /* Setting the IV and checking it was actually set. */ - wc_CamelliaSetIV(&cam, ivc); - if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE)) + ret = wc_CamelliaSetIV(&cam, ivc); + if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE)) return -4700; /* Setting the IV to NULL should be same as all zeros IV */ diff --git a/wolfssl/wolfcrypt/camellia.h b/wolfssl/wolfcrypt/camellia.h index 923d5a940..12a306188 100644 --- a/wolfssl/wolfcrypt/camellia.h +++ b/wolfssl/wolfcrypt/camellia.h @@ -78,13 +78,13 @@ typedef struct Camellia { WOLFSSL_API int wc_CamelliaSetKey(Camellia* cam, const byte* key, word32 len, const byte* iv); WOLFSSL_API int wc_CamelliaSetIV(Camellia* cam, const byte* iv); -WOLFSSL_API void wc_CamelliaEncryptDirect(Camellia* cam, byte* out, +WOLFSSL_API int wc_CamelliaEncryptDirect(Camellia* cam, byte* out, const byte* in); -WOLFSSL_API void wc_CamelliaDecryptDirect(Camellia* cam, byte* out, +WOLFSSL_API int wc_CamelliaDecryptDirect(Camellia* cam, byte* out, const byte* in); -WOLFSSL_API void wc_CamelliaCbcEncrypt(Camellia* cam, +WOLFSSL_API int wc_CamelliaCbcEncrypt(Camellia* cam, byte* out, const byte* in, word32 sz); -WOLFSSL_API void wc_CamelliaCbcDecrypt(Camellia* cam, +WOLFSSL_API int wc_CamelliaCbcDecrypt(Camellia* cam, byte* out, const byte* in, word32 sz);