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

Add test for driver-only RSA (crypto only) #8616

Merged
merged 9 commits into from
Dec 14, 2023

Conversation

lpy4105
Copy link
Contributor

@lpy4105 lpy4105 commented Dec 7, 2023

Description

Fix: #8553

Two test components added:

  • test_psa_crypto_config_accel_rsa_crypto
  • test_psa_crypto_config_reference_rsa_crypto

PR checklist

  • changelog not required, test components
  • backport not required, no driver support on 2.28
  • tests not required

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
`psa_get_key_attributes` depends on some built-in
implementation of RSA. Guard the check with coresponding
macros.

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
@lpy4105 lpy4105 added enhancement 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 size-s Estimated task size: small (~2d) component-test Test framework and CI scripts priority-high High priority - will be reviewed soon labels Dec 7, 2023
Copy link
Contributor Author

@lpy4105 lpy4105 left a comment

Choose a reason for hiding this comment

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

A question for reviewers.

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 pretty good to me, thanks! Just minor things, plus one question about something I need to understand better before I can approve.

@@ -9685,6 +9685,9 @@ void generate_key_rsa(int bits_arg,
}

/* Test the key information */
#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: it's not obvious to me why you picked those exact guards, can you explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These macros come from:

mbedtls/library/psa_crypto.c

Lines 1375 to 1405 in f3ccfdd

switch (slot->attr.type) {
#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
case PSA_KEY_TYPE_RSA_KEY_PAIR:
case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
/* TODO: reporting the public exponent for opaque keys
* is not yet implemented.
* https://github.com/ARMmbed/mbed-crypto/issues/216
*/
if (!psa_key_lifetime_is_external(slot->attr.lifetime)) {
mbedtls_rsa_context *rsa = NULL;
status = mbedtls_psa_rsa_load_representation(
slot->attr.type,
slot->key.data,
slot->key.bytes,
&rsa);
if (status != PSA_SUCCESS) {
break;
}
status = psa_get_rsa_public_exponent(rsa,
attributes);
mbedtls_rsa_free(rsa);
mbedtls_free(rsa);
}
break;
#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */

If these conditions are not fitted, psa_get_key_attributes can't get RSA key attributes but returns PSA_SUCCESS.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation! I'll note the comment there is outdated (references an issue that was closed) and I'm tempted to slightly expand the scope of that PR to include updating that comment, but I'm not sure what the proper replacement would be yet.

@gilles-peskine-arm It looks like the prediction in the last comment in the issue listed above was not realised, as we seem to now find ourselves in a situation where we have an (experimental, in crypto_extra.h) API for custom domains parameters, with psa_set/get_key_domain_parameters() allowing to set/get custom public exponents for RSA keys, but no corresponding support in the driver API that I could find.

More precisely, I'm not sure how psa_get_key_attributes() is supposed to populate the attributes structure with the public exponent in case RSA keys are supported only by a driver? I think it's OK if we have a gap for now (which could be closed either by expanding driver support or by removing the experimental domain_parameters extension), but then we need to make sure we have an open issue tracking it.

Also, if we accept this gap for now, I'm a bit uncomfortable with the fact that psa_get_key_attributes() and then psa_get_key_domain_parameters() will both return success even when the actual value of e can't be accessed. I'd say this would deserve at least a warning in the documentation of psa_get_key_domain_parameters() that with RSA keys handled by a driver, the exponent will always be reported as the default one regardless of its actual value. Or should we make psa_get_key_domain_parameters() return an error in this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

We're in the uncomfortable position where domain parameters are deprecated, so we haven't included them in newer parts of the library, such as the driver interface. But we haven't removed them partly because removing takes time and partly because there is one thing that we care about that requires them, namely generating an RSA key with a non-default exponent.

My preferred resolution would be to:

  1. Finish designing psa_generate_key_ext and implement it and its driver counterpart.
  2. In a subsequent minor release, remove domain parameters.
  3. Now you don't have this problem anymore.

But obviously that would take time that we don't have.

In the meantime, I think ideally psa_get_key_domain_parameters should return an error if it can't obtain the public exponent. psa_get_key_attributes must always succeed (except if access to the key object fails in some way, e.g. an invalid key identifier or an operational error).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, good to know it's already tracked.

Then for this PR I'd like to suggest the following approach. In the test function: keep calling psa_get_key_attributes() etc. in all builds. However, for psa_get_key_domain_parameters(), instead of using PSA_ASSERT() (meaning it must always succeed) store the return value then:

#if WE_HAVE_ENOUGH_BUILTIN_RSA_FOR_THIS_TO_WORK
// assert status was SUCCESS
// check the value is as expected
#else
// assert status for NOT_SUPPORTED
// ignore unused parameters
#endif

And make the necessary changes for this test to pass.

Add comments / update existing comments to explain that this is a temporary situation with a reference to #6494.

@lpy4105 Does that work for you? That's arguably a slight scope extension, but it seems natural to fix the issue that was uncovered by the improved testing.

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
@lpy4105 lpy4105 requested a review from mpg December 7, 2023 09:42
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.

LGTM, thanks!

I'm withholding approval for now because I still want to understand better the limitation you uncovered about custom domain parameters (ie, non-default public exponents). It probably won't be resolved here, but I'd like to make sure we have task(s) created to track it before closing that discussion.

mpg
mpg previously approved these changes Dec 8, 2023
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.

LGTM if the CI agrees

From time being, domain_parameters could not be extracted
from driver. We need to return error to indicate this
situation. This is temporary and would be fixed after Mbed-TLS#6494.

Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
@lpy4105 lpy4105 force-pushed the issue/8553/test-driver-only-rsa branch from 3bdff07 to d90fbf7 Compare December 8, 2023 09:36
Signed-off-by: Pengyu Lv <pengyu.lv@arm.com>
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.

LGTM, thanks!

@mpg mpg requested a review from valeriosetti December 11, 2023 08:39
Copy link
Contributor

@valeriosetti valeriosetti left a comment

Choose a reason for hiding this comment

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

LGTM

@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, needs-reviewer This PR needs someone to pick it up for review labels Dec 14, 2023
@mpg mpg added this pull request to the merge queue Dec 14, 2023
Merged via the queue into Mbed-TLS:development with commit 1f67363 Dec 14, 2023
mpg added a commit to mpg/mbedtls that referenced this pull request Dec 18, 2023
Improved by Mbed-TLS#8616 - closing
8553.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this pull request Dec 18, 2023
It was introduced in Mbed-TLS#8616 but not
documented.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
case PSA_KEY_TYPE_RSA_KEY_PAIR:
case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
attributes->domain_parameters = NULL;
attributes->domain_parameters_size = SIZE_MAX;
Copy link
Contributor

Choose a reason for hiding this comment

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

Setting domain_parameters_size to SIZE_MAX is wrong: it's a promise that you can read SIZE_MAX bytes from domain_parameters. This might not be critical since this is a private field, and its only accessor function psa_get_domain_parameters takes care of that case. But at the very least, it needs to be documented in the definition of struct psa_key_attributes_s in psa/crypto_struct.h.

#8644

mpg added a commit to mpg/mbedtls that referenced this pull request Dec 22, 2023
Improved by Mbed-TLS#8616 - closing
8553.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
mpg added a commit to mpg/mbedtls that referenced this pull request Jan 8, 2024
Not related to other commits in this PR, should have been done in Mbed-TLS#8616
really, but since I'm updating the document, might as well do it here.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
mpg added a commit to mpg/mbedtls that referenced this pull request Jan 10, 2024
Improved by Mbed-TLS#8616 - closing
8553.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
mpg added a commit to mpg/mbedtls that referenced this pull request Mar 18, 2024
Improved by Mbed-TLS#8616 - closing
8553.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
mpg added a commit to mpg/mbedtls that referenced this pull request Apr 9, 2024
Improved by Mbed-TLS#8616 - closing
8553.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
@mpg mpg mentioned this pull request Apr 9, 2024
3 tasks
mpg added a commit to mpg/mbedtls that referenced this pull request Apr 12, 2024
Improved by Mbed-TLS#8616 - closing
8553.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
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 component-test Test framework and CI scripts enhancement priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add test for driver-only RSA (crypto only)
4 participants