Minor changes to get it working on macOS

This commit is contained in:
Dylan McNamee
2018-07-05 18:18:57 -07:00
parent 80ca79465f
commit b472549e15
4 changed files with 127 additions and 47 deletions

67
software/Makefile.macos Normal file
View File

@@ -0,0 +1,67 @@
GIT_VERSION := $(shell git --no-pager describe --tags --always)
GIT_COMMIT := $(shell git rev-parse --verify HEAD)
GIT_DATE := $(firstword $(shell git --no-pager show --date=iso-strict --format="%ad" --name-only))
PREFIX = $(DESTDIR)/usr/local
FTDILOC = /usr/local/Cellar/libftdi/1.4/include/libftdi1/
CFLAGS = -Wall -Wextra -Werror -std=c99 -O3 -fPIC -I Keccak -I $(FTDILOC) \
-DGIT_VERSION=\"$(GIT_VERSION)\"\
-DGIT_COMMIT=\"$(GIT_COMMIT)\"\
-DGIT_DATE=\"$(GIT_DATE)\"\
-DMACOS
FOUND = $(shell /sbin/ldconfig -p | grep --silent libftdi.so && echo found)
ifeq ($(FOUND), found)
FTDI= -lftdi
else
FTDI= -lftdi1
endif
all: libinfnoise.a libinfnoise.so infnoise
infnoise: libinfnoise.a infnoise.o daemon.o
$(CC) $(CFLAGS) -o infnoise infnoise.o daemon.o libinfnoise.a $(FTDI) -lm -L. -linfnoise
%.o: %.c infnoise.h libinfnoise.h
$(CC) -c -o $@ $< $(CFLAGS)
KeccakF-1600-reference.o: Keccak/KeccakF-1600-reference.c Keccak/KeccakF-1600-interface.h Keccak/brg_endian.h
$(CC) -c -o $@ $< $(CFLAGS)
# static lib compiled into infnoise binary
libinfnoise.o: libinfnoise.c libinfnoise.h libinfnoise_private.h healthcheck.c
$(CC) $(CFLAGS) -c libinfnoise.c
libinfnoise.a: libinfnoise.o healthcheck.o KeccakF-1600-reference.o
ar rcs libinfnoise.a libinfnoise.o healthcheck.o KeccakF-1600-reference.o
ranlib libinfnoise.a
# shared lib
libinfnoise.so: libinfnoise.o healthcheck.o KeccakF-1600-reference.o
$(CC) $(CFLAGS) -fvisibility=hidden -o libinfnoise.so libinfnoise.o healthcheck.o KeccakF-1600-reference.o -Wl $(FTDI) -lm -shared
libs: libinfnoise.a
clean:
$(RM) infnoise *.o *.a *.gch *.so libinfnoise-example
install-lib: libinfnoise.so
install -d $(PREFIX)/include
install -m 0644 libinfnoise.h $(PREFIX)/include
install -d $(PREFIX)/lib
install -m 0644 libinfnoise.so $(PREFIX)/lib
ldconfig $(PREFIX)/lib
install: infnoise
install -d $(PREFIX)/sbin
install -m 0755 infnoise $(PREFIX)/sbin/
install -d $(PREFIX)/lib/udev/rules.d/
install -m 0644 init_scripts/75-infnoise.rules $(PREFIX)/lib/udev/rules.d/
install -d $(PREFIX)/lib/systemd/system
install -m 0644 init_scripts/infnoise.service $(PREFIX)/lib/systemd/system
postinstall:
systemctl restart systemd-udevd
systemctl enable infnoise

View File

@@ -18,18 +18,18 @@
#include "KeccakF-1600-interface.h"
static void initOpts(struct opt_struct *opts) {
opts->outputMultiplier = 0u;
opts->daemon = false;
opts->debug = false;
opts->devRandom = false;
opts->noOutput = false;
opts->listDevices = false;
opts->raw = false;
opts->version = false;
opts->help = false;
opts->none = false;
opts->pidFileName =
opts->serial = NULL;
opts->outputMultiplier = 0u;
opts->daemon = false;
opts->debug = false;
opts->devRandom = false;
opts->noOutput = false;
opts->listDevices = false;
opts->raw = false;
opts->version = false;
opts->help = false;
opts->none = false;
opts->pidFileName =
opts->serial = NULL;
}
int main(int argc, char **argv)
@@ -84,7 +84,7 @@ int main(int argc, char **argv)
opts.help = true;
} else {
opts.help = true;
opts.none = true;
opts.none = true;
}
}
@@ -103,7 +103,7 @@ int main(int argc, char **argv)
" --list-devices - list available devices\n"
" --version - show version information\n"
" --help - this help output\n", stdout);
if (opts.none) {
if (opts.none) {
return 1;
} else {
return 0;
@@ -142,10 +142,10 @@ int main(int argc, char **argv)
}
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;
printf("GIT VERSION - %s\n", GIT_VERSION);
printf("GIT COMMIT - %s\n", GIT_COMMIT);
printf("GIT DATE - %s\n", GIT_DATE);
return 0;
}
char *message = "no data?";
@@ -156,12 +156,18 @@ int main(int argc, char **argv)
return 1;
}
//fputs(message, stdout); // todo: put list of devices to &message and print here, not in libinfnoise
return 0;
return 0;
}
if (opts.devRandom) {
#ifdef LINUX
inmWriteEntropyStart(BUFLEN/8u, opts.debug); // todo: create method in libinfnoise.h for this?
// also todo: check superUser in this mode (it will fail silently if not :-/)
// also todo: check superUser in this mode (it will fail silently if not :-/)
#endif
#ifdef MACOS
message = "dev/random not supported on macOS";
return 0;
#endif
}
// Optionally run in the background and optionally write a PID-file

View File

@@ -89,7 +89,7 @@ uint32_t extractBytes(uint8_t *bytes, uint8_t *inBuf, char **message, bool *erro
// This is a good place to feed the bit from the INM to the health checker.
if(!inmHealthCheckAddBit(evenBit, oddBit, even)) {
*message = "Health check of Infinite Noise Multiplier failed!";
*errorFlag = true;
*errorFlag = true;
return 0;
}
}
@@ -113,8 +113,15 @@ bool outputBytes(uint8_t *bytes, uint32_t length, uint32_t entropy, bool writeD
return false;
}
} else {
#ifdef MACOS
*message = "macOS doesn't support writes to entropy pool";
entropy = 0; // suppress warning
return false;
#endif
#ifdef LINUX
inmWaitForPoolToHaveRoom();
inmWriteEntropyToPool(bytes, length, entropy);
#endif
}
return true;
}
@@ -141,14 +148,14 @@ uint32_t processBytes(uint8_t *bytes, uint8_t *result, uint32_t entropy,
// In raw mode, we just output raw data from the INM.
if (!noOutput) {
if (!outputBytes(bytes, BUFLEN/8u, entropy, writeDevRandom, message)) {
*errorFlag = true;
*errorFlag = true;
return 0; // write failed
}
} else {
if (result != NULL) {
if (result != NULL) {
memcpy(result, bytes, BUFLEN/8u * sizeof(uint8_t));
}
}
}
return BUFLEN/8u;
}
@@ -164,16 +171,16 @@ uint32_t processBytes(uint8_t *bytes, uint8_t *result, uint32_t entropy,
if(outputMultiplier == 0u) {
// Output all the bytes of entropy we have
KeccakExtract(keccakState, dataOut, (entropy + 63u)/64u);
if (!noOutput) {
if (!outputBytes(dataOut, entropy/8u, entropy & 0x7u, writeDevRandom, message)) {
if (!noOutput) {
if (!outputBytes(dataOut, entropy/8u, entropy & 0x7u, writeDevRandom, message)) {
*errorFlag = true;
return 0;
}
} else {
if (result != NULL) {
} else {
if (result != NULL) {
memcpy(result, dataOut, entropy/8u * sizeof(uint8_t));
}
}
}
return entropy/8u;
}
@@ -192,20 +199,20 @@ uint32_t processBytes(uint8_t *bytes, uint8_t *result, uint32_t entropy,
if(entropyThisTime > 8u*bytesToWrite) {
entropyThisTime = 8u*bytesToWrite;
}
if (!noOutput) {
if (!noOutput) {
if (!outputBytes(dataOut, bytesToWrite, entropyThisTime, writeDevRandom, message)) {
*errorFlag = true;
return 0;
}
} else {
} else {
//memcpy(result + bytesWritten, dataOut, bytesToWrite * sizeof(uint8_t)); //doesn't work?
// alternative: loop through dataOut and append array elements to result..
if (result != NULL) {
if (result != NULL) {
for (uint32_t i =0; i < bytesToWrite; i++ ) {
result[bytesWritten + i] = dataOut[i];
}
}
}
}
bytesWritten += bytesToWrite;
numBits -= bytesToWrite*8u;
entropy -= entropyThisTime;
@@ -215,7 +222,7 @@ uint32_t processBytes(uint8_t *bytes, uint8_t *result, uint32_t entropy,
}
if(bytesWritten != outputMultiplier*(256u/8u)) {
*message = "Internal error outputing bytes";
*errorFlag = true;
*errorFlag = true;
return 0;
}
return bytesWritten;
@@ -247,15 +254,15 @@ bool listUSBDevices(struct ftdi_context *ftdic, char** message) {
if (rc < 0) {
if (!isSuperUser()) {
*message = "Can't find Infinite Noise Multiplier. Try running as super user?";
return false;
return false;
}
//*message = "ftdi_usb_get_strings failed: %d (%s)\n", rc, ftdi_get_error_string(ftdic));
return false;
}
return false;
}
// print to stdout
// print to stdout
printf("Manufacturer: %s, Description: %s, Serial: %s", manufacturer, description, serial);
curdev = curdev->next;
curdev = curdev->next;
}
return true;
@@ -275,10 +282,10 @@ bool initializeUSB(struct ftdi_context *ftdic, char **message, char *serial) {
// only one found, or no serial given
if (rc >= 0) {
if (serial == NULL) {
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!";
*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()) {
@@ -287,7 +294,7 @@ bool initializeUSB(struct ftdi_context *ftdic, char **message, char *serial) {
*message = "Can't open Infinite Noise Multiplier";
}
return false;
}
}
} else {
// serial specified
rc = ftdi_usb_open_desc(ftdic, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID, NULL, serial);
@@ -298,7 +305,7 @@ bool initializeUSB(struct ftdi_context *ftdic, char **message, char *serial) {
*message = "Can't find Infinite Noise Multiplier with given serial";
}
return false;
}
}
}
}
@@ -369,10 +376,10 @@ uint32_t readData_private(struct ftdi_context *ftdic, uint8_t *result, char **me
uint8_t bytes[BUFLEN/8u];
uint32_t entropy = extractBytes(bytes, inBuf, message, errorFlag);
// call health check and process bytes if OK
// call health check and process bytes if OK
if (inmHealthCheckOkToUseData() && inmEntropyOnTarget(entropy, BUFLEN)) {
uint32_t byteswritten = processBytes(bytes, result, entropy, raw, devRandom, outputMultiplier, noOutput, message, errorFlag);
return byteswritten;
return byteswritten;
}
}
return 0;
@@ -408,10 +415,10 @@ int main() {
uint64_t bytesWritten = 0u;
bytesWritten = readData(&ftdic, keccakState, result, multiplier);
// check for -1, indicating an error
// check for -1, indicating an error
totalBytesWritten += bytesWritten;
// make sure to only read as many bytes as readData returned. Only those have passed the health check in this round (usually all)
// make sure to only read as many bytes as readData returned. Only those have passed the health check in this round (usually all)
fwrite(result, 1, bytesWritten, stdout);
}
}