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

Enforce minimum key size when generating RSA key size #7864

Merged

Conversation

waleed-elmelegy-arm
Copy link
Contributor

@waleed-elmelegy-arm waleed-elmelegy-arm commented Jun 30, 2023

Add configuration to enforce minimum size when
generating a RSA key, it's default value is 1024
bits since this the minimum secure value currently but it can be any value greater than or equal 128
bits. Tests were modified to accommodate for this
change.

Description

Add configuration to enforce minimum size when
generating a RSA key, it's default value is 1024
bits since this the minimum secure value currently but it can be any value greater than or equal 128
bits. Tests were modified to accommodate for this
change.
fixes #7556

PR checklist

Please tick as appropriate and edit the reasons (e.g.: "backport: not needed because this is a new feature")

  • changelog provided
  • backport not required - adding a new config option seems a bit invasive for a backport
  • tests provided, or not required

Notes for the submitter

Please refer to the contributing guidelines, especially the
checklist for PR contributors.

@waleed-elmelegy-arm waleed-elmelegy-arm added needs-work needs-ci Needs to pass CI tests size-xs Estimated task size: extra small (a few hours at most) labels Jun 30, 2023
@waleed-elmelegy-arm waleed-elmelegy-arm marked this pull request as draft June 30, 2023 15:57
*
* Minimum possible value is 128 bits.
*/
#define MBEDTLS_RSA_MIN_KEY_SIZE 1024
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm Jul 3, 2023

Choose a reason for hiding this comment

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

IIRC numerical values need to go into the “General configuration options” section, due to how config.py works. And for those there's an #if !defined(…) clause in the relevant header (here rsa.h) to set a default if the value is not set in the config file (users can write their own config file where they only enable the options they care about). I'm not sure if that's why test_psa_crypto_config_accel_rsa_signature is failing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added it, Thank you this was the reason CI was failing.

Comment on lines 388 to 394
RSA Generate Key - 128bit key
depends_on:MBEDTLS_RSA_MIN_KEY_SIZE == 128
mbedtls_rsa_gen_key:128:3:0

RSA Generate Key - 128bit key (Less than minimum size)
depends_on:MBEDTLS_RSA_MIN_KEY_SIZE > 128
mbedtls_rsa_gen_key:128:3:MBEDTLS_ERR_RSA_BAD_INPUT_DATA
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather parametrize the test cases on MBEDTLS_RSA_MIN_KEY_SIZE:

Suggested change
RSA Generate Key - 128bit key
depends_on:MBEDTLS_RSA_MIN_KEY_SIZE == 128
mbedtls_rsa_gen_key:128:3:0
RSA Generate Key - 128bit key (Less than minimum size)
depends_on:MBEDTLS_RSA_MIN_KEY_SIZE > 128
mbedtls_rsa_gen_key:128:3:MBEDTLS_ERR_RSA_BAD_INPUT_DATA
RSA Generate Key - minimum size
mbedtls_rsa_gen_key:MBEDTLS_RSA_MIN_KEY_SIZE:3:0
RSA Generate Key - less than minimum size
depends_on:MBEDTLS_RSA_MIN_KEY_SIZE - 2 >= 128
mbedtls_rsa_gen_key:MBEDTLS_RSA_MIN_KEY_SIZE - 2:3:MBEDTLS_ERR_RSA_BAD_INPUT_DATA

Copy link
Contributor Author

@waleed-elmelegy-arm waleed-elmelegy-arm Jul 7, 2023

Choose a reason for hiding this comment

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

Done.
(for some reason MBEDTLS_RSA_MIN_KEY_SIZE - 2 was giving an error so I changed it to MBEDTLS_RSA_MIN_KEY_SIZE >= 130 )

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yes. Dependencies have to be of the form NAME, !NAME or NAME OP INTEGER where OP is a comparison operator. (I don't think there's really a good reason for that.)

@waleed-elmelegy-arm waleed-elmelegy-arm marked this pull request as ready for review July 7, 2023 14:47
@waleed-elmelegy-arm waleed-elmelegy-arm added 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-work labels Jul 7, 2023
@waleed-elmelegy-arm waleed-elmelegy-arm self-assigned this Jul 7, 2023
@gilles-peskine-arm gilles-peskine-arm added size-s Estimated task size: small (~2d) and removed needs-ci Needs to pass CI tests size-xs Estimated task size: extra small (a few hours at most) labels Jul 11, 2023
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.

Looks mostly good to me, but a few minor issues, mostly with naming and documentation.

include/mbedtls/mbedtls_config.h Outdated Show resolved Hide resolved
@@ -3670,6 +3670,9 @@
//#define MBEDTLS_ECP_WINDOW_SIZE 4 /**< Maximum window size used */
//#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */

/* RSA OPTIONS */
//#define MBEDTLS_RSA_MIN_KEY_SIZE 1024 /**< Minimum RSA key size allowed in bits (Minimum possible value is 128 bits) */
Copy link
Contributor

Choose a reason for hiding this comment

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

More precisely, this is the minimum allowed for generation. It's still possible to import a smaller key.

So maybe the name should indicate that? (See also my comment about the PSA constant, which definitely needs a more precise name.) Something like MBEDTLS_RSA_GEN_KEY_MIN_SIZE given that the affected function is called mbedtls_rsa_gen_key.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, but maybe MBEDTLS_RSA_GENERATE_KEY_MIN_SIZE rather than MBEDTLS_RSA_GEN_KEY_MIN_SIZE?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, we should enforce the 128 bit minimum via check_config.h

Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding the name: why not stick to something close to the name of the relevant function?

Regarding where to enforce the minimum: this would be better done in rsa.h or rsa.c. check_config.h is more suited to cross-module considerations.

Copy link
Contributor Author

@waleed-elmelegy-arm waleed-elmelegy-arm Jul 20, 2023

Choose a reason for hiding this comment

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

changed it to MBEDTLS_RSA_GEN_KEY_MIN_SIZE and added a check in rsa.h to check for 128 bits minimum, I removed the check from mbedtls_rsa_gen_key

include/psa/crypto_sizes.h Show resolved Hide resolved
include/psa/crypto_sizes.h Outdated Show resolved Hide resolved
* This is a vendor-specific macro.
*
* Limits RSA key generation to a minimum due to security reasons.
* This value cannot be less than 128 bits.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not actually enforced when RSA key generation goes through a driver. I'm not sure we need to enforce it. If we do, we could do it with #if … > 128 #error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

library/rsa.c 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, needs-reviewer This PR needs someone to pick it up for review labels Jul 11, 2023
@daverodgman daverodgman self-requested a review July 18, 2023 09:14
@daverodgman daverodgman added the priority-high High priority - will be reviewed soon label Jul 18, 2023
@waleed-elmelegy-arm waleed-elmelegy-arm force-pushed the enforce-min-RSA-key-size branch from fcaf38a to 5af233c Compare July 24, 2023 13:17
@waleed-elmelegy-arm waleed-elmelegy-arm added 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-work labels Jul 24, 2023
@daverodgman daverodgman removed the needs-reviewer This PR needs someone to pick it up for review label Jul 25, 2023
@daverodgman daverodgman added the needs-ci Needs to pass CI tests label Jul 27, 2023
daverodgman
daverodgman previously approved these changes Jul 27, 2023
Add configuration to enforce minimum size when
generating a RSA key, it's default value is 1024
bits since this the minimum secure value currently
but it can be any value greater than or equal 128
bits. Tests were modifed to accommodate for this
change.

Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
daverodgman
daverodgman previously approved these changes Jul 27, 2023
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm left a comment

Choose a reason for hiding this comment

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

LGTM

@tom-cosgrove-arm tom-cosgrove-arm added the approved Design and code approved - may be waiting for CI or backports label Jul 27, 2023
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.

Something went wrong in the latest rebase: a block of changes in test_suite_psa_crypto.data disappeared in "Improve naming of mimimum RSA key size generation configurations"

Other than that, looks good to me.

@gilles-peskine-arm gilles-peskine-arm added needs-work and removed approved Design and code approved - may be waiting for CI or backports labels Jul 27, 2023
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
@waleed-elmelegy-arm waleed-elmelegy-arm force-pushed the enforce-min-RSA-key-size branch from b87a4ae to d7bdbbe Compare July 27, 2023 14:56
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.

Sorry, d7bdbbe is still not right. The test improvements to adjust to a world where RSA key generation has a minimum of 1024, which were added in d9b303c, are still missing.

I think that the pull request is good otherwise, and its history makes sense . "Add a minimum rsa key size config to psa config" now doesn't adjust all the test cases optimally, but that's ok, it's the sort of things that can be spread into multiple commits. So I propose to stop attempting rebases and instead do an adjustment of the tests in a new commit.

The two things that are wrong in d7bdbbe are:

  • Some test cases are never executed on the CI, because they try to generate an RSA key that's too small. You can see that in the CI logs, in the step “tests/scripts/analyze_outcomes.py outcomes.csv”. This step is not shown in the default “blue ocean” view due to limitations of this view for which we haven't implemented a workaround yet, you have to go to the pipeline steps in the “classic” view. Search for Warning: Test case not executed: test_suite_psa_crypto;PSA generate key: RSA. This is a warning rather than an error because we have a backlog of test cases that are never executed (or wrongly detected as such), but we shouldn't add new ones.
  • Some negative test cases are now failing because they try to generate an RSA key that's too small, rather than due to the other error condition that we wanted to test.

I've left individual comments on each of the test cases that need updating (with one comment covering two test cases).

tests/suites/test_suite_psa_crypto.data Show resolved Hide resolved
tests/suites/test_suite_psa_crypto.data Show resolved Hide resolved
tests/suites/test_suite_psa_crypto.data Show resolved Hide resolved
tests/suites/test_suite_psa_crypto.data Show resolved Hide resolved
tests/suites/test_suite_psa_crypto.data Show resolved Hide resolved
tests/suites/test_suite_psa_crypto.data Show resolved Hide resolved
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
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.

LGTM

depends_on:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS <= 512
generate_key:PSA_KEY_TYPE_RSA_KEY_PAIR:512:PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT:PSA_ALG_RSA_PKCS1V15_CRYPT:PSA_SUCCESS:0
PSA generate key: RSA, minimum allowed key size, good, encrypt (PKCS#1 v1.5)
depends_on:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS >= 256:PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS <= 2048
Copy link
Contributor

Choose a reason for hiding this comment

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

Non-blocker: why PSA_VENDOR_RSA_GENERATE_MIN_KEY_BITS <= 2048?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because of this line:
unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; in psa_exercise_key.c:378
the output size is not enough for the output.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. The hard-coded sizes are probably back from before there were proper macros for buffer sizes. I've filed #8031 to remember about it. It's not urgent.

@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members, and removed needs-work labels Aug 2, 2023
@daverodgman daverodgman added this pull request to the merge queue Aug 3, 2023
Merged via the queue into Mbed-TLS:development with commit 6f80ac4 Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci Needs to pass CI tests needs-review Every commit must be reviewed by at least two team members, priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Enforce a sane key size when generating an RSA key
4 participants