chiark / gitweb /
rand/noise.c (noise_devrandom): Use OpenBSD system call `getentropy'.
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:30 +0000 (14:55 +0100)
If it's available, it does the right thing.  I think.

configure.ac
rand/noise.c

index 631f78383d49a0bf4985f96784dd025103766068..7887da325ded1da8e052ca7a9362adaffe9b76e2 100644 (file)
@@ -222,6 +222,7 @@ AC_SUBST([limits])
 dnl Functions used for noise-gathering.
 AC_CHECK_FUNCS([setgroups])
 AC_CHECK_HEADERS([linux/random.h])
+AC_CHECK_FUNCS([getentropy])
 AC_CACHE_CHECK([whether the freewheel noise generator will work],
        [catacomb_cv_freewheel],
 [AC_TRY_LINK(
index b59fd8ad99f4e2cbfe3de26c7a26964c815219c7..2c30c13c0db79b570ae94f8b09b7ac81fcf3b169 100644 (file)
@@ -172,6 +172,9 @@ int noise_devrandom(rand_pool *r)
   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) &&                                          \
@@ -189,6 +192,18 @@ int noise_devrandom(rand_pool *r)
   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 --- */