3
#include "knuth-lfib.h"
13
/* Lagged fibonacci sequence as described in Knuth 3.6 */
15
struct knuth_lfib_ctx lfib;
18
get_event(FILE *f, struct sha256_ctx *hash,
19
unsigned *key, unsigned *time)
29
sha256_update(hash, sizeof(buf), buf);
33
t += (knuth_lfib_get(&lfib) % 10000);
40
open_file(const char *name)
42
/* Tries opening the file in $srcdir, if set, otherwise the current
43
* working directory */
45
const char *srcdir = getenv("srcdir");
46
if (srcdir && srcdir[0])
48
/* Leaks this name, but that doesn't matter. */
49
char *buf = xalloc(strlen(name) + strlen(srcdir) + 10);
50
sprintf(buf, "%s/%s", srcdir, name);
54
/* Opens the file in text mode. */
55
return fopen(name, "r");
63
struct yarrow256_ctx yarrow;
64
struct yarrow_key_event_ctx estimator;
66
struct yarrow_source sources[2];
68
struct sha256_ctx output_hash;
69
struct sha256_ctx input_hash;
70
uint8_t digest[SHA256_DIGEST_SIZE];
72
const uint8_t *expected_output
73
= decode_hex_dup("06ca66b204a92939 e75e09e11922153e"
74
"a2391000e0686da4 c7d27afb37a4630f");
76
const uint8_t *expected_input
77
= decode_hex_dup("fec4c0767434a8a3 22d6d5d0c9f49c42"
78
"988ce8c159b1a806 29d51aa40c2e99aa");
80
const uint8_t *expected_seed_file
81
= decode_hex_dup("87213a8a863a91f9 0e776c01e0d7c3a8"
82
"6b2ecf9977b06da5 34f3df8375918ac9");
84
unsigned c; unsigned t;
86
unsigned processed = 0;
91
static const char zeroes[100];
93
yarrow256_init(&yarrow, 2, sources);
94
memset(&yarrow.seed_file, 0, sizeof(yarrow.seed_file));
96
yarrow_key_event_init(&estimator);
97
sha256_init(&input_hash);
98
sha256_init(&output_hash);
100
knuth_lfib_init(&lfib, 31416);
102
/* Fake input to source 0 */
103
yarrow256_update(&yarrow, 0, 200, sizeof(zeroes), zeroes);
106
printf("source 0 entropy: %d\n",
107
sources[0].estimate[YARROW_SLOW]);
109
assert(!yarrow256_is_seeded(&yarrow));
111
input = open_file("rfc1750.txt");
115
fprintf(stderr, "Couldn't open `rfc1750.txt', errno = %d\n",
120
while (get_event(input, &input_hash, &c, &t))
126
WRITE_UINT32(buf, c);
127
WRITE_UINT32(buf + 4, t);
128
yarrow256_update(&yarrow, 1,
129
yarrow_key_event_estimate(&estimator, c, t),
132
if (yarrow256_is_seeded(&yarrow))
134
static const unsigned sizes[4] = { 1, 16, 500, 37 };
135
unsigned size = sizes[processed % 4];
139
if (verbose && !output)
140
printf("Generator was seeded after %d events\n",
143
yarrow256_random(&yarrow, size, buf);
145
sha256_update(&output_hash, size, buf);
149
printf("%02x ", buf[0]);
150
if (! (processed % 16))
161
for (i = 0; i<2; i++)
162
printf("source %d, (fast, slow) entropy: (%d, %d)\n",
164
sources[i].estimate[YARROW_FAST],
165
sources[i].estimate[YARROW_SLOW]);
167
printf("Processed input: %d octets\n", processed);
171
sha256_digest(&input_hash, sizeof(digest), digest);
175
print_hex(sizeof(digest), digest);
179
if (memcmp(digest, expected_input, sizeof(digest)))
181
fprintf(stderr, "Failed.\n");
187
printf("New seed file: ");
188
print_hex(sizeof(yarrow.seed_file), yarrow.seed_file);
192
if (memcmp(yarrow.seed_file, expected_seed_file, sizeof(yarrow.seed_file)))
194
fprintf(stderr, "Failed.\n");
200
printf("Generated output: %d octets\n", output);
204
sha256_digest(&output_hash, sizeof(digest), digest);
208
print_hex(sizeof(digest), digest);
212
if (memcmp(digest, expected_output, sizeof(digest)))
214
fprintf(stderr, "Failed.\n");