Coldfire SEC

This commit is contained in:
kojo 2013-09-11 17:06:28 +09:00
parent 7e609028bd
commit 0869da34a0
3 changed files with 302 additions and 3 deletions

View File

@ -55,7 +55,7 @@
word32 length); word32 length);
#endif #endif
#ifdef STM32F2_CRYPTO #if STM32F2_CRYPTO
/* /*
* STM32F2 hardware AES support through the STM32F2 standard peripheral * STM32F2 hardware AES support through the STM32F2 standard peripheral
* library. Documentation located in STM32F2xx Standard Peripheral Library * library. Documentation located in STM32F2xx Standard Peripheral Library
@ -419,6 +419,140 @@
#endif /* CYASSL_AES_COUNTER */ #endif /* CYASSL_AES_COUNTER */
#elif defined(HAVE_COLDFIRE_SEC)
#include "sec.h"
#include "mcf548x_sec.h"
#include "mcf548x_siu.h"
#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 ;
#define SEC_DESC_AES_CBC_ENCRYPT 0x60300010
#define SEC_DESC_AES_CBC_DECRYPT 0x60200010
#define AES_BLOCK_LENGTH 16
extern volatile unsigned char __MBAR[];
int AesCbcEncrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
{
//printf("AesCbcEncrypt(%x, %x, %x, %d)\n", aes, po, pi, sz) ;
return(AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_ENCRYPT)) ;
}
int AesCbcDecrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
{
//printf("AesCbcDecrypt(%x, %x, %x, %d)\n", aes, po, pi, 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 */
}
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);
}
if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
return BAD_FUNC_ARG;
if (aes == NULL)
return BAD_FUNC_ARG;
aes->rounds = keylen/4 + 6;
XMEMCPY(aes->key, userKey, keylen);
if (iv)
XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
return 0;
}
#else /* CTaoCrypt software implementation */ #else /* CTaoCrypt software implementation */
static const word32 rcon[] = { static const word32 rcon[] = {

View File

@ -44,6 +44,9 @@
word32 length); word32 length);
#endif #endif
#ifdef STM32F2_CRYPTO #ifdef STM32F2_CRYPTO
/* /*
* STM32F2 hardware DES/3DES support through the STM32F2 standard * STM32F2 hardware DES/3DES support through the STM32F2 standard
@ -260,6 +263,156 @@
Des3Crypt(des, out, in, sz, DES_DECRYPTION); Des3Crypt(des, out, in, sz, DES_DECRYPTION);
} }
#elif defined(HAVE_COLDFIRE_SEC)
#include "sec.h"
#include "mcf548x_sec.h"
#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 ;
#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
extern volatile unsigned char __MBAR[];
static void Des_Cbc(Des* des, byte* out, const byte* in, word32 sz, word32 desc)
{
static volatile SECdescriptorType descriptor = { NULL } ;
int ret ; int stat1,stat2 ;
int i ; int size ;
volatile int v ;
while(sz) {
if((sz%DES_BUFFER_SIZE) == sz) {
size = sz ;
sz = 0 ;
} else {
size = DES_BUFFER_SIZE ;
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 ;
*/
/* Initialize SEC and wait for encryption to complete */
MCF_SEC_CCCR0 = 0x0000001A; //enable channel done notification
/* Point SEC to the location of the descriptor */
MCF_SEC_FR0 = (uint32)&descriptor;
/* poll SISR to determine when channel is complete */
while (!(MCF_SEC_SISRL) && !(MCF_SEC_SISRH))
;
for(v=0; v<500; v++) ;
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) ;
XMEMCPY(out, DesBuffer, 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) ;
} else {
XMEMCPY((void*)des->iv, (void*)&(in[size-DES_IVLEN]), DES_IVLEN) ;
}
in += size ;
out += size ;
}
}
void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
{
Des_Cbc(des, out, in, sz, 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) ;
}
void Des3_CbcEncrypt(Des3* des3, byte* out, const byte* in, word32 sz)
{
Des_Cbc((Des *)des3, out, in, sz, SEC_DESC_DES3_CBC_ENCRYPT) ;
}
void Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz)
{
Des_Cbc((Des *)des3, out, in, sz, SEC_DESC_DES3_CBC_DECRYPT) ;
}
void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
{
int i ; int status ;
if(DesBuffer == NULL) {
status = tx_byte_allocate(&mp_ncached,(void *)&DesBuffer,DES_BUFFER_SIZE,TX_NO_WAIT);
}
XMEMCPY(des->key, key, DES_KEYLEN);
des->keylen = DES_KEYLEN ;
des->ivlen = 0 ;
if (iv) {
XMEMCPY(des->iv, iv, DES_IVLEN);
des->ivlen = DES_IVLEN ;
} else {
for(i=0; i<DES_IVLEN; i++)
des->iv[i] = 0x0 ;
}
}
void Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
{
int i ; int status ;
if(DesBuffer == NULL) {
status = tx_byte_allocate(&mp_ncached,(void *)&DesBuffer,DES_BUFFER_SIZE,TX_NO_WAIT);
}
XMEMCPY(des3->key, key, DES3_KEYLEN);
des3->keylen = DES3_KEYLEN ;
des3->ivlen = 0 ;
if (iv) {
XMEMCPY(des3->iv, iv, DES3_IVLEN);
des3->ivlen = DES3_IVLEN ;
} else {
for(i=0; i<DES_IVLEN; i++)
des3->iv[i] = 0x0 ;
}
}
#else /* CTaoCrypt software implementation */ #else /* CTaoCrypt software implementation */
/* permuted choice table (key) */ /* permuted choice table (key) */

View File

@ -45,6 +45,12 @@ enum {
DES_DECRYPTION = 1 DES_DECRYPTION = 1
}; };
#define DES_IVLEN 8
#define DES_KEYLEN 8
#define DES3_IVLEN 8
#define DES3_KEYLEN 24
#ifdef STM32F2_CRYPTO #ifdef STM32F2_CRYPTO
enum { enum {
DES_CBC = 0, DES_CBC = 0,
@ -54,15 +60,21 @@ enum {
/* DES encryption and decryption */ /* DES encryption and decryption */
typedef struct Des { typedef struct Des {;
word32 key[DES_KS_SIZE];
word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */ word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */
byte keylen ; /* for Coldfire SEC */
byte ivlen ; /* for Coldfire SEC */
byte iv[DES3_IVLEN]; /* for Coldfire SEC */
word32 key[DES_KS_SIZE]
} Des; } Des;
/* DES3 encryption and decryption */ /* DES3 encryption and decryption */
typedef struct Des3 { typedef struct Des3 {
byte keylen ; /* for Coldfire SEC */
byte ivlen ; /* for Coldfire SEC */
byte iv[DES3_IVLEN]; /* for Coldfire SEC */
word32 key[3][DES_KS_SIZE]; word32 key[3][DES_KS_SIZE];
word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */ word32 reg[DES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */ word32 tmp[DES_BLOCK_SIZE / sizeof(word32)]; /* same */