0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-13 11:25:02 -05:00

Merge bitcoin/bitcoin#24238: random: use arc4random on OpenBSD

0c49e52b22 build: remove unneeded getentropy detection (HAVE_GETENTROPY) (Sebastian Falbesoner)
5cd15ffdce random: use arc4random on OpenBSD (Sebastian Falbesoner)

Pull request description:

  Inspired by a discussion on obtaining randomness on various OSes in a secp256k1 PR (https://github.com/bitcoin-core/secp256k1/pull/748#discussion_r524605472, 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 0c49e52b22

Tree-SHA512: b5ed3d0718962c5a3839db9a28f93d08a0ac93094cc664f83bc4cf1cfad25049e6240b7b81fe06b71e6a3a0ca24a2c337eab088abec5470ad014e10c04fdb216
This commit is contained in:
laanwj 2022-02-10 10:00:46 +01:00
commit a7e80449c0
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
2 changed files with 8 additions and 19 deletions

View file

@ -1112,13 +1112,6 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
[ AC_MSG_RESULT([no])] [ AC_MSG_RESULT([no])]
) )
AC_MSG_CHECKING([for getentropy])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>]],
[[ getentropy(nullptr, 32) ]])],
[ AC_MSG_RESULT([yes]); AC_DEFINE([HAVE_GETENTROPY], [1], [Define this symbol if the BSD getentropy system call is available]) ],
[ AC_MSG_RESULT([no])]
)
AC_MSG_CHECKING([for getentropy via random.h]) AC_MSG_CHECKING([for getentropy via random.h])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h> AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
#include <sys/random.h>]], #include <sys/random.h>]],

View file

@ -32,10 +32,8 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <linux/random.h> #include <linux/random.h>
#endif #endif
#if defined(HAVE_GETENTROPY) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
#include <unistd.h>
#endif
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) #if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
#include <unistd.h>
#include <sys/random.h> #include <sys/random.h>
#endif #endif
#ifdef HAVE_SYSCTL_ARND #ifdef HAVE_SYSCTL_ARND
@ -305,16 +303,14 @@ void GetOSRand(unsigned char *ent32)
RandFailure(); RandFailure();
} }
} }
#elif defined(HAVE_GETENTROPY) && defined(__OpenBSD__) #elif defined(__OpenBSD__)
/* On OpenBSD this can return up to 256 bytes of entropy, will return an /* OpenBSD. From the arc4random(3) man page:
* error if more are requested. "Use of these functions is encouraged for almost all random number
* The call cannot return less than the requested number of bytes. consumption because the other interfaces are deficient in either
getentropy is explicitly limited to openbsd here, as a similar (but not quality, portability, standardization, or availability."
the same) function may exist on other platforms via glibc. The function call is always successful.
*/ */
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) { arc4random_buf(ent32, NUM_OS_RANDOM_BYTES);
RandFailure();
}
// Silence a compiler warning about unused function. // Silence a compiler warning about unused function.
(void)GetDevURandom; (void)GetDevURandom;
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX) #elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)