Add IDEA unit test functions.

This commit is contained in:
jrblixt
2017-05-29 11:49:58 -06:00
parent 2b085351b6
commit 25ce52cd0c
4 changed files with 264 additions and 10 deletions

View File

@@ -64,6 +64,9 @@
#ifdef WOLFSSL_RIPEMD #ifdef WOLFSSL_RIPEMD
#include <wolfssl/wolfcrypt/ripemd.h> #include <wolfssl/wolfcrypt/ripemd.h>
#endif #endif
#ifdef HAVE_IDEA
#include <wolfssl/wolfcrypt/idea.h>
#endif
#ifndef NO_DES3 #ifndef NO_DES3
#include <wolfssl/wolfcrypt/des3.h> #include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/wc_encrypt.h> #include <wolfssl/wolfcrypt/wc_encrypt.h>
@@ -3735,6 +3738,231 @@ static int test_wc_Sha224Final (void)
return 0; return 0;
} /* END test_wc_Sha224Final */ } /* END test_wc_Sha224Final */
/*
* unit test for wc_IdeaSetKey()
*/
static int test_wc_IdeaSetKey (void)
{
#ifdef HAVE_IDEA
Idea idea;
const byte key[] =
{
0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
};
int ret, flag = 0;
printf(testingFmt, "wc_IdeaSetKey()");
/*IV can be NULL, default value is 0*/
ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
if (ret == 0) {
ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
}
/* Bad args. */
if (ret == 0) {
ret = wc_IdeaSetKey(NULL, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
if (ret != BAD_FUNC_ARG) {
flag = 1;
}
ret = wc_IdeaSetKey(&idea, NULL, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
if (ret != BAD_FUNC_ARG) {
flag = 1;
}
ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE - 1,
NULL, IDEA_ENCRYPTION);
if (ret != BAD_FUNC_ARG) {
flag = 1;
}
ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, -1);
if (ret != BAD_FUNC_ARG) {
flag = 1;
}
if (flag == 1) {
ret = SSL_FATAL_ERROR;
} else {
ret = 0;
}
} /* END Test Bad Args. */
printf(resultFmt, ret == 0 ? passed : failed);
#endif
return 0;
} /* END test_wc_IdeaSetKey */
/*
* Unit test for wc_IdeaSetIV()
*/
static int test_wc_IdeaSetIV (void)
{
#ifdef HAVE_IDEA
Idea idea;
int ret;
printf(testingFmt, "wc_IdeaSetIV()");
ret = wc_IdeaSetIV(&idea, NULL);
/* Test bad args. */
if (ret == 0) {
ret = wc_IdeaSetIV(NULL, NULL);
}
if (ret == BAD_FUNC_ARG) {
ret = 0;
} else {
ret = SSL_FATAL_ERROR;
}
printf(resultFmt, ret == 0 ? passed : failed);
#endif
return 0;
} /* END test_wc_IdeaSetIV */
/*
* Unit test for wc_IdeaCipher()
*/
static int test_wc_IdeaCipher (void)
{
#ifdef HAVE_IDEA
Idea idea;
const byte key[] =
{
0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48
};
const byte plain[] =
{
0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
};
byte enc[sizeof(plain)];
byte dec[sizeof(enc)];
int ret;
printf(testingFmt, "wc_IdeaCipher()");
ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
if (ret == 0) {
ret = wc_IdeaCipher(&idea, enc, plain);
if (ret != 0) {
ret = SSL_FATAL_ERROR;
}
}
if (ret == 0) {
ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
if (ret == 0) {
ret = wc_IdeaCipher(&idea, dec, enc);
}
if (ret == 0) {
ret = XMEMCMP(plain, dec, IDEA_BLOCK_SIZE);
}
if (ret != 0) {
ret = SSL_FATAL_ERROR;
}
}
/* Pass Bad Args. */
if (ret == 0) {
ret = wc_IdeaCipher(NULL, enc, dec);
if (ret == BAD_FUNC_ARG) {
ret = wc_IdeaCipher(&idea, NULL, dec);
}
if (ret == BAD_FUNC_ARG) {
ret = wc_IdeaCipher(&idea, enc, NULL);
}
if (ret == BAD_FUNC_ARG) {
ret = 0;
} else {
ret = SSL_FATAL_ERROR;
}
}
printf(resultFmt, ret == 0 ? passed : failed);
#endif
return 0;
} /* END test_wc_IdeaCipher */
/*
* Unit test for functions wc_IdeaCbcEncrypt and wc_IdeaCbcDecrypt
*/
static int test_wc_IdeaCbcEncyptDecrypt (void)
{
#ifdef HAVE_IDEA
Idea idea;
const byte key[] =
{
0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
};
const char* message = "International Data Encryption Algorithm";
byte msg_enc[40];
byte msg_dec[40];
int ret;
printf(testingFmt, "wc_IdeaCbcEncrypt()");
ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_ENCRYPTION);
if (ret == 0) {
ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
(word32)XSTRLEN(message) + 1);
}
if (ret == 0) {
ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_DECRYPTION);
}
if (ret == 0) {
ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
(word32)XSTRLEN(message) + 1);
if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
ret = SSL_FATAL_ERROR;
}
}
/* Test bad args. Enc */
if (ret == 0) {
ret = wc_IdeaCbcEncrypt(NULL, msg_enc, (byte*)message,
(word32)XSTRLEN(message) + 1);
if (ret == BAD_FUNC_ARG) {
ret = wc_IdeaCbcEncrypt(&idea, NULL, (byte*)message,
(word32)XSTRLEN(message) + 1);
}
if (ret == BAD_FUNC_ARG) {
ret = wc_IdeaCbcEncrypt(&idea, msg_enc, NULL,
(word32)XSTRLEN(message) + 1);
}
if (ret != BAD_FUNC_ARG) {
ret = SSL_FATAL_ERROR;
} else {
ret = 0;
}
} /* END test bad args ENC */
/* Test bad args DEC */
if (ret == 0) {
ret = wc_IdeaCbcDecrypt(NULL, msg_dec, msg_enc,
(word32)XSTRLEN(message) + 1);
if (ret == BAD_FUNC_ARG) {
ret = wc_IdeaCbcDecrypt(&idea, NULL, msg_enc,
(word32)XSTRLEN(message) + 1);
}
if (ret == BAD_FUNC_ARG) {
ret = wc_IdeaCbcDecrypt(&idea, msg_dec, NULL,
(word32)XSTRLEN(message) + 1);
}
if (ret != BAD_FUNC_ARG) {
ret = SSL_FATAL_ERROR;
} else {
ret = 0;
}
}
printf(resultFmt, ret == 0 ? passed : failed);
#endif
return 0;
} /* END test_wc_IdeaCbcEncryptDecrypt */
/* /*
* Test function for wc_HmacSetKey * Test function for wc_HmacSetKey
@@ -6346,6 +6574,10 @@ void ApiTest(void)
AssertIntEQ(test_wc_Des3_SetKey(), 0); AssertIntEQ(test_wc_Des3_SetKey(), 0);
AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0); AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0); AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
AssertIntEQ(test_wc_IdeaSetKey(), 0);
AssertIntEQ(test_wc_IdeaSetIV(), 0);
AssertIntEQ(test_wc_IdeaCipher(), 0);
AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
printf(" End API Tests\n"); printf(" End API Tests\n");
} }

View File

@@ -194,12 +194,16 @@ int wc_IdeaSetIV(Idea *idea, const byte* iv)
/* encryption/decryption for a block (64 bits) /* encryption/decryption for a block (64 bits)
*/ */
void wc_IdeaCipher(Idea *idea, byte* out, const byte* in) int wc_IdeaCipher(Idea *idea, byte* out, const byte* in)
{ {
word32 t1, t2; word32 t1, t2;
word16 i, skey_idx = 0, idx = 0; word16 i, skey_idx = 0, idx = 0;
word16 x[4]; word16 x[4];
if (idea == NULL || out == NULL || in == NULL) {
return BAD_FUNC_ARG;
}
/* put input byte block in word16 */ /* put input byte block in word16 */
for (i = 0; i < IDEA_BLOCK_SIZE/2; i++) { for (i = 0; i < IDEA_BLOCK_SIZE/2; i++) {
x[i] = (word16)in[idx++] << 8; x[i] = (word16)in[idx++] << 8;
@@ -241,11 +245,14 @@ void wc_IdeaCipher(Idea *idea, byte* out, const byte* in)
x[3] = idea_mult(x[3], idea->skey[skey_idx++]); x[3] = idea_mult(x[3], idea->skey[skey_idx++]);
out[6] = (x[3] >> 8) & 0xFF; out[6] = (x[3] >> 8) & 0xFF;
out[7] = x[3] & 0xFF; out[7] = x[3] & 0xFF;
return 0;
} }
int wc_IdeaCbcEncrypt(Idea *idea, byte* out, const byte* in, word32 len) int wc_IdeaCbcEncrypt(Idea *idea, byte* out, const byte* in, word32 len)
{ {
int blocks; int blocks;
int ret;
if (idea == NULL || out == NULL || in == NULL) if (idea == NULL || out == NULL || in == NULL)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
@@ -253,7 +260,11 @@ int wc_IdeaCbcEncrypt(Idea *idea, byte* out, const byte* in, word32 len)
blocks = len / IDEA_BLOCK_SIZE; blocks = len / IDEA_BLOCK_SIZE;
while (blocks--) { while (blocks--) {
xorbuf((byte*)idea->reg, in, IDEA_BLOCK_SIZE); xorbuf((byte*)idea->reg, in, IDEA_BLOCK_SIZE);
wc_IdeaCipher(idea, (byte*)idea->reg, (byte*)idea->reg); ret = wc_IdeaCipher(idea, (byte*)idea->reg, (byte*)idea->reg);
if (ret != 0) {
return ret;
}
XMEMCPY(out, idea->reg, IDEA_BLOCK_SIZE); XMEMCPY(out, idea->reg, IDEA_BLOCK_SIZE);
out += IDEA_BLOCK_SIZE; out += IDEA_BLOCK_SIZE;
@@ -266,6 +277,7 @@ int wc_IdeaCbcEncrypt(Idea *idea, byte* out, const byte* in, word32 len)
int wc_IdeaCbcDecrypt(Idea *idea, byte* out, const byte* in, word32 len) int wc_IdeaCbcDecrypt(Idea *idea, byte* out, const byte* in, word32 len)
{ {
int blocks; int blocks;
int ret;
if (idea == NULL || out == NULL || in == NULL) if (idea == NULL || out == NULL || in == NULL)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
@@ -273,7 +285,11 @@ int wc_IdeaCbcDecrypt(Idea *idea, byte* out, const byte* in, word32 len)
blocks = len / IDEA_BLOCK_SIZE; blocks = len / IDEA_BLOCK_SIZE;
while (blocks--) { while (blocks--) {
XMEMCPY((byte*)idea->tmp, in, IDEA_BLOCK_SIZE); XMEMCPY((byte*)idea->tmp, in, IDEA_BLOCK_SIZE);
wc_IdeaCipher(idea, out, (byte*)idea->tmp); ret = wc_IdeaCipher(idea, out, (byte*)idea->tmp);
if (ret != 0) {
return ret;
}
xorbuf(out, (byte*)idea->reg, IDEA_BLOCK_SIZE); xorbuf(out, (byte*)idea->reg, IDEA_BLOCK_SIZE);
XMEMCPY(idea->reg, idea->tmp, IDEA_BLOCK_SIZE); XMEMCPY(idea->reg, idea->tmp, IDEA_BLOCK_SIZE);

View File

@@ -5227,8 +5227,8 @@ int idea_test(void)
} }
/* Data encryption */ /* Data encryption */
wc_IdeaCipher(&idea, data, v1_plain[i]); ret = wc_IdeaCipher(&idea, data, v1_plain[i]);
if (XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) { if (ret != 0 || XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) {
printf("Bad encryption\n"); printf("Bad encryption\n");
return -4801; return -4801;
} }
@@ -5243,8 +5243,8 @@ int idea_test(void)
} }
/* Data decryption */ /* Data decryption */
wc_IdeaCipher(&idea, data, data); ret = wc_IdeaCipher(&idea, data, data);
if (XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) { if (ret != 0 || XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) {
printf("Bad decryption\n"); printf("Bad decryption\n");
return -4803; return -4803;
} }
@@ -5302,7 +5302,10 @@ int idea_test(void)
/* 100 times data encryption */ /* 100 times data encryption */
XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE); XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
for (j = 0; j < 100; j++) { for (j = 0; j < 100; j++) {
wc_IdeaCipher(&idea, data, data); ret = wc_IdeaCipher(&idea, data, data);
if (ret != 0) {
return -4821;
}
} }
if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) { if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) {
@@ -5313,7 +5316,10 @@ int idea_test(void)
/* 1000 times data encryption */ /* 1000 times data encryption */
XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE); XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
for (j = 0; j < 1000; j++) { for (j = 0; j < 1000; j++) {
wc_IdeaCipher(&idea, data, data); ret = wc_IdeaCipher(&idea, data, data);
if (ret != 0) {
return -4822;
}
} }
if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) { if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) {

View File

@@ -54,7 +54,7 @@ typedef struct Idea {
WOLFSSL_API int wc_IdeaSetKey(Idea *idea, const byte* key, word16 keySz, WOLFSSL_API int wc_IdeaSetKey(Idea *idea, const byte* key, word16 keySz,
const byte *iv, int dir); const byte *iv, int dir);
WOLFSSL_API int wc_IdeaSetIV(Idea *idea, const byte* iv); WOLFSSL_API int wc_IdeaSetIV(Idea *idea, const byte* iv);
WOLFSSL_API void wc_IdeaCipher(Idea *idea, byte* out, const byte* in); WOLFSSL_API int wc_IdeaCipher(Idea *idea, byte* out, const byte* in);
WOLFSSL_API int wc_IdeaCbcEncrypt(Idea *idea, byte* out, WOLFSSL_API int wc_IdeaCbcEncrypt(Idea *idea, byte* out,
const byte* in, word32 len); const byte* in, word32 len);
WOLFSSL_API int wc_IdeaCbcDecrypt(Idea *idea, byte* out, WOLFSSL_API int wc_IdeaCbcDecrypt(Idea *idea, byte* out,