File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change 32
32
#include < sys/sysctl.h>
33
33
#endif
34
34
35
+ #include < mutex>
36
+
35
37
#include < openssl/err.h>
36
38
#include < openssl/rand.h>
37
39
@@ -192,6 +194,10 @@ void GetRandBytes(unsigned char* buf, int num)
192
194
}
193
195
}
194
196
197
+ static std::mutex cs_rng_state;
198
+ static unsigned char rng_state[32 ] = {0 };
199
+ static uint64_t rng_counter = 0 ;
200
+
195
201
void GetStrongRandBytes (unsigned char * out, int num)
196
202
{
197
203
assert (num <= 32 );
@@ -207,8 +213,17 @@ void GetStrongRandBytes(unsigned char* out, int num)
207
213
GetOSRand (buf);
208
214
hasher.Write (buf, 32 );
209
215
216
+ // Combine with and update state
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
+
210
226
// Produce output
211
- hasher.Finalize (buf);
212
227
memcpy (out, buf, num);
213
228
memory_cleanse (buf, 64 );
214
229
}
You can’t perform that action at this time.
0 commit comments