-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
psa: Support RSA signature without MBEDTLS_GENPRIME #4513
psa: Support RSA signature without MBEDTLS_GENPRIME #4513
Conversation
5d6c048
to
0b6d6ae
Compare
Until we have a fix for Mbed-TLS/mbedtls#4512, we need to patch the fix during import time. Otherwise, we run into linker errors when PSA attempts to use RSA key generation, which we've excluded. This patch is extracted from Mbed-TLS/mbedtls#4513
@paul-elliott-arm Could you verify that the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the simple solution is not acceptable since it breaks other use cases.
include/mbedtls/config_psa.h
Outdated
@@ -593,8 +593,10 @@ extern "C" { | |||
#define MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS 1 | |||
#define PSA_WANT_ALG_RSA_PSS 1 | |||
#endif /* MBEDTLS_PKCS1_V21 */ | |||
#if defined(MBEDTLS_GENPRIME) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also disables signature and decryption.
Unfortunately, there is no way to express “private-key operations but not key generation” in the current PSA crypto configuration system. This is a known limitation. Resolving this limitation will require more design work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is a bug, but I am able to perform a signature using an imported RSA key pair even when MBEDTLS_GENPRIME is not set (without this fix in Mbed TLS v2.24.0, and with this fix in v2.25.0). I can provide a sample application if that's helpful.
What's the intention behind both MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR
and MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY
? Why differentiate?
The fix here provides symmetry in what MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR
sets, and what TLS options enable it.
There was (in Mbed TLS v2.24.0) a way to express “private-key operations but not key generation” in the PSA crypto configuration system. Was it intentional to remove this in v2.25.0 and up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention between differentiating have-key-pair and have-public-key is to allow builds that have public-key operations only (typically: I want to verify some signatures, but I'm not going to sign anything), which can save significant code size if the implementation is structured for it: you don't need any blinding code, CRT parameter handling, etc. Mbed TLS is not yet structured for it, but we'll work on that in the coming months.
Private key operations may have worked accidentally even when only PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY
was enabled and not PSA_WANT_KEY_TYPE_RSA_KEY_PAIR
, but we're progressively tightening down on the configuration, and also ramping up testing (#4444 will add systematic negative testing).
There was (in Mbed TLS v2.24.0) a way to express “private-key operations but not key generation” in the PSA crypto configuration system.
In the classic configuration system, yes. In the PSA configuration system, no. We didn't intend to remove anything, but we're aware that the new system doesn't exactly match the granularity of the old one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should also highlight that I'm not using MBEDTLS_PSA_CRYPTO_CONFIG
or PSA_WANT_*
. Indeed, with PSA_WANT_*
there is no way to express "private-key operations but not key generation".
I am using the "old-style configuration mechanism", which it seems there is some intention to provide backwards compatibility with, hence my raising of this PR to help with that effort. If this aspect of PSA configuration isn't one you want to support because PSA_WANT_*
can't express it yet, that's not great for my use case, but I can understand.
Another question would be if the configuration settings I've added to all.sh are considered invalid somehow. Is building with the default config minus MBEDTLS_GENPRIME
(and no other changes) no longer supported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking configurations without MBEDTLS_GENPRIME
was an unintended regression. I fully agree that we should fix it. But I don't want to fix it by breaking something else. I think it should be possible to fix it by adding (back?) a few #ifdef MBDETLS_GENPRIME
in the right places.
With MBEDTLS_PSA_CRYPTO_CONFIG
, MBEDTLS_GENPRIME
is a mandatory consequence of PSA_WANT_KEY_TYPE_RSA_KEY_PAIR
for the time being, that's a limitation to fix later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a quick fix, I think the best solution would be to add #if defined(MBEDTLS_GENPRIME)
back into psa_crypto_*.c
. That's not the long-term solution, because it only matters for the software implementation, it doesn't allow e.g. an accelerator to declare that it can accelerate RSA signatures but not RSA key generation. But it's a good short-term solution because it fixes the problem you care about without breaking (or worsening) things that currently work (-ish), and it doesn't require any new design that we're not sure is the right design.
tests/scripts/all.sh
Outdated
component_build_psa_crypto_rsa_no_genprime() { | ||
msg "build: default config minus MBEDTLS_GENPRIME" | ||
scripts/config.py unset MBEDTLS_GENPRIME | ||
make |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please run make test
as well
0b6d6ae
to
2d89d08
Compare
Modified the RSA software implementation to use the |
2d89d08
to
63bf66b
Compare
Also call |
One outstanding issue is if the accelerator configuration #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
( defined(PSA_CRYPTO_DRIVER_TEST) && \
defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ) )
#define BUILTIN_KEY_TYPE_RSA_KEY_PAIR 1
#endif |
library/psa_crypto_rsa.c
Outdated
@@ -274,7 +274,10 @@ static psa_status_t rsa_export_public_key( | |||
#endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || | |||
* defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */ | |||
|
|||
#if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) | |||
/* XXX Does MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR use these functions? Both | |||
* of them or just rsa_generate_key? */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently only rsa_generate_key
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it use this implementation of rsa_generate_key()
or is another rsa_generate_key()
substituted in at link time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't do link-time substitution.
63bf66b
to
ada2a54
Compare
Updated with removal of XXX review comment question and addition of |
The test "PSA generate key: RSA, 1024 bits, good, encrypt (OAEP SHA-256)" had a dependency on MBEDTLS_GENPRIME, but this was not listed in the dependencies. Add MBEDTLS_GENPRIME to the test's dependencies to ensure it has what it needs to run. Signed-off-by: Jaeden Amero <[email protected]>
On space-constrained platforms, it is a useful configuration to be able to import/export and perform RSA key pair operations, but to exclude RSA key generation, potentially saving flash space. It is not possible to express this with the PSA_WANT_ configuration system at the present time. However, in previous versions of Mbed TLS (v2.24.0 and earlier) it was possible to configure a software PSA implementation which was capable of making RSA signatures but not capable of generating RSA keys. To do this, one unset MBEDTLS_GENPRIME. Since the addition of MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR, this expressivity was lost. Expressing that you wanted to work with RSA key pairs forced you to include the ability to generate key pairs as well. Change psa_crypto_rsa.c to only call mbedtls_rsa_gen_key() if MBEDTLS_GENPRIME is also set. This restores the configuration behavior present in Mbed TLS v2.24.0 and earlier versions. It left as a future exercise to add the ability to PSA to be able to express a desire for a software or accelerator configuration that includes RSA key pair operations, like signature, but excludes key pair generation. Without this change, linker errors will occur when attempts to call, which doesn't exist when MBEDTLS_GENPRIME is unset. psa_crypto_rsa.c.obj: in function `rsa_generate_key': psa_crypto_rsa.c:320: undefined reference to `mbedtls_rsa_gen_key' Fixes Mbed-TLS#4512 Signed-off-by: Jaeden Amero <[email protected]>
ada2a54
to
424fa93
Compare
Updated to add missing dependency to "PSA generate key: RSA, 1024 bits, good, encrypt (OAEP SHA-256)". It wasn't depending on MBEDTLS_GENPRIME, but should have been. |
There was a problem hiding this 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. Please make a backport to development_2.x
.
* Restore the ability to configure PSA via Mbed TLS options to support RSA | ||
key pair operations but exclude RSA key generation. When MBEDTLS_GENPRIME | ||
is not defined PSA will no longer attempt to use mbedtls_rsa_gen_key(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could just have said “Fix the build when MBEDTLS_GENPRIME
is disabled.” But ok.
Raised backport to |
Until we have a fix for Mbed-TLS/mbedtls#4512, we need to patch the fix during import time. Otherwise, we run into linker errors when PSA attempts to use RSA key generation, which we've excluded. This patch is extracted from Mbed-TLS/mbedtls#4513
Description
On space-constrained platforms, it is a useful configuration to be able
to import/export and perform RSA key pair operations, but to exclude RSA
key generation, potentially saving flash space. It is not possible to
express this with the PSA_WANT_ configuration system at the present
time. However, in previous versions of Mbed TLS (v2.24.0 and earlier) it
was possible to configure a software PSA implementation which was
capable of making RSA signatures but not capable of generating RSA keys.
To do this, one unset MBEDTLS_GENPRIME.
Since the addition of MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR, this
expressivity was lost. Expressing that you wanted to work with RSA key
pairs forced you to include the ability to generate key pairs as well.
Change psa_crypto_rsa.c to only call mbedtls_rsa_gen_key() if
MBEDTLS_GENPRIME is also set. This restores the configuration behavior
present in Mbed TLS v2.24.0 and earlier versions.
It left as a future exercise to add the ability to PSA to be able to
express a desire for a software or accelerator configuration that
includes RSA key pair operations, like signature, but excludes key pair
generation.
Without this change, linker errors will occur when attempts to call,
which doesn't exist when MBEDTLS_GENPRIME is unset.
psa_crypto_rsa.c.obj: in function
rsa_generate_key': psa_crypto_rsa.c:320: undefined reference to
mbedtls_rsa_gen_key'Fixes #4512
Signed-off-by: Jaeden Amero [email protected]
Status
READY
Requires Backporting
Yes
Migrations
If there is any API change, what's the incentive and logic for it.
NO
Additional comments
None
Todos
[ ] DocumentationSteps to test or reproduce
Build with MBEDTLS_GENPRIME unset.