Skip to content

Commit a7e8044

Browse files
committed
Merge bitcoin/bitcoin#24238: random: use arc4random on OpenBSD
0c49e52 build: remove unneeded getentropy detection (HAVE_GETENTROPY) (Sebastian Falbesoner) 5cd15ff random: use arc4random on OpenBSD (Sebastian Falbesoner) Pull request description: Inspired by a discussion on obtaining randomness on various OSes in a secp256k1 PR (bitcoin-core/secp256k1#748 (comment), see also https://bitcoincore.reviews/libsecp256k1-748), I think it makes sense to follow best practices and use `arc4random_buf` rather than `getentropy` on OpenBSD in our random module. The [getentropy(2) man page](https://man.openbsd.org/getentropy.2) states: ``` getentropy() is not intended for regular code; please use the arc4random(3) family of functions instead. ``` The [arc4random(3) man page](https://man.openbsd.org/arc4random.3) states: ``` Use of these functions is encouraged for almost all random number consumption because the other interfaces are deficient in either quality, portability, standardization, or availability. ``` On the linked PR discussion worries about using RC4 internally has been expressed (see https://security.stackexchange.com/questions/85601/is-arc4random-secure-enough/172905#172905), but this would only affect users of OpenBSD <5.5, using a version that was released more than 8 years ago. ACKs for top commit: laanwj: Tested ACK 0c49e52 Tree-SHA512: b5ed3d0718962c5a3839db9a28f93d08a0ac93094cc664f83bc4cf1cfad25049e6240b7b81fe06b71e6a3a0ca24a2c337eab088abec5470ad014e10c04fdb216
2 parents 243a9c3 + 0c49e52 commit a7e8044

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

configure.ac

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,13 +1112,6 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
11121112
[ AC_MSG_RESULT([no])]
11131113
)
11141114

1115-
AC_MSG_CHECKING([for getentropy])
1116-
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>]],
1117-
[[ getentropy(nullptr, 32) ]])],
1118-
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETENTROPY], [1], [Define this symbol if the BSD getentropy system call is available]) ],
1119-
[ AC_MSG_RESULT([no])]
1120-
)
1121-
11221115
AC_MSG_CHECKING([for getentropy via random.h])
11231116
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
11241117
#include <sys/random.h>]],

src/random.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232
#include <sys/syscall.h>
3333
#include <linux/random.h>
3434
#endif
35-
#if defined(HAVE_GETENTROPY) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
36-
#include <unistd.h>
37-
#endif
3835
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
36+
#include <unistd.h>
3937
#include <sys/random.h>
4038
#endif
4139
#ifdef HAVE_SYSCTL_ARND
@@ -305,16 +303,14 @@ void GetOSRand(unsigned char *ent32)
305303
RandFailure();
306304
}
307305
}
308-
#elif defined(HAVE_GETENTROPY) && defined(__OpenBSD__)
309-
/* On OpenBSD this can return up to 256 bytes of entropy, will return an
310-
* error if more are requested.
311-
* The call cannot return less than the requested number of bytes.
312-
getentropy is explicitly limited to openbsd here, as a similar (but not
313-
the same) function may exist on other platforms via glibc.
306+
#elif defined(__OpenBSD__)
307+
/* OpenBSD. From the arc4random(3) man page:
308+
"Use of these functions is encouraged for almost all random number
309+
consumption because the other interfaces are deficient in either
310+
quality, portability, standardization, or availability."
311+
The function call is always successful.
314312
*/
315-
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
316-
RandFailure();
317-
}
313+
arc4random_buf(ent32, NUM_OS_RANDOM_BYTES);
318314
// Silence a compiler warning about unused function.
319315
(void)GetDevURandom;
320316
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)

0 commit comments

Comments
 (0)