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])
49 char *buf = xalloc(strlen(name) + strlen(srcdir) + 10);
50 sprintf(buf, "%s/%s", srcdir, name);
57 /* Opens the file in text mode. */
58 return fopen(name, "r");
66 struct yarrow256_ctx yarrow;
67 struct yarrow_key_event_ctx estimator;
69 struct yarrow_source sources[2];
71 struct sha256_ctx output_hash;
72 struct sha256_ctx input_hash;
73 uint8_t digest[SHA256_DIGEST_SIZE];
75 uint8_t seed_file[YARROW256_SEED_FILE_SIZE];
77 const uint8_t *expected_output
78 = H("dd304aacac3dc95e 70d684a642967c89"
79 "58501f7c8eb88b79 43b2ffccde6f0f79");
81 const uint8_t *expected_input
82 = H("e0596cf006025506 65d1195f32a87e4a"
83 "5c354910dfbd0a31 e2105b262f5ce3d8");
85 const uint8_t *expected_seed_file
86 = H("b03518f32b1084dd 983e6a445d47bb6f"
87 "13bb7b998740d570 503d6aaa62e28901");
89 unsigned c; unsigned t;
91 unsigned processed = 0;
96 static const char zeroes[100];
98 yarrow256_init(&yarrow, 2, sources);
100 yarrow_key_event_init(&estimator);
101 sha256_init(&input_hash);
102 sha256_init(&output_hash);
104 knuth_lfib_init(&lfib, 31416);
106 /* Fake input to source 0 */
107 yarrow256_update(&yarrow, 0, 200, sizeof(zeroes), zeroes);
110 printf("source 0 entropy: %d\n",
111 sources[0].estimate[YARROW_SLOW]);
113 ASSERT(!yarrow256_is_seeded(&yarrow));
115 input = open_file("gold-bug.txt");
119 fprintf(stderr, "Couldn't open `gold-bug.txt', errno = %d\n",
124 while (get_event(input, &input_hash, &c, &t))
130 WRITE_UINT32(buf, c);
131 WRITE_UINT32(buf + 4, t);
132 yarrow256_update(&yarrow, 1,
133 yarrow_key_event_estimate(&estimator, c, t),
136 if (yarrow256_is_seeded(&yarrow))
138 static const unsigned sizes[4] = { 1, 16, 500, 37 };
139 unsigned size = sizes[processed % 4];
143 if (verbose && !output)
144 printf("Generator was seeded after %d events\n",
147 yarrow256_random(&yarrow, size, buf);
149 sha256_update(&output_hash, size, buf);
153 printf("%02x ", buf[0]);
154 if (! (processed % 16))
167 for (i = 0; i<2; i++)
168 printf("source %d, (fast, slow) entropy: (%d, %d)\n",
170 sources[i].estimate[YARROW_FAST],
171 sources[i].estimate[YARROW_SLOW]);
173 printf("Processed input: %d octets\n", processed);
177 sha256_digest(&input_hash, sizeof(digest), digest);
181 print_hex(sizeof(digest), digest);
185 ASSERT (memcmp(digest, expected_input, sizeof(digest)) == 0);
187 yarrow256_random(&yarrow, sizeof(seed_file), seed_file);
190 printf("New seed file: ");
191 print_hex(sizeof(seed_file), seed_file);
195 ASSERT (memcmp(seed_file, expected_seed_file, sizeof(seed_file)) == 0);
199 printf("Generated output: %d octets\n", output);
203 sha256_digest(&output_hash, sizeof(digest), digest);
207 print_hex(sizeof(digest), digest);
211 ASSERT (memcmp(digest, expected_output, sizeof(digest)) == 0);