-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Backport 2.16: Allow xxx_drbg_set_entropy_len before xxx_drbg_seed #2894
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
Merged
Patater
merged 6 commits into
Mbed-TLS:mbedtls-2.16
from
gilles-peskine-arm:drbg-set_entropy_len-2.16
Nov 29, 2019
Merged
Backport 2.16: Allow xxx_drbg_set_entropy_len before xxx_drbg_seed #2894
Patater
merged 6 commits into
Mbed-TLS:mbedtls-2.16
from
gilles-peskine-arm:drbg-set_entropy_len-2.16
Nov 29, 2019
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
While making ARMmbed/mbed-crypto#305, I discovered some documentation that I'd failed to update. https://github.com/gilles-peskine-arm/mbedtls/tree/drbg-set_entropy_len-doc_cleanup-2.16 contains fixup commits for these missing updates, and I rewrote the history of this branch to squash the fixups. |
mbedtls_hmac_drbg_seed() always set the entropy length to the default, so a call to mbedtls_hmac_drbg_set_entropy_len() before seed() had no effect. Change this to the more intuitive behavior that set_entropy_len() sets the entropy length and seed() respects that and only uses the default entropy length if there was no call to set_entropy_len().
Move the definitions of mbedtls_ctr_drbg_seed_entropy_len() and mbedtls_ctr_drbg_seed() to after they are used. This makes the code easier to read and to maintain.
mbedtls_ctr_drbg_seed() always set the entropy length to the default, so a call to mbedtls_ctr_drbg_set_entropy_len() before seed() had no effect. Change this to the more intuitive behavior that set_entropy_len() sets the entropy length and seed() respects that and only uses the default entropy length if there was no call to set_entropy_len(). The former test-only function mbedtls_ctr_drbg_seed_entropy_len() is no longer used, but keep it for strict ABI compatibility.
0215ee4 to
0e59c47
Compare
AndrzejKurek
previously approved these changes
Oct 24, 2019
You can't reuse a CTR_DRBG context without free()ing it and re-init()ing. This generally happened to work, but was never guaranteed. It could have failed with alternative implementations of the AES module because mbedtls_ctr_drbg_seed() calls mbedtls_aes_init() on a context which is already initialized if mbedtls_ctr_drbg_seed() hasn't been called before, plausibly causing a memory leak. Calling free() and seed() with no intervening init fails when MBEDTLS_THREADING_C is enabled and all-bits-zero is not a valid mutex representation. So add the missing free() and init().
You can't reuse a CTR_DRBG context without free()ing it and re-init()ing it. This generally happened to work, but was never guaranteed. It could have failed with alternative implementations of the AES module because mbedtls_ctr_drbg_seed() calls mbedtls_aes_init() on a context which is already initialized if mbedtls_ctr_drbg_seed() hasn't been called before, plausibly causing a memory leak. Calling free() and seed() with no intervening init fails when MBEDTLS_THREADING_C is enabled and all-bits-zero is not a valid mutex representation.
a6e29ad to
b02a233
Compare
k-stachowiak
approved these changes
Oct 29, 2019
AndrzejKurek
approved these changes
Nov 4, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
approved
Design and code approved - may be waiting for CI or backports
bug
component-crypto
Crypto primitives and low-level interfaces
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The functions
mbedtls_ctr_drbg_set_entropy_len()andmbedtls_hmac_drbg_set_entropy_len()were only supported after the call tombedtls_ctr_drbg_seed()andmbedtls_hmac_drbg_seed(), so the initial seeding always grabbed the amount of entropy defined at compile time (MBEDTLS_CTR_DRBG_ENTROPY_LENfor CTR_DRBG, calculated from the hash algorithm for HMAC_DRBG). There was no good reason for that and the documentation was not clear prior to ARMmbed/mbed-crypto#287. Change the API to be intuitive: you can callset_entropy_len()afterinit(), and it will influence both the initial seeding (if you call it beforeseed()) and subsequent reseeds.Keep the test-only function
mbedtls_ctr_drbg_seed_entropy_lenfor strict ABI compatibility.Backport of ARMmbed/mbed-crypto#299, plus some follow-up fixes from ARMmbed/mbed-crypto#305.