aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpasta <pasta@dashboost.org>2022-01-31 19:29:33 +0700
committerpasta <pasta@dashboost.org>2022-03-23 17:36:33 -0500
commit3ae7791bcaa88f5c68592673b8926ee807242ce7 (patch)
treea3c9f0be42e9082a9047f26d4eecb65b441e39a8
parentcea230eec40054a82fcd31fa97cf46f1585c4a35 (diff)
refactor: use Span in random.*
-rw-r--r--src/addrdb.cpp2
-rw-r--r--src/dbwrapper.cpp2
-rw-r--r--src/key.cpp6
-rw-r--r--src/net_processing.cpp2
-rw-r--r--src/random.cpp7
-rw-r--r--src/random.h5
-rw-r--r--src/rpc/request.cpp2
-rw-r--r--src/test/key_tests.cpp2
-rw-r--r--src/torcontrol.cpp2
-rw-r--r--src/util/bytevectorhash.cpp4
-rw-r--r--src/wallet/test/wallet_crypto_tests.cpp2
-rw-r--r--src/wallet/wallet.cpp4
12 files changed, 21 insertions, 19 deletions
diff --git a/src/addrdb.cpp b/src/addrdb.cpp
index 0fa8f3c3da..61b5add6bb 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/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<unsigned char> CDBWrapper::CreateObfuscateKey() const
{
std::vector<uint8_t> 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 354bd097ce..2bdbc8677e 100644
--- a/src/key.cpp
+++ b/src/key.cpp
@@ -159,7 +159,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;
@@ -244,7 +244,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<unsigned char> vchSig;
@@ -397,7 +397,7 @@ void ECC_Start() {
{
// Pass in a random blinding seed to the secp256k1 context.
std::vector<unsigned char, secure_allocator<unsigned char>> 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 77efac3364..76f35439fa 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -4426,7 +4426,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 b862510524..2881d277ac 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -16,6 +16,7 @@
#include <logging.h> // for LogPrintf()
#include <randomenv.h>
#include <support/allocators/secure.h>
+#include <span.h>
#include <sync.h> // for Mutex
#include <util/time.h> // 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<unsigned char> bytes) noexcept { ProcRand(bytes.data(), bytes.size(), RNGLevel::FAST); }
+void GetStrongRandBytes(Span<unsigned char> 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 <crypto/chacha20.h>
#include <crypto/common.h>
+#include <span.h>
#include <uint256.h>
#include <chrono>
@@ -66,7 +67,7 @@
*
* Thread-safe.
*/
-void GetRandBytes(unsigned char* buf, int num) noexcept;
+void GetRandBytes(Span<unsigned char> 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<unsigned char> 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 95a7c25b93..d0e068de19 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/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<uint8_t>(status_cookie.second.begin(), status_cookie.second.end());
clientNonce = std::vector<uint8_t>(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<unsigned char*>(&m_k0), sizeof(m_k0));
- GetRandBytes(reinterpret_cast<unsigned char*>(&m_k1), sizeof(m_k1));
+ GetRandBytes({reinterpret_cast<unsigned char*>(&m_k0), sizeof(m_k0)});
+ GetRandBytes({reinterpret_cast<unsigned char*>(&m_k1), sizeof(m_k1)});
}
size_t ByteVectorHash::operator()(const std::vector<unsigned char>& 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<unsigned char> 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 261d042529..a64c56bf67 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -669,12 +669,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();