Merge pull request #83 from psiegl/writeent

Writeentropy reduced in amount of global vars. To this, waits until /dev/random in usage
This commit is contained in:
Manuel Domke
2019-03-29 19:03:53 +01:00
committed by GitHub
3 changed files with 11 additions and 18 deletions

View File

@@ -270,7 +270,6 @@ int main(int argc, char **argv) {
fprintf(stderr, "Error: %s\n", context.message); fprintf(stderr, "Error: %s\n", context.message);
return 1; // ERROR return 1; // ERROR
} }
context.errorFlag = false;
// calculate output size based on the parameters: // calculate output size based on the parameters:
uint64_t resultSize; uint64_t resultSize;

View File

@@ -241,6 +241,7 @@ devlist_node listUSBDevices(char **message) {
current_entry->next = NULL; current_entry->next = NULL;
} }
} }
ftdi_list_free2(devlist);
return return_list; return return_list;
} }
@@ -255,6 +256,7 @@ bool initializeUSB(struct ftdi_context *ftdic, char **message, char *serial) {
*message = "Can't find Infinite Noise Multiplier"; *message = "Can't find Infinite Noise Multiplier";
return false; return false;
} }
ftdi_list_free2(devlist);
// only one found, or no serial given // only one found, or no serial given
if (serial == NULL) { if (serial == NULL) {

View File

@@ -13,9 +13,7 @@
#define SIZE_PROC_FILENAME "/proc/sys/kernel/random/poolsize" #define SIZE_PROC_FILENAME "/proc/sys/kernel/random/poolsize"
#define FILL_PROC_FILENAME "/proc/sys/kernel/random/write_wakeup_threshold" #define FILL_PROC_FILENAME "/proc/sys/kernel/random/write_wakeup_threshold"
static uint32_t inmBufLen; static struct pollfd pfd;
static bool inmDebug;
static int inmDevRandomFD;
static uint32_t inmFillWatermark; static uint32_t inmFillWatermark;
static struct rand_pool_info *inmPoolInfo; static struct rand_pool_info *inmPoolInfo;
@@ -39,12 +37,10 @@ static uint32_t readNumberFromFile(char *fileName) {
// Open /dev/random // Open /dev/random
void inmWriteEntropyStart(uint32_t bufLen, bool debug) { void inmWriteEntropyStart(uint32_t bufLen, bool debug) {
inmBufLen = bufLen; pfd.events = POLLOUT;
inmDebug = debug; //pfd.fd = open("/dev/random", O_WRONLY);
pfd.fd = open("/dev/random", O_RDWR);
//inmDevRandomFD = open("/dev/random", O_WRONLY); if(pfd.fd < 0) {
inmDevRandomFD = open("/dev/random", O_RDWR);
if(inmDevRandomFD < 0) {
fprintf(stderr, "Unable to open /dev/random\n"); fprintf(stderr, "Unable to open /dev/random\n");
exit(1); exit(1);
} }
@@ -54,7 +50,7 @@ void inmWriteEntropyStart(uint32_t bufLen, bool debug) {
exit(1); exit(1);
} }
inmFillWatermark = readNumberFromFile(FILL_PROC_FILENAME); inmFillWatermark = readNumberFromFile(FILL_PROC_FILENAME);
if(inmDebug) { if(debug) {
printf("Entropy pool size:%u, fill watermark:%u\n", readNumberFromFile(SIZE_PROC_FILENAME), inmFillWatermark); printf("Entropy pool size:%u, fill watermark:%u\n", readNumberFromFile(SIZE_PROC_FILENAME), inmFillWatermark);
} }
} }
@@ -66,14 +62,10 @@ void inmWriteEntropyEnd() {
// Block until either the entropy pool has room, or 1 minute has passed. // Block until either the entropy pool has room, or 1 minute has passed.
void inmWaitForPoolToHaveRoom() { void inmWaitForPoolToHaveRoom() {
int ent_count; int ent_count;
struct pollfd pfd = { if (ioctl(pfd.fd, RNDGETENTCNT, &ent_count) == 0 && (uint32_t)ent_count < inmFillWatermark) {
.fd = inmDevRandomFD,
.events = POLLOUT,
};
if (ioctl(inmDevRandomFD, RNDGETENTCNT, &ent_count) == 0 && (uint32_t)ent_count < inmFillWatermark) {
return; 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 // Add the bytes to the entropy pool. This can be unwhitenened, but the estimated bits of
@@ -84,5 +76,5 @@ void inmWriteEntropyToPool(uint8_t *bytes, uint32_t length, uint32_t entropy) {
inmPoolInfo->buf_size = length; inmPoolInfo->buf_size = length;
memcpy(inmPoolInfo->buf, bytes, length); memcpy(inmPoolInfo->buf, bytes, length);
//printf("Writing %u bytes with %u bits of entropy to /dev/random\n", length, entropy); //printf("Writing %u bytes with %u bits of entropy to /dev/random\n", length, entropy);
ioctl(inmDevRandomFD, RNDADDENTROPY, inmPoolInfo); ioctl(pfd.fd, RNDADDENTROPY, inmPoolInfo);
} }