add dynamic setup of entropy delay on init

This commit is contained in:
Jacob Barthelmeh
2021-06-05 00:41:10 +07:00
parent d7117cd8bb
commit 9ef43c5aff
4 changed files with 112 additions and 71 deletions

View File

@ -438,7 +438,8 @@ static void print_jdkek()
/* instantiate RNG and create JDKEK, TDKEK, and TDSK key */ /* instantiate RNG and create JDKEK, TDKEK, and TDSK key */
static unsigned int wc_rng_start[] = { static unsigned int wc_rng_start[] = {
CAAM_HEAD | 0x00000006, CAAM_HEAD | 0x00000006,
CAAM_OP | CAAM_CLASS1 | CAAM_RNG | 0x00000004, /* Instantiate RNG handle 0 with TRNG */ CAAM_OP | CAAM_CLASS1 | CAAM_RNG | 0x00000004, /* Instantiate RNG handle 0
with TRNG */
CAAM_JUMP | 0x02000001, /* wait for Class1 RNG and jump to next cmd */ CAAM_JUMP | 0x02000001, /* wait for Class1 RNG and jump to next cmd */
CAAM_LOAD | 0x00880004, /* Load to clear written register */ CAAM_LOAD | 0x00880004, /* Load to clear written register */
0x00000001, /* reset done interrupt */ 0x00000001, /* reset done interrupt */
@ -446,86 +447,83 @@ static unsigned int wc_rng_start[] = {
}; };
/* Initialize CAAM RNG /* Initialize CAAM RNG
* returns 0 on success */ * returns 0 on success */
int caamInitRng(struct CAAM_DEVICE* dev); int caamInitRng(struct CAAM_DEVICE* dev);
int caamInitRng(struct CAAM_DEVICE* dev) int caamInitRng(struct CAAM_DEVICE* dev)
{ {
DESCSTRUCT desc; DESCSTRUCT desc;
unsigned int reg, status; unsigned int reg, entropy_delay;
int ret = 0; int ret = 0, i;
/* set up the job description for RNG initialization */
memset(&desc, 0, sizeof(DESCSTRUCT)); memset(&desc, 0, sizeof(DESCSTRUCT));
desc.desc[desc.idx++] = CAAM_HEAD; /* later will put size to header*/
/* Set up use of the TRNG for seeding wolfSSL HASH-DRBG */ for (i = 1; i < 6; i = i + 1) {
/* check out the status and see if already setup */ desc.desc[desc.idx++] = wc_rng_start[i];
CAAM_WRITE(CAAM_RTMCTL, CAAM_PRGM);
CAAM_WRITE(CAAM_RTMCTL, CAAM_READ(CAAM_RTMCTL) | 0x40); /* reset */
/* Set up reading from TRNG */
CAAM_WRITE(CAAM_RTMCTL, CAAM_READ(CAAM_RTMCTL) | CAAM_TRNG);
/* Set up delay for TRNG @TODO Optimizations?
* Shift left with RTSDCTL because 0-15 is for sample number
* Also setting the max and min frequencies */
CAAM_WRITE(CAAM_RTSDCTL, (CAAM_ENT_DLY << 16) | 0x09C4);
CAAM_WRITE(CAAM_RTFRQMIN, CAAM_ENT_DLY >> 1); /* 1/2 */
CAAM_WRITE(CAAM_RTFRQMAX, CAAM_ENT_DLY << 3); /* up to 8x */
/* Set back to run mode and clear RTMCL error bit */
reg = CAAM_READ(CAAM_RTMCTL) ^ CAAM_PRGM;
CAAM_WRITE(CAAM_RTMCTL, reg);
reg = CAAM_READ(CAAM_RTMCTL);
reg |= CAAM_CTLERR;
CAAM_WRITE(CAAM_RTMCTL, reg);
/* check out the status and see if already setup */
reg = CAAM_READ(CAAM_RDSTA);
if (((reg >> 16) & 0xF) > 0) {
WOLFSSL_MSG("RNG is in error state");
caamReset();
} }
desc.caam = dev;
if (reg & (1U << 30)) { /* Attempt to start the RNG, first trying the fastest entropy delay value
WOLFSSL_MSG("JKDKEK rng was setup using a non determinstic key"); * and increasing it after each failed attempt until either a success is hit
return 0; * or the max delay value is.
} */
for (entropy_delay = CAAM_ENT_DLY; entropy_delay <= CAAM_ENT_DLY_MAX;
entropy_delay = entropy_delay + CAAM_ENT_DLY_INCREMENT) {
if (CAAM_READ(0x1014) > 0) { /* Set up use of the TRNG for seeding wolfSSL HASH-DRBG */
int i; /* check out the status and see if already setup */
#ifdef CAAM_DEBUG_MODE CAAM_WRITE(CAAM_RTMCTL, CAAM_PRGM);
for (i = 0; i < 6; i = i + 1) { CAAM_WRITE(CAAM_RTMCTL, CAAM_READ(CAAM_RTMCTL) | 0x40); /* reset */
desc.desc[desc.idx++] = wc_rng_start[i];
/* Set up reading from TRNG */
CAAM_WRITE(CAAM_RTMCTL, CAAM_READ(CAAM_RTMCTL) | CAAM_TRNG);
/* Set up delay for TRNG
* Shift left with RTSDCTL because 0-15 is for sample number
* Also setting the max and min frequencies */
CAAM_WRITE(CAAM_RTSDCTL, (entropy_delay << 16) | CAAM_ENT_SAMPLE);
CAAM_WRITE(CAAM_RTFRQMIN, entropy_delay >> CAAM_ENT_MINSHIFT);
CAAM_WRITE(CAAM_RTFRQMAX, entropy_delay << CAAM_ENT_MAXSHIFT);
#ifdef WOLFSSL_CAAM_PRINT
printf("Attempt with entropy delay set to %d\n", entropy_delay);
printf("Min delay of %d and max of %d\n",
entropy_delay >> CAAM_ENT_MINSHIFT,
entropy_delay << CAAM_ENT_MAXSHIFT);
#endif
/* Set back to run mode and clear RTMCL error bit */
reg = CAAM_READ(CAAM_RTMCTL) ^ CAAM_PRGM;
CAAM_WRITE(CAAM_RTMCTL, reg);
reg = CAAM_READ(CAAM_RTMCTL);
reg |= CAAM_CTLERR;
CAAM_WRITE(CAAM_RTMCTL, reg);
/* check out the status and see if already setup */
reg = CAAM_READ(CAAM_RDSTA);
if (((reg >> 16) & 0xF) > 0) {
WOLFSSL_MSG("RNG is in error state, resetting");
caamReset();
} }
desc.caam = dev; if (reg & (1U << 30)) {
ret = caamDoJob(&desc); WOLFSSL_MSG("JKDKEK rng was setup using a non determinstic key");
#else return 0;
unsigned int *pt = (unsigned int*)caam.ring.VirtualDesc; }
for (i = 0; i < 6; i = i + 1) {
pt[i] = wc_rng_start[i];
}
pt = (unsigned int*)caam.ring.VirtualIn;
pt[0] = (unsigned int)caam.ring.Desc;
/* start process */ do {
#if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT) ret = caamDoJob(&desc);
printf("incrementing job count\n"); } while (ret == CAAM_WAITING);
fflush(stdout);
#endif
CAAM_WRITE(CAAM_IRJAR0, 0x00000001);
#endif
}
else {
return CAAM_WAITING;
}
do { /* if this entropy delay frequency succeeded then break out, otherwise
ret = caamGetJob(dev, &status); * try again with increasing the delay value */
CAAM_CPU_CHILL(); if (ret == Success) {
} while (ret == CAAM_WAITING); WOLFSSL_MSG("Init RNG success");
break;
}
WOLFSSL_MSG("Increasing entropy delay");
}
if (ret == Success) if (ret == Success)
return 0; return 0;
@ -1457,16 +1455,31 @@ int InitCAAM(void)
CAAM_JOBRING_SIZE * sizeof(unsigned int), CAAM_JOBRING_SIZE * sizeof(unsigned int),
PROT_READ | PROT_WRITE | PROT_NOCACHE, PROT_READ | PROT_WRITE | PROT_NOCACHE,
MAP_SHARED | MAP_PHYS, caam.ring.JobIn); MAP_SHARED | MAP_PHYS, caam.ring.JobIn);
if (caam.ring.VirtualIn == MAP_FAILED) {
WOLFSSL_MSG("Error mapping virtual in");
INTERRUPT_Panic();
return -1;
}
memset(caam.ring.VirtualIn, 0, CAAM_JOBRING_SIZE * sizeof(unsigned int)); memset(caam.ring.VirtualIn, 0, CAAM_JOBRING_SIZE * sizeof(unsigned int));
caam.ring.VirtualOut = mmap_device_memory(NULL, caam.ring.VirtualOut = mmap_device_memory(NULL,
2 * CAAM_JOBRING_SIZE * sizeof(unsigned int), 2 * CAAM_JOBRING_SIZE * sizeof(unsigned int),
PROT_READ | PROT_WRITE | PROT_NOCACHE, PROT_READ | PROT_WRITE | PROT_NOCACHE,
MAP_SHARED | MAP_PHYS, caam.ring.JobOut); MAP_SHARED | MAP_PHYS, caam.ring.JobOut);
if (caam.ring.VirtualOut == MAP_FAILED) {
WOLFSSL_MSG("Error mapping virtual out");
INTERRUPT_Panic();
return -1;
}
memset(caam.ring.VirtualOut, 0, 2 * CAAM_JOBRING_SIZE * sizeof(unsigned int)); memset(caam.ring.VirtualOut, 0, 2 * CAAM_JOBRING_SIZE * sizeof(unsigned int));
caam.ring.VirtualDesc = mmap_device_memory(NULL, caam.ring.VirtualDesc = mmap_device_memory(NULL,
CAAM_DESC_MAX * CAAM_JOBRING_SIZE, CAAM_DESC_MAX * CAAM_JOBRING_SIZE,
PROT_READ | PROT_WRITE | PROT_NOCACHE, PROT_READ | PROT_WRITE | PROT_NOCACHE,
MAP_SHARED | MAP_PHYS, caam.ring.Desc); MAP_SHARED | MAP_PHYS, caam.ring.Desc);
if (caam.ring.VirtualDesc == MAP_FAILED) {
WOLFSSL_MSG("Error mapping virtual desc");
INTERRUPT_Panic();
return -1;
}
memset(caam.ring.VirtualDesc, 0, CAAM_DESC_MAX * CAAM_JOBRING_SIZE); memset(caam.ring.VirtualDesc, 0, CAAM_DESC_MAX * CAAM_JOBRING_SIZE);
#if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT) #if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT)
@ -1497,6 +1510,9 @@ int InitCAAM(void)
printf("RTMCTL = 0x%08X\n", CAAM_READ(0x0600)); printf("RTMCTL = 0x%08X\n", CAAM_READ(0x0600));
#endif #endif
WOLFSSL_MSG("Successfully initilazed CAAM driver"); WOLFSSL_MSG("Successfully initilazed CAAM driver");
#if defined(WOLFSSL_CAAM_DEBUG) || defined(WOLFSSL_CAAM_PRINT)
fflush(stdout);
#endif
return 0; return 0;
} }

View File

@ -1255,8 +1255,8 @@ int main(int argc, char *argv[])
if (dpp == NULL) { if (dpp == NULL) {
exit (1); exit (1);
} }
memset (&rattr, 0, sizeof (rattr)); memset(&rattr, 0, sizeof(rattr));
iofunc_func_init (_RESMGR_CONNECT_NFUNCS, &connect_funcs, iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs,
_RESMGR_IO_NFUNCS, &io_funcs); _RESMGR_IO_NFUNCS, &io_funcs);
connect_funcs.open = io_open; connect_funcs.open = io_open;
@ -1266,7 +1266,7 @@ int main(int argc, char *argv[])
io_funcs.devctl = io_devctl; io_funcs.devctl = io_devctl;
iofunc_attr_init (&ioattr, S_IFCHR | 0666, NULL, NULL); iofunc_attr_init (&ioattr, S_IFCHR | 0666, NULL, NULL);
name = resmgr_attach (dpp, &rattr, "/dev/wolfCrypt", name = resmgr_attach(dpp, &rattr, "/dev/wolfCrypt",
_FTYPE_ANY, 0, &connect_funcs, &io_funcs, &ioattr); _FTYPE_ANY, 0, &connect_funcs, &io_funcs, &ioattr);
if (name == -1) { if (name == -1) {
exit (1); exit (1);
@ -1279,7 +1279,7 @@ int main(int argc, char *argv[])
CleanupCAAM(); CleanupCAAM();
exit (1); exit (1);
} }
dispatch_handler (ctp); dispatch_handler(ctp);
} }
pthread_mutex_destroy(&sm_mutex); pthread_mutex_destroy(&sm_mutex);

View File

@ -182,8 +182,28 @@
/* RNG Masks/Values */ /* RNG Masks/Values */
#ifndef CAAM_ENT_DLY #ifndef CAAM_ENT_DLY
#define CAAM_ENT_DLY 1200 /* @TODO lower value may gain performance */ /*less than half the default value to try and increase entropy collection */
#define CAAM_ENT_DLY 1200
#endif #endif
#ifndef CAAM_ENT_DLY_INCREMENT
#define CAAM_ENT_DLY_INCREMENT 500
#endif
#ifndef CAAM_ENT_SAMPLE
/* default sample value from reference manual */
#define CAAM_ENT_SAMPLE 0x09C4
#endif
#ifndef CAAM_ENT_DLY_MAX
#define CAAM_ENT_DLY_MAX 12000
#endif
#ifndef CAAM_ENT_MINSHIFT
/* default to the minimum entropy delay of 1/4 */
#define CAAM_ENT_MINSHIFT 2
#endif
#ifndef CAAM_ENT_MAXSHIFT
/* default to the maximum entropy delay of 16 times */
#define CAAM_ENT_MAXSHIFT 4
#endif
#define CAAM_PRGM 0x00010000 /* Set RTMCTL to program state */ #define CAAM_PRGM 0x00010000 /* Set RTMCTL to program state */
#define CAAM_TRNG 0x00000020 /* Set TRNG access */ #define CAAM_TRNG 0x00000020 /* Set TRNG access */
#define CAAM_CTLERR 0x00001000 #define CAAM_CTLERR 0x00001000

View File

@ -25,7 +25,12 @@
#ifndef CAAM_QNX_H #ifndef CAAM_QNX_H
#define CAAM_QNX_H #define CAAM_QNX_H
#ifdef WOLFSSL_CAAM_PRINT
#include <stdio.h>
#define WOLFSSL_MSG(in) printf("%s\n", (in))
#else
#define WOLFSSL_MSG(in) #define WOLFSSL_MSG(in)
#endif
#include <sys/mman.h> #include <sys/mman.h>
#include <hw/inout.h> #include <hw/inout.h>