diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 1fa2644647..68198bf1ad 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -49,7 +49,7 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data { // Generate random temporary filename uint16_t randv = 0; - GetRandBytes((unsigned char*)&randv, sizeof(randv)); + GetRandBytes({(unsigned char*)&randv, sizeof(randv)}); std::string tmpfn = strprintf("%s.%04x", prefix, randv); // open temp output file, and associate with CAutoFile diff --git a/src/blind.cpp b/src/blind.cpp old mode 100644 new mode 100755 index e1a15ab05a..d84fec1f27 --- a/src/blind.cpp +++ b/src/blind.cpp @@ -194,7 +194,7 @@ bool SurjectOutput(CTxOutWitness& txoutwit, const std::vector& input_value_blinding_factors, const } // Fill out the value blinders and blank asset blinder - GetStrongRandBytes(&blind[num_blind_attempts-1][0], 32); + GetStrongRandBytes(Span(&blind[num_blind_attempts-1][0], 32)); // Issuances are not asset-blinded memset(&asset_blind[num_blind_attempts-1][0], 0, 32); value_blindptrs.push_back(&blind[num_blind_attempts-1][0]); @@ -563,8 +563,8 @@ int BlindTransaction(std::vector& input_value_blinding_factors, const asset = out.nAsset.GetAsset(); blinded_amounts.push_back(conf_value.GetAmount()); - GetStrongRandBytes(&blind[num_blind_attempts-1][0], 32); - GetStrongRandBytes(&asset_blind[num_blind_attempts-1][0], 32); + GetStrongRandBytes(Span(&blind[num_blind_attempts-1][0], 32)); + GetStrongRandBytes(Span(&asset_blind[num_blind_attempts-1][0], 32)); value_blindptrs.push_back(&blind[num_blind_attempts-1][0]); asset_blindptrs.push_back(&asset_blind[num_blind_attempts-1][0]); diff --git a/src/blindpsbt.cpp b/src/blindpsbt.cpp index fde37a5dcf..2e12c18776 100644 --- a/src/blindpsbt.cpp +++ b/src/blindpsbt.cpp @@ -42,7 +42,7 @@ bool CreateAssetSurjectionProof(std::vector& output_proof, const // 1 to 3 targets size_t inputs_to_select = std::min(num_targets, fixed_input_tags.size()); unsigned char randseed[32]; - GetStrongRandBytes(randseed, 32); + GetStrongRandBytes(Span(randseed, 32)); size_t input_index; secp256k1_surjectionproof proof; secp256k1_fixed_asset_tag fixed_output_tag; @@ -128,7 +128,7 @@ static bool CreateBlindValueProof(std::vector& rangeproof, const // Generate a new random nonce uint256 nonce; - GetStrongRandBytes(nonce.begin(), nonce.size()); + GetStrongRandBytes(Span(nonce.begin(), nonce.size())); // Make the rangeproof int res = secp256k1_rangeproof_sign(secp256k1_blind_context, rangeproof.data(), &rangeproof_len, /* min_value */ amount, &value_commit, value_blinder.begin(), nonce.begin(), /* exp */ -1, /* min_bits */ 0, amount, /* message */ nullptr, /* message_len */ 0, /* extra_commit */ nullptr, /* extra_commit_len */ 0, &gen); @@ -509,8 +509,8 @@ BlindingStatus BlindPSBT(PartiallySignedTransaction& psbt, std::map(value_blinder.begin(), value_blinder.size())); + GetStrongRandBytes(Span(asset_blinder.begin(), asset_blinder.size())); // Compute the scalar for this blinding and add to the output scalar if (!ComputeAndAddToScalarOffset(output_scalar, *output.amount, asset_blinder, value_blinder)) return BlindingStatus::SCALAR_UNABLE; diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index b0ea80ea1a..50a601c684 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -227,7 +227,7 @@ const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8; std::vector CDBWrapper::CreateObfuscateKey() const { std::vector ret(OBFUSCATE_KEY_NUM_BYTES); - GetRandBytes(ret.data(), OBFUSCATE_KEY_NUM_BYTES); + GetRandBytes(ret); return ret; } diff --git a/src/key.cpp b/src/key.cpp index 8509071d86..a12c2b3296 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -160,7 +160,7 @@ bool CKey::Check(const unsigned char *vch) { void CKey::MakeNewKey(bool fCompressedIn) { do { - GetStrongRandBytes(keydata.data(), keydata.size()); + GetStrongRandBytes(keydata); } while (!Check(keydata.data())); fValid = true; fCompressed = fCompressedIn; @@ -254,7 +254,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const { } unsigned char rnd[8]; std::string str = "Bitcoin key verification\n"; - GetRandBytes(rnd, sizeof(rnd)); + GetRandBytes(rnd); uint256 hash; CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash); std::vector vchSig; @@ -411,7 +411,7 @@ void ECC_Start() { { // Pass in a random blinding seed to the secp256k1 context. std::vector> vseed(32); - GetRandBytes(vseed.data(), 32); + GetRandBytes(vseed); bool ret = secp256k1_context_randomize(ctx, vseed.data()); assert(ret); } diff --git a/src/net_processing.cpp b/src/net_processing.cpp index f61c18cbe3..a19f4c7ee8 100755 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4536,7 +4536,7 @@ void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::mic if (pingSend) { uint64_t nonce = 0; while (nonce == 0) { - GetRandBytes((unsigned char*)&nonce, sizeof(nonce)); + GetRandBytes({(unsigned char*)&nonce, sizeof(nonce)}); } peer.m_ping_queued = false; peer.m_ping_start = now; diff --git a/src/random.cpp b/src/random.cpp index 77d2ae4d43..6ae08103b1 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include // for Mutex #include // for GetTimeMicros() @@ -578,8 +579,8 @@ static void ProcRand(unsigned char* out, int num, RNGLevel level) noexcept } } -void GetRandBytes(unsigned char* buf, int num) noexcept { ProcRand(buf, num, RNGLevel::FAST); } -void GetStrongRandBytes(unsigned char* buf, int num) noexcept { ProcRand(buf, num, RNGLevel::SLOW); } +void GetRandBytes(Span bytes) noexcept { ProcRand(bytes.data(), bytes.size(), RNGLevel::FAST); } +void GetStrongRandBytes(Span bytes) noexcept { ProcRand(bytes.data(), bytes.size(), RNGLevel::SLOW); } void RandAddPeriodic() noexcept { ProcRand(nullptr, 0, RNGLevel::PERIODIC); } void RandAddEvent(const uint32_t event_info) noexcept { GetRNGState().AddEvent(event_info); } @@ -598,7 +599,7 @@ int GetRandInt(int nMax) noexcept uint256 GetRandHash() noexcept { uint256 hash; - GetRandBytes((unsigned char*)&hash, sizeof(hash)); + GetRandBytes(hash); return hash; } diff --git a/src/random.h b/src/random.h index 97302d61ab..285158b1c3 100644 --- a/src/random.h +++ b/src/random.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -66,7 +67,7 @@ * * Thread-safe. */ -void GetRandBytes(unsigned char* buf, int num) noexcept; +void GetRandBytes(Span bytes) noexcept; /** Generate a uniform random integer in the range [0..range). Precondition: range > 0 */ uint64_t GetRand(uint64_t nMax) noexcept; /** Generate a uniform random duration in the range [0..max). Precondition: max.count() > 0 */ @@ -105,7 +106,7 @@ uint256 GetRandHash() noexcept; * * Thread-safe. */ -void GetStrongRandBytes(unsigned char* buf, int num) noexcept; +void GetStrongRandBytes(Span bytes) noexcept; /** * Gather entropy from various expensive sources, and feed them to the PRNG state. diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp index 2588f8b385..2280466616 100644 --- a/src/rpc/request.cpp +++ b/src/rpc/request.cpp @@ -82,7 +82,7 @@ bool GenerateAuthCookie(std::string *cookie_out) { const size_t COOKIE_SIZE = 32; unsigned char rand_pwd[COOKIE_SIZE]; - GetRandBytes(rand_pwd, COOKIE_SIZE); + GetRandBytes(rand_pwd); std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd); /** the umask determines what permissions are used to create this file - diff --git a/src/test/bech32_tests.cpp b/src/test/bech32_tests.cpp index b47d13709f..16fa77f006 100644 --- a/src/test/bech32_tests.cpp +++ b/src/test/bech32_tests.cpp @@ -162,8 +162,8 @@ BOOST_AUTO_TEST_CASE(bech32_polymod_sanity) { std::vector data(40); // GetRandBytes only allows 32 bytes at a time - GetRandBytes(data.data(), 32); - GetRandBytes(data.data() + 32, data.size() - 32); + GetRandBytes(Span(data.data(), 32)); + GetRandBytes(Span(data.data() + 32, data.size() - 32)); std::vector base32; ConvertBits<8, 5, true>([&](unsigned char c) { base32.push_back(c); }, data.begin(), data.end()); diff --git a/src/test/blech32_tests.cpp b/src/test/blech32_tests.cpp index 95e69213a8..4e2fbd60ba 100644 --- a/src/test/blech32_tests.cpp +++ b/src/test/blech32_tests.cpp @@ -104,8 +104,8 @@ BOOST_AUTO_TEST_CASE(blech32_polymod_sanity) { std::vector data(40); // GetRandBytes only allows 32 bytes at a time - GetRandBytes(data.data(), 32); - GetRandBytes(data.data() + 32, data.size() - 32); + GetRandBytes(Span(data.data(), 32)); + GetRandBytes(Span(data.data() + 32, data.size() - 32)); std::vector base32; ConvertBits<8, 5, true>([&](unsigned char c) { base32.push_back(c); }, data.begin(), data.end()); diff --git a/src/test/key_tests.cpp b/src/test/key_tests.cpp index 61d334ab18..8cb0515a8a 100644 --- a/src/test/key_tests.cpp +++ b/src/test/key_tests.cpp @@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE(key_key_negation) // create a dummy hash for signature comparison unsigned char rnd[8]; std::string str = "Bitcoin key verification\n"; - GetRandBytes(rnd, sizeof(rnd)); + GetRandBytes(rnd); uint256 hash; CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash); diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp index a15094e5c8..74450f591d 100644 --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -582,7 +582,7 @@ void TorController::protocolinfo_cb(TorControlConnection& _conn, const TorContro // _conn.Command("AUTHENTICATE " + HexStr(status_cookie.second), std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2)); cookie = std::vector(status_cookie.second.begin(), status_cookie.second.end()); clientNonce = std::vector(TOR_NONCE_SIZE, 0); - GetRandBytes(clientNonce.data(), TOR_NONCE_SIZE); + GetRandBytes(clientNonce); _conn.Command("AUTHCHALLENGE SAFECOOKIE " + HexStr(clientNonce), std::bind(&TorController::authchallenge_cb, this, std::placeholders::_1, std::placeholders::_2)); } else { if (status_cookie.first) { diff --git a/src/util/bytevectorhash.cpp b/src/util/bytevectorhash.cpp index f87d0e04b3..bc060a44c9 100644 --- a/src/util/bytevectorhash.cpp +++ b/src/util/bytevectorhash.cpp @@ -8,8 +8,8 @@ ByteVectorHash::ByteVectorHash() { - GetRandBytes(reinterpret_cast(&m_k0), sizeof(m_k0)); - GetRandBytes(reinterpret_cast(&m_k1), sizeof(m_k1)); + GetRandBytes({reinterpret_cast(&m_k0), sizeof(m_k0)}); + GetRandBytes({reinterpret_cast(&m_k1), sizeof(m_k1)}); } size_t ByteVectorHash::operator()(const std::vector& input) const diff --git a/src/wallet/test/wallet_crypto_tests.cpp b/src/wallet/test/wallet_crypto_tests.cpp index 166e27bab9..327c28412a 100644 --- a/src/wallet/test/wallet_crypto_tests.cpp +++ b/src/wallet/test/wallet_crypto_tests.cpp @@ -81,7 +81,7 @@ BOOST_AUTO_TEST_CASE(passphrase) { std::string hash(GetRandHash().ToString()); std::vector vchSalt(8); - GetRandBytes(vchSalt.data(), vchSalt.size()); + GetRandBytes(vchSalt); uint32_t rounds = InsecureRand32(); if (rounds > 30000) rounds = 30000; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 780b3f35c5..c249b97804 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -692,12 +692,12 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) CKeyingMaterial _vMasterKey; _vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE); - GetStrongRandBytes(_vMasterKey.data(), WALLET_CRYPTO_KEY_SIZE); + GetStrongRandBytes(_vMasterKey); CMasterKey kMasterKey; kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE); - GetStrongRandBytes(kMasterKey.vchSalt.data(), WALLET_CRYPTO_SALT_SIZE); + GetStrongRandBytes(kMasterKey.vchSalt); CCrypter crypter; int64_t nStartTime = GetTimeMillis();