]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/yarrow.c
IFC @ r255209
[FreeBSD/FreeBSD.git] / sys / dev / random / yarrow.c
1 /*-
2  * Copyright (c) 2000-2004 Mark R V Murray
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
14  *
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.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/random.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39
40 #include <crypto/rijndael/rijndael-api-fst.h>
41 #include <crypto/sha2/sha2.h>
42
43 #include <dev/random/hash.h>
44 #include <dev/random/random_adaptors.h>
45 #include <dev/random/randomdev_soft.h>
46 #include <dev/random/yarrow.h>
47
48 RANDOM_CHECK_UINT(gengateinterval, 4, 64);
49 RANDOM_CHECK_UINT(bins, 2, 16);
50 RANDOM_CHECK_UINT(fastthresh, BLOCKSIZE/4, BLOCKSIZE);
51 RANDOM_CHECK_UINT(slowthresh, BLOCKSIZE/4, BLOCKSIZE);
52 RANDOM_CHECK_UINT(slowoverthresh, 1, 5);
53
54 /* Structure holding the entropy state */
55 static struct random_state random_state;
56
57 static void generator_gate(void);
58 static void reseed(u_int);
59
60 /* The reseed thread mutex */
61 struct mtx random_reseed_mtx;
62
63 /* Process a single stochastic event off the harvest queue */
64 void
65 random_process_event(struct harvest *event)
66 {
67         u_int pl, overthreshhold[2];
68         struct source *source;
69         enum esource src;
70
71         /* Unpack the event into the appropriate source accumulator */
72         pl = random_state.which;
73         source = &random_state.pool[pl].source[event->source];
74         yarrow_hash_iterate(&random_state.pool[pl].hash, event->entropy,
75                 sizeof(event->entropy));
76         yarrow_hash_iterate(&random_state.pool[pl].hash, &event->somecounter,
77                 sizeof(event->somecounter));
78         source->frac += event->frac;
79         source->bits += event->bits + source->frac/1024;
80         source->frac %= 1024;
81
82         /* Count the over-threshold sources in each pool */
83         for (pl = 0; pl < 2; pl++) {
84                 overthreshhold[pl] = 0;
85                 for (src = RANDOM_START; src < ENTROPYSOURCE; src++) {
86                         if (random_state.pool[pl].source[src].bits
87                                 > random_state.pool[pl].thresh)
88                                 overthreshhold[pl]++;
89                 }
90         }
91
92         /* if any fast source over threshhold, reseed */
93         if (overthreshhold[FAST])
94                 reseed(FAST);
95
96         /* if enough slow sources are over threshhold, reseed */
97         if (overthreshhold[SLOW] >= random_state.slowoverthresh)
98                 reseed(SLOW);
99
100         /* Invert the fast/slow pool selector bit */
101         random_state.which = !random_state.which;
102 }
103
104 void
105 random_yarrow_init_alg(struct sysctl_ctx_list *clist)
106 {
107         int i;
108         struct sysctl_oid *random_yarrow_o;
109
110         /* Yarrow parameters. Do not adjust these unless you have
111          * have a very good clue about what they do!
112          */
113         random_yarrow_o = SYSCTL_ADD_NODE(clist,
114                 SYSCTL_STATIC_CHILDREN(_kern_random),
115                 OID_AUTO, "yarrow", CTLFLAG_RW, 0,
116                 "Yarrow Parameters");
117
118         SYSCTL_ADD_PROC(clist,
119                 SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
120                 "gengateinterval", CTLTYPE_INT|CTLFLAG_RW,
121                 &random_state.gengateinterval, 10,
122                 random_check_uint_gengateinterval, "I",
123                 "Generation gate interval");
124
125         SYSCTL_ADD_PROC(clist,
126                 SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
127                 "bins", CTLTYPE_INT|CTLFLAG_RW,
128                 &random_state.bins, 10,
129                 random_check_uint_bins, "I",
130                 "Execution time tuner");
131
132         SYSCTL_ADD_PROC(clist,
133                 SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
134                 "fastthresh", CTLTYPE_INT|CTLFLAG_RW,
135                 &random_state.pool[0].thresh, (3*BLOCKSIZE)/4,
136                 random_check_uint_fastthresh, "I",
137                 "Fast reseed threshold");
138
139         SYSCTL_ADD_PROC(clist,
140                 SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
141                 "slowthresh", CTLTYPE_INT|CTLFLAG_RW,
142                 &random_state.pool[1].thresh, BLOCKSIZE,
143                 random_check_uint_slowthresh, "I",
144                 "Slow reseed threshold");
145
146         SYSCTL_ADD_PROC(clist,
147                 SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
148                 "slowoverthresh", CTLTYPE_INT|CTLFLAG_RW,
149                 &random_state.slowoverthresh, 2,
150                 random_check_uint_slowoverthresh, "I",
151                 "Slow over-threshold reseed");
152
153         random_state.gengateinterval = 10;
154         random_state.bins = 10;
155         random_state.pool[0].thresh = (3*BLOCKSIZE)/4;
156         random_state.pool[1].thresh = BLOCKSIZE;
157         random_state.slowoverthresh = 2;
158         random_state.which = FAST;
159
160         /* Initialise the fast and slow entropy pools */
161         for (i = 0; i < 2; i++)
162                 yarrow_hash_init(&random_state.pool[i].hash);
163
164         /* Clear the counter */
165         for (i = 0; i < 4; i++)
166                 random_state.counter[i] = 0;
167
168         /* Set up a lock for the reseed process */
169         mtx_init(&random_reseed_mtx, "random reseed", NULL, MTX_DEF);
170 }
171
172 void
173 random_yarrow_deinit_alg(void)
174 {
175         mtx_destroy(&random_reseed_mtx);
176 }
177
178 static void
179 reseed(u_int fastslow)
180 {
181         /* Interrupt-context stack is a limited resource; make large
182          * structures static.
183          */
184         static u_char v[TIMEBIN][KEYSIZE];      /* v[i] */
185         static struct yarrowhash context;
186         u_char hash[KEYSIZE];                   /* h' */
187         u_char temp[KEYSIZE];
188         u_int i;
189         enum esource j;
190
191         /* The reseed task must not be jumped on */
192         mtx_lock(&random_reseed_mtx);
193
194         /* 1. Hash the accumulated entropy into v[0] */
195
196         yarrow_hash_init(&context);
197         /* Feed the slow pool hash in if slow */
198         if (fastslow == SLOW)
199                 yarrow_hash_iterate(&context,
200                         &random_state.pool[SLOW].hash,
201                         sizeof(struct yarrowhash));
202         yarrow_hash_iterate(&context,
203                 &random_state.pool[FAST].hash, sizeof(struct yarrowhash));
204         yarrow_hash_finish(&context, v[0]);
205
206         /* 2. Compute hash values for all v. _Supposed_ to be computationally
207          *    intensive.
208          */
209
210         if (random_state.bins > TIMEBIN)
211                 random_state.bins = TIMEBIN;
212         for (i = 1; i < random_state.bins; i++) {
213                 yarrow_hash_init(&context);
214                 /* v[i] #= h(v[i - 1]) */
215                 yarrow_hash_iterate(&context, v[i - 1], KEYSIZE);
216                 /* v[i] #= h(v[0]) */
217                 yarrow_hash_iterate(&context, v[0], KEYSIZE);
218                 /* v[i] #= h(i) */
219                 yarrow_hash_iterate(&context, &i, sizeof(u_int));
220                 /* Return the hashval */
221                 yarrow_hash_finish(&context, v[i]);
222         }
223
224         /* 3. Compute a new key; h' is the identity function here;
225          *    it is not being ignored!
226          */
227
228         yarrow_hash_init(&context);
229         yarrow_hash_iterate(&context, &random_state.key, KEYSIZE);
230         for (i = 1; i < random_state.bins; i++)
231                 yarrow_hash_iterate(&context, &v[i], KEYSIZE);
232         yarrow_hash_finish(&context, temp);
233         yarrow_encrypt_init(&random_state.key, temp);
234
235         /* 4. Recompute the counter */
236
237         for (i = 0; i < 4; i++)
238                 random_state.counter[i] = 0;
239         yarrow_encrypt(&random_state.key, random_state.counter, temp);
240         memcpy(random_state.counter, temp, sizeof(random_state.counter));
241
242         /* 5. Reset entropy estimate accumulators to zero */
243
244         for (i = 0; i <= fastslow; i++) {
245                 for (j = RANDOM_START; j < ENTROPYSOURCE; j++) {
246                         random_state.pool[i].source[j].bits = 0;
247                         random_state.pool[i].source[j].frac = 0;
248                 }
249         }
250
251         /* 6. Wipe memory of intermediate values */
252
253         memset((void *)v, 0, sizeof(v));
254         memset((void *)temp, 0, sizeof(temp));
255         memset((void *)hash, 0, sizeof(hash));
256
257         /* 7. Dump to seed file */
258         /* XXX Not done here yet */
259
260         /* Unblock the device if it was blocked due to being unseeded */
261         random_yarrow_unblock();
262
263         /* Release the reseed mutex */
264         mtx_unlock(&random_reseed_mtx);
265 }
266
267 /* Internal function to return processed entropy from the PRNG */
268 int
269 random_yarrow_read(void *buf, int count)
270 {
271         static int cur = 0;
272         static int gate = 1;
273         static u_char genval[KEYSIZE];
274         size_t tomove;
275         int i;
276         int retval;
277
278         /* The reseed task must not be jumped on */
279         mtx_lock(&random_reseed_mtx);
280
281         if (gate) {
282                 generator_gate();
283                 random_state.outputblocks = 0;
284                 gate = 0;
285         }
286         if (count > 0 && (size_t)count >= sizeof(random_state.counter)) {
287                 retval = 0;
288                 for (i = 0; i < count; i += (int)sizeof(random_state.counter)) {
289                         random_state.counter[0]++;
290                         yarrow_encrypt(&random_state.key, random_state.counter,
291                                 genval);
292                         tomove = min(count - i, sizeof(random_state.counter));
293                         memcpy((char *)buf + i, genval, tomove);
294                         if (++random_state.outputblocks >=
295                                 random_state.gengateinterval) {
296                                 generator_gate();
297                                 random_state.outputblocks = 0;
298                         }
299                         retval += (int)tomove;
300                         cur = 0;
301                 }
302         }
303         else {
304                 if (!cur) {
305                         random_state.counter[0]++;
306                         yarrow_encrypt(&random_state.key, random_state.counter,
307                                 genval);
308                         memcpy(buf, genval, (size_t)count);
309                         cur = (int)sizeof(random_state.counter) - count;
310                         if (++random_state.outputblocks >=
311                                 random_state.gengateinterval) {
312                                 generator_gate();
313                                 random_state.outputblocks = 0;
314                         }
315                         retval = count;
316                 }
317                 else {
318                         retval = MIN(cur, count);
319                         memcpy(buf,
320                             &genval[(int)sizeof(random_state.counter) - cur],
321                             (size_t)retval);
322                         cur -= retval;
323                 }
324         }
325         mtx_unlock(&random_reseed_mtx);
326         return retval;
327 }
328
329 static void
330 generator_gate(void)
331 {
332         u_int i;
333         u_char temp[KEYSIZE];
334
335         for (i = 0; i < KEYSIZE; i += sizeof(random_state.counter)) {
336                 random_state.counter[0]++;
337                 yarrow_encrypt(&random_state.key, random_state.counter,
338                         &(temp[i]));
339         }
340
341         yarrow_encrypt_init(&random_state.key, temp);
342         memset((void *)temp, 0, KEYSIZE);
343
344 }
345
346 /* Helper routine to perform explicit reseeds */
347 void
348 random_yarrow_reseed(void)
349 {
350         reseed(SLOW);
351 }