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

Remove pkwrite dependency in pk using PSA for ECDSA #6389

Conversation

gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Oct 4, 2022

Remove the dependency on MBEDTLS_PK_WRITE_C when MBEDTLS_PSA_CRYPTO_C and MBEDTLS_PK_C are enabled but not MBEDTLS_RSA_C, by no longer using pkwrite functions for ECDSA.

Status: needs to be rebased now that #6408 has been fixed. done
Depends on #6970

Gatekeeper checklist

  • changelog not required (optimization only)
  • backport not required (optimization only)
  • tests provided (covered by existing tests)

@gilles-peskine-arm gilles-peskine-arm added enhancement component-crypto Crypto primitives and low-level interfaces needs-ci Needs to pass CI tests priority-medium Medium priority - this can be reviewed as time permits labels Oct 4, 2022
@gilles-peskine-arm gilles-peskine-arm force-pushed the ecdsa-use-psa-without-pkwrite branch from a6fb419 to cf4e95e Compare October 5, 2022 17:49
@gilles-peskine-arm gilles-peskine-arm added the size-s Estimated task size: small (~2d) label Oct 5, 2022
@gilles-peskine-arm gilles-peskine-arm force-pushed the ecdsa-use-psa-without-pkwrite branch from cf4e95e to f9c1cec Compare October 5, 2022 20:19
@gilles-peskine-arm gilles-peskine-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-ci Needs to pass CI tests labels Oct 6, 2022
@gilles-peskine-arm gilles-peskine-arm added needs-preceding-pr Requires another PR to be merged first 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 priority-medium Medium priority - this can be reviewed as time permits labels Oct 11, 2022
@gilles-peskine-arm gilles-peskine-arm removed the needs-preceding-pr Requires another PR to be merged first label Dec 7, 2022
@mpg mpg added the priority-medium Medium priority - this can be reviewed as time permits label Jan 6, 2023
@mpg mpg self-requested a review January 6, 2023 09:27
@valeriosetti valeriosetti self-requested a review January 16, 2023 08:57
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.

In general it LGTM. I just have a couple of comments/suggestions for defines

Comment on lines 937 to 954
unsigned char buf[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(
PSA_VENDOR_ECC_MAX_CURVE_BITS )];
Copy link
Contributor

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

Comment on lines 1119 to 1136
unsigned char buf[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(
PSA_VENDOR_ECC_MAX_CURVE_BITS )];
Copy link
Contributor

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?

@gilles-peskine-arm
Copy link
Contributor Author

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.

@valeriosetti
Copy link
Contributor

if someone has time now and wants to adopt it, please feel free

I'll be glad to complete this work ;)

@valeriosetti valeriosetti self-assigned this Jan 26, 2023
@valeriosetti valeriosetti force-pushed the ecdsa-use-psa-without-pkwrite branch from 2099adb to 368fed2 Compare January 27, 2023 15:31
@valeriosetti
Copy link
Contributor

I just did a force push in order to align with the new code style

@valeriosetti valeriosetti added needs-ci Needs to pass CI tests and removed needs-work needs-ci Needs to pass CI tests labels Jan 30, 2023
mpg
mpg previously approved these changes Feb 8, 2023
gilles-peskine-arm and others added 13 commits February 8, 2023 13:39
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>
@valeriosetti
Copy link
Contributor

Following the suggestion made previously and since #6970 was just merged into development, this branch was rebased

@valeriosetti valeriosetti requested a review from mpg February 9, 2023 08:22
@valeriosetti valeriosetti removed needs-ci Needs to pass CI tests needs-preceding-pr Requires another PR to be merged first labels Feb 9, 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.

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" .

Copy link
Contributor

@mprse mprse left a 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];
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 buffer on stack and holds public key or signature, but just to confirm. Shouldn't this buffer be cleared in cleanup stage?

Copy link
Contributor

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.

@mprse mprse 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 Feb 24, 2023
@gilles-peskine-arm gilles-peskine-arm merged commit 7e677fa into Mbed-TLS:development Feb 28, 2023
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-crypto Crypto primitives and low-level interfaces enhancement priority-medium Medium priority - this can be reviewed as time permits size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants