From: Mark Wooding Date: Mon, 22 Dec 2014 20:32:58 +0000 (+0000) Subject: rand/rand.[ch]: Don't dynamically construct the global generator. X-Git-Tag: 2.2.0~7^2~12 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/catacomb/commitdiff_plain/a7a76ebd30d6af9302fa58b4acad09c465cbac78 rand/rand.[ch]: Don't dynamically construct the global generator. Now it always exists, and is statically initialized. There should be no observable change. --- diff --git a/rand/rand.c b/rand/rand.c index fa6dab86..be39d798 100644 --- a/rand/rand.c +++ b/rand/rand.c @@ -37,6 +37,8 @@ #include "arena.h" #include "blowfish-cbc.h" #include "paranoia.h" + +#define RAND__HACKS #include "rand.h" #include "rmd160.h" #include "rmd160-hmac.h" @@ -45,22 +47,28 @@ static const grand_ops gops; -typedef struct gctx { +typedef struct rand__gctx { grand r; rand_pool p; } gctx; -static gctx *pool = 0; /* Default random pool */ +gctx rand_global = { + { &gops }, + { { 0 }, 0, 0, 0, + { 0 }, RAND_SECSZ, 0, + { { 0xb7, 0xb0, 0xb4, 0xdb, 0x59, 0x75, 0x49, 0x32, + 0x1a, 0x8d, 0x4b, 0x86, 0x3a, 0x38, 0xfd, 0x59, + 0xc1, 0x63, 0x66, 0xd9 }, 64, + { 0x91, 0x9a, 0xe6, 0xa1, 0x9d, 0x3a, 0x86, 0xef, + 0xb2, 0xb9, 0xca, 0xfc, 0x26, 0xf8, 0xb1, 0x04, + 0x4a, 0x41, 0xc4, 0x7a }, 64 }, + 0 } +}; /*----- Macros ------------------------------------------------------------*/ -#define RAND_RESOLVE(r) do { \ - if ((r) == RAND_GLOBAL) { \ - if (!pool) \ - pool = (gctx *)rand_create(); \ - (r) = &pool->p; \ - } \ -} while (0) +#define RAND_RESOLVE(r) \ + do { if ((r) == RAND_GLOBAL) r = &rand_global.p; } while (0) #define TIMER(r) do { \ if ((r)->s && (r)->s->timer) \ @@ -421,21 +429,10 @@ void rand_getgood(rand_pool *r, void *p, size_t sz) /*----- 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; - GRESOLVE(g, r); - if (g != pool) { + gctx *g = (gctx *)r; + if (g != &rand_global) { BURN(*g); S_DESTROY(g); } @@ -443,12 +440,11 @@ static void gdestroy(grand *r) static int gmisc(grand *r, unsigned op, ...) { - gctx *g; + gctx *g = (gctx *)r; va_list ap; int rc = 0; va_start(ap, op); - GRESOLVE(g, r); switch (op) { case GRAND_CHECK: switch (va_arg(ap, unsigned)) { @@ -531,26 +527,23 @@ static int gmisc(grand *r, unsigned op, ...) static octet gbyte(grand *r) { - gctx *g; + gctx *g = (gctx *)r; octet o; - GRESOLVE(g, r); rand_getgood(&g->p, &o, 1); return (o); } static uint32 gword(grand *r) { - gctx *g; + gctx *g = (gctx *)r; 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; - GRESOLVE(g, r); + gctx *g = (gctx *)r; rand_get(&g->p, p, sz); } @@ -561,8 +554,6 @@ static const grand_ops gops = { gword, gbyte, gword, grand_range, gfill }; -grand rand_global = { &gops }; - /* --- @rand_create@ --- * * * Arguments: --- diff --git a/rand/rand.h b/rand/rand.h index a024c0f4..389526d2 100644 --- a/rand/rand.h +++ b/rand/rand.h @@ -290,7 +290,11 @@ enum { /* --- Default random number generator --- */ -extern grand rand_global; +#ifdef RAND__HACKS + extern struct rand__gctx rand_global; +#else + extern grand rand_global; +#endif /* --- @rand_create@ --- * *