Minor tweaks to make OSX and FreeBSD and other BSD variants work.

This commit is contained in:
Dirk-Willem van Gulik
2018-07-11 13:04:13 +02:00
parent fcb1fe0a1f
commit 20fb5173a9
7 changed files with 131 additions and 16 deletions

65
software/Makefile.freebsd Normal file
View File

@@ -0,0 +1,65 @@
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
# Against: libftdi1 from ports/pkg
#
FTDILOCI = /usr/local/include/libftdi1
FTDILOCL = /usr/local/lib
FTDI= -lftdi1
CFLAGS = -Wall -Wextra -Werror -std=c99 -O3 -fPIC -I Keccak -I $(FTDILOCI) \
-DGIT_VERSION=\"$(GIT_VERSION)\"\
-DGIT_COMMIT=\"$(GIT_COMMIT)\"\
-DGIT_DATE=\"$(GIT_DATE)\"\
RM=rm
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. -L $(FTDILOCL)
%.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 -L $(FTDILOCL) -Wl $(FTDI) -lm -shared
libs: libinfnoise.a
clean:
$(RM) -f 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

@@ -3,23 +3,24 @@ 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) \
# Against 'libftdi0' from MacOS X ports or brew
#
FTDILOCI = /opt/local/include
FTDILOCL = /opt/local/lib
FTDI= -lftdi
CFLAGS = -Wall -Wextra -Werror -std=c99 -O3 -fPIC -I Keccak -I $(FTDILOCI) \
-DGIT_VERSION=\"$(GIT_VERSION)\"\
-DGIT_COMMIT=\"$(GIT_COMMIT)\"\
-DGIT_DATE=\"$(GIT_DATE)\"\
-DMACOS
FTDI= -lftdi1
RM=rm
all: libinfnoise.a libinfnoise.so infnoise
clean:
rm -f *.o *.a infnoise
infnoise: libinfnoise.a infnoise.o daemon.o
$(CC) $(CFLAGS) -o infnoise infnoise.o daemon.o libinfnoise.a $(FTDI) -lm -L. -linfnoise
$(CC) $(CFLAGS) -o infnoise infnoise.o daemon.o libinfnoise.a $(FTDI) -lm -L. -L $(FTDILOCL) -linfnoise
%.o: %.c infnoise.h libinfnoise.h
$(CC) -c -o $@ $< $(CFLAGS)
@@ -38,12 +39,12 @@ libinfnoise.a: libinfnoise.o healthcheck.o KeccakF-1600-reference.o
# 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
$(CC) $(CFLAGS) -fvisibility=hidden -o libinfnoise.so libinfnoise.o healthcheck.o KeccakF-1600-reference.o -L $(FTDILOCL) -Wl $(FTDI) -lm -shared
libs: libinfnoise.a
clean:
$(RM) infnoise *.o *.a *.gch *.so libinfnoise-example
$(RM) -f infnoise *.o *.a *.gch *.so libinfnoise-example
install-lib: libinfnoise.so
install -d $(PREFIX)/include

View File

@@ -8,6 +8,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
#include <fcntl.h>
#endif
#include <string.h>
#include <time.h>
#include <sys/types.h>
@@ -191,7 +194,15 @@ int main(int argc, char **argv) {
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 :-/)
#endif
#ifdef MACOS
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
int devRandomFD = open("/dev/random", O_WRONLY);
if(devRandomFD < 0) {
fprintf(stderr, "Unable to open /dev/random\n");
exit(1);
}
close(devRandomFD);
#endif
#if defined(__APPLE__)
message = "dev/random not supported on macOS";
return 0;
#endif
@@ -202,7 +213,7 @@ int main(int argc, char **argv) {
// initialize USB device, health check and Keccak state (see libinfnoise)
if (!initInfnoise(&ftdic, opts.serial, &message, !opts.raw, opts.debug)) {
fputs(message, stderr);
fprintf(stderr, "Error: %s\n", message);
return 1; // ERROR
}

View File

@@ -1,7 +1,11 @@
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) || defined(__FreeBSD__)
#include <limits.h>
#else
#include <linux/limits.h>
#endif
#include <ftdi.h>
// Structure for parsed command line options

View File

@@ -19,6 +19,10 @@
#include "libinfnoise.h"
#include "KeccakF-1600-interface.h"
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
#include <fcntl.h>
#endif
uint8_t keccakState[KeccakPermutationSizeInBytes];
bool initInfnoise(struct ftdi_context *ftdic,char *serial, char **message, bool keccak, bool debug) {
prepareOutputBuffer();
@@ -108,13 +112,34 @@ double diffTime(struct timespec *start, struct timespec *end) {
// Write the bytes to either stdout, or /dev/random.
bool outputBytes(uint8_t *bytes, uint32_t length, uint32_t entropy, bool writeDevRandom, char **message) {
if(!writeDevRandom) {
if(!writeDevRandom)
{
if(fwrite(bytes, 1, length, stdout) != length) {
*message = "Unable to write output from Infinite Noise Multiplier";
return false;
}
} else {
#ifdef MACOS
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
// quell compiler warning about unused variable.
static int devRandomFD = -1;
(void)entropy;
if (devRandomFD < 0)
devRandomFD = open("/dev/random",O_WRONLY);
if (devRandomFD < 0) {
*message = "Unable to open random(4)";
return false;
};
// we are not trapping EINT and EAGAIN; as the random(4) driver seems
// to not treat partial writes as not an error. So we think that comparing
// to length is fine.
//
if (write(devRandomFD, bytes, length) != length) {
*message = "Unable to write output from Infinite Noise Multiplier to random(4)";
return false;
}
#endif
#if defined(__APPLE__)
*message = "macOS doesn't support writes to entropy pool";
entropy = 0; // suppress warning
return false;
@@ -295,8 +320,9 @@ bool initializeUSB(struct ftdi_context *ftdic, char **message, char *serial) {
#ifdef LINUX
*message = "Can't open Infinite Noise Multiplier.";
#endif
#ifdef MACOS
*message = "Can't open Infinite Noise Multiplier. sudo kextunload -b com.FTDI.driver.FTDIUSBSerialDriver ?";
#if defined(__APPLE__)
*message = "Can't open Infinite Noise Multiplier. sudo kextunload -b com.FTDI.driver.FTDIUSBSerialDriver ? sudo kextunload -b com.apple.driver.AppleUSBFTDI ?";
#endif
}
return false;

View File

@@ -1,7 +1,11 @@
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) || defined(__FreeBSD__)
#include <limits.h>
#else
#include <linux/limits.h>
#endif
#include <ftdi.h>
#include <time.h>

View File

@@ -1,7 +1,11 @@
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) || defined(__FreeBSD__)
#include <limits.h>
#else
#include <linux/limits.h>
#endif
#include <ftdi.h>
#include <time.h>