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

Replace MBEDTLS_PK_HAVE_ECDSA* with PSA_WANT counterparts #9385

Merged
merged 10 commits into from
Sep 19, 2024

Conversation

eleuzi01
Copy link
Contributor

@eleuzi01 eleuzi01 commented Jul 11, 2024

Description

Resolves #9337

PR checklist

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

Notes for the submitter

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

Help make review efficient:

  • Multiple simple commits
    • please structure your PR into a series of small commits, each of which does one thing
  • Avoid force-push
    • please do not force-push to update your PR - just add new commit(s)
  • See our Guidelines for Contributors for more details about the review process.

@eleuzi01 eleuzi01 added enhancement component-crypto Crypto primitives and low-level interfaces needs-ci Needs to pass CI tests priority-high High priority - will be reviewed soon size-xs Estimated task size: extra small (a few hours at most) labels Jul 11, 2024
@gabor-mezei-arm gabor-mezei-arm self-requested a review July 17, 2024 10:32
@@ -2388,7 +2388,7 @@ static inline int mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(
const uint16_t sig_alg)
{
switch (sig_alg) {
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
#if defined(PSA_WANT_ALG_ECDSA)
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be defined(PSA_WANT_ALG_ECDSA) || defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA). IIRC we've already decided to add a definition for that in include/psa/crypto_adjust_*.h. I don't know if it's already being added by a PR in flight.

Copy link
Contributor

Choose a reason for hiding this comment

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

PSA_HAVE_ALG_SOME_ECDSA from #9384

@eleuzi01 eleuzi01 added the needs-preceding-pr Requires another PR to be merged first label Jul 30, 2024
@eleuzi01 eleuzi01 force-pushed the replace-ecdsa-some branch 3 times, most recently from 2a7b29a to 8e3c7a8 Compare August 1, 2024 13:58
@eleuzi01 eleuzi01 removed the needs-preceding-pr Requires another PR to be merged first label Aug 1, 2024
@eleuzi01 eleuzi01 changed the title Replace MBEDTLS_PK_CAN_ECDSA_SOME with PSA_WANT_ALG_ECDSA Replace MBEDTLS_PK_CAN_ECDSA_SOME with PSA_HAVE_ALG_SOME_ECDSA Aug 6, 2024
@@ -2686,7 +2686,7 @@ int main(int argc, char *argv[])
}
key_cert_init = 2;
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
#if defined(PSA_HAVE_ALG_SOME_ECDSA)
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 one of many places where we were using MBEDTLS_PK_CAN_ECDSA_SOME but we should actually have used MBEDTLS_PK_CAN_ECDSA_SIGN. (Or MBEDTLS_PK_CAN_ECDSA_VERIFY in some places, but there's no way to configure the library with MBEDTLS_PK_CAN_ECDSA_SOME && !MBEDTLS_PK_CAN_ECDSA_VERIFY, since there's no way to have private-key ECC operations without the corresponding public-key ECC operation.) See #9473.

Technically, we're losing information here: now, when we see PSA_HAVE_ALG_SOME_ECDSA, we won't know whether that came from the incorrect usage of MBEDTLS_PK_CAN_ECDSA_SOME. It doesn't matter much because we don't currently test any configuration where MBEDTLS_PK_CAN_ECDSA_VERIFY is enabled but not MBEDTLS_PK_CAN_ECDSA_SIGN, so I expect there are more places where we don't have the correct dependencies. Still, I think it would be better to change to PSA_HAVE_ALG_SOME_ECDSA && PSA_WANT_KEY_TYPE_ECC_KEY_PAIR in places where ECDSA-sign is required.

(Incidentally, PSA_HAVE_ALG_SOME_ECDSA && PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY is effectively equivalent to PSA_HAVE_ALG_SOME_ECDSA, because ECDSA can only be done with an ECC key.)

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.

I've reviewed this pull request, paying particular attention to where MBEDTLS_PK_CAN_ECDSA_SOME should have been MBEDTLS_PK_CAN_ECDSA_SIGN (see #9473). I think we should fix those while we're at it.

I also noticed a couple of other preexisting problems, which we can either fix now or declare out of scope.

We should backport the fixes to 3.6 (e.g. if we're adding && PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_xxx here then we should replace _SOME by _SIGN in 3.6).

@@ -2686,7 +2686,7 @@ int main(int argc, char *argv[])
}
key_cert_init = 2;
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
#if defined(PSA_HAVE_ALG_SOME_ECDSA)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#if defined(PSA_HAVE_ALG_SOME_ECDSA)
#if defined(PSA_HAVE_ALG_SOME_ECDSA) && defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT)

@@ -3297,7 +3297,7 @@ exit:
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:PSA_WANT_ECC_SECP_R1_256:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_PK_CAN_ECDSA_SOME */
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:PSA_WANT_ECC_SECP_R1_256:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:PSA_HAVE_ALG_SOME_ECDSA */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:PSA_WANT_ECC_SECP_R1_256:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:PSA_HAVE_ALG_SOME_ECDSA */
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:PSA_WANT_ECC_SECP_R1_256:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:PSA_HAVE_ALG_SOME_ECDSA:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT */

Preexisting and out of scope, but related to your recent work: why is there both PSA_WANT_ECC_SECP_R1_256 and MBEDTLS_ECP_HAVE_SECP384R1 here?

Also those dependencies look wrong in other preexisting ways (missing ECDH, and I don't see why RSA is required).

@@ -903,7 +903,7 @@ depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_TEST_PSA_ECC_AT_LEAST_ONE_CURVE
pk_get_psa_attributes_fail:MBEDTLS_PK_ECKEY_DH:FROM_PAIR:PSA_KEY_USAGE_DECRYPT:MBEDTLS_ERR_PK_TYPE_MISMATCH

PSA attributes for pk: ECDSA pair DECRYPT (bad)
depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_TEST_PSA_ECC_AT_LEAST_ONE_CURVE:MBEDTLS_PK_CAN_ECDSA_SOME
depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_TEST_PSA_ECC_AT_LEAST_ONE_CURVE:PSA_HAVE_ALG_SOME_ECDSA
Copy link
Contributor

Choose a reason for hiding this comment

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

All the test cases that have MBEDTLS_PK_ECxxx as the first parameter and FROM_PAIR as the second parameter should depend on PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT. (The ones with FROM_PUBLIC should depend on PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY, but that's not important since you can't usefully enable ECDSA without that.) But that may be covered later when replacing MBEDTLS_PK_HAVE_ECC_KEYS.

@@ -377,11 +377,11 @@ depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_NO_PADDING:PSA_WANT_ALG_SHA_25
handshake_cipher:"TLS-DHE-RSA-WITH-AES-256-CBC-SHA256":MBEDTLS_PK_RSA:0

Handshake, ECDHE-ECDSA-WITH-AES-256-CCM
depends_on:PSA_WANT_KEY_TYPE_AES:MBEDTLS_SSL_HAVE_CCM:MBEDTLS_PK_CAN_ECDSA_SOME:PSA_WANT_ECC_SECP_R1_256:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED:!MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
depends_on:PSA_WANT_KEY_TYPE_AES:MBEDTLS_SSL_HAVE_CCM:PSA_HAVE_ALG_SOME_ECDSA:PSA_WANT_ECC_SECP_R1_256:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED:!MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically all handshake_cipher and handshake_ciphersuite_select test cases involving a cipher suite with ECDH, ECDHE or ECDSA in its name should depend on PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT (for ECDSA on the server) and PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_GENERATE (for ECDHE on both sides). But the dependency on MBEDTLS_KEY_EXCHANGE_xxx_ENABLED is enough and we could remove the dependencies on PSA_WANT_ALG_ECxxx and PSA_WANT_KEY_TYPE_ECC_xxx.

@@ -2858,7 +2858,7 @@ SSL TLS 1.3 Record Encryption, tls13.ulfheim.net Example #1
# - App data payload: 70696e67
# - Complete record: 1703030015c74061535eb12f5f25a781957874742ab7fb305dd5
# - Padding used: No (== granularity 1)
depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_GCM:PSA_WANT_ALG_SHA_256:MBEDTLS_PK_CAN_ECDSA_SOME:MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_GCM:PSA_WANT_ALG_SHA_256:PSA_HAVE_ALG_SOME_ECDSA:MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
Copy link
Contributor

Choose a reason for hiding this comment

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

Preexisting, but I think ECC-related dependencies on ssl_tls13_record_protection are wrong. As far as I can tell from the name and code, this test does not involve ECC in any way. I'm sure that MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED is wrong since that's a TLS 1.2 feature but the test function is about TLS 1.3 only.

Suggested change
depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_GCM:PSA_WANT_ALG_SHA_256:PSA_HAVE_ALG_SOME_ECDSA:MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_GCM:PSA_WANT_ALG_SHA_256

and same for the other ssl_tls13_record_protection test cases.

@eleuzi01 eleuzi01 changed the title Replace MBEDTLS_PK_CAN_ECDSA_SOME with PSA_HAVE_ALG_SOME_ECDSA Replace MBEDTLS_PK_CAN/HAVE_ECDSA* with PSA_WANT counterparts Sep 3, 2024
@eleuzi01 eleuzi01 force-pushed the replace-ecdsa-some branch 2 times, most recently from 37dbab3 to 5b20b62 Compare September 4, 2024 11:00
@ronald-cron-arm ronald-cron-arm requested review from davidhorstmann-arm and removed request for gabor-mezei-arm September 4, 2024 11:04
Copy link
Contributor

@davidhorstmann-arm davidhorstmann-arm left a comment

Choose a reason for hiding this comment

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

Code changes LGTM (but CI is unhappy)

Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
To make CI happier

Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
@eleuzi01
Copy link
Contributor Author

Forgot to ask: are we happy with the names PSA_HAVE_ALG_ECDSA_SIGN and PSA_HAVE_ALG_ECDSA_VERIFY or should these be called something else?

Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
@eleuzi01 eleuzi01 changed the title Replace MBEDTLS_PK_CAN/HAVE_ECDSA* with PSA_WANT counterparts Replace MBEDTLS_PK_HAVE_ECDSA* with PSA_WANT counterparts Sep 12, 2024
@eleuzi01 eleuzi01 added needs-review Every commit must be reviewed by at least two team members, and removed needs-work needs-ci Needs to pass CI tests labels Sep 12, 2024
@eleuzi01 eleuzi01 added size-s Estimated task size: small (~2d) and removed size-xs Estimated task size: extra small (a few hours at most) labels Sep 16, 2024
Copy link
Contributor

@davidhorstmann-arm davidhorstmann-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, thanks!

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

@@ -377,11 +377,11 @@ depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_NO_PADDING:PSA_WANT_ALG_SHA_25
handshake_cipher:"TLS-DHE-RSA-WITH-AES-256-CBC-SHA256":MBEDTLS_PK_RSA:0

Handshake, ECDHE-ECDSA-WITH-AES-256-CCM
depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CCM:PSA_HAVE_ALG_SOME_ECDSA:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ECC_SECP_R1_384:MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED:!MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CCM:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_ECC_SECP_R1_384:MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED:!MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
Copy link
Contributor

Choose a reason for hiding this comment

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

A note on commit messages — never mind for this time, but please keep this in mind in the future.

Commit messages are useful for future readers. They should summarize what is being done, and explain why it's done. The relative importance can vary: sometimes one or the other is obvious, or part of a larger series.

For example, the first commit in this PR does a mass renaming. Its commit message “Replace MBEDTLS_PK_CAN_ECDSA_SOME with PSA_HAVE_ALG_SOME_ECDSA” is a good because the diff is very long, but the content is conceptually simple and nicely summarized by the title line. The reason why we're doing this renaming is that it's part of a larger task, and there's no point in explaining why we do this task in every commit. So that commit message is fine.

On the other hand, “Address review comments: remove dependencies” is not a very good commit message. “Remove dependencies” is ok as a summary, but not very informative: which dependencies? As a rule of thumb, apart from really boring things like “fix typo in documentation”, if you can potentially have multiple commits in the same pull request with the same commit title line, the title line isn't informative enough. On the other hand, “address review comments” is not relevant in a commit message. Technically that's why you're doing it, sure, but that reason won't matter after the pull request is merged. Why was there a review comment that suggested to remove these dependencies? For a future reader, there are several plausible reasons and it's hard to tell. Maybe (1) the tests were skipped when they shouldn't be? Or maybe (2) we've made the test code or the library more capable and so we're running more tests? Or (3) the dependencies are actually redundant, with no change in behavior. As it turns out, it's a mixture of (1) and (3), and it would be useful to convey this to a future reader. So a better commit message would be something like

Remove redundant dependencies on ECC/ECDSA

  • Requiring ECC/ECDSA from cryptography is redundant to requiring ECDH/ECDSA in the key exchange.
  • Don't require ECDSA in test cases that only do record protection and not a handshake.

Note how writing this down has brought me to mention both ECC and ECDSA, and the distinction between the two is part of why “Bring back some dependencies” was needed. (Though I'm not sure why MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED doesn't imply PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY — I guess for some legacy builds because we're only part way through the configuration changes?)

@gilles-peskine-arm gilles-peskine-arm added approved Design and code approved - may be waiting for CI or backports needs-backports Backports are missing or are pending review and approval. and removed needs-review Every commit must be reviewed by at least two team members, labels Sep 18, 2024
@davidhorstmann-arm davidhorstmann-arm removed the needs-backports Backports are missing or are pending review and approval. label Sep 19, 2024
@davidhorstmann-arm davidhorstmann-arm added this pull request to the merge queue Sep 19, 2024
Merged via the queue into Mbed-TLS:development with commit bae154d Sep 19, 2024
4 of 6 checks passed
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-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)
Development

Successfully merging this pull request may close these issues.

Replace MBEDTLS_PK_CAN/HAVE_ECDSA* with its PSA_WANT counterparts
3 participants