-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Remove pkwrite dependency in pk using PSA for ECDSA #6389
Remove pkwrite dependency in pk using PSA for ECDSA #6389
Conversation
a6fb419
to
cf4e95e
Compare
cf4e95e
to
f9c1cec
Compare
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.
In general it LGTM. I just have a couple of comments/suggestions for defines
library/pk_wrap.c
Outdated
unsigned char buf[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE( | ||
PSA_VENDOR_ECC_MAX_CURVE_BITS )]; |
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.
Isn't it possible to use the MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
define here? It seems to me that it should be equivalent
library/pk_wrap.c
Outdated
unsigned char buf[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE( | ||
PSA_VENDOR_ECC_MAX_CURVE_BITS )]; |
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.
In case the above comment makes sense to you, what about creating a define to MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
for this purpose?
Perhaps PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
is already OK for this?
I held off on this while other high-priority work was touching pk. Now I think it would help with the ongoing work on driver-only ECC. But I don't have time for it right now — if someone has time now and wants to adopt it, please feel free. Otherwise I'll get around to it eventually. |
I'll be glad to complete this work ;) |
2099adb
to
368fed2
Compare
I just did a force push in order to align with the new code style |
Under MBEDTLS_USE_PSA_CRYPTO, ecdsa_verify_wrap() was calling mbedtls_pk_write_pubkey() to write a public key in the form of a subjectPublicKey, only to then extract the part that represents the EC point which psa_import_key() actually wants. Instead, call an ecp function to directly get the public key in the desired format (just the point). This slightly reduces the code size and stack usage, and removes a dependency on pk_write. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Under MBEDTLS_USE_PSA_CRYPTO, ecdsa_sign_wrap() was calling mbedtls_pk_write_key_der() to write a private key in SEC1 format, only to then extract the part that represents the private value which is what psa_import_key() actually wants. Instead, call an mpi function to directly get the private key in the desired format. This slightly reduces the code size and stack usage, and removes a dependency on pk_write. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
The dependency is still useful for RSA, for which PSA encodes keys with an ASN.1 structure. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
To better reflect what the code relies on, limit the headers that are included when MBEDTLS_USE_PSA_CRYPTO is disabled. Also stop including "pkwrite.h" when it is no longer needed. Include "mbedlts/platform_util.h" unconditionally. It was only included for RSA ALT but was also used for MBEDTLS_USE_PSA_CRYPTO (the code worked because other headers include "mbedtls/platform_util.h"). Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
f6a5b87
to
80d0798
Compare
Following the suggestion made previously and since #6970 was just merged into |
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.
LGTM.
I've verified that after the last commit there is no occurrence of ECDSA_C
left in pk.c
and pk_wrap.c
other than (1) those associated with restartable and (2) those that guard #include "mbedtls/ecdsa.h"
.
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.
LGTM, left one question.
* is not currently supported in PSA, the public key is one byte longer | ||
* (header byte + 2 numbers, while the signature is only 2 numbers), | ||
* so use that as the buffer size. */ | ||
unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
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 is buffer on stack and holds public key or signature, but just to confirm. Shouldn't this buffer be cleared in cleanup
stage?
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.
Ah, excellent question! As the name implies, public keys are not supposed to be sensitive data so they don't need to be wiped out. I'd say signatures are not sensitive either in general, but I may be missing some corner cases, so I'll ask on slack.
Remove the dependency on
MBEDTLS_PK_WRITE_C
whenMBEDTLS_PSA_CRYPTO_C
andMBEDTLS_PK_C
are enabled but notMBEDTLS_RSA_C
, by no longer using pkwrite functions for ECDSA.Status:
needs to be rebased now that #6408 has been fixed.doneDepends on #6970Gatekeeper checklist