Changeset View
Changeset View
Standalone View
Standalone View
src/random.cpp
Show First 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) | #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) | ||||
#include <cpuid.h> | #include <cpuid.h> | ||||
#endif | #endif | ||||
#include <openssl/err.h> | #include <openssl/err.h> | ||||
#include <openssl/rand.h> | #include <openssl/rand.h> | ||||
[[noreturn]] static void RandFailure() { | [[noreturn]] static void RandFailure() { | ||||
LogPrintf("Failed to read randomness, aborting\n"); | LogPrint(BCLog::RAND, "Failed to read randomness, aborting\n"); | ||||
std::abort(); | std::abort(); | ||||
} | } | ||||
static inline int64_t GetPerformanceCounter() { | static inline int64_t GetPerformanceCounter() { | ||||
// Read the hardware time stamp counter when available. | // Read the hardware time stamp counter when available. | ||||
// See https://en.wikipedia.org/wiki/Time_Stamp_Counter for more information. | // See https://en.wikipedia.org/wiki/Time_Stamp_Counter for more information. | ||||
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) | #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) | ||||
return __rdtsc(); | return __rdtsc(); | ||||
Show All 16 Lines | |||||
#if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) | #if defined(__x86_64__) || defined(__amd64__) || defined(__i386__) | ||||
static std::atomic<bool> hwrand_initialized{false}; | static std::atomic<bool> hwrand_initialized{false}; | ||||
static bool rdrand_supported = false; | static bool rdrand_supported = false; | ||||
static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000; | static constexpr uint32_t CPUID_F1_ECX_RDRAND = 0x40000000; | ||||
static void RDRandInit() { | static void RDRandInit() { | ||||
uint32_t eax, ebx, ecx, edx; | uint32_t eax, ebx, ecx, edx; | ||||
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx & CPUID_F1_ECX_RDRAND)) { | if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) && (ecx & CPUID_F1_ECX_RDRAND)) { | ||||
LogPrintf("Using RdRand as an additional entropy source\n"); | LogPrint(BCLog::RAND, "Using RdRand as an additional entropy source\n"); | ||||
rdrand_supported = true; | rdrand_supported = true; | ||||
} | } | ||||
hwrand_initialized.store(true); | hwrand_initialized.store(true); | ||||
} | } | ||||
#else | #else | ||||
static void RDRandInit() {} | static void RDRandInit() {} | ||||
#endif | #endif | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | #ifdef WIN32 | ||||
if (ret == ERROR_SUCCESS) { | if (ret == ERROR_SUCCESS) { | ||||
RAND_add(vData.data(), nSize, nSize / 100.0); | RAND_add(vData.data(), nSize, nSize / 100.0); | ||||
memory_cleanse(vData.data(), nSize); | memory_cleanse(vData.data(), nSize); | ||||
LogPrint(BCLog::RAND, "%s: %lu bytes\n", __func__, nSize); | LogPrint(BCLog::RAND, "%s: %lu bytes\n", __func__, nSize); | ||||
} else { | } else { | ||||
// Warn only once | // Warn only once | ||||
static bool warned = false; | static bool warned = false; | ||||
if (!warned) { | if (!warned) { | ||||
LogPrintf("%s: Warning: RegQueryValueExA(HKEY_PERFORMANCE_DATA) " | LogPrint(BCLog::RAND, | ||||
"%s: Warning: RegQueryValueExA(HKEY_PERFORMANCE_DATA) " | |||||
"failed with code %i\n", | "failed with code %i\n", | ||||
__func__, ret); | __func__, ret); | ||||
warned = true; | warned = true; | ||||
} | } | ||||
} | } | ||||
#endif | #endif | ||||
} | } | ||||
#ifndef WIN32 | #ifndef WIN32 | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 292 Lines • Show Last 20 Lines |