initial cavium, crypto only, no rsa

This commit is contained in:
Todd Ouska
2013-01-29 16:22:49 -08:00
parent ef644d4de0
commit a361f5c4bf
15 changed files with 829 additions and 15 deletions

View File

@@ -41,6 +41,15 @@
#endif
#ifdef HAVE_CAVIUM
static int AesCaviumSetKey(Aes* aes, const byte* key, word32 length,
const byte* iv);
static void AesCaviumCbcEncrypt(Aes* aes, byte* out, const byte* in,
word32 length);
static void AesCaviumCbcDecrypt(Aes* aes, byte* out, const byte* in,
word32 length);
#endif
#ifdef STM32F2_CRYPTO
/*
* STM32F2 hardware AES support through the STM32F2 standard peripheral
@@ -1349,6 +1358,11 @@ int AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
return BAD_FUNC_ARG;
#ifdef HAVE_CAVIUM
if (aes->magic == CYASSL_AES_CAVIUM_MAGIC)
return AesCaviumSetKey(aes, userKey, keylen, iv);
#endif
#ifdef CYASSL_AESNI
if (checkAESNI == 0) {
haveAESNI = Check_CPU_support_AES();
@@ -1661,6 +1675,11 @@ void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
word32 blocks = sz / AES_BLOCK_SIZE;
#ifdef HAVE_CAVIUM
if (aes->magic == CYASSL_AES_CAVIUM_MAGIC)
return AesCaviumCbcEncrypt(aes, out, in, sz);
#endif
#ifdef CYASSL_AESNI
if (haveAESNI) {
#ifdef DEBUG_AESNI
@@ -1695,6 +1714,11 @@ void AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
word32 blocks = sz / AES_BLOCK_SIZE;
#ifdef HAVE_CAVIUM
if (aes->magic == CYASSL_AES_CAVIUM_MAGIC)
return AesCaviumCbcDecrypt(aes, out, in, sz);
#endif
#ifdef CYASSL_AESNI
if (haveAESNI) {
#ifdef DEBUG_AESNI
@@ -2738,5 +2762,108 @@ int AesSetIV(Aes* aes, const byte* iv)
}
#ifdef HAVE_CAVIUM
#include <cyassl/ctaocrypt/logging.h>
#include "cavium_common.h"
/* Initiliaze Aes for use with Nitrox device */
int AesInitCavium(Aes* aes, int devId)
{
if (aes == NULL)
return -1;
if (CspAllocContext(CONTEXT_SSL, &aes->contextHandle, devId) != 0)
return -1;
aes->devId = devId;
aes->magic = CYASSL_AES_CAVIUM_MAGIC;
return 0;
}
/* Free Aes from use with Nitrox device */
void AesFreeCavium(Aes* aes)
{
if (aes == NULL)
return;
CspFreeContext(CONTEXT_SSL, aes->contextHandle, aes->devId);
aes->magic = 0;
}
static int AesCaviumSetKey(Aes* aes, const byte* key, word32 length,
const byte* iv)
{
if (aes == NULL)
return -1;
XMEMCPY(aes->key, key, length); /* key still holds key, iv still in reg */
if (length == 16)
aes->type = AES_128;
else if (length == 24)
aes->type = AES_192;
else if (length == 32)
aes->type = AES_256;
return AesSetIV(aes, iv);
}
static void AesCaviumCbcEncrypt(Aes* aes, byte* out, const byte* in,
word32 length)
{
word offset = 0;
word32 requestId;
while (length > CYASSL_MAX_16BIT) {
word16 slen = (word16)CYASSL_MAX_16BIT;
if (CspEncryptAes(CAVIUM_BLOCKING, aes->contextHandle, CAVIUM_NO_UPDATE,
aes->type, slen, (byte*)in + offset, out + offset,
(byte*)aes->reg, (byte*)aes->key, &requestId,
aes->devId) != 0) {
CYASSL_MSG("Bad Cavium Aes Encrypt");
}
length -= CYASSL_MAX_16BIT;
}
if (length) {
word16 slen = (word16)length;
if (CspEncryptAes(CAVIUM_BLOCKING, aes->contextHandle, CAVIUM_NO_UPDATE,
aes->type, slen, (byte*)in + offset, out + offset,
(byte*)aes->reg, (byte*)aes->key, &requestId,
aes->devId) != 0) {
CYASSL_MSG("Bad Cavium Aes Encrypt");
}
}
}
static void AesCaviumCbcDecrypt(Aes* aes, byte* out, const byte* in,
word32 length)
{
word32 requestId;
while (length > CYASSL_MAX_16BIT) {
word16 slen = (word16)CYASSL_MAX_16BIT;
if (CspDecryptAes(CAVIUM_BLOCKING, aes->contextHandle, CAVIUM_NO_UPDATE,
aes->type, slen, (byte*)in, out, (byte*)aes->reg,
(byte*)aes->key, &requestId, aes->devId) != 0) {
CYASSL_MSG("Bad Cavium Aes Decrypt");
}
length -= CYASSL_MAX_16BIT;
}
if (length) {
word16 slen = (word16)length;
if (CspDecryptAes(CAVIUM_BLOCKING, aes->contextHandle, CAVIUM_NO_UPDATE,
aes->type, slen, (byte*)in, out, (byte*)aes->reg,
(byte*)aes->key, &requestId, aes->devId) != 0) {
CYASSL_MSG("Bad Cavium Aes Decrypt");
}
}
}
#endif /* HAVE_CAVIUM */
#endif /* NO_AES */

View File

@@ -26,11 +26,23 @@
#include <cyassl/ctaocrypt/arc4.h>
#ifdef HAVE_CAVIUM
static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length);
static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in,
word32 length);
#endif
void Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
{
word32 i;
word32 keyIndex = 0, stateIndex = 0;
#ifdef HAVE_CAVIUM
if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC)
return Arc4CaviumSetKey(arc4, key, length);
#endif
arc4->x = 1;
arc4->y = 0;
@@ -66,8 +78,16 @@ static INLINE byte MakeByte(word32* x, word32* y, byte* s)
void Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
{
word32 x = arc4->x;
word32 y = arc4->y;
word32 x;
word32 y;
#ifdef HAVE_CAVIUM
if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC)
return Arc4CaviumProcess(arc4, out, in, length);
#endif
x = arc4->x;
y = arc4->y;
while(length--)
*out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
@@ -76,3 +96,74 @@ void Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
arc4->y = (byte)y;
}
#ifdef HAVE_CAVIUM
#include <cyassl/ctaocrypt/logging.h>
#include "cavium_common.h"
/* Initiliaze Arc4 for use with Nitrox device */
int Arc4InitCavium(Arc4* arc4, int devId)
{
if (arc4 == NULL)
return -1;
if (CspAllocContext(CONTEXT_SSL, &arc4->contextHandle, devId) != 0)
return -1;
arc4->devId = devId;
arc4->magic = CYASSL_ARC4_CAVIUM_MAGIC;
return 0;
}
/* Free Arc4 from use with Nitrox device */
void Arc4FreeCavium(Arc4* arc4)
{
if (arc4 == NULL)
return;
CspFreeContext(CONTEXT_SSL, arc4->contextHandle, arc4->devId);
arc4->magic = 0;
}
static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length)
{
word32 requestId;
if (CspInitializeRc4(CAVIUM_BLOCKING, arc4->contextHandle, length,
(byte*)key, &requestId, arc4->devId) != 0) {
CYASSL_MSG("Bad Cavium Arc4 Init");
}
}
static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in,
word32 length)
{
word offset = 0;
word32 requestId;
while (length > CYASSL_MAX_16BIT) {
word16 slen = (word16)CYASSL_MAX_16BIT;
if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_NO_UPDATE,
slen, (byte*)in + offset, out + offset, &requestId,
arc4->devId) != 0) {
CYASSL_MSG("Bad Cavium Arc4 Encrypt");
}
length -= CYASSL_MAX_16BIT;
offset += CYASSL_MAX_16BIT;
}
if (length) {
word16 slen = (word16)length;
if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_NO_UPDATE,
slen, (byte*)in + offset, out + offset, &requestId,
arc4->devId) != 0) {
CYASSL_MSG("Bad Cavium Arc4 Encrypt");
}
}
}
#endif /* HAVE_CAVIUM */

View File

@@ -34,6 +34,14 @@
#endif
#ifdef HAVE_CAVIUM
static void Des3_CaviumSetKey(Des3* des3, const byte* key, const byte* iv);
static void Des3_CaviumCbcEncrypt(Des3* des3, byte* out, const byte* in,
word32 length);
static void Des3_CaviumCbcDecrypt(Des3* des3, byte* out, const byte* in,
word32 length);
#endif
#ifdef STM32F2_CRYPTO
/*
* STM32F2 hardware DES/3DES support through the STM32F2 standard
@@ -554,6 +562,11 @@ void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
void Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
{
#ifdef HAVE_CAVIUM
if (des->magic == CYASSL_3DES_CAVIUM_MAGIC)
return Des3_CaviumSetKey(des, key, iv);
#endif
DesSetKey(key + (dir == DES_ENCRYPTION ? 0 : 16), dir, des->key[0]);
DesSetKey(key + 8, Reverse(dir), des->key[1]);
DesSetKey(key + (dir == DES_DECRYPTION ? 0 : 16), dir, des->key[2]);
@@ -682,8 +695,14 @@ void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
void Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
{
word32 blocks = sz / DES_BLOCK_SIZE;
word32 blocks;
#ifdef HAVE_CAVIUM
if (des->magic == CYASSL_3DES_CAVIUM_MAGIC)
return Des3_CaviumCbcEncrypt(des, out, in, sz);
#endif
blocks = sz / DES_BLOCK_SIZE;
while (blocks--) {
xorbuf((byte*)des->reg, in, DES_BLOCK_SIZE);
Des3ProcessBlock(des, (byte*)des->reg, (byte*)des->reg);
@@ -697,8 +716,14 @@ void Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
void Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
{
word32 blocks = sz / DES_BLOCK_SIZE;
word32 blocks;
#ifdef HAVE_CAVIUM
if (des->magic == CYASSL_3DES_CAVIUM_MAGIC)
return Des3_CaviumCbcDecrypt(des, out, in, sz);
#endif
blocks = sz / DES_BLOCK_SIZE;
while (blocks--) {
XMEMCPY(des->tmp, in, DES_BLOCK_SIZE);
Des3ProcessBlock(des, (byte*)des->tmp, out);
@@ -743,4 +768,104 @@ void Des3_SetIV(Des3* des, const byte* iv)
}
#ifdef HAVE_CAVIUM
#include <cyassl/ctaocrypt/logging.h>
#include "cavium_common.h"
/* Initiliaze Des3 for use with Nitrox device */
int Des3_InitCavium(Des3* des3, int devId)
{
if (des3 == NULL)
return -1;
if (CspAllocContext(CONTEXT_SSL, &des3->contextHandle, devId) != 0)
return -1;
des3->devId = devId;
des3->magic = CYASSL_3DES_CAVIUM_MAGIC;
return 0;
}
/* Free Des3 from use with Nitrox device */
void Des3_FreeCavium(Des3* des3)
{
if (des3 == NULL)
return;
CspFreeContext(CONTEXT_SSL, des3->contextHandle, des3->devId);
des3->magic = 0;
}
static void Des3_CaviumSetKey(Des3* des3, const byte* key, const byte* iv)
{
if (des3 == NULL)
return;
/* key[0] holds key, iv in reg */
XMEMCPY(des3->key[0], key, DES_BLOCK_SIZE*3);
Des3_SetIV(des3, iv);
}
static void Des3_CaviumCbcEncrypt(Des3* des3, byte* out, const byte* in,
word32 length)
{
word offset = 0;
word32 requestId;
while (length > CYASSL_MAX_16BIT) {
word16 slen = (word16)CYASSL_MAX_16BIT;
if (CspEncrypt3Des(CAVIUM_BLOCKING, des3->contextHandle,
CAVIUM_NO_UPDATE, slen, (byte*)in + offset,
out + offset, (byte*)des3->reg, (byte*)des3->key[0],
&requestId, des3->devId) != 0) {
CYASSL_MSG("Bad Cavium 3DES Cbc Encrypt");
}
length -= CYASSL_MAX_16BIT;
}
if (length) {
word16 slen = (word16)length;
if (CspEncrypt3Des(CAVIUM_BLOCKING, des3->contextHandle,
CAVIUM_NO_UPDATE, slen, (byte*)in + offset,
out + offset, (byte*)des3->reg, (byte*)des3->key[0],
&requestId, des3->devId) != 0) {
CYASSL_MSG("Bad Cavium 3DES Cbc Encrypt");
}
}
}
static void Des3_CaviumCbcDecrypt(Des3* des3, byte* out, const byte* in,
word32 length)
{
word32 requestId;
while (length > CYASSL_MAX_16BIT) {
word16 slen = (word16)CYASSL_MAX_16BIT;
if (CspDecrypt3Des(CAVIUM_BLOCKING, des3->contextHandle,
CAVIUM_NO_UPDATE, slen, (byte*)in, out,
(byte*)des3->reg, (byte*)des3->key[0], &requestId,
des3->devId) != 0) {
CYASSL_MSG("Bad Cavium 3Des Decrypt");
}
length -= CYASSL_MAX_16BIT;
}
if (length) {
word16 slen = (word16)length;
if (CspDecrypt3Des(CAVIUM_BLOCKING, des3->contextHandle,
CAVIUM_NO_UPDATE, slen, (byte*)in, out,
(byte*)des3->reg, (byte*)des3->key[0], &requestId,
des3->devId) != 0) {
CYASSL_MSG("Bad Cavium 3Des Decrypt");
}
}
}
#endif /* HAVE_CAVIUM */
#endif /* NO_DES3 */

View File

@@ -29,6 +29,14 @@
#include <cyassl/ctaocrypt/error.h>
#ifdef HAVE_CAVIUM
static void HmacCaviumFinal(Hmac* hmac, byte* hash);
static void HmacCaviumUpdate(Hmac* hmac, const byte* msg, word32 length);
static void HmacCaviumSetKey(Hmac* hmac, int type, const byte* key,
word32 length);
#endif
static int InitHmac(Hmac* hmac, int type)
{
hmac->innerHashKeyed = 0;
@@ -74,6 +82,11 @@ void HmacSetKey(Hmac* hmac, int type, const byte* key, word32 length)
byte* op = (byte*) hmac->opad;
word32 i, hmac_block_size = SHA_BLOCK_SIZE;
#ifdef HAVE_CAVIUM
if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC)
return HmacCaviumSetKey(hmac, type, key, length);
#endif
InitHmac(hmac, type);
switch (hmac->macType) {
@@ -187,6 +200,11 @@ static void HmacKeyInnerHash(Hmac* hmac)
void HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
{
#ifdef HAVE_CAVIUM
if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC)
return HmacCaviumUpdate(hmac, msg, length);
#endif
if (!hmac->innerHashKeyed)
HmacKeyInnerHash(hmac);
@@ -222,6 +240,11 @@ void HmacUpdate(Hmac* hmac, const byte* msg, word32 length)
void HmacFinal(Hmac* hmac, byte* hash)
{
#ifdef HAVE_CAVIUM
if (hmac->magic == CYASSL_HMAC_CAVIUM_MAGIC)
return HmacCaviumFinal(hmac, hash);
#endif
if (!hmac->innerHashKeyed)
HmacKeyInnerHash(hmac);
@@ -290,5 +313,115 @@ void HmacFinal(Hmac* hmac, byte* hash)
}
#ifdef HAVE_CAVIUM
/* Initiliaze Hmac for use with Nitrox device */
int HmacInitCavium(Hmac* hmac, int devId)
{
if (hmac == NULL)
return -1;
if (CspAllocContext(CONTEXT_SSL, &hmac->contextHandle, devId) != 0)
return -1;
hmac->keyLen = 0;
hmac->dataLen = 0;
hmac->type = 0;
hmac->devId = devId;
hmac->magic = CYASSL_HMAC_CAVIUM_MAGIC;
hmac->data = NULL; /* buffered input data */
hmac->innerHashKeyed = 0;
return 0;
}
/* Free Hmac from use with Nitrox device */
void HmacFreeCavium(Hmac* hmac)
{
if (hmac == NULL)
return;
CspFreeContext(CONTEXT_SSL, hmac->contextHandle, hmac->devId);
hmac->magic = 0;
XFREE(hmac->data, NULL, DYNAMIC_TYPE_CAVIUM_TMP);
hmac->data = NULL;
}
static void HmacCaviumFinal(Hmac* hmac, byte* hash)
{
word32 requestId;
if (CspHmac(CAVIUM_BLOCKING, hmac->type, NULL, hmac->keyLen,
(byte*)hmac->ipad, hmac->dataLen, hmac->data, hash, &requestId,
hmac->devId) != 0) {
CYASSL_MSG("Cavium Hmac failed");
}
hmac->innerHashKeyed = 0; /* tell update to start over if used again */
}
static void HmacCaviumUpdate(Hmac* hmac, const byte* msg, word32 length)
{
word16 add = (word16)length;
word32 total;
byte* tmp;
if (length > CYASSL_MAX_16BIT) {
CYASSL_MSG("Too big msg for cavium hmac");
return;
}
if (hmac->innerHashKeyed == 0) { /* starting new */
hmac->dataLen = 0;
hmac->innerHashKeyed = 1;
}
total = add + hmac->dataLen;
if (total > CYASSL_MAX_16BIT) {
CYASSL_MSG("Too big msg for cavium hmac");
return;
}
tmp = XMALLOC(hmac->dataLen + add, NULL,DYNAMIC_TYPE_CAVIUM_TMP);
if (tmp == NULL) {
CYASSL_MSG("Out of memory for cavium update");
return;
}
if (hmac->dataLen)
XMEMCPY(tmp, hmac->data, hmac->dataLen);
XMEMCPY(tmp + hmac->dataLen, msg, add);
hmac->dataLen += add;
XFREE(hmac->data, NULL, DYNAMIC_TYPE_CAVIUM_TMP);
hmac->data = tmp;
}
static void HmacCaviumSetKey(Hmac* hmac, int type, const byte* key,
word32 length)
{
hmac->macType = (byte)type;
if (type == MD5)
hmac->type = MD5_TYPE;
else if (type == SHA)
hmac->type = SHA1_TYPE;
else if (type == SHA256)
hmac->type = SHA256_TYPE;
else {
CYASSL_MSG("unsupported cavium hmac type");
}
hmac->innerHashKeyed = 0; /* should we key Startup flag */
hmac->keyLen = (word16)length;
/* store key in ipad */
XMEMCPY(hmac->ipad, key, length);
}
#endif /* HAVE_CAVIUM */
#endif /* NO_HMAC */

View File

@@ -307,8 +307,13 @@ int InitRng(RNG* rng)
{
byte key[32];
byte junk[256];
int ret;
int ret = GenerateSeed(&rng->seed, key, sizeof(key));
#ifdef HAVE_CAVIUM
if (rng->magic == CYASSL_RNG_CAVIUM_MAGIC)
return 0;
#endif
ret = GenerateSeed(&rng->seed, key, sizeof(key));
if (ret == 0) {
Arc4SetKey(&rng->cipher, key, sizeof(key));
@@ -318,10 +323,17 @@ int InitRng(RNG* rng)
return ret;
}
#ifdef HAVE_CAVIUM
static void CaviumRNG_GenerateBlock(RNG* rng, byte* output, word32 sz);
#endif
/* place a generated block in output */
void RNG_GenerateBlock(RNG* rng, byte* output, word32 sz)
{
#ifdef HAVE_CAVIUM
if (rng->magic == CYASSL_RNG_CAVIUM_MAGIC)
return CaviumRNG_GenerateBlock(rng, output, sz);
#endif
XMEMSET(output, 0, sz);
Arc4Process(&rng->cipher, output, output, sz);
}
@@ -335,6 +347,50 @@ byte RNG_GenerateByte(RNG* rng)
return b;
}
#ifdef HAVE_CAVIUM
#include <cyassl/ctaocrypt/logging.h>
#include "cavium_common.h"
/* Initiliaze RNG for use with Nitrox device */
int InitRngCavium(RNG* rng, int devId)
{
if (rng == NULL)
return -1;
rng->devId = devId;
rng->magic = CYASSL_RNG_CAVIUM_MAGIC;
return 0;
}
static void CaviumRNG_GenerateBlock(RNG* rng, byte* output, word32 sz)
{
word offset = 0;
word32 requestId;
while (sz > CYASSL_MAX_16BIT) {
word16 slen = (word16)CYASSL_MAX_16BIT;
if (CspRandom(CAVIUM_BLOCKING, slen, output + offset, &requestId,
rng->devId) != 0) {
CYASSL_MSG("Cavium RNG failed");
}
sz -= CYASSL_MAX_16BIT;
offset += CYASSL_MAX_16BIT;
}
if (sz) {
word16 slen = (word16)sz;
if (CspRandom(CAVIUM_BLOCKING, slen, output + offset, &requestId,
rng->devId) != 0) {
CYASSL_MSG("Cavium RNG failed");
}
}
}
#endif /* HAVE_CAVIUM */
#endif /* NO_RC4 */