From 4d20a8cbdd016e1553bc0818f2c1ac618216d9a7 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sat, 26 Oct 2019 12:20:12 -0700 Subject: [PATCH] [MOVEONLY] Move perfmon data gathering to new randomenv module Cherry-picked from: cea3902015185adc88adbd031d919f91bc844fd7 --- src/Makefile.am | 2 ++ src/random.cpp | 51 +++++++---------------------------- src/randomenv.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++ src/randomenv.h | 17 ++++++++++++ 4 files changed, 97 insertions(+), 42 deletions(-) create mode 100644 src/randomenv.cpp create mode 100644 src/randomenv.h diff --git a/src/Makefile.am b/src/Makefile.am index e2e8f39cc7c..47127af7846 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -147,6 +147,7 @@ BITCOIN_CORE_H = \ primitives/pureheader.h \ protocol.h \ random.h \ + randomenv.h \ reverselock.h \ rpc/blockchain.h \ rpc/client.h \ @@ -412,6 +413,7 @@ libdogecoin_util_a_SOURCES = \ fs.cpp \ random.cpp \ rpc/protocol.cpp \ + randomenv.cpp \ support/cleanse.cpp \ sync.cpp \ threadinterrupt.cpp \ diff --git a/src/random.cpp b/src/random.cpp index 188ec7acc89..1d162f82cd2 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -18,6 +18,8 @@ #include #include +#include + #include #ifndef WIN32 @@ -170,44 +172,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 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", NULL, NULL, 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. @@ -491,8 +455,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); @@ -507,8 +471,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); diff --git a/src/randomenv.cpp b/src/randomenv.cpp new file mode 100644 index 00000000000..4e41444b502 --- /dev/null +++ b/src/randomenv.cpp @@ -0,0 +1,69 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include // for GetTime() +#ifdef WIN32 +#include // for Windows API +#endif + +#include +#include + +#include + +namespace { + +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 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 +} + +} // namespace + +void RandAddDynamicEnv(CSHA512& hasher) +{ + RandAddSeedPerfmon(hasher); +} + +void RandAddStaticEnv(CSHA512& hasher) +{ +} diff --git a/src/randomenv.h b/src/randomenv.h new file mode 100644 index 00000000000..46cea6f6f28 --- /dev/null +++ b/src/randomenv.h @@ -0,0 +1,17 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_RANDOMENV_H +#define BITCOIN_RANDOMENV_H + +#include + +/** Gather non-cryptographic environment data that changes over time. */ +void RandAddDynamicEnv(CSHA512& hasher); + +/** Gather non-cryptographic environment data that does not change over time. */ +void RandAddStaticEnv(CSHA512& hasher); + +#endif