add mcapi tdes with tests

This commit is contained in:
toddouska
2013-03-20 18:35:26 -07:00
parent a6d29aa628
commit 46442075f4
3 changed files with 150 additions and 5 deletions

View File

@@ -33,6 +33,7 @@
#include <cyassl/ctaocrypt/hmac.h>
#include <cyassl/ctaocrypt/compress.h>
#include <cyassl/ctaocrypt/random.h>
#include <cyassl/ctaocrypt/des3.h>
/* Initialize MD5 */
@@ -274,6 +275,50 @@ int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX* rng, unsigned char* b,
}
/* Triple DES Key Set, may have iv, will have direction */
int CRYPT_TDES_KeySet(CRYPT_TDES_CTX* tdes, const unsigned char* key,
const unsigned char* iv, int dir)
{
typedef char tdes_test[sizeof(CRYPT_TDES_CTX) >= sizeof(Des3) ? 1 : -1];
(void)sizeof(tdes_test);
Des3_SetKey((Des3*)tdes, key, iv, dir);
return 0;
}
/* Triple DES Iv Set, sometimes added later */
int CRYPT_TDES_IvSet(CRYPT_TDES_CTX* tdes, const unsigned char* iv)
{
Des3_SetIV((Des3*)tdes, iv);
return 0;
}
/* Triple DES CBC Encrypt */
int CRYPT_TDES_CBC_Encrypt(CRYPT_TDES_CTX* tdes, unsigned char* out,
const unsigned char* in, unsigned int inSz)
{
Des3_CbcEncrypt((Des3*)tdes, out, in, inSz);
return 0;
}
/* Triple DES CBC Decrypt */
int CRYPT_TDES_CBC_Decrypt(CRYPT_TDES_CTX* tdes, unsigned char* out,
const unsigned char* in, unsigned int inSz)
{
Des3_CbcDecrypt((Des3*)tdes, out, in, inSz);
return 0;
}

View File

@@ -125,6 +125,7 @@ int CRYPT_HUFFMAN_Compress(unsigned char*, unsigned int, const unsigned char*,
int CRYPT_HUFFMAN_DeCompress(unsigned char*, unsigned int, const unsigned char*,
unsigned int);
/* flag to use static huffman */
enum {
CRYPT_HUFFMAN_COMPRESS_STATIC = 1
};
@@ -140,6 +141,25 @@ int CRYPT_RNG_Get(CRYPT_RNG_CTX*, unsigned char*);
int CRYPT_RNG_BlockGenerate(CRYPT_RNG_CTX*, unsigned char*, unsigned int);
/* TDES */
typedef struct CRYPT_TDES_CTX {
int holder[100]; /* big enough to hold internal, but check on init */
} CRYPT_TDES_CTX;
int CRYPT_TDES_KeySet(CRYPT_TDES_CTX*, const unsigned char*,
const unsigned char*, int);
int CRYPT_TDES_IvSet(CRYPT_TDES_CTX*, const unsigned char*);
int CRYPT_TDES_CBC_Encrypt(CRYPT_TDES_CTX*, unsigned char*,
const unsigned char*, unsigned int);
int CRYPT_TDES_CBC_Decrypt(CRYPT_TDES_CTX*, unsigned char*,
const unsigned char*, unsigned int);
/* key direction flags for setup */
enum {
CRYPT_TDES_ENCRYPTION = 0,
CRYPT_TDES_DECRYPTION = 1
};
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -35,6 +35,7 @@
#include <cyassl/ctaocrypt/hmac.h>
#include <cyassl/ctaocrypt/compress.h>
#include <cyassl/ctaocrypt/random.h>
#include <cyassl/ctaocrypt/des3.h>
/* c stdlib headers */
#include <stdio.h>
@@ -48,6 +49,7 @@
#define OUR_DATA_SIZE 1024
static byte ourData[OUR_DATA_SIZE];
static byte* key = NULL;
static byte* iv = NULL;
static int check_md5(void);
static int check_sha(void);
@@ -57,6 +59,7 @@ static int check_sha512(void);
static int check_hmac(void);
static int check_compress(void);
static int check_rng(void);
static int check_des3(void);
int main(int argc, char** argv)
@@ -71,13 +74,19 @@ int main(int argc, char** argv)
DBINIT();
#endif
/* align key pointer */
/* align key, iv pointers */
key = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_KEY);
if (key == NULL) {
printf("mcapi key alloc failed\n");
return -1;
}
iv = (byte*)XMALLOC(16, NULL, DYNAMIC_TYPE_KEY);
if (iv == NULL) {
printf("mcapi iv alloc failed\n");
return -1;
}
for (i = 0; i < OUR_DATA_SIZE; i++)
ourData[i] = (byte)i;
@@ -129,8 +138,15 @@ int main(int argc, char** argv)
return -1;
}
ret = check_des3();
if (ret != 0) {
printf("mcapi check_des3 failed\n");
return -1;
}
XFREE(iv, NULL, DYNAMIC_TYPE_KEY);
XFREE(key, NULL, DYNAMIC_TYPE_KEY);
return 0;
@@ -469,24 +485,24 @@ static int check_rng(void)
ret = CRYPT_RNG_Initialize(&rng);
if (ret != 0) {
printf("mcap rng init failed\n");
printf("mcapi rng init failed\n");
return -1;
}
ret = CRYPT_RNG_Get(&rng, &out[0]);
if (ret != 0) {
printf("mcap rng get failed\n");
printf("mcapi rng get failed\n");
return -1;
}
ret = CRYPT_RNG_BlockGenerate(&rng, out, RANDOM_BYTE_SZ);
if (ret != 0) {
printf("mcap rng block gen failed\n");
printf("mcapi rng block gen failed\n");
return -1;
}
if (memcmp(in, out, RANDOM_BYTE_SZ) == 0) {
printf("mcap rng block gen output failed\n");
printf("mcapi rng block gen output failed\n");
return -1;
}
@@ -496,5 +512,69 @@ static int check_rng(void)
}
#define TDES_TEST_SIZE 32
/* check mcapi des3 */
static int check_des3(void)
{
CRYPT_TDES_CTX mcDes3;
Des3 defDes3;
int ret;
byte out1[TDES_TEST_SIZE];
byte out2[TDES_TEST_SIZE];
strncpy((char*)key, "1234567890abcdefghijklmn", 24);
strncpy((char*)iv, "12345678", 8);
/* cbc encrypt */
ret = CRYPT_TDES_KeySet(&mcDes3, key, iv, CRYPT_TDES_ENCRYPTION);
if (ret != 0) {
printf("mcapi tdes key set failed\n");
return -1;
}
Des3_SetKey(&defDes3, key, iv, DES_ENCRYPTION);
ret = CRYPT_TDES_CBC_Encrypt(&mcDes3, out1, ourData, TDES_TEST_SIZE);
if (ret != 0) {
printf("mcapi tdes cbc encrypt failed\n");
return -1;
}
Des3_CbcEncrypt(&defDes3, out2, ourData, TDES_TEST_SIZE);
if (memcmp(out1, out2, TDES_TEST_SIZE) != 0) {
printf("mcapi tdes cbc encrypt cmp failed\n");
return -1;
}
/* cbc decrypt */
ret = CRYPT_TDES_KeySet(&mcDes3, key, iv, CRYPT_TDES_DECRYPTION);
if (ret != 0) {
printf("mcapi tdes key set failed\n");
return -1;
}
Des3_SetKey(&defDes3, key, iv, DES_DECRYPTION);
ret = CRYPT_TDES_CBC_Decrypt(&mcDes3, out2, out1, TDES_TEST_SIZE);
if (ret != 0) {
printf("mcapi tdes cbc decrypt failed\n");
return -1;
}
Des3_CbcDecrypt(&defDes3, out1, out1, TDES_TEST_SIZE);
if (memcmp(out1, out2, TDES_TEST_SIZE) != 0) {
printf("mcapi tdes cbc decrypt cmp failed\n");
return -1;
}
if (memcmp(out1, ourData, TDES_TEST_SIZE) != 0) {
printf("mcapi tdes cbc decrypt orig cmp failed\n");
return -1;
}
printf("tdes mcapi test passed\n");
return 0;
}