Skip to content

wallet: include a checksum of encrypted private keys - #16946

Merged
laanwj merged 3 commits into
bitcoin:masterfrom
achow101:wallet-enckey-checksum
May 21, 2020
Merged

laanwj merged 3 commits into
bitcoin:masterfrom
achow101:wallet-enckey-checksum

Conversation

@achow101

Copy link
Copy Markdown
Member

Adds a checksum to the encrypted key record in the wallet database so that encrypted keys can be checked for corruption on wallet loading, in the same way that unencrypted keys are. This allows for us to skip the full decryption of keys upon the first unlocking of the wallet in that session as any key corruption will have already been detected. The checksum is just the double SHA256 of the encrypted key and it is appended to the record after the encrypted key itself.

This is backwards compatible as old wallets will be able to read the encrypted key and ignore that there is more data in the stream. Additionally, old wallets will be upgraded upon their first unlocking (so that key decryption is checked before we commit to a checksum of the encrypted key) and a wallet flag set indicating that. The presence of the wallet flag lets us skip the full decryption as if fDecryptionThoroughlyChecked were true.

This does mean that the first time an old wallet is unlocked in a new version will take much longer, but subsequent unlocks will be instantaneous. Furthermore, corruption will be detected upon loading rather than on trying to send so wallet corruption will be detected sooner.

Fixes #12423

@DrahtBot

DrahtBot commented Sep 23, 2019 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@achow101
achow101 force-pushed the wallet-enckey-checksum branch 4 times, most recently from 33d1bf5 to 2b79a27 Compare September 25, 2019 16:25
@practicalswift

practicalswift commented Sep 25, 2019 •

Copy link
Copy Markdown
Contributor

I'm afraid the current version of this PR will introduce a bug in WalletBatch::WriteCryptedKey:

https://github.com/bitcoin/bitcoin/blob/2b79a270e7442309ab167b161384060d93808695/src/wallet/walletdb.cpp#L118-L128

Please note the first return false; which makes the re-try logic unreachable :)

@achow101
achow101 force-pushed the wallet-enckey-checksum branch from 2b79a27 to d76d302 Compare September 25, 2019 22:09
@achow101

Copy link
Copy Markdown
Member Author

I'm afraid the current version of this PR will introduce a bug in WalletBatch::WriteCryptedKey:

Please note the first return false; which makes the re-try logic unreachable :)

Whoops, fixed.

@laanwj laanwj added the Feature label Sep 30, 2019
@laanwj

laanwj commented Oct 28, 2019

Copy link
Copy Markdown
Member

Concept ACK

@laanwj laanwj added this to the 0.20.0 milestone Oct 28, 2019
@achow101
achow101 force-pushed the wallet-enckey-checksum branch from d76d302 to 667b7c7 Compare October 29, 2019 15:06
@luke-jr

luke-jr commented Nov 3, 2019

Copy link
Copy Markdown
Contributor

a wallet flag set indicating that. The presence of the wallet flag lets us skip the full decryption as if fDecryptionThoroughlyChecked were true.

What happens if I open with new version, then switch back to an old version, generate some new keys, then switch back to the new?

Is a wallet flag really needed at all? Shouldn't verifying checksums also reveal if there are checksums missing?

@achow101

achow101 commented Nov 4, 2019

Copy link
Copy Markdown
Member Author

This is currently getting messed up by the ScriptPubKeyMan changes. Will fix, rebase, and address comments after ScriptPubKeyMan is fully introduced.

@achow101
achow101 force-pushed the wallet-enckey-checksum branch from 28ac72e to 667b7c7 Compare November 4, 2019 18:44
@achow101
achow101 force-pushed the wallet-enckey-checksum branch from 667b7c7 to 93a1e2a Compare December 4, 2019 00:23
@achow101

achow101 commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

I've rebased onto master and included #17369 as a prerequisite. I've also reworked it according to @luke-jr's suggestions. Instead of a wallet flag, we reuse fDecryptionThoroughlyChecked to let as avoid the decryption check. This also makes it easier for us to write (or rewrite) the checksums if they don't exist (or are invalid).

@laanwj

laanwj commented Dec 12, 2019

Copy link
Copy Markdown
Member

Might want to rebase to get rid of the commits from #17369 and make it easier to review.

@achow101
achow101 force-pushed the wallet-enckey-checksum branch from 8746a6d to 8ad4244 Compare December 12, 2019 16:13
@achow101

Copy link
Copy Markdown
Member Author

Rebased

…ms are valid

Change fDecryptionThoroughlyChecked to default to true so that it can
latch to false when an invalid checksum is seen. Checksums may be invalid
if the wallet does not have checksums or if the wallet became corrupted.

It is safe to default fDecryptionThoroughlyChecked to true because any
existing wallet without a checksum will set it to false. Any new or
blank wallet where encrypted keys are added will then set this to true
when the first encrypted key is generated by virtue of CheckDecryptionKey
doing that during the initial Unlock prior to keys being added.
If fDecryptionThoroughlyChecked is false, after a key has been checked,
write (or rewrite) its checksum. This serves to upgrade wallets
and correct those which have the checksum corrupted but not the key.
@achow101
achow101 force-pushed the wallet-enckey-checksum branch from 8ad4244 to d67055e Compare January 30, 2020 19:56

@jonatack jonatack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK d67055e

Rebased on current master @ 5236b2e, built, ran tests, ran bitcoind.

Could use test coverage.

@meshcollider meshcollider left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK d67055e

@achow101

Copy link
Copy Markdown
Member Author

ping @instagibbs

@laanwj

laanwj commented May 21, 2020

Copy link
Copy Markdown
Member

code review ACK d67055e

@laanwj
laanwj merged commit 9abed46 into bitcoin:master May 21, 2020
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 4, 2021
Summary:
d67055e00dd90f504384e5c3f229fc95306d5aac Upgrade or rewrite encrypted key checksums (Andrew Chow)
c9a9ddb4142af0af5f7b1a5ccd13f8e585007089 Set fDecryptionThoroughlyChecked based on whether crypted key checksums are valid (Andrew Chow)
a8334f7ac39532528c5f8bd3b0eea05aa63e8794 Read and write a checksum for encrypted keys (Andrew Chow)

Pull request description:

  Adds a checksum to the encrypted key record in the wallet database so that encrypted keys can be checked for corruption on wallet loading, in the same way that unencrypted keys are. This allows for us to skip the full decryption of keys upon the first unlocking of the wallet in that session as any key corruption will have already been detected. The checksum is just the double SHA256 of the encrypted key and it is appended to the record after the encrypted key itself.

  This is backwards compatible as old wallets will be able to read the encrypted key and ignore that there is more data in the stream. Additionally, old wallets will be upgraded upon their first unlocking (so that key decryption is checked before we commit to a checksum of the encrypted key) and a wallet flag set indicating that. The presence of the wallet flag lets us skip the full decryption as if `fDecryptionThoroughlyChecked` were true.

  This does mean that the first time an old wallet is unlocked in a new version will take much longer, but subsequent unlocks will be instantaneous. Furthermore, corruption will be detected upon loading rather than on trying to send so wallet corruption will be detected sooner.

  Fixes #12423

---

Backport of Core [[bitcoin/bitcoin#16946 | PR16946]]

Test Plan:
  ninja all check check-functional

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D9156
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

initial walletpassphrase slow and freezing

8 participants