rand/rand.[ch]: Add external `rand_quick' function.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 26 May 2016 08:26:09 +0000 (09:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 4 Jun 2016 13:55:22 +0000 (14:55 +0100)
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
rand/rand.h

index 8de6607..aa3fb45 100644 (file)
@@ -27,6 +27,8 @@
 
 /*----- Header files ------------------------------------------------------*/
 
+#include "config.h"
+
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
@@ -35,6 +37,7 @@
 #include <mLib/sub.h>
 
 #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);
index ab1c4a8..5584c54 100644 (file)
@@ -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