da07974cf27dcd09bfaab6ea406c0c2ca18b3744
3 * $Id: rand.c,v 1.3 1999/12/10 23:28:07 mdw Exp $
5 * Secure random number generator
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 1999/12/10 23:28:07 mdw
34 * Bug fix: rand_getgood didn't update buffer pointer.
36 * Revision 1.2 1999/10/12 21:00:15 mdw
37 * Make pool and buffer sizes more sensible.
39 * Revision 1.1 1999/09/03 08:41:12 mdw
44 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/bits.h>
53 #include "blowfish-cbc.h"
57 #include "rmd160-hmac.h"
59 /*----- Static variables --------------------------------------------------*/
61 static const grand_ops gops
;
68 static gctx pool
= { { &gops
} }; /* Default random pool */
70 /*----- Macros ------------------------------------------------------------*/
72 #define RAND_RESOLVE(r) do { \
73 if ((r) == RAND_GLOBAL) \
77 #define TIMER(r) do { \
78 if ((r)->s && (r)->s->timer) \
82 /*----- Main code ---------------------------------------------------------*/
84 /* --- @rand_init@ --- *
86 * Arguments: @rand_pool *r@ = pointer to a randomness pool
90 * Use: Initializes a randomness pool. The pool doesn't start out
91 * very random: that's your job to sort out.
94 void rand_init(rand_pool
*r
)
97 memset(r
->pool
, 0, sizeof(r
->pool
));
98 memset(r
->buf
, 0, sizeof(r
->buf
));
101 r
->ibits
= r
->obits
= 0;
104 rmd160_hmacinit(&r
->k
, 0, 0);
108 /* --- @rand_noisesrc@ --- *
110 * Arguments: @rand_pool *r@ = pointer to a randomness pool
111 * @const rand_source *s@ = pointer to source definition
115 * Use: Sets a noise source for a randomness pool. When the pool's
116 * estimate of good random bits falls to zero, the @getnoise@
117 * function is called, passing the pool handle as an argument.
118 * It is expected to increase the number of good bits by at
119 * least one, because it'll be called over and over again until
120 * there are enough bits to satisfy the caller. The @timer@
121 * function is called frequently throughout the generator's
125 void rand_noisesrc(rand_pool
*r
, const rand_source
*s
)
131 /* --- @rand_key@ --- *
133 * Arguments: @rand_pool *r@ = pointer to a randomness pool
134 * @const void *k@ = pointer to key data
135 * @size_t sz@ = size of key data
139 * Use: Sets the secret key for a randomness pool. The key is used
140 * when mixing in new random bits.
143 void rand_key(rand_pool
*r
, const void *k
, size_t sz
)
146 rmd160_hmacinit(&r
->k
, k
, sz
);
149 /* --- @rand_add@ --- *
151 * Arguments: @rand_pool *r@ = pointer to a randomness pool
152 * @const void *p@ = pointer a buffer of data to add
153 * @size_t sz@ = size of the data buffer
154 * @unsigned goodbits@ = number of good bits estimated in buffer
158 * Use: Mixes the data in the buffer with the contents of the
159 * pool. The estimate of the number of good bits is added to
160 * the pool's own count. The mixing operation is not
161 * cryptographically strong. However, data in the input pool
162 * isn't output directly, only through the one-way gating
163 * operation, so that shouldn't matter.
166 void rand_add(rand_pool
*r
, const void *p
, size_t sz
, unsigned goodbits
)
171 #if RAND_POOLSZ != 128
172 # error Polynomial in rand_add is out of date. Fix it.
177 i
= r
->i
; rot
= r
->irot
;
181 r
->pool
[i
] ^= (ROL8(o
, rot
) ^
182 r
->pool
[(i
+ 1) % RAND_POOLSZ
] ^
183 r
->pool
[(i
+ 2) % RAND_POOLSZ
] ^
184 r
->pool
[(i
+ 7) % RAND_POOLSZ
]);
186 i
++; if (i
>= RAND_POOLSZ
) i
-= RAND_POOLSZ
;
192 r
->ibits
+= goodbits
;
193 if (r
->ibits
> RAND_IBITS
)
194 r
->ibits
= RAND_IBITS
;
197 /* --- @rand_goodbits@ --- *
199 * Arguments: @rand_pool *r@ = pointer to a randomness pool
201 * Returns: Estimate of the number of good bits remaining in the pool.
204 unsigned rand_goodbits(rand_pool
*r
)
207 return (r
->ibits
+ r
->obits
);
210 /* --- @rand_gate@ --- *
212 * Arguments: @rand_pool *r@ = pointer to a randomness pool
216 * Use: Mixes up the entire state of the generator in a nonreversible
220 void rand_gate(rand_pool
*r
)
222 octet mac
[RMD160_HASHSZ
];
227 /* --- Hash up all the data in the pool --- */
232 rmd160_macinit(&mc
, &r
->k
);
233 rmd160_machash(&mc
, r
->pool
, sizeof(r
->pool
));
234 rmd160_machash(&mc
, r
->buf
, sizeof(r
->buf
));
235 rmd160_macdone(&mc
, mac
);
239 /* --- Now mangle all of the data based on the hash --- */
244 blowfish_cbcinit(&bc
, mac
, sizeof(mac
), 0);
245 blowfish_cbcencrypt(&bc
, r
->pool
, r
->pool
, sizeof(r
->pool
));
246 blowfish_cbcencrypt(&bc
, r
->buf
, r
->buf
, sizeof(r
->buf
));
250 /* --- Reset the various state variables --- */
253 r
->obits
+= r
->ibits
;
254 if (r
->obits
> RAND_OBITS
) {
255 r
->ibits
= r
->obits
- r
->ibits
;
256 r
->obits
= RAND_OBITS
;
262 /* --- @rand_stretch@ --- *
264 * Arguments: @rand_pool *r@ = pointer to a randomness pool
268 * Use: Stretches the contents of the output buffer by transforming
269 * it in a nonreversible way. This doesn't add any entropy
270 * worth speaking about, but it works well enough when the
271 * caller doesn't care about that sort of thing.
274 void rand_stretch(rand_pool
*r
)
276 octet mac
[RMD160_HASHSZ
];
281 /* --- Hash up all the data in the buffer --- */
286 rmd160_macinit(&mc
, &r
->k
);
287 rmd160_machash(&mc
, r
->pool
, sizeof(r
->pool
));
288 rmd160_machash(&mc
, r
->buf
, sizeof(r
->buf
));
289 rmd160_macdone(&mc
, mac
);
293 /* --- Now mangle the buffer based on that hash --- */
298 blowfish_cbcinit(&bc
, mac
, sizeof(mac
), 0);
299 blowfish_cbcencrypt(&bc
, r
->buf
, r
->buf
, sizeof(r
->buf
));
303 /* --- Reset the various state variables --- */
309 /* --- @rand_get@ --- *
311 * Arguments: @rand_pool *r@ = pointer to a randomness pool
312 * @void *p@ = pointer to output buffer
313 * @size_t sz@ = size of output buffer
317 * Use: Gets random data from the pool. The pool's contents can't be
318 * determined from the output of this function; nor can the
319 * output data be determined from a knowledge of the data input
320 * to the pool wihtout also having knowledge of the secret key.
321 * The good bits counter is decremented, although no special
322 * action is taken if it reaches zero.
325 void rand_get(rand_pool
*r
, void *p
, size_t sz
)
335 if (r
->o
+ sz
<= RAND_BUFSZ
) {
336 memcpy(o
, r
->buf
+ r
->o
, sz
);
340 size_t chunk
= RAND_BUFSZ
- r
->o
;
342 memcpy(o
, r
->buf
+ r
->o
, chunk
);
350 if (r
->obits
> sz
* 8)
356 /* --- @rand_getgood@ --- *
358 * Arguments: @rand_pool *r@ = pointer to a randomness pool
359 * @void *p@ = pointer to output buffer
360 * @size_t sz@ = size of output buffer
364 * Use: Gets random data from the pool. The pool's contents can't be
365 * determined from the output of this function; nor can the
366 * output data be determined from a knowledge of the data input
367 * to the pool wihtout also having knowledge of the secret key.
368 * If a noise source is attached to the pool in question, it is
369 * called to replenish the supply of good bits in the pool;
370 * otherwise this call is equivalent to @rand_get@.
373 void rand_getgood(rand_pool
*r
, void *p
, size_t sz
)
381 if (!r
->s
|| !r
->s
->getnoise
) {
390 if (chunk
* 8 > r
->obits
) {
391 if (chunk
* 8 > r
->ibits
+ r
->obits
)
392 do r
->s
->getnoise(r
); while (r
->ibits
+ r
->obits
< 128);
394 if (chunk
* 8 > r
->obits
)
395 chunk
= r
->obits
/ 8;
398 if (chunk
+ r
->o
> RAND_BUFSZ
)
399 chunk
= RAND_BUFSZ
- r
->o
;
401 memcpy(o
, r
->buf
+ r
->o
, chunk
);
403 r
->obits
-= chunk
* 8;
409 /*----- Generic random number generator interface -------------------------*/
411 static void gdestroy(grand
*r
)
413 gctx
*g
= (r
== &rand_global ?
&pool
: (gctx
*)r
);
418 static int gmisc(grand
*r
, unsigned op
, ...)
420 gctx
*g
= (r
== &rand_global ?
&pool
: (gctx
*)r
);
427 switch (va_arg(ap
, unsigned)) {
430 case GRAND_SEEDUINT32
:
431 case GRAND_SEEDBLOCK
:
443 case GRAND_SEEDINT
: {
444 unsigned u
= va_arg(ap
, unsigned);
445 rand_add(&g
->p
, &u
, sizeof(u
), sizeof(u
));
447 case GRAND_SEEDUINT32
: {
448 uint32 i
= va_arg(ap
, uint32
);
449 rand_add(&g
->p
, &i
, sizeof(i
), 4);
451 case GRAND_SEEDBLOCK
: {
452 const void *p
= va_arg(ap
, const void *);
453 size_t sz
= va_arg(ap
, size_t);
454 rand_add(&g
->p
, p
, sz
, sz
);
463 const void *k
= va_arg(ap
, const void *);
464 size_t sz
= va_arg(ap
, size_t);
465 rand_key(&g
->p
, k
, sz
);
468 rand_noisesrc(&g
->p
, va_arg(ap
, const rand_source
*));
479 static octet
gbyte(grand
*r
)
481 gctx
*g
= (r
== &rand_global ?
&pool
: (gctx
*)r
);
483 rand_getgood(&g
->p
, &o
, 1);
487 static uint32
gword(grand
*r
)
489 gctx
*g
= (r
== &rand_global ?
&pool
: (gctx
*)r
);
491 rand_getgood(&g
->p
, &b
, sizeof(b
));
495 static void gfill(grand
*r
, void *p
, size_t sz
)
497 gctx
*g
= (r
== &rand_global ?
&pool
: (gctx
*)r
);
498 rand_getgood(&g
->p
, p
, sz
);
501 static const grand_ops gops
= {
505 gword
, gbyte
, gword
, grand_range
, gfill
508 grand rand_global
= { &gops
};
510 /* --- @rand_create@ --- *
514 * Returns: Pointer to a generic generator.
516 * Use: Constructs a generic generator interface over a Catacomb
517 * entropy pool generator.
520 grand
*rand_create(void)
522 gctx
*g
= CREATE(gctx
);
528 /*----- That's all, folks -------------------------------------------------*/