Skip to content

Commit

Permalink
bats tests for the driver + libinfnoise cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-domke committed Apr 21, 2018
1 parent 8bc29e0 commit 3e04d09
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 48 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "software/tests/test_helper/bats-support"]
path = software/tests/test_helper/bats-support
url = https://github.com/ztombol/bats-support
[submodule "software/tests/test_helper/bats-assert"]
path = software/tests/test_helper/bats-assert
url = https://github.com/ztombol/bats-assert
30 changes: 3 additions & 27 deletions software/examples/README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
# Usage examples for Infinite Noise

## Driver (binary)

### Command-Line

Raw output to file:

infnoise --raw > output.txt

Whitened output to file:

infnoise > output.txt

Whietened, mutliplied output to file:

infnoise --multiplier 10 > output.txt

Debug mode:

infnoise --debug --no-output

/dev/random mode:

infnoise --debug --dev-random (--multiplier)

### Integrate
## Integrate the binary to python

See the python examples `serial-numbers.py` and `randomserver.py` to see how you could integrate it with python.

Expand All @@ -42,9 +18,9 @@ This simple version just prints the serials to stdout. Call like this:

A simple webserver based on the web.py framework to serve random data via a REST interface. An example is hosted at https://rng.13-37.org (running on a Raspberry Pi in Amsterdam, thanks to pcextreme.nl!)

It has only two resources: `/get` and `/status`.
It has only two resources: `/get` and `/status`.

## Library
## libinfnoise

TODO

4 changes: 1 addition & 3 deletions software/infnoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,10 @@ int main(int argc, char **argv)
uint8_t keccakState[KeccakPermutationSizeInBytes];
KeccakInitializeState(keccakState);

uint8_t result[1024]; // only used in noOutput mode (and libinfnoise)

uint64_t totalBytesWritten = 0u;
while(true) {
uint64_t prevTotalBytesWritten = totalBytesWritten;
uint64_t bytesWritten = readData_private(&ftdic, keccakState, result, &message, opts.noOutput, opts.raw, opts.outputMultiplier, opts.devRandom); // calling libinfnoise's private readData method
uint64_t bytesWritten = readData_private(&ftdic, keccakState, NULL, &message, opts.noOutput, opts.raw, opts.outputMultiplier, opts.devRandom); // calling libinfnoise's private readData method

if (totalBytesWritten == (unsigned long)-1) {
fputs(message, stderr);
Expand Down
34 changes: 16 additions & 18 deletions software/libinfnoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes, uint8_t *result, uin
if (!noOutput) {
outputBytes(bytes, BUFLEN/8u, entropy, writeDevRandom);
} else {
memcpy(result, bytes, BUFLEN/8u * sizeof(uint8_t));
//result=bytes;
if (result != NULL) {
memcpy(result, bytes, BUFLEN/8u * sizeof(uint8_t));
}
}
return BUFLEN/8u;
}
Expand All @@ -109,10 +110,12 @@ uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes, uint8_t *result, uin
if (!noOutput) {
outputBytes(dataOut, entropy/8u, entropy & 0x7u, writeDevRandom);
} else {
memcpy(result, dataOut, entropy/8u * sizeof(uint8_t));
if (result != NULL) {
memcpy(result, dataOut, entropy/8u * sizeof(uint8_t));
}
}
return entropy/8u;
} // todo: write to result array
}

// Output 256*outputMultipler bits.
uint32_t numBits = outputMultiplier*256u;
Expand All @@ -132,16 +135,12 @@ uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes, uint8_t *result, uin
if (!noOutput) {
outputBytes(dataOut, bytesToWrite, entropyThisTime, writeDevRandom);
} else {
// append data in result array until we have finished squeezing the keccak sponge
// its important to have an result array of the approriate size: outputMultiplier*32
//fprintf(stderr, "bytes written: %d\n", bytesWritten);
//fprintf(stderr, "bytes to write: %d\n", bytesToWrite);

//memcpy(result + bytesWritten, dataOut, bytesToWrite * sizeof(uint8_t)); //doesn't work
//memcpy(result + bytesWritten, dataOut, bytesToWrite * sizeof(uint8_t)); //doesn't work?
// alternative: loop through dataOut and append array elements to result..
for (uint32_t i =0; i < bytesToWrite; i++ ) {
fprintf(stderr, " result[%d] = dataOut[%d];\n", bytesWritten + i, i);
result[bytesWritten + i] = dataOut[i];
if (result != NULL) {
for (uint32_t i =0; i < bytesToWrite; i++ ) {
result[bytesWritten + i] = dataOut[i];
}
}
}
bytesWritten += bytesToWrite;
Expand All @@ -155,7 +154,6 @@ uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes, uint8_t *result, uin
fprintf(stderr, "Internal error outputing bytes\n");
exit(1);
}
fprintf(stderr, "bytes written: %d\n", bytesWritten);
return bytesWritten;
}

Expand Down Expand Up @@ -364,12 +362,12 @@ int main() {
bool debug = false;

// calculate output size based on the parameters:
// when using the multiplier, we need a result array of max 1024 bytes - otherwise 64(BUFLEN/8) bytes
// when using the multiplier, we need a result array of 32*MULTIPLIER - otherwise 64(BUFLEN/8) bytes
uint32_t resultSize;
if (multiplier == 0 || rawOutput == true) {
resultSize = BUFLEN/8u;
} else {
resultSize = 1024; // optimize?
resultSize = multiplier*32u;
}
fprintf(stderr, "%d\n", resultSize);

Expand All @@ -381,10 +379,10 @@ int main() {
uint64_t bytesWritten = 0u;
bytesWritten = readData(&ftdic, keccakState, result, multiplier);

// check for -1!
// 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 but..)
// 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);
}
}
Expand Down
1 change: 1 addition & 0 deletions software/tests/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bats tests for the binary driver.
87 changes: 87 additions & 0 deletions software/tests/infnoise.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env bats

load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'

# Tests for the infnoise binary

@test "test --raw output for expected entropy" {
# capture some data
TMP_FILE=`mktemp -u $BATS_TMPDIR/infnoise-test-XXXXXXX`
timeout 5s ./infnoise --raw > $TMP_FILE || true

# run ent
run ent $TMP_FILE

assert_line --index 0 --regexp '^Entropy = 7.2[5-9][0-9]+ bits per byte.$'

# cleanup
rm $TMP_FILE
}

@test "test whitened output for expected entropy" {
# capture some data
TMP_FILE=`mktemp -u $BATS_TMPDIR/infnoise-test-XXXXXXX`
timeout 5s ./infnoise > $TMP_FILE || true

# run ent
run ent $TMP_FILE

# check ent's result
assert_line --index 0 --regexp '^Entropy = 7.99[0-9]+ bits per byte.$'

# cleanup
rm $TMP_FILE
}

@test "test whitened output (multiplier=10) for expected entropy" {
# capture some data
TMP_FILE=`mktemp -u $BATS_TMPDIR/infnoise-test-XXXXXXX`
timeout 5s ./infnoise --multiplier 10 > $TMP_FILE || true

# run ent
run ent $TMP_FILE

assert_line --index 0 --regexp '^Entropy = 7.99[0-9]+ bits per byte.$'

# cleanup
rm $TMP_FILE
}

@test "test --no-output --debug" {
# capture some data
TMP_FILE=`mktemp -u $BATS_TMPDIR/infnoise-test-XXXXXXX`
run timeout 5s ./infnoise --no-output --debug

echo $output
[ "$status" -eq 124 ]

assert_line --index 0 --regexp '^Generated 1048576 bits. OK to use data. Estimated entropy per bit: 0\.[0-8][6-8][0-9]+, estimated K: 1\.8[1-5][0-9]+$'
assert_line --index 1 --regexp '^num1s:50.[0-9]+%, even misfires:0.[0-1][0-9]+%, odd misfires:0.[0-1][0-9]+%$'
}

@test "test --list-devices" {
run ./infnoise --list-devices
echo $output
[ "$status" -eq 0 ]

# FTDI serial:
assert_line --index 1 --regexp '^Manufacturer: FTDI, Description: FT240X USB FIFO, Serial: [0-9A-Z]+$'

# 13-37.org serial:
assert_line --index 0 --regexp '^Manufacturer: 13-37.org, Description: Infinite Noise TRNG, Serial: [0-9A-F]+$'
}

@test "test --serial with not connected serial (results in error)" {
run ./infnoise --serial 4711
echo $output
[ "$status" -eq 1 ]
[ "${lines[0]}" = "Can't find Infinite Noise Multiplier. Try running as super user?" ]
}

@test "test --help" {
run ./infnoise --help
echo $output
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Usage: infnoise [options]" ]
}
1 change: 1 addition & 0 deletions software/tests/test_helper/bats-assert
Submodule bats-assert added at 9f88b4
1 change: 1 addition & 0 deletions software/tests/test_helper/bats-support
Submodule bats-support added at 004e70

0 comments on commit 3e04d09

Please sign in to comment.