Skip to content

Commit 7cad849

Browse files
committed
sanity: Move OS random to sanity check function
Move the OS random test to a sanity check function that is called every time bitcoind is initialized. Keep `src/test/random_tests.cpp` for the case that later random tests are added, and keep a rudimentary test that just calls the sanity check.
1 parent aa09ccb commit 7cad849

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

src/init.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/random.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

src/random.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ static const ssize_t NUM_OS_RANDOM_BYTES = 32;
5858
*/
5959
void 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

src/test/random_tests.cpp

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,9 @@
1010

1111
BOOST_FIXTURE_TEST_SUITE(random_tests, BasicTestingSetup)
1212

13-
static const ssize_t MAX_TRIES = 1024;
14-
1513
BOOST_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

4518
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)