3 * Secure random number generator
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/bits.h>
45 #include "twofish-counter.h"
48 #define CIPHER_CTX twofish_counterctx
49 #define CIPHER_INIT twofish_counterinit
50 #define CIPHER_ENCRYPT twofish_counterencrypt
51 #define CIPHER_IVSZ TWOFISH_BLKSZ
52 #define CIPHER_KEYSZ TWOFISH_KEYSZ
54 #define HASH_CTX sha256_ctx
55 #define HASH_INIT sha256_init
56 #define HASH sha256_hash
57 #define HASH_DONE sha256_done
58 #define HASH_SZ SHA256_HASHSZ
60 /*----- Static variables --------------------------------------------------*/
62 static const grand_ops gops;
64 typedef struct rand__gctx {
73 { "Catacomb global random byte pool" },
77 /*----- Macros ------------------------------------------------------------*/
79 #define RAND_RESOLVE(r) \
80 do { if ((r) == RAND_GLOBAL) r = &rand_global.p; } while (0)
82 #define TIMER(r) do { \
83 if ((r)->s && (r)->s->timer) \
87 /*----- Main code ---------------------------------------------------------*/
89 /* --- @rand_init@ --- *
91 * Arguments: @rand_pool *r@ = pointer to a randomness pool
95 * Use: Initializes a randomness pool. The pool doesn't start out
96 * very random: that's your job to sort out. A good suggestion
97 * would be to attach an appropriate noise source and call
101 void rand_init(rand_pool *r)
104 memset(r->pool, 0, sizeof(r->pool));
105 memset(r->buf, 0, sizeof(r->buf));
108 r->ibits = r->obits = 0;
110 r->s = &noise_source;
115 /* --- @rand_noisesrc@ --- *
117 * Arguments: @rand_pool *r@ = pointer to a randomness pool
118 * @const rand_source *s@ = pointer to source definition
122 * Use: Sets a noise source for a randomness pool. When the pool's
123 * estimate of good random bits falls to zero, the @getnoise@
124 * function is called, passing the pool handle as an argument.
125 * It is expected to increase the number of good bits by at
126 * least one, because it'll be called over and over again until
127 * there are enough bits to satisfy the caller. The @timer@
128 * function is called frequently throughout the generator's
132 void rand_noisesrc(rand_pool *r, const rand_source *s)
138 /* --- @rand_seed@ --- *
140 * Arguments: @rand_pool *r@ = pointer to a randomness pool
141 * @unsigned bits@ = number of bits to ensure
145 * Use: Ensures that there are at least @bits@ good bits of entropy
146 * in the pool. It is recommended that you call this after
147 * initializing a new pool. Requesting @bits > RAND_IBITS@ is
148 * doomed to failure (and is an error).
151 void rand_seed(rand_pool *r, unsigned bits)
155 assert(((void)"bits pointlessly large in rand_seed", bits <= RAND_IBITS));
156 assert(((void)"no noise source in rand_seed", r->s));
158 while (r->ibits < bits)
163 /* --- @rand_key@ --- *
165 * Arguments: @rand_pool *r@ = pointer to a randomness pool
166 * @const void *k@ = pointer to key data
167 * @size_t sz@ = size of key data
171 * Use: Sets the secret key for a randomness pool. The key is used
172 * when mixing in new random bits.
175 void rand_key(rand_pool *r, const void *k, size_t sz)
179 static const char label[] = "Catacomb random pool key";
183 assert(HASH_SZ >= RAND_KEYSZ);
185 HASH(&hc, label, sizeof(label));
186 if (sz) HASH(&hc, k, sz);
188 memcpy(r->k.k, h, RAND_KEYSZ);
191 /* --- @rand_add@ --- *
193 * Arguments: @rand_pool *r@ = pointer to a randomness pool
194 * @const void *p@ = pointer a buffer of data to add
195 * @size_t sz@ = size of the data buffer
196 * @unsigned goodbits@ = number of good bits estimated in buffer
200 * Use: Mixes the data in the buffer with the contents of the
201 * pool. The estimate of the number of good bits is added to
202 * the pool's own count. The mixing operation is not
203 * cryptographically strong. However, data in the input pool
204 * isn't output directly, only through the one-way gating
205 * operation, so that shouldn't matter.
208 void rand_add(rand_pool *r, const void *p, size_t sz, unsigned goodbits)
213 #if RAND_POOLSZ != 128
214 # error Polynomial in rand_add is out of date. Fix it.
219 i = r->i; rot = r->irot;
223 r->pool[i] ^= (ROL8(o, rot) ^
224 r->pool[(i + 1) % RAND_POOLSZ] ^
225 r->pool[(i + 2) % RAND_POOLSZ] ^
226 r->pool[(i + 7) % RAND_POOLSZ]);
228 i++; if (i >= RAND_POOLSZ) i -= RAND_POOLSZ;
234 r->ibits += goodbits;
235 if (r->ibits > RAND_IBITS)
236 r->ibits = RAND_IBITS;
239 /* --- @rand_goodbits@ --- *
241 * Arguments: @rand_pool *r@ = pointer to a randomness pool
243 * Returns: Estimate of the number of good bits remaining in the pool.
246 unsigned rand_goodbits(rand_pool *r)
249 return (r->ibits + r->obits);
252 /* --- @rand_gate@ --- *
254 * Arguments: @rand_pool *r@ = pointer to a randomness pool
258 * Use: Mixes up the entire state of the generator in a nonreversible
262 void rand_gate(rand_pool *r)
271 /* --- Hash up all the data in the pool --- */
274 HASH(&hc, r->pool, RAND_POOLSZ);
275 HASH(&hc, r->buf, RAND_BUFSZ);
279 /* --- Now mangle all of the data based on the hash --- */
281 assert(CIPHER_KEYSZ <= HASH_SZ);
282 CIPHER_INIT(&cc, h, CIPHER_KEYSZ, 0);
283 CIPHER_ENCRYPT(&cc, r->pool, r->pool, RAND_POOLSZ);
284 CIPHER_ENCRYPT(&cc, r->buf, r->buf, RAND_BUFSZ);
287 /* --- Reset the various state variables --- */
290 r->obits += r->ibits;
291 if (r->obits > RAND_OBITS) {
292 r->ibits = r->obits - r->ibits;
293 r->obits = RAND_OBITS;
299 /* --- @rand_stretch@ --- *
301 * Arguments: @rand_pool *r@ = pointer to a randomness pool
305 * Use: Stretches the contents of the output buffer by transforming
306 * it in a nonreversible way. This doesn't add any entropy
307 * worth speaking about, but it works well enough when the
308 * caller doesn't care about that sort of thing.
311 void rand_stretch(rand_pool *r)
320 /* --- Hash up all the data in the buffer --- */
323 HASH(&hc, r->pool, RAND_POOLSZ);
324 HASH(&hc, r->buf, RAND_BUFSZ);
328 /* --- Now mangle the buffer based on the hash --- */
330 assert(CIPHER_KEYSZ < HASH_SZ);
331 CIPHER_INIT(&cc, h, CIPHER_KEYSZ, 0);
332 CIPHER_ENCRYPT(&cc, r->buf, r->buf, RAND_BUFSZ);
335 /* --- Reset the various state variables --- */
341 /* --- @rand_get@ --- *
343 * Arguments: @rand_pool *r@ = pointer to a randomness pool
344 * @void *p@ = pointer to output buffer
345 * @size_t sz@ = size of output buffer
349 * Use: Gets random data from the pool. The pool's contents can't be
350 * determined from the output of this function; nor can the
351 * output data be determined from a knowledge of the data input
352 * to the pool wihtout also having knowledge of the secret key.
353 * The good bits counter is decremented, although no special
354 * action is taken if it reaches zero.
357 void rand_get(rand_pool *r, void *p, size_t sz)
367 if (r->o + sz <= RAND_BUFSZ) {
368 memcpy(o, r->buf + r->o, sz);
372 size_t chunk = RAND_BUFSZ - r->o;
374 memcpy(o, r->buf + r->o, chunk);
382 if (r->obits > sz * 8)
388 /* --- @rand_getgood@ --- *
390 * Arguments: @rand_pool *r@ = pointer to a randomness pool
391 * @void *p@ = pointer to output buffer
392 * @size_t sz@ = size of output buffer
396 * Use: Gets random data from the pool, ensuring that there are
397 * enough good bits. This interface isn't recommended: it makes
398 * the generator slow, and doesn't provide much more security
399 * than @rand_get@, assuming you've previously done a
403 void rand_getgood(rand_pool *r, void *p, size_t sz)
411 if (!r->s || !r->s->getnoise) {
420 if (chunk * 8 > r->obits) {
421 if (chunk * 8 > r->ibits + r->obits)
422 do r->s->getnoise(r); while (r->ibits + r->obits < 256);
424 if (chunk * 8 > r->obits)
425 chunk = r->obits / 8;
428 if (chunk + r->o > RAND_BUFSZ)
429 chunk = RAND_BUFSZ - r->o;
431 memcpy(o, r->buf + r->o, chunk);
433 r->obits -= chunk * 8;
439 /*----- Generic random number generator interface -------------------------*/
441 static void gdestroy(grand *r)
444 if (g != &rand_global) {
450 static int gmisc(grand *r, unsigned op, ...)
459 switch (va_arg(ap, unsigned)) {
462 case GRAND_SEEDUINT32:
463 case GRAND_SEEDBLOCK:
480 case GRAND_SEEDINT: {
481 unsigned u = va_arg(ap, unsigned);
482 rand_add(&g->p, &u, sizeof(u), sizeof(u));
484 case GRAND_SEEDUINT32: {
485 uint32 i = va_arg(ap, uint32);
486 rand_add(&g->p, &i, sizeof(i), 4);
488 case GRAND_SEEDBLOCK: {
489 const void *p = va_arg(ap, const void *);
490 size_t sz = va_arg(ap, size_t);
491 rand_add(&g->p, p, sz, sz);
493 case GRAND_SEEDRAND: {
494 grand *rr = va_arg(ap, grand *);
496 rr->ops->fill(rr, buf, sizeof(buf));
497 rand_add(&g->p, buf, sizeof(buf), 8);
506 const void *k = va_arg(ap, const void *);
507 size_t sz = va_arg(ap, size_t);
508 rand_key(&g->p, k, sz);
511 rand_noisesrc(&g->p, va_arg(ap, const rand_source *));
514 rand_seed(&g->p, va_arg(ap, unsigned));
520 rc = rand_goodbits(&g->p);
523 const void *p = va_arg(ap, const void *);
524 size_t sz = va_arg(ap, size_t);
525 unsigned goodbits = va_arg(ap, unsigned);
526 rand_add(&g->p, p, sz, goodbits);
537 static octet gbyte(grand *r)
541 rand_getgood(&g->p, &o, 1);
545 static uint32 gword(grand *r)
549 rand_getgood(&g->p, &b, sizeof(b));
553 static void gfill(grand *r, void *p, size_t sz)
556 rand_get(&g->p, p, sz);
559 static const grand_ops gops = {
563 gword, gbyte, gword, grand_range, gfill
566 /* --- @rand_create@ --- *
570 * Returns: Pointer to a generic generator.
572 * Use: Constructs a generic generator interface over a Catacomb
573 * entropy pool generator.
576 grand *rand_create(void)
578 gctx *g = S_CREATE(gctx);
584 /*----- That's all, folks -------------------------------------------------*/