Skip to content

Commit cea3902

Browse files
committed
[MOVEONLY] Move perfmon data gathering to new randomenv module
1 parent b51bae1 commit cea3902

File tree

4 files changed

+97
-42
lines changed

4 files changed

+97
-42
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ BITCOIN_CORE_H = \
175175
protocol.h \
176176
psbt.h \
177177
random.h \
178+
randomenv.h \
178179
reverse_iterator.h \
179180
reverselock.h \
180181
rpc/blockchain.h \
@@ -502,6 +503,7 @@ libbitcoin_util_a_SOURCES = \
502503
interfaces/handler.cpp \
503504
logging.cpp \
504505
random.cpp \
506+
randomenv.cpp \
505507
rpc/request.cpp \
506508
support/cleanse.cpp \
507509
sync.cpp \

src/random.cpp

+9-42
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <stdlib.h>
1919
#include <thread>
2020

21+
#include <randomenv.h>
22+
2123
#include <support/allocators/secure.h>
2224

2325
#ifndef WIN32
@@ -263,44 +265,6 @@ static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA51
263265
memory_cleanse(buffer, sizeof(buffer));
264266
}
265267

266-
static void RandAddSeedPerfmon(CSHA512& hasher)
267-
{
268-
#ifdef WIN32
269-
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
270-
// Seed with the entire set of perfmon data
271-
272-
// This can take up to 2 seconds, so only do it every 10 minutes
273-
static int64_t nLastPerfmon;
274-
if (GetTime() < nLastPerfmon + 10 * 60)
275-
return;
276-
nLastPerfmon = GetTime();
277-
278-
std::vector<unsigned char> vData(250000, 0);
279-
long ret = 0;
280-
unsigned long nSize = 0;
281-
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
282-
while (true) {
283-
nSize = vData.size();
284-
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
285-
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
286-
break;
287-
vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
288-
}
289-
RegCloseKey(HKEY_PERFORMANCE_DATA);
290-
if (ret == ERROR_SUCCESS) {
291-
hasher.Write(vData.data(), nSize);
292-
memory_cleanse(vData.data(), nSize);
293-
} else {
294-
// Performance data is only a best-effort attempt at improving the
295-
// situation when the OS randomness (and other sources) aren't
296-
// adequate. As a result, failure to read it is isn't considered critical,
297-
// so we don't call RandFailure().
298-
// TODO: Add logging when the logger is made functional before global
299-
// constructors have been invoked.
300-
}
301-
#endif
302-
}
303-
304268
#ifndef WIN32
305269
/** Fallback: get 32 bytes of system entropy from /dev/urandom. The most
306270
* compatible way to get cryptographic randomness on UNIX-ish platforms.
@@ -585,8 +549,8 @@ static void SeedSleep(CSHA512& hasher, RNGState& rng)
585549
// High-precision timestamp after sleeping (as we commit to both the time before and after, this measures the delay)
586550
SeedTimestamp(hasher);
587551

588-
// Windows performance monitor data (once every 10 minutes)
589-
RandAddSeedPerfmon(hasher);
552+
// Dynamic environment data (performance monitoring, ...; once every 10 minutes)
553+
RandAddDynamicEnv(hasher);
590554

591555
// Strengthen every minute
592556
SeedStrengthen(hasher, rng);
@@ -600,8 +564,11 @@ static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
600564
// Everything that the 'slow' seeder includes.
601565
SeedSlow(hasher);
602566

603-
// Windows performance monitor data.
604-
RandAddSeedPerfmon(hasher);
567+
// Dynamic environment data
568+
RandAddDynamicEnv(hasher);
569+
570+
// Static environment data
571+
RandAddStaticEnv(hasher);
605572

606573
// Strengthen
607574
SeedStrengthen(hasher, rng);

src/randomenv.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include <randomenv.h>
7+
8+
#include <crypto/sha512.h>
9+
#include <support/cleanse.h>
10+
#include <util/time.h> // for GetTime()
11+
#ifdef WIN32
12+
#include <compat.h> // for Windows API
13+
#endif
14+
15+
#include <algorithm>
16+
#include <vector>
17+
18+
#include <stdint.h>
19+
20+
namespace {
21+
22+
void RandAddSeedPerfmon(CSHA512& hasher)
23+
{
24+
#ifdef WIN32
25+
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
26+
// Seed with the entire set of perfmon data
27+
28+
// This can take up to 2 seconds, so only do it every 10 minutes
29+
static int64_t nLastPerfmon;
30+
if (GetTime() < nLastPerfmon + 10 * 60)
31+
return;
32+
nLastPerfmon = GetTime();
33+
34+
std::vector<unsigned char> vData(250000, 0);
35+
long ret = 0;
36+
unsigned long nSize = 0;
37+
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
38+
while (true) {
39+
nSize = vData.size();
40+
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
41+
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
42+
break;
43+
vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
44+
}
45+
RegCloseKey(HKEY_PERFORMANCE_DATA);
46+
if (ret == ERROR_SUCCESS) {
47+
hasher.Write(vData.data(), nSize);
48+
memory_cleanse(vData.data(), nSize);
49+
} else {
50+
// Performance data is only a best-effort attempt at improving the
51+
// situation when the OS randomness (and other sources) aren't
52+
// adequate. As a result, failure to read it is isn't considered critical,
53+
// so we don't call RandFailure().
54+
// TODO: Add logging when the logger is made functional before global
55+
// constructors have been invoked.
56+
}
57+
#endif
58+
}
59+
60+
} // namespace
61+
62+
void RandAddDynamicEnv(CSHA512& hasher)
63+
{
64+
RandAddSeedPerfmon(hasher);
65+
}
66+
67+
void RandAddStaticEnv(CSHA512& hasher)
68+
{
69+
}

src/randomenv.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_RANDOMENV_H
7+
#define BITCOIN_RANDOMENV_H
8+
9+
#include <crypto/sha512.h>
10+
11+
/** Gather non-cryptographic environment data that changes over time. */
12+
void RandAddDynamicEnv(CSHA512& hasher);
13+
14+
/** Gather non-cryptographic environment data that does not change over time. */
15+
void RandAddStaticEnv(CSHA512& hasher);
16+
17+
#endif

0 commit comments

Comments
 (0)