File tree Expand file tree Collapse file tree 4 files changed +42
-28
lines changed
Expand file tree Collapse file tree 4 files changed +42
-28
lines changed Original file line number Diff line number Diff line change @@ -687,9 +687,15 @@ bool InitSanityCheck(void)
687687 InitError (" Elliptic curve cryptography sanity check failure. Aborting." );
688688 return false ;
689689 }
690+
690691 if (!glibc_sanity_test () || !glibcxx_sanity_test ())
691692 return false ;
692693
694+ if (!Random_SanityCheck ()) {
695+ InitError (" OS cryptographic RNG sanity check failure. Aborting." );
696+ return false ;
697+ }
698+
693699 return true ;
694700}
695701
Original file line number Diff line number Diff line change @@ -239,3 +239,33 @@ FastRandomContext::FastRandomContext(bool fDeterministic)
239239 }
240240}
241241
242+ bool Random_SanityCheck ()
243+ {
244+ /* This does not measure the quality of randomness, but it does test that
245+ * OSRandom() overwrites all 32 bytes of the output given a maximum
246+ * number of tries.
247+ */
248+ static const ssize_t MAX_TRIES = 1024 ;
249+ uint8_t data[NUM_OS_RANDOM_BYTES];
250+ bool overwritten[NUM_OS_RANDOM_BYTES] = {}; /* Tracks which bytes have been overwritten at least once */
251+ int num_overwritten;
252+ int tries = 0 ;
253+ /* Loop until all bytes have been overwritten at least once, or max number tries reached */
254+ do {
255+ memset (data, 0 , NUM_OS_RANDOM_BYTES);
256+ GetOSRand (data);
257+ for (int x=0 ; x < NUM_OS_RANDOM_BYTES; ++x) {
258+ overwritten[x] |= (data[x] != 0 );
259+ }
260+
261+ num_overwritten = 0 ;
262+ for (int x=0 ; x < NUM_OS_RANDOM_BYTES; ++x) {
263+ if (overwritten[x]) {
264+ num_overwritten += 1 ;
265+ }
266+ }
267+
268+ tries += 1 ;
269+ } while (num_overwritten < NUM_OS_RANDOM_BYTES && tries < MAX_TRIES);
270+ return (num_overwritten == NUM_OS_RANDOM_BYTES); /* If this failed, bailed out after too many tries */
271+ }
Original file line number Diff line number Diff line change @@ -58,4 +58,9 @@ static const ssize_t NUM_OS_RANDOM_BYTES = 32;
5858 */
5959void GetOSRand (unsigned char *ent32);
6060
61+ /* * Check that OS randomness is available and returning the requested number
62+ * of bytes.
63+ */
64+ bool Random_SanityCheck ();
65+
6166#endif // BITCOIN_RANDOM_H
Original file line number Diff line number Diff line change 1010
1111BOOST_FIXTURE_TEST_SUITE (random_tests, BasicTestingSetup)
1212
13- static const ssize_t MAX_TRIES = 1024;
14-
1513BOOST_AUTO_TEST_CASE(osrandom_tests)
1614{
17- /* This does not measure the quality of randomness, but it does test that
18- * OSRandom() overwrites all 32 bytes of the output given a maximum
19- * number of tries.
20- */
21- uint8_t data[NUM_OS_RANDOM_BYTES];
22- bool overwritten[NUM_OS_RANDOM_BYTES] = {}; /* Tracks which bytes have been overwritten at least once */
23- int num_overwritten;
24- int tries = 0 ;
25- /* Loop until all bytes have been overwritten at least once */
26- do {
27- memset (data, 0 , NUM_OS_RANDOM_BYTES);
28- GetOSRand (data);
29- for (int x=0 ; x < NUM_OS_RANDOM_BYTES; ++x) {
30- overwritten[x] |= (data[x] != 0 );
31- }
32-
33- num_overwritten = 0 ;
34- for (int x=0 ; x < NUM_OS_RANDOM_BYTES; ++x) {
35- if (overwritten[x]) {
36- num_overwritten += 1 ;
37- }
38- }
39-
40- tries += 1 ;
41- } while (num_overwritten < NUM_OS_RANDOM_BYTES && tries < MAX_TRIES);
42- BOOST_CHECK (num_overwritten == NUM_OS_RANDOM_BYTES); /* If this failed, bailed out after too many tries */
15+ BOOST_CHECK (Random_SanityCheck ());
4316}
4417
4518BOOST_AUTO_TEST_SUITE_END ()
You can’t perform that action at this time.
0 commit comments