Skip to content
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

Support set *_drbg reseed interval before seed #3393

Merged
merged 2 commits into from Dec 7, 2020

Conversation

gavacq
Copy link
Contributor

@gavacq gavacq commented Jun 3, 2020

Description

Fixes #2927

Summary of change

mbedtls_hmac_drbg_seed() and mbedtls_ctr_drbg_seed() were setting the entropy context reseed_interval to the default value of 10 000 even when set_reseed_interval() had been called prior to seed(). The documentation did not describe the API behaving this way, and it doesn't make sense that seed() would remove any prior preference for reseed_interval so this behavior has been fixed. Now, the value set by set_reseed_interval() will persist through after a call to seed().

Impact of changes

This change would break any application that

  • set the reseed_interval and expected it to be reset to the default value once they called seed()
  • set the reseed_interval and was unknowingly using the default value for the lifetime of the DRBG object

The first situation could be dangerous if the set interval was very high and the PRNG was never reseeded. It is an unlikely scenario however because a user that understood the prior behavior would probably not design their application in this way anyways.

In the second situation, this change could expose a vulnerability in the user's application, so this could provide them with a net benefit.

Migration actions required

None

Documentation
  • Comments have been updated.
  • Some formatting was done to align comment indentation.

Pull request type

[x] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[] Covered by existing mbed-os tests (Greentea or Unittest)
[x] Tests / results supplied as part of this PR

Modifies *_drbg_entropy_usage() so that set_reseed_interval() is called before seed(). This would fail before this patch.

@danh-arm danh-arm added bug needs-ci Needs to pass CI tests labels Jun 4, 2020
@danh-arm
Copy link
Contributor

danh-arm commented Jun 4, 2020

Hi @gacquroff . Thanks for your contribution.

It looks like this patch breaks our doxygen generation:

******************************************************************

* check_doxygen_warnings: Check: doxygen warnings (builds the documentation) 

* Wed Jun  3 16:37:05 UTC 2020

******************************************************************

/var/lib/build/include/mbedtls/ctr_drbg.h:306: warning: Invalid list item found

FAIL

^^^^check_doxygen_warnings: Check: doxygen warnings (builds the documentation): tests/scripts/doxygen.sh -> 1^^^^

Could you please fix this before we review? Regards, Dan.

@danh-arm danh-arm added this to To do in Mbed TLS PRs via automation Jun 4, 2020
@gilles-peskine-arm
Copy link
Contributor

For some reason (network glitch?) the Travis log wasn't linked automatically.

@gavacq
Copy link
Contributor Author

gavacq commented Jun 7, 2020

Travis CI build passed, but I'm not sure about the PR-3393-head TLS Testing failure. Should I be able to see the log from that?

@ronald-cron-arm
Copy link
Contributor

Hi @gacquroff, the PR-3393-head TLS Testing failure was just a CI glitch (a repo cloning failed) thus from the CI point of view it is all good now. Thanks for reverting the comment changes.

@gavacq
Copy link
Contributor Author

gavacq commented Jun 11, 2020

Hi @ronald-cron-arm, no problem. Let me know if I need to fix any other CI bugs.

@gilles-peskine-arm gilles-peskine-arm added the needs-review Every commit must be reviewed by at least two team members, label Jun 11, 2020
@gilles-peskine-arm
Copy link
Contributor

FreeBSD failed again in a re-run, but it isn't the fault of this PR. We'll need a clean run on FreeBSD before merging, but I don't expect any problems.

Mbed TLS PRs automation moved this from To do to In progress Jun 11, 2020
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

I noticed a missing link in the documentation. Other than that, looks good to me, thanks.

include/mbedtls/hmac_drbg.h Outdated Show resolved Hide resolved
@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Jun 11, 2020
include/mbedtls/ctr_drbg.h Outdated Show resolved Hide resolved
include/mbedtls/hmac_drbg.h Outdated Show resolved Hide resolved
library/ctr_drbg.c Outdated Show resolved Hide resolved
@gavacq gavacq force-pushed the development branch 2 times, most recently from 2d66042 to 5185515 Compare July 4, 2020 06:32
mbedtls_hmac_drbg_free( &ctx );
mbedtls_hmac_drbg_init( &ctx );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmac_drbg_no_reseed() test was failing before this change due to the reseed_interval not set to a non zero value after hmac_drbg_free(). This caused the subsquent mbedtls_hmac_drbg_random_with_add() to reseed, failing the test.

@ronald-cron-arm I would argue that we should not support using the DRBG after free() but before another init(), since free() destroys the context mutex. However we don't say this anywhere in the documentation. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for this late reply. I've checked with @gilles-peskine-arm and in fact the test is right and thus shouldn't be changed. mbedtls_hmac_drbg_free() should reset the context to a state that's equivalent to its state after the initial call to mbedtls_hmac_drbg_init(). One could argue that is more a reset than a free probably but that's the convention across mbedtls interfaces. The same apply to ctr_drbg. If this is ok with you, while at it, you are welcome to update the documentation of the free() functions to clarify their behavior. It would be much appreciated but no worries if you don't have time to do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this 'reset' convention does not apply to the MBEDTLS_THREADING_C mutex as after mbedtls_hmac_drbg_free the mutex handle is NULL. I added mbedtls_mutex_init after mbedtls_platform_zeroize in mbedtls_hmac_drbg_free in the latest patch. Likewise with ctr_drbg. This would be consistent with 'resetting context to state after initial call to mbedtls_hmac_drbg_init'

@gavacq
Copy link
Contributor Author

gavacq commented Aug 10, 2020

@ronald-cron-arm Are those test failures another CI glitch? I'm happy to clean up anything else here.

@danh-arm
Copy link
Contributor

The CI failed due to this having the "needs: work" label. I've removed and re-triggered now.

Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

The free() functions have to be fixed (see #3393 (comment)).

* #MBEDTLS_CTR_DRBG_RESEED_INTERVAL by default.
* You can override it by calling
* mbedtls_ctr_drbg_set_reseed_interval().
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a changelog entry file in ChangeLog.d. Suggested content:

Bugfix
   * In CTR_DRBG and HMAC_DRBG, don't reset the reseed interval in seed().
     Fixes #2927.

@@ -0,0 +1,3 @@
Bugfix
* In CTR_DRBG and HMAC_DRBG, don't reset the reseed interval in seed().
Fixes #2927.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a newline at the end of the file (Unix text encoding).

ronald-cron-arm
ronald-cron-arm previously approved these changes Dec 3, 2020
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this. Apart from the CI issue pointed out by @gilles-peskine-arm this looks good to me. I've checked that by reverting the changes in ctr_drbg.c and hmac_drbg.c the non-regression tests are falling as expected and where expected. Looking forward for the backports so we can merge this PR.

@ronald-cron-arm ronald-cron-arm added needs-ci Needs to pass CI tests and removed needs: changelog needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Dec 3, 2020
Signed-off-by: gacquroff <gavina352@gmail.com>
@gavacq
Copy link
Contributor Author

gavacq commented Dec 3, 2020

See backports:
2.16: #3938
2.7: #3937

@gilles-peskine-arm gilles-peskine-arm removed needs-backports Backports are missing or are pending review and approval. needs-ci Needs to pass CI tests labels Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug component-crypto Crypto primitives and low-level interfaces
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected reset of DRBG reseed interval
5 participants