d03ab969 |
1 | /* -*-c-*- |
2 | * |
b056b793 |
3 | * $Id: rand.c,v 1.4 1999/12/13 15:34:28 mdw Exp $ |
d03ab969 |
4 | * |
5 | * Secure random number generator |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: rand.c,v $ |
b056b793 |
33 | * Revision 1.4 1999/12/13 15:34:28 mdw |
34 | * Increase the entropy threshhold in rand_getgood. |
35 | * |
aacbc1c6 |
36 | * Revision 1.3 1999/12/10 23:28:07 mdw |
37 | * Bug fix: rand_getgood didn't update buffer pointer. |
38 | * |
ba044e65 |
39 | * Revision 1.2 1999/10/12 21:00:15 mdw |
40 | * Make pool and buffer sizes more sensible. |
41 | * |
d03ab969 |
42 | * Revision 1.1 1999/09/03 08:41:12 mdw |
43 | * Initial import. |
44 | * |
45 | */ |
46 | |
47 | /*----- Header files ------------------------------------------------------*/ |
48 | |
aacbc1c6 |
49 | #include <stdarg.h> |
d03ab969 |
50 | #include <stdio.h> |
51 | #include <string.h> |
52 | |
53 | #include <mLib/bits.h> |
aacbc1c6 |
54 | #include <mLib/sub.h> |
d03ab969 |
55 | |
56 | #include "blowfish-cbc.h" |
57 | #include "paranoia.h" |
58 | #include "rand.h" |
59 | #include "rmd160.h" |
60 | #include "rmd160-hmac.h" |
61 | |
62 | /*----- Static variables --------------------------------------------------*/ |
63 | |
aacbc1c6 |
64 | static const grand_ops gops; |
65 | |
66 | typedef struct gctx { |
67 | grand r; |
68 | rand_pool p; |
69 | } gctx; |
70 | |
71 | static gctx pool = { { &gops } }; /* Default random pool */ |
d03ab969 |
72 | |
73 | /*----- Macros ------------------------------------------------------------*/ |
74 | |
75 | #define RAND_RESOLVE(r) do { \ |
76 | if ((r) == RAND_GLOBAL) \ |
aacbc1c6 |
77 | (r) = &pool.p; \ |
d03ab969 |
78 | } while (0) |
79 | |
80 | #define TIMER(r) do { \ |
81 | if ((r)->s && (r)->s->timer) \ |
82 | (r)->s->timer(r); \ |
83 | } while (0) |
84 | |
85 | /*----- Main code ---------------------------------------------------------*/ |
86 | |
87 | /* --- @rand_init@ --- * |
88 | * |
89 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
90 | * |
91 | * Returns: --- |
92 | * |
93 | * Use: Initializes a randomness pool. The pool doesn't start out |
94 | * very random: that's your job to sort out. |
95 | */ |
96 | |
97 | void rand_init(rand_pool *r) |
98 | { |
99 | RAND_RESOLVE(r); |
100 | memset(r->pool, 0, sizeof(r->pool)); |
101 | memset(r->buf, 0, sizeof(r->buf)); |
102 | r->i = 0; |
103 | r->irot = 0; |
104 | r->ibits = r->obits = 0; |
105 | r->o = RAND_SECSZ; |
106 | r->s = 0; |
aacbc1c6 |
107 | rmd160_hmacinit(&r->k, 0, 0); |
d03ab969 |
108 | rand_gate(r); |
109 | } |
110 | |
111 | /* --- @rand_noisesrc@ --- * |
112 | * |
113 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
114 | * @const rand_source *s@ = pointer to source definition |
115 | * |
116 | * Returns: --- |
117 | * |
118 | * Use: Sets a noise source for a randomness pool. When the pool's |
119 | * estimate of good random bits falls to zero, the @getnoise@ |
120 | * function is called, passing the pool handle as an argument. |
121 | * It is expected to increase the number of good bits by at |
122 | * least one, because it'll be called over and over again until |
123 | * there are enough bits to satisfy the caller. The @timer@ |
124 | * function is called frequently throughout the generator's |
125 | * operation. |
126 | */ |
127 | |
128 | void rand_noisesrc(rand_pool *r, const rand_source *s) |
129 | { |
130 | RAND_RESOLVE(r); |
131 | r->s = s; |
132 | } |
133 | |
134 | /* --- @rand_key@ --- * |
135 | * |
136 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
137 | * @const void *k@ = pointer to key data |
138 | * @size_t sz@ = size of key data |
139 | * |
140 | * Returns: --- |
141 | * |
142 | * Use: Sets the secret key for a randomness pool. The key is used |
143 | * when mixing in new random bits. |
144 | */ |
145 | |
146 | void rand_key(rand_pool *r, const void *k, size_t sz) |
147 | { |
148 | RAND_RESOLVE(r); |
aacbc1c6 |
149 | rmd160_hmacinit(&r->k, k, sz); |
d03ab969 |
150 | } |
151 | |
152 | /* --- @rand_add@ --- * |
153 | * |
154 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
155 | * @const void *p@ = pointer a buffer of data to add |
156 | * @size_t sz@ = size of the data buffer |
157 | * @unsigned goodbits@ = number of good bits estimated in buffer |
158 | * |
159 | * Returns: --- |
160 | * |
161 | * Use: Mixes the data in the buffer with the contents of the |
162 | * pool. The estimate of the number of good bits is added to |
163 | * the pool's own count. The mixing operation is not |
164 | * cryptographically strong. However, data in the input pool |
165 | * isn't output directly, only through the one-way gating |
166 | * operation, so that shouldn't matter. |
167 | */ |
168 | |
169 | void rand_add(rand_pool *r, const void *p, size_t sz, unsigned goodbits) |
170 | { |
171 | const octet *c = p; |
ba044e65 |
172 | int i, rot; |
d03ab969 |
173 | |
ba044e65 |
174 | #if RAND_POOLSZ != 128 |
d03ab969 |
175 | # error Polynomial in rand_add is out of date. Fix it. |
176 | #endif |
177 | |
178 | RAND_RESOLVE(r); |
179 | |
ba044e65 |
180 | i = r->i; rot = r->irot; |
d03ab969 |
181 | |
182 | while (sz) { |
183 | octet o = *c++; |
ba044e65 |
184 | r->pool[i] ^= (ROL8(o, rot) ^ |
185 | r->pool[(i + 1) % RAND_POOLSZ] ^ |
186 | r->pool[(i + 2) % RAND_POOLSZ] ^ |
187 | r->pool[(i + 7) % RAND_POOLSZ]); |
d03ab969 |
188 | rot = (rot + 5) & 7; |
189 | i++; if (i >= RAND_POOLSZ) i -= RAND_POOLSZ; |
d03ab969 |
190 | sz--; |
191 | } |
192 | |
193 | r->i = i; |
194 | r->irot = rot; |
195 | r->ibits += goodbits; |
196 | if (r->ibits > RAND_IBITS) |
197 | r->ibits = RAND_IBITS; |
198 | } |
199 | |
200 | /* --- @rand_goodbits@ --- * |
201 | * |
202 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
203 | * |
204 | * Returns: Estimate of the number of good bits remaining in the pool. |
205 | */ |
206 | |
207 | unsigned rand_goodbits(rand_pool *r) |
208 | { |
209 | RAND_RESOLVE(r); |
210 | return (r->ibits + r->obits); |
211 | } |
212 | |
213 | /* --- @rand_gate@ --- * |
214 | * |
215 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
216 | * |
217 | * Returns: --- |
218 | * |
219 | * Use: Mixes up the entire state of the generator in a nonreversible |
220 | * way. |
221 | */ |
222 | |
223 | void rand_gate(rand_pool *r) |
224 | { |
225 | octet mac[RMD160_HASHSZ]; |
226 | |
227 | RAND_RESOLVE(r); |
228 | TIMER(r); |
229 | |
230 | /* --- Hash up all the data in the pool --- */ |
231 | |
232 | { |
233 | rmd160_macctx mc; |
234 | |
235 | rmd160_macinit(&mc, &r->k); |
aacbc1c6 |
236 | rmd160_machash(&mc, r->pool, sizeof(r->pool)); |
237 | rmd160_machash(&mc, r->buf, sizeof(r->buf)); |
d03ab969 |
238 | rmd160_macdone(&mc, mac); |
239 | BURN(mc); |
240 | } |
241 | |
242 | /* --- Now mangle all of the data based on the hash --- */ |
243 | |
244 | { |
245 | blowfish_cbcctx bc; |
246 | |
247 | blowfish_cbcinit(&bc, mac, sizeof(mac), 0); |
248 | blowfish_cbcencrypt(&bc, r->pool, r->pool, sizeof(r->pool)); |
249 | blowfish_cbcencrypt(&bc, r->buf, r->buf, sizeof(r->buf)); |
250 | BURN(bc); |
251 | } |
252 | |
253 | /* --- Reset the various state variables --- */ |
254 | |
255 | r->o = RAND_SECSZ; |
256 | r->obits += r->ibits; |
257 | if (r->obits > RAND_OBITS) { |
258 | r->ibits = r->obits - r->ibits; |
259 | r->obits = RAND_OBITS; |
260 | } else |
261 | r->ibits = 0; |
262 | TIMER(r); |
263 | } |
264 | |
265 | /* --- @rand_stretch@ --- * |
266 | * |
267 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
268 | * |
269 | * Returns: --- |
270 | * |
271 | * Use: Stretches the contents of the output buffer by transforming |
272 | * it in a nonreversible way. This doesn't add any entropy |
273 | * worth speaking about, but it works well enough when the |
274 | * caller doesn't care about that sort of thing. |
275 | */ |
276 | |
277 | void rand_stretch(rand_pool *r) |
278 | { |
279 | octet mac[RMD160_HASHSZ]; |
280 | |
281 | RAND_RESOLVE(r); |
282 | TIMER(r); |
283 | |
284 | /* --- Hash up all the data in the buffer --- */ |
285 | |
286 | { |
287 | rmd160_macctx mc; |
288 | |
289 | rmd160_macinit(&mc, &r->k); |
aacbc1c6 |
290 | rmd160_machash(&mc, r->pool, sizeof(r->pool)); |
291 | rmd160_machash(&mc, r->buf, sizeof(r->buf)); |
d03ab969 |
292 | rmd160_macdone(&mc, mac); |
293 | BURN(mc); |
294 | } |
295 | |
296 | /* --- Now mangle the buffer based on that hash --- */ |
297 | |
298 | { |
299 | blowfish_cbcctx bc; |
300 | |
301 | blowfish_cbcinit(&bc, mac, sizeof(mac), 0); |
302 | blowfish_cbcencrypt(&bc, r->buf, r->buf, sizeof(r->buf)); |
303 | BURN(bc); |
304 | } |
305 | |
306 | /* --- Reset the various state variables --- */ |
307 | |
308 | r->o = RAND_SECSZ; |
309 | TIMER(r); |
310 | } |
311 | |
312 | /* --- @rand_get@ --- * |
313 | * |
314 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
315 | * @void *p@ = pointer to output buffer |
316 | * @size_t sz@ = size of output buffer |
317 | * |
318 | * Returns: --- |
319 | * |
320 | * Use: Gets random data from the pool. The pool's contents can't be |
321 | * determined from the output of this function; nor can the |
322 | * output data be determined from a knowledge of the data input |
323 | * to the pool wihtout also having knowledge of the secret key. |
324 | * The good bits counter is decremented, although no special |
325 | * action is taken if it reaches zero. |
326 | */ |
327 | |
328 | void rand_get(rand_pool *r, void *p, size_t sz) |
329 | { |
330 | octet *o = p; |
331 | |
332 | RAND_RESOLVE(r); |
333 | TIMER(r); |
334 | |
335 | if (!sz) |
336 | return; |
337 | for (;;) { |
338 | if (r->o + sz <= RAND_BUFSZ) { |
339 | memcpy(o, r->buf + r->o, sz); |
340 | r->o += sz; |
341 | break; |
342 | } else { |
343 | size_t chunk = RAND_BUFSZ - r->o; |
344 | if (chunk) { |
345 | memcpy(o, r->buf + r->o, chunk); |
346 | sz -= chunk; |
347 | o += chunk; |
348 | } |
349 | rand_stretch(r); |
350 | } |
351 | } |
352 | |
353 | if (r->obits > sz * 8) |
354 | r->obits -= sz * 8; |
355 | else |
356 | r->obits = 0; |
357 | } |
358 | |
359 | /* --- @rand_getgood@ --- * |
360 | * |
361 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
362 | * @void *p@ = pointer to output buffer |
363 | * @size_t sz@ = size of output buffer |
364 | * |
365 | * Returns: --- |
366 | * |
367 | * Use: Gets random data from the pool. The pool's contents can't be |
368 | * determined from the output of this function; nor can the |
369 | * output data be determined from a knowledge of the data input |
370 | * to the pool wihtout also having knowledge of the secret key. |
371 | * If a noise source is attached to the pool in question, it is |
372 | * called to replenish the supply of good bits in the pool; |
373 | * otherwise this call is equivalent to @rand_get@. |
374 | */ |
375 | |
376 | void rand_getgood(rand_pool *r, void *p, size_t sz) |
377 | { |
378 | octet *o = p; |
379 | |
380 | RAND_RESOLVE(r); |
381 | |
382 | if (!sz) |
383 | return; |
384 | if (!r->s || !r->s->getnoise) { |
385 | rand_get(r, p, sz); |
386 | return; |
387 | } |
388 | TIMER(r); |
389 | |
390 | while (sz) { |
391 | size_t chunk = sz; |
392 | |
393 | if (chunk * 8 > r->obits) { |
394 | if (chunk * 8 > r->ibits + r->obits) |
b056b793 |
395 | do r->s->getnoise(r); while (r->ibits + r->obits < 256); |
d03ab969 |
396 | rand_gate(r); |
397 | if (chunk * 8 > r->obits) |
398 | chunk = r->obits / 8; |
399 | } |
400 | |
401 | if (chunk + r->o > RAND_BUFSZ) |
402 | chunk = RAND_BUFSZ - r->o; |
403 | |
404 | memcpy(o, r->buf + r->o, chunk); |
aacbc1c6 |
405 | r->o += chunk; |
d03ab969 |
406 | r->obits -= chunk * 8; |
407 | o += chunk; |
408 | sz -= chunk; |
409 | } |
410 | } |
411 | |
aacbc1c6 |
412 | /*----- Generic random number generator interface -------------------------*/ |
413 | |
414 | static void gdestroy(grand *r) |
415 | { |
416 | gctx *g = (r == &rand_global ? &pool : (gctx *)r); |
417 | if (g != &pool) |
418 | DESTROY(g); |
419 | } |
420 | |
421 | static int gmisc(grand *r, unsigned op, ...) |
422 | { |
423 | gctx *g = (r == &rand_global ? &pool : (gctx *)r); |
424 | va_list ap; |
425 | int rc = 0; |
426 | va_start(ap, op); |
427 | |
428 | switch (op) { |
429 | case GRAND_CHECK: |
430 | switch (va_arg(ap, unsigned)) { |
431 | case GRAND_CHECK: |
432 | case GRAND_SEEDINT: |
433 | case GRAND_SEEDUINT32: |
434 | case GRAND_SEEDBLOCK: |
b056b793 |
435 | case GRAND_SEEDRAND: |
aacbc1c6 |
436 | case RAND_GATE: |
437 | case RAND_STRETCH: |
438 | case RAND_KEY: |
439 | case RAND_NOISESRC: |
440 | rc = 1; |
441 | break; |
442 | default: |
443 | rc = 0; |
444 | break; |
445 | } |
446 | break; |
447 | case GRAND_SEEDINT: { |
448 | unsigned u = va_arg(ap, unsigned); |
449 | rand_add(&g->p, &u, sizeof(u), sizeof(u)); |
450 | } break; |
451 | case GRAND_SEEDUINT32: { |
452 | uint32 i = va_arg(ap, uint32); |
453 | rand_add(&g->p, &i, sizeof(i), 4); |
454 | } break; |
455 | case GRAND_SEEDBLOCK: { |
456 | const void *p = va_arg(ap, const void *); |
457 | size_t sz = va_arg(ap, size_t); |
458 | rand_add(&g->p, p, sz, sz); |
459 | } break; |
b056b793 |
460 | case GRAND_SEEDRAND: { |
461 | grand *rr = va_arg(ap, grand *); |
462 | octet buf[16]; |
463 | rr->ops->fill(rr, buf, sizeof(buf)); |
464 | rand_add(&g->p, buf, sizeof(buf), 8); |
465 | } break; |
aacbc1c6 |
466 | case RAND_GATE: |
467 | rand_gate(&g->p); |
468 | break; |
469 | case RAND_STRETCH: |
470 | rand_stretch(&g->p); |
471 | break; |
472 | case RAND_KEY: { |
473 | const void *k = va_arg(ap, const void *); |
474 | size_t sz = va_arg(ap, size_t); |
475 | rand_key(&g->p, k, sz); |
476 | } break; |
477 | case RAND_NOISESRC: |
478 | rand_noisesrc(&g->p, va_arg(ap, const rand_source *)); |
479 | break; |
480 | default: |
481 | GRAND_BADOP; |
482 | break; |
483 | } |
484 | |
485 | va_end(ap); |
486 | return (rc); |
487 | } |
488 | |
489 | static octet gbyte(grand *r) |
490 | { |
491 | gctx *g = (r == &rand_global ? &pool : (gctx *)r); |
492 | octet o; |
493 | rand_getgood(&g->p, &o, 1); |
494 | return (o); |
495 | } |
496 | |
497 | static uint32 gword(grand *r) |
498 | { |
499 | gctx *g = (r == &rand_global ? &pool : (gctx *)r); |
500 | octet b[4]; |
501 | rand_getgood(&g->p, &b, sizeof(b)); |
502 | return (LOAD32(b)); |
503 | } |
504 | |
505 | static void gfill(grand *r, void *p, size_t sz) |
506 | { |
507 | gctx *g = (r == &rand_global ? &pool : (gctx *)r); |
508 | rand_getgood(&g->p, p, sz); |
509 | } |
510 | |
511 | static const grand_ops gops = { |
512 | "rand", |
513 | 0, |
514 | gmisc, gdestroy, |
515 | gword, gbyte, gword, grand_range, gfill |
516 | }; |
517 | |
518 | grand rand_global = { &gops }; |
519 | |
520 | /* --- @rand_create@ --- * |
521 | * |
522 | * Arguments: --- |
523 | * |
524 | * Returns: Pointer to a generic generator. |
525 | * |
526 | * Use: Constructs a generic generator interface over a Catacomb |
527 | * entropy pool generator. |
528 | */ |
529 | |
530 | grand *rand_create(void) |
531 | { |
532 | gctx *g = CREATE(gctx); |
533 | g->r.ops = &gops; |
534 | rand_init(&g->p); |
535 | return (&g->r); |
536 | } |
537 | |
d03ab969 |
538 | /*----- That's all, folks -------------------------------------------------*/ |