Skip to content

Commit 399fb8f

Browse files
committed
Add internal method to add new random data to our internal RNG state
1 parent daf3e7d commit 399fb8f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/random.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ static std::mutex cs_rng_state;
207207
static unsigned char rng_state[32] = {0};
208208
static uint64_t rng_counter = 0;
209209

210+
static void AddDataToRng(void* data, size_t len) {
211+
CSHA512 hasher;
212+
hasher.Write((const unsigned char*)&len, sizeof(len));
213+
hasher.Write((const unsigned char*)data, len);
214+
unsigned char buf[64];
215+
{
216+
std::unique_lock<std::mutex> lock(cs_rng_state);
217+
hasher.Write(rng_state, sizeof(rng_state));
218+
hasher.Write((const unsigned char*)&rng_counter, sizeof(rng_counter));
219+
++rng_counter;
220+
hasher.Finalize(buf);
221+
memcpy(rng_state, buf + 32, 32);
222+
}
223+
memory_cleanse(buf, 64);
224+
}
225+
210226
void GetStrongRandBytes(unsigned char* out, int num)
211227
{
212228
assert(num <= 32);

0 commit comments

Comments
 (0)