mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
wallet: refactor: dedup master key encryption / derivation rounds setting
This commit is contained in:
parent
a6d9b415aa
commit
846545947c
1 changed files with 35 additions and 34 deletions
|
@ -576,6 +576,35 @@ void CWallet::UpgradeDescriptorCache()
|
||||||
SetWalletFlag(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED);
|
SetWalletFlag(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Given a wallet passphrase string and an unencrypted master key, determine the proper key
|
||||||
|
* derivation parameters (should take at least 100ms) and encrypt the master key. */
|
||||||
|
static bool EncryptMasterKey(const SecureString& wallet_passphrase, const CKeyingMaterial& plain_master_key, CMasterKey& master_key)
|
||||||
|
{
|
||||||
|
constexpr MillisecondsDouble target{100};
|
||||||
|
auto start{SteadyClock::now()};
|
||||||
|
CCrypter crypter;
|
||||||
|
|
||||||
|
crypter.SetKeyFromPassphrase(wallet_passphrase, master_key.vchSalt, master_key.nDeriveIterations, master_key.nDerivationMethod);
|
||||||
|
master_key.nDeriveIterations = static_cast<unsigned int>(master_key.nDeriveIterations * target / (SteadyClock::now() - start));
|
||||||
|
|
||||||
|
start = SteadyClock::now();
|
||||||
|
crypter.SetKeyFromPassphrase(wallet_passphrase, master_key.vchSalt, master_key.nDeriveIterations, master_key.nDerivationMethod);
|
||||||
|
master_key.nDeriveIterations = (master_key.nDeriveIterations + static_cast<unsigned int>(master_key.nDeriveIterations * target / (SteadyClock::now() - start))) / 2;
|
||||||
|
|
||||||
|
if (master_key.nDeriveIterations < CMasterKey::DEFAULT_DERIVE_ITERATIONS) {
|
||||||
|
master_key.nDeriveIterations = CMasterKey::DEFAULT_DERIVE_ITERATIONS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!crypter.SetKeyFromPassphrase(wallet_passphrase, master_key.vchSalt, master_key.nDeriveIterations, master_key.nDerivationMethod)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!crypter.Encrypt(plain_master_key, master_key.vchCryptedKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool CWallet::Unlock(const SecureString& strWalletPassphrase)
|
bool CWallet::Unlock(const SecureString& strWalletPassphrase)
|
||||||
{
|
{
|
||||||
CCrypter crypter;
|
CCrypter crypter;
|
||||||
|
@ -619,24 +648,11 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
|
||||||
return false;
|
return false;
|
||||||
if (Unlock(_vMasterKey))
|
if (Unlock(_vMasterKey))
|
||||||
{
|
{
|
||||||
constexpr MillisecondsDouble target{100};
|
if (!EncryptMasterKey(strNewWalletPassphrase, _vMasterKey, pMasterKey.second)) {
|
||||||
auto start{SteadyClock::now()};
|
return false;
|
||||||
crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
|
}
|
||||||
pMasterKey.second.nDeriveIterations = static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * target / (SteadyClock::now() - start));
|
|
||||||
|
|
||||||
start = SteadyClock::now();
|
|
||||||
crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
|
|
||||||
pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * target / (SteadyClock::now() - start))) / 2;
|
|
||||||
|
|
||||||
if (pMasterKey.second.nDeriveIterations < CMasterKey::DEFAULT_DERIVE_ITERATIONS)
|
|
||||||
pMasterKey.second.nDeriveIterations = CMasterKey::DEFAULT_DERIVE_ITERATIONS;
|
|
||||||
|
|
||||||
WalletLogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
|
WalletLogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
|
||||||
|
|
||||||
if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
|
|
||||||
return false;
|
|
||||||
if (!crypter.Encrypt(_vMasterKey, pMasterKey.second.vchCryptedKey))
|
|
||||||
return false;
|
|
||||||
WalletBatch(GetDatabase()).WriteMasterKey(pMasterKey.first, pMasterKey.second);
|
WalletBatch(GetDatabase()).WriteMasterKey(pMasterKey.first, pMasterKey.second);
|
||||||
if (fWasLocked)
|
if (fWasLocked)
|
||||||
Lock();
|
Lock();
|
||||||
|
@ -822,26 +838,11 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
|
||||||
kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
|
kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
|
||||||
GetStrongRandBytes(kMasterKey.vchSalt);
|
GetStrongRandBytes(kMasterKey.vchSalt);
|
||||||
|
|
||||||
CCrypter crypter;
|
if (!EncryptMasterKey(strWalletPassphrase, _vMasterKey, kMasterKey)) {
|
||||||
constexpr MillisecondsDouble target{100};
|
return false;
|
||||||
auto start{SteadyClock::now()};
|
}
|
||||||
crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, CMasterKey::DEFAULT_DERIVE_ITERATIONS, kMasterKey.nDerivationMethod);
|
|
||||||
kMasterKey.nDeriveIterations = static_cast<unsigned int>(CMasterKey::DEFAULT_DERIVE_ITERATIONS * target / (SteadyClock::now() - start));
|
|
||||||
|
|
||||||
start = SteadyClock::now();
|
|
||||||
crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
|
|
||||||
kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + static_cast<unsigned int>(kMasterKey.nDeriveIterations * target / (SteadyClock::now() - start))) / 2;
|
|
||||||
|
|
||||||
if (kMasterKey.nDeriveIterations < CMasterKey::DEFAULT_DERIVE_ITERATIONS)
|
|
||||||
kMasterKey.nDeriveIterations = CMasterKey::DEFAULT_DERIVE_ITERATIONS;
|
|
||||||
|
|
||||||
WalletLogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
|
WalletLogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
|
||||||
|
|
||||||
if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
|
|
||||||
return false;
|
|
||||||
if (!crypter.Encrypt(_vMasterKey, kMasterKey.vchCryptedKey))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
LOCK2(m_relock_mutex, cs_wallet);
|
LOCK2(m_relock_mutex, cs_wallet);
|
||||||
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
|
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
|
||||||
|
|
Loading…
Add table
Reference in a new issue