Skip to content

Commit a6d9b41

Browse files
committed
wallet: refactor: introduce CMasterKey::DEFAULT_DERIVE_ITERATIONS constant
This gets rid of the magic number used in both the `CMasterKey` ctor and the wallet encryption / passphrase change methods.
1 parent 62c209f commit a6d9b41

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/wallet/crypter.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ class CMasterKey
4242
//! Use this for more parameters to key derivation (currently unused)
4343
std::vector<unsigned char> vchOtherDerivationParameters;
4444

45+
//! Default/minimum number of key derivation rounds
46+
// 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
47+
// ie slightly lower than the lowest hardware we need bother supporting
48+
static constexpr unsigned int DEFAULT_DERIVE_ITERATIONS = 25000;
49+
4550
SERIALIZE_METHODS(CMasterKey, obj)
4651
{
4752
READWRITE(obj.vchCryptedKey, obj.vchSalt, obj.nDerivationMethod, obj.nDeriveIterations, obj.vchOtherDerivationParameters);
4853
}
4954

5055
CMasterKey()
5156
{
52-
// 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
53-
// ie slightly lower than the lowest hardware we need bother supporting
54-
nDeriveIterations = 25000;
57+
nDeriveIterations = DEFAULT_DERIVE_ITERATIONS;
5558
nDerivationMethod = 0;
5659
vchOtherDerivationParameters = std::vector<unsigned char>(0);
5760
}

src/wallet/test/fuzz/crypter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ FUZZ_TARGET(crypter, .init = initialize_crypter)
3838
// Limiting the value of rounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
3939
crypt.SetKeyFromPassphrase(/*key_data=*/secure_string,
4040
/*salt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
41-
/*rounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
41+
/*rounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, CMasterKey::DEFAULT_DERIVE_ITERATIONS),
4242
/*derivation_method=*/derivation_method);
4343
}
4444

src/wallet/test/wallet_crypto_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void TestEncrypt(const CCrypter& crypt, const std::span<const unsigned ch
8383
BOOST_AUTO_TEST_CASE(passphrase) {
8484
// These are expensive.
8585

86-
TestCrypter::TestPassphrase("0000deadbeef0000"_hex_u8, "test", 25000,
86+
TestCrypter::TestPassphrase("0000deadbeef0000"_hex_u8, "test", CMasterKey::DEFAULT_DERIVE_ITERATIONS,
8787
"fc7aba077ad5f4c3a0988d8daa4810d0d4a0e3bcb53af662998898f33df0556a"_hex_u8,
8888
"cf2f2691526dd1aa220896fb8bf7c369"_hex_u8);
8989

@@ -99,7 +99,7 @@ BOOST_AUTO_TEST_CASE(passphrase) {
9999
BOOST_AUTO_TEST_CASE(encrypt) {
100100
constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};
101101
CCrypter crypt;
102-
crypt.SetKeyFromPassphrase("passphrase", salt, 25000, 0);
102+
crypt.SetKeyFromPassphrase("passphrase", salt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, 0);
103103
TestCrypter::TestEncrypt(crypt, "22bcade09ac03ff6386914359cfe885cfeb5f77ff0d670f102f619687453b29d"_hex_u8);
104104

105105
for (int i = 0; i != 100; i++)
@@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(encrypt) {
113113
BOOST_AUTO_TEST_CASE(decrypt) {
114114
constexpr std::array<uint8_t, WALLET_CRYPTO_SALT_SIZE> salt{"0000deadbeef0000"_hex_u8};
115115
CCrypter crypt;
116-
crypt.SetKeyFromPassphrase("passphrase", salt, 25000, 0);
116+
crypt.SetKeyFromPassphrase("passphrase", salt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, 0);
117117

118118
// Some corner cases the came up while testing
119119
TestCrypter::TestDecrypt(crypt,"795643ce39d736088367822cdc50535ec6f103715e3e48f4f3b1a60a08ef59ca"_hex_u8);

src/wallet/wallet.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
628628
crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
629629
pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * target / (SteadyClock::now() - start))) / 2;
630630

631-
if (pMasterKey.second.nDeriveIterations < 25000)
632-
pMasterKey.second.nDeriveIterations = 25000;
631+
if (pMasterKey.second.nDeriveIterations < CMasterKey::DEFAULT_DERIVE_ITERATIONS)
632+
pMasterKey.second.nDeriveIterations = CMasterKey::DEFAULT_DERIVE_ITERATIONS;
633633

634634
WalletLogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
635635

@@ -825,15 +825,15 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
825825
CCrypter crypter;
826826
constexpr MillisecondsDouble target{100};
827827
auto start{SteadyClock::now()};
828-
crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
829-
kMasterKey.nDeriveIterations = static_cast<unsigned int>(25000 * target / (SteadyClock::now() - start));
828+
crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, kMasterKey.nDerivationMethod);
829+
kMasterKey.nDeriveIterations = static_cast<unsigned int>(CMasterKey::DEFAULT_DERIVE_ITERATIONS * target / (SteadyClock::now() - start));
830830

831831
start = SteadyClock::now();
832832
crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
833833
kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + static_cast<unsigned int>(kMasterKey.nDeriveIterations * target / (SteadyClock::now() - start))) / 2;
834834

835-
if (kMasterKey.nDeriveIterations < 25000)
836-
kMasterKey.nDeriveIterations = 25000;
835+
if (kMasterKey.nDeriveIterations < CMasterKey::DEFAULT_DERIVE_ITERATIONS)
836+
kMasterKey.nDeriveIterations = CMasterKey::DEFAULT_DERIVE_ITERATIONS;
837837

838838
WalletLogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
839839

0 commit comments

Comments
 (0)