Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Replace OpenSSL PRNG with built-in Fortuna implementation #5885

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,23 @@ libbitcoin_wallet_a_SOURCES = \
# crypto primitives library
crypto_libbitcoin_crypto_a_CPPFLAGS = $(BITCOIN_CONFIG_INCLUDES)
crypto_libbitcoin_crypto_a_SOURCES = \
crypto/aes.cpp \
crypto/sha1.cpp \
crypto/sha256.cpp \
crypto/sha512.cpp \
crypto/hmac_sha256.cpp \
crypto/hmac_sha512.cpp \
crypto/ripemd160.cpp \
crypto/fortuna.cpp \
crypto/common.h \
crpyot/aes.h \
crypto/sha256.h \
crypto/sha512.h \
crypto/hmac_sha256.h \
crypto/hmac_sha512.h \
crypto/sha1.h \
crypto/ripemd160.h
crypto/ripemd160.h \
crypto/fortuna.h

# univalue JSON library
univalue_libbitcoin_univalue_a_SOURCES = \
Expand Down
14 changes: 8 additions & 6 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@

using namespace std;

int CAddrInfo::GetTriedBucket(const std::vector<unsigned char>& nKey) const
int CAddrInfo::GetTriedBucket(const uint256& nKey) const
{
CDataStream ss1(SER_GETHASH, 0);
std::vector<unsigned char> vchKey = GetKey();
ss1 << nKey << vchKey;
ss1 << ((unsigned char)32) << nKey << vchKey;
uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetCheapHash();

CDataStream ss2(SER_GETHASH, 0);
std::vector<unsigned char> vchGroupKey = GetGroup();
ss2 << nKey << vchGroupKey << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP);
ss2 << ((unsigned char)32) << nKey << vchGroupKey << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP);
uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetCheapHash();
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
}

int CAddrInfo::GetNewBucket(const std::vector<unsigned char>& nKey, const CNetAddr& src) const
int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src) const
{
CDataStream ss1(SER_GETHASH, 0);
std::vector<unsigned char> vchGroupKey = GetGroup();
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup();
ss1 << nKey << vchGroupKey << vchSourceGroupKey;
ss1 << ((unsigned char)32) << nKey << vchGroupKey << vchSourceGroupKey;
uint64_t hash1 = Hash(ss1.begin(), ss1.end()).GetCheapHash();

CDataStream ss2(SER_GETHASH, 0);
ss2 << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP);
ss2 << ((unsigned char)32) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP);
uint64_t hash2 = Hash(ss2.begin(), ss2.end()).GetCheapHash();
return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
}
Expand Down Expand Up @@ -482,6 +482,8 @@ int CAddrMan::Check_()
return -13;
if (mapNew.size())
return -15;
if (nKey.IsNull())
return -16;

return 0;
}
Expand Down
21 changes: 15 additions & 6 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "random.h"
#include "sync.h"
#include "timedata.h"
#include "uint256.h"
#include "util.h"

#include <map>
Expand Down Expand Up @@ -79,13 +80,13 @@ class CAddrInfo : public CAddress
}

//! Calculate in which "tried" bucket this entry belongs
int GetTriedBucket(const std::vector<unsigned char> &nKey) const;
int GetTriedBucket(const uint256 &nKey) const;

//! Calculate in which "new" bucket this entry belongs, given a certain source
int GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAddr& src) const;
int GetNewBucket(const uint256 &nKey, const CNetAddr& src) const;

//! Calculate in which "new" bucket this entry belongs, using its default source
int GetNewBucket(const std::vector<unsigned char> &nKey) const
int GetNewBucket(const uint256 &nKey) const
{
return GetNewBucket(nKey, source);
}
Expand Down Expand Up @@ -176,7 +177,7 @@ class CAddrMan
mutable CCriticalSection cs;

//! secret key to randomize bucket select with
std::vector<unsigned char> nKey;
uint256 nKey;

//! last used nId
int nIdCount;
Expand Down Expand Up @@ -284,6 +285,7 @@ class CAddrMan

unsigned char nVersion = 0;
s << nVersion;
s << ((unsigned char)32);
s << nKey;
s << nNew;
s << nTried;
Expand Down Expand Up @@ -328,6 +330,9 @@ class CAddrMan

unsigned char nVersion;
s >> nVersion;
unsigned char nKeySize;
s >> nKeySize;
if (nKeySize != 32) throw std::ios_base::failure("Incorrect keysize in addrman");
s >> nKey;
s >> nNew;
s >> nTried;
Expand Down Expand Up @@ -393,14 +398,18 @@ class CAddrMan

CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
{
nKey.resize(32);
GetRandBytes(&nKey[0], 32);
nKey = GetRandHash();

nIdCount = 0;
nTried = 0;
nNew = 0;
}

~CAddrMan()
{
nKey.SetNull();
}

//! Return the number of (unique) addresses in all tables.
int size()
{
Expand Down
2 changes: 2 additions & 0 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "primitives/transaction.h"
#include "core_io.h"
#include "coins.h"
#include "random.h"
#include "keystore.h"
#include "script/script.h"
#include "script/sign.h"
Expand Down Expand Up @@ -602,6 +603,7 @@ static int CommandLineRawTx(int argc, char* argv[])
int main(int argc, char* argv[])
{
SetupEnvironment();
RandSeedSystem(false);

try {
if(!AppInitRawTx(argc, argv))
Expand Down
9 changes: 3 additions & 6 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,18 @@ struct SeedSpec6 {
* Main network
*/

//! Convert the pnSeeds6 array into usable address objects.
static void convertSeed6(std::vector<CAddress> &vSeedsOut, const SeedSpec6 *data, unsigned int count)
//! Convert the pnSeeds6 array into usable service objects.
static void convertSeed6(std::vector<CService> &vSeedsOut, const SeedSpec6 *data, unsigned int count)
{
// It'll only connect to one or two seed nodes because once it connects,
// it'll get a pile of addresses with newer timestamps.
// Seed nodes are given a random 'last seen time' of between one and two
// weeks ago.
const int64_t nOneWeek = 7*24*60*60;
for (unsigned int i = 0; i < count; i++)
{
struct in6_addr ip;
memcpy(&ip, data[i].addr, sizeof(ip));
CAddress addr(CService(ip, data[i].port));
addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
vSeedsOut.push_back(addr);
vSeedsOut.push_back(CService(ip, data[i].port));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CChainParams
std::string NetworkIDString() const { return strNetworkID; }
const std::vector<CDNSSeedData>& DNSSeeds() const { return vSeeds; }
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
const std::vector<CAddress>& FixedSeeds() const { return vFixedSeeds; }
const std::vector<CService>& FixedSeeds() const { return vFixedSeeds; }
virtual const Checkpoints::CCheckpointData& Checkpoints() const = 0;
protected:
CChainParams() {}
Expand All @@ -95,7 +95,7 @@ class CChainParams
std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
std::string strNetworkID;
CBlock genesis;
std::vector<CAddress> vFixedSeeds;
std::vector<CService> vFixedSeeds;
bool fRequireRPCPassword;
bool fMiningRequiresPeers;
bool fDefaultCheckMemPool;
Expand Down
Loading