update libinfnoise examples

This commit is contained in:
Manuel Domke
2018-10-04 18:15:25 +02:00
parent 2a5be01ffb
commit 51b9662086
4 changed files with 34 additions and 25 deletions

View File

@@ -16,11 +16,22 @@ This simple version just prints the serials to stdout. Call like this:
#### randomserver.py
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`.
A simple webserver based on the web.py framework to serve random data via a REST interface.
An improved version is hosted on [rng.13-37.org](https://rng.13-37.org).
## libinfnoise
TODO
Under libinfnoise/examples you'll find two examples on how to integrate libinfnoise, which consist of the following functions:
// returns a struct of infnoise_devlist_node listing all connected FTDI FT240 devices by their USB descriptors
devlist_node listUSBDevices(char **message);
// initialize the Infinite Noise TRNG - must be called once before readData() works
bool initInfnoise(struct infnoise_context *context, char *serial, bool keccak, bool debug);
// Reads some bytes from the TRNG and stores them in the "result" byte array.
// The array has to be of sufficient size. Please refer to the example programs.
// (64 byte for normal operation or 128byte for multiplier mode)
uint32_t readData(struct infnoise_context *context, uint8_t *result, bool raw, uint32_t outputMultiplier);
The infnoise_context struct is also part of the interface. See [libinfnoise.h](../libinfnoise.h) for it's definition and the interface documentation.

View File

@@ -18,19 +18,16 @@ int main()
struct infnoise_context context;
if (!initInfnoise(&context, serial, initKeccak, debug)) {
fputs(context.message, stderr);
fprintf(stdout, "Error: %s\n", context.message);
return 1; // ERROR
}
uint32_t resultSize;
if (multiplier <= 1 || initKeccak == false) {
resultSize = 32u;
} else if (multiplier==2) {
resultSize=64;
if (multiplier <= 2 || initKeccak == false) {
resultSize = 64u;
} else {
resultSize = 128u;
}
fprintf(stdout, "Error: %i\n", resultSize);
// read and print in a loop (until 1M is read)
uint64_t totalBytesWritten = 0u;
@@ -47,8 +44,8 @@ int main()
fprintf(stderr, "Error: %s\n", context.message);
return -1;
}
fprintf(stderr, "infnoise bytes read: %lu\n", (unsigned long) bytesWritten);
totalBytesWritten += bytesWritten;
fprintf(stderr, "infnoise bytes read: %lu\n", (unsigned long) totalBytesWritten);
// print as many bytes as readData told us
fwrite(result, 1, bytesWritten, stdout);

View File

@@ -17,15 +17,14 @@ int main()
// initialize hardware and health monitor
struct infnoise_context context;
if (!initInfnoise(&context, serial, initKeccak, debug)) {
fprintf(stdout, "erri: %s\n", "");
fputs(context.message, stderr);
fprintf(stderr, "Error: %s\n", context.message);
return 1; // ERROR
}
// fixed result size of 512 bit (32byte)
uint8_t resultSize = 32u;
// fixed result size of 512 bit (64byte)
uint8_t resultSize = 64u;
// read and print in a loop (until 1M is read)
// read and print in a loop (until 1MB is read)
uint64_t totalBytesWritten = 0u;
while (totalBytesWritten < 1000000) {
uint8_t result[resultSize];
@@ -42,13 +41,14 @@ int main()
fprintf(stderr, "Error: %s\n", context.message);
return -1;
}
fprintf(stderr, "infnoise bytes read: %lu\n", (unsigned long) bytesWritten);
// print as many bytes as readData told us
fwrite(result, 1, bytesWritten, stdout);
// sum up
totalBytesWritten += bytesWritten;
fprintf(stderr, "bytes read: %lu\n", (unsigned long) totalBytesWritten);
}
return 0;
}

View File

@@ -38,7 +38,7 @@ struct infnoise_devlist_node {
typedef struct infnoise_devlist_node *devlist_node;
/*
* returns a struct of infnoise_devlist_node listing all connected FTDI FT240 devices by its USB descriptors,
* returns a struct of infnoise_devlist_node listing all connected FTDI FT240 devices by their USB descriptors
*
* parameters:
* - message: pointer for error message
@@ -47,9 +47,8 @@ typedef struct infnoise_devlist_node *devlist_node;
*/
devlist_node listUSBDevices(char **message);
/*
* initialize the Infinite Noise TRNG - must be called once before readData() works.
* initialize the Infinite Noise TRNG - must be called once before readData() works
*
* parameters:
* - context: pointer to infnoise_context struct
@@ -62,11 +61,13 @@ bool initInfnoise(struct infnoise_context *context, char *serial, bool keccak, b
/*
* Reads some bytes from the TRNG and stores them in the "result" byte array.
* The array has to be of sufficient size. Please refer to the example programs.
* The array has to be of sufficient size. Please refer to the example programs.
* (64 byte for normal operation or 128byte for multiplier mode)
*
* After each read operation, the infnoise_context's errorFlag must be checked,
* and the data from this call has to be discarded!
* Detailed error messages can be found in context->message.
* After every read operation, the infnoise_context's errorFlag must be checked,
* and the data from this call has to be discarded when it returns true!
*
* Detailed error messages can then be found in context->message.
*
* parameters:
* - context: infnoise_context struct with device pointer and state variables
@@ -74,6 +75,6 @@ bool initInfnoise(struct infnoise_context *context, char *serial, bool keccak, b
* - raw: boolean flag for raw or whitened output
* - outputMultiplier: only used for whitened output
*
* returns: number of bytes written to the array
* returns: number of bytes written to the byte-array
*/
uint32_t readData(struct infnoise_context *context, uint8_t *result, bool raw, uint32_t outputMultiplier);