aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cryptech_novena_i2c_trng.c (renamed from src/cryptech_novena_i2c_entropy.c)39
1 files changed, 24 insertions, 15 deletions
diff --git a/src/cryptech_novena_i2c_entropy.c b/src/cryptech_novena_i2c_trng.c
index b2e6e22..599c0db 100644
--- a/src/cryptech_novena_i2c_entropy.c
+++ b/src/cryptech_novena_i2c_trng.c
@@ -1,11 +1,11 @@
/*
- * cryptech_novena_i2c_entropy.c
+ * cryptech_novena_i2c_trng.c
* ------------------------------
*
* This is an early prototype Hardware Adaption Layer (HAL) for using
* Cryptlib with the Cryptech project's FGPA cores over an I2C bus on
* the Novena PVT1 development board using the "coretest" byte stream
- * protocol. This is compatible with the test/novena_entropy FPGA build.
+ * protocol. This is compatible with the test/novena_trng FPGA build.
*
* The communication channel used here is not suitable for production
* use, this is just a prototype.
@@ -111,12 +111,12 @@
/*
* Address for reading 32 bits of entropy from the noise board.
- * ENTROPY_VALID is nonzero if there's valid entropy available.
+ * TRNG_VALID is nonzero if valid random bits are available.
*/
-#define ENTROPY_PREFIX 0x20
-#define ENTROPY_ADDR 0x20
-#define ENTROPY_VALID 0x11
+#define TRNG_PREFIX 0x0b
+#define TRNG_DATA 0x20
+#define TRNG_VALID 0x11
static int i2cfd = -1;
static int debug = 0;
@@ -363,9 +363,14 @@ static int i2c_wait_valid(const unsigned char addr0)
/*
* First attempt at reading random data from the Novena.
- * Not sure what we should do if ENTROPY_VALID isn't lit, spin wait?
+ *
+ * In theory, we should wait for TRNG_VALID before reading random
+ * data, but as long as this is running over I2C we're going to be so
+ * slow that there's no point, and checking would just make us slower.
*/
+#define WAIT_FOR_TRNG_VALID 0
+
static int readRandom(void *buffer, const int length)
{
unsigned char temp[4], *buf = buffer;
@@ -376,23 +381,27 @@ static int readRandom(void *buffer, const int length)
REQUIRES_B(length >= 1 && length < MAX_INTLENGTH);
for (i = 0; i < length; i += 4) {
- if (!i2c_wait_valid(ENTROPY_PREFIX)) {
- fprintf(stderr, "[ i2c_wait_valid(ENTROPY_PREFIX) failed ]\n");
+
+#if WAIT_FOR_TRNG_VALID
+ if (!i2c_wait_valid(TRNG_PREFIX)) {
+ fprintf(stderr, "[ i2c_wait_valid(TRNG_PREFIX) failed ]\n");
return 0;
}
do {
- if (!i2c_read(ENTROPY_PREFIX, ENTROPY_VALID, temp)) {
- fprintf(stderr, "[ i2c_read(ENTROPY_VALID) failed ]\n");
+ if (!i2c_read(TRNG_PREFIX, TRNG_VALID, temp)) {
+ fprintf(stderr, "[ i2c_read(TRNG_VALID) failed ]\n");
return 0;
}
} while (!temp[3]);
- if (!i2c_wait_valid(ENTROPY_PREFIX)) {
- fprintf(stderr, "[ i2c_wait_valid(ENTROPY_PREFIX) failed ]\n");
+ if (!i2c_wait_valid(TRNG_PREFIX)) {
+ fprintf(stderr, "[ i2c_wait_valid(TRNG_PREFIX) failed ]\n");
return 0;
}
+#endif /* WAIT_FOR_TRNG_VALID */
+
last = (length - i) < 4;
- if (!i2c_read(ENTROPY_PREFIX, ENTROPY_ADDR, last ? temp : buf + i)) {
- fprintf(stderr, "[ i2c_read(ENTROPY_ADDR) failed ]\n");
+ if (!i2c_read(TRNG_PREFIX, TRNG_DATA, (last ? temp : (buf + i)))) {
+ fprintf(stderr, "[ i2c_read(TRNG_DATA) failed ]\n");
return 0;
}
if (last) {