des3 parity. internal.h to ctaocrypt/types.h

This commit is contained in:
Takashi Kojo 2014-04-25 13:35:35 +09:00
parent a67cb1207b
commit e3becc3a65
3 changed files with 31 additions and 48 deletions

View File

@ -605,7 +605,7 @@
#elif defined(HAVE_COLDFIRE_SEC)
#include <cyassl/internal.h>
#include <cyassl/ctaocrypt/types.h>
#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;

View File

@ -275,7 +275,7 @@
#elif defined(HAVE_COLDFIRE_SEC)
#include <cyassl/internal.h>
#include <cyassl/ctaocrypt/types.h>
#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 {

View File

@ -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<len; i++) {
out[i] = in[i] ;
v = out[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;
}