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> -I../.. -lstdthreads -Wall \
37 ../../crypto/rijndael/rijndael-api-fst.c \
38 ../../crypto/rijndael/rijndael-alg-fst.c \
39 ../../crypto/sha2/sha256c.c \
44 Where <alg> is YARROW or FORTUNA.
47 #include <sys/types.h>
56 #include "randomdev.h"
57 #include "unit_test.h"
62 static volatile int stopseeding = 0;
65 check_err(int err, const char *func)
68 fprintf(stderr, "Compress error in %s: %d\n", func, err);
74 myalloc(void *q, unsigned n, unsigned m)
77 return (calloc(n, m));
80 void myfree(void *q, void *p)
87 block_deflate(uint8_t *uncompr, uint8_t *compr, const size_t len)
95 c_stream.zalloc = myalloc;
96 c_stream.zfree = myfree;
97 c_stream.opaque = NULL;
99 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
100 check_err(err, "deflateInit");
102 c_stream.next_in = uncompr;
103 c_stream.next_out = compr;
104 c_stream.avail_in = len;
105 c_stream.avail_out = len*2u +512u;
107 while (c_stream.total_in != len && c_stream.total_out < (len*2u + 512u)) {
108 err = deflate(&c_stream, Z_NO_FLUSH);
110 printf("deflate progress: len = %zd total_in = %lu total_out = %lu\n", len, c_stream.total_in, c_stream.total_out);
112 check_err(err, "deflate(..., Z_NO_FLUSH)");
116 err = deflate(&c_stream, Z_FINISH);
118 printf("deflate final: len = %zd total_in = %lu total_out = %lu\n", len, c_stream.total_in, c_stream.total_out);
120 if (err == Z_STREAM_END) break;
121 check_err(err, "deflate(..., Z_STREAM_END)");
124 err = deflateEnd(&c_stream);
125 check_err(err, "deflateEnd");
127 return ((size_t)c_stream.total_out);
131 randomdev_unblock(void)
135 if (mtx_trylock(&random_reseed_mtx) == thrd_busy)
136 printf("Mutex held. Good.\n");
138 printf("Mutex not held. PANIC!!\n");
142 printf("random: unblocking device.\n");
146 RunHarvester(void *arg __unused)
149 struct harvest_event e;
155 printf("Harvest: %d\n", i);
157 e.he_somecounter = i;
158 *((uint64_t *)e.he_entropy) = random();
160 e.he_bits = random()%4;
161 e.he_destination = i;
162 e.he_source = (i + 3)%7;
164 random_alg_context.ra_event_processor(&e);
168 printf("Thread #0 ends\n");
176 ReadCSPRNG(void *threadid)
186 tid = (size_t)threadid;
187 printf("Thread #%zd starts\n", tid);
189 while (!random_alg_context.ra_seeded())
191 random_alg_context.ra_pre_read();
195 for (i = 0; i < 100000; i++) {
196 buffersize = i + RANDOM_BLOCKSIZE;
197 buffersize -= buffersize%RANDOM_BLOCKSIZE;
198 buf = malloc(buffersize);
199 zbuf = malloc(2*i + 1024);
201 printf("Thread read %zd - %d\n", tid, i);
202 if (buf != NULL && zbuf != NULL) {
203 random_alg_context.ra_pre_read();
204 random_alg_context.ra_read(buf, buffersize);
205 zsize = block_deflate(buf, zbuf, i);
207 printf("ERROR!! Compressible RNG output!\n");
209 printf("RNG output:\n");
210 for (j = 0; j < i; j++) {
211 printf(" %02X", buf[j]);
212 if (j % 32 == 31 || j == i - 1)
215 printf("Compressed output:\n");
216 for (j = 0; j < zsize; j++) {
217 printf(" %02X", zbuf[j]);
218 if (j % 32 == 31 || j == zsize - 1)
228 printf("Thread #%zd ends\n", tid);
236 main(int argc, char *argv[])
238 thrd_t threads[NUM_THREADS];
242 random_alg_context.ra_init_alg(NULL);
244 for (t = 0; t < NUM_THREADS; t++) {
245 printf("In main: creating thread %ld\n", t);
246 rc = thrd_create(&threads[t], (t == 0 ? RunHarvester : ReadCSPRNG), NULL);
247 if (rc != thrd_success) {
248 printf("ERROR; return code from thrd_create() is %d\n", rc);
253 for (t = 2; t < NUM_THREADS; t++)
254 thrd_join(threads[t], &rc);
258 thrd_join(threads[1], &rc);
259 thrd_join(threads[0], &rc);
261 random_alg_context.ra_deinit_alg(NULL);
263 /* Last thing that main() should do */