aboutsummaryrefslogtreecommitdiff
path: root/src/wallet
diff options
context:
space:
mode:
authorAndrew Chow <achow101-github@achow101.com>2019-09-23 18:10:13 -0400
committerAndrew Chow <achow101-github@achow101.com>2020-01-30 14:42:50 -0500
commita8334f7ac39532528c5f8bd3b0eea05aa63e8794 (patch)
treeb10658788c466183e2652de0dfce52024128eef1 /src/wallet
parent3b69310beb172b2b56fbfc38c45898a1b627ac54 (diff)
downloadbitcoin-a8334f7ac39532528c5f8bd3b0eea05aa63e8794.tar.xz
Read and write a checksum for encrypted keys
Diffstat (limited to 'src/wallet')
-rw-r--r--src/wallet/walletdb.cpp17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index a1928f45c4..098047bb39 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -109,7 +109,11 @@ bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
return false;
}
- if (!WriteIC(std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey), vchCryptedSecret, false)) {
+ // Compute a checksum of the encrypted key
+ uint256 checksum = Hash(vchCryptedSecret.begin(), vchCryptedSecret.end());
+
+ const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey);
+ if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) {
return false;
}
EraseIC(std::make_pair(DBKeys::KEY, vchPubKey));
@@ -332,6 +336,17 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
}
std::vector<unsigned char> vchPrivKey;
ssValue >> vchPrivKey;
+
+ // Get the checksum and check it
+ if (!ssValue.eof()) {
+ uint256 checksum;
+ ssValue >> checksum;
+ if (Hash(vchPrivKey.begin(), vchPrivKey.end()) != checksum) {
+ strErr = "Error reading wallet database: Crypted key corrupt";
+ return false;
+ }
+ }
+
wss.nCKeys++;
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey))