2 * Copyright (c) 2000-2013 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 \
43 Where <alg> is YARROW or FORTUNA.
46 #include <sys/types.h>
53 #include "unit_test.h"
56 #include "dev/random/yarrow.h"
59 #include "dev/random/fortuna.h"
64 static volatile int stopseeding = 0;
67 random_adaptor_unblock(void)
71 if (mtx_trylock(&random_reseed_mtx) == thrd_busy)
72 printf("Mutex held. Good.\n");
74 printf("Mutex not held. PANIC!!\n");
78 printf("random: unblocking device.\n");
82 RunHarvester(void *arg __unused)
85 struct harvest_event e;
91 printf("Harvest: %d\n", i);
94 *((uint64_t *)e.he_entropy) = random();
96 e.he_bits = random()%4;
98 e.he_source = (i + 3)%7;
101 random_yarrow_process_event(&e);
103 #ifdef RANDOM_FORTUNA
104 random_fortuna_process_event(&e);
109 printf("Thread #0 ends\n");
117 WriteCSPRNG(void *threadid)
122 printf("Thread #1 starts\n");
129 printf("Thread write 1 - %d\n", i);
132 random_yarrow_write(buf, i);
134 #ifdef RANDOM_FORTUNA
135 random_fortuna_write(buf, i);
142 printf("Thread #1 ends\n");
150 ReadCSPRNG(void *threadid)
156 tid = (size_t)threadid;
157 printf("Thread #%zd starts\n", tid);
160 while (!random_yarrow_seeded())
162 #ifdef RANDOM_FORTUNA
163 while (!random_fortuna_seeded())
167 random_yarrow_read(NULL, 0);
168 random_yarrow_read(NULL, 1);
170 #ifdef RANDOM_FORTUNA
171 random_fortuna_read(NULL, 0);
172 random_fortuna_read(NULL, 1);
177 for (i = 0; i < 100000; i++) {
180 printf("Thread read %zd - %d %p\n", tid, i, buf);
183 random_yarrow_read(NULL, 0);
184 random_yarrow_read(buf, i);
185 random_yarrow_read(NULL, 1);
187 #ifdef RANDOM_FORTUNA
188 random_fortuna_read(NULL, 0);
189 random_fortuna_read(buf, i);
190 random_fortuna_read(NULL, 1);
196 for (j = 0; j < i; j++) {
197 printf(" %02X", buf[j]);
198 if (j % 32 == 31 || j == i - 1)
208 printf("Thread #%zd ends\n", tid);
216 main(int argc, char *argv[])
218 thrd_t threads[NUM_THREADS];
223 random_yarrow_init_alg();
225 #ifdef RANDOM_FORTUNA
226 random_fortuna_init_alg();
229 for (t = 0; t < NUM_THREADS; t++) {
230 printf("In main: creating thread %ld\n", t);
231 rc = thrd_create(&threads[t], (t == 0 ? RunHarvester : (t == 1 ? WriteCSPRNG : ReadCSPRNG)), t);
232 if (rc != thrd_success) {
233 printf("ERROR; return code from thrd_create() is %d\n", rc);
238 for (t = 2; t < NUM_THREADS; t++)
239 thrd_join(threads[t], &rc);
243 thrd_join(threads[1], &rc);
244 thrd_join(threads[0], &rc);
247 random_yarrow_deinit_alg();
249 #ifdef RANDOM_FORTUNA
250 random_fortuna_deinit_alg();
253 /* Last thing that main() should do */