Merge branch 'master' into cleanup
This commit is contained in:
@@ -8,43 +8,35 @@
|
||||
|
||||
#include "infnoise.h"
|
||||
|
||||
// Write af PID in a file
|
||||
// Write PID in a file
|
||||
static bool writePid(int32_t pid, char *fileName) {
|
||||
FILE *pidFile;
|
||||
int ret;
|
||||
pidFile = fopen(fileName,"w");
|
||||
FILE *pidFile = fopen(fileName, "w");
|
||||
if(pidFile == NULL) {
|
||||
return errno;
|
||||
}
|
||||
ret = fprintf(pidFile, "%d\n", pid);
|
||||
if (ret < 0) {
|
||||
return false;
|
||||
}
|
||||
int ret = fprintf(pidFile, "%d\n", pid);
|
||||
fclose(pidFile);
|
||||
return true;
|
||||
return (ret >= 0);
|
||||
}
|
||||
|
||||
void startDaemon(struct opt_struct* opts) {
|
||||
if(!opts->daemon) {
|
||||
// No backgrounding, optionslly write current PID
|
||||
if(opts->pidFileName != NULL) {
|
||||
writePid(getpid(), opts->pidFileName);
|
||||
}
|
||||
return;
|
||||
}
|
||||
int32_t pid = fork();
|
||||
if(pid < 0) {
|
||||
fputs("fork() failed\n", stderr);
|
||||
exit(1);
|
||||
} else if(pid > 0) {
|
||||
// Parent
|
||||
if(opts->pidFileName != NULL) {
|
||||
if(!writePid(pid, opts->pidFileName)) {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
// Child
|
||||
if(!opts->daemon) {
|
||||
// No backgrounding, optionally write current PID
|
||||
if(opts->pidFileName != NULL) {
|
||||
writePid(getpid(), opts->pidFileName);
|
||||
}
|
||||
return;
|
||||
}
|
||||
int32_t pid = fork();
|
||||
if(pid < 0) {
|
||||
fputs("fork() failed\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
if(pid > 0) {
|
||||
// Parent
|
||||
exit(opts->pidFileName != NULL
|
||||
&& !writePid(pid, opts->pidFileName));
|
||||
}
|
||||
// Child
|
||||
}
|
||||
|
||||
|
||||
@@ -101,10 +101,10 @@ bool inmHealthCheckStart(uint8_t N, double K, bool debug) {
|
||||
inmK = K;
|
||||
inmN = N;
|
||||
inmPrevBits = 0u;
|
||||
inmOnesEven = calloc(1u << N, sizeof(uint32_t));
|
||||
inmZerosEven = calloc(1u << N, sizeof(uint32_t));
|
||||
inmOnesOdd = calloc(1u << N, sizeof(uint32_t));
|
||||
inmZerosOdd = calloc(1u << N, sizeof(uint32_t));
|
||||
inmOnesEven = calloc(1u << N, sizeof(*inmOnesEven));
|
||||
inmZerosEven = calloc(1u << N, sizeof(*inmZerosEven));
|
||||
inmOnesOdd = calloc(1u << N, sizeof(*inmOnesOdd));
|
||||
inmZerosOdd = calloc(1u << N, sizeof(*inmZerosOdd));
|
||||
inmExpectedEntropyPerBit = log(K)/log(2.0);
|
||||
inmTotalBits = 0u;
|
||||
inmPrevBit = false;
|
||||
@@ -156,16 +156,10 @@ bool inmHealthCheckAddBit(bool evenBit, bool oddBit, bool even) {
|
||||
bool bit;
|
||||
if(even) {
|
||||
bit = evenBit;
|
||||
if(evenBit != inmPrevEven) {
|
||||
inmEvenMisfires++;
|
||||
//printf("even misfire\n");
|
||||
}
|
||||
inmEvenMisfires += (evenBit != inmPrevEven);
|
||||
} else {
|
||||
bit = oddBit;
|
||||
if(oddBit != inmPrevOdd) {
|
||||
inmOddMisfires++;
|
||||
//printf("odd misfire\n");
|
||||
}
|
||||
inmOddMisfires += (oddBit != inmPrevOdd);
|
||||
}
|
||||
inmPrevEven = evenBit;
|
||||
inmPrevOdd = oddBit;
|
||||
@@ -177,8 +171,8 @@ bool inmHealthCheckAddBit(bool evenBit, bool oddBit, bool even) {
|
||||
fprintf(stderr, "num1s:%f%%, even misfires:%f%%, odd misfires:%f%%\n",
|
||||
inmTotalOnes*100.0/(inmTotalZeros + inmTotalOnes),
|
||||
inmEvenMisfires*100.0/inmNumBitsSampled, inmOddMisfires*100.0/inmNumBitsSampled);
|
||||
fflush(stderr);
|
||||
}
|
||||
fflush(stderr);
|
||||
}
|
||||
inmPrevBits = (inmPrevBits << 1) & ((1 << inmN)-1);
|
||||
if(inmPrevBit) {
|
||||
inmPrevBits |= 1;
|
||||
@@ -305,12 +299,10 @@ static void checkLSBStatsForNBits(uint8_t N) {
|
||||
uint32_t totalGuesses = 0u;
|
||||
uint32_t totalRight = 0.0;
|
||||
for(i = 0u; i < (1u << N); i++) {
|
||||
uint32_t total = 0u;
|
||||
uint32_t zeros = 0u;
|
||||
uint32_t ones = 0u;
|
||||
for(j = 0u; j < (1u << (inmN - N)); j++) {
|
||||
uint32_t pos = i + j*(1u << N);
|
||||
total += inmZerosEven[pos] + inmOnesEven[pos];
|
||||
zeros += inmZerosEven[pos];
|
||||
ones += inmOnesEven[pos];
|
||||
}
|
||||
@@ -319,7 +311,7 @@ static void checkLSBStatsForNBits(uint8_t N) {
|
||||
} else {
|
||||
totalRight += ones;
|
||||
}
|
||||
totalGuesses += total;
|
||||
totalGuesses += zeros + ones;
|
||||
}
|
||||
printf("Probability of guessing correctly with %u bits: %f\n", N, (double)totalRight/totalGuesses);
|
||||
}
|
||||
@@ -356,17 +348,17 @@ static inline bool computeRandBit(double *A, double K, double noiseAmplitude) {
|
||||
}
|
||||
|
||||
static void initOpts(struct opt_struct *opts) {
|
||||
opts->outputMultiplier = 0u;
|
||||
opts->daemon =
|
||||
opts->outputMultiplier = 0u;
|
||||
opts->daemon =
|
||||
opts->debug =
|
||||
opts->devRandom =
|
||||
opts->noOutput =
|
||||
opts->listDevices =
|
||||
opts->raw = false;
|
||||
opts->version = false;
|
||||
opts->help = false;
|
||||
opts->none = false;
|
||||
opts->pidFileName =
|
||||
opts->version = false;
|
||||
opts->help = false;
|
||||
opts->none = false;
|
||||
opts->pidFileName =
|
||||
opts->serial = NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -137,14 +137,14 @@ int main(int argc, char **argv) {
|
||||
break;
|
||||
case 'p':
|
||||
opts.pidFileName = optarg;
|
||||
if (opts.pidFileName == NULL || !strcmp("", opts.pidFileName)) {
|
||||
if (opts.pidFileName == NULL || opts.pidFileName[0] == '\0') {
|
||||
fputs("--pidfile without file name\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
case 's':
|
||||
opts.serial = optarg;
|
||||
if (opts.serial == NULL || !strcmp("", opts.serial)) {
|
||||
if (opts.serial == NULL || opts.serial[0] == '\0') {
|
||||
fputs("--serial without value\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
@@ -184,31 +184,31 @@ int main(int argc, char **argv) {
|
||||
" -v, --version - show version information\n"
|
||||
" -h, --help - this help output\n",
|
||||
stdout);
|
||||
if (opts.none) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return opts.none;
|
||||
}
|
||||
|
||||
if (opts.version) {
|
||||
printf("GIT VERSION - %s\n", GIT_VERSION);
|
||||
printf("GIT COMMIT - %s\n", GIT_COMMIT);
|
||||
printf("GIT DATE - %s\n", GIT_DATE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// read environment variables, not overriding command line options
|
||||
if (opts.serial == NULL) {
|
||||
if (getenv("INFNOISE_SERIAL") != NULL) {
|
||||
opts.serial = getenv("INFNOISE_SERIAL");
|
||||
}
|
||||
opts.serial = getenv("INFNOISE_SERIAL");
|
||||
}
|
||||
|
||||
if (!opts.debug) {
|
||||
if (getenv("INFNOISE_DEBUG") != NULL) {
|
||||
if (!strcmp("true", getenv("INFNOISE_DEBUG"))) {
|
||||
opts.debug = true;
|
||||
}
|
||||
}
|
||||
char *envDbg = getenv("INFNOISE_DEBUG");
|
||||
opts.debug = (envDbg != NULL
|
||||
&& !strcmp("true", envDbg));
|
||||
}
|
||||
|
||||
if (!multiplierAssigned) {
|
||||
if (getenv("INFNOISE_MULTIPLIER") != NULL) {
|
||||
int tmpOutputMult = atoi(getenv("INFNOISE_MULTIPLIER"));
|
||||
char *envMultiplier = getenv("INFNOISE_MULTIPLIER");
|
||||
if (envMultiplier != NULL) {
|
||||
int tmpOutputMult = atoi(envMultiplier);
|
||||
if (tmpOutputMult < 0) {
|
||||
fputs("Multiplier must be >= 0\n", stderr);
|
||||
return 1;
|
||||
@@ -222,25 +222,22 @@ int main(int argc, char **argv) {
|
||||
opts.outputMultiplier = 2u; // Don't throw away entropy when writing to /dev/random unless told to do so
|
||||
}
|
||||
|
||||
if (opts.version) {
|
||||
printf("GIT VERSION - %s\n", GIT_VERSION);
|
||||
printf("GIT COMMIT - %s\n", GIT_COMMIT);
|
||||
printf("GIT DATE - %s\n", GIT_DATE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (opts.listDevices) {
|
||||
devlist_node devlist = listUSBDevices(&context.message);
|
||||
if (devlist == NULL) {
|
||||
fprintf(stderr, "Error: %s\n", context.message);
|
||||
return 1;
|
||||
}
|
||||
devlist_node curdev = NULL;
|
||||
uint8_t i = 0;
|
||||
for (curdev = devlist; curdev != NULL; i++) {
|
||||
printf("ID: %i, Manufacturer: %s, Description: %s, Serial: %s\n", curdev->id, curdev->manufacturer,
|
||||
devlist_node curdev;
|
||||
for (curdev = devlist; curdev != NULL; ) {
|
||||
printf("ID: %i, Manufacturer: %s, Description: %s, Serial: %s\n",
|
||||
curdev->id, curdev->manufacturer,
|
||||
curdev->description, curdev->serial);
|
||||
|
||||
// cleanup:
|
||||
devlist_node olddev = curdev;
|
||||
curdev = curdev->next;
|
||||
free(olddev);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -315,9 +312,7 @@ int main(int argc, char **argv) {
|
||||
fprintf(stderr, "Output %lu bytes\n", (unsigned long) totalBytesWritten);
|
||||
}
|
||||
}
|
||||
|
||||
deinitInfnoise(&context);
|
||||
inmWriteEntropyEnd();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -82,9 +82,10 @@ void prepareOutputBuffer() {
|
||||
uint32_t i;
|
||||
|
||||
// Endless loop: set SW1EN and SW2EN alternately
|
||||
for (i = 0u; i < BUFLEN; i++) {
|
||||
for (i = 0u; i < BUFLEN; i+=2) {
|
||||
// Alternate Ph1 and Ph2
|
||||
outBuf[i] = i & 1 ? (1 << SWEN2) : (1 << SWEN1);
|
||||
outBuf[i] = (1 << SWEN1);
|
||||
outBuf[i+1] = (1 << SWEN2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,10 +93,10 @@ void prepareOutputBuffer() {
|
||||
// changes, not both, so alternate reading bits from them. We get 1 INM bit of output
|
||||
// per byte read. Feed bits from the INM to the health checker. Return the expected
|
||||
// bits of entropy.
|
||||
uint32_t extractBytes(uint8_t *bytes, uint8_t *inBuf, char **message, bool *errorFlag) {
|
||||
uint32_t extractBytes(uint8_t *bytes, uint32_t length, uint8_t *inBuf, char **message, bool *errorFlag) {
|
||||
inmClearEntropyLevel();
|
||||
uint32_t i;
|
||||
for (i = 0u; i < BUFLEN / 8u; i++) {
|
||||
for (i = 0u; i < length; i++) {
|
||||
uint32_t j;
|
||||
uint8_t byte = 0u;
|
||||
for (j = 0u; j < 8u; j++) {
|
||||
@@ -203,8 +204,6 @@ devlist_node listUSBDevices(char **message) {
|
||||
ftdi_init(&ftdic);
|
||||
|
||||
struct ftdi_device_list *devlist = NULL;
|
||||
struct ftdi_device_list *curdev = NULL;
|
||||
int i = 0;
|
||||
|
||||
// search devices
|
||||
int rc = ftdi_usb_find_all(&ftdic, &devlist, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID);
|
||||
@@ -216,9 +215,11 @@ devlist_node listUSBDevices(char **message) {
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
devlist_node return_list = malloc(sizeof(struct infnoise_devlist_node));
|
||||
devlist_node current_entry = return_list;
|
||||
|
||||
int i = 0;
|
||||
struct ftdi_device_list *curdev = NULL;
|
||||
for (curdev = devlist; curdev != NULL; curdev = curdev->next, i++) {
|
||||
rc = ftdi_usb_get_strings(&ftdic, curdev->dev,
|
||||
current_entry->manufacturer, sizeof(current_entry->manufacturer),
|
||||
@@ -249,50 +250,46 @@ bool initializeUSB(struct ftdi_context *ftdic, char **message, char *serial) {
|
||||
struct ftdi_device_list *devlist;
|
||||
|
||||
// search devices
|
||||
int rc;
|
||||
if ((rc = ftdi_usb_find_all(ftdic, &devlist, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID)) < 0) {
|
||||
int rc = ftdi_usb_find_all(ftdic, &devlist, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID);
|
||||
if (rc < 0) {
|
||||
*message = "Can't find Infinite Noise Multiplier";
|
||||
return false;
|
||||
}
|
||||
|
||||
// only one found, or no serial given
|
||||
if (rc >= 0) {
|
||||
if (serial == NULL) {
|
||||
// more than one found AND no serial given
|
||||
if (rc >= 2) {
|
||||
*message = "Multiple Infnoise TRNGs found and serial not specified, using the first one!";
|
||||
}
|
||||
if (ftdi_usb_open(ftdic, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID) < 0) {
|
||||
if (!isSuperUser()) {
|
||||
*message = "Can't open Infinite Noise Multiplier. Try running as super user?";
|
||||
} else {
|
||||
if (serial == NULL) {
|
||||
// more than one found AND no serial given
|
||||
if (rc >= 2) {
|
||||
*message = "Multiple Infnoise TRNGs found and serial not specified, using the first one!";
|
||||
}
|
||||
if (ftdi_usb_open(ftdic, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID) < 0) {
|
||||
if (!isSuperUser()) {
|
||||
*message = "Can't open Infinite Noise Multiplier. Try running as super user?";
|
||||
} else {
|
||||
#ifdef LINUX
|
||||
*message = "Can't open Infinite Noise Multiplier.";
|
||||
*message = "Can't open Infinite Noise Multiplier.";
|
||||
#endif
|
||||
#if defined(__APPLE__)
|
||||
|
||||
*message = "Can't open Infinite Noise Multiplier. sudo kextunload -b com.FTDI.driver.FTDIUSBSerialDriver ? sudo kextunload -b com.apple.driver.AppleUSBFTDI ?";
|
||||
*message = "Can't open Infinite Noise Multiplier. sudo kextunload -b com.FTDI.driver.FTDIUSBSerialDriver ? sudo kextunload -b com.apple.driver.AppleUSBFTDI ?";
|
||||
#endif
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// serial specified
|
||||
rc = ftdi_usb_open_desc(ftdic, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID, NULL, serial);
|
||||
if (rc < 0) {
|
||||
if (!isSuperUser()) {
|
||||
*message = "Can't find Infinite Noise Multiplier. Try running as super user?";
|
||||
} else {
|
||||
*message = "Can't find Infinite Noise Multiplier with given serial";
|
||||
}
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// serial specified
|
||||
if (ftdi_usb_open_desc(ftdic, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID, NULL, serial) < 0) {
|
||||
if (!isSuperUser()) {
|
||||
*message = "Can't find Infinite Noise Multiplier. Try running as super user?";
|
||||
} else {
|
||||
*message = "Can't find Infinite Noise Multiplier with given serial";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Set high baud rate
|
||||
rc = ftdi_set_baudrate(ftdic, 30000);
|
||||
switch (rc) {
|
||||
switch (ftdi_set_baudrate(ftdic, 30000)) {
|
||||
case -1:
|
||||
*message = "Invalid baud rate";
|
||||
return false;
|
||||
@@ -305,8 +302,8 @@ bool initializeUSB(struct ftdi_context *ftdic, char **message, char *serial) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
rc = ftdi_set_bitmode(ftdic, MASK, BITMODE_SYNCBB);
|
||||
switch (rc) {
|
||||
|
||||
switch (ftdi_set_bitmode(ftdic, MASK, BITMODE_SYNCBB)) {
|
||||
case -1:
|
||||
*message = "Can't enable bit-bang mode";
|
||||
return false;
|
||||
@@ -354,13 +351,13 @@ uint32_t readData(struct infnoise_context *context, uint8_t *result, bool raw, u
|
||||
clock_gettime(CLOCK_REALTIME, &start);
|
||||
|
||||
// write clock signal
|
||||
if (ftdi_write_data(&context->ftdic, outBuf, BUFLEN) != BUFLEN) {
|
||||
if (ftdi_write_data(&context->ftdic, outBuf, sizeof(outBuf)) != sizeof(outBuf)) {
|
||||
context->message = "USB write failed";
|
||||
context->errorFlag = true;
|
||||
}
|
||||
|
||||
// and read 512 byte from the internal buffer (in synchronous bitbang mode)
|
||||
if (ftdi_read_data(&context->ftdic, inBuf, BUFLEN) != BUFLEN) {
|
||||
if (ftdi_read_data(&context->ftdic, inBuf, sizeof(inBuf)) != sizeof(inBuf)) {
|
||||
context->message = "USB read failed";
|
||||
context->errorFlag = true;
|
||||
return 0;
|
||||
@@ -372,7 +369,7 @@ uint32_t readData(struct infnoise_context *context, uint8_t *result, bool raw, u
|
||||
|
||||
if (us <= MAX_MICROSEC_FOR_SAMPLES) {
|
||||
uint8_t bytes[BUFLEN / 8u];
|
||||
context->entropyThisTime = extractBytes(bytes, inBuf, &context->message, &context->errorFlag);
|
||||
context->entropyThisTime = extractBytes(bytes, sizeof(bytes), inBuf, &context->message, &context->errorFlag);
|
||||
if (context->errorFlag) {
|
||||
// todo: message?
|
||||
return 0;
|
||||
|
||||
@@ -56,9 +56,9 @@ void prepareOutputBuffer();
|
||||
|
||||
struct timespec;
|
||||
double diffTime(struct timespec *start, struct timespec *end);
|
||||
uint32_t extractBytes(uint8_t *bytes, uint8_t *inBuf, char **message, bool *errorFlag);
|
||||
uint32_t extractBytes(uint8_t *bytes, uint32_t length, uint8_t *inBuf, char **message, bool *errorFlag);
|
||||
|
||||
bool outputBytes(uint8_t *bytes, uint32_t length, uint32_t entropy, bool writeDevRandom, char **message);
|
||||
|
||||
uint32_t processBytes(uint8_t *bytes, uint8_t *result, uint32_t *entropy, uint32_t *numBits, uint32_t *bytesWritten, bool raw,
|
||||
uint32_t outputMultiplier);
|
||||
uint32_t outputMultiplier);
|
||||
|
||||
Reference in New Issue
Block a user