Skip to content

Commit

Permalink
random: Remove remaining OpenSSL calls and locking infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
fanquake authored and Fuzzbawls committed May 11, 2021
1 parent 602c0b2 commit 690c938
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@
#include <sys/sysctl.h>
#endif

#include <mutex>

#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/conf.h>

[[noreturn]] static void RandFailure()
{
LogPrintf("Failed to read randomness, aborting\n");
Expand Down Expand Up @@ -352,8 +346,6 @@ void GetOSRand(unsigned char *ent32)
#endif
}

void LockingCallbackOpenSSL(int mode, int i, const char* file, int line);

namespace {

class RNGState {
Expand All @@ -369,7 +361,6 @@ class RNGState {
unsigned char m_state[32] GUARDED_BY(m_mutex) = {0};
uint64_t m_counter GUARDED_BY(m_mutex) = 0;
bool m_strongly_seeded GUARDED_BY(m_mutex) = false;
std::unique_ptr<Mutex[]> m_mutex_openssl;

Mutex m_events_mutex;
CSHA256 m_events_hasher GUARDED_BY(m_events_mutex);
Expand All @@ -378,25 +369,10 @@ class RNGState {
RNGState() noexcept
{
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()
{
// Securely erase the memory used by the OpenSSL PRNG
RAND_cleanup();
// Shutdown OpenSSL library multithreading support
CRYPTO_set_locking_callback(nullptr);
}

void AddEvent(uint32_t event_info) noexcept
Expand Down Expand Up @@ -461,8 +437,6 @@ class RNGState {
memory_cleanse(buf, 64);
return ret;
}

Mutex& GetOpenSSLMutex(int i) { return m_mutex_openssl[i]; }
};

RNGState& GetRNGState() noexcept
Expand All @@ -474,17 +448,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:
*
* None of the RNG code should ever throw any exception.
Expand Down

0 comments on commit 690c938

Please sign in to comment.