chiark / gitweb /
rand/noise.c: Make the high-res timer function be a bit more abstract.
[catacomb] / rand / noise.c
index 3969b4e441b03cc30cf5edaff4b44b3d2c6573e6..f01af65187d639e6c3cf5b71b0429b100e022f68 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "config.h"
 
+#include <errno.h>
 #include <setjmp.h>
 #include <signal.h>
 #include <stdio.h>
 #  include <grp.h>
 #endif
 
+#if defined(HAVE_LINUX_RANDOM_H)
+#  include <linux/random.h>
+#  include <sys/syscall.h>
+#endif
+
 #include <mLib/bits.h>
 #include <mLib/mdup.h>
 #include <mLib/sel.h>
 /*----- Magical numbers ---------------------------------------------------*/
 
 #define NOISE_KIDLIFE 100000           /* @noise_filter@ child lifetime */
-#define MILLION 1000000                        /* One million */
+
+#  define TIMESTRUCT timeval
+#  define tv_SEC tv_sec
+#  define tv_FRAC tv_usec
+#  define TIMERES 1000000
+#  define GETTIME(tv) (gettimeofday((tv), 0))
+#  define TOTIMEVAL(tv, xx) (*(tv) = *(xx))
 
 /*----- Noise source definition -------------------------------------------*/
 
@@ -87,8 +99,8 @@ static gid_t noise_gid = NOISE_NOSETGID; /* Gid to set to spawn processes */
 
 static int bitcount(unsigned long x)
 {
-  char ctab[] = { 0, 1, 1, 2, 1, 2, 2, 3,
-                 1, 2, 2, 3, 2, 3, 3, 4 };
+  static const char ctab[] = { 0, 1, 1, 2, 1, 2, 2, 3,
+                              1, 2, 2, 3, 2, 3, 3, 4 };
   int count = 0;
   while (x) {
     count += ctab[x & 0xfu];
@@ -100,20 +112,20 @@ static int bitcount(unsigned long x)
 /* --- @timer@ --- *
  *
  * Arguments:  @rand_pool *r@ = pointer to randomness pool
- *             @struct timeval *tv@ = pointer to time block
+ *             @const struct TIMESTRUCT *tv@ = pointer to time block
  *
  * Returns:    Nonzero if some randomness was contributed.
  *
  * Use:                Low-level timer contributor.
  */
 
-static int timer(rand_pool *r, struct timeval *tv)
+static int timer(rand_pool *r, const struct TIMESTRUCT *tv)
 {
   unsigned long x, d, dd;
   int de, dde;
   int ret;
 
-  x = tv->tv_usec + MILLION * tv->tv_sec;
+  x = tv->tv_FRAC + TIMERES*tv->tv_SEC;
   d = x ^ noise_last;
   dd = d ^ noise_diff;
   noise_last = x;
@@ -140,9 +152,8 @@ static int timer(rand_pool *r, struct timeval *tv)
 
 int noise_timer(rand_pool *r)
 {
-  struct timeval tv;
-  gettimeofday(&tv, 0);
-  return (timer(r, &tv));
+  struct TIMESTRUCT tv;
+  GETTIME(&tv); return (timer(r, &tv));
 }
 
 /* --- @noise_devrandom@ --- *
@@ -157,11 +168,57 @@ int noise_timer(rand_pool *r)
 
 int noise_devrandom(rand_pool *r)
 {
-  int fd;
+  int fd = -1;
   octet buf[RAND_POOLSZ];
   ssize_t len;
   size_t n = 0;
   int ret = 0;
+#ifdef __linux__
+  fd_set infd;
+  struct timeval tv = { 0, 0 };
+#endif
+#ifdef HAVE_GETENTROPY
+  size_t nn;
+#endif
+
+#if defined(HAVE_LINUX_RANDOM_H) &&                                    \
+    defined(GRND_NONBLOCK) &&                                          \
+    defined(SYS_getrandom)
+  /* --- Use the new shinies if available --- */
+
+  while (n < sizeof(buf)) {
+    if ((len = syscall(SYS_getrandom, buf + n, sizeof(buf) - n,
+                      GRND_NONBLOCK)) <= 0) {
+      if (errno == ENOSYS) break;
+      else goto done;
+    }
+    n += len;
+  }
+  if (n == sizeof(buf)) goto win;
+#endif
+
+#ifdef HAVE_GETENTROPY
+  /* --- OpenBSD-flavoured shinies --- */
+
+  while (n < sizeof(buf)) {
+    nn = sizeof(buf) - nn;
+    if (nn > 256) nn = 256;
+    if (getentropy(buf + n, nn)) break;
+    n += nn;
+  }
+  if (n == sizeof(buf)) goto win;
+#endif
+
+#ifdef __linux__
+  /* --- Don't take from `/dev/urandom' if `/dev/random' would block --- */
+
+  if ((fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) < 0) goto done;
+  FD_ZERO(&infd);
+  FD_SET(fd, &infd);
+  if (select(fd + 1, &infd, 0, 0, &tv) < 0 || !FD_ISSET(fd, &infd))
+    goto done;
+  close(fd); fd = -1;
+#endif
 
   /* --- Be nice to other clients of the random device --- *
    *
@@ -171,18 +228,24 @@ int noise_devrandom(rand_pool *r)
    * needs to get some more entropy from somewhere.
    */
 
-  if ((fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
+  if (fd >= 0 ||
+      (fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
       (fd = open("/dev/arandom", O_RDONLY | O_NONBLOCK)) >= 0 ||
       (fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) >= 0) {
     while (n < sizeof(buf)) {
       if ((len = read(fd, buf + n, sizeof(buf) - n)) <= 0) break;
       n += len;
     }
-    rand_add(r, buf, n, n * 8);
-    BURN(buf);
-    if (n == sizeof(buf)) ret = 1;
-    close(fd);
+    if (n == sizeof(buf)) goto win;
   }
+  goto done;
+
+win:
+  ret = 1;
+done:
+  if (fd >= 0) close(fd);
+  rand_add(r, buf, n, 8*n);
+  BURN(buf);
   noise_timer(r);
   return (ret);
 }
@@ -261,6 +324,7 @@ int noise_filter(rand_pool *r, int good, const char *c)
   pid_t kid;
   int fd[2];
   struct timeval dead;
+  struct TIMESTRUCT now;
   int ret = 0;
   struct noisekid nk = { 0 };
   sel_state sel;
@@ -273,8 +337,8 @@ int noise_filter(rand_pool *r, int good, const char *c)
 
   /* --- Remember when this business started --- */
 
-  gettimeofday(&dead, 0);
-  timer(r, &dead);
+  GETTIME(&now); timer(r, &now);
+  TOTIMEVAL(&dead, &now);
 
   /* --- Create a pipe --- */