2014-06-26 14:41:53 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-06-26 14:41:53 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <random.h>
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <crypto/sha512.h>
|
|
|
|
#include <support/cleanse.h>
|
2014-06-26 14:41:53 +02:00
|
|
|
#ifdef WIN32
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <compat.h> // for Windows API
|
2016-04-16 12:25:12 +02:00
|
|
|
#include <wincrypt.h>
|
2014-06-26 14:41:53 +02:00
|
|
|
#endif
|
2018-05-15 11:27:14 +02:00
|
|
|
#include <logging.h> // for LogPrint()
|
2017-11-08 23:07:40 +01:00
|
|
|
#include <sync.h> // for WAIT_LOCK
|
2018-10-23 00:51:11 +02:00
|
|
|
#include <util/time.h> // for GetTime()
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2016-04-23 18:07:35 +02:00
|
|
|
#include <stdlib.h>
|
2017-05-03 03:21:33 +02:00
|
|
|
#include <chrono>
|
2017-05-05 20:32:06 +02:00
|
|
|
#include <thread>
|
2014-09-14 12:43:56 +02:00
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
#ifndef WIN32
|
2018-05-15 11:27:14 +02:00
|
|
|
#include <fcntl.h>
|
2014-06-26 14:41:53 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2014-09-14 12:43:56 +02:00
|
|
|
|
2017-02-21 17:36:37 +01:00
|
|
|
#ifdef HAVE_SYS_GETRANDOM
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#endif
|
2017-07-27 14:34:09 +02:00
|
|
|
#if defined(HAVE_GETENTROPY) || (defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX))
|
2017-02-21 17:36:37 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2017-07-27 14:34:09 +02:00
|
|
|
#if defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
|
|
|
|
#include <sys/random.h>
|
|
|
|
#endif
|
2017-02-21 17:36:37 +01:00
|
|
|
#ifdef HAVE_SYSCTL_ARND
|
2018-10-23 00:51:11 +02:00
|
|
|
#include <util/strencodings.h> // for ARRAYLEN
|
2017-02-21 17:36:37 +01:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2017-05-03 12:07:53 +02:00
|
|
|
#include <mutex>
|
|
|
|
|
2017-07-14 01:43:05 +02:00
|
|
|
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
|
|
|
|
#include <cpuid.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/rand.h>
|
2019-01-16 01:03:54 +01:00
|
|
|
#include <openssl/conf.h>
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2017-07-16 14:56:43 +02:00
|
|
|
[[noreturn]] static void RandFailure()
|
2016-04-23 18:07:35 +02:00
|
|
|
{
|
|
|
|
LogPrintf("Failed to read randomness, aborting\n");
|
2017-07-16 14:56:43 +02:00
|
|
|
std::abort();
|
2016-04-23 18:07:35 +02:00
|
|
|
}
|
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
static inline int64_t GetPerformanceCounter()
|
|
|
|
{
|
2017-05-03 03:21:33 +02:00
|
|
|
// Read the hardware time stamp counter when available.
|
|
|
|
// See https://en.wikipedia.org/wiki/Time_Stamp_Counter for more information.
|
|
|
|
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
|
|
|
|
return __rdtsc();
|
|
|
|
#elif !defined(_MSC_VER) && defined(__i386__)
|
|
|
|
uint64_t r = 0;
|
|
|
|
__asm__ volatile ("rdtsc" : "=A"(r)); // Constrain the r variable to the eax:edx pair.
|
|
|
|
return r;
|
|
|
|
#elif !defined(_MSC_VER) && (defined(__x86_64__) || defined(__amd64__))
|
|
|
|
uint64_t r1 = 0, r2 = 0;
|
|
|
|
__asm__ volatile ("rdtsc" : "=a"(r1), "=d"(r2)); // Constrain r1 to rax and r2 to rdx.
|
|
|
|
return (r2 << 32) | r1;
|
2014-06-26 14:41:53 +02:00
|
|
|
#else
|
2017-05-03 03:21:33 +02:00
|
|
|
// Fall back to using C++11 clock (usually microsecond or nanosecond precision)
|
|
|
|
return std::chrono::high_resolution_clock::now().time_since_epoch().count();
|
2014-06-26 14:41:53 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-10 00:13:00 +02:00
|
|
|
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
|
|
|
|
static bool rdrand_supported = false;
|
|
|
|
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000;
|
2019-01-17 00:15:21 +01:00
|
|
|
static void InitHardwareRand()
|
2017-05-10 00:13:00 +02:00
|
|
|
{
|
2017-07-14 01:43:05 +02:00
|
|
|
uint32_t eax, ebx, ecx, edx;
|
|
|
|
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx & CPUID_F1_ECX_RDRAND)) {
|
2017-05-10 00:13:00 +02:00
|
|
|
rdrand_supported = true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 01:48:21 +01:00
|
|
|
|
2019-01-17 00:15:21 +01:00
|
|
|
static void ReportHardwareRand()
|
2018-12-18 01:48:21 +01:00
|
|
|
{
|
|
|
|
if (rdrand_supported) {
|
|
|
|
// This must be done in a separate function, as HWRandInit() may be indirectly called
|
|
|
|
// from global constructors, before logging is initialized.
|
|
|
|
LogPrintf("Using RdRand as an additional entropy source\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 00:13:00 +02:00
|
|
|
#else
|
2019-01-17 00:15:21 +01:00
|
|
|
/* Access to other hardware random number generators could be added here later,
|
|
|
|
* assuming it is sufficiently fast (in the order of a few hundred CPU cycles).
|
|
|
|
* Slower sources should probably be invoked separately, and/or only from
|
|
|
|
* RandAddSeedSleep (which is called during idle background operation).
|
|
|
|
*/
|
|
|
|
static void InitHardwareRand() {}
|
|
|
|
static void ReportHardwareRand() {}
|
2017-05-10 00:13:00 +02:00
|
|
|
#endif
|
|
|
|
|
2019-01-17 00:15:21 +01:00
|
|
|
static bool GetHardwareRand(unsigned char* ent32) {
|
2017-05-10 00:13:00 +02:00
|
|
|
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__)
|
|
|
|
if (rdrand_supported) {
|
|
|
|
uint8_t ok;
|
|
|
|
// Not all assemblers support the rdrand instruction, write it in hex.
|
|
|
|
#ifdef __i386__
|
|
|
|
for (int iter = 0; iter < 4; ++iter) {
|
|
|
|
uint32_t r1, r2;
|
|
|
|
__asm__ volatile (".byte 0x0f, 0xc7, 0xf0;" // rdrand %eax
|
|
|
|
".byte 0x0f, 0xc7, 0xf2;" // rdrand %edx
|
|
|
|
"setc %2" :
|
|
|
|
"=a"(r1), "=d"(r2), "=q"(ok) :: "cc");
|
|
|
|
if (!ok) return false;
|
|
|
|
WriteLE32(ent32 + 8 * iter, r1);
|
|
|
|
WriteLE32(ent32 + 8 * iter + 4, r2);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
uint64_t r1, r2, r3, r4;
|
|
|
|
__asm__ volatile (".byte 0x48, 0x0f, 0xc7, 0xf0, " // rdrand %rax
|
|
|
|
"0x48, 0x0f, 0xc7, 0xf3, " // rdrand %rbx
|
|
|
|
"0x48, 0x0f, 0xc7, 0xf1, " // rdrand %rcx
|
|
|
|
"0x48, 0x0f, 0xc7, 0xf2; " // rdrand %rdx
|
|
|
|
"setc %4" :
|
|
|
|
"=a"(r1), "=b"(r2), "=c"(r3), "=d"(r4), "=q"(ok) :: "cc");
|
|
|
|
if (!ok) return false;
|
|
|
|
WriteLE64(ent32, r1);
|
|
|
|
WriteLE64(ent32 + 8, r2);
|
|
|
|
WriteLE64(ent32 + 16, r3);
|
|
|
|
WriteLE64(ent32 + 24, r4);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
static void RandAddSeedPerfmon(CSHA512& hasher)
|
2014-06-26 14:41:53 +02:00
|
|
|
{
|
2015-02-25 03:03:53 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
|
|
|
|
// Seed with the entire set of perfmon data
|
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
// 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();
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
std::vector<unsigned char> vData(250000, 0);
|
2014-06-26 14:41:53 +02:00
|
|
|
long ret = 0;
|
|
|
|
unsigned long nSize = 0;
|
|
|
|
const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
|
2014-09-19 19:21:46 +02:00
|
|
|
while (true) {
|
2014-06-26 14:41:53 +02:00
|
|
|
nSize = vData.size();
|
2017-08-07 07:36:37 +02:00
|
|
|
ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", nullptr, nullptr, vData.data(), &nSize);
|
2014-06-26 14:41:53 +02:00
|
|
|
if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
|
|
|
|
break;
|
2014-09-19 19:21:46 +02:00
|
|
|
vData.resize(std::max((vData.size() * 3) / 2, nMaxSize)); // Grow size of buffer exponentially
|
2014-06-26 14:41:53 +02:00
|
|
|
}
|
|
|
|
RegCloseKey(HKEY_PERFORMANCE_DATA);
|
2014-09-19 19:21:46 +02:00
|
|
|
if (ret == ERROR_SUCCESS) {
|
2018-12-14 03:37:29 +01:00
|
|
|
hasher.Write(vData.data(), nSize);
|
2016-12-09 04:01:37 +01:00
|
|
|
memory_cleanse(vData.data(), nSize);
|
2014-06-26 14:41:53 +02:00
|
|
|
} else {
|
2018-12-19 10:50:36 +01:00
|
|
|
// 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.
|
2014-06-26 14:41:53 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-22 08:51:26 +01:00
|
|
|
#ifndef WIN32
|
|
|
|
/** Fallback: get 32 bytes of system entropy from /dev/urandom. The most
|
|
|
|
* compatible way to get cryptographic randomness on UNIX-ish platforms.
|
|
|
|
*/
|
2018-05-02 17:14:48 +02:00
|
|
|
static void GetDevURandom(unsigned char *ent32)
|
2017-02-22 08:51:26 +01:00
|
|
|
{
|
|
|
|
int f = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (f == -1) {
|
|
|
|
RandFailure();
|
|
|
|
}
|
|
|
|
int have = 0;
|
|
|
|
do {
|
|
|
|
ssize_t n = read(f, ent32 + have, NUM_OS_RANDOM_BYTES - have);
|
|
|
|
if (n <= 0 || n + have > NUM_OS_RANDOM_BYTES) {
|
2017-07-15 21:34:52 +02:00
|
|
|
close(f);
|
2017-02-22 08:51:26 +01:00
|
|
|
RandFailure();
|
|
|
|
}
|
|
|
|
have += n;
|
|
|
|
} while (have < NUM_OS_RANDOM_BYTES);
|
|
|
|
close(f);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-16 12:25:12 +02:00
|
|
|
/** Get 32 bytes of system entropy. */
|
2017-02-21 17:36:37 +01:00
|
|
|
void GetOSRand(unsigned char *ent32)
|
2016-04-16 12:25:12 +02:00
|
|
|
{
|
2017-02-21 17:36:37 +01:00
|
|
|
#if defined(WIN32)
|
2016-04-16 12:25:12 +02:00
|
|
|
HCRYPTPROV hProvider;
|
2017-08-07 07:36:37 +02:00
|
|
|
int ret = CryptAcquireContextW(&hProvider, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
2016-04-23 18:07:35 +02:00
|
|
|
if (!ret) {
|
|
|
|
RandFailure();
|
|
|
|
}
|
2017-02-21 17:36:37 +01:00
|
|
|
ret = CryptGenRandom(hProvider, NUM_OS_RANDOM_BYTES, ent32);
|
2016-04-23 18:07:35 +02:00
|
|
|
if (!ret) {
|
|
|
|
RandFailure();
|
|
|
|
}
|
2016-04-16 12:25:12 +02:00
|
|
|
CryptReleaseContext(hProvider, 0);
|
2017-02-21 17:36:37 +01:00
|
|
|
#elif defined(HAVE_SYS_GETRANDOM)
|
|
|
|
/* Linux. From the getrandom(2) man page:
|
|
|
|
* "If the urandom source has been initialized, reads of up to 256 bytes
|
|
|
|
* will always return as many bytes as requested and will not be
|
|
|
|
* interrupted by signals."
|
|
|
|
*/
|
2017-02-22 08:51:26 +01:00
|
|
|
int rv = syscall(SYS_getrandom, ent32, NUM_OS_RANDOM_BYTES, 0);
|
|
|
|
if (rv != NUM_OS_RANDOM_BYTES) {
|
|
|
|
if (rv < 0 && errno == ENOSYS) {
|
|
|
|
/* Fallback for kernel <3.17: the return value will be -1 and errno
|
|
|
|
* ENOSYS if the syscall is not available, in that case fall back
|
|
|
|
* to /dev/urandom.
|
|
|
|
*/
|
|
|
|
GetDevURandom(ent32);
|
|
|
|
} else {
|
|
|
|
RandFailure();
|
|
|
|
}
|
2017-02-21 17:36:37 +01:00
|
|
|
}
|
2017-07-17 19:14:53 +02:00
|
|
|
#elif defined(HAVE_GETENTROPY) && defined(__OpenBSD__)
|
2017-02-21 17:36:37 +01:00
|
|
|
/* On OpenBSD this can return up to 256 bytes of entropy, will return an
|
|
|
|
* error if more are requested.
|
|
|
|
* The call cannot return less than the requested number of bytes.
|
2017-07-17 19:14:53 +02:00
|
|
|
getentropy is explicitly limited to openbsd here, as a similar (but not
|
|
|
|
the same) function may exist on other platforms via glibc.
|
2017-02-21 17:36:37 +01:00
|
|
|
*/
|
|
|
|
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
|
|
|
|
RandFailure();
|
|
|
|
}
|
2017-07-27 14:34:09 +02:00
|
|
|
#elif defined(HAVE_GETENTROPY_RAND) && defined(MAC_OSX)
|
|
|
|
// We need a fallback for OSX < 10.12
|
2017-08-16 17:26:07 +02:00
|
|
|
if (&getentropy != nullptr) {
|
2017-07-27 14:34:09 +02:00
|
|
|
if (getentropy(ent32, NUM_OS_RANDOM_BYTES) != 0) {
|
|
|
|
RandFailure();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GetDevURandom(ent32);
|
|
|
|
}
|
2017-02-21 17:36:37 +01:00
|
|
|
#elif defined(HAVE_SYSCTL_ARND)
|
|
|
|
/* FreeBSD and similar. It is possible for the call to return less
|
|
|
|
* bytes than requested, so need to read in a loop.
|
|
|
|
*/
|
|
|
|
static const int name[2] = {CTL_KERN, KERN_ARND};
|
|
|
|
int have = 0;
|
|
|
|
do {
|
|
|
|
size_t len = NUM_OS_RANDOM_BYTES - have;
|
2017-08-07 07:36:37 +02:00
|
|
|
if (sysctl(name, ARRAYLEN(name), ent32 + have, &len, nullptr, 0) != 0) {
|
2017-02-21 17:36:37 +01:00
|
|
|
RandFailure();
|
|
|
|
}
|
|
|
|
have += len;
|
|
|
|
} while (have < NUM_OS_RANDOM_BYTES);
|
2016-04-16 12:25:12 +02:00
|
|
|
#else
|
2017-02-21 17:36:37 +01:00
|
|
|
/* Fall back to /dev/urandom if there is no specific method implemented to
|
|
|
|
* get system entropy for this OS.
|
|
|
|
*/
|
2017-02-22 08:51:26 +01:00
|
|
|
GetDevURandom(ent32);
|
2016-04-16 12:25:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-01-16 01:03:54 +01:00
|
|
|
void LockingCallbackOpenSSL(int mode, int i, const char* file, int line);
|
|
|
|
|
2018-12-18 01:48:21 +01:00
|
|
|
namespace {
|
2019-01-16 01:03:54 +01:00
|
|
|
|
2018-12-18 01:48:21 +01:00
|
|
|
struct RNGState {
|
|
|
|
Mutex m_mutex;
|
2018-12-18 02:03:30 +01:00
|
|
|
unsigned char m_state[32] GUARDED_BY(m_mutex) = {0};
|
|
|
|
uint64_t m_counter GUARDED_BY(m_mutex) = 0;
|
2018-12-14 03:37:29 +01:00
|
|
|
bool m_strongly_seeded GUARDED_BY(m_mutex) = false;
|
2019-01-16 01:03:54 +01:00
|
|
|
std::unique_ptr<Mutex[]> m_mutex_openssl;
|
2018-12-18 01:48:21 +01:00
|
|
|
|
2018-12-18 02:03:30 +01:00
|
|
|
RNGState()
|
|
|
|
{
|
2019-01-17 00:15:21 +01:00
|
|
|
InitHardwareRand();
|
2019-01-16 01:03:54 +01:00
|
|
|
|
|
|
|
// Init OpenSSL library multithreading support
|
|
|
|
m_mutex_openssl.reset(new Mutex[CRYPTO_num_locks()]);
|
|
|
|
CRYPTO_set_locking_callback(LockingCallbackOpenSSL);
|
|
|
|
|
|
|
|
// OpenSSL can optionally load a config file which lists optional loadable modules and engines.
|
|
|
|
// We don't use them so we don't require the config. However some of our libs may call functions
|
|
|
|
// which attempt to load the config file, possibly resulting in an exit() or crash if it is missing
|
|
|
|
// or corrupt. Explicitly tell OpenSSL not to try to load the file. The result for our libs will be
|
|
|
|
// that the config appears to have been loaded and there are no modules/engines available.
|
|
|
|
OPENSSL_no_config();
|
|
|
|
}
|
|
|
|
|
|
|
|
~RNGState()
|
|
|
|
{
|
|
|
|
// Securely erase the memory used by the OpenSSL PRNG
|
|
|
|
RAND_cleanup();
|
|
|
|
// Shutdown OpenSSL library multithreading support
|
|
|
|
CRYPTO_set_locking_callback(nullptr);
|
2018-12-18 01:48:21 +01:00
|
|
|
}
|
2018-12-18 01:04:35 +01:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
/** Extract up to 32 bytes of entropy from the RNG state, mixing in new entropy from hasher.
|
|
|
|
*
|
|
|
|
* If this function has never been called with strong_seed = true, false is returned.
|
|
|
|
*/
|
|
|
|
bool MixExtract(unsigned char* out, size_t num, CSHA512&& hasher, bool strong_seed)
|
2018-12-18 01:04:35 +01:00
|
|
|
{
|
|
|
|
assert(num <= 32);
|
|
|
|
unsigned char buf[64];
|
|
|
|
static_assert(sizeof(buf) == CSHA512::OUTPUT_SIZE, "Buffer needs to have hasher's output size");
|
2018-12-14 03:37:29 +01:00
|
|
|
bool ret;
|
2018-12-18 01:04:35 +01:00
|
|
|
{
|
|
|
|
LOCK(m_mutex);
|
2018-12-14 03:37:29 +01:00
|
|
|
ret = (m_strongly_seeded |= strong_seed);
|
2018-12-18 01:04:35 +01:00
|
|
|
// Write the current state of the RNG into the hasher
|
|
|
|
hasher.Write(m_state, 32);
|
|
|
|
// Write a new counter number into the state
|
|
|
|
hasher.Write((const unsigned char*)&m_counter, sizeof(m_counter));
|
|
|
|
++m_counter;
|
|
|
|
// Finalize the hasher
|
|
|
|
hasher.Finalize(buf);
|
|
|
|
// Store the last 32 bytes of the hash output as new RNG state.
|
|
|
|
memcpy(m_state, buf + 32, 32);
|
|
|
|
}
|
|
|
|
// If desired, copy (up to) the first 32 bytes of the hash output as output.
|
|
|
|
if (num) {
|
|
|
|
assert(out != nullptr);
|
|
|
|
memcpy(out, buf, num);
|
|
|
|
}
|
|
|
|
// Best effort cleanup of internal state
|
|
|
|
hasher.Reset();
|
|
|
|
memory_cleanse(buf, 64);
|
2018-12-14 03:37:29 +01:00
|
|
|
return ret;
|
2018-12-18 01:04:35 +01:00
|
|
|
}
|
2018-12-18 01:48:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
RNGState& GetRNGState()
|
|
|
|
{
|
|
|
|
// This C++11 idiom relies on the guarantee that static variable are initialized
|
|
|
|
// on first call, even when multiple parallel calls are permitted.
|
|
|
|
static std::unique_ptr<RNGState> g_rng{new RNGState()};
|
|
|
|
return *g_rng;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 01:03:54 +01:00
|
|
|
void LockingCallbackOpenSSL(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
|
|
|
|
{
|
|
|
|
RNGState& rng = GetRNGState();
|
|
|
|
|
|
|
|
if (mode & CRYPTO_LOCK) {
|
|
|
|
rng.m_mutex_openssl[i].lock();
|
|
|
|
} else {
|
|
|
|
rng.m_mutex_openssl[i].unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
static void SeedTimestamp(CSHA512& hasher)
|
|
|
|
{
|
|
|
|
int64_t perfcounter = GetPerformanceCounter();
|
|
|
|
hasher.Write((const unsigned char*)&perfcounter, sizeof(perfcounter));
|
|
|
|
}
|
2017-05-22 22:01:52 +02:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
static void SeedFast(CSHA512& hasher)
|
2017-05-22 22:01:52 +02:00
|
|
|
{
|
2018-12-14 03:37:29 +01:00
|
|
|
unsigned char buffer[32];
|
2018-12-18 01:04:35 +01:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
// Stack pointer to indirectly commit to thread/callstack
|
|
|
|
const unsigned char* ptr = buffer;
|
|
|
|
hasher.Write((const unsigned char*)&ptr, sizeof(ptr));
|
2017-05-22 22:01:52 +02:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
// Hardware randomness is very fast when available; use it always.
|
|
|
|
bool have_hw_rand = GetHardwareRand(buffer);
|
|
|
|
if (have_hw_rand) hasher.Write(buffer, sizeof(buffer));
|
2017-05-22 22:01:52 +02:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
// High-precision timestamp
|
|
|
|
SeedTimestamp(hasher);
|
2017-05-22 22:01:52 +02:00
|
|
|
}
|
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
static void SeedSlow(CSHA512& hasher)
|
|
|
|
{
|
|
|
|
unsigned char buffer[32];
|
|
|
|
|
|
|
|
// Everything that the 'fast' seeder includes
|
|
|
|
SeedFast(hasher);
|
|
|
|
|
|
|
|
// OS randomness
|
|
|
|
GetOSRand(buffer);
|
|
|
|
hasher.Write(buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
// OpenSSL RNG (for now)
|
|
|
|
RAND_bytes(buffer, sizeof(buffer));
|
|
|
|
hasher.Write(buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
// High-precision timestamp.
|
|
|
|
//
|
|
|
|
// Note that we also commit to a timestamp in the Fast seeder, so we indirectly commit to a
|
|
|
|
// benchmark of all the entropy gathering sources in this function).
|
|
|
|
SeedTimestamp(hasher);
|
2017-05-22 22:01:29 +02:00
|
|
|
}
|
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
static void SeedSleep(CSHA512& hasher)
|
2016-04-16 12:25:12 +02:00
|
|
|
{
|
2018-12-14 03:37:29 +01:00
|
|
|
// Everything that the 'fast' seeder includes
|
|
|
|
SeedFast(hasher);
|
2018-12-18 01:48:21 +01:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
// High-precision timestamp
|
|
|
|
SeedTimestamp(hasher);
|
2016-04-16 12:25:12 +02:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
// Sleep for 1ms
|
|
|
|
MilliSleep(1);
|
2016-04-16 12:25:12 +02:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
// High-precision timestamp after sleeping (as we commit to both the time before and after, this measures the delay)
|
|
|
|
SeedTimestamp(hasher);
|
2016-04-16 12:25:12 +02:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
// Windows performance monitor data (once every 10 minutes)
|
|
|
|
RandAddSeedPerfmon(hasher);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SeedStartup(CSHA512& hasher)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
RAND_screen();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Everything that the 'slow' seeder includes.
|
|
|
|
SeedSlow(hasher);
|
|
|
|
|
|
|
|
// Windows performance monitor data.
|
|
|
|
RandAddSeedPerfmon(hasher);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class RNGLevel {
|
|
|
|
FAST, //!< Automatically called by GetRandBytes
|
|
|
|
SLOW, //!< Automatically called by GetStrongRandBytes
|
|
|
|
SLEEP, //!< Called by RandAddSeedSleep()
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ProcRand(unsigned char* out, int num, RNGLevel level)
|
|
|
|
{
|
|
|
|
// Make sure the RNG is initialized first (as all Seed* function possibly need hwrand to be available).
|
|
|
|
RNGState& rng = GetRNGState();
|
|
|
|
|
|
|
|
assert(num <= 32);
|
|
|
|
|
|
|
|
CSHA512 hasher;
|
|
|
|
switch (level) {
|
|
|
|
case RNGLevel::FAST:
|
|
|
|
SeedFast(hasher);
|
|
|
|
break;
|
|
|
|
case RNGLevel::SLOW:
|
|
|
|
SeedSlow(hasher);
|
|
|
|
break;
|
|
|
|
case RNGLevel::SLEEP:
|
|
|
|
SeedSleep(hasher);
|
|
|
|
break;
|
2017-05-10 00:13:00 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:07:53 +02:00
|
|
|
// Combine with and update state
|
2018-12-14 03:37:29 +01:00
|
|
|
if (!rng.MixExtract(out, num, std::move(hasher), false)) {
|
|
|
|
// On the first invocation, also seed with SeedStartup().
|
|
|
|
CSHA512 startup_hasher;
|
|
|
|
SeedStartup(startup_hasher);
|
|
|
|
rng.MixExtract(out, num, std::move(startup_hasher), true);
|
|
|
|
}
|
2017-05-03 12:07:53 +02:00
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
// For anything but the 'fast' level, feed the resulting RNG output (after an additional hashing step) back into OpenSSL.
|
|
|
|
if (level != RNGLevel::FAST) {
|
|
|
|
unsigned char buf[64];
|
|
|
|
CSHA512().Write(out, num).Finalize(buf);
|
|
|
|
RAND_add(buf, sizeof(buf), num);
|
|
|
|
memory_cleanse(buf, 64);
|
|
|
|
}
|
2016-04-16 12:25:12 +02:00
|
|
|
}
|
|
|
|
|
2018-12-14 03:37:29 +01:00
|
|
|
void GetRandBytes(unsigned char* buf, int num) { ProcRand(buf, num, RNGLevel::FAST); }
|
|
|
|
void GetStrongRandBytes(unsigned char* buf, int num) { ProcRand(buf, num, RNGLevel::SLOW); }
|
|
|
|
void RandAddSeedSleep() { ProcRand(nullptr, 0, RNGLevel::SLEEP); }
|
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
uint64_t GetRand(uint64_t nMax)
|
|
|
|
{
|
|
|
|
if (nMax == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// The range of the random source must be a multiple of the modulus
|
|
|
|
// to give every possible output value an equal possibility
|
|
|
|
uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
|
|
|
|
uint64_t nRand = 0;
|
|
|
|
do {
|
|
|
|
GetRandBytes((unsigned char*)&nRand, sizeof(nRand));
|
|
|
|
} while (nRand >= nRange);
|
|
|
|
return (nRand % nMax);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetRandInt(int nMax)
|
|
|
|
{
|
|
|
|
return GetRand(nMax);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 GetRandHash()
|
|
|
|
{
|
|
|
|
uint256 hash;
|
|
|
|
GetRandBytes((unsigned char*)&hash, sizeof(hash));
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2017-02-16 02:45:22 +01:00
|
|
|
void FastRandomContext::RandomSeed()
|
2014-06-26 14:41:53 +02:00
|
|
|
{
|
2017-02-16 02:45:22 +01:00
|
|
|
uint256 seed = GetRandHash();
|
|
|
|
rng.SetKey(seed.begin(), 32);
|
|
|
|
requires_seed = false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:04:31 +02:00
|
|
|
uint256 FastRandomContext::rand256()
|
|
|
|
{
|
|
|
|
if (bytebuf_size < 32) {
|
|
|
|
FillByteBuffer();
|
|
|
|
}
|
|
|
|
uint256 ret;
|
|
|
|
memcpy(ret.begin(), bytebuf + 64 - bytebuf_size, 32);
|
|
|
|
bytebuf_size -= 32;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
|
|
|
|
{
|
2018-10-31 22:25:11 +01:00
|
|
|
if (requires_seed) RandomSeed();
|
2017-05-02 20:04:31 +02:00
|
|
|
std::vector<unsigned char> ret(len);
|
|
|
|
if (len > 0) {
|
|
|
|
rng.Output(&ret[0], len);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-16 02:45:22 +01:00
|
|
|
FastRandomContext::FastRandomContext(const uint256& seed) : requires_seed(false), bytebuf_size(0), bitbuf_size(0)
|
|
|
|
{
|
|
|
|
rng.SetKey(seed.begin(), 32);
|
2014-06-26 14:41:53 +02:00
|
|
|
}
|
2016-10-13 16:19:20 +02:00
|
|
|
|
2017-02-22 08:02:50 +01:00
|
|
|
bool Random_SanityCheck()
|
|
|
|
{
|
2017-05-05 20:32:06 +02:00
|
|
|
uint64_t start = GetPerformanceCounter();
|
|
|
|
|
2017-02-22 08:02:50 +01:00
|
|
|
/* This does not measure the quality of randomness, but it does test that
|
|
|
|
* OSRandom() overwrites all 32 bytes of the output given a maximum
|
|
|
|
* number of tries.
|
|
|
|
*/
|
|
|
|
static const ssize_t MAX_TRIES = 1024;
|
|
|
|
uint8_t data[NUM_OS_RANDOM_BYTES];
|
|
|
|
bool overwritten[NUM_OS_RANDOM_BYTES] = {}; /* Tracks which bytes have been overwritten at least once */
|
|
|
|
int num_overwritten;
|
|
|
|
int tries = 0;
|
|
|
|
/* Loop until all bytes have been overwritten at least once, or max number tries reached */
|
|
|
|
do {
|
|
|
|
memset(data, 0, NUM_OS_RANDOM_BYTES);
|
|
|
|
GetOSRand(data);
|
|
|
|
for (int x=0; x < NUM_OS_RANDOM_BYTES; ++x) {
|
|
|
|
overwritten[x] |= (data[x] != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
num_overwritten = 0;
|
|
|
|
for (int x=0; x < NUM_OS_RANDOM_BYTES; ++x) {
|
|
|
|
if (overwritten[x]) {
|
|
|
|
num_overwritten += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tries += 1;
|
|
|
|
} while (num_overwritten < NUM_OS_RANDOM_BYTES && tries < MAX_TRIES);
|
2017-05-05 20:32:06 +02:00
|
|
|
if (num_overwritten != NUM_OS_RANDOM_BYTES) return false; /* If this failed, bailed out after too many tries */
|
|
|
|
|
|
|
|
// Check that GetPerformanceCounter increases at least during a GetOSRand() call + 1ms sleep.
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
uint64_t stop = GetPerformanceCounter();
|
|
|
|
if (stop == start) return false;
|
|
|
|
|
2017-05-05 20:45:37 +02:00
|
|
|
// We called GetPerformanceCounter. Use it as entropy.
|
2018-12-14 03:37:29 +01:00
|
|
|
CSHA512 to_add;
|
|
|
|
to_add.Write((const unsigned char*)&start, sizeof(start));
|
|
|
|
to_add.Write((const unsigned char*)&stop, sizeof(stop));
|
|
|
|
GetRNGState().MixExtract(nullptr, 0, std::move(to_add), false);
|
2017-05-05 20:45:37 +02:00
|
|
|
|
2017-05-05 20:32:06 +02:00
|
|
|
return true;
|
2017-02-22 08:02:50 +01:00
|
|
|
}
|
2017-02-16 02:45:22 +01:00
|
|
|
|
|
|
|
FastRandomContext::FastRandomContext(bool fDeterministic) : requires_seed(!fDeterministic), bytebuf_size(0), bitbuf_size(0)
|
|
|
|
{
|
|
|
|
if (!fDeterministic) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint256 seed;
|
|
|
|
rng.SetKey(seed.begin(), 32);
|
|
|
|
}
|
2017-05-10 00:13:00 +02:00
|
|
|
|
2018-10-31 23:41:13 +01:00
|
|
|
FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexcept
|
|
|
|
{
|
|
|
|
requires_seed = from.requires_seed;
|
|
|
|
rng = from.rng;
|
|
|
|
std::copy(std::begin(from.bytebuf), std::end(from.bytebuf), std::begin(bytebuf));
|
|
|
|
bytebuf_size = from.bytebuf_size;
|
|
|
|
bitbuf = from.bitbuf;
|
|
|
|
bitbuf_size = from.bitbuf_size;
|
|
|
|
from.requires_seed = true;
|
|
|
|
from.bytebuf_size = 0;
|
|
|
|
from.bitbuf_size = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-05-10 00:13:00 +02:00
|
|
|
void RandomInit()
|
|
|
|
{
|
2018-12-18 01:48:21 +01:00
|
|
|
// Invoke RNG code to trigger initialization (if not already performed)
|
2018-12-14 03:37:29 +01:00
|
|
|
ProcRand(nullptr, 0, RNGLevel::FAST);
|
2018-12-18 01:48:21 +01:00
|
|
|
|
2019-01-17 00:15:21 +01:00
|
|
|
ReportHardwareRand();
|
2017-05-10 00:13:00 +02:00
|
|
|
}
|