-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infnoise.c
522 lines (481 loc) · 18.1 KB
/
infnoise.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/* Driver for the Infinite Noise Multiplier USB stick */
// Required to include clock_gettime
#define _POSIX_C_SOURCE 200809L
#define INFNOISE_VENDOR_ID 0x0403
#define INFNOISE_PRODUCT_ID 0x6015
#include "infnoise.h"
#include "KeccakF-1600-interface.h"
#include "ftdi.h"
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
// Extract the INM output from the data received. Basically, either COMP1 or
// COMP2 changes, not both, so alternate reading bits from them. We get 1 INM
// bit of output per byte read. Feed bits from the INM to the health checker.
// Return the expected bits of entropy.
static uint32_t extractBytes(uint8_t *bytes, uint8_t *inBuf) {
inmClearEntropyLevel();
uint32_t i;
for (i = 0u; i < BUFLEN / 8u; i++) {
uint32_t j;
uint8_t byte = 0u;
for (j = 0u; j < 8u; j++) {
uint8_t val = inBuf[i * 8u + j];
uint8_t evenBit = (val >> COMP2) & 1u;
uint8_t oddBit = (val >> COMP1) & 1u;
bool even = j & 1u; // Use the even bit if j is odd
uint8_t bit = even ? evenBit : oddBit;
byte = (byte << 1u) | bit;
// This is a good place to feed the bit from the INM to the health
// checker.
if (!inmHealthCheckAddBit(evenBit, oddBit, even)) {
fputs("Health check of Infinite Noise Multiplier failed!\n",
stderr);
exit(1);
}
}
bytes[i] = byte;
}
return inmGetEntropyLevel();
}
// Write the bytes to either stdout, or /dev/random.
static void outputBytes(uint8_t *bytes, uint32_t length, uint32_t entropy,
struct opt_struct *opts) {
if (!opts->devRandom) {
if (fwrite(bytes, 1, length, stdout) != length) {
fputs("Unable to write output from Infinite Noise Multiplier\n",
stderr);
exit(1);
}
} else {
inmWaitForPoolToHaveRoom();
inmWriteEntropyToPool(bytes, length, entropy);
}
}
// Whiten the output, if requested, with a Keccak sponge. Output bytes only if
// the health checker says it's OK. Using outputMultiplier > 1 is a nice way to
// generate a lot more cryptographically secure pseudo-random data than the INM
// generates. If outputMultiplier is 0, we output only as many bits as we
// measure in entropy. This allows a user to generate hundreds of MiB per second
// if needed, for use as cryptogrpahic keys.
static uint32_t processBytes(uint8_t *keccakState, uint8_t *bytes,
uint32_t entropy, struct opt_struct *opts) {
// Use the lower of the measured entropy and the provable lower bound on
// average entropy.
if (entropy > inmExpectedEntropyPerBit * BUFLEN / INM_ACCURACY) {
entropy = inmExpectedEntropyPerBit * BUFLEN / INM_ACCURACY;
}
if (opts->raw) {
// In raw mode, we just output raw data from the INM.
outputBytes(bytes, BUFLEN / 8u, entropy, opts);
return BUFLEN / 8u;
}
// Note that BUFLEN has to be less than 1600 by enough to make the sponge
// secure, since outputing all 1600 bits would tell an attacker the Keccak
// state, allowing him to predict any further output, when outputMultiplier
// > 1, until the next call to processBytes. All 512 bits are absorbed
// before sqeezing data out to insure that we instantly recover (reseed)
// from a state compromise, which is when an attacker gets a snapshot of the
// keccak state. BUFLEN must be a multiple of 64, since Keccak-1600 uses
// 64-bit "lanes".
KeccakAbsorb(keccakState, bytes, BUFLEN / 64u);
uint8_t dataOut[16u * 8u];
if (opts->outputMultiplier == 0u) {
// Output all the bytes of entropy we have
KeccakExtract(keccakState, dataOut, (entropy + 63u) / 64u);
outputBytes(dataOut, entropy / 8u, entropy & 0x7u, opts);
return entropy / 8u;
}
// Output 256*outputMultipler bytes.
uint32_t numBits = opts->outputMultiplier * 256u;
uint32_t bytesWritten = 0u;
while (numBits > 0u) {
// Write up to 1024 bits at a time.
uint32_t bytesToWrite = 1024u / 8u;
if (bytesToWrite > numBits / 8u) {
bytesToWrite = numBits / 8u;
}
KeccakExtract(keccakState, dataOut, bytesToWrite / 8u);
uint32_t entropyThisTime = entropy;
if (entropyThisTime > 8u * bytesToWrite) {
entropyThisTime = 8u * bytesToWrite;
}
outputBytes(dataOut, bytesToWrite, entropyThisTime, opts);
bytesWritten += bytesToWrite;
numBits -= bytesToWrite * 8u;
entropy -= entropyThisTime;
if (numBits > 0u) {
KeccakPermutation(keccakState);
}
}
if (bytesWritten != opts->outputMultiplier * (256u / 8u)) {
fprintf(stderr, "Internal error outputing bytes\n");
exit(1);
}
return bytesWritten;
}
// Return a list of all infinite noise multipliers found.
static bool listUSBDevices(struct ftdi_context *ftdic) {
ftdi_init(ftdic);
struct ftdi_device_list *devlist;
struct ftdi_device_list *curdev;
char manufacturer[128], description[128], serial[128];
int i = 0;
// search devices
int rc = ftdi_usb_find_all(ftdic, &devlist, INFNOISE_VENDOR_ID,
INFNOISE_PRODUCT_ID);
if (rc < 0) {
if (!isSuperUser()) {
fprintf(stderr, "Can't find Infinite Noise Multiplier. Try "
"running as super user?\n");
} else {
fprintf(stderr, "Can't find Infinite Noise Multiplier\n");
}
}
for (curdev = devlist; curdev != NULL; i++) {
printf("Device: %d, ", i);
rc = ftdi_usb_get_strings(ftdic, curdev->dev, manufacturer, 128,
description, 128, serial, 128);
if (rc < 0) {
if (!isSuperUser()) {
fprintf(stderr, "Can't find Infinite Noise Multiplier. Try "
"running as super user?\n");
}
fprintf(stderr, "ftdi_usb_get_strings failed: %d (%s)\n", rc,
ftdi_get_error_string(ftdic));
return false;
}
printf("Manufacturer: %s, Description: %s, Serial: %s\n", manufacturer,
description, serial);
curdev = curdev->next;
}
return true;
}
// Initialize the Infinite Noise Multiplier USB interface.
static bool initializeUSB(struct ftdi_context *ftdic, char **message,
char *serial) {
ftdi_init(ftdic);
struct ftdi_device_list *devlist;
// search devices
int rc = 0;
if ((rc = ftdi_usb_find_all(ftdic, &devlist, INFNOISE_VENDOR_ID,
INFNOISE_PRODUCT_ID)) < 0) {
*message = "Can't find Infinite Noise Multiplier\n";
return false;
}
// only one found, or no serial given
if (rc >= 0) {
if (serial == NULL) {
// more than one found AND no serial given
if (rc >= 2) {
fprintf(stderr, "Multiple Infnoise TRNGs found and serial not "
"specified, using the first one!\n");
}
if (ftdi_usb_open(ftdic, INFNOISE_VENDOR_ID, INFNOISE_PRODUCT_ID) <
0) {
if (!isSuperUser()) {
*message = "Can't open Infinite Noise Multiplier. Try "
"running as super user?\n";
} else {
*message = "Can't open Infinite Noise Multiplier\n";
}
return false;
}
} else {
// serial specified
rc = ftdi_usb_open_desc(ftdic, INFNOISE_VENDOR_ID,
INFNOISE_PRODUCT_ID, NULL, serial);
if (rc < 0) {
if (!isSuperUser()) {
*message = "Can't find Infinite Noise Multiplier. Try "
"running as super user?\n";
} else {
*message = "Can't find Infinite Noise Multiplier with "
"given serial\n";
}
return false;
}
}
}
// Set high baud rate
rc = ftdi_set_baudrate(ftdic, 30000);
if (rc == -1) {
*message = "Invalid baud rate\n";
return false;
} else if (rc == -2) {
*message = "Setting baud rate failed\n";
return false;
} else if (rc == -3) {
*message = "Infinite Noise Multiplier unavailable\n";
return false;
}
rc = ftdi_set_bitmode(ftdic, MASK, BITMODE_SYNCBB);
if (rc == -1) {
*message = "Can't enable bit-bang mode\n";
return false;
} else if (rc == -2) {
*message = "Infinite Noise Multiplier unavailable\n";
return false;
}
// Just test to see that we can write and read.
uint8_t buf[64u] = {
0u,
};
if (ftdi_write_data(ftdic, buf, 64) != 64) {
*message = "USB write failed\n";
return false;
}
if (ftdi_read_data(ftdic, buf, 64) != 64) {
*message = "USB read failed\n";
return false;
}
return true;
}
static void initOpts(struct opt_struct *opts) {
opts->outputMultiplier = 0u;
opts->daemon = opts->debug = opts->devRandom = opts->noOutput =
opts->listDevices = opts->raw = false;
opts->version = false;
opts->help = false;
opts->none = false;
opts->pidFileName = opts->serial = NULL;
}
// Return the differnece in the times as a double in microseconds.
static double diffTime(struct timespec *start, struct timespec *end) {
uint32_t seconds = end->tv_sec - start->tv_sec;
int32_t nanoseconds = end->tv_nsec - start->tv_nsec;
return seconds * 1.0e6 + nanoseconds / 1000.0;
}
// getopt_long(3) options descriptor
static struct option longopts[] = {{"raw", no_argument, NULL, 'r'},
{"debug", no_argument, NULL, 'D'},
{"dev-random", no_argument, NULL, 'R'},
{"no-output", no_argument, NULL, 'n'},
{"multiplier", required_argument, NULL, 'm'},
{"pidfile", required_argument, NULL, 'p'},
{"serial", required_argument, NULL, 's'},
{"wait-time", required_argument, NULL, 'w'},
{"daemon", no_argument, NULL, 'd'},
{"list-devices", no_argument, NULL, 'l'},
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}};
int main(int argc, char **argv) {
struct ftdi_context ftdic;
struct opt_struct opts;
int ch;
bool multiplierAssigned = false;
bool waitTimeAssigned = false;
initOpts(&opts);
// Process arguments
while ((ch = getopt_long(argc, argv, "rDRnm:p:s:w:dlvh", longopts, NULL)) !=
-1) {
switch (ch) {
case 'r':
opts.raw = true;
break;
case 'D':
opts.debug = true;
break;
case 'R':
opts.devRandom = true;
break;
case 'n':
opts.noOutput = true;
break;
case 'm':
multiplierAssigned = true;
int tmpOutputMult = atoi(optarg);
if (tmpOutputMult < 0) {
fputs("Multiplier must be >= 0\n", stderr);
return 1;
}
opts.outputMultiplier = tmpOutputMult;
break;
case 'p':
opts.pidFileName = optarg;
if (opts.pidFileName == NULL || !strcmp("", opts.pidFileName)) {
fputs("--pidfile without file name\n", stderr);
return 1;
}
break;
case 's':
opts.serial = optarg;
if (opts.serial == NULL || !strcmp("", opts.serial)) {
fputs("--serial without value\n", stderr);
return 1;
}
break;
case 'w':
waitTimeAssigned = true;
int tmpWaitTime = atoi(optarg);
if ((tmpWaitTime < 1) && (tmpWaitTime > 5000)) {
fputs("Wait time must be in the range between 1 to 5000 "
"microseconds\n",
stderr);
return 1;
}
opts.waitTime = tmpWaitTime;
break;
case 'd':
opts.daemon = true;
break;
case 'l':
opts.listDevices = true;
break;
case 'v':
opts.version = true;
break;
case 'h':
opts.help = true;
break;
default:
opts.help = true;
opts.none = true;
}
}
if (opts.help) {
fputs("Usage: infnoise [options]\n"
"Options are:\n"
" -D, --debug - turn on some debug output\n"
" -R, --dev-random - write entropy to /dev/random instead of "
"stdout\n"
" -r, --raw - do not whiten the output\n"
" -m, --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"
" -n, --no-output - do not write random output data\n"
" -p, --pidfile <file> - write process ID to file\n"
" -d, --daemon - run in the background\n"
" -s, --serial <serial> - use specified device\n"
" -w, --wait-time <microseconds> - set wait time per each USB "
"polling (1~5000)\n"
" -l, --list-devices - list available devices\n"
" -v, --version - show version information\n"
" -h, --help - this help output\n",
stdout);
if (opts.none) {
return 1;
} else {
return 0;
}
}
// read environment variables, not overriding command line options
if (opts.serial == NULL) {
if (getenv("INFNOISE_SERIAL") != NULL) {
opts.serial = getenv("INFNOISE_SERIAL");
}
}
if (opts.debug == false) {
if (getenv("INFNOISE_DEBUG") != NULL) {
if (!strcmp("true", getenv("INFNOISE_DEBUG"))) {
opts.debug = true;
}
}
}
if (multiplierAssigned == false) {
if (getenv("INFNOISE_MULTIPLIER") != NULL) {
int tmpOutputMult = atoi(getenv("INFNOISE_MULTIPLIER"));
if (tmpOutputMult < 0) {
fputs("Multiplier must be >= 0\n", stderr);
return 1;
}
multiplierAssigned = true;
opts.outputMultiplier = tmpOutputMult;
}
}
if (!multiplierAssigned && opts.devRandom) {
opts.outputMultiplier = 2u; // Don't throw away entropy when writing to
// /dev/random unless told to do so
}
if (waitTimeAssigned == false) {
opts.waitTime = 0;
waitTimeAssigned = true;
}
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;
}
if (opts.listDevices) {
listUSBDevices(&ftdic);
return 0;
}
// Optionally run in the background and optionally write a PID-file
startDaemon(&opts);
if (opts.devRandom) {
inmWriteEntropyStart(BUFLEN / 8u, &opts);
}
if (!inmHealthCheckStart(PREDICTION_BITS, DESIGN_K, &opts)) {
fputs("Can't intialize health checker\n", stderr);
return 1;
}
KeccakInitialize();
uint8_t keccakState[KeccakPermutationSizeInBytes];
KeccakInitializeState(keccakState);
char *message;
if (!initializeUSB(&ftdic, &message, opts.serial)) {
// Sometimes have to do it twice - not sure why
if (!initializeUSB(&ftdic, &message, opts.serial)) {
fputs(message, stderr);
return 1;
}
}
// Endless loop: set SW1EN and SW2EN alternately
uint32_t i;
uint8_t outBuf[BUFLEN], inBuf[BUFLEN];
for (i = 0u; i < BUFLEN; i++) {
// Alternate Ph1 and Ph2
outBuf[i] = i & 1 ? (1 << SWEN2) : (1 << SWEN1);
}
struct timespec wait;
wait.tv_sec = 0;
wait.tv_nsec = (long)opts.waitTime * 1000L;
bool waitEnabled = (wait.tv_nsec > 0);
uint32_t maxTimeForSamples = MAX_MICROSEC_FOR_SAMPLES + opts.waitTime;
uint64_t totalBytesWritten = 0u;
while (true) {
struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
if (ftdi_write_data(&ftdic, outBuf, BUFLEN) != BUFLEN) {
fputs("USB write failed\n", stderr);
return 1;
}
if (ftdi_read_data(&ftdic, inBuf, BUFLEN) != BUFLEN) {
fputs("USB read failed\n", stderr);
return 1;
}
if (waitEnabled) {
nanosleep(&wait, NULL);
}
struct timespec end;
clock_gettime(CLOCK_REALTIME, &end);
uint32_t us = diffTime(&start, &end);
// fprintf(stderr, "us = %u\n", us);
if (us <= maxTimeForSamples) {
uint8_t bytes[BUFLEN / 8u];
uint32_t entropy = extractBytes(bytes, inBuf);
if (!opts.noOutput && inmHealthCheckOkToUseData() &&
inmEntropyOnTarget(entropy, BUFLEN)) {
uint64_t prevTotalBytesWritten = totalBytesWritten;
totalBytesWritten +=
processBytes(keccakState, bytes, entropy, &opts);
if (opts.debug &&
(1u << 20u) * (totalBytesWritten / (1u << 20u)) >
(1u << 20u) * (prevTotalBytesWritten / (1u << 20u))) {
fprintf(stderr, "Output %lu bytes\n",
(unsigned long)totalBytesWritten);
}
}
}
}
return 0;
}