Browse Source

Add perf counter data to GetStrongRandBytes state in scheduler

0.15
Matt Corallo 8 years ago
parent
commit
888cce57a6
  1. 17
      src/random.cpp
  2. 7
      src/random.h
  3. 6
      src/scheduler.cpp

17
src/random.cpp

@ -203,6 +203,23 @@ void GetRandBytes(unsigned char* buf, int num) @@ -203,6 +203,23 @@ void GetRandBytes(unsigned char* buf, int num)
}
}
static void AddDataToRng(void* data, size_t len);
void RandAddSeedSleep()
{
int64_t nPerfCounter1 = GetPerformanceCounter();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
int64_t nPerfCounter2 = GetPerformanceCounter();
// Combine with and update state
AddDataToRng(&nPerfCounter1, sizeof(nPerfCounter1));
AddDataToRng(&nPerfCounter2, sizeof(nPerfCounter2));
memory_cleanse(&nPerfCounter1, sizeof(nPerfCounter1));
memory_cleanse(&nPerfCounter2, sizeof(nPerfCounter2));
}
static std::mutex cs_rng_state;
static unsigned char rng_state[32] = {0};
static uint64_t rng_counter = 0;

7
src/random.h

@ -23,6 +23,13 @@ uint64_t GetRand(uint64_t nMax); @@ -23,6 +23,13 @@ uint64_t GetRand(uint64_t nMax);
int GetRandInt(int nMax);
uint256 GetRandHash();
/**
* Add a little bit of randomness to the output of GetStrongRangBytes.
* This sleeps for a millisecond, so should only be called when there is
* no other work to be done.
*/
void RandAddSeedSleep();
/**
* Function to gather random data from multiple sources, failing whenever any
* of those source fail to provide a result.

6
src/scheduler.cpp

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
#include "scheduler.h"
#include "random.h"
#include "reverselock.h"
#include <assert.h>
@ -39,6 +40,11 @@ void CScheduler::serviceQueue() @@ -39,6 +40,11 @@ void CScheduler::serviceQueue()
// is called.
while (!shouldStop()) {
try {
if (!shouldStop() && taskQueue.empty()) {
reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
// Use this chance to get a tiny bit more entropy
RandAddSeedSleep();
}
while (!shouldStop() && taskQueue.empty()) {
// Wait until there is something to do.
newTaskScheduled.wait(lock);

Loading…
Cancel
Save