Skip to content

Commit

Permalink
Encapsulate RNGState better
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and Fuzzbawls committed Apr 14, 2021
1 parent 787d72f commit 080deb3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,14 @@ void LockingCallbackOpenSSL(int mode, int i, const char* file, int line);

namespace {

struct RNGState {
class RNGState {
Mutex m_mutex;
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;

public:
RNGState() noexcept
{
InitHardwareRand();
Expand Down Expand Up @@ -342,6 +343,8 @@ struct RNGState {
memory_cleanse(buf, 64);
return ret;
}

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

RNGState& GetRNGState() noexcept
Expand All @@ -358,9 +361,9 @@ void LockingCallbackOpenSSL(int mode, int i, const char* file, int line) NO_THRE
RNGState& rng = GetRNGState();

if (mode & CRYPTO_LOCK) {
rng.m_mutex_openssl[i].lock();
rng.GetOpenSSLMutex(i).lock();
} else {
rng.m_mutex_openssl[i].unlock();
rng.GetOpenSSLMutex(i).unlock();
}
}

Expand Down

0 comments on commit 080deb3

Please sign in to comment.