/* -*-c-*-
*
- * $Id: rand.c,v 1.3 1999/12/10 23:28:07 mdw Exp $
+ * $Id$
*
* Secure random number generator
*
* (c) 1998 Straylight/Edgeware
*/
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------*
*
* This file is part of Catacomb.
*
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
- *
+ *
* Catacomb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Library General Public
* License along with Catacomb; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: rand.c,v $
- * Revision 1.3 1999/12/10 23:28:07 mdw
- * Bug fix: rand_getgood didn't update buffer pointer.
- *
- * Revision 1.2 1999/10/12 21:00:15 mdw
- * Make pool and buffer sizes more sensible.
- *
- * Revision 1.1 1999/09/03 08:41:12 mdw
- * Initial import.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include <stdarg.h>
#include <mLib/bits.h>
#include <mLib/sub.h>
+#include "arena.h"
#include "blowfish-cbc.h"
#include "paranoia.h"
#include "rand.h"
rand_pool p;
} gctx;
-static gctx pool = { { &gops } }; /* Default random pool */
+static gctx *pool = 0; /* Default random pool */
/*----- Macros ------------------------------------------------------------*/
#define RAND_RESOLVE(r) do { \
- if ((r) == RAND_GLOBAL) \
- (r) = &pool.p; \
+ if ((r) == RAND_GLOBAL) { \
+ if (!pool) \
+ pool = (gctx *)rand_create(); \
+ (r) = &pool->p; \
+ } \
} while (0)
#define TIMER(r) do { \
* Returns: ---
*
* Use: Initializes a randomness pool. The pool doesn't start out
- * very random: that's your job to sort out.
+ * very random: that's your job to sort out. A good suggestion
+ * would be to attach an appropriate noise source and call
+ * @rand_seed@.
*/
void rand_init(rand_pool *r)
r->s = s;
}
+/* --- @rand_seed@ --- *
+ *
+ * Arguments: @rand_pool *r@ = pointer to a randomness pool
+ * @unsigned bits@ = number of bits to ensure
+ *
+ * Returns: ---
+ *
+ * Use: Ensures that there are at least @bits@ good bits of entropy
+ * in the pool. It is recommended that you call this after
+ * initializing a new pool. Requesting @bits > RAND_IBITS@ is
+ * doomed to failure (and is an error).
+ */
+
+void rand_seed(rand_pool *r, unsigned bits)
+{
+ RAND_RESOLVE(r);
+
+ assert(((void)"bits pointlessly large in rand_seed", bits <= RAND_IBITS));
+ assert(((void)"no noise source in rand_seed", r->s));
+
+ while (r->ibits < bits)
+ r->s->getnoise(r);
+ rand_gate(r);
+}
+
/* --- @rand_key@ --- *
*
* Arguments: @rand_pool *r@ = pointer to a randomness pool
/* --- @rand_goodbits@ --- *
*
- * Arguments: @rand_pool *r@ = pointer to a randomness pool
+ * Arguments: @rand_pool *r@ = pointer to a randomness pool
*
- * Returns: Estimate of the number of good bits remaining in the pool.
+ * Returns: Estimate of the number of good bits remaining in the pool.
*/
unsigned rand_goodbits(rand_pool *r)
*
* Returns: ---
*
- * Use: Gets random data from the pool. The pool's contents can't be
- * determined from the output of this function; nor can the
- * output data be determined from a knowledge of the data input
- * to the pool wihtout also having knowledge of the secret key.
- * If a noise source is attached to the pool in question, it is
- * called to replenish the supply of good bits in the pool;
- * otherwise this call is equivalent to @rand_get@.
+ * Use: Gets random data from the pool, ensuring that there are
+ * enough good bits. This interface isn't recommended: it makes
+ * the generator slow, and doesn't provide much more security
+ * than @rand_get@, assuming you've previously done a
+ * @rand_seed@.
*/
void rand_getgood(rand_pool *r, void *p, size_t sz)
if (chunk * 8 > r->obits) {
if (chunk * 8 > r->ibits + r->obits)
- do r->s->getnoise(r); while (r->ibits + r->obits < 128);
+ do r->s->getnoise(r); while (r->ibits + r->obits < 256);
rand_gate(r);
if (chunk * 8 > r->obits)
chunk = r->obits / 8;
/*----- Generic random number generator interface -------------------------*/
+#define GRESOLVE(g, r) do { \
+ if (r != &rand_global) \
+ g = (gctx *)r; \
+ else { \
+ if (!pool) \
+ pool = (gctx *)rand_create(); \
+ g = pool; \
+ } \
+} while (0)
+
static void gdestroy(grand *r)
{
- gctx *g = (r == &rand_global ? &pool : (gctx *)r);
- if (g != &pool)
- DESTROY(g);
+ gctx *g;
+ GRESOLVE(g, r);
+ if (g != pool) {
+ BURN(*g);
+ S_DESTROY(g);
+ }
}
static int gmisc(grand *r, unsigned op, ...)
{
- gctx *g = (r == &rand_global ? &pool : (gctx *)r);
+ gctx *g;
va_list ap;
int rc = 0;
va_start(ap, op);
+ GRESOLVE(g, r);
switch (op) {
case GRAND_CHECK:
switch (va_arg(ap, unsigned)) {
case GRAND_SEEDINT:
case GRAND_SEEDUINT32:
case GRAND_SEEDBLOCK:
+ case GRAND_SEEDRAND:
case RAND_GATE:
case RAND_STRETCH:
case RAND_KEY:
case RAND_NOISESRC:
+ case RAND_SEED:
+ case RAND_TIMER:
+ case RAND_GOODBITS:
+ case RAND_ADD:
rc = 1;
break;
default:
size_t sz = va_arg(ap, size_t);
rand_add(&g->p, p, sz, sz);
} break;
+ case GRAND_SEEDRAND: {
+ grand *rr = va_arg(ap, grand *);
+ octet buf[16];
+ rr->ops->fill(rr, buf, sizeof(buf));
+ rand_add(&g->p, buf, sizeof(buf), 8);
+ } break;
case RAND_GATE:
rand_gate(&g->p);
break;
case RAND_NOISESRC:
rand_noisesrc(&g->p, va_arg(ap, const rand_source *));
break;
+ case RAND_SEED:
+ rand_seed(&g->p, va_arg(ap, unsigned));
+ break;
+ case RAND_TIMER:
+ TIMER(&g->p);
+ break;
+ case RAND_GOODBITS:
+ rc = rand_goodbits(&g->p);
+ break;
+ case RAND_ADD: {
+ const void *p = va_arg(ap, const void *);
+ size_t sz = va_arg(ap, size_t);
+ unsigned goodbits = va_arg(ap, unsigned);
+ rand_add(&g->p, p, sz, goodbits);
+ } break;
default:
GRAND_BADOP;
break;
static octet gbyte(grand *r)
{
- gctx *g = (r == &rand_global ? &pool : (gctx *)r);
+ gctx *g;
octet o;
+ GRESOLVE(g, r);
rand_getgood(&g->p, &o, 1);
return (o);
}
static uint32 gword(grand *r)
{
- gctx *g = (r == &rand_global ? &pool : (gctx *)r);
+ gctx *g;
octet b[4];
+ GRESOLVE(g, r);
rand_getgood(&g->p, &b, sizeof(b));
return (LOAD32(b));
}
static void gfill(grand *r, void *p, size_t sz)
{
- gctx *g = (r == &rand_global ? &pool : (gctx *)r);
- rand_getgood(&g->p, p, sz);
+ gctx *g;
+ GRESOLVE(g, r);
+ rand_get(&g->p, p, sz);
}
static const grand_ops gops = {
"rand",
- 0,
+ GRAND_CRYPTO, 0,
gmisc, gdestroy,
gword, gbyte, gword, grand_range, gfill
};
grand *rand_create(void)
{
- gctx *g = CREATE(gctx);
+ gctx *g = S_CREATE(gctx);
g->r.ops = &gops;
rand_init(&g->p);
return (&g->r);