File tree Expand file tree Collapse file tree 3 files changed +30
-0
lines changed
Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -203,6 +203,23 @@ 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+
206223static std::mutex cs_rng_state;
207224static unsigned char rng_state[32 ] = {0 };
208225static uint64_t rng_counter = 0 ;
Original file line number Diff line number Diff line change @@ -23,6 +23,13 @@ uint64_t GetRand(uint64_t nMax);
2323int GetRandInt (int nMax);
2424uint256 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.
Original file line number Diff line number Diff line change 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);
You can’t perform that action at this time.
0 commit comments