Skip to content

Commit ee2d10a

Browse files
committed
Check if sys/random.h is required for getentropy on OSX.
1 parent ba1bbb0 commit ee2d10a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

configure.ac

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,14 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>]],
675675
[ AC_MSG_RESULT(no)]
676676
)
677677

678+
AC_MSG_CHECKING(for getentropy via random.h)
679+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
680+
#include <sys/random.h>]],
681+
[[ getentropy(nullptr, 32) ]])],
682+
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GETENTROPY_RAND, 1,[Define this symbol if the BSD getentropy system call is available with sys/random.h]) ],
683+
[ AC_MSG_RESULT(no)]
684+
)
685+
678686
AC_MSG_CHECKING(for sysctl KERN_ARND)
679687
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
680688
#include <sys/sysctl.h>]],

src/random.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
#include <sys/syscall.h>
2828
#include <linux/random.h>
2929
#endif
30-
#ifdef HAVE_GETENTROPY
30+
#if defined(HAVE_GETENTROPY) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
3131
#include <unistd.h>
3232
#endif
33+
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
34+
#include <sys/random.h>
35+
#endif
3336
#ifdef HAVE_SYSCTL_ARND
3437
#include <sys/sysctl.h>
3538
#endif
@@ -237,6 +240,15 @@ void GetOSRand(unsigned char *ent32)
237240
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
238241
RandFailure();
239242
}
243+
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
244+
// We need a fallback for OSX < 10.12
245+
if (&getentropy != NULL) {
246+
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
247+
RandFailure();
248+
}
249+
} else {
250+
GetDevURandom(ent32);
251+
}
240252
#elif defined(HAVE_SYSCTL_ARND)
241253
/* FreeBSD and similar. It is possible for the call to return less
242254
* bytes than requested, so need to read in a loop.

0 commit comments

Comments
 (0)