aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-05-03 03:07:53 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2017-05-04 10:13:40 -0700
commit97477c537e9e9d9b9a14ec2f965c9c6121ac818b (patch)
treecbc36ffe905656dfcba34afeb7def273d5e10b75 /src
parent35da2aeed7d4000dde93957c3b6e048ab83c4f2b (diff)
downloadbitcoin-97477c537e9e9d9b9a14ec2f965c9c6121ac818b.tar.xz
Maintain state across GetStrongRandBytes calls
Diffstat (limited to 'src')
-rw-r--r--src/random.cpp17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/random.cpp b/src/random.cpp
index 6187f16290..6ec01b878f 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -32,6 +32,8 @@
#include <sys/sysctl.h>
#endif
+#include <mutex>
+
#include <openssl/err.h>
#include <openssl/rand.h>
@@ -192,6 +194,10 @@ void GetRandBytes(unsigned char* buf, int num)
}
}
+static std::mutex cs_rng_state;
+static unsigned char rng_state[32] = {0};
+static uint64_t rng_counter = 0;
+
void GetStrongRandBytes(unsigned char* out, int num)
{
assert(num <= 32);
@@ -207,8 +213,17 @@ void GetStrongRandBytes(unsigned char* out, int num)
GetOSRand(buf);
hasher.Write(buf, 32);
+ // Combine with and update state
+ {
+ std::unique_lock<std::mutex> lock(cs_rng_state);
+ hasher.Write(rng_state, sizeof(rng_state));
+ hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
+ ++rng_counter;
+ hasher.Finalize(buf);
+ memcpy(rng_state, buf + 32, 32);
+ }
+
// Produce output
- hasher.Finalize(buf);
memcpy(out, buf, num);
memory_cleanse(buf, 64);
}