Skip to content

Commit 6e6b3b9

Browse files
committed
Merge #14955: Switch all RNG code to the built-in PRNG
223de8d Document RNG design in random.h (Pieter Wuille) f2e60ca Use secure allocator for RNG state (Pieter Wuille) cddb31b Encapsulate RNGState better (Pieter Wuille) 152146e DRY: Implement GetRand using FastRandomContext::randrange (Pieter Wuille) a1f252e Sprinkle some sweet noexcepts over the RNG code (Pieter Wuille) 4ea8e50 Remove hwrand_initialized. (Pieter Wuille) 9d7032e Switch all RNG code to the built-in PRNG. (Pieter Wuille) 16e40a8 Integrate util/system's CInit into RNGState (Pieter Wuille) 2ccc3d3 Abstract out seeding/extracting entropy into RNGState::MixExtract (Pieter Wuille) aae8b9b Add thread safety annotations to RNG state (Pieter Wuille) d3f54d1 Rename some hardware RNG related functions (Pieter Wuille) 05fde14 Automatically initialize RNG on first use. (Pieter Wuille) 2d1cc50 Don't log RandAddSeedPerfmon details (Pieter Wuille) 6a57ca9 Use FRC::randbytes instead of reading >32 bytes from RNG (Pieter Wuille) Pull request description: This does not remove OpenSSL, but makes our own PRNG the 'main' one; for GetStrongRandBytes, the OpenSSL RNG is still used (indirectly, by feeding its output into our PRNG state). It includes a few policy changes (regarding what entropy is seeded when). Before this PR: * GetRand*: * OpenSSL * GetStrongRand*: * CPU cycle counter * Perfmon data (on Windows, once 10 min) * /dev/urandom (or equivalent) * rdrand (if available) * From scheduler when idle: * CPU cycle counter before and after 1ms sleep * At startup: * CPU cycle counter before and after 1ms sleep After this PR: * GetRand*: * Stack pointer (which indirectly identifies thread and some call stack information) * rdrand (if available) * CPU cycle counter * GetStrongRand*: * Stack pointer (which indirectly identifies thread and some call stack information) * rdrand (if available) * CPU cycle counter * /dev/urandom (or equivalent) * OpenSSL * CPU cycle counter again * From scheduler when idle: * Stack pointer (which indirectly identifies thread and some call stack information) * rdrand (if available) * CPU cycle counter before and after 1ms sleep * Perfmon data (on Windows, once every 10 min) * At startup: * Stack pointer (which indirectly identifies thread and some call stack information) * rdrand (if available) * CPU cycle counter * /dev/urandom (or equivalent) * OpenSSL * CPU cycle counter again * Perfmon data (on Windows, once every 10 min) The interface of random.h is also simplified, and documentation is added. This implements most of #14623. Tree-SHA512: 0120e19bd4ce80a509b5c180a4f29497d299ce8242e25755880851344b825bc2d64a222bc245e659562fb5463fb7c70fbfcf003616be4dc59d0ed6534f93dd20
2 parents ace87ea + 223de8d commit 6e6b3b9

File tree

8 files changed

+344
-185
lines changed

8 files changed

+344
-185
lines changed

src/bench/bench_bitcoin.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <crypto/sha256.h>
88
#include <key.h>
9-
#include <random.h>
109
#include <util/system.h>
1110
#include <util/strencodings.h>
1211
#include <validation.h>
@@ -67,7 +66,6 @@ int main(int argc, char** argv)
6766
const fs::path bench_datadir{SetDataDir()};
6867

6968
SHA256AutoDetect();
70-
RandomInit();
7169
ECC_Start();
7270
SetupEnvironment();
7371

src/crypto/sha512.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CSHA512
1717
uint64_t bytes;
1818

1919
public:
20-
static const size_t OUTPUT_SIZE = 64;
20+
static constexpr size_t OUTPUT_SIZE = 64;
2121

2222
CSHA512();
2323
CSHA512& Write(const unsigned char* data, size_t len);

src/qt/test/paymentservertests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,12 @@ void PaymentServerTests::paymentServerTests()
181181
QCOMPARE(PaymentServer::verifyExpired(r.paymentRequest.getDetails()), true);
182182

183183
// Test BIP70 DoS protection:
184-
unsigned char randData[BIP70_MAX_PAYMENTREQUEST_SIZE + 1];
185-
GetRandBytes(randData, sizeof(randData));
184+
auto randdata = FastRandomContext().randbytes(BIP70_MAX_PAYMENTREQUEST_SIZE + 1);
185+
186186
// Write data to a temp file:
187187
QTemporaryFile tempFile;
188188
tempFile.open();
189-
tempFile.write((const char*)randData, sizeof(randData));
189+
tempFile.write((const char*)randdata.data(), randdata.size());
190190
tempFile.close();
191191
// compares 50001 <= BIP70_MAX_PAYMENTREQUEST_SIZE == false
192192
QCOMPARE(PaymentServer::verifySize(tempFile.size()), false);

0 commit comments

Comments
 (0)