Changeset View
Changeset View
Standalone View
Standalone View
src/random.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <random.h> | #include <random.h> | ||||
#ifdef WIN32 | #ifdef WIN32 | ||||
#include <compat.h> // for Windows API | #include <compat.h> // for Windows API | ||||
#include <wincrypt.h> | #include <wincrypt.h> | ||||
#endif | #endif | ||||
#include <crypto/sha512.h> | #include <crypto/sha512.h> | ||||
#include <logging.h> // for LogPrint() | #include <logging.h> // for LogPrint() | ||||
#include <support/allocators/secure.h> | |||||
#include <support/cleanse.h> | #include <support/cleanse.h> | ||||
#include <sync.h> // for WAIT_LOCK | #include <sync.h> // for WAIT_LOCK | ||||
#include <util/time.h> // for GetTime() | #include <util/time.h> // for GetTime() | ||||
#include <openssl/conf.h> | #include <openssl/conf.h> | ||||
#include <openssl/err.h> | #include <openssl/err.h> | ||||
#include <openssl/rand.h> | #include <openssl/rand.h> | ||||
#include <chrono> | #include <chrono> | ||||
#include <cstdlib> | #include <cstdlib> | ||||
#include <limits> | #include <limits> | ||||
#include <mutex> | #include <mutex> | ||||
#include <thread> | #include <thread> | ||||
#include <support/allocators/secure.h> | |||||
#ifndef WIN32 | #ifndef WIN32 | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <sys/time.h> | #include <sys/time.h> | ||||
#endif | #endif | ||||
#ifdef HAVE_SYS_GETRANDOM | #ifdef HAVE_SYS_GETRANDOM | ||||
#include <linux/random.h> | #include <linux/random.h> | ||||
#include <sys/syscall.h> | #include <sys/syscall.h> | ||||
▲ Show 20 Lines • Show All 499 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
void GetStrongRandBytes(uint8_t *buf, int num) noexcept { | void GetStrongRandBytes(uint8_t *buf, int num) noexcept { | ||||
ProcRand(buf, num, RNGLevel::SLOW); | ProcRand(buf, num, RNGLevel::SLOW); | ||||
} | } | ||||
void RandAddSeedSleep() { | void RandAddSeedSleep() { | ||||
ProcRand(nullptr, 0, RNGLevel::SLEEP); | ProcRand(nullptr, 0, RNGLevel::SLEEP); | ||||
} | } | ||||
bool g_mock_deterministic_tests{false}; | |||||
uint64_t GetRand(uint64_t nMax) noexcept { | uint64_t GetRand(uint64_t nMax) noexcept { | ||||
return FastRandomContext().randrange(nMax); | return FastRandomContext(g_mock_deterministic_tests).randrange(nMax); | ||||
} | } | ||||
int GetRandInt(int nMax) noexcept { | int GetRandInt(int nMax) noexcept { | ||||
return GetRand(nMax); | return GetRand(nMax); | ||||
} | } | ||||
uint256 GetRandHash() noexcept { | uint256 GetRandHash() noexcept { | ||||
uint256 hash; | uint256 hash; | ||||
▲ Show 20 Lines • Show All 122 Lines • Show Last 20 Lines |