PK: inline and simplify get_bitlen functions #522

Closed
opened 2025-10-14 08:21:59 -05:00 by mpg · 4 comments
mpg commented 2025-10-14 08:21:59 -05:00 (Migrated from github.com)

Same as #515 and #519 but with get_bitlen function pointers. Should be simpler.

Same as #515 and #519 but with `get_bitlen` function pointers. Should be simpler.
valeriosetti commented 2025-12-10 03:31:27 -06:00 (Migrated from github.com)

Here's an attempt to define a test matrix for this task. @mpg @bjwtaylor feel free to extend/integrated/fix if needed.

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 matrix:

  • negative: verify that a NULL PK context pointer and NULL pk_info return 0 length

  • non-wrapping -> mbedtls_pk_parse_key

    • mbedtls_pk_get_bitlen() for EC curve#1
    • mbedtls_pk_get_bitlen() for EC curve#2
    • mbedtls_pk_get_bitlen() for RSA bit-size#1
    • mbedtls_pk_get_bitlen() for RSA bit-size#2
  • non-wrapping -> mbedtls_pk_copy_from_psa

    • mbedtls_pk_get_bitlen() for EC curve#1
    • mbedtls_pk_get_bitlen() for EC curve#2
    • mbedtls_pk_get_bitlen() for RSA bit-size#1
    • mbedtls_pk_get_bitlen() for RSA bit-size#2
  • non-wrapping -> mbedtls_pk_copy_public_from_psa

    • mbedtls_pk_get_bitlen() for EC curve#1
    • mbedtls_pk_get_bitlen() for EC curve#2
    • mbedtls_pk_get_bitlen() for RSA bit-size#1
    • mbedtls_pk_get_bitlen() for RSA bit-size#2
  • non-wrapping -> mbedtls_pk_parse_public_key

    • mbedtls_pk_get_bitlen() for EC curve#1
    • mbedtls_pk_get_bitlen() for EC curve#2
    • mbedtls_pk_get_bitlen() for RSA bit-size#1
    • mbedtls_pk_get_bitlen() for RSA bit-size#2
  • wrapping -> mbedtls_pk_wrap_psa

    • mbedtls_pk_get_bitlen() for EC curve#1
    • mbedtls_pk_get_bitlen() for EC curve#2
    • mbedtls_pk_get_bitlen() for RSA bit-size#1
    • mbedtls_pk_get_bitlen() for RSA bit-size#2

Notes

  • EC curve#1 and EC curve#2 are 2 curves of different bit size. For example secp256r1 and secp521r1.
  • RSA bit-size#1 and RSA bit-size#2 are 2 different bit size for RSA. For example 1024 and 4096
Here's an attempt to define a test matrix for this task. @mpg @bjwtaylor feel free to extend/integrated/fix if needed. # 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 matrix: - [ ] negative: verify that a NULL PK context pointer and NULL pk_info return 0 length - non-wrapping -> mbedtls_pk_parse_key - [ ] mbedtls_pk_get_bitlen() for EC curve#1 - [ ] mbedtls_pk_get_bitlen() for EC curve#2 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#1 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#2 - non-wrapping -> mbedtls_pk_copy_from_psa - [ ] mbedtls_pk_get_bitlen() for EC curve#1 - [ ] mbedtls_pk_get_bitlen() for EC curve#2 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#1 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#2 - non-wrapping -> mbedtls_pk_copy_public_from_psa - [ ] mbedtls_pk_get_bitlen() for EC curve#1 - [ ] mbedtls_pk_get_bitlen() for EC curve#2 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#1 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#2 - non-wrapping -> mbedtls_pk_parse_public_key - [ ] mbedtls_pk_get_bitlen() for EC curve#1 - [ ] mbedtls_pk_get_bitlen() for EC curve#2 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#1 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#2 - wrapping -> mbedtls_pk_wrap_psa - [ ] mbedtls_pk_get_bitlen() for EC curve#1 - [ ] mbedtls_pk_get_bitlen() for EC curve#2 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#1 - [ ] mbedtls_pk_get_bitlen() for RSA bit-size#2 # Notes - `EC curve#1` and `EC curve#2` are 2 curves of different bit size. For example secp256r1 and secp521r1. - `RSA bit-size#1` and `RSA bit-size#2` are 2 different bit size for RSA. For example 1024 and 4096
mpg commented 2025-12-10 05:54:03 -06:00 (Migrated from github.com)

Thanks @valeriosetti for making the test matrix explicit! Just one point though: I don't think the negative needs to be repeated: there are 5 ways to populate a context, but only 1 way not to populate it (that is, try using it right after pk_init()).

In terms of implementing this test matrix, I was thinking we could use the same strategy as in https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/577:

  1. one function for the negative cases: (a) NULL pointer and (b) unpopulated context (== NULL pk_info))
  2. one function for contexts populated with either parse_key() or parse_public_key(), with an enum parameter telling which one it is, and the key material in the test data
  3. one function for context populated from a PSA key, with a parameter indicating how, based on make_psa_key_from_predefined() (see also https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/598)

For (1) I think we already have a general function for this kind of negative testing, and probably it already tests those two things already, we should just double-check and add (b) if missing. For (2) and (3) we'd create new test functions for clarity. That way it's obvious from the data file that get_bitlen() is properly tested, because we can see the expanded test matrix right there in the data file. (As is the case in #577)

Wdyt @valeriosetti @bjwtaylor ?

Thanks @valeriosetti for making the test matrix explicit! Just one point though: I don't think the negative needs to be repeated: there are 5 ways to populate a context, but only 1 way not to populate it (that is, try using it right after `pk_init()`). In terms of implementing this test matrix, I was thinking we could use the same strategy as in https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/577: 1. one function for the negative cases: (a) NULL pointer and (b) unpopulated context (== NULL pk_info)) 2. one function for contexts populated with either `parse_key()` or `parse_public_key()`, with an enum parameter telling which one it is, and the key material in the test data 3. one function for context populated from a PSA key, with a parameter indicating how, based on `make_psa_key_from_predefined()` (see also https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/598) For (1) I think we already have a general function for this kind of negative testing, and probably it already tests those two things already, we should just double-check and add (b) if missing. For (2) and (3) we'd create new test functions for clarity. That way it's obvious from the data file that `get_bitlen()` is properly tested, because we can see the expanded test matrix right there in the data file. (As is the case in #577) Wdyt @valeriosetti @bjwtaylor ?
valeriosetti commented 2025-12-10 07:03:39 -06:00 (Migrated from github.com)

That's fine for me. I updated the test matrix above.

That's fine for me. I updated the test matrix above.
bjwtaylor commented 2025-12-10 07:33:52 -06:00 (Migrated from github.com)

Ok, let me look into it and I'll update the PR as per the above.

Ok, let me look into it and I'll update the PR as per the above.
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#522
No description provided.