mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-30 18:57:27 +02:00
third optimization pass with aes-ctr
This commit is contained in:
committed by
Jacob Barthelmeh
parent
14b0f422c8
commit
f9f1347e8d
@ -79,6 +79,7 @@ Error caamAddJob(DESCSTRUCT* desc);
|
|||||||
Error caamDoJob(DESCSTRUCT* desc);
|
Error caamDoJob(DESCSTRUCT* desc);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
Internal CAAM Job Ring and partition functions
|
Internal CAAM Job Ring and partition functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -1124,37 +1125,22 @@ static int caamAesInternal(DESCSTRUCT* desc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* AES operations follow the buffer sequence of KEY -> (IV) -> Input -> Output
|
/* Scattered memory, does a mapping for each variable. Less performant than
|
||||||
|
* caamAesCombined.
|
||||||
|
* AES operations follow the buffer sequence of KEY -> (IV) -> Input -> Output
|
||||||
*/
|
*/
|
||||||
int caamAes(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
|
int caamAes(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
|
||||||
{
|
{
|
||||||
Value ofst = 0;
|
|
||||||
Error err;
|
|
||||||
int ivSz = 0;
|
|
||||||
void *iv = NULL, *key, *in, *out;
|
void *iv = NULL, *key, *in, *out;
|
||||||
int keySz;
|
int ivSz = 0, keySz, inSz, outSz;
|
||||||
int inSz;
|
|
||||||
int outSz;
|
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
int state = CAAM_ALG_UPDATE;
|
|
||||||
|
|
||||||
unsigned int keyPhy;
|
unsigned int keyPhy = 0, inPhy = 0, outPhy = 0, ivPhy = 0;
|
||||||
|
|
||||||
if (desc->state != CAAM_ENC && desc->state != CAAM_DEC) {
|
|
||||||
return CAAM_ARGS_E;
|
|
||||||
}
|
|
||||||
|
|
||||||
keySz = buf[idx].Length;
|
|
||||||
if (keySz != 16 && keySz != 24 && keySz != 32) {
|
|
||||||
WOLFSSL_MSG("Bad AES key size found");
|
|
||||||
return CAAM_ARGS_E;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* map and copy over key */
|
/* map and copy over key */
|
||||||
key = (void*)buf[idx].TheAddress;
|
key = (void*)buf[idx].TheAddress;
|
||||||
|
keySz = buf[idx].Length;
|
||||||
keyPhy = CAAM_ADR_TO_PHYSICAL(key, keySz);
|
keyPhy = CAAM_ADR_TO_PHYSICAL(key, keySz);
|
||||||
desc->desc[desc->idx++] = (CAAM_KEY | CAAM_CLASS1 | CAAM_NWB) + keySz;
|
|
||||||
desc->desc[desc->idx++] = keyPhy;
|
|
||||||
idx++;
|
idx++;
|
||||||
|
|
||||||
/* get IV if needed by algorithm */
|
/* get IV if needed by algorithm */
|
||||||
@ -1163,22 +1149,11 @@ int caamAes(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case CAAM_AESCTR:
|
case CAAM_AESCTR:
|
||||||
ofst = 0x00001000;
|
|
||||||
/* fall through because states are the same only the offset changes */
|
|
||||||
|
|
||||||
case CAAM_AESCBC:
|
case CAAM_AESCBC:
|
||||||
{
|
{
|
||||||
int maxSz = 16; /* default to CBC/CTR max size */
|
ivSz = buf[idx].Length;
|
||||||
|
iv = (void*)buf[idx].TheAddress;
|
||||||
ivSz = buf[idx].Length;
|
ivPhy = CAAM_ADR_TO_PHYSICAL(iv, ivSz);
|
||||||
if (ivSz != maxSz) {
|
|
||||||
WOLFSSL_MSG("Invalid AES-CBC IV size\n");
|
|
||||||
return CAAM_ARGS_E;
|
|
||||||
}
|
|
||||||
|
|
||||||
iv = (void*)buf[idx].TheAddress;
|
|
||||||
desc->desc[desc->idx++] = (CAAM_LOAD_CTX | CAAM_CLASS1 | ofst) + maxSz;
|
|
||||||
desc->desc[desc->idx++] = (CAAM_ADDRESS)CAAM_ADR_TO_PHYSICAL(iv, ivSz);
|
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -1188,48 +1163,31 @@ int caamAes(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
|
|||||||
return CAAM_ARGS_E;
|
return CAAM_ARGS_E;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* write operation */
|
|
||||||
desc->desc[desc->idx++] = CAAM_OP | CAAM_CLASS1 | desc->type |
|
|
||||||
state | desc->state;
|
|
||||||
|
|
||||||
/* get input */
|
/* get input */
|
||||||
inSz = buf[idx].Length;
|
inSz = buf[idx].Length;
|
||||||
in = (void*)buf[idx].TheAddress;
|
in = (void*)buf[idx].TheAddress;
|
||||||
|
inPhy = CAAM_ADR_TO_PHYSICAL(in, inSz);
|
||||||
idx++;
|
idx++;
|
||||||
|
|
||||||
/* set output */
|
/* set output */
|
||||||
outSz = buf[idx].Length;
|
outSz = buf[idx].Length;
|
||||||
out = (void*)buf[idx].TheAddress;
|
out = (void*)buf[idx].TheAddress;
|
||||||
|
outPhy = CAAM_ADR_TO_PHYSICAL(out, outSz);
|
||||||
idx++;
|
idx++;
|
||||||
|
|
||||||
/* set input/output in descriptor */
|
return caamAesInternal(desc, keyPhy, keySz, ivPhy, ivSz, inPhy, inSz,
|
||||||
caamAddFIFOLPhy(desc, keyPhy + keySz, inSz, FIFOL_TYPE_LC1);
|
outPhy, outSz);
|
||||||
caamAddFIFOS(desc, out, outSz);
|
|
||||||
|
|
||||||
/* store updated IV */
|
|
||||||
switch (desc->type) {
|
|
||||||
case CAAM_AESCBC:
|
|
||||||
case CAAM_AESCTR:
|
|
||||||
if (ivSz > 0) {
|
|
||||||
desc->desc[desc->idx++] = CAAM_STORE_CTX | CAAM_CLASS1 | ofst | 16;
|
|
||||||
desc->desc[desc->idx++] = (CAAM_ADDRESS)CAAM_ADR_TO_PHYSICAL(iv, ivSz);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
err = caamDoJob(desc);
|
|
||||||
} while (err == CAAM_WAITING);
|
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* When a single buffer is used [key] [in] [iv] [out] for optimization */
|
/* When a single buffer is used [key] [in] [iv] [out] for optimization
|
||||||
int caamAesCombined(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
|
* phyMem is non 0 when the physical memory location is already known
|
||||||
|
*/
|
||||||
|
int caamAesCombined(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4],
|
||||||
|
unsigned int phyMem)
|
||||||
{
|
{
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
unsigned int keyPhy, inPhy, ivPhy, outPhy;
|
unsigned int keyPhy;
|
||||||
int keySz, inSz, ivSz = 0, outSz;
|
int keySz, inSz, ivSz = 0, outSz;
|
||||||
void* pt;
|
void* pt;
|
||||||
|
|
||||||
@ -1263,11 +1221,17 @@ int caamAesCombined(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
|
|||||||
outSz = buf[idx].Length;
|
outSz = buf[idx].Length;
|
||||||
idx++;
|
idx++;
|
||||||
|
|
||||||
keyPhy = CAAM_ADR_TO_PHYSICAL(pt, keySz + inSz + ivSz + outSz);
|
if (phyMem != 0) {
|
||||||
|
keyPhy = phyMem;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
keyPhy = CAAM_ADR_TO_PHYSICAL(pt, keySz + inSz + ivSz + outSz);
|
||||||
|
}
|
||||||
|
|
||||||
return caamAesInternal(desc, keyPhy, keySz, keyPhy + keySz + inSz,ivSz,
|
return caamAesInternal(desc, keyPhy, keySz,
|
||||||
keyPhy + keySz, inSz,
|
keyPhy + keySz + inSz,ivSz, /* IV */
|
||||||
keyPhy + keySz + inSz + ivSz, outSz);
|
keyPhy + keySz, inSz, /* IN buffer */
|
||||||
|
keyPhy + keySz + inSz + ivSz, outSz); /* OUT buffer */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,10 +46,22 @@
|
|||||||
#include <sys/neutrino.h>
|
#include <sys/neutrino.h>
|
||||||
#include <sys/resmgr.h>
|
#include <sys/resmgr.h>
|
||||||
#include <devctl.h>
|
#include <devctl.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
|
||||||
/* virtual address for accessing CAAM addresses */
|
/* virtual address for accessing CAAM addresses */
|
||||||
uintptr_t virtual_base = 0;
|
uintptr_t virtual_base = 0;
|
||||||
|
|
||||||
|
static void* localMemory = NULL;
|
||||||
|
static unsigned int localPhy = 0;
|
||||||
|
sem_t localMemSem;
|
||||||
|
|
||||||
|
/* Can be overriden, variable for how large of a local buffer to have.
|
||||||
|
* This allows for large performance gains when avoiding mapping new memory
|
||||||
|
* for each operation. */
|
||||||
|
#ifndef WOLFSSL_CAAM_QNX_MEMORY
|
||||||
|
#define WOLFSSL_CAAM_QNX_MEMORY 250000
|
||||||
|
#endif
|
||||||
|
|
||||||
/* keep track of which ID memory belongs to so it can be free'd up */
|
/* keep track of which ID memory belongs to so it can be free'd up */
|
||||||
#define MAX_PART 7
|
#define MAX_PART 7
|
||||||
pthread_mutex_t sm_mutex;
|
pthread_mutex_t sm_mutex;
|
||||||
@ -739,12 +751,16 @@ static int doAES(resmgr_context_t *ctp, io_devctl_t *msg, unsigned int args[4],
|
|||||||
{
|
{
|
||||||
int ret = EOK, i = 0;
|
int ret = EOK, i = 0;
|
||||||
DESCSTRUCT desc;
|
DESCSTRUCT desc;
|
||||||
CAAM_BUFFER tmp[6] = {0};
|
CAAM_BUFFER tmp[6];
|
||||||
iov_t in_iovs[6], out_iovs[2];
|
iov_t in_iovs[6], out_iovs[2];
|
||||||
int inIdx = 0, outIdx = 0;
|
int inIdx = 0, outIdx = 0;
|
||||||
int algo;
|
int algo;
|
||||||
unsigned char *key = NULL, *iv = NULL, *in = NULL, *out = NULL;
|
unsigned char *key = NULL, *iv = NULL, *in = NULL, *out = NULL;
|
||||||
|
unsigned char *pt = NULL;
|
||||||
int keySz, ivSz = 0, inSz, outSz;
|
int keySz, ivSz = 0, inSz, outSz;
|
||||||
|
unsigned int phyMem = 0;
|
||||||
|
|
||||||
|
memset(tmp, 0, sizeof(tmp));
|
||||||
|
|
||||||
/* get key info */
|
/* get key info */
|
||||||
keySz = args[1] & 0xFFFF; /* key size */
|
keySz = args[1] & 0xFFFF; /* key size */
|
||||||
@ -754,7 +770,19 @@ static int doAES(resmgr_context_t *ctp, io_devctl_t *msg, unsigned int args[4],
|
|||||||
ivSz = 16;
|
ivSz = 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
key = (unsigned char*)CAAM_ADR_MAP(0, keySz + inSz + outSz + ivSz, 0);
|
if (keySz + inSz + outSz + ivSz < WOLFSSL_CAAM_QNX_MEMORY) {
|
||||||
|
if (sem_trywait(&localMemSem) == 0) {
|
||||||
|
key = localMemory;
|
||||||
|
phyMem = localPhy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* local pre-mapped memory was not used, try to map some memory now */
|
||||||
|
if (key == NULL) {
|
||||||
|
pt = (unsigned char*)CAAM_ADR_MAP(0, keySz + inSz + outSz + ivSz, 0);
|
||||||
|
key = pt;
|
||||||
|
}
|
||||||
|
|
||||||
if (key == NULL) {
|
if (key == NULL) {
|
||||||
ret = ECANCELED;
|
ret = ECANCELED;
|
||||||
}
|
}
|
||||||
@ -828,7 +856,7 @@ static int doAES(resmgr_context_t *ctp, io_devctl_t *msg, unsigned int args[4],
|
|||||||
tmp[i].TheAddress = (CAAM_ADDRESS)out;
|
tmp[i].TheAddress = (CAAM_ADDRESS)out;
|
||||||
|
|
||||||
caamDescInit(&desc, algo, args, tmp, 6);
|
caamDescInit(&desc, algo, args, tmp, 6);
|
||||||
if (caamAesCombined(&desc, tmp, args) != Success) {
|
if (caamAesCombined(&desc, tmp, args, phyMem) != Success) {
|
||||||
ret = ECANCELED;
|
ret = ECANCELED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -848,8 +876,13 @@ static int doAES(resmgr_context_t *ctp, io_devctl_t *msg, unsigned int args[4],
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key != NULL)
|
if (pt != NULL) {
|
||||||
CAAM_ADR_UNMAP(key, 0, keySz + inSz + ivSz + outSz, 0);
|
CAAM_ADR_UNMAP(pt, 0, keySz + inSz + outSz + ivSz, 0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* done using local mapped memory */
|
||||||
|
sem_post(&localMemSem);
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -1686,6 +1719,9 @@ int main(int argc, char *argv[])
|
|||||||
WOLFSSL_MSG("unable to start up caam driver!");
|
WOLFSSL_MSG("unable to start up caam driver!");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
localMemory = (unsigned char*)CAAM_ADR_MAP(0, WOLFSSL_CAAM_QNX_MEMORY, 0);
|
||||||
|
localPhy = CAAM_ADR_TO_PHYSICAL(localMemory, WOLFSSL_CAAM_QNX_MEMORY);
|
||||||
|
sem_init(&localMemSem, 1, 1);
|
||||||
|
|
||||||
dpp = dispatch_create();
|
dpp = dispatch_create();
|
||||||
if (dpp == NULL) {
|
if (dpp == NULL) {
|
||||||
@ -1729,7 +1765,11 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sem_destroy(&localMemSem);
|
||||||
pthread_mutex_destroy(&sm_mutex);
|
pthread_mutex_destroy(&sm_mutex);
|
||||||
|
if (localMemory != NULL)
|
||||||
|
CAAM_ADR_UNMAP(localMemory, 0, WOLFSSL_CAAM_QNX_MEMORY, 0);
|
||||||
|
|
||||||
CleanupCAAM();
|
CleanupCAAM();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user