diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2017-05-09 10:13:45 -0700 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2017-05-09 10:24:45 -0700 |
commit | daf3e7def7b9e5db7a32f5a20b5c4e09e3f0dd18 (patch) | |
tree | 1388dffdbc8286322ab244cfc5bb53c50ec04a69 /src/random.cpp | |
parent | bc64b5aa0fc543fe8fd3dbaec275f89df44dc409 (diff) | |
parent | 97477c537e9e9d9b9a14ec2f965c9c6121ac818b (diff) |
Merge #10338: Maintain state across GetStrongRandBytes calls
97477c5 Maintain state across GetStrongRandBytes calls (Pieter Wuille)
Tree-SHA512: 77e9b1f3c6eeb0c2a3e0c64358150767222ff0b7120ccd5f4ae0276cea0e4fa275c1b757e3f20be07dc0b4ef07f70ab0b70112080c8d3d0cb6ed703db8a59168
Diffstat (limited to 'src/random.cpp')
-rw-r--r-- | src/random.cpp | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/random.cpp b/src/random.cpp index 805d7d3872..3b9df3edaa 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -34,6 +34,8 @@ #include <sys/sysctl.h> #endif +#include <mutex> + #include <openssl/err.h> #include <openssl/rand.h> @@ -201,6 +203,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); @@ -216,8 +222,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); } |