6 Commits

Author SHA1 Message Date
Pieter Wuille
47b48521bb
Make FastRandomContext support standard C++11 RNG interface
This makes it possible to plug it into the various standard C++11 random
distribution algorithms and other functions like std::shuffle.

Cherry-picked from: 1ec1602a4549f6b68586cead8eff701bceb624f5
2024-06-18 16:38:09 -07:00
Pieter Wuille
a8cad7ed4c
Add FastRandomContext::rand256() and ::randbytes()
FastRandomContext now provides all functionality that the real Rand* functions
provide.

Cherry-picked from: 37e864eb9fee4b592bd61c5ec3555b00a2de2cf7
2024-06-13 11:42:47 -07:00
Pieter Wuille
3f511675be
Add a FastRandomContext::randrange and use it
Cherry-picked from: 4fd2d2fc97e21efceab849576e544160fd5e3e3d
2024-06-13 11:42:46 -07:00
Pieter Wuille
b2dc5e349b
Switch FastRandomContext to ChaCha20
Cherry-picked from: 16329224e70d0525208f6b0ba00c5e1531a4f5ea
2024-06-13 11:42:46 -07:00
Wladimir J. van der Laan
18a8fd4506
sanity: Move OS random to sanity check function
Move the OS random test to a sanity check function that is called every
time bitcoind is initialized.

Keep `src/test/random_tests.cpp` for the case that later random tests
are added, and keep a rudimentary test that just calls the sanity check.

Cherry-picked from: 7cad84929907c4294f07377453aa77887911b486
2024-06-13 11:42:45 -07:00
Wladimir J. van der Laan
f75d5ea728
util: Specific GetOSRandom for Linux/FreeBSD/OpenBSD
These are available in sandboxes without access to files or
devices. Also [they are safer and more straightforward](https://en.wikipedia.org/wiki/Entropy-supplying_system_calls)
to use than `/dev/urandom` as reading from a file has quite a few edge
cases:

- Linux: `getrandom(buf, buflen, 0)`. [getrandom(2)](http://man7.org/linux/man-pages/man2/getrandom.2.html)
  was introduced in version 3.17 of the Linux kernel.
- OpenBSD: `getentropy(buf, buflen)`. The [getentropy(2)](http://man.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2)
  function appeared in OpenBSD 5.6.
- FreeBSD and NetBSD: `sysctl(KERN_ARND)`. Not sure when this was added
  but it has existed for quite a while.

Alternatives:

- Linux has sysctl `CTL_KERN` / `KERN_RANDOM` / `RANDOM_UUID`
  which gives 16 bytes of randomness. This may be available
  on older kernels, however [sysctl is deprecated on Linux](https://lwn.net/Articles/605392/)
  and even removed in some distros so we shouldn't use it.

Add tests for `GetOSRand()`:

- Test that no error happens (otherwise `RandFailure()` which aborts)
- Test that all 32 bytes are overwritten (initialize with zeros, try multiple times)

Discussion:

- When to use these? Currently they are always used when available.
  Another option would be to use them only when `/dev/urandom` is not
  available. But this would mean these code paths receive less testing,
  and I'm not sure there is any reason to prefer `/dev/urandom`.

Closes: #9676

Cherry-picked from: 224e6eb089a0f4977d22f3803fc27e44b5e7eea5

Contains squashed commit of aa09ccbb74ea9febd83ce3362238ac5339069909 squashme: comment that NUM_OS_RANDOM_BYTES should not be changed lightly
2024-06-13 11:42:45 -07:00