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 uint8_t seed_file[YARROW256_SEED_FILE_SIZE];
74 const uint8_t *expected_output
75 = decode_hex_dup("dd304aacac3dc95e 70d684a642967c89"
76 "58501f7c8eb88b79 43b2ffccde6f0f79");
78 const uint8_t *expected_input
79 = decode_hex_dup("e0596cf006025506 65d1195f32a87e4a"
80 "5c354910dfbd0a31 e2105b262f5ce3d8");
82 const uint8_t *expected_seed_file
83 = decode_hex_dup("b03518f32b1084dd 983e6a445d47bb6f"
84 "13bb7b998740d570 503d6aaa62e28901");
86 unsigned c; unsigned t;
88 unsigned processed = 0;
93 static const char zeroes[100];
95 yarrow256_init(&yarrow, 2, sources);
97 yarrow_key_event_init(&estimator);
98 sha256_init(&input_hash);
99 sha256_init(&output_hash);
101 knuth_lfib_init(&lfib, 31416);
103 /* Fake input to source 0 */
104 yarrow256_update(&yarrow, 0, 200, sizeof(zeroes), zeroes);
107 printf("source 0 entropy: %d\n",
108 sources[0].estimate[YARROW_SLOW]);
110 assert(!yarrow256_is_seeded(&yarrow));
112 input = open_file("gold-bug.txt");
116 fprintf(stderr, "Couldn't open `gold-bug.txt', errno = %d\n",
121 while (get_event(input, &input_hash, &c, &t))
127 WRITE_UINT32(buf, c);
128 WRITE_UINT32(buf + 4, t);
129 yarrow256_update(&yarrow, 1,
130 yarrow_key_event_estimate(&estimator, c, t),
133 if (yarrow256_is_seeded(&yarrow))
135 static const unsigned sizes[4] = { 1, 16, 500, 37 };
136 unsigned size = sizes[processed % 4];
140 if (verbose && !output)
141 printf("Generator was seeded after %d events\n",
144 yarrow256_random(&yarrow, size, buf);
146 sha256_update(&output_hash, size, buf);
150 printf("%02x ", buf[0]);
151 if (! (processed % 16))
162 for (i = 0; i<2; i++)
163 printf("source %d, (fast, slow) entropy: (%d, %d)\n",
165 sources[i].estimate[YARROW_FAST],
166 sources[i].estimate[YARROW_SLOW]);
168 printf("Processed input: %d octets\n", processed);
172 sha256_digest(&input_hash, sizeof(digest), digest);
176 print_hex(sizeof(digest), digest);
180 if (memcmp(digest, expected_input, sizeof(digest)))
182 fprintf(stderr, "Failed.\n");
186 yarrow256_random(&yarrow, sizeof(seed_file), seed_file);
189 printf("New seed file: ");
190 print_hex(sizeof(seed_file), seed_file);
194 if (memcmp(seed_file, expected_seed_file, sizeof(seed_file)))
196 fprintf(stderr, "Failed.\n");
202 printf("Generated output: %d octets\n", output);
206 sha256_digest(&output_hash, sizeof(digest), digest);
210 print_hex(sizeof(digest), digest);
214 if (memcmp(digest, expected_output, sizeof(digest)))
216 fprintf(stderr, "Failed.\n");