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

dhm: Fix bias in private key generation #4628

Merged
merged 13 commits into from
Jun 16, 2021

Conversation

ronald-cron-arm
Copy link
Contributor

@ronald-cron-arm ronald-cron-arm commented Jun 8, 2021

Description

Fix #4612

Status

READY

@ronald-cron-arm ronald-cron-arm force-pushed the dhm-key-generation-bias branch from acc268e to d35a7bb Compare June 8, 2021 14:02
@ronald-cron-arm ronald-cron-arm self-assigned this Jun 8, 2021
@ronald-cron-arm ronald-cron-arm added component-crypto Crypto primitives and low-level interfaces needs-design-approval labels Jun 8, 2021
@ronald-cron-arm
Copy link
Contributor Author

@gilles-peskine-arm @mpg I believe the commit in this PR addresses in a minimalist way #4612. Do we want to do more like fixing the equivalent bias in blinding values in dhm.c ? in ecp.c ?

@gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm @mpg I believe the commit in this PR addresses in a minimalist way #4612. Do we want to do more like fixing the equivalent bias in blinding values in dhm.c ? in ecp.c ?

Ideally yes, if it can be done without too much trouble.

gilles-peskine-arm and others added 11 commits June 10, 2021 10:34
Improve the validation of the output from mbedtls_dhm_make_params:
* Test that the output in the byte buffer matches the value in the
  context structure.
* Test that the calculated values are in the desired range.

Cherry-picked dc0b6e4.
Changed mbedtls_test_rnd_pseudo_rand to rnd_pseudo_rand.
Removed test step code.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: Ronald Cron <[email protected]>
Repeat a few tests that use random data. This way the code is
exercised with a few different random values.

Signed-off-by: Gilles Peskine <[email protected]>
mbedtls_dhm_make_params() with x_size != size of P is not likely to be
useful, but it's supported, so test it.

Cherry-picked 33ec863
Changed mbedtls_test_rnd_pseudo_info type to rnd_pseudo_info

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: Ronald Cron <[email protected]>
Unify the common parts of mbedtls_dhm_make_params and mbedtls_dhm_make_public.

No intended behavior change, except that the exact error code may
change in some corner cases which are too exotic for the existing unit
tests.

Removed usage of MBEDTLS_ERROR_ADD which does not exist in 2.16.

Signed-off-by: Gilles Peskine <[email protected]>
P-1 is as bad as 1 as a blinding value. Don't accept it.

The chance that P-1 would be randomly generated is infinitesimal, so
this is not a practical issue, but it makes the code cleaner. It was
inconsistent to accept P-1 as a blinding value but not as a private key.

Signed-off-by: Gilles Peskine <[email protected]>
dhm_make_common includes a piece of code that is identical to
dhm_random_below except for returning a different error code in one
case. Call dhm_random_below instead of repeating the code.

Signed-off-by: Gilles Peskine <[email protected]>
No need to build a bignum for the value 2.

Signed-off-by: Gilles Peskine <[email protected]>
@ronald-cron-arm ronald-cron-arm force-pushed the dhm-key-generation-bias branch from d35a7bb to 424d13c Compare June 10, 2021 08:54
@ronald-cron-arm
Copy link
Contributor Author

I believe I have a decent proposal now for the backport of #4276 in 2.16 regarding DHM (ECP not handled yet). I've back-ported all DHM related commits but "DHM: use mbedtls_mpi_random for blinding and key generation" that I have replaced by "dhm: Fix bias in private key generation and blinding". When the backport of a commit was not clean I've added some lines in the commit message starting with "Cherry-picked ..." and signed-off the commit myself as well. Please have a look.

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 am satisfied that this is a complete backport (with minimal necessary adaptations) of the part of Backport 2.x: Fix non-uniform random generation in a range #4276 that concerns Diffie-Hellman, except for the change to use the new function mbedtls_mpi_random in dhm_random_below and the changelog entry.
  • I am satisfied that the commit "dhm: Fix bias in private key generation and blinding" is correct and does the same job as the part of Backport 2.x: Fix non-uniform random generation in a range #4276 concerning dhm.c that was not backported.
  • I am satisfied that the changes are not too disruptive for a LTS branch. I have slight concerns about "DHM refactoring: unify mbedtls_dhm_make_{params,public}" changing error codes in corner cases, but I can't identify one in a reasonable amount of time, so I think they would be too exotic to matter even by LTS standards.

So all that's missing for my approval is a changelog entry.

@gilles-peskine-arm gilles-peskine-arm added 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 and removed needs-design-approval labels Jun 10, 2021
@ronald-cron-arm
Copy link
Contributor Author

Thanks for the feedback @gilles-peskine-arm. For ECP, I propose to just fix the blinding values generation in ecp_randomize_jac/mxz(). I've added a commit for that. Otherwise I've added the change log entry as in #4276.

@ronald-cron-arm ronald-cron-arm removed the needs-reviewer This PR needs someone to pick it up for review label Jun 15, 2021
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

Looks good to me. I've verified that most of the commits are from the original PR (thanks for including details about the changes in the commit message!), and that all relevant commits have been backported. I've then review the two new commits independently, and I'm happy with the result.

@mpg mpg added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Jun 16, 2021
@mpg mpg merged commit 56efc52 into Mbed-TLS:mbedtls-2.16 Jun 16, 2021
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants