mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-05-21 17:42:37 +02:00
random: Remove remaining OpenSSL calls and locking infrastructure
This commit is contained in:
parent
4fcfcc294e
commit
b49b6b0f70
@ -43,10 +43,6 @@
|
|||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <openssl/err.h>
|
|
||||||
#include <openssl/rand.h>
|
|
||||||
#include <openssl/conf.h>
|
|
||||||
|
|
||||||
[[noreturn]] static void RandFailure()
|
[[noreturn]] static void RandFailure()
|
||||||
{
|
{
|
||||||
LogPrintf("Failed to read randomness, aborting\n");
|
LogPrintf("Failed to read randomness, aborting\n");
|
||||||
@ -347,8 +343,6 @@ void GetOSRand(unsigned char *ent32)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void LockingCallbackOpenSSL(int mode, int i, const char* file, int line);
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class RNGState {
|
class RNGState {
|
||||||
@ -364,31 +358,15 @@ class RNGState {
|
|||||||
unsigned char m_state[32] GUARDED_BY(m_mutex) = {0};
|
unsigned char m_state[32] GUARDED_BY(m_mutex) = {0};
|
||||||
uint64_t m_counter GUARDED_BY(m_mutex) = 0;
|
uint64_t m_counter GUARDED_BY(m_mutex) = 0;
|
||||||
bool m_strongly_seeded GUARDED_BY(m_mutex) = false;
|
bool m_strongly_seeded GUARDED_BY(m_mutex) = false;
|
||||||
std::unique_ptr<Mutex[]> m_mutex_openssl;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RNGState() noexcept
|
RNGState() noexcept
|
||||||
{
|
{
|
||||||
InitHardwareRand();
|
InitHardwareRand();
|
||||||
|
|
||||||
// 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()
|
~RNGState()
|
||||||
{
|
{
|
||||||
// Securely erase the memory used by the OpenSSL PRNG
|
|
||||||
RAND_cleanup();
|
|
||||||
// Shutdown OpenSSL library multithreading support
|
|
||||||
CRYPTO_set_locking_callback(nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Extract up to 32 bytes of entropy from the RNG state, mixing in new entropy from hasher.
|
/** Extract up to 32 bytes of entropy from the RNG state, mixing in new entropy from hasher.
|
||||||
@ -424,8 +402,6 @@ public:
|
|||||||
memory_cleanse(buf, 64);
|
memory_cleanse(buf, 64);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mutex& GetOpenSSLMutex(int i) { return m_mutex_openssl[i]; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
RNGState& GetRNGState() noexcept
|
RNGState& GetRNGState() noexcept
|
||||||
@ -437,17 +413,6 @@ RNGState& GetRNGState() noexcept
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LockingCallbackOpenSSL(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
|
|
||||||
{
|
|
||||||
RNGState& rng = GetRNGState();
|
|
||||||
|
|
||||||
if (mode & CRYPTO_LOCK) {
|
|
||||||
rng.GetOpenSSLMutex(i).lock();
|
|
||||||
} else {
|
|
||||||
rng.GetOpenSSLMutex(i).unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* A note on the use of noexcept in the seeding functions below:
|
/* A note on the use of noexcept in the seeding functions below:
|
||||||
*
|
*
|
||||||
* None of the RNG code should ever throw any exception, with the sole exception
|
* None of the RNG code should ever throw any exception, with the sole exception
|
||||||
|
Loading…
Reference in New Issue
Block a user