forked from wolfSSL/wolfssl
Merge branch 'master' of github.com:cyassl/cyassl
This commit is contained in:
@@ -55,6 +55,184 @@
|
||||
word32 length);
|
||||
#endif
|
||||
|
||||
#if defined(CYASSL_PIC32MZ_CRYPT)
|
||||
|
||||
#include "../../cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h"
|
||||
#define DEBUG_CYASSL
|
||||
|
||||
/* core hardware crypt engine driver */
|
||||
static void AesCrypt(Aes *aes, byte* out, const byte* in, word32 sz,
|
||||
int dir, int algo, int cryptoalgo)
|
||||
{
|
||||
securityAssociation *sa_p ;
|
||||
bufferDescriptor *bd_p ;
|
||||
|
||||
volatile securityAssociation sa __attribute__((aligned (8)));
|
||||
volatile bufferDescriptor bd __attribute__((aligned (8)));
|
||||
volatile int k ;
|
||||
|
||||
/* get uncached address */
|
||||
sa_p = KVA0_TO_KVA1(&sa) ;
|
||||
bd_p = KVA0_TO_KVA1(&bd) ;
|
||||
|
||||
/* Sync cache and physical memory */
|
||||
if(PIC32MZ_IF_RAM(in)) {
|
||||
XMEMCPY((void *)KVA0_TO_KVA1(in), (void *)in, sz);
|
||||
}
|
||||
XMEMSET((void *)KVA0_TO_KVA1(out), 0, sz);
|
||||
/* Set up the Security Association */
|
||||
XMEMSET((byte *)KVA0_TO_KVA1(&sa), 0, sizeof(sa));
|
||||
sa_p->SA_CTRL.ALGO = algo ; /* AES */
|
||||
sa_p->SA_CTRL.LNC = 1;
|
||||
sa_p->SA_CTRL.LOADIV = 1;
|
||||
sa_p->SA_CTRL.FB = 1;
|
||||
sa_p->SA_CTRL.ENCTYPE = dir ; /* Encryption/Decryption */
|
||||
sa_p->SA_CTRL.CRYPTOALGO = cryptoalgo;
|
||||
|
||||
if(cryptoalgo == PIC32_CRYPTOALGO_AES_GCM){
|
||||
switch(aes->keylen) {
|
||||
case 32:
|
||||
sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_256 ;
|
||||
break ;
|
||||
case 24:
|
||||
sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_192 ;
|
||||
break ;
|
||||
case 16:
|
||||
sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_128 ;
|
||||
break ;
|
||||
}
|
||||
} else
|
||||
sa_p->SA_CTRL.KEYSIZE = PIC32_AES_KEYSIZE_128 ;
|
||||
|
||||
ByteReverseWords(
|
||||
(word32 *)KVA0_TO_KVA1(sa.SA_ENCKEY + 8 - aes->keylen/sizeof(word32)),
|
||||
(word32 *)aes->key_ce, aes->keylen);
|
||||
ByteReverseWords(
|
||||
(word32*)KVA0_TO_KVA1(sa.SA_ENCIV), (word32 *)aes->iv_ce, 16);
|
||||
|
||||
XMEMSET((byte *)KVA0_TO_KVA1(&bd), 0, sizeof(bd));
|
||||
/* Set up the Buffer Descriptor */
|
||||
bd_p->BD_CTRL.BUFLEN = sz;
|
||||
if(cryptoalgo == PIC32_CRYPTOALGO_AES_GCM) {
|
||||
if(sz % 0x10)
|
||||
bd_p->BD_CTRL.BUFLEN = (sz/0x10 + 1) * 0x10 ;
|
||||
}
|
||||
bd_p->BD_CTRL.LIFM = 1;
|
||||
bd_p->BD_CTRL.SA_FETCH_EN = 1;
|
||||
bd_p->BD_CTRL.LAST_BD = 1;
|
||||
bd_p->BD_CTRL.DESC_EN = 1;
|
||||
|
||||
bd_p->SA_ADDR = (unsigned int)KVA_TO_PA(&sa) ;
|
||||
bd_p->SRCADDR = (unsigned int)KVA_TO_PA(in) ;
|
||||
bd_p->DSTADDR = (unsigned int)KVA_TO_PA(out);
|
||||
bd_p->MSGLEN = sz ;
|
||||
|
||||
CECON = 1 << 6;
|
||||
while (CECON);
|
||||
|
||||
/* Run the engine */
|
||||
CEBDPADDR = (unsigned int)KVA_TO_PA(&bd) ;
|
||||
CEINTEN = 0x07;
|
||||
CECON = 0x27;
|
||||
|
||||
WAIT_ENGINE ;
|
||||
|
||||
if((cryptoalgo == PIC32_CRYPTOALGO_CBC) ||
|
||||
(cryptoalgo == PIC32_CRYPTOALGO_TCBC)||
|
||||
(cryptoalgo == PIC32_CRYPTOALGO_RCBC)) {
|
||||
/* set iv for the next call */
|
||||
if(dir == PIC32_ENCRYPTION) {
|
||||
XMEMCPY((void *)aes->iv_ce,
|
||||
(void*)KVA0_TO_KVA1(out + sz - AES_BLOCK_SIZE),
|
||||
AES_BLOCK_SIZE) ;
|
||||
} 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);
|
||||
}
|
||||
|
||||
int AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
AesCrypt(aes, out, in, sz, PIC32_ENCRYPTION, PIC32_ALGO_AES,
|
||||
PIC32_CRYPTOALGO_RCBC );
|
||||
}
|
||||
|
||||
int AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
AesCrypt(aes, out, in, sz, PIC32_DECRYPTION, PIC32_ALGO_AES,
|
||||
PIC32_CRYPTOALGO_RCBC);
|
||||
}
|
||||
|
||||
#if defined(CYASSL_AES_COUNTER)
|
||||
void AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
int i ;
|
||||
char out_block[AES_BLOCK_SIZE] ;
|
||||
int odd ;
|
||||
int even ;
|
||||
char *tmp ; /* (char *)aes->tmp, for short */
|
||||
|
||||
tmp = (char *)aes->tmp ;
|
||||
if(aes->left) {
|
||||
if((aes->left + sz) >= AES_BLOCK_SIZE){
|
||||
odd = AES_BLOCK_SIZE - aes->left ;
|
||||
} else {
|
||||
odd = sz ;
|
||||
}
|
||||
XMEMCPY(tmp+aes->left, in, odd) ;
|
||||
if((odd+aes->left) == AES_BLOCK_SIZE){
|
||||
AesCrypt(aes, out_block, tmp, AES_BLOCK_SIZE,
|
||||
PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCTR);
|
||||
XMEMCPY(out, out_block+aes->left, odd) ;
|
||||
aes->left = 0 ;
|
||||
XMEMSET(tmp, 0x0, AES_BLOCK_SIZE) ;
|
||||
/* Increment IV */
|
||||
for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
|
||||
if (++((byte *)aes->iv_ce)[i])
|
||||
break ;
|
||||
}
|
||||
}
|
||||
in += odd ;
|
||||
out+= odd ;
|
||||
sz -= odd ;
|
||||
}
|
||||
odd = sz % AES_BLOCK_SIZE ; /* if there is tail flagment */
|
||||
if(sz / AES_BLOCK_SIZE) {
|
||||
even = (sz/AES_BLOCK_SIZE)*AES_BLOCK_SIZE ;
|
||||
AesCrypt(aes, out, in, even, PIC32_ENCRYPTION, PIC32_ALGO_AES,
|
||||
PIC32_CRYPTOALGO_RCTR);
|
||||
out += even ;
|
||||
in += even ;
|
||||
do { /* Increment IV */
|
||||
for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
|
||||
if (++((byte *)aes->iv_ce)[i])
|
||||
break ;
|
||||
}
|
||||
even -= AES_BLOCK_SIZE ;
|
||||
} while((int)even > 0) ;
|
||||
}
|
||||
if(odd) {
|
||||
XMEMSET(tmp+aes->left, 0x0, AES_BLOCK_SIZE - aes->left) ;
|
||||
XMEMCPY(tmp+aes->left, in, odd) ;
|
||||
AesCrypt(aes, out_block, tmp, AES_BLOCK_SIZE,
|
||||
PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCTR);
|
||||
XMEMCPY(out, out_block+aes->left,odd) ;
|
||||
aes->left += odd ;
|
||||
}
|
||||
}
|
||||
#endif /* CYASSL_AES_COUNTER */
|
||||
|
||||
#ifdef HAVE_AESGCM
|
||||
#define HAVE_AES_ENGINE
|
||||
/* Hardware AESGCM borows most of the software AESGCM, GMAC */
|
||||
#endif
|
||||
|
||||
#endif /* CYASSL_PIC32MZ_CRYPT */
|
||||
|
||||
#ifdef STM32F2_CRYPTO
|
||||
/*
|
||||
* STM32F2 hardware AES support through the STM32F2 standard peripheral
|
||||
@@ -439,13 +617,11 @@ 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)) ;
|
||||
}
|
||||
|
||||
@@ -1322,7 +1498,6 @@ static const word32 Td[5][256] = {
|
||||
};
|
||||
|
||||
|
||||
|
||||
#define GETBYTE(x, y) (word32)((byte)((x) >> (8 * (y))))
|
||||
|
||||
|
||||
@@ -1487,6 +1662,19 @@ static int AesSetKeyLocal(Aes* aes, const byte* userKey, word32 keylen,
|
||||
ByteReverseWords(rk, rk, keylen);
|
||||
#endif
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_CRYPT
|
||||
{
|
||||
word32 *akey1 = aes->key_ce;
|
||||
word32 *areg = aes->iv_ce ;
|
||||
aes->keylen = keylen ;
|
||||
XMEMCPY(akey1, userKey, keylen);
|
||||
if (iv)
|
||||
XMEMCPY(areg, iv, AES_BLOCK_SIZE);
|
||||
else
|
||||
XMEMSET(areg, 0, AES_BLOCK_SIZE);
|
||||
}
|
||||
#endif
|
||||
|
||||
switch(keylen)
|
||||
{
|
||||
case 16:
|
||||
@@ -1976,7 +2164,7 @@ static void AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
|
||||
XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
|
||||
}
|
||||
|
||||
|
||||
#ifndef HAVE_AES_ENGINE
|
||||
int AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
word32 blocks = sz / AES_BLOCK_SIZE;
|
||||
@@ -2083,7 +2271,7 @@ int AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef CYASSL_AES_DIRECT
|
||||
|
||||
@@ -2116,7 +2304,7 @@ int AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
|
||||
#endif /* CYASSL_AES_DIRECT || CYASSL_AES_COUNTER */
|
||||
|
||||
|
||||
#ifdef CYASSL_AES_COUNTER
|
||||
#if defined(CYASSL_AES_COUNTER) && !defined(HAVE_AES_ENGINE)
|
||||
|
||||
/* Increment AES counter */
|
||||
static INLINE void IncrementAesCounter(byte* inOutCtr)
|
||||
@@ -2806,34 +2994,51 @@ void AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
|
||||
word32 partial = sz % AES_BLOCK_SIZE;
|
||||
const byte* p = in;
|
||||
byte* c = out;
|
||||
byte ctr[AES_BLOCK_SIZE];
|
||||
byte counter[AES_BLOCK_SIZE];
|
||||
byte *ctr ;
|
||||
byte scratch[AES_BLOCK_SIZE];
|
||||
|
||||
CYASSL_ENTER("AesGcmEncrypt");
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_CRYPT
|
||||
ctr = (char *)aes->iv_ce ;
|
||||
#else
|
||||
ctr = counter ;
|
||||
#endif
|
||||
|
||||
XMEMSET(ctr, 0, AES_BLOCK_SIZE);
|
||||
XMEMCPY(ctr, iv, ivSz);
|
||||
InitGcmCounter(ctr);
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_CRYPT
|
||||
if(blocks)
|
||||
AesCrypt(aes, out, in, blocks*AES_BLOCK_SIZE,
|
||||
PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM );
|
||||
#endif
|
||||
while (blocks--) {
|
||||
IncrementGcmCounter(ctr);
|
||||
#ifndef CYASSL_PIC32MZ_CRYPT
|
||||
AesEncrypt(aes, ctr, scratch);
|
||||
xorbuf(scratch, p, AES_BLOCK_SIZE);
|
||||
XMEMCPY(c, scratch, AES_BLOCK_SIZE);
|
||||
|
||||
#endif
|
||||
p += AES_BLOCK_SIZE;
|
||||
c += AES_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
if (partial != 0) {
|
||||
IncrementGcmCounter(ctr);
|
||||
AesEncrypt(aes, ctr, scratch);
|
||||
xorbuf(scratch, p, partial);
|
||||
XMEMCPY(c, scratch, partial);
|
||||
|
||||
}
|
||||
|
||||
GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz);
|
||||
InitGcmCounter(ctr);
|
||||
AesEncrypt(aes, ctr, scratch);
|
||||
xorbuf(authTag, scratch, authTagSz);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2846,11 +3051,18 @@ int AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
|
||||
word32 partial = sz % AES_BLOCK_SIZE;
|
||||
const byte* c = in;
|
||||
byte* p = out;
|
||||
byte ctr[AES_BLOCK_SIZE];
|
||||
byte counter[AES_BLOCK_SIZE];
|
||||
byte *ctr ;
|
||||
byte scratch[AES_BLOCK_SIZE];
|
||||
|
||||
CYASSL_ENTER("AesGcmDecrypt");
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_CRYPT
|
||||
ctr = (char *)aes->iv_ce ;
|
||||
#else
|
||||
ctr = counter ;
|
||||
#endif
|
||||
|
||||
XMEMSET(ctr, 0, AES_BLOCK_SIZE);
|
||||
XMEMCPY(ctr, iv, ivSz);
|
||||
InitGcmCounter(ctr);
|
||||
@@ -2864,17 +3076,25 @@ int AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
|
||||
GHASH(aes, authIn, authInSz, in, sz, Tprime, sizeof(Tprime));
|
||||
AesEncrypt(aes, ctr, EKY0);
|
||||
xorbuf(Tprime, EKY0, sizeof(Tprime));
|
||||
|
||||
if (XMEMCMP(authTag, Tprime, authTagSz) != 0) {
|
||||
return AES_GCM_AUTH_E;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_CRYPT
|
||||
if(blocks)
|
||||
AesCrypt(aes, out, in, blocks*AES_BLOCK_SIZE,
|
||||
PIC32_DECRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM );
|
||||
#endif
|
||||
|
||||
while (blocks--) {
|
||||
IncrementGcmCounter(ctr);
|
||||
#ifndef CYASSL_PIC32MZ_CRYPT
|
||||
AesEncrypt(aes, ctr, scratch);
|
||||
xorbuf(scratch, c, AES_BLOCK_SIZE);
|
||||
XMEMCPY(p, scratch, AES_BLOCK_SIZE);
|
||||
|
||||
#endif
|
||||
p += AES_BLOCK_SIZE;
|
||||
c += AES_BLOCK_SIZE;
|
||||
}
|
||||
@@ -2884,11 +3104,11 @@ int AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
|
||||
xorbuf(scratch, c, partial);
|
||||
XMEMCPY(p, scratch, partial);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
CYASSL_API void GmacSetKey(Gmac* gmac, const byte* key, word32 len)
|
||||
{
|
||||
AesGcmSetKey(&gmac->aes, key, len);
|
||||
|
||||
@@ -594,6 +594,138 @@ void Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#elif defined(CYASSL_PIC32MZ_CRYPT)
|
||||
|
||||
#include "../../cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h"
|
||||
|
||||
void Des_SetIV(Des* des, const byte* iv);
|
||||
void Des3_SetIV(Des3* des, const byte* iv);
|
||||
|
||||
void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
|
||||
{
|
||||
word32 *dkey = des->key ;
|
||||
word32 *dreg = des->reg ;
|
||||
|
||||
XMEMCPY((byte *)dkey, (byte *)key, 8);
|
||||
ByteReverseWords(dkey, dkey, 8);
|
||||
XMEMCPY((byte *)dreg, (byte *)iv, 8);
|
||||
ByteReverseWords(dreg, dreg, 8);
|
||||
}
|
||||
|
||||
void Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
|
||||
{
|
||||
word32 *dkey1 = des->key[0];
|
||||
word32 *dreg = des->reg ;
|
||||
|
||||
XMEMCPY(dkey1, key, 24);
|
||||
ByteReverseWords(dkey1, dkey1, 24);
|
||||
XMEMCPY(dreg, iv, 8);
|
||||
ByteReverseWords(dreg, dreg, 8) ;
|
||||
|
||||
}
|
||||
|
||||
void DesCrypt(word32 *key, word32 *iv, byte* out, const byte* in, word32 sz,
|
||||
int dir, int algo, int cryptoalgo)
|
||||
{
|
||||
securityAssociation *sa_p ;
|
||||
bufferDescriptor *bd_p ;
|
||||
const byte *in_p, *in_l ;
|
||||
byte *out_p, *out_l ;
|
||||
volatile securityAssociation sa __attribute__((aligned (8)));
|
||||
volatile bufferDescriptor bd __attribute__((aligned (8)));
|
||||
volatile int k ;
|
||||
|
||||
/* get uncached address */
|
||||
|
||||
in_l = in;
|
||||
out_l = out ;
|
||||
sa_p = KVA0_TO_KVA1(&sa) ;
|
||||
bd_p = KVA0_TO_KVA1(&bd) ;
|
||||
in_p = KVA0_TO_KVA1(in_l) ;
|
||||
out_p= KVA0_TO_KVA1(out_l);
|
||||
|
||||
if(PIC32MZ_IF_RAM(in_p))
|
||||
XMEMCPY((void *)in_p, (void *)in, sz);
|
||||
XMEMSET((void *)out_p, 0, sz);
|
||||
|
||||
/* Set up the Security Association */
|
||||
XMEMSET((byte *)KVA0_TO_KVA1(&sa), 0, sizeof(sa));
|
||||
sa_p->SA_CTRL.ALGO = algo ;
|
||||
sa_p->SA_CTRL.LNC = 1;
|
||||
sa_p->SA_CTRL.LOADIV = 1;
|
||||
sa_p->SA_CTRL.FB = 1;
|
||||
sa_p->SA_CTRL.ENCTYPE = dir ; /* Encryption/Decryption */
|
||||
sa_p->SA_CTRL.CRYPTOALGO = cryptoalgo;
|
||||
sa_p->SA_CTRL.KEYSIZE = 1 ; /* KEY is 192 bits */
|
||||
XMEMCPY((byte *)KVA0_TO_KVA1(&sa.SA_ENCKEY[algo==PIC32_ALGO_TDES ? 2 : 6]),
|
||||
(byte *)key, algo==PIC32_ALGO_TDES ? 24 : 8);
|
||||
XMEMCPY((byte *)KVA0_TO_KVA1(&sa.SA_ENCIV[2]), (byte *)iv, 8);
|
||||
|
||||
XMEMSET((byte *)KVA0_TO_KVA1(&bd), 0, sizeof(bd));
|
||||
/* Set up the Buffer Descriptor */
|
||||
bd_p->BD_CTRL.BUFLEN = sz;
|
||||
bd_p->BD_CTRL.LIFM = 1;
|
||||
bd_p->BD_CTRL.SA_FETCH_EN = 1;
|
||||
bd_p->BD_CTRL.LAST_BD = 1;
|
||||
bd_p->BD_CTRL.DESC_EN = 1;
|
||||
|
||||
bd_p->SA_ADDR = (unsigned int)KVA_TO_PA(&sa) ; // (unsigned int)sa_p ;
|
||||
bd_p->SRCADDR = (unsigned int)KVA_TO_PA(in) ; // (unsigned int)in_p ;
|
||||
bd_p->DSTADDR = (unsigned int)KVA_TO_PA(out); // (unsigned int)out_p ;
|
||||
bd_p->NXTPTR = (unsigned int)KVA_TO_PA(&bd);
|
||||
bd_p->MSGLEN = sz ;
|
||||
|
||||
/* Fire in the hole! */
|
||||
CECON = 1 << 6;
|
||||
while (CECON);
|
||||
|
||||
/* Run the engine */
|
||||
CEBDPADDR = (unsigned int)KVA_TO_PA(&bd) ; // (unsigned int)bd_p ;
|
||||
CEINTEN = 0x07;
|
||||
CECON = 0x27;
|
||||
|
||||
WAIT_ENGINE ;
|
||||
|
||||
if((cryptoalgo == PIC32_CRYPTOALGO_CBC) ||
|
||||
(cryptoalgo == PIC32_CRYPTOALGO_TCBC)||
|
||||
(cryptoalgo == PIC32_CRYPTOALGO_RCBC)) {
|
||||
/* set iv for the next call */
|
||||
if(dir == PIC32_ENCRYPTION) {
|
||||
XMEMCPY((void *)iv, (void*)&(out_p[sz-DES_IVLEN]), DES_IVLEN) ;
|
||||
} else {
|
||||
ByteReverseWords((word32*)iv, (word32 *)&(in_p[sz-DES_IVLEN]), DES_IVLEN);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ByteReverseWords((word32*)out, (word32 *)KVA0_TO_KVA1(out), sz);
|
||||
}
|
||||
|
||||
void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
DesCrypt(des->key, des->reg, out, in, sz,
|
||||
PIC32_ENCRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC );
|
||||
}
|
||||
|
||||
void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
DesCrypt(des->key, des->reg, out, in, sz,
|
||||
PIC32_DECRYPTION, PIC32_ALGO_DES, PIC32_CRYPTOALGO_CBC);
|
||||
}
|
||||
|
||||
void Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
DesCrypt(des->key[0], des->reg, out, in, sz,
|
||||
PIC32_ENCRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC);
|
||||
}
|
||||
|
||||
void Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
|
||||
{
|
||||
DesCrypt(des->key[0], des->reg, out, in, sz,
|
||||
PIC32_DECRYPTION, PIC32_ALGO_TDES, PIC32_CRYPTOALGO_TCBC);
|
||||
}
|
||||
|
||||
#else /* CTaoCrypt software implementation */
|
||||
|
||||
/* permuted choice table (key) */
|
||||
|
||||
@@ -27,6 +27,22 @@
|
||||
|
||||
#ifndef NO_HMAC
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_HASH
|
||||
|
||||
#define InitMd5 InitMd5_sw
|
||||
#define Md5Update Md5Update_sw
|
||||
#define Md5Final Md5Final_sw
|
||||
|
||||
#define InitSha InitSha_sw
|
||||
#define ShaUpdate ShaUpdate_sw
|
||||
#define ShaFinal ShaFinal_sw
|
||||
|
||||
#define InitSha256 InitSha256_sw
|
||||
#define Sha256Update Sha256Update_sw
|
||||
#define Sha256Final Sha256Final_sw
|
||||
|
||||
#endif
|
||||
|
||||
#include <cyassl/ctaocrypt/hmac.h>
|
||||
#include <cyassl/ctaocrypt/error.h>
|
||||
|
||||
|
||||
@@ -26,7 +26,13 @@
|
||||
|
||||
#include <cyassl/ctaocrypt/settings.h>
|
||||
|
||||
#ifndef NO_MD5
|
||||
#if !defined(NO_MD5)
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_HASH
|
||||
#define InitMd5 InitMd5_sw
|
||||
#define Md5Update Md5Update_sw
|
||||
#define Md5Final Md5Final_sw
|
||||
#endif
|
||||
|
||||
#include <cyassl/ctaocrypt/md5.h>
|
||||
|
||||
|
||||
243
ctaocrypt/src/port/pic32/pic32mz-hash.c
Normal file
243
ctaocrypt/src/port/pic32/pic32mz-hash.c
Normal file
@@ -0,0 +1,243 @@
|
||||
/* pic32mz-hash.c
|
||||
*
|
||||
* Copyright (C) 2006-2013 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of CyaSSL.
|
||||
*
|
||||
* CyaSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* CyaSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <cyassl/ctaocrypt/settings.h>
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_HASH
|
||||
|
||||
#include <cyassl/ctaocrypt/logging.h>
|
||||
#include <cyassl/ctaocrypt/md5.h>
|
||||
#include <cyassl/ctaocrypt/sha.h>
|
||||
#include <cyassl/ctaocrypt/sha256.h>
|
||||
|
||||
#include <cyassl/ctaocrypt/port/pic32/pic32mz-crypt.h>
|
||||
|
||||
#if !defined(NO_MD5) && !defined(NO_SHA) && !defined(NO_SHA256)
|
||||
|
||||
static void reset_engine(pic32mz_desc *desc_l, int algo)
|
||||
{
|
||||
pic32mz_desc *desc ;
|
||||
desc = KVA0_TO_KVA1(desc_l) ;
|
||||
|
||||
CECON = 1 << 6;
|
||||
while (CECON);
|
||||
|
||||
/* Make sure everything is clear first before we make settings. */
|
||||
XMEMSET((void *)KVA0_TO_KVA1(&desc->sa), 0, sizeof(desc->sa));
|
||||
XMEMSET((void *)KVA0_TO_KVA1(&desc->bd[0]), 0, sizeof(desc->bd[0]));
|
||||
XMEMSET((void *)KVA0_TO_KVA1(&desc->bd[1]), 0, sizeof(desc->bd[1]));
|
||||
|
||||
/* Set up the security association */
|
||||
desc->sa.SA_CTRL.ALGO = algo ;
|
||||
desc->sa.SA_CTRL.LNC = 1;
|
||||
desc->sa.SA_CTRL.FB = 1;
|
||||
desc->sa.SA_CTRL.ENCTYPE = 1;
|
||||
desc->sa.SA_CTRL.LOADIV = 1;
|
||||
|
||||
/* Set up the buffer descriptor */
|
||||
desc->err = 0 ;
|
||||
desc->bd[0].BD_CTRL.LAST_BD = 1;
|
||||
desc->bd[0].BD_CTRL.LIFM = 1;
|
||||
desc->bd[0].SA_ADDR = KVA_TO_PA(&desc->sa);
|
||||
desc->bd[1].BD_CTRL.LAST_BD = 1;
|
||||
desc->bd[1].BD_CTRL.LIFM = 1;
|
||||
desc->bd[1].SA_ADDR = KVA_TO_PA(&desc->sa);
|
||||
desc_l->bdCount = 0 ;
|
||||
CEBDPADDR = KVA_TO_PA(&(desc->bd[0]));
|
||||
|
||||
CECON = 0x27;
|
||||
}
|
||||
|
||||
#define PIC32MZ_IF_RAM(addr) (KVA_TO_PA(addr) < 0x80000)
|
||||
|
||||
static void update_engine(pic32mz_desc *desc_l, const char *input, word32 len,
|
||||
word32 *hash)
|
||||
{
|
||||
pic32mz_desc *desc ;
|
||||
int i ;
|
||||
int total ;
|
||||
desc = KVA0_TO_KVA1(desc_l) ;
|
||||
|
||||
i = desc_l->bdCount ;
|
||||
if(i >= PIC32MZ_MAX_BD) {
|
||||
desc_l->err = 1 ;
|
||||
return ;
|
||||
}
|
||||
|
||||
if(PIC32MZ_IF_RAM(input))
|
||||
XMEMCPY(KVA0_TO_KVA1(input), input, len) ; /* Sync phys with cache */
|
||||
desc->bd[i].SRCADDR = KVA_TO_PA(input);
|
||||
/* Finally, turn on the buffer descriptor */
|
||||
if (len % 4)
|
||||
desc->bd[i].BD_CTRL.BUFLEN = (len + 4) - (len % 4);
|
||||
else desc->bd[i].BD_CTRL.BUFLEN = len ;
|
||||
|
||||
if(i == 0) {
|
||||
desc->bd[i].MSGLEN = len ;
|
||||
desc->bd[i].BD_CTRL.SA_FETCH_EN = 1;
|
||||
} else {
|
||||
desc->bd[i-1].NXTPTR = KVA_TO_PA(&(desc->bd[i])) ;
|
||||
desc->bd[i].BD_CTRL.DESC_EN = 1;
|
||||
desc->bd[i-1].BD_CTRL.LAST_BD = 0 ;
|
||||
desc->bd[i-1].BD_CTRL.LIFM = 0 ;
|
||||
total = desc->bd[i-1].MSGLEN + len ;
|
||||
desc->bd[i].MSGLEN = total ;
|
||||
desc->bd[i-1].MSGLEN = total ;
|
||||
}
|
||||
desc->bd[i].UPDPTR = KVA_TO_PA(hash);
|
||||
desc_l->bdCount ++ ;
|
||||
|
||||
#ifdef DEBUG_CYASSL
|
||||
printf("Input[bd=%d, len=%d]:%x->\"%s\"\n", desc_l->bdCount, len, input, input) ;
|
||||
print_mem(input, len+4) ;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void start_engine(pic32mz_desc *desc) {
|
||||
bufferDescriptor *hash_bd[2] ;
|
||||
hash_bd[0] = (bufferDescriptor *)KVA0_TO_KVA1(&(desc->bd[0])) ;
|
||||
hash_bd[0]->BD_CTRL.DESC_EN = 1;
|
||||
}
|
||||
|
||||
void wait_engine(pic32mz_desc *desc, char *hash, int hash_sz) {
|
||||
unsigned int i;
|
||||
unsigned int *intptr;
|
||||
#undef DEBUG_CYASSL
|
||||
#ifdef DEBUG_CYASSL
|
||||
printf("desc(%x)[bd:%d * 2, sz:%d]\n", desc, sizeof(desc->bd[0]),
|
||||
sizeof(desc->sa) );
|
||||
print_mem(KVA0_TO_KVA1(&(desc->bd[0])), sizeof(desc->bd[0])) ;
|
||||
print_mem(KVA0_TO_KVA1(&(desc->bd[1])), sizeof(desc->bd[0])) ;
|
||||
#endif
|
||||
|
||||
WAIT_ENGINE ;
|
||||
|
||||
XMEMCPY(hash, KVA0_TO_KVA1(hash), hash_sz) ;
|
||||
|
||||
#ifdef DEBUG_CYASSL
|
||||
print_mem(KVA0_TO_KVA1(hash), hash_sz) ;
|
||||
print_mem( hash , hash_sz) ;
|
||||
#endif
|
||||
for (i = 0, intptr = (unsigned int *)hash; i < hash_sz/sizeof(unsigned int);
|
||||
i++, intptr++)
|
||||
{
|
||||
*intptr = ntohl(*intptr);
|
||||
}
|
||||
}
|
||||
|
||||
static int fillBuff(char *buff, int *bufflen, const char *data, int len, int blocksz)
|
||||
{
|
||||
int room, copysz ;
|
||||
|
||||
room = blocksz - *bufflen ;
|
||||
copysz = (len <= room) ? len : room ;
|
||||
XMEMCPY(buff, data, copysz) ;
|
||||
*bufflen += copysz ;
|
||||
return (*bufflen == blocksz) ? 1 : 0 ;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef NO_MD5
|
||||
void InitMd5(Md5* md5)
|
||||
{
|
||||
CYASSL_ENTER("InitMd5\n") ;
|
||||
XMEMSET((void *)md5, 0xcc, sizeof(Md5)) ;
|
||||
XMEMSET((void *)KVA0_TO_KVA1(md5), 0xcc, sizeof(Md5)) ;
|
||||
reset_engine(&(md5->desc), PIC32_ALGO_MD5) ;
|
||||
|
||||
}
|
||||
|
||||
void Md5Update(Md5* md5, const byte* data, word32 len)
|
||||
{
|
||||
CYASSL_ENTER("Md5Update\n") ;
|
||||
update_engine(&(md5->desc), data, len, md5->digest) ;
|
||||
}
|
||||
|
||||
void Md5Final(Md5* md5, byte* hash)
|
||||
{
|
||||
CYASSL_ENTER("Md5Final\n") ;
|
||||
start_engine(&(md5->desc)) ;
|
||||
wait_engine(&(md5->desc), (char *)md5->digest, MD5_HASH_SIZE) ;
|
||||
XMEMCPY(hash, md5->digest, MD5_HASH_SIZE) ;
|
||||
InitMd5(md5); /* reset state */
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef NO_SHA
|
||||
void 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) ;
|
||||
}
|
||||
|
||||
void ShaUpdate(Sha* sha, const byte* data, word32 len)
|
||||
{
|
||||
CYASSL_ENTER("ShaUpdate\n") ;
|
||||
update_engine(&(sha->desc), data, len, sha->digest) ;
|
||||
}
|
||||
|
||||
void ShaFinal(Sha* sha, byte* hash)
|
||||
{
|
||||
CYASSL_ENTER("ShaFinal\n") ;
|
||||
start_engine(&(sha->desc)) ;
|
||||
wait_engine(&(sha->desc), (char *)sha->digest, SHA1_HASH_SIZE) ;
|
||||
XMEMCPY(hash, sha->digest, SHA1_HASH_SIZE) ;
|
||||
|
||||
InitSha(sha); /* reset state */
|
||||
}
|
||||
#endif /* NO_SHA */
|
||||
|
||||
#ifndef NO_SHA256
|
||||
void 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) ;
|
||||
}
|
||||
|
||||
void Sha256Update(Sha256* sha256, const byte* data, word32 len)
|
||||
{
|
||||
CYASSL_ENTER("Sha256Update\n") ;
|
||||
update_engine(&(sha256->desc), data, len, sha256->digest) ;
|
||||
}
|
||||
|
||||
void Sha256Final(Sha256* sha256, byte* hash)
|
||||
{
|
||||
CYASSL_ENTER("Sha256Final\n") ;
|
||||
start_engine(&(sha256->desc)) ;
|
||||
wait_engine(&(sha256->desc), (char *)sha256->digest, SHA256_HASH_SIZE) ;
|
||||
XMEMCPY(hash, sha256->digest, SHA256_HASH_SIZE) ;
|
||||
InitSha256(sha256); /* reset state */
|
||||
}
|
||||
#endif /* NO_SHA256 */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +27,22 @@
|
||||
|
||||
#ifndef NO_PWDBASED
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_HASH
|
||||
|
||||
#define InitMd5 InitMd5_sw
|
||||
#define Md5Update Md5Update_sw
|
||||
#define Md5Final Md5Final_sw
|
||||
|
||||
#define InitSha InitSha_sw
|
||||
#define ShaUpdate ShaUpdate_sw
|
||||
#define ShaFinal ShaFinal_sw
|
||||
|
||||
#define InitSha256 InitSha256_sw
|
||||
#define Sha256Update Sha256Update_sw
|
||||
#define Sha256Final Sha256Final_sw
|
||||
|
||||
#endif
|
||||
|
||||
#include <cyassl/ctaocrypt/pwdbased.h>
|
||||
#include <cyassl/ctaocrypt/hmac.h>
|
||||
#include <cyassl/ctaocrypt/integer.h>
|
||||
|
||||
@@ -51,7 +51,8 @@
|
||||
#include <windows.h>
|
||||
#include <wincrypt.h>
|
||||
#else
|
||||
#if !defined(NO_DEV_RANDOM) && !defined(CYASSL_MDK_ARM)
|
||||
#if !defined(NO_DEV_RANDOM) && !defined(CYASSL_MDK_ARM) \
|
||||
&& !defined(CYASSL_IAR_ARM)
|
||||
#include <fcntl.h>
|
||||
#ifndef EBSNET
|
||||
#include <unistd.h>
|
||||
@@ -468,23 +469,60 @@ int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
|
||||
#endif
|
||||
#define PIC32_SEED_COUNT ReadCoreTimer
|
||||
#endif
|
||||
#ifdef CYASSL_MIC32MZ_RNG
|
||||
#include "xc.h"
|
||||
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
|
||||
{
|
||||
int i ;
|
||||
byte rnd[8] ;
|
||||
word32 *rnd32 = (word32 *)rnd ;
|
||||
word32 size = sz ;
|
||||
byte* op = output ;
|
||||
|
||||
/* uses the core timer, in nanoseconds to seed srand */
|
||||
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
|
||||
{
|
||||
int i;
|
||||
srand(PIC32_SEED_COUNT() * 25);
|
||||
/* This part has to be replaced with better random seed */
|
||||
RNGNUMGEN1 = ReadCoreTimer();
|
||||
RNGPOLY1 = ReadCoreTimer();
|
||||
RNGPOLY2 = ReadCoreTimer();
|
||||
RNGNUMGEN2 = ReadCoreTimer();
|
||||
#ifdef DEBUG_CYASSL
|
||||
printf("GenerateSeed::Seed=%08x, %08x\n", RNGNUMGEN1, RNGNUMGEN2) ;
|
||||
#endif
|
||||
RNGCONbits.PLEN = 0x40;
|
||||
RNGCONbits.PRNGEN = 1;
|
||||
for(i=0; i<5; i++) { /* wait for RNGNUMGEN ready */
|
||||
volatile int x ;
|
||||
x = RNGNUMGEN1 ;
|
||||
x = RNGNUMGEN2 ;
|
||||
}
|
||||
do {
|
||||
rnd32[0] = RNGNUMGEN1;
|
||||
rnd32[1] = RNGNUMGEN2;
|
||||
|
||||
for (i = 0; i < sz; i++ ) {
|
||||
output[i] = rand() % 256;
|
||||
if ( (i % 8) == 7)
|
||||
for(i=0; i<8; i++, op++) {
|
||||
*op = rnd[i] ;
|
||||
size -- ;
|
||||
if(size==0)break ;
|
||||
}
|
||||
} while(size) ;
|
||||
return 0;
|
||||
}
|
||||
#else /* CYASSL_MIC32MZ_RNG */
|
||||
/* uses the core timer, in nanoseconds to seed srand */
|
||||
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
|
||||
{
|
||||
int i;
|
||||
srand(PIC32_SEED_COUNT() * 25);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK)
|
||||
for (i = 0; i < sz; i++ ) {
|
||||
output[i] = rand() % 256;
|
||||
if ( (i % 8) == 7)
|
||||
srand(PIC32_SEED_COUNT() * 25);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif /* CYASSL_MIC32MZ_RNG */
|
||||
#elif defined(CYASSL_SAFERTOS) || defined(CYASSL_LEANPSK) \
|
||||
|| defined(CYASSL_IAR_ARM)
|
||||
|
||||
#warning "write a real random seed!!!!, just for testing now"
|
||||
|
||||
|
||||
@@ -26,7 +26,13 @@
|
||||
|
||||
#include <cyassl/ctaocrypt/settings.h>
|
||||
|
||||
#ifndef NO_SHA
|
||||
#if !defined(NO_SHA)
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_HASH
|
||||
#define InitSha InitSha_sw
|
||||
#define ShaUpdate ShaUpdate_sw
|
||||
#define ShaFinal ShaFinal_sw
|
||||
#endif
|
||||
|
||||
#include <cyassl/ctaocrypt/sha.h>
|
||||
#ifdef NO_INLINE
|
||||
@@ -50,8 +56,8 @@
|
||||
* document (See note in README).
|
||||
*/
|
||||
#include "stm32f2xx.h"
|
||||
#include "stm32f2xx_hash.h"
|
||||
|
||||
#include "stm32f2xx_hash.h"
|
||||
|
||||
void InitSha(Sha* sha)
|
||||
{
|
||||
/* STM32F2 struct notes:
|
||||
|
||||
@@ -28,7 +28,13 @@
|
||||
|
||||
#include <cyassl/ctaocrypt/settings.h>
|
||||
|
||||
#ifndef NO_SHA256
|
||||
#if !defined(NO_SHA256)
|
||||
|
||||
#ifdef CYASSL_PIC32MZ_HASH
|
||||
#define InitSha256 InitSha256_sw
|
||||
#define Sha256Update Sha256Update_sw
|
||||
#define Sha256Final Sha256Final_sw
|
||||
#endif
|
||||
|
||||
#include <cyassl/ctaocrypt/sha256.h>
|
||||
#ifdef NO_INLINE
|
||||
|
||||
Reference in New Issue
Block a user