Applied daemon patch, thanks to Rune Magnussen

This commit is contained in:
Bill Cox
2015-08-18 09:44:00 -07:00
parent dbc4dd0db9
commit d785240e80
7 changed files with 116 additions and 3 deletions

1
software/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
infnoise

View File

@@ -2,8 +2,8 @@ CFLAGS = -Wall -std=c99 -O3 -I Keccak
all: infnoise
infnoise: infnoise.c infnoise.h healthcheck.c writeentropy.c Keccak/KeccakF-1600-reference.c Keccak/brg_endian.h
$(CC) $(CFLAGS) -o infnoise infnoise.c healthcheck.c writeentropy.c Keccak/KeccakF-1600-reference.c -lftdi -lm -lrt
infnoise: infnoise.c infnoise.h healthcheck.c writeentropy.c daemon.c Keccak/KeccakF-1600-reference.c Keccak/brg_endian.h
$(CC) $(CFLAGS) -o infnoise infnoise.c healthcheck.c writeentropy.c daemon.c Keccak/KeccakF-1600-reference.c -lftdi -lm -lrt
clean:
$(RM) -f infnoise

21
software/daemon.c Normal file
View File

@@ -0,0 +1,21 @@
/* Functions used when running in the background
*/
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include "infnoise.h"
int writePid(pid_t pid, char *fileName) {
FILE *pidFile;
int ret;
pidFile = fopen(fileName,"w");
if(pidFile == NULL)
return errno;
ret = fprintf(pidFile, "%d\n", pid);
if (ret < 0)
return ret;
return fclose(pidFile);
}

View File

@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ftdi.h>
#include "infnoise.h"
#include "KeccakF-1600-interface.h"
@@ -265,6 +266,9 @@ int main(int argc, char **argv)
uint32_t outputMultiplier = 0; // We output all the entropy when outputMultiplier == 0
uint32_t xArg;
bool multiplierAssigned = false;
bool pidFile = false;
char *pidFileName = NULL;
bool runDaemon = false;
// Process arguments
for(xArg = 1; xArg < argc; xArg++) {
@@ -285,6 +289,12 @@ int main(int argc, char **argv)
return 1;
}
outputMultiplier = tmpOutputMult;
} else if(!strcmp(argv[xArg], "--pidfile")) {
xArg++;
pidFileName = argv[xArg];
pidFile = true;
} else if(!strcmp(argv[xArg], "--daemon")) {
runDaemon = true;
} else {
fputs("Usage: infnoise [options]\n"
"Options are:\n"
@@ -293,15 +303,43 @@ int main(int argc, char **argv)
" --raw - do not whiten the output\n"
" --multiplier <value> - write 256 bits * value for each 512 bits written to\n"
" the Keccak sponge. Default of 0 means write all the entropy.\n"
" --no-output - do not write random output data\n", stderr);
" --no-output - do not write random output data\n"
" --pidfile <file> - write process ID to file\n"
" --daemon - run in the background\n", stderr);
return 1;
}
}
if(geteuid() != 0) {
fputs("Super user access needed.\n", stderr);
return 1;
}
if(!multiplierAssigned && writeDevRandom) {
outputMultiplier = 2; // Don't throw away entropy when writing to /dev/random unless told to do so
}
/* If the user wants a pid-file, but no backgrounding then write the current
PID. */
if (pidFile && !runDaemon) {
writePid(getpid(), pidFileName);
}
if (runDaemon) {
pid_t pid = fork();
if(pid < 0) {
fputs("fork() failed\n", stderr);
return 1;
} else if(pid > 0) {
/* Parrent */
if(pidFile) {
return writePid(pid, pidFileName);
}
return 0;
}
/* Pid == 0 - Child */
}
if(writeDevRandom) {
inmWriteEntropyStart(BUFLEN/8, debug);
}

View File

@@ -1,5 +1,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
// Required accuracy of estimated vs measured entropy in health monitor
#define INM_ACCURACY 1.03
@@ -17,5 +18,6 @@ void inmWriteEntropyStart(uint32_t bufLen, bool debug);
void inmWriteEntropyToPool(uint8_t *bytes, uint32_t length, uint32_t entropy);
void inmWaitForPoolToHaveRoom(void);
void inmDumpStats(void);
int writePid(pid_t pid, char *fileName);
extern double inmK, inmExpectedEntropyPerBit;

7
software/tools/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
bin2hex
dice
entcheck
findlongest
flipbits
healthcheck
hex2bin

44
software/tools/passgen.c Normal file
View File

@@ -0,0 +1,44 @@
// This is a simple program to read from a binary file of random bits and generate
// dice rolls.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Role a single die.
static uint32_t rollDie(uint32_t sides, FILE *file) {
uint32_t randVal;
do {
uint32_t numBytes = 0;
randVal = 0;
while (1 << (8*numBytes) <= sides) {
numBytes++;
int c = getc(file);
if (c == EOF) {
printf("Ran out of random data\n");
exit(1);
}
randVal = (randVal << 8) | getc(file);
}
} while(randVal >= sides);
return randVal;
}
int main(int argc, char **argv) {
if (argc != 3) {
printf("Usage: passgen randFile numKeys\n");
return 1;
}
FILE *file = fopen(argv[1], "r");
uint32_t keys = atoi(argv[2]);
uint32_t i;
printf("password:");
for (i = 0; i < keys; i++) {
uint32_t randVal = rollDie(26, file);
putchar('a' + randVal);
}
printf("\nThis password has %.2f bits of entropy\n", log(pow(26.0, keys))/log(2));
fclose(file);
return 0;
}