Skip to content

Commit 001a53d

Browse files
author
Philip Kaufmann
committed
add GetRandBytes() as wrapper for RAND_bytes()
- add a small wrapper in util around RAND_bytes() and replace with GetRandBytes() in the code to log errors from calling RAND_bytes() - remove OpenSSL header rand.h where no longer needed
1 parent 2ee918d commit 001a53d

File tree

8 files changed

+31
-30
lines changed

8 files changed

+31
-30
lines changed

src/addrman.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include <stdint.h>
1717
#include <vector>
1818

19-
#include <openssl/rand.h>
20-
2119
/** Extended statistics about a CAddress */
2220
class CAddrInfo : public CAddress
2321
{
@@ -384,7 +382,7 @@ class CAddrMan
384382
CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
385383
{
386384
nKey.resize(32);
387-
RAND_bytes(&nKey[0], 32);
385+
GetRandBytes(&nKey[0], 32);
388386

389387
nIdCount = 0;
390388
nTried = 0;

src/key.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Copyright (c) 2009-2013 The Bitcoin developers
1+
// Copyright (c) 2009-2014 The Bitcoin developers
22
// Distributed under the MIT/X11 software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include "key.h"
66

77
#include "crypto/sha2.h"
8-
#include <openssl/rand.h>
8+
#include "random.h"
99

1010
#ifdef USE_SECP256K1
1111
#include <secp256k1.h>
@@ -194,7 +194,7 @@ class CECKey {
194194
if (d2i_ECPrivateKey(&pkey, &pbegin, privkey.size())) {
195195
if(fSkipCheck)
196196
return true;
197-
197+
198198
// d2i_ECPrivateKey returns true if parsing succeeds.
199199
// This doesn't necessarily mean the key is valid.
200200
if (EC_KEY_check_key(pkey))
@@ -412,7 +412,7 @@ bool CKey::CheckSignatureElement(const unsigned char *vch, int len, bool half) {
412412

413413
void CKey::MakeNewKey(bool fCompressedIn) {
414414
do {
415-
RAND_bytes(vch, sizeof(vch));
415+
GetRandBytes(vch, sizeof(vch));
416416
} while (!Check(vch));
417417
fValid = true;
418418
fCompressed = fCompressedIn;
@@ -745,5 +745,3 @@ bool ECC_InitSanityCheck() {
745745
return true;
746746
#endif
747747
}
748-
749-

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4370,7 +4370,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
43704370
if (pingSend) {
43714371
uint64_t nonce = 0;
43724372
while (nonce == 0) {
4373-
RAND_bytes((unsigned char*)&nonce, sizeof(nonce));
4373+
GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
43744374
}
43754375
pto->fPingQueued = false;
43764376
pto->nPingUsecStart = GetTimeMicros();

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ void CNode::PushVersion()
546546
int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
547547
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0)));
548548
CAddress addrMe = GetLocalAddress(&addr);
549-
RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
549+
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
550550
if (fLogIPs)
551551
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id);
552552
else
@@ -1931,7 +1931,7 @@ bool CAddrDB::Write(const CAddrMan& addr)
19311931
{
19321932
// Generate random temporary filename
19331933
unsigned short randv = 0;
1934-
RAND_bytes((unsigned char *)&randv, sizeof(randv));
1934+
GetRandBytes((unsigned char*)&randv, sizeof(randv));
19351935
std::string tmpfn = strprintf("peers.dat.%04x", randv);
19361936

19371937
// serialize addresses, checksum data up to that point, then append csum

src/rpcserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ void StartRPCThreads()
531531
(mapArgs["-rpcuser"] == mapArgs["-rpcpassword"])) && Params().RequireRPCPassword())
532532
{
533533
unsigned char rand_pwd[32];
534-
RAND_bytes(rand_pwd, 32);
534+
GetRandBytes(rand_pwd, 32);
535535
string strWhatAmI = "To use bitcoind";
536536
if (mapArgs.count("-server"))
537537
strWhatAmI = strprintf(_("To use the %s option"), "\"-server\"");

src/util.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#include <boost/program_options/detail/config_file.hpp>
7070
#include <boost/program_options/parsers.hpp>
7171
#include <openssl/crypto.h>
72+
#include <openssl/err.h>
7273
#include <openssl/rand.h>
7374

7475
// Work around clang compilation problem in Boost 1.46:
@@ -141,12 +142,14 @@ class CInit
141142
}
142143
instance_of_cinit;
143144

144-
145-
146-
147-
148-
149-
145+
bool GetRandBytes(unsigned char *buf, int num)
146+
{
147+
if (RAND_bytes(buf, num) == 0) {
148+
LogPrint("rand", "%s : OpenSSL RAND_bytes() failed with error: %s\n", __func__, ERR_error_string(ERR_get_error(), NULL));
149+
return false;
150+
}
151+
return true;
152+
}
150153

151154
void RandAddSeed()
152155
{
@@ -207,9 +210,9 @@ uint64_t GetRand(uint64_t nMax)
207210
// to give every possible output value an equal possibility
208211
uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
209212
uint64_t nRand = 0;
210-
do
211-
RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
212-
while (nRand >= nRange);
213+
do {
214+
GetRandBytes((unsigned char*)&nRand, sizeof(nRand));
215+
} while (nRand >= nRange);
213216
return (nRand % nMax);
214217
}
215218

@@ -221,7 +224,7 @@ int GetRandInt(int nMax)
221224
uint256 GetRandHash()
222225
{
223226
uint256 hash;
224-
RAND_bytes((unsigned char*)&hash, sizeof(hash));
227+
GetRandBytes((unsigned char*)&hash, sizeof(hash));
225228
return hash;
226229
}
227230

@@ -1196,18 +1199,18 @@ uint32_t insecure_rand_Rz = 11;
11961199
uint32_t insecure_rand_Rw = 11;
11971200
void seed_insecure_rand(bool fDeterministic)
11981201
{
1199-
//The seed values have some unlikely fixed points which we avoid.
1202+
// The seed values have some unlikely fixed points which we avoid.
12001203
if(fDeterministic)
12011204
{
12021205
insecure_rand_Rz = insecure_rand_Rw = 11;
12031206
} else {
12041207
uint32_t tmp;
12051208
do {
1206-
RAND_bytes((unsigned char*)&tmp, 4);
1209+
GetRandBytes((unsigned char*)&tmp, 4);
12071210
} while(tmp == 0 || tmp == 0x9068ffffU);
12081211
insecure_rand_Rz = tmp;
12091212
do {
1210-
RAND_bytes((unsigned char*)&tmp, 4);
1213+
GetRandBytes((unsigned char*)&tmp, 4);
12111214
} while(tmp == 0 || tmp == 0x464fffffU);
12121215
insecure_rand_Rw = tmp;
12131216
}

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ extern bool fLogTimestamps;
103103
extern bool fLogIPs;
104104
extern volatile bool fReopenDebugLog;
105105

106+
bool GetRandBytes(unsigned char *buf, int num);
106107
void RandAddSeed();
107108
void RandAddSeedPerfmon();
108109
void SetupEnvironment();

src/wallet.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "timedata.h"
1313

1414
#include <boost/algorithm/string/replace.hpp>
15-
#include <openssl/rand.h>
1615

1716
using namespace std;
1817

@@ -384,13 +383,15 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
384383
RandAddSeedPerfmon();
385384

386385
vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
387-
RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
386+
if (!GetRandBytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE))
387+
return false;
388388

389389
CMasterKey kMasterKey;
390-
391390
RandAddSeedPerfmon();
391+
392392
kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
393-
RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
393+
if (!GetRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE))
394+
return false;
394395

395396
CCrypter crypter;
396397
int64_t nStartTime = GetTimeMillis();

0 commit comments

Comments
 (0)