15
15
#include " util.h" // for LogPrint()
16
16
#include " utilstrencodings.h" // for GetTime()
17
17
18
+ #include < stdlib.h>
18
19
#include < limits>
19
20
20
21
#ifndef WIN32
24
25
#include < openssl/err.h>
25
26
#include < openssl/rand.h>
26
27
28
+ static void RandFailure ()
29
+ {
30
+ LogPrintf (" Failed to read randomness, aborting\n " );
31
+ abort ();
32
+ }
33
+
27
34
static inline int64_t GetPerformanceCounter ()
28
35
{
29
36
int64_t nCounter = 0 ;
@@ -91,17 +98,25 @@ static void GetOSRand(unsigned char *ent32)
91
98
#ifdef WIN32
92
99
HCRYPTPROV hProvider;
93
100
int ret = CryptAcquireContextW (&hProvider, NULL , NULL , PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
94
- assert (ret);
101
+ if (!ret) {
102
+ RandFailure ();
103
+ }
95
104
ret = CryptGenRandom (hProvider, 32 , ent32);
96
- assert (ret);
105
+ if (!ret) {
106
+ RandFailure ();
107
+ }
97
108
CryptReleaseContext (hProvider, 0 );
98
109
#else
99
110
int f = open (" /dev/urandom" , O_RDONLY);
100
- assert (f != -1 );
111
+ if (f == -1 ) {
112
+ RandFailure ();
113
+ }
101
114
int have = 0 ;
102
115
do {
103
116
ssize_t n = read (f, ent32 + have, 32 - have);
104
- assert (n > 0 && n <= 32 - have);
117
+ if (n <= 0 || n + have > 32 ) {
118
+ RandFailure ();
119
+ }
105
120
have += n;
106
121
} while (have < 32 );
107
122
close (f);
@@ -111,8 +126,7 @@ static void GetOSRand(unsigned char *ent32)
111
126
void GetRandBytes (unsigned char * buf, int num)
112
127
{
113
128
if (RAND_bytes (buf, num) != 1 ) {
114
- LogPrintf (" %s: OpenSSL RAND_bytes() failed with error: %s\n " , __func__, ERR_error_string (ERR_get_error (), NULL ));
115
- assert (false );
129
+ RandFailure ();
116
130
}
117
131
}
118
132
0 commit comments