Skip to content

Commit a8334f7

Browse files
committed
Read and write a checksum for encrypted keys
1 parent 3b69310 commit a8334f7

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/wallet/walletdb.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
109109
return false;
110110
}
111111

112-
if (!WriteIC(std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey), vchCryptedSecret, false)) {
112+
// Compute a checksum of the encrypted key
113+
uint256 checksum = Hash(vchCryptedSecret.begin(), vchCryptedSecret.end());
114+
115+
const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey);
116+
if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) {
113117
return false;
114118
}
115119
EraseIC(std::make_pair(DBKeys::KEY, vchPubKey));
@@ -332,6 +336,17 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
332336
}
333337
std::vector<unsigned char> vchPrivKey;
334338
ssValue >> vchPrivKey;
339+
340+
// Get the checksum and check it
341+
if (!ssValue.eof()) {
342+
uint256 checksum;
343+
ssValue >> checksum;
344+
if (Hash(vchPrivKey.begin(), vchPrivKey.end()) != checksum) {
345+
strErr = "Error reading wallet database: Crypted key corrupt";
346+
return false;
347+
}
348+
}
349+
335350
wss.nCKeys++;
336351

337352
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey))

0 commit comments

Comments
 (0)