forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MOVEONLY] Move perfmon data gathering to new randomenv module
- Loading branch information
Showing
5 changed files
with
98 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "randomenv.h" | ||
|
||
#include "crypto/sha512.h" | ||
#include "support/cleanse.h" | ||
#include "utiltime.h" // for GetTime() | ||
#ifdef WIN32 | ||
#include "compat.h" // for Windows API | ||
#endif | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
|
||
#include <stdint.h> | ||
|
||
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<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 | ||
} | ||
|
||
} // namespace | ||
|
||
void RandAddDynamicEnv(CSHA512& hasher) | ||
{ | ||
RandAddSeedPerfmon(hasher); | ||
} | ||
|
||
void RandAddStaticEnv(CSHA512& hasher) | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "crypto/sha512.h" | ||
|
||
/** 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 |