From 84e2014fa5c310e14ea196aaa36c88e06968235b Mon Sep 17 00:00:00 2001 From: Patrick Siegl Date: Thu, 28 Mar 2019 00:12:13 +0100 Subject: [PATCH 1/4] Use the struct instead of multipe global vars. --- software/writeentropy.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/software/writeentropy.c b/software/writeentropy.c index 0ff42d6..ac34e8a 100644 --- a/software/writeentropy.c +++ b/software/writeentropy.c @@ -13,9 +13,7 @@ #define SIZE_PROC_FILENAME "/proc/sys/kernel/random/poolsize" #define FILL_PROC_FILENAME "/proc/sys/kernel/random/write_wakeup_threshold" -static uint32_t inmBufLen; -static bool inmDebug; -static int inmDevRandomFD; +static struct pollfd pfd; static uint32_t inmFillWatermark; static struct rand_pool_info *inmPoolInfo; @@ -41,12 +39,10 @@ static uint32_t readNumberFromFile(char *fileName) { // Open /dev/random void inmWriteEntropyStart(uint32_t bufLen, bool debug) { - inmBufLen = bufLen; - inmDebug = debug; - - //inmDevRandomFD = open("/dev/random", O_WRONLY); - inmDevRandomFD = open("/dev/random", O_RDWR); - if(inmDevRandomFD < 0) { + pfd.events = POLLOUT; + //pfd.fd = open("/dev/random", O_WRONLY); + pfd.fd = open("/dev/random", O_RDWR); + if(pfd.fd < 0) { fprintf(stderr, "Unable to open /dev/random\n"); exit(1); } @@ -56,7 +52,7 @@ void inmWriteEntropyStart(uint32_t bufLen, bool debug) { exit(1); } inmFillWatermark = readNumberFromFile(FILL_PROC_FILENAME); - if(inmDebug) { + if(debug) { printf("Entropy pool size:%u, fill watermark:%u\n", readNumberFromFile(SIZE_PROC_FILENAME), inmFillWatermark); } } @@ -68,11 +64,7 @@ void inmWriteEntropyEnd() { // Block until either the entropy pool has room, or 1 minute has passed. void inmWaitForPoolToHaveRoom() { int ent_count; - struct pollfd pfd = { - .fd = inmDevRandomFD, - .events = POLLOUT, - }; - if (ioctl(inmDevRandomFD, RNDGETENTCNT, &ent_count) == 0 && (uint32_t)ent_count < inmFillWatermark) { + if (ioctl(pfd.fd, RNDGETENTCNT, &ent_count) == 0 && (uint32_t)ent_count < inmFillWatermark) { return; } poll(&pfd, 1, 1000u * 60u); // One minute @@ -86,5 +78,5 @@ void inmWriteEntropyToPool(uint8_t *bytes, uint32_t length, uint32_t entropy) { inmPoolInfo->buf_size = length; memcpy(inmPoolInfo->buf, bytes, length); //printf("Writing %u bytes with %u bits of entropy to /dev/random\n", length, entropy); - ioctl(inmDevRandomFD, RNDADDENTROPY, inmPoolInfo); + ioctl(pfd.fd, RNDADDENTROPY, inmPoolInfo); } From fdfe30136d46ba6fe27b8a2209922e9a73848137 Mon Sep 17 00:00:00 2001 From: Patrick Siegl Date: Thu, 28 Mar 2019 00:13:14 +0100 Subject: [PATCH 2/4] Wait until /dev/random is truly required --- software/writeentropy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/software/writeentropy.c b/software/writeentropy.c index ac34e8a..9f363fe 100644 --- a/software/writeentropy.c +++ b/software/writeentropy.c @@ -67,7 +67,7 @@ void inmWaitForPoolToHaveRoom() { if (ioctl(pfd.fd, RNDGETENTCNT, &ent_count) == 0 && (uint32_t)ent_count < inmFillWatermark) { return; } - poll(&pfd, 1, 1000u * 60u); // One minute + poll(&pfd, 1, -1); // waits until /dev/random is in usage } // Add the bytes to the entropy pool. This can be unwhitenened, but the estimated bits of From 64fe6d84a6eed1f66e1b38c7e4a6b06ea6b1fe66 Mon Sep 17 00:00:00 2001 From: Patrick Siegl Date: Thu, 28 Mar 2019 22:03:48 +0100 Subject: [PATCH 3/4] errorflag already set in init --- software/infnoise.c | 1 - 1 file changed, 1 deletion(-) diff --git a/software/infnoise.c b/software/infnoise.c index 03806bf..c468286 100644 --- a/software/infnoise.c +++ b/software/infnoise.c @@ -270,7 +270,6 @@ int main(int argc, char **argv) { fprintf(stderr, "Error: %s\n", context.message); return 1; // ERROR } - context.errorFlag = false; // calculate output size based on the parameters: uint64_t resultSize; From 1dee982ba09f6ebf94924e84ad134c0ac005c0dc Mon Sep 17 00:00:00 2001 From: Patrick Siegl Date: Thu, 28 Mar 2019 22:19:02 +0100 Subject: [PATCH 4/4] ftdi_usb_find_all() requires a free() --- software/libinfnoise.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/software/libinfnoise.c b/software/libinfnoise.c index 636e00e..8b62270 100644 --- a/software/libinfnoise.c +++ b/software/libinfnoise.c @@ -241,6 +241,7 @@ devlist_node listUSBDevices(char **message) { current_entry->next = NULL; } } + ftdi_list_free2(devlist); return return_list; } @@ -255,6 +256,7 @@ bool initializeUSB(struct ftdi_context *ftdic, char **message, char *serial) { *message = "Can't find Infinite Noise Multiplier"; return false; } + ftdi_list_free2(devlist); // only one found, or no serial given if (serial == NULL) {