From 6d4416ccb0bbe02105c4b9dfa1442b9198c0d923 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 26 May 2016 09:26:09 +0100 Subject: [PATCH] rand/noise.c (noise_devrandom): Use OpenBSD system call `getentropy'. If it's available, it does the right thing. I think. --- configure.ac | 1 + rand/noise.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/configure.ac b/configure.ac index 631f7838..7887da32 100644 --- a/configure.ac +++ b/configure.ac @@ -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( diff --git a/rand/noise.c b/rand/noise.c index b59fd8ad..2c30c13c 100644 --- a/rand/noise.c +++ b/rand/noise.c @@ -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 --- */ -- 2.11.0