third optimization pass with aes-ctr

This commit is contained in:
JacobBarthelmeh 2023-03-10 08:34:17 -08:00 committed by Jacob Barthelmeh
parent 14b0f422c8
commit f9f1347e8d
2 changed files with 80 additions and 76 deletions

View File

@ -79,6 +79,7 @@ Error caamAddJob(DESCSTRUCT* desc);
Error caamDoJob(DESCSTRUCT* desc);
/******************************************************************************
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])
{
Value ofst = 0;
Error err;
int ivSz = 0;
void *iv = NULL, *key, *in, *out;
int keySz;
int inSz;
int outSz;
int ivSz = 0, keySz, inSz, outSz;
int idx = 0;
int state = CAAM_ALG_UPDATE;
unsigned int keyPhy;
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;
}
unsigned int keyPhy = 0, inPhy = 0, outPhy = 0, ivPhy = 0;
/* 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);
desc->desc[desc->idx++] = (CAAM_KEY | CAAM_CLASS1 | CAAM_NWB) + keySz;
desc->desc[desc->idx++] = keyPhy;
idx++;
/* get IV if needed by algorithm */
@ -1163,22 +1149,11 @@ int caamAes(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
break;
case CAAM_AESCTR:
ofst = 0x00001000;
/* fall through because states are the same only the offset changes */
case CAAM_AESCBC:
{
int maxSz = 16; /* default to CBC/CTR max size */
ivSz = buf[idx].Length;
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);
ivSz = buf[idx].Length;
iv = (void*)buf[idx].TheAddress;
ivPhy = CAAM_ADR_TO_PHYSICAL(iv, ivSz);
idx++;
}
break;
@ -1188,48 +1163,31 @@ int caamAes(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
return CAAM_ARGS_E;
}
/* write operation */
desc->desc[desc->idx++] = CAAM_OP | CAAM_CLASS1 | desc->type |
state | desc->state;
/* get input */
inSz = buf[idx].Length;
in = (void*)buf[idx].TheAddress;
inSz = buf[idx].Length;
in = (void*)buf[idx].TheAddress;
inPhy = CAAM_ADR_TO_PHYSICAL(in, inSz);
idx++;
/* set output */
outSz = buf[idx].Length;
out = (void*)buf[idx].TheAddress;
outSz = buf[idx].Length;
out = (void*)buf[idx].TheAddress;
outPhy = CAAM_ADR_TO_PHYSICAL(out, outSz);
idx++;
/* set input/output in descriptor */
caamAddFIFOLPhy(desc, keyPhy + keySz, inSz, FIFOL_TYPE_LC1);
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;
return caamAesInternal(desc, keyPhy, keySz, ivPhy, ivSz, inPhy, inSz,
outPhy, outSz);
}
/* When a single buffer is used [key] [in] [iv] [out] for optimization */
int caamAesCombined(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
/* When a single buffer is used [key] [in] [iv] [out] for optimization
* 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;
unsigned int keyPhy, inPhy, ivPhy, outPhy;
unsigned int keyPhy;
int keySz, inSz, ivSz = 0, outSz;
void* pt;
@ -1263,11 +1221,17 @@ int caamAesCombined(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4])
outSz = buf[idx].Length;
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,
keyPhy + keySz, inSz,
keyPhy + keySz + inSz + ivSz, outSz);
return caamAesInternal(desc, keyPhy, keySz,
keyPhy + keySz + inSz,ivSz, /* IV */
keyPhy + keySz, inSz, /* IN buffer */
keyPhy + keySz + inSz + ivSz, outSz); /* OUT buffer */
}

View File

@ -46,10 +46,22 @@
#include <sys/neutrino.h>
#include <sys/resmgr.h>
#include <devctl.h>
#include <semaphore.h>
/* virtual address for accessing CAAM addresses */
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 */
#define MAX_PART 7
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;
DESCSTRUCT desc;
CAAM_BUFFER tmp[6] = {0};
CAAM_BUFFER tmp[6];
iov_t in_iovs[6], out_iovs[2];
int inIdx = 0, outIdx = 0;
int algo;
unsigned char *key = NULL, *iv = NULL, *in = NULL, *out = NULL;
unsigned char *pt = NULL;
int keySz, ivSz = 0, inSz, outSz;
unsigned int phyMem = 0;
memset(tmp, 0, sizeof(tmp));
/* get key info */
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;
}
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) {
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;
caamDescInit(&desc, algo, args, tmp, 6);
if (caamAesCombined(&desc, tmp, args) != Success) {
if (caamAesCombined(&desc, tmp, args, phyMem) != Success) {
ret = ECANCELED;
}
}
@ -848,8 +876,13 @@ static int doAES(resmgr_context_t *ctp, io_devctl_t *msg, unsigned int args[4],
}
}
if (key != NULL)
CAAM_ADR_UNMAP(key, 0, keySz + inSz + ivSz + outSz, 0);
if (pt != NULL) {
CAAM_ADR_UNMAP(pt, 0, keySz + inSz + outSz + ivSz, 0);
}
else {
/* done using local mapped memory */
sem_post(&localMemSem);
}
return ret;
}
@ -1686,6 +1719,9 @@ int main(int argc, char *argv[])
WOLFSSL_MSG("unable to start up caam driver!");
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();
if (dpp == NULL) {
@ -1729,7 +1765,11 @@ int main(int argc, char *argv[])
}
}
sem_destroy(&localMemSem);
pthread_mutex_destroy(&sm_mutex);
if (localMemory != NULL)
CAAM_ADR_UNMAP(localMemory, 0, WOLFSSL_CAAM_QNX_MEMORY, 0);
CleanupCAAM();
return 0;
}