PK: inline & simplify sign_func #515

Open
opened 2025-10-13 05:47:46 -05:00 by mpg · 1 comment
mpg commented 2025-10-13 05:47:46 -05:00 (Migrated from github.com)

This is an internal refactoring that should be completely transparent to users. Before the refactoring with want to double-check that we have sufficient testing, and add tests if necessary.

Things that should be tested:

  • Non-wrapping context:
    • Positive: RSA (v1.5), ECC (see "common" below)
    • Negative: trying to sign with a public key (seems missing?)
    • Negative: trying to sign with a key that doesn't allow signing (ECKEY_DH) -> according to lcov this is missing!
  • Wrapping context (created with pk_wrap_psa()):
    • Positive: RSA (v1.5 and v2.1 (anysalt or not)), ECC (plain ECDSA or deterministic) (see "common" below)
    • Negative: the key's usage bits don't allow signing
    • Negative: the key's algorithm is not a signature algorithm (eg RSA decrypt or ECDH)
  • Common:
    • Positive: check the result with pk_verify() or verify_ext(). When we know deterministic ECDSA will be used, check the signature output against test vectors.
    • Negative: the output buffer is too small (seems missing?)

Once testing is in good shape, we want to:

  • Move the body of all sign_func functions to the body of pk_sign_restartable().
  • Merge as much as possible of the various branches.
  • Avoid reliance on the PK type as much as possible.
  • Do any other cleanup or simplification we can think of.

See Manuel's prototype: #518

Note: we should not change observable behaviour, except that on error cases we can return a different error code, as long as the documentation was not promising a specific error code for this particular case (and the new code makes sense of course).

Scope

This issue is about the public function mbedtls_pk_sign() and the corresponding sign_func function pointers. The fact the mbedtls_pk_sign() happens to be implemented as the non-restartable case of mbedtls_pk_sign_restartable() is an implementation detail. The testing part of this issue is all about ensuring mbedtls_pk_sign() is tested appropriately.

Other issues in this series:

  • #522 is about mbedtls_pk_get_bitlen()
  • #521 is about mbedtls_pk_check_pair()
  • #519 is about mbedtls_pk_verify()
  • #520 is about mbedtls_pk_sign_restartable() and mbedtls_pk_verify_restartable()
This is an internal refactoring that should be completely transparent to users. Before the refactoring with want to double-check that we have sufficient testing, and add tests if necessary. Things that should be tested: - Non-wrapping context: - Positive: RSA (v1.5), ECC (see "common" below) - Negative: trying to sign with a public key (seems missing?) - Negative: trying to sign with a key that doesn't allow signing (`ECKEY_DH`) -> according to lcov this is missing! - Wrapping context (created with `pk_wrap_psa()`): - Positive: RSA (v1.5 and v2.1 (anysalt or not)), ECC (plain ECDSA or deterministic) (see "common" below) - Negative: the key's usage bits don't allow signing - Negative: the key's algorithm is not a signature algorithm (eg RSA decrypt or ECDH) - Common: - Positive: check the result with `pk_verify()` or `verify_ext()`. When we know deterministic ECDSA will be used, check the signature output against test vectors. - Negative: the output buffer is too small (seems missing?) Once testing is in good shape, we want to: - Move the body of all `sign_func` functions to the body of `pk_sign_restartable()`. - Merge as much as possible of the various branches. - Avoid reliance on the PK type as much as possible. - Do any other cleanup or simplification we can think of. See Manuel's prototype: #518 Note: we should not change observable behaviour, except that on error cases we can return a different error code, as long as the documentation was not promising a specific error code for this particular case (and the new code makes sense of course). ## Scope This issue is about the public function `mbedtls_pk_sign()` and the corresponding `sign_func` function pointers. The fact the `mbedtls_pk_sign()` happens to be implemented as the non-restartable case of `mbedtls_pk_sign_restartable()` is an implementation detail. The testing part of this issue is all about ensuring `mbedtls_pk_sign()` is tested appropriately. Other issues in this series: - #522 is about `mbedtls_pk_get_bitlen()` - #521 is about `mbedtls_pk_check_pair()` - #519 is about `mbedtls_pk_verify()` - #520 is about `mbedtls_pk_sign_restartable()` and `mbedtls_pk_verify_restartable()`
valeriosetti commented 2025-12-05 08:49:02 -06:00 (Migrated from github.com)

Based on the discussions we had during the meeting here I'm writing down the test matrix that I created by expanding requirements above. @mpg @bjwtaylor feel free to edit this message if you think that something is not correct or missing.

PK generating functions

  • non-wrapping with keypair
    • mbedtls_pk_copy_from_psa
    • mbedtls_pk_parse_key
  • non-wrapping with only public key
    • mbedtls_pk_copy_public_from_psa
    • mbedtls_pk_parse_public_key
  • wrapping
    • mbedtls_pk_wrap_psa

Test plan requirement:

  • Non-wrapping context:
    • Positive: RSA (v1.5), ECC (see "common" below)
    • Negative: trying to sign with a public key (seems missing?)
    • Negative: trying to sign with a key that doesn't allow signing (ECKEY_DH) -> according to lcov this is missing!
  • Wrapping context (created with pk_wrap_psa()):
    • Positive: RSA (v1.5 and v2.1 (anysalt or not)), ECC (plain ECDSA or deterministic) (see "common" below)
    • Negative: the key's usage bits don't allow signing
    • Negative: the key's algorithm is not a signature algorithm (eg RSA decrypt or ECDH)
  • Common:
    • Positive: check the result with pk_verify() or verify_ext(). When we know deterministic ECDSA will be used, check the signature output against test vectors.
    • Negative: the output buffer is too small (seems missing?)

Matrix expanded:

  • non-wrapping -> mbedtls_pk_parse_key

    • EC
      • positive: sign with pk_sign() and verify with pk_verify()
      • positive: sign with deterministic ECDSA and compare against test vectors
      • negative: sign with outbuf buffer to small
      • negative: sign with a key that doesn't allow signing (ECKEY_DH)
    • RSA
      • positive: sign with pk_sign() and verify with pk_verify()
      • positive: sign with pk_sign_ext() (for PSS) and verify with pk_verify_ext()
      • negative: sign with outbuf buffer to small
  • non-wrapping -> mbedtls_pk_copy_from_psa

    • EC
      • positive: sign with pk_sign() and verify with pk_verify()
      • positive: sign with deterministic ECDSA and compare against test vectors
      • negative: sign with outbuf buffer to small
      • negative: sign with a key that doesn't allow signing (ECKEY_DH)
    • RSA
      • positive: sign with pk_sign() and verify with pk_verify()
      • positive: sign with pk_sign_ext() (for PSS) and verify with pk_verify_ext()
      • negative: sign with outbuf buffer to small
  • non-wrapping -> mbedtls_pk_parse_public_key

    • EC
      • negative: sign with only public key
    • RSA
      • negative: sign with only public key
  • non-wrapping -> mbedtls_pk_copy_public_from_psa

    • EC
      • negative: sign with only public key
    • RSA
      • negative: sign with only public key
  • wrapping -> mbedtls_pk_wrap_psa

    • EC
      • positive: sign with pk_sign() and verify with pk_verify()
      • positive: sign with deterministic ECDSA and compare against test vectors
      • negative: sign with outbuf buffer to small
      • negative: sign when the key's usage bits don't allow signing
      • negative: the key's algorithm is not a signature algorithm (ex: ECDH)
    • RSA
      • positive: sign with pk_sign() and verify with pk_verify()
      • positive: sign with pk_sign_ext() and verify with pk_verify_ext() for PSS
      • negative: sign with outbuf buffer to small
      • negative: sign when the key's usage bits don't allow signing
      • negative: the key's algorithm is not a signature algorithm (ex: RSA encrypt)
Based on the discussions we had during the meeting here I'm writing down the test matrix that I created by expanding requirements above. @mpg @bjwtaylor feel free to edit this message if you think that something is not correct or missing. # PK generating functions - non-wrapping with keypair - mbedtls_pk_copy_from_psa - mbedtls_pk_parse_key - non-wrapping with only public key - mbedtls_pk_copy_public_from_psa - mbedtls_pk_parse_public_key - wrapping - mbedtls_pk_wrap_psa # Test plan requirement: - Non-wrapping context: - Positive: RSA (v1.5), ECC (see "common" below) - Negative: trying to sign with a public key (seems missing?) - Negative: trying to sign with a key that doesn't allow signing (ECKEY_DH) -> according to lcov this is missing! - Wrapping context (created with pk_wrap_psa()): - Positive: RSA (v1.5 and v2.1 (anysalt or not)), ECC (plain ECDSA or deterministic) (see "common" below) - Negative: the key's usage bits don't allow signing - Negative: the key's algorithm is not a signature algorithm (eg RSA decrypt or ECDH) - Common: - Positive: check the result with pk_verify() or verify_ext(). When we know deterministic ECDSA will be used, check the signature output against test vectors. - Negative: the output buffer is too small (seems missing?) # Matrix expanded: - non-wrapping -> mbedtls_pk_parse_key - EC - [ ] positive: sign with pk_sign() and verify with pk_verify() - [ ] positive: sign with deterministic ECDSA and compare against test vectors - [ ] negative: sign with outbuf buffer to small - [ ] negative: sign with a key that doesn't allow signing (ECKEY_DH) - RSA - [ ] positive: sign with pk_sign() and verify with pk_verify() - [ ] positive: sign with pk_sign_ext() (for PSS) and verify with pk_verify_ext() - [ ] negative: sign with outbuf buffer to small - non-wrapping -> mbedtls_pk_copy_from_psa - EC - [ ] positive: sign with pk_sign() and verify with pk_verify() - [ ] positive: sign with deterministic ECDSA and compare against test vectors - [ ] negative: sign with outbuf buffer to small - [ ] negative: sign with a key that doesn't allow signing (ECKEY_DH) - RSA - [ ] positive: sign with pk_sign() and verify with pk_verify() - [ ] positive: sign with pk_sign_ext() (for PSS) and verify with pk_verify_ext() - [ ] negative: sign with outbuf buffer to small - non-wrapping -> mbedtls_pk_parse_public_key - EC - [ ] negative: sign with only public key - RSA - [ ] negative: sign with only public key - non-wrapping -> mbedtls_pk_copy_public_from_psa - EC - [ ] negative: sign with only public key - RSA - [ ] negative: sign with only public key - wrapping -> mbedtls_pk_wrap_psa - EC - [ ] positive: sign with pk_sign() and verify with pk_verify() - [ ] positive: sign with deterministic ECDSA and compare against test vectors - [ ] negative: sign with outbuf buffer to small - [ ] negative: sign when the key's usage bits don't allow signing - [ ] negative: the key's algorithm is not a signature algorithm (ex: ECDH) - RSA - [ ] positive: sign with pk_sign() and verify with pk_verify() - [ ] positive: sign with pk_sign_ext() and verify with pk_verify_ext() for PSS - [ ] negative: sign with outbuf buffer to small - [ ] negative: sign when the key's usage bits don't allow signing - [ ] negative: the key's algorithm is not a signature algorithm (ex: RSA encrypt)
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-Mbed-TLS/TF-PSA-Crypto#515
No description provided.