Skip to content

Commit 17dd13e

Browse files
TheBlueMattWarrows
authored andcommitted
Add internal method to add new random data to our internal RNG state
1 parent c7a1602 commit 17dd13e

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
@@ -209,6 +209,22 @@ static std::mutex cs_rng_state;
209209
static unsigned char rng_state[32] = {0};
210210
static uint64_t rng_counter = 0;
211211

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

0 commit comments

Comments
 (0)