aboutsummaryrefslogtreecommitdiff
path: root/src/random.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2019-10-26 12:20:12 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2019-11-12 14:50:44 -0800
commitcea3902015185adc88adbd031d919f91bc844fd7 (patch)
tree845d407ac3c2c00364ba64c12896ab927c9421b9 /src/random.cpp
parentb51bae1a5a4fa8ef7825dd1bb09e3f47f96d7a5a (diff)
[MOVEONLY] Move perfmon data gathering to new randomenv module
Diffstat (limited to 'src/random.cpp')
-rw-r--r--src/random.cpp51
1 files changed, 9 insertions, 42 deletions
diff --git a/src/random.cpp b/src/random.cpp
index b453fdd9b8..25ff7b3198 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -18,6 +18,8 @@
#include <stdlib.h>
#include <thread>
+#include <randomenv.h>
+
#include <support/allocators/secure.h>
#ifndef WIN32
@@ -263,44 +265,6 @@ static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA51
memory_cleanse(buffer, sizeof(buffer));
}
-static void RandAddSeedPerfmon(CSHA512& hasher)
-{
-#ifdef WIN32
- // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
- // Seed with the entire set of perfmon data
-
- // This can take up to 2 seconds, so only do it every 10 minutes
- static int64_t nLastPerfmon;
- if (GetTime() < nLastPerfmon + 10 * 60)
- return;
- nLastPerfmon = GetTime();
-
- std::vector<unsigned char> vData(250000, 0);
- long ret = 0;
- unsigned long nSize = 0;
- const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
- while (true) {
- nSize = vData.size();
- ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
- if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
- break;
- vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
- }
- RegCloseKey(HKEY_PERFORMANCE_DATA);
- if (ret == ERROR_SUCCESS) {
- hasher.Write(vData.data(), nSize);
- memory_cleanse(vData.data(), nSize);
- } else {
- // Performance data is only a best-effort attempt at improving the
- // situation when the OS randomness (and other sources) aren't
- // adequate. As a result, failure to read it is isn't considered critical,
- // so we don't call RandFailure().
- // TODO: Add logging when the logger is made functional before global
- // constructors have been invoked.
- }
-#endif
-}
-
#ifndef WIN32
/** Fallback: get 32 bytes of system entropy from /dev/urandom. The most
* compatible way to get cryptographic randomness on UNIX-ish platforms.
@@ -585,8 +549,8 @@ static void SeedSleep(CSHA512& hasher, RNGState& rng)
// High-precision timestamp after sleeping (as we commit to both the time before and after, this measures the delay)
SeedTimestamp(hasher);
- // Windows performance monitor data (once every 10 minutes)
- RandAddSeedPerfmon(hasher);
+ // Dynamic environment data (performance monitoring, ...; once every 10 minutes)
+ RandAddDynamicEnv(hasher);
// Strengthen every minute
SeedStrengthen(hasher, rng);
@@ -600,8 +564,11 @@ static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
// Everything that the 'slow' seeder includes.
SeedSlow(hasher);
- // Windows performance monitor data.
- RandAddSeedPerfmon(hasher);
+ // Dynamic environment data
+ RandAddDynamicEnv(hasher);
+
+ // Static environment data
+ RandAddStaticEnv(hasher);
// Strengthen
SeedStrengthen(hasher, rng);