From f1da6683c88aa279a6350122d96856185d581925 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 26 May 2016 09:26:09 +0100 Subject: [PATCH] rand/rand.[ch]: Add external `rand_quick' function. This is a function which can be called frequently to top up the internal entropy. Internally, rename `TIMER' to `QUICK', and call the internal `quick' function as well as the noise-source timer. The `quick' core currently doesn't do anything, but will act as a dispatcher to CPU-specific entropy sources. --- rand/rand.c | 47 +++++++++++++++++++++++++++++++++++++---------- rand/rand.h | 13 +++++++++++++ 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/rand/rand.c b/rand/rand.c index 8de66079..aa3fb453 100644 --- a/rand/rand.c +++ b/rand/rand.c @@ -27,6 +27,8 @@ /*----- Header files ------------------------------------------------------*/ +#include "config.h" + #include #include #include @@ -35,6 +37,7 @@ #include #include "arena.h" +#include "dispatch.h" #include "paranoia.h" #define RAND__HACKS @@ -84,9 +87,10 @@ gctx rand_global = { if (r->gen != gen) { r->gen = gen; rand_gate(r); } \ } while (0) -#define TIMER(r) do { \ - if ((r)->s && (r)->s->timer) \ - (r)->s->timer(r); \ +static int quick(rand_pool *); +#define QUICK(r) do { \ + quick(r); \ + if ((r)->s && (r)->s->timer) (r)->s->timer(r); \ } while (0) /*----- Main code ---------------------------------------------------------*/ @@ -141,6 +145,29 @@ void rand_noisesrc(rand_pool *r, const rand_source *s) r->s = s; } +/* --- @rand_quick@ --- * + * + * Arguments: @rand_pool *r@ = pointer to a randomness pool + * + * Returns: Zero on success; @-1@ on failure. + * + * Use Attempts to use some machine-specific `quick' source of + * entropy to top up @r@. This may not do anything at all on + * many systems. + */ + +CPU_DISPATCH(static, return, int, quick, (rand_pool *r), (r), + pick_quick, trivial_quick); + +static int trivial_quick(rand_pool *r) { return (-1); } + +static quick__functype *pick_quick(void) +{ + DISPATCH_PICK_FALLBACK(rand_quick, trivial_quick); +} + +int rand_quick(rand_pool *r) { RAND_RESOLVE(r); return (quick(r)); } + /* --- @rand_seed@ --- * * * Arguments: @rand_pool *r@ = pointer to a randomness pool @@ -272,7 +299,7 @@ void rand_gate(rand_pool *r) CIPHER_CTX cc; RAND_RESOLVE(r); - TIMER(r); + QUICK(r); /* --- Hash up all the data in the pool --- */ @@ -300,7 +327,7 @@ void rand_gate(rand_pool *r) r->obits = RAND_OBITS; } else r->ibits = 0; - TIMER(r); + QUICK(r); } /* --- @rand_stretch@ --- * @@ -322,7 +349,7 @@ void rand_stretch(rand_pool *r) CIPHER_CTX cc; RAND_RESOLVE(r); - TIMER(r); + QUICK(r); /* --- Hash up all the data in the buffer --- */ @@ -343,7 +370,7 @@ void rand_stretch(rand_pool *r) /* --- Reset the various state variables --- */ r->o = RAND_SECSZ; - TIMER(r); + QUICK(r); } /* --- @rand_get@ --- * @@ -368,7 +395,7 @@ void rand_get(rand_pool *r, void *p, size_t sz) RAND_RESOLVE(r); GENCHECK(r); - TIMER(r); + QUICK(r); if (!sz) return; @@ -422,7 +449,7 @@ void rand_getgood(rand_pool *r, void *p, size_t sz) return; } GENCHECK(r); - TIMER(r); + QUICK(r); while (sz) { size_t chunk = sz; @@ -524,7 +551,7 @@ static int gmisc(grand *r, unsigned op, ...) rand_seed(&g->p, va_arg(ap, unsigned)); break; case RAND_TIMER: - TIMER(&g->p); + QUICK(&g->p); break; case RAND_GOODBITS: rc = rand_goodbits(&g->p); diff --git a/rand/rand.h b/rand/rand.h index ab1c4a8b..5584c54d 100644 --- a/rand/rand.h +++ b/rand/rand.h @@ -195,6 +195,19 @@ extern void rand_seed(rand_pool */*r*/, unsigned /*bits*/); extern void rand_key(rand_pool */*r*/, const void */*k*/, size_t /*sz*/); +/* --- @rand_quick@ --- * + * + * Arguments: @rand_pool *r@ = pointer to a randomness pool + * + * Returns: Zero on success; @-1@ on failure. + * + * Use Attempts to use some machine-specific `quick' source of + * entropy to top up @r@. This may not do anything at all on + * many systems. + */ + +extern int rand_quick(rand_pool */*r*/); + /* --- @rand_add@ --- * * * Arguments: @rand_pool *r@ = pointer to a randomness pool -- 2.11.0