From 1ba30b1eb667458f1c79c756bed51e7def54baad Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Wed, 23 Apr 2014 16:55:18 +0900 Subject: [PATCH 01/12] des3 parity --- src/keys.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/keys.c b/src/keys.c index 463ba9f4c..35d2c28e7 100644 --- a/src/keys.c +++ b/src/keys.c @@ -1473,6 +1473,25 @@ static int SetPrefix(byte* sha_input, int idx) #endif +static void setParity(byte *out, byte *in, int len) /* set parity for DES3 */ +{ + int i, j ; + byte v ; + int bits ; + + for(i=0; i> 1 ; + out[i] = v << 1 ; + bits = 0 ; + for(j=0; j<7; j++) { + bits += (v&0x1) ; + v = v >> 1 ; + } + out[i] |= (1 - (bits&0x1)) ; + } +} + static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs, byte side, void* heap, int devId) { @@ -1605,22 +1624,28 @@ static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs, } #endif if (side == CYASSL_CLIENT_END) { - desRet = Des3_SetKey(enc->des3, keys->client_write_key, + byte key[DES_KEYLEN*3] ; + setParity(key, keys->client_write_key, DES_KEYLEN*3) ; + desRet = Des3_SetKey(enc->des3, key, keys->client_write_IV, DES_ENCRYPTION); if (desRet != 0) return desRet; - desRet = Des3_SetKey(dec->des3, keys->server_write_key, + setParity(key, keys->server_write_key, DES_KEYLEN*3) ; + desRet = Des3_SetKey(dec->des3, key, keys->server_write_IV, DES_DECRYPTION); if (desRet != 0) return desRet; } else { - desRet = Des3_SetKey(enc->des3, keys->server_write_key, + byte key[DES_KEYLEN*3] ; + setParity(key, keys->server_write_key, DES_KEYLEN*3) ; + desRet = Des3_SetKey(enc->des3, key, keys->server_write_IV, DES_ENCRYPTION); if (desRet != 0) return desRet; - desRet = Des3_SetKey(dec->des3, keys->client_write_key, - keys->client_write_IV, DES_DECRYPTION); + setParity(key, keys->client_write_key, DES_KEYLEN*3) ; + desRet = Des3_SetKey(dec->des3, key, + keys->client_write_IV, DES_DECRYPTION); if (desRet != 0) return desRet; } From 41199a480df1851d1492ec6d108084a008784d98 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Wed, 23 Apr 2014 16:56:37 +0900 Subject: [PATCH 02/12] ColdFire SEC, fix cache control in aes, des3 driver --- ctaocrypt/src/aes.c | 231 ++++++++++++++++++++++------------------ ctaocrypt/src/des3.c | 207 ++++++++++++++++++++++------------- cyassl/ctaocrypt/des3.h | 18 +--- 3 files changed, 263 insertions(+), 193 deletions(-) diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index 869203c41..7f4fef43a 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -148,14 +148,14 @@ (cryptoalgo == PIC32_CRYPTOALGO_RCBC)) { /* set iv for the next call */ if(dir == PIC32_ENCRYPTION) { - XMEMCPY((void *)aes->iv_ce, + XMEMCPY((void *)aes->iv_ce, (void*)KVA0_TO_KVA1(out + sz - AES_BLOCK_SIZE), AES_BLOCK_SIZE) ; - } else { + } else { ByteReverseWords((word32*)aes->iv_ce, (word32 *)KVA0_TO_KVA1(in + sz - AES_BLOCK_SIZE), AES_BLOCK_SIZE); - } + } } XMEMCPY((byte *)out, (byte *)KVA0_TO_KVA1(out), sz) ; ByteReverseWords((word32*)out, (word32 *)out, sz); @@ -603,124 +603,152 @@ #endif /* CYASSL_AES_COUNTER */ +#elif defined(HAVE_COLDFIRE_SEC) -#elif defined(HAVE_COLDFIRE_SEC) +#include #include "sec.h" -#include "mcf548x_sec.h" -#include "mcf548x_siu.h" +#include "mcf5475_sec.h" +#include "mcf5475_siu.h" +#if defined (HAVE_THREADX) #include "memory_pools.h" extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */ -#define AES_BUFFER_SIZE (AES_BLOCK_SIZE * 8) -static unsigned char *AESBuffer = NULL ; +#endif +#define AES_BUFFER_SIZE (AES_BLOCK_SIZE * 64) +static unsigned char *AESBuffIn = NULL ; +static unsigned char *AESBuffOut = NULL ; +static byte *secReg ; +static byte *secKey ; +static volatile SECdescriptorType *secDesc ; + +static CyaSSL_Mutex Mutex_AesSEC ; + #define SEC_DESC_AES_CBC_ENCRYPT 0x60300010 #define SEC_DESC_AES_CBC_DECRYPT 0x60200010 -#define AES_BLOCK_LENGTH 16 extern volatile unsigned char __MBAR[]; + +static int TimeCount = 0 ; + +static int AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz, word32 descHeader) +{ + + int i ; int stat1, stat2 ; + int ret ; int size ; + volatile int v ; + + if((pi == NULL) || (po == NULL)) + return BAD_FUNC_ARG;/*wrong pointer*/ + + LockMutex(&Mutex_AesSEC) ; + + /* Set descriptor for SEC */ + secDesc->length1 = 0x0; + secDesc->pointer1 = NULL; + + secDesc->length2 = AES_BLOCK_SIZE; + secDesc->pointer2 = (byte *)secReg ; /* Initial Vector */ + + switch(aes->rounds) { + case 10: secDesc->length3 = 16 ; break ; + case 12: secDesc->length3 = 24 ; break ; + case 14: secDesc->length3 = 32 ; break ; + } + XMEMCPY(secKey, aes->key, secDesc->length3) ; + + secDesc->pointer3 = (byte *)secKey; + secDesc->pointer4 = AESBuffIn ; + secDesc->pointer5 = AESBuffOut ; + secDesc->length6 = 0x0; + secDesc->pointer6 = NULL; + secDesc->length7 = 0x0; + secDesc->pointer7 = NULL; + secDesc->nextDescriptorPtr = NULL; + + while(sz) { + secDesc->header = descHeader ; + XMEMCPY(secReg, aes->reg, AES_BLOCK_SIZE) ; + if((sz%AES_BUFFER_SIZE) == sz) { + size = sz ; + sz = 0 ; + } else { + size = AES_BUFFER_SIZE ; + sz -= AES_BUFFER_SIZE ; + } + secDesc->length4 = size; + secDesc->length5 = size; + + XMEMCPY(AESBuffIn, pi, size) ; + if(descHeader == SEC_DESC_AES_CBC_DECRYPT) { + XMEMCPY((void*)aes->tmp, (void*)&(pi[size-AES_BLOCK_SIZE]), AES_BLOCK_SIZE) ; + } + + /* Point SEC to the location of the descriptor */ + MCF_SEC_FR0 = (uint32)secDesc; + /* Initialize SEC and wait for encryption to complete */ + MCF_SEC_CCCR0 = 0x0000001a; + /* poll SISR to determine when channel is complete */ + v=0 ; + while((secDesc->header>> 24) != 0xff)v++ ; + + ret = MCF_SEC_SISRH; + stat1 = MCF_SEC_AESSR ; + stat2 = MCF_SEC_AESISR ; + if(ret & 0xe0000000) + { + db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, AESISR=%08x\n", i, ret, stat1, stat2) ; + } + + XMEMCPY(po, AESBuffOut, size) ; + + if(descHeader == SEC_DESC_AES_CBC_ENCRYPT) { + XMEMCPY((void*)aes->reg, (void*)&(po[size-AES_BLOCK_SIZE]), AES_BLOCK_SIZE) ; + } else { + XMEMCPY((void*)aes->reg, (void*)aes->tmp, AES_BLOCK_SIZE) ; + } + + pi += size ; + po += size ; + } + UnLockMutex(&Mutex_AesSEC) ; + return 0 ; /* for descriptier header 0xff000000 mode */ +} int AesCbcEncrypt(Aes* aes, byte* po, const byte* pi, word32 sz) { - return(AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_ENCRYPT)) ; + return(AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_ENCRYPT)) ; } int AesCbcDecrypt(Aes* aes, byte* po, const byte* pi, word32 sz) { - return(AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_DECRYPT)) ; -} - -static int AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz, word32 descHeader) -{ - - int i ; int stat1, stat2 ; - int ret ; int size ; - static SECdescriptorType descriptor; - volatile int v ; - - if((pi == NULL) || (po == NULL)) - return BAD_FUNC_ARG;/*wrong pointer*/ - - while(sz) { - if((sz%AES_BUFFER_SIZE) == sz) { - size = sz ; - sz = 0 ; - } else { - size = AES_BUFFER_SIZE ; - sz -= AES_BUFFER_SIZE ; - } - - /* Set descriptor for SEC */ - descriptor.header = descHeader ; - /* - descriptor.length1 = 0x0; - descriptor.pointer1 = NULL; - */ - descriptor.length2 = AES_BLOCK_SIZE; - descriptor.pointer2 = (byte *)aes->reg ; /* Initial Vector */ - - switch(aes->rounds) { - case 10: descriptor.length3 = 16 ; break ; - case 12: descriptor.length3 = 24 ; break ; - case 14: descriptor.length3 = 32 ; break ; - } - - descriptor.pointer3 = (byte *)aes->key; - descriptor.length4 = size; - descriptor.pointer4 = (byte *)pi ; - descriptor.length5 = size; - descriptor.pointer5 = AESBuffer ; - /* - descriptor.length6 = 0x0; - descriptor.pointer6 = NULL; - descriptor.length7 = 0x0; - descriptor.pointer7 = NULL; - descriptor.nextDescriptorPtr = NULL; - */ - - /* Initialize SEC and wait for encryption to complete */ - MCF_SEC_CCCR0 = 0x00000000; - - /* Point SEC to the location of the descriptor */ - MCF_SEC_FR0 = (uint32)&descriptor; - - /* poll SISR to determine when channel is complete */ - i=0 ; - while (!(MCF_SEC_SISRL) && !(MCF_SEC_SISRH))i++ ; - for(v=0; v<100; v++) ; - - ret = MCF_SEC_SISRH; - stat1 = MCF_SEC_AESSR ; - stat2 = MCF_SEC_AESISR ; - if(ret & 0xe0000000) - { - db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, AESISR=%08x\n", i, ret, stat1, stat2) ; - } - - XMEMCPY(po, AESBuffer, size) ; - - if(descHeader == SEC_DESC_AES_CBC_ENCRYPT) { - XMEMCPY((void*)aes->reg, (void*)&(po[size-AES_BLOCK_SIZE]), AES_BLOCK_SIZE) ; - } else { - XMEMCPY((void*)aes->reg, (void*)&(pi[size-AES_BLOCK_SIZE]), AES_BLOCK_SIZE) ; - } - - pi += size ; - po += size ; - } - - return 0 ; /* for descriptier header 0xff000000 mode */ + return(AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_DECRYPT)) ; } int AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv, int dir) { - int status ; - - if(AESBuffer == NULL) { - status = tx_byte_allocate(&mp_ncached,(void *)&AESBuffer, AES_BUFFER_SIZE,TX_NO_WAIT); - } + int s1, s2, s3, s4, s5 ; + + if(AESBuffIn == NULL) { + #if defined (HAVE_THREADX) + s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc, sizeof(SECdescriptorType), TX_NO_WAIT); + s1 = tx_byte_allocate(&mp_ncached,(void *)&AESBuffIn, AES_BUFFER_SIZE, TX_NO_WAIT); + s2 = tx_byte_allocate(&mp_ncached,(void *)&AESBuffOut, AES_BUFFER_SIZE, TX_NO_WAIT); + s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, AES_BLOCK_SIZE*2,TX_NO_WAIT); + s4 = tx_byte_allocate(&mp_ncached,(void *)&secReg, AES_BLOCK_SIZE, TX_NO_WAIT); + TimeCount = 0 ; + + if(s1 || s2 || s3 || s4 || s5) + return BAD_FUNC_ARG; + + #else + #error "Allocate non-Cache buffers" + #endif + + InitMutex(&Mutex_AesSEC) ; + } if (!((keylen == 16) || (keylen == 24) || (keylen == 32))) return BAD_FUNC_ARG; @@ -732,6 +760,7 @@ int AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv, XMEMCPY(aes->key, userKey, keylen); if (iv) XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE); + return 0; } @@ -2724,7 +2753,7 @@ static void GMULT(word64* X, word64* Y) { word64 Z[2] = {0,0}; word64 V[2] ; - int i, j; + int i, j; V[0] = X[0] ; V[1] = X[1] ; for (i = 0; i < 2; i++) @@ -2825,7 +2854,7 @@ static void GHASH(Aes* aes, const byte* a, word32 aSz, /* Hash in the lengths in bits of A and C */ { word64 len[2] ; - len[0] = aSz ; len[1] = cSz; + len[0] = aSz ; len[1] = cSz; /* Lengths are in bytes. Convert to bits. */ len[0] *= 8; @@ -2851,7 +2880,7 @@ static void GMULT(word32* X, word32* Y) int i, j; V[0] = X[0]; V[1] = X[1]; V[2] = X[2]; V[3] = X[3]; - + for (i = 0; i < 4; i++) { word32 y = Y[i]; diff --git a/ctaocrypt/src/des3.c b/ctaocrypt/src/des3.c index dc1aac910..c390d5921 100644 --- a/ctaocrypt/src/des3.c +++ b/ctaocrypt/src/des3.c @@ -273,32 +273,72 @@ return 0; } +#elif defined(HAVE_COLDFIRE_SEC) -#elif defined(HAVE_COLDFIRE_SEC) +#include #include "sec.h" -#include "mcf548x_sec.h" +#include "mcf5475_sec.h" +#include "mcf5475_siu.h" +#if defined (HAVE_THREADX) #include "memory_pools.h" extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */ -#define DES_BUFFER_SIZE (DES_BLOCK_SIZE * 16) -static unsigned char *DesBuffer = NULL ; +#endif + +#define DES_BUFFER_SIZE (DES_BLOCK_SIZE * 64) +static unsigned char *desBuffIn = NULL ; +static unsigned char *desBuffOut = NULL ; +static byte *secIV ; +static byte *secKey ; +static volatile SECdescriptorType *secDesc ; + +static CyaSSL_Mutex Mutex_DesSEC ; #define SEC_DESC_DES_CBC_ENCRYPT 0x20500010 #define SEC_DESC_DES_CBC_DECRYPT 0x20400010 #define SEC_DESC_DES3_CBC_ENCRYPT 0x20700010 #define SEC_DESC_DES3_CBC_DECRYPT 0x20600010 +#define DES_IVLEN 8 +#define DES_KEYLEN 8 +#define DES3_IVLEN 8 +#define DES3_KEYLEN 24 + extern volatile unsigned char __MBAR[]; -static void Des_Cbc(Des* des, byte* out, const byte* in, word32 sz, word32 desc) +static int TimeCount = 0 ; + +static void Des_Cbc(byte* out, const byte* in, word32 sz, + byte *key, byte *iv, word32 desc) { - static volatile SECdescriptorType descriptor = { NULL } ; int ret ; int stat1,stat2 ; int i ; int size ; volatile int v ; - + + LockMutex(&Mutex_DesSEC) ; + + secDesc->length1 = 0x0; + secDesc->pointer1 = NULL; + if((desc==SEC_DESC_DES_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_DECRYPT)){ + secDesc->length2 = DES_IVLEN ; + secDesc->length3 = DES_KEYLEN ; + } else { + secDesc->length2 = DES3_IVLEN ; + secDesc->length3 = DES3_KEYLEN ; + } + secDesc->pointer2 = secIV ; + secDesc->pointer3 = secKey; + secDesc->pointer4 = desBuffIn ; + secDesc->pointer5 = desBuffOut ; + secDesc->length6 = 0; + secDesc->pointer6 = NULL; + secDesc->length7 = 0x0; + secDesc->pointer7 = NULL; + secDesc->nextDescriptorPtr = NULL ; + while(sz) { + XMEMCPY(secIV, iv, secDesc->length2) ; if((sz%DES_BUFFER_SIZE) == sz) { size = sz ; sz = 0 ; @@ -307,38 +347,21 @@ static void Des_Cbc(Des* des, byte* out, const byte* in, word32 sz, word32 desc) sz -= DES_BUFFER_SIZE ; } - descriptor.header = desc ; - /* - escriptor.length1 = 0x0; - descriptor.pointer1 = NULL; - */ - descriptor.length2 = des->ivlen ; - descriptor.pointer2 = (byte *)des->iv ; - descriptor.length3 = des->keylen ; - descriptor.pointer3 = (byte *)des->key; - descriptor.length4 = size; - descriptor.pointer4 = (byte *)in ; - descriptor.length5 = size; - descriptor.pointer5 = DesBuffer ; - /* - descriptor.length6 = 0; - descriptor.pointer6 = NULL; - descriptor.length7 = 0x0; - descriptor.pointer7 = NULL; - descriptor.nextDescriptorPtr = NULL ; - */ + XMEMCPY(desBuffIn, in, size) ; + XMEMCPY(secKey, key, secDesc->length3) ; - /* Initialize SEC and wait for encryption to complete */ - MCF_SEC_CCCR0 = 0x0000001A; //enable channel done notification - + secDesc->header = desc ; + secDesc->length4 = size; + secDesc->length5 = size; /* Point SEC to the location of the descriptor */ - MCF_SEC_FR0 = (uint32)&descriptor; - + MCF_SEC_FR0 = (uint32)secDesc; + /* Initialize SEC and wait for encryption to complete */ + MCF_SEC_CCCR0 = 0x0000001a; /* poll SISR to determine when channel is complete */ - while (!(MCF_SEC_SISRL) && !(MCF_SEC_SISRH)) - ; - - for(v=0; v<500; v++) ; + v=0 ; + while((secDesc->header>> 24) != 0xff) { + if(v++ > 1000)break ; + } ret = MCF_SEC_SISRH; stat1 = MCF_SEC_DSR ; @@ -346,86 +369,120 @@ static void Des_Cbc(Des* des, byte* out, const byte* in, word32 sz, word32 desc) if(ret & 0xe0000000) db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2) ; - XMEMCPY(out, DesBuffer, size) ; + XMEMCPY(out, desBuffOut, size) ; if((desc==SEC_DESC_DES3_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_ENCRYPT)) { - XMEMCPY((void*)des->iv, (void*)&(out[size-DES_IVLEN]), DES_IVLEN) ; + XMEMCPY((void*)iv, (void*)&(out[size-secDesc->length2]), secDesc->length2) ; } else { - XMEMCPY((void*)des->iv, (void*)&(in[size-DES_IVLEN]), DES_IVLEN) ; + XMEMCPY((void*)iv, (void*)&(in[size-secDesc->length2]), secDesc->length2) ; } in += size ; out += size ; } + UnLockMutex(&Mutex_DesSEC) ; + + if((TimeCount++ % 6)==0) + tx_thread_sleep(1) ; /* DELAY */ } void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) { - Des_Cbc(des, out, in, sz, SEC_DESC_DES_CBC_ENCRYPT) ; + Des_Cbc(out, in, sz, des->key, des->reg, SEC_DESC_DES_CBC_ENCRYPT) ; } void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) { - Des_Cbc(des, out, in, sz, SEC_DESC_DES_CBC_DECRYPT) ; + Des_Cbc(out, in, sz, des->key, des->reg, SEC_DESC_DES_CBC_DECRYPT) ; } -int Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz) +void Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz) { - Des_Cbc((Des *)des3, out, in, sz, SEC_DESC_DES3_CBC_ENCRYPT) ; - return 0; -} - -int Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz) -{ - Des_Cbc((Des *)des3, out, in, sz, SEC_DESC_DES3_CBC_DECRYPT) ; - return 0; + Des_Cbc(out, in, sz, des3->key, des3->reg, SEC_DESC_DES3_CBC_ENCRYPT) ; } -int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) +void Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz) { - int i ; int status ; - - if(DesBuffer == NULL) { - status = tx_byte_allocate(&mp_ncached,(void *)&DesBuffer,DES_BUFFER_SIZE,TX_NO_WAIT); + Des_Cbc(out, in, sz, des3->key, des3->reg, SEC_DESC_DES3_CBC_DECRYPT) ; +} + +static void setParity(byte *buf, int len) +{ + int i, j ; + byte v ; + int bits ; + + for(i=0; i> 1 ; + buf[i] = v << 1 ; + bits = 0 ; + for(j=0; j<7; j++) + { + bits += (v&0x1) ; + v = v >> 1 ; + } + buf[i] |= (1 - (bits&0x1)) ; } - XMEMCPY(des->key, key, DES_KEYLEN); - des->keylen = DES_KEYLEN ; - des->ivlen = 0 ; +} + + +void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) +{ + int i ; int s1, s2, s3, s4, s5 ; + + if(desBuffIn == NULL) { + #if defined (HAVE_THREADX) + s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc, + sizeof(SECdescriptorType), TX_NO_WAIT); + s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT); + s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT); + /* Don't know des or des3 to be used. Allocate larger buffers */ + s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT); + s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT); + #else + #error "Allocate non-Cache buffers" + #endif + + InitMutex(&Mutex_DesSEC) ; + } + + XMEMCPY(des->key, key, DES_KEYLEN); if (iv) { - XMEMCPY(des->iv, iv, DES_IVLEN); - des->ivlen = DES_IVLEN ; + XMEMCPY(des->reg, iv, DES_IVLEN); } else { - for(i=0; iiv[i] = 0x0 ; + XMEMSET(des->reg, 0x0, DES_IVLEN) ; } - return 0; } -int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) +void Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) { - int i ; int status ; + int i ; int s1, s2, s3, s4, s5 ; - if(DesBuffer == NULL) { - status = tx_byte_allocate(&mp_ncached,(void *)&DesBuffer,DES_BUFFER_SIZE,TX_NO_WAIT); + if(desBuffIn == NULL) { + s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc, + sizeof(SECdescriptorType), TX_NO_WAIT); + s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT); + s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT); + s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT); + s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT); + + InitMutex(&Mutex_DesSEC) ; } - XMEMCPY(des3->key, key, DES3_KEYLEN); - des3->keylen = DES3_KEYLEN ; - des3->ivlen = 0 ; + XMEMCPY(des3->key[0], key, DES3_KEYLEN); + setParity((byte *)des3->key[0], DES3_KEYLEN) ; if (iv) { - XMEMCPY(des3->iv, iv, DES3_IVLEN); - des3->ivlen = DES3_IVLEN ; + XMEMCPY(des3->reg, iv, DES3_IVLEN); } else { - for(i=0; iiv[i] = 0x0 ; + XMEMSET(des3->reg, 0x0, DES3_IVLEN) ; } - return 0; } #elif defined FREESCALE_MMCAU diff --git a/cyassl/ctaocrypt/des3.h b/cyassl/ctaocrypt/des3.h index 5a95851d6..a4ceee741 100644 --- a/cyassl/ctaocrypt/des3.h +++ b/cyassl/ctaocrypt/des3.h @@ -40,17 +40,11 @@ enum { DES3_ENC_TYPE = 3, /* cipher unique type */ DES_BLOCK_SIZE = 8, DES_KS_SIZE = 32, - + DES_KEYLEN = 8 , DES_ENCRYPTION = 0, DES_DECRYPTION = 1 }; -#define DES_IVLEN 8 -#define DES_KEYLEN 8 -#define DES3_IVLEN 8 -#define DES3_KEYLEN 24 - - #ifdef STM32F2_CRYPTO enum { DES_CBC = 0, @@ -63,22 +57,12 @@ enum { typedef struct Des { word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */ -#ifdef HAVE_COLDFIRE_SEC - byte keylen ; /* for Coldfire SEC */ - byte ivlen ; /* for Coldfire SEC */ - byte iv[DES3_IVLEN]; /* for Coldfire SEC */ -#endif word32 key[DES_KS_SIZE]; } Des; /* DES3 encryption and decryption */ typedef struct Des3 { -#ifdef HAVE_COLDFIRE_SEC - byte keylen ; /* for Coldfire SEC */ - byte ivlen ; /* for Coldfire SEC */ - byte iv[DES3_IVLEN]; /* for Coldfire SEC */ -#endif word32 key[3][DES_KS_SIZE]; word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */ From a67cb1207b67e4ca09ccb9830e3590bb9358a638 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Wed, 23 Apr 2014 20:45:19 +0900 Subject: [PATCH 03/12] ColdFire SEC for aes, des3: Eliminated type mismatch --- ctaocrypt/src/aes.c | 4 ++-- ctaocrypt/src/des3.c | 53 +++++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index 7f4fef43a..7c1af0ba3 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -698,7 +698,7 @@ static int AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz, word32 des stat2 = MCF_SEC_AESISR ; if(ret & 0xe0000000) { - db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, AESISR=%08x\n", i, ret, stat1, stat2) ; + /* db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, AESISR=%08x\n", i, ret, stat1, stat2) ; */ } XMEMCPY(po, AESBuffOut, size) ; @@ -744,7 +744,7 @@ int AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv, return BAD_FUNC_ARG; #else - #error "Allocate non-Cache buffers" + #warning "Allocate non-Cache buffers" #endif InitMutex(&Mutex_AesSEC) ; diff --git a/ctaocrypt/src/des3.c b/ctaocrypt/src/des3.c index c390d5921..6153402c1 100644 --- a/ctaocrypt/src/des3.c +++ b/ctaocrypt/src/des3.c @@ -307,13 +307,11 @@ static CyaSSL_Mutex Mutex_DesSEC ; extern volatile unsigned char __MBAR[]; -static int TimeCount = 0 ; - static void Des_Cbc(byte* out, const byte* in, word32 sz, byte *key, byte *iv, word32 desc) { - int ret ; int stat1,stat2 ; - int i ; int size ; + int ret ; /* int stat1,stat2 ; */ + int size ; volatile int v ; LockMutex(&Mutex_DesSEC) ; @@ -364,11 +362,12 @@ static void Des_Cbc(byte* out, const byte* in, word32 sz, } ret = MCF_SEC_SISRH; - stat1 = MCF_SEC_DSR ; - stat2 = MCF_SEC_DISR ; - if(ret & 0xe0000000) - db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2) ; - + /* stat1 = MCF_SEC_DSR ; */ + /* stat2 = MCF_SEC_DISR ; */ + if(ret & 0xe0000000) { + /* db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2) ; */ + } + XMEMCPY(out, desBuffOut, size) ; if((desc==SEC_DESC_DES3_CBC_ENCRYPT)||(desc==SEC_DESC_DES_CBC_ENCRYPT)) { @@ -383,30 +382,30 @@ static void Des_Cbc(byte* out, const byte* in, word32 sz, } UnLockMutex(&Mutex_DesSEC) ; - if((TimeCount++ % 6)==0) - tx_thread_sleep(1) ; /* DELAY */ } void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz) { - Des_Cbc(out, in, sz, des->key, des->reg, SEC_DESC_DES_CBC_ENCRYPT) ; + Des_Cbc(out, in, sz, (byte *)des->key, (byte *)des->reg, SEC_DESC_DES_CBC_ENCRYPT) ; } void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz) { - Des_Cbc(out, in, sz, des->key, des->reg, SEC_DESC_DES_CBC_DECRYPT) ; + Des_Cbc(out, in, sz, (byte *)des->key, (byte *)des->reg, SEC_DESC_DES_CBC_DECRYPT) ; } -void Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz) +int Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz) { - Des_Cbc(out, in, sz, des3->key, des3->reg, SEC_DESC_DES3_CBC_ENCRYPT) ; + Des_Cbc(out, in, sz, (byte *)des3->key, (byte *)des3->reg, SEC_DESC_DES3_CBC_ENCRYPT) ; + return 0; } -void Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz) +int Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz) { - Des_Cbc(out, in, sz, des3->key, des3->reg, SEC_DESC_DES3_CBC_DECRYPT) ; + Des_Cbc(out, in, sz, (byte *)des3->key, (byte *)des3->reg, SEC_DESC_DES3_CBC_DECRYPT) ; + return 0; } static void setParity(byte *buf, int len) @@ -431,12 +430,11 @@ static void setParity(byte *buf, int len) } -void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) +int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) { - int i ; int s1, s2, s3, s4, s5 ; - if(desBuffIn == NULL) { #if defined (HAVE_THREADX) + int s1, s2, s3, s4, s5 ; s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc, sizeof(SECdescriptorType), TX_NO_WAIT); s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT); @@ -445,7 +443,7 @@ void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT); s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT); #else - #error "Allocate non-Cache buffers" + #warning "Allocate non-Cache buffers" #endif InitMutex(&Mutex_DesSEC) ; @@ -457,21 +455,25 @@ void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) } else { XMEMSET(des->reg, 0x0, DES_IVLEN) ; } - + return 0; } -void Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) +int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) { - int i ; int s1, s2, s3, s4, s5 ; if(desBuffIn == NULL) { + #if defined (HAVE_THREADX) + int s1, s2, s3, s4, s5 ; s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc, sizeof(SECdescriptorType), TX_NO_WAIT); s1 = tx_byte_allocate(&mp_ncached,(void *)&desBuffIn, DES_BUFFER_SIZE, TX_NO_WAIT); s2 = tx_byte_allocate(&mp_ncached,(void *)&desBuffOut, DES_BUFFER_SIZE, TX_NO_WAIT); s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, DES3_KEYLEN,TX_NO_WAIT); s4 = tx_byte_allocate(&mp_ncached,(void *)&secIV, DES3_IVLEN, TX_NO_WAIT); - + #else + #warning "Allocate non-Cache buffers" + #endif + InitMutex(&Mutex_DesSEC) ; } @@ -482,6 +484,7 @@ void Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) } else { XMEMSET(des3->reg, 0x0, DES3_IVLEN) ; } + return 0; } From e3becc3a653239a9305204f8c053ef37941b5e52 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Fri, 25 Apr 2014 13:35:35 +0900 Subject: [PATCH 04/12] des3 parity. internal.h to ctaocrypt/types.h --- ctaocrypt/src/aes.c | 21 +++++++++++---------- ctaocrypt/src/des3.c | 23 +++++++++++++++-------- src/keys.c | 35 +++++------------------------------ 3 files changed, 31 insertions(+), 48 deletions(-) diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index 7c1af0ba3..3ed21b4c5 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -605,7 +605,7 @@ #elif defined(HAVE_COLDFIRE_SEC) -#include +#include #include "sec.h" #include "mcf5475_sec.h" @@ -629,14 +629,14 @@ static CyaSSL_Mutex Mutex_AesSEC ; #define SEC_DESC_AES_CBC_DECRYPT 0x60200010 extern volatile unsigned char __MBAR[]; - -static int TimeCount = 0 ; static int AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz, word32 descHeader) { + #ifdef DEBUG_CYASSL + int i ; int stat1, stat2 ; int ret ; + #endif - int i ; int stat1, stat2 ; - int ret ; int size ; + int size ; volatile int v ; if((pi == NULL) || (po == NULL)) @@ -693,13 +693,15 @@ static int AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz, word32 des v=0 ; while((secDesc->header>> 24) != 0xff)v++ ; +#ifdef DEBUG_CYASSL ret = MCF_SEC_SISRH; stat1 = MCF_SEC_AESSR ; - stat2 = MCF_SEC_AESISR ; + stat2 = MCF_SEC_AESISR ; if(ret & 0xe0000000) { - /* db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, AESISR=%08x\n", i, ret, stat1, stat2) ; */ + db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, AESISR=%08x\n", i, ret, stat1, stat2) ; } +#endif XMEMCPY(po, AESBuffOut, size) ; @@ -713,7 +715,7 @@ static int AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz, word32 des po += size ; } UnLockMutex(&Mutex_AesSEC) ; - return 0 ; /* for descriptier header 0xff000000 mode */ + return 0 ; } int AesCbcEncrypt(Aes* aes, byte* po, const byte* pi, word32 sz) @@ -729,16 +731,15 @@ int AesCbcDecrypt(Aes* aes, byte* po, const byte* pi, word32 sz) int AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv, int dir) { - int s1, s2, s3, s4, s5 ; if(AESBuffIn == NULL) { #if defined (HAVE_THREADX) + int s1, s2, s3, s4, s5 ; s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc, sizeof(SECdescriptorType), TX_NO_WAIT); s1 = tx_byte_allocate(&mp_ncached,(void *)&AESBuffIn, AES_BUFFER_SIZE, TX_NO_WAIT); s2 = tx_byte_allocate(&mp_ncached,(void *)&AESBuffOut, AES_BUFFER_SIZE, TX_NO_WAIT); s3 = tx_byte_allocate(&mp_ncached,(void *)&secKey, AES_BLOCK_SIZE*2,TX_NO_WAIT); s4 = tx_byte_allocate(&mp_ncached,(void *)&secReg, AES_BLOCK_SIZE, TX_NO_WAIT); - TimeCount = 0 ; if(s1 || s2 || s3 || s4 || s5) return BAD_FUNC_ARG; diff --git a/ctaocrypt/src/des3.c b/ctaocrypt/src/des3.c index 6153402c1..6b1987d47 100644 --- a/ctaocrypt/src/des3.c +++ b/ctaocrypt/src/des3.c @@ -275,7 +275,7 @@ #elif defined(HAVE_COLDFIRE_SEC) -#include +#include #include "sec.h" #include "mcf5475_sec.h" @@ -310,7 +310,9 @@ extern volatile unsigned char __MBAR[]; static void Des_Cbc(byte* out, const byte* in, word32 sz, byte *key, byte *iv, word32 desc) { - int ret ; /* int stat1,stat2 ; */ + #ifdef DEBUG_CYASSL + int ret ; int stat1,stat2 ; + #endif int size ; volatile int v ; @@ -360,13 +362,15 @@ static void Des_Cbc(byte* out, const byte* in, word32 sz, while((secDesc->header>> 24) != 0xff) { if(v++ > 1000)break ; } - + +#ifdef DEBUG_CYASSL ret = MCF_SEC_SISRH; - /* stat1 = MCF_SEC_DSR ; */ - /* stat2 = MCF_SEC_DISR ; */ + stat1 = MCF_SEC_DSR ; + stat2 = MCF_SEC_DISR ; if(ret & 0xe0000000) { /* db_printf("Des_Cbc(%x):ISRH=%08x, DSR=%08x, DISR=%08x\n", desc, ret, stat1, stat2) ; */ } +#endif XMEMCPY(out, desBuffOut, size) ; @@ -448,8 +452,10 @@ int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) InitMutex(&Mutex_DesSEC) ; } - - XMEMCPY(des->key, key, DES_KEYLEN); + + XMEMCPY(des->key, key, DES_KEYLEN); + setParity((byte *)des->key, DES_KEYLEN) ; + if (iv) { XMEMCPY(des->reg, iv, DES_IVLEN); } else { @@ -478,7 +484,8 @@ int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) } XMEMCPY(des3->key[0], key, DES3_KEYLEN); - setParity((byte *)des3->key[0], DES3_KEYLEN) ; + setParity((byte *)des3->key[0], DES3_KEYLEN) ; + if (iv) { XMEMCPY(des3->reg, iv, DES3_IVLEN); } else { diff --git a/src/keys.c b/src/keys.c index 35d2c28e7..463ba9f4c 100644 --- a/src/keys.c +++ b/src/keys.c @@ -1473,25 +1473,6 @@ static int SetPrefix(byte* sha_input, int idx) #endif -static void setParity(byte *out, byte *in, int len) /* set parity for DES3 */ -{ - int i, j ; - byte v ; - int bits ; - - for(i=0; i> 1 ; - out[i] = v << 1 ; - bits = 0 ; - for(j=0; j<7; j++) { - bits += (v&0x1) ; - v = v >> 1 ; - } - out[i] |= (1 - (bits&0x1)) ; - } -} - static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs, byte side, void* heap, int devId) { @@ -1624,28 +1605,22 @@ static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs, } #endif if (side == CYASSL_CLIENT_END) { - byte key[DES_KEYLEN*3] ; - setParity(key, keys->client_write_key, DES_KEYLEN*3) ; - desRet = Des3_SetKey(enc->des3, key, + desRet = Des3_SetKey(enc->des3, keys->client_write_key, keys->client_write_IV, DES_ENCRYPTION); if (desRet != 0) return desRet; - setParity(key, keys->server_write_key, DES_KEYLEN*3) ; - desRet = Des3_SetKey(dec->des3, key, + desRet = Des3_SetKey(dec->des3, keys->server_write_key, keys->server_write_IV, DES_DECRYPTION); if (desRet != 0) return desRet; } else { - byte key[DES_KEYLEN*3] ; - setParity(key, keys->server_write_key, DES_KEYLEN*3) ; - desRet = Des3_SetKey(enc->des3, key, + desRet = Des3_SetKey(enc->des3, keys->server_write_key, keys->server_write_IV, DES_ENCRYPTION); if (desRet != 0) return desRet; - setParity(key, keys->client_write_key, DES_KEYLEN*3) ; - desRet = Des3_SetKey(dec->des3, key, - keys->client_write_IV, DES_DECRYPTION); + desRet = Des3_SetKey(dec->des3, keys->client_write_key, + keys->client_write_IV, DES_DECRYPTION); if (desRet != 0) return desRet; } From 11aff767fdab7ed8159e94de923799d617d45bc8 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Fri, 25 Apr 2014 13:50:12 +0900 Subject: [PATCH 05/12] corrected pic32mz header path --- ctaocrypt/src/aes.c | 2 +- ctaocrypt/src/des3.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index 3ed21b4c5..89a7aa915 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -63,7 +63,7 @@ #if defined(CYASSL_PIC32MZ_CRYPT) -#include "../../cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h" +#include "cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h" #define DEBUG_CYASSL /* core hardware crypt engine driver */ diff --git a/ctaocrypt/src/des3.c b/ctaocrypt/src/des3.c index 6b1987d47..a704b7910 100644 --- a/ctaocrypt/src/des3.c +++ b/ctaocrypt/src/des3.c @@ -685,7 +685,7 @@ int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) #elif defined(CYASSL_PIC32MZ_CRYPT) - #include "../../cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h" + #include "cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h" void Des_SetIV(Des* des, const byte* iv); int Des3_SetIV(Des3* des, const byte* iv); From 246067971827ccbf1fee649bb4c04b5d084e14ae Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Fri, 25 Apr 2014 14:11:56 +0900 Subject: [PATCH 06/12] des3.h --- cyassl/ctaocrypt/des3.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cyassl/ctaocrypt/des3.h b/cyassl/ctaocrypt/des3.h index a4ceee741..7d9345fc1 100644 --- a/cyassl/ctaocrypt/des3.h +++ b/cyassl/ctaocrypt/des3.h @@ -1,6 +1,6 @@ /* des3.h * - * Copyright (C) 2006-2014 wolfSSL Inc. + * Copyright (C) 2006-2013 wolfSSL Inc. * * This file is part of CyaSSL. * @@ -40,11 +40,17 @@ enum { DES3_ENC_TYPE = 3, /* cipher unique type */ DES_BLOCK_SIZE = 8, DES_KS_SIZE = 32, - DES_KEYLEN = 8 , + DES_ENCRYPTION = 0, DES_DECRYPTION = 1 }; +#define DES_IVLEN 8 +#define DES_KEYLEN 8 +#define DES3_IVLEN 8 +#define DES3_KEYLEN 24 + + #ifdef STM32F2_CRYPTO enum { DES_CBC = 0, From cb70631512d672f4fbf8f997f47b552a477c8e8a Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Fri, 25 Apr 2014 14:18:23 +0900 Subject: [PATCH 07/12] aes.c indentation --- ctaocrypt/src/aes.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index 89a7aa915..1184311cd 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -148,14 +148,14 @@ (cryptoalgo == PIC32_CRYPTOALGO_RCBC)) { /* set iv for the next call */ if(dir == PIC32_ENCRYPTION) { - XMEMCPY((void *)aes->iv_ce, + XMEMCPY((void *)aes->iv_ce, (void*)KVA0_TO_KVA1(out + sz - AES_BLOCK_SIZE), AES_BLOCK_SIZE) ; - } else { + } else { ByteReverseWords((word32*)aes->iv_ce, (word32 *)KVA0_TO_KVA1(in + sz - AES_BLOCK_SIZE), AES_BLOCK_SIZE); - } + } } XMEMCPY((byte *)out, (byte *)KVA0_TO_KVA1(out), sz) ; ByteReverseWords((word32*)out, (word32 *)out, sz); @@ -2754,7 +2754,7 @@ static void GMULT(word64* X, word64* Y) { word64 Z[2] = {0,0}; word64 V[2] ; - int i, j; + int i, j; V[0] = X[0] ; V[1] = X[1] ; for (i = 0; i < 2; i++) @@ -2855,7 +2855,7 @@ static void GHASH(Aes* aes, const byte* a, word32 aSz, /* Hash in the lengths in bits of A and C */ { word64 len[2] ; - len[0] = aSz ; len[1] = cSz; + len[0] = aSz ; len[1] = cSz; /* Lengths are in bytes. Convert to bits. */ len[0] *= 8; @@ -2881,7 +2881,7 @@ static void GMULT(word32* X, word32* Y) int i, j; V[0] = X[0]; V[1] = X[1]; V[2] = X[2]; V[3] = X[3]; - + for (i = 0; i < 4; i++) { word32 y = Y[i]; From 6e3bbd135eafe2d50b6c1fdb02c22ab1bdd9111d Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Fri, 25 Apr 2014 14:21:28 +0900 Subject: [PATCH 08/12] des3.h --- cyassl/ctaocrypt/des3.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cyassl/ctaocrypt/des3.h b/cyassl/ctaocrypt/des3.h index 7d9345fc1..13da7e28a 100644 --- a/cyassl/ctaocrypt/des3.h +++ b/cyassl/ctaocrypt/des3.h @@ -1,6 +1,6 @@ /* des3.h * - * Copyright (C) 2006-2013 wolfSSL Inc. + * Copyright (C) 2006-2014 wolfSSL Inc. * * This file is part of CyaSSL. * From f9ad1888cbe94430f340ffa200da2f5019befe04 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Mon, 28 Apr 2014 10:51:18 +0900 Subject: [PATCH 09/12] Fixed pic32mx/mz clock for benchmark --- ctaocrypt/benchmark/benchmark.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ctaocrypt/benchmark/benchmark.c b/ctaocrypt/benchmark/benchmark.c index 2cccd362a..8afe9bfbf 100644 --- a/ctaocrypt/benchmark/benchmark.c +++ b/ctaocrypt/benchmark/benchmark.c @@ -1166,10 +1166,10 @@ void bench_eccKeyAgree(void) #elif defined MICROCHIP_PIC32 #if defined(CYASSL_MICROCHIP_PIC32MZ) - #define CLOCK 8000000.0 + #define CLOCK 80000000.0 #else #include - #define CLOCK 4000000.0 + #define CLOCK 40000000.0 #endif double current_time(int reset) From 6cf1c8149b2804a42524476597cc81912d2857a8 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Mon, 28 Apr 2014 10:52:35 +0900 Subject: [PATCH 10/12] AesCbcEncrypt/Decrypt return value --- ctaocrypt/src/aes.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index 1184311cd..bc38012a4 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -165,12 +165,14 @@ { AesCrypt(aes, out, in, sz, PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCBC ); + return 0 ; } int AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) { AesCrypt(aes, out, in, sz, PIC32_DECRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCBC); + return 0 ; } #if defined(CYASSL_AES_COUNTER) From 20f12af8ec160b60eaf82d1699e221e376cb214b Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Mon, 28 Apr 2014 15:05:44 +0900 Subject: [PATCH 11/12] crypto.h --- mplabx/crypto.h | 82 ------------------------------------------------- 1 file changed, 82 deletions(-) delete mode 100644 mplabx/crypto.h diff --git a/mplabx/crypto.h b/mplabx/crypto.h deleted file mode 100644 index 76dccadff..000000000 --- a/mplabx/crypto.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * File: crypto.h - * Author: C15009 - * - * Created on July 23, 2013, 12:26 PM - */ - -#ifndef CRYPTO_H -#define CRYPTO_H - -#ifdef __cplusplus -extern "C" { -#endif - - typedef struct saCtrl { - unsigned int CRYPTOALGO : 4; - unsigned int MULTITASK : 3; - unsigned int KEYSIZE : 2; - unsigned int ENCTYPE : 1; - unsigned int ALGO : 7; - unsigned int : 3; - unsigned int FLAGS : 1; - unsigned int FB : 1; - unsigned int LOADIV : 1; - unsigned int LNC : 1; - unsigned int IRFLAG : 1; - unsigned int ICVONLY : 1; - unsigned int OR_EN : 1; - unsigned int NO_RX : 1; - unsigned int : 1; - unsigned int VERIFY : 1; - unsigned int : 2; - } saCtrl; - - typedef struct securityAssociation { - saCtrl SA_CTRL; - unsigned int SA_AUTHKEY[8]; - unsigned int SA_ENCKEY[8]; - unsigned int SA_AUTHIV[8]; - unsigned int SA_ENCIV[4]; - } securityAssociation; - - typedef struct bdCtrl { - unsigned int BUFLEN : 16; - unsigned int CBD_INT_EN : 1; - unsigned int PKT_INT_EN : 1; - unsigned int LIFM : 1; - unsigned int LAST_BD: 1; - unsigned int : 2; - unsigned int SA_FETCH_EN : 1; - unsigned int : 4; - unsigned int CRY_MODE: 3; - unsigned int : 1; - unsigned int DESC_EN : 1; - /* Naveen did this - unsigned int CRDMA_EN: 1; - unsigned int UPD_RES : 1; - unsigned int SA_FETCH_EN : 1; - unsigned int SEC_CODE : 1; - unsigned int : 7; - unsigned int DESC_EN : 1; */ - } bdCtrl; - - typedef struct bufferDescriptor { - bdCtrl BD_CTRL; -// unsigned int BD_CTRL; - unsigned int SA_ADDR; - unsigned int SRCADDR; - unsigned int DSTADDR; - unsigned int NXTPTR; - unsigned int UPDPTR; - unsigned int MSGLEN; - unsigned int ENCOFF; - } bufferDescriptor; - - -#ifdef __cplusplus -} -#endif - -#endif /* CRYPTO_H */ - From 6ac7b056ee5393026133cc479ab1a1521a03cbb9 Mon Sep 17 00:00:00 2001 From: Takashi Kojo Date: Mon, 5 May 2014 16:41:07 +0900 Subject: [PATCH 12/12] Pic benchmark, clock config --- ctaocrypt/src/port/pic32/pic32mz-hash.c | 12 ++++++++---- mplabx/MZ-configBits.h | 11 +++++++++++ mplabx/PIC32MZ-serial.h | 4 ++-- mplabx/README | 2 ++ mplabx/benchmark_main.c | 14 +++++++++++--- 5 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 mplabx/MZ-configBits.h diff --git a/ctaocrypt/src/port/pic32/pic32mz-hash.c b/ctaocrypt/src/port/pic32/pic32mz-hash.c index 7511725c7..4ee7eaf16 100644 --- a/ctaocrypt/src/port/pic32/pic32mz-hash.c +++ b/ctaocrypt/src/port/pic32/pic32mz-hash.c @@ -187,21 +187,23 @@ void Md5Final(Md5* md5, byte* hash) #endif #ifndef NO_SHA -void InitSha(Sha* sha) +int InitSha(Sha* sha) { CYASSL_ENTER("InitSha\n") ; XMEMSET((void *)sha, 0xcc, sizeof(Sha)) ; XMEMSET((void *)KVA0_TO_KVA1(sha), 0xcc, sizeof(Sha)) ; reset_engine(&(sha->desc), PIC32_ALGO_SHA1) ; + return 0; } -void ShaUpdate(Sha* sha, const byte* data, word32 len) +int ShaUpdate(Sha* sha, const byte* data, word32 len) { CYASSL_ENTER("ShaUpdate\n") ; update_engine(&(sha->desc), data, len, sha->digest) ; + return 0; } -void ShaFinal(Sha* sha, byte* hash) +int ShaFinal(Sha* sha, byte* hash) { CYASSL_ENTER("ShaFinal\n") ; start_engine(&(sha->desc)) ; @@ -209,16 +211,18 @@ void ShaFinal(Sha* sha, byte* hash) XMEMCPY(hash, sha->digest, SHA1_HASH_SIZE) ; InitSha(sha); /* reset state */ + return 0; } #endif /* NO_SHA */ #ifndef NO_SHA256 -void InitSha256(Sha256* sha256) +int InitSha256(Sha256* sha256) { CYASSL_ENTER("InitSha256\n") ; XMEMSET((void *)sha256, 0xcc, sizeof(Sha256)) ; XMEMSET((void *)KVA0_TO_KVA1(sha256), 0xcc, sizeof(Sha256)) ; reset_engine(&(sha256->desc), PIC32_ALGO_SHA256) ; + return 0; } int Sha256Update(Sha256* sha256, const byte* data, word32 len) diff --git a/mplabx/MZ-configBits.h b/mplabx/MZ-configBits.h new file mode 100644 index 000000000..6aac8113a --- /dev/null +++ b/mplabx/MZ-configBits.h @@ -0,0 +1,11 @@ + /* Config bits for PI32MZ, Starter Kit */ + #pragma config FPLLIDIV = DIV_1 // System PLL Input Divider (1x Divider) + #pragma config FPLLRNG = RANGE_5_10_MHZ + #pragma config FPLLICLK = PLL_FRC // System PLL Input Clock Selection (FRC is input to the System PLL) + #pragma config FPLLMULT = MUL_50 // System PLL Multiplier (PLL Multiply by 50) + #pragma config FPLLODIV = DIV_2 + // DEVCFG1 + #pragma config FNOSC = SPLL // Oscillator Selection (System PLL) + + #pragma config ICESEL = ICS_PGx2 + /* ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2) */ \ No newline at end of file diff --git a/mplabx/PIC32MZ-serial.h b/mplabx/PIC32MZ-serial.h index 323de525b..97bd1e0b5 100644 --- a/mplabx/PIC32MZ-serial.h +++ b/mplabx/PIC32MZ-serial.h @@ -7,12 +7,12 @@ static void init_serial() { SYSKEY = 0x00000000; SYSKEY = 0xAA996655; SYSKEY = 0x556699AA; - PB2DIV = 0x00008018; + PB2DIV = 0x00008808; SYSKEY = 0x33333333; /* UART2 Init */ // U2BRG = 0x0C; - U2BRG = 0x7; + U2BRG = 0x047; ANSELBCLR = 0x4000; ANSELGCLR = 0x0040; RPB14R = 0x02; diff --git a/mplabx/README b/mplabx/README index 715c227bf..ab1e70609 100644 --- a/mplabx/README +++ b/mplabx/README @@ -33,6 +33,8 @@ Included Project Files 3. CTaoCrypt Benchmark App (ctaocrypt_benchmark.X) This project builds the CTaoCrypt benchmark application. + For the benchmark timer, adjust CLOCK value under + "#elif defined MICROCHIP_PIC32" in ctaocrypt/benchmark/benchmark.c PIC32MX/PIC32MZ --------------- diff --git a/mplabx/benchmark_main.c b/mplabx/benchmark_main.c index 4d115de78..7610a3142 100644 --- a/mplabx/benchmark_main.c +++ b/mplabx/benchmark_main.c @@ -27,8 +27,9 @@ #if defined(CYASSL_MICROCHIP_PIC32MZ) #define MICROCHIP_PIC32 #include - #pragma config ICESEL = ICS_PGx2 - /* ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2) */ + + #include "MZ-configBits.h" + #include "PIC32MZ-serial.h" #define SYSTEMConfigPerformance /* void out SYSTEMConfigPerformance(); */ #else @@ -66,11 +67,18 @@ void bench_eccKeyAgree(void); int main(int argc, char** argv) { volatile int i ; int j ; - + + PRECONbits.PFMWS = 2; + PRECONbits.PREFEN = 0b11; + init_serial() ; /* initialize PIC32MZ serial I/O */ SYSTEMConfigPerformance(80000000); DBINIT(); + for(j=0; j<100; j++) { + for(i=0; i<10000000; i++); + printf("time=%f\n", current_time(0)) ; + } printf("wolfCrypt Benchmark:\n"); #ifndef NO_AES