mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 20:03:34 -03:00
Merge #10338: Maintain state across GetStrongRandBytes calls
97477c5
Maintain state across GetStrongRandBytes calls (Pieter Wuille)
Tree-SHA512: 77e9b1f3c6eeb0c2a3e0c64358150767222ff0b7120ccd5f4ae0276cea0e4fa275c1b757e3f20be07dc0b4ef07f70ab0b70112080c8d3d0cb6ed703db8a59168
This commit is contained in:
commit
daf3e7def7
1 changed files with 16 additions and 1 deletions
|
@ -34,6 +34,8 @@
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
#include <openssl/rand.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)
|
void GetStrongRandBytes(unsigned char* out, int num)
|
||||||
{
|
{
|
||||||
assert(num <= 32);
|
assert(num <= 32);
|
||||||
|
@ -216,8 +222,17 @@ void GetStrongRandBytes(unsigned char* out, int num)
|
||||||
GetOSRand(buf);
|
GetOSRand(buf);
|
||||||
hasher.Write(buf, 32);
|
hasher.Write(buf, 32);
|
||||||
|
|
||||||
// Produce output
|
// 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);
|
hasher.Finalize(buf);
|
||||||
|
memcpy(rng_state, buf + 32, 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Produce output
|
||||||
memcpy(out, buf, num);
|
memcpy(out, buf, num);
|
||||||
memory_cleanse(buf, 64);
|
memory_cleanse(buf, 64);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue