-
Notifications
You must be signed in to change notification settings - Fork 37.8k
crypto: Use secure_allocator for AES256_ctx
#31774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
crypto: Use secure_allocator for AES256_ctx
#31774
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31774. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsNo conflicts as of last run. |
ab21aa1
to
68f1370
Compare
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
Concept ACK on clearing out the ctx/iv members I'm wondering if a minimum-diff fix which simply replaces the bitcoin/src/wallet/crypter.cpp Lines 85 to 91 in 94ca99a
bitcoin/src/wallet/crypter.cpp Lines 102 to 108 in 94ca99a
The reasoning might be a bit loose as there might be other uses of these classes in the future where the situation is different. Curious about other opinions. |
I think the approach here might have an unacceptable performance impact, as it is allocating and deallocating a secure area for every individual key being encrypted/decrypted. It may be better to:
|
046c5e4
to
a3be398
Compare
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
fda2ec1
to
15d8500
Compare
It seems right to me that this structure is always short lived, but I also felt in #31166
I added a benchmark for
But if the short-lived nature of the key material makes just using |
This may still be true, though FWIW the idea behind the But agree that for short-lived key material, it's less important, there is little to no chance of it ending up in swap anyway. |
15d8500
to
95d266f
Compare
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
95d266f
to
591764f
Compare
Rebased to address merge conflict, dropped legacy wallet encryption benchmark, and reduced number of keys since the benchmark was taking an unreasonably long time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 591764f, just coding style nits
591764f
to
3d7d2c3
Compare
Rebase to address style feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code-review ACK 3d7d2c3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why the first commit is necessary. Couldn't you just mock the time so that it's fixed and the number of derivation rounds always stays at the default value?
Maybe I'm not thinking creatively enough, but I think because the timing takes place entirely inside of Lines 581 to 589 in 68ac9f1
Footnotes |
Yes, it seems simpler and more scoped to address that inside the encryption function (which could arguably be considered a 'fix', since we shouldn’t be dividing by zero regardless of whether the clock time is messed up) than to add a test-only field to the wallet and another function arg just just for this. |
AFAIK,
I agree that adding a test-only field and arg is very bad, and would like to avoid it, but it seems to me worse to imbue secret meaning into the benchmark by handling a time difference of 0 in this special way which appears on the surface to be error-handling, but is really a "side-channel" for test code to sneak an extra parameter into |
7237df9
to
90528e7
Compare
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
Adds a special case where if the elapsed time during measurement of DKF performance is 0, the default derive iterations are used so that behavior is stable for testing and benchmarks.
Allows `crypto` functions and classes to use `secure_allocator`.
90528e7
to
8bdcd12
Compare
Refactored to address @furszy feedback to avoid adding a test-only parameter to I don't like the idea of using a |
Fixes #31744
Reuse
secure_allocator
forAES256_ctx
in the aes 256 encrypters and decrypters and theiv
ofAES256CBC
encrypters and decrypters. These classes are relevant toCCrypter
, used for encrypting wallets, and my understanding is that if an attacker knows some or all of the contents of these data structures (AES256_ctx
&iv
) they might be able to decrypt a user's wallet.Presently the
secure_allocator
tries to protect sensitive data withmlock()
on POSIX systems andVirtualLock()
on Windows to prevent memory being paged to disk, and by zero'ing out memory contents on deallocation withmemory_cleanse()
which is similar toOPENSSL_cleanse()
by scaring compilers away from optimizingmemset
calls on non-Windows systems, and usingSecureZeroMemory()
on Windows.