2 * Copyright (c) 2000-2015 Mark R V Murray
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 cc -g -O0 -pthread -DRANDOM_<alg> -DRANDOM_DEBUG -I../.. -lstdthreads -Wall \
37 ../../crypto/rijndael/rijndael-api-fst.c \
38 ../../crypto/rijndael/rijndael-alg-fst.c \
39 ../../crypto/sha2/sha2.c \
44 Where <alg> is YARROW or FORTUNA.
47 #include <sys/types.h>
55 #include "randomdev.h"
56 #include "unit_test.h"
61 static volatile int stopseeding = 0;
64 check_err(int err, const char *func)
67 fprintf(stderr, "Compress error in %s: %d\n", func, err);
73 myalloc(void *q, unsigned n, unsigned m)
76 return (calloc(n, m));
79 void myfree(void *q, void *p)
86 block_deflate(uint8_t *uncompr, uint8_t *compr, const size_t len)
94 c_stream.zalloc = myalloc;
95 c_stream.zfree = myfree;
96 c_stream.opaque = NULL;
98 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
99 check_err(err, "deflateInit");
101 c_stream.next_in = uncompr;
102 c_stream.next_out = compr;
103 c_stream.avail_in = len;
104 c_stream.avail_out = len*2u +512u;
106 while (c_stream.total_in != len && c_stream.total_out < (len*2u + 512u)) {
107 err = deflate(&c_stream, Z_NO_FLUSH);
109 printf("deflate progress: len = %zd total_in = %lu total_out = %lu\n", len, c_stream.total_in, c_stream.total_out);
111 check_err(err, "deflate(..., Z_NO_FLUSH)");
115 err = deflate(&c_stream, Z_FINISH);
117 printf("deflate final: len = %zd total_in = %lu total_out = %lu\n", len, c_stream.total_in, c_stream.total_out);
119 if (err == Z_STREAM_END) break;
120 check_err(err, "deflate(..., Z_STREAM_END)");
123 err = deflateEnd(&c_stream);
124 check_err(err, "deflateEnd");
126 return ((size_t)c_stream.total_out);
130 randomdev_unblock(void)
134 if (mtx_trylock(&random_reseed_mtx) == thrd_busy)
135 printf("Mutex held. Good.\n");
137 printf("Mutex not held. PANIC!!\n");
141 printf("random: unblocking device.\n");
145 RunHarvester(void *arg __unused)
148 struct harvest_event e;
154 printf("Harvest: %d\n", i);
156 e.he_somecounter = i;
157 *((uint64_t *)e.he_entropy) = random();
159 e.he_bits = random()%4;
160 e.he_destination = i;
161 e.he_source = (i + 3)%7;
163 random_alg_context.ra_event_processor(&e);
167 printf("Thread #0 ends\n");
175 WriteCSPRNG(void *threadid)
180 printf("Thread #1 starts\n");
187 printf("Thread write 1 - %d\n", i);
189 printf("Thread 1 writing.\n");
190 random_alg_context.ra_write(buf, i);
196 printf("Thread #1 ends\n");
204 ReadCSPRNG(void *threadid)
214 tid = (size_t)threadid;
215 printf("Thread #%zd starts\n", tid);
217 while (!random_alg_context.ra_seeded())
219 random_alg_context.ra_pre_read();
223 for (i = 0; i < 100000; i++) {
224 buffersize = i + RANDOM_BLOCKSIZE;
225 buffersize -= buffersize%RANDOM_BLOCKSIZE;
226 buf = malloc(buffersize);
227 zbuf = malloc(2*i + 1024);
229 printf("Thread read %zd - %d\n", tid, i);
230 if (buf != NULL && zbuf != NULL) {
231 random_alg_context.ra_pre_read();
232 random_alg_context.ra_read(buf, buffersize);
233 zsize = block_deflate(buf, zbuf, i);
235 printf("ERROR!! Compressible RNG output!\n");
237 printf("RNG output:\n");
238 for (j = 0; j < i; j++) {
239 printf(" %02X", buf[j]);
240 if (j % 32 == 31 || j == i - 1)
243 printf("Compressed output:\n");
244 for (j = 0; j < zsize; j++) {
245 printf(" %02X", zbuf[j]);
246 if (j % 32 == 31 || j == zsize - 1)
256 printf("Thread #%zd ends\n", tid);
264 main(int argc, char *argv[])
266 thrd_t threads[NUM_THREADS];
270 random_alg_context.ra_init_alg(NULL);
272 for (t = 0; t < NUM_THREADS; t++) {
273 printf("In main: creating thread %ld\n", t);
274 rc = thrd_create(&threads[t], (t == 0 ? RunHarvester : (t == 1 ? WriteCSPRNG : ReadCSPRNG)), NULL);
275 if (rc != thrd_success) {
276 printf("ERROR; return code from thrd_create() is %d\n", rc);
281 for (t = 2; t < NUM_THREADS; t++)
282 thrd_join(threads[t], &rc);
286 thrd_join(threads[1], &rc);
287 thrd_join(threads[0], &rc);
289 random_alg_context.ra_deinit_alg(NULL);
291 /* Last thing that main() should do */