Skip to content

Commit 15254e9

Browse files
committed
Merge #10372: Add perf counter data to GetStrongRandBytes state in scheduler
888cce5 Add perf counter data to GetStrongRandBytes state in scheduler (Matt Corallo) 399fb8f Add internal method to add new random data to our internal RNG state (Matt Corallo) Tree-SHA512: 9732a3804d015eaf48d56b60c73880014845fd17a098f1ac2eff6bd50a4beb8b8be65956ac1f0d641e96e3a53c17daddd919401edbf2e3797c3fb687730fb913
2 parents e76a392 + 888cce5 commit 15254e9

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/random.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,43 @@ void GetRandBytes(unsigned char* buf, int num)
203203
}
204204
}
205205

206+
static void AddDataToRng(void* data, size_t len);
207+
208+
void RandAddSeedSleep()
209+
{
210+
int64_t nPerfCounter1 = GetPerformanceCounter();
211+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
212+
int64_t nPerfCounter2 = GetPerformanceCounter();
213+
214+
// Combine with and update state
215+
AddDataToRng(&nPerfCounter1, sizeof(nPerfCounter1));
216+
AddDataToRng(&nPerfCounter2, sizeof(nPerfCounter2));
217+
218+
memory_cleanse(&nPerfCounter1, sizeof(nPerfCounter1));
219+
memory_cleanse(&nPerfCounter2, sizeof(nPerfCounter2));
220+
}
221+
222+
206223
static std::mutex cs_rng_state;
207224
static unsigned char rng_state[32] = {0};
208225
static uint64_t rng_counter = 0;
209226

227+
static void AddDataToRng(void* data, size_t len) {
228+
CSHA512 hasher;
229+
hasher.Write((const unsigned char*)&len, sizeof(len));
230+
hasher.Write((const unsigned char*)data, len);
231+
unsigned char buf[64];
232+
{
233+
std::unique_lock<std::mutex> lock(cs_rng_state);
234+
hasher.Write(rng_state, sizeof(rng_state));
235+
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
236+
++rng_counter;
237+
hasher.Finalize(buf);
238+
memcpy(rng_state, buf + 32, 32);
239+
}
240+
memory_cleanse(buf, 64);
241+
}
242+
210243
void GetStrongRandBytes(unsigned char* out, int num)
211244
{
212245
assert(num <= 32);

src/random.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ uint64_t GetRand(uint64_t nMax);
2323
int GetRandInt(int nMax);
2424
uint256 GetRandHash();
2525

26+
/**
27+
* Add a little bit of randomness to the output of GetStrongRangBytes.
28+
* This sleeps for a millisecond, so should only be called when there is
29+
* no other work to be done.
30+
*/
31+
void RandAddSeedSleep();
32+
2633
/**
2734
* Function to gather random data from multiple sources, failing whenever any
2835
* of those source fail to provide a result.

src/scheduler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "scheduler.h"
66

7+
#include "random.h"
78
#include "reverselock.h"
89

910
#include <assert.h>
@@ -39,6 +40,11 @@ void CScheduler::serviceQueue()
3940
// is called.
4041
while (!shouldStop()) {
4142
try {
43+
if (!shouldStop() && taskQueue.empty()) {
44+
reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
45+
// Use this chance to get a tiny bit more entropy
46+
RandAddSeedSleep();
47+
}
4248
while (!shouldStop() && taskQueue.empty()) {
4349
// Wait until there is something to do.
4450
newTaskScheduled.wait(lock);

0 commit comments

Comments
 (0)