Inline get bitlen #575

Merged
bjwtaylor merged 47 commits from inline-get_bitlen into development 2026-01-14 08:50:32 -06:00
bjwtaylor commented 2025-11-17 04:31:40 -06:00 (Migrated from github.com)

Description

Add additional tests to for the get bitlen functionality and simplify the current implementation by in lining it. Resolves https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/522

Verification report provided here

https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/575#issuecomment-3670594867

PR checklist

Please remove the segment/s on either side of the | symbol as appropriate, and add any relevant link/s to the end of the line.
If the provided content is part of the present PR remove the # symbol.

  • changelog not required because: No public changes
  • framework PR not required
  • mbedtls development PR not required because: No changes
  • mbedtls 3.6 PR not required because: No backports
  • tests provided
## Description Add additional tests to for the get bitlen functionality and simplify the current implementation by in lining it. Resolves https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/522 Verification report provided here https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/575#issuecomment-3670594867 ## PR checklist Please remove the segment/s on either side of the | symbol as appropriate, and add any relevant link/s to the end of the line. If the provided content is part of the present PR remove the # symbol. - [x] **changelog** not required because: No public changes - [x] **framework PR** not required - [x] **mbedtls development PR** not required because: No changes - [x] **mbedtls 3.6 PR** not required because: No backports - **tests** provided
gilles-peskine-arm (Migrated from github.com) reviewed 2025-11-17 04:31:40 -06:00
gilles-peskine-arm (Migrated from github.com) reviewed 2025-11-19 07:54:16 -06:00
gilles-peskine-arm (Migrated from github.com) commented 2025-11-19 07:54:16 -06:00
depends_on:PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY:PSA_VENDOR_RSA_MAX_KEY_BITS>=4096
```suggestion depends_on:PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY:PSA_VENDOR_RSA_MAX_KEY_BITS>=4096 ```
bjwtaylor (Migrated from github.com) reviewed 2025-11-19 08:03:50 -06:00
bjwtaylor (Migrated from github.com) commented 2025-11-19 08:03:50 -06:00

Thanks, I'll add this in

Thanks, I'll add this in
bjwtaylor (Migrated from github.com) reviewed 2025-11-19 09:01:22 -06:00
bjwtaylor (Migrated from github.com) commented 2025-11-19 09:01:22 -06:00

I've committed the change, though locally it seems to fail on keys bigger than 2048. Lets see if the CI agrees, though have I missed something?

I've committed the change, though locally it seems to fail on keys bigger than 2048. Lets see if the CI agrees, though have I missed something?
bjwtaylor (Migrated from github.com) reviewed 2025-11-19 09:43:18 -06:00
bjwtaylor (Migrated from github.com) commented 2025-11-19 09:43:18 -06:00

Looks like the CI has failed with that variable set. From the code it looks like maybe I need MBEDTLS_TEST_PK_ALLOW_RSA_KEY_PAIR_4096 or MBEDTLS_TEST_STATIC_KEY_SLOTS_SUPPORT_RSA_4096 as this appears to get set here github.com/Mbed-TLS/mbedtls-framework@6c9076eef1/tests/include/test/psa_crypto_helpers.h (L534) when the buffer is big enough to store 4096. Though I'm not sure this is what these variables were intended for?

Looks like the CI has failed with that variable set. From the code it looks like maybe I need MBEDTLS_TEST_PK_ALLOW_RSA_KEY_PAIR_4096 or MBEDTLS_TEST_STATIC_KEY_SLOTS_SUPPORT_RSA_4096 as this appears to get set here https://github.com/Mbed-TLS/mbedtls-framework/blob/6c9076eef1aaba371550ebc1145aed7227154b99/tests/include/test/psa_crypto_helpers.h#L534 when the buffer is big enough to store 4096. Though I'm not sure this is what these variables were intended for?
bjwtaylor (Migrated from github.com) reviewed 2025-11-20 04:22:50 -06:00
bjwtaylor (Migrated from github.com) commented 2025-11-20 04:22:50 -06:00

I've added a commit that changes the dependency to !MBEDTLS_PSA_STATIC_KEY_SLOTS, which I think it what @davidhorstmann-arm was suggesting. It seems to do the trick in the local tests, though let me know what people think and lets see if the CI agrees.

I've added a commit that changes the dependency to !MBEDTLS_PSA_STATIC_KEY_SLOTS, which I think it what @davidhorstmann-arm was suggesting. It seems to do the trick in the local tests, though let me know what people think and lets see if the CI agrees.
valeriosetti (Migrated from github.com) requested changes 2025-12-01 08:21:54 -06:00
valeriosetti (Migrated from github.com) commented 2025-12-01 08:00:35 -06:00

I don't see why changes added by this PR require this include. Why is it necessary?

I don't see why changes added by this PR require this include. Why is it necessary?
valeriosetti (Migrated from github.com) commented 2025-12-01 08:06:26 -06:00

I'm not a huge fan of this approach. Nowadays:

  • a PK context with a key pair has ctx->priv_id set (EC, RSA, Opaque);
  • a PK context with transparent EC|RSA key pair or public key has ctx->bits set.

I suggest to simplify this block like this:

    if (ctx->bits != 0) {
        return ctx->bits;
    } else {
        if (PSA_SUCCESS != psa_get_key_attributes(ctx->priv_id, &attributes)) {
            return 0;
        }
        bits = psa_get_key_bits(&attributes);
        psa_reset_key_attributes(&attributes);
        return bits;
    }

It should work the same (TBH I didn't test it), but it drops the dependency on strncmp().

I'm not a huge fan of this approach. Nowadays: - a PK context with a key pair has `ctx->priv_id` set (EC, RSA, Opaque); - a PK context with transparent EC|RSA key pair or public key has `ctx->bits` set. I suggest to simplify this block like this: ``` if (ctx->bits != 0) { return ctx->bits; } else { if (PSA_SUCCESS != psa_get_key_attributes(ctx->priv_id, &attributes)) { return 0; } bits = psa_get_key_bits(&attributes); psa_reset_key_attributes(&attributes); return bits; } ``` It should work the same (TBH I didn't test it), but it drops the dependency on `strncmp()`.
valeriosetti (Migrated from github.com) commented 2025-12-01 08:10:42 -06:00

??

??
valeriosetti (Migrated from github.com) commented 2025-12-01 08:13:30 -06:00
    TEST_ASSERT(mbedtls_pk_get_len(&priv_pk) == PSA_BITS_TO_BYTES(bitlen));

?

```suggestion TEST_ASSERT(mbedtls_pk_get_len(&priv_pk) == PSA_BITS_TO_BYTES(bitlen)); ``` ?
valeriosetti (Migrated from github.com) commented 2025-12-01 08:19:42 -06:00

I think you can use mbedtls_pk_copy_public_from_psa() here and get rid of psa_pub_key_from_priv() call and the pub_key variable

I think you can use `mbedtls_pk_copy_public_from_psa()` here and get rid of `psa_pub_key_from_priv()` call and the `pub_key` variable
valeriosetti (Migrated from github.com) commented 2025-12-01 08:21:44 -06:00

The call to psa_destroy_key() here wasn't required before this PR as the key should normally be destroyed just before the exit label. Is this really necessary?

The call to `psa_destroy_key()` here wasn't required before this PR as the key should normally be destroyed just before the `exit` label. Is this really necessary?
mpg (Migrated from github.com) reviewed 2025-12-02 03:13:08 -06:00
mpg (Migrated from github.com) commented 2025-12-02 03:13:08 -06:00

Seconded. In addition to Valerio's points, eventually we want to remove pk_info entirely, so please don't rely on it in new code. (Also, eventually we want to remove most if not all strings from crypto, so don't rely on string comparison in new code.)

Seconded. In addition to Valerio's points, eventually we want to remove `pk_info` entirely, so please don't rely on it in new code. (Also, eventually we want to remove most if not all strings from crypto, so don't rely on string comparison in new code.)
mpg (Migrated from github.com) requested changes 2025-12-02 03:58:28 -06:00
mpg (Migrated from github.com) left a comment

In addition to the line comments I left, I'm not convinced by the test plan. There are currently 5 ways to populate a PK context:

  1. mbedtls_pk_wrap_psa()
  2. mbedtls_pk_copy_from_psa()
  3. mbedtls_pk_copy_public_from_psa()
  4. mbedtls_pk_parse_key()
  5. mbedtls_pk_parse_public_key()

We want each of them to be appropriately tested (both keypairs and public keys (except for (1)), both RSA and ECC, each with 2 different key sizes).

I'm satisfied that the first case is well covered by the improvements you made to pk_psa_utils().

With minor adaptations (see line comment) this function can probably also cover (2) and (3). On second thought, I'm not sure that would be right: pk_psa_utils() is currently focused on wrapping, so adding unrelated stuff just muddies the water. Perhaps having a dedicated testing function would be cleaner. Or re-use pk_copy_from_psa_success()?

However I'm more worried about (4) and (5). Superficially, it looks like it should be the job of the pk_utils() test function, but it calls pk_setup() which constructs the context manually. (Also, it only does keypairs, not public keys.)

So, unless I missed something, (4) and (5) are not covered at all. I'm not sure what's the simplest way to test them at this point. One way would be to add a bitlen parameter to the existing parsing functions in test_suite_pkparse, but that would probably mean a lot of lines changed. Or we could just add a dedicated function in test_suite_pk that takes as input a serialized private or public key (with a parameter indicating which it is), and its expected bitlen, then calls the appropriate parse function, then get_bitlen() and we're done.

Wdyt?

In addition to the line comments I left, I'm not convinced by the test plan. There are currently 5 ways to populate a PK context: 1. `mbedtls_pk_wrap_psa()` 2. `mbedtls_pk_copy_from_psa()` 3. `mbedtls_pk_copy_public_from_psa()` 4. `mbedtls_pk_parse_key()` 5. `mbedtls_pk_parse_public_key()` We want each of them to be appropriately tested (both keypairs and public keys (except for (1)), both RSA and ECC, each with 2 different key sizes). I'm satisfied that the first case is well covered by the improvements you made to `pk_psa_utils()`. With minor adaptations (see line comment) this function can probably also cover (2) and (3). On second thought, I'm not sure that would be right: `pk_psa_utils()` is currently focused on wrapping, so adding unrelated stuff just muddies the water. Perhaps having a dedicated testing function would be cleaner. Or re-use `pk_copy_from_psa_success()`? However I'm more worried about (4) and (5). Superficially, it looks like it should be the job of the `pk_utils()` test function, but it calls `pk_setup()` which constructs the context manually. (Also, it only does keypairs, not public keys.) So, unless I missed something, (4) and (5) are not covered at all. I'm not sure what's the simplest way to test them at this point. One way would be to add a `bitlen` parameter to the existing parsing functions in `test_suite_pkparse`, but that would probably mean a lot of lines changed. Or we could just add a dedicated function in `test_suite_pk` that takes as input a serialized private or public key (with a parameter indicating which it is), and its expected bitlen, then calls the appropriate parse function, then `get_bitlen()` and we're done. Wdyt?
mpg (Migrated from github.com) commented 2025-12-02 03:16:42 -06:00

On second thought, shouldn't we just ensure that ctx->bits is always set for all keys, as we do for psa_type in #532? I don't really like struct having fields that are sometimes set sometimes not depending on context and you have to remember when - sure it's documented, but how often do we forget to update the documentation when we change the code?

On second thought, shouldn't we just ensure that `ctx->bits` is always set for all keys, as we do for `psa_type` in #532? I don't really like struct having fields that are sometimes set sometimes not depending on context and you have to remember when - sure it's documented, but how often do we forget to update the documentation when we change the code?
mpg (Migrated from github.com) commented 2025-12-02 03:21:07 -06:00

Pre-existing, but this looks wrong: we'll be creating a key pair, so we should depend on RSA_KEY_PAIR not RSA_PUBLIC_KEY.

Pre-existing, but this looks wrong: we'll be creating a key pair, so we should depend on `RSA_KEY_PAIR` not `RSA_PUBLIC_KEY`.
mpg (Migrated from github.com) commented 2025-12-02 03:43:29 -06:00

I think ideally, here or elsewhere, we want to test get_bitlen() both on contexts created with copy_from_psa() and with copy_public_from_psa() (see my general comment about the test plan). It would probably be convenient to do it here.

However this should either be done using new PK contexts, or after the existing "test that freeing the context does not destroy the key" block, that's currently below.

I think ideally, here or elsewhere, we want to test `get_bitlen()` both on contexts created with `copy_from_psa()` and with `copy_public_from_psa()` (see my general comment about the test plan). It would probably be convenient to do it here. However this should either be done using new PK contexts, or after the existing "test that freeing the context does not destroy the key" block, that's currently below.
mpg (Migrated from github.com) commented 2025-12-02 03:27:00 -06:00

Even if the key is normally destroyed earlier, we should also consider the abnormal path where an assertion fails and we jump to exit early: in that case we still want to destroy the key in order to avoid any leaks. So, I think the previous version was wrong and the new one is more robust.

(Perhaps the comment on line 720 should be moved to the line before to make it clearer it applies to the whole paragraph not just its first line.)

Even if the key is normally destroyed earlier, we should also consider the abnormal path where an assertion fails and we jump to exit early: in that case we still want to destroy the key in order to avoid any leaks. So, I think the previous version was wrong and the new one is more robust. (Perhaps the comment on line 720 should be moved to the line before to make it clearer it applies to the whole paragraph not just its first line.)
valeriosetti commented 2025-12-02 04:18:26 -06:00 (Migrated from github.com)

One way would be to add a bitlen parameter to the existing parsing functions in test_suite_pkparse, but that would probably mean a lot of lines changed. Or we could just add a dedicated function in test_suite_pk that takes as input a serialized private or public key (with a parameter indicating which it is), and its expected bitlen, then calls the appropriate parse function, then get_bitlen() and we're done.

Adding my personal opinion here. Unless changes to already existing test functions is trivial/simple I prefer to create new test functions for specific purposes. Extending what's already there makes test functions hard to read: lot of input parameters, lot of operations carried out internally... IMO this is not easy to review and maintain.

The worst consequence that I can think of in adding new test functions is that we duplicate some test that's already there. This is not bad though: it's true that testing will take slightly longer, but that's better than missing test coverage or make something hard to read.

> One way would be to add a bitlen parameter to the existing parsing functions in test_suite_pkparse, but that would probably mean a lot of lines changed. Or we could just add a dedicated function in test_suite_pk that takes as input a serialized private or public key (with a parameter indicating which it is), and its expected bitlen, then calls the appropriate parse function, then get_bitlen() and we're done. Adding my personal opinion here. Unless changes to already existing test functions is trivial/simple I prefer to create new test functions for specific purposes. Extending what's already there makes test functions hard to read: lot of input parameters, lot of operations carried out internally... IMO this is not easy to review and maintain. The worst consequence that I can think of in adding new test functions is that we duplicate some test that's already there. This is not bad though: it's true that testing will take slightly longer, but that's better than missing test coverage or make something hard to read.
bjwtaylor (Migrated from github.com) reviewed 2025-12-02 09:28:55 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-02 09:28:54 -06:00

It's not required, it should now be removed.

It's not required, it should now be removed.
bjwtaylor (Migrated from github.com) reviewed 2025-12-02 09:31:51 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-02 09:31:51 -06:00

Now removed.

Now removed.
bjwtaylor (Migrated from github.com) reviewed 2025-12-02 09:34:11 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-02 09:34:11 -06:00

Should now be updated.

Should now be updated.
bjwtaylor (Migrated from github.com) reviewed 2025-12-02 09:37:36 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-02 09:37:36 -06:00

Now updated as per @mpg's comment.

Now updated as per @mpg's comment.
bjwtaylor (Migrated from github.com) reviewed 2025-12-03 04:38:15 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-03 04:38:15 -06:00

So, @valeriosetti comment looks sensible. So I've updated the code to reflect this @mpg I'm not sure what you're after in your final comment. However let me know if you're unhappy with the original suggestion and we can change it.

So, @valeriosetti comment looks sensible. So I've updated the code to reflect this @mpg I'm not sure what you're after in your final comment. However let me know if you're unhappy with the original suggestion and we can change it.
bjwtaylor (Migrated from github.com) reviewed 2025-12-03 04:50:33 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-03 04:50:33 -06:00

@valeriosetti I've updated this line as per your comment. @mpg I'll include you point on test coverage in a separate commit in response to the test section.

@valeriosetti I've updated this line as per your comment. @mpg I'll include you point on test coverage in a separate commit in response to the test section.
bjwtaylor (Migrated from github.com) reviewed 2025-12-03 05:41:37 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-03 05:41:37 -06:00

Should now be updated.

Should now be updated.
bjwtaylor (Migrated from github.com) reviewed 2025-12-05 02:47:54 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-05 02:47:53 -06:00

So, if I use this config the tests never get run. I've had a dig around and PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC seems to be the likely candidate. So I've updated to this, lets see what the CI does and let me know what you think to this choice?

So, if I use this config the tests never get run. I've had a dig around and PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC seems to be the likely candidate. So I've updated to this, lets see what the CI does and let me know what you think to this choice?
bjwtaylor (Migrated from github.com) reviewed 2025-12-05 05:14:27 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-05 05:14:27 -06:00

@mpg copying you comment here so we can reply in a thread and it's easier to track:
On second thought, shouldn't we just ensure that ctx->bits is always set for all keys, as we do for psa_type in https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/532? I don't really like struct having fields that are sometimes set sometimes not depending on context and you have to remember when - sure it's documented, but how often do we forget to update the documentation when we change the code?

@mpg copying you comment here so we can reply in a thread and it's easier to track: On second thought, shouldn't we just ensure that ctx->bits is always set for all keys, as we do for psa_type in https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/532? I don't really like struct having fields that are sometimes set sometimes not depending on context and you have to remember when - sure it's documented, but how often do we forget to update the documentation when we change the code?
bjwtaylor (Migrated from github.com) reviewed 2025-12-05 05:21:38 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-05 05:16:49 -06:00

Not sure what you're after here? Is it a general architectural question on whether we should be checking somewhere else whether bits != 0 or are you after the above (recently added solution) or something else?

Not sure what you're after here? Is it a general architectural question on whether we should be checking somewhere else whether bits != 0 or are you after the above (recently added solution) or something else?
bjwtaylor (Migrated from github.com) commented 2025-12-05 05:17:59 -06:00

@mpg again adding your comments as a thread to collate them better:
In addition to the line comments I left, I'm not convinced by the test plan. There are currently 5 ways to populate a PK context:

  1. mbedtls_pk_wrap_psa()
  2. mbedtls_pk_copy_from_psa()
  3. mbedtls_pk_copy_public_from_psa()
  4. mbedtls_pk_parse_key()
  5. mbedtls_pk_parse_public_key()

We want each of them to be appropriately tested (both keypairs and public keys (except for (1)), both RSA and ECC, each with 2 different key sizes).

I'm satisfied that the first case is well covered by the improvements you made to pk_psa_utils().

With minor adaptations (see line comment) this function can probably also cover (2) and (3). On second thought, I'm not sure that would be right: pk_psa_utils() is currently focused on wrapping, so adding unrelated stuff just muddies the water. Perhaps having a dedicated testing function would be cleaner. Or re-use pk_copy_from_psa_success()?

However I'm more worried about (4) and (5). Superficially, it looks like it should be the job of the pk_utils() test function, but it calls pk_setup() which constructs the context manually. (Also, it only does keypairs, not public keys.)

So, unless I missed something, (4) and (5) are not covered at all. I'm not sure what's the simplest way to test them at this point. One way would be to add a bitlen parameter to the existing parsing functions in test_suite_pkparse, but that would probably mean a lot of lines changed. Or we could just add a dedicated function in test_suite_pk that takes as input a serialized private or public key (with a parameter indicating which it is), and its expected bitlen, then calls the appropriate parse function, then get_bitlen() and we're done.

Wdyt?

@mpg again adding your comments as a thread to collate them better: In addition to the line comments I left, I'm not convinced by the test plan. There are currently 5 ways to populate a PK context: 1. mbedtls_pk_wrap_psa() 2. mbedtls_pk_copy_from_psa() 3. mbedtls_pk_copy_public_from_psa() 4. mbedtls_pk_parse_key() 5. mbedtls_pk_parse_public_key() We want each of them to be appropriately tested (both keypairs and public keys (except for (1)), both RSA and ECC, each with 2 different key sizes). I'm satisfied that the first case is well covered by the improvements you made to pk_psa_utils(). With minor adaptations (see line comment) this function can probably also cover (2) and (3). On second thought, I'm not sure that would be right: pk_psa_utils() is currently focused on wrapping, so adding unrelated stuff just muddies the water. Perhaps having a dedicated testing function would be cleaner. Or re-use pk_copy_from_psa_success()? However I'm more worried about (4) and (5). Superficially, it looks like it should be the job of the pk_utils() test function, but it calls pk_setup() which constructs the context manually. (Also, it only does keypairs, not public keys.) So, unless I missed something, (4) and (5) are not covered at all. I'm not sure what's the simplest way to test them at this point. One way would be to add a bitlen parameter to the existing parsing functions in test_suite_pkparse, but that would probably mean a lot of lines changed. Or we could just add a dedicated function in test_suite_pk that takes as input a serialized private or public key (with a parameter indicating which it is), and its expected bitlen, then calls the appropriate parse function, then get_bitlen() and we're done. Wdyt?
bjwtaylor (Migrated from github.com) commented 2025-12-05 05:18:40 -06:00

@valeriosetti I think your comment is related to the above, so adding it to the same thread:
One way would be to add a bitlen parameter to the existing parsing functions in test_suite_pkparse, but that would probably mean a lot of lines changed. Or we could just add a dedicated function in test_suite_pk that takes as input a serialized private or public key (with a parameter indicating which it is), and its expected bitlen, then calls the appropriate parse function, then get_bitlen() and we're done.

Adding my personal opinion here. Unless changes to already existing test functions is trivial/simple I prefer to create new test functions for specific purposes. Extending what's already there makes test functions hard to read: lot of input parameters, lot of operations carried out internally... IMO this is not easy to review and maintain.

The worst consequence that I can think of in adding new test functions is that we duplicate some test that's already there. This is not bad though: it's true that testing will take slightly longer, but that's better than missing test coverage or make something hard to read.

@valeriosetti I think your comment is related to the above, so adding it to the same thread: One way would be to add a bitlen parameter to the existing parsing functions in test_suite_pkparse, but that would probably mean a lot of lines changed. Or we could just add a dedicated function in test_suite_pk that takes as input a serialized private or public key (with a parameter indicating which it is), and its expected bitlen, then calls the appropriate parse function, then get_bitlen() and we're done. Adding my personal opinion here. Unless changes to already existing test functions is trivial/simple I prefer to create new test functions for specific purposes. Extending what's already there makes test functions hard to read: lot of input parameters, lot of operations carried out internally... IMO this is not easy to review and maintain. The worst consequence that I can think of in adding new test functions is that we duplicate some test that's already there. This is not bad though: it's true that testing will take slightly longer, but that's better than missing test coverage or make something hard to read.
mpg (Migrated from github.com) reviewed 2025-12-05 05:44:25 -06:00
mpg (Migrated from github.com) commented 2025-12-05 05:44:24 -06:00

Thanks for creating a thread :) Yes, that's a general architectural question: once a pk_context has been populated (by one of the 5 methods mentioned above), is ctx->bits guaranteed to be valid, ie hold the actual bit length of the key?

I think as a general principle, it would be nice for the answer to this question to be "yes", rather than "well, depends which function was used to populate your context - if it was X or Y then yes, otherwise you'll have to do Z instead to get the bitlen". That's a general thought about fields in pk_context.

However, for this particular field, since we have a getter function which can be used everywhere (instead of accessing the field), I think it's not that important. So, on second thought, not requesting any change here - especially as this PR is already large enough as it is :)

Thanks for creating a thread :) Yes, that's a general architectural question: once a `pk_context` has been populated (by one of the 5 methods mentioned above), is `ctx->bits` guaranteed to be valid, ie hold the actual bit length of the key? I think as a general principle, it would be nice for the answer to this question to be "yes", rather than "well, depends which function was used to populate your context - if it was X or Y then yes, otherwise you'll have to do Z instead to get the bitlen". That's a general thought about fields in `pk_context`. However, for this particular field, since we have a getter function which can be used everywhere (instead of accessing the field), I think it's not that important. So, on second thought, not requesting any change here - especially as this PR is already large enough as it is :)
bjwtaylor (Migrated from github.com) reviewed 2025-12-05 05:53:08 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-05 05:53:08 -06:00

@mpg gave this the thumb up, so I'll add a function that takes a key(s) and calls the function and getbitlen. Let me know. Hopefully this should complete the required test plan, though let me know if anyone thinks we need anything else.

@mpg gave this the thumb up, so I'll add a function that takes a key(s) and calls the function and getbitlen. Let me know. Hopefully this should complete the required test plan, though let me know if anyone thinks we need anything else.
mpg (Migrated from github.com) reviewed 2025-12-05 05:55:03 -06:00
mpg (Migrated from github.com) commented 2025-12-05 05:55:03 -06:00

As discussed, we want to reach a balance between to valid goals: (a) not making tests more difficult to read than they already are, and (b) not stalling PRs looking for perfection.

IMO, here a good solution could be:

  • Add testing of mbedtls_pk_copy_from_psa() and mbedtls_pk_copy_public_from_psa() to the existing function that tests mbedtls_pk_wrap_psa() - IMO this is a natural grouping as those are the 3 ways to populate a PK context from a PSA key. This is the approach used in this PR so far, that we can keep and just slightly expand to test both copy and copy_public.
  • OTOH, for keys creating from parsing, don't extend the existing test functions for parsing with a new bitlen parameter (as I previously suggested, but Valerio pointed out this approach is bad in the long run), but instead create a new dedicated testing function "parse and check bitlen". We can probably use the same function for both parse_key and parse_public_key with a parameter indicating which one it is.

Note that if this works this is probably a general pattern for testing something with a PK context:

  • one function for the 3 cases of "populated from a PSA key"
  • another function for the 2 cases of "populated by parsing"

Wdyt @bjwtaylor @valeriosetti ?

As discussed, we want to reach a balance between to valid goals: (a) not making tests more difficult to read than they already are, and (b) not stalling PRs looking for perfection. IMO, here a good solution could be: - Add testing of `mbedtls_pk_copy_from_psa()` and `mbedtls_pk_copy_public_from_psa()` to the existing function that tests `mbedtls_pk_wrap_psa()` - IMO this is a natural grouping as those are the 3 ways to populate a PK context from a PSA key. This is the approach used in this PR so far, that we can keep and just slightly expand to test both `copy` and `copy_public`. - OTOH, for keys creating from parsing, don't extend the existing test functions for parsing with a new bitlen parameter (as I previously suggested, but Valerio pointed out this approach is bad in the long run), but instead create a new dedicated testing function "parse and check bitlen". We can probably use the same function for both `parse_key` and `parse_public_key` with a parameter indicating which one it is. Note that if this works this is probably a general pattern for testing something with a PK context: - one function for the 3 cases of "populated from a PSA key" - another function for the 2 cases of "populated by parsing" Wdyt @bjwtaylor @valeriosetti ?
mpg (Migrated from github.com) reviewed 2025-12-05 05:58:51 -06:00
mpg (Migrated from github.com) commented 2025-12-05 05:58:51 -06:00

Note: in principle, it is desirable for tests that parse key to parse them from a string in the .data file, as opposed to a file in the filesystem, in order to maintain coverage in configurations without filesystem support (MBEDTLS_FS_IO disabled).

However, in the spirit of balance between quality and speed, I'm happy for the parsing tests in this PR (and others in the same series) to use files as inputs, as I think that's both easier to write and to review (not having to document and check long strings of hex).

Note: in principle, it is desirable for tests that parse key to parse them from a string in the .data file, as opposed to a file in the filesystem, in order to maintain coverage in configurations without filesystem support (`MBEDTLS_FS_IO` disabled). However, in the spirit of balance between quality and speed, I'm happy for the parsing tests in this PR (and others in the same series) to use files as inputs, as I think that's both easier to write and to review (not having to document and check long strings of hex).
bjwtaylor (Migrated from github.com) reviewed 2025-12-05 07:37:31 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-05 07:37:31 -06:00

I think both approaches have their pro's and con's, so I don't have a strong preference for either. So if @mpg and @valeriosetti you can agree on one or the other I'm happy with either.

I think both approaches have their pro's and con's, so I don't have a strong preference for either. So if @mpg and @valeriosetti you can agree on one or the other I'm happy with either.
bjwtaylor (Migrated from github.com) reviewed 2025-12-05 07:54:55 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-05 07:54:55 -06:00

Also I've rocket emoji'd the original threads from this, as the discussion has been now moved here.

Also I've rocket emoji'd the original threads from this, as the discussion has been now moved here.
davidhorstmann-arm commented 2025-12-10 09:00:48 -06:00 (Migrated from github.com)

I'm sure there is some rework still to go, but at present this PR saves 36B of code size on M55 🙂

Module Base PR Delta % Delta
pk 3963 4015 +52 +1.31%
pk_wrap 2090 2002 -88 -4.21%
TOTAL 206690 206654 -36 -0.02%
I'm sure there is some rework still to go, but at present this PR saves 36B of code size on M55 :slightly_smiling_face: | Module | Base | PR | Delta | % Delta | | --- | --- | --- | --- | --- | | pk | 3963 | 4015 | +52 | +1.31% | | pk_wrap | 2090 | 2002 | -88 | -4.21% | | TOTAL | 206690 | 206654 | -36 | -0.02% |
bjwtaylor commented 2025-12-18 08:33:02 -06:00 (Migrated from github.com)

So, I believe I've now implemented the requested test plan as per the below verification matrix

<html>
Test Case Generating Function Test Case Test function Test case Line
TC1 non-wrapping -> mbedtls_pk_parse_key positive: mbedtls_pk_get_bitlen() for EC curve#1 test_bitlen_setup Test bitlen of 256 bit ECC keys 991
TC2 non-wrapping -> mbedtls_pk_parse_key positive: mbedtls_pk_get_bitlen() for EC curve#2 test_bitlen_setup Test bitlen of 521 bit ECC keys 991
TC3 non-wrapping -> mbedtls_pk_parse_key positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 test_bitlen_setup Test bitlen of 2048 bit RSA keys 991
TC4 non-wrapping -> mbedtls_pk_parse_key positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 test_bitlen_setup Test bitlen of 1024 bit RSA keys 991
TC6 non-wrapping -> mbedtls_pk_copy_from_psa positive: mbedtls_pk_get_bitlen() for EC curve#1 test_bitlen_setup Test bitlen of 256 bit ECC keys 999
TC7 non-wrapping -> mbedtls_pk_copy_from_psa positive: mbedtls_pk_get_bitlen() for EC curve#2 test_bitlen_setup Test bitlen of 521 bit ECC keys 999
TC8 non-wrapping -> mbedtls_pk_copy_from_psa positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 test_bitlen_setup Test bitlen of 2048 bit RSA keys 999
TC9 non-wrapping -> mbedtls_pk_copy_from_psa positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 test_bitlen_setup Test bitlen of 1024 bit RSA keys 999
TC10 non-wrapping -> mbedtls_pk_copy_public_from_psa positive: mbedtls_pk_get_bitlen() for EC curve#1 test_bitlen_setup Test bitlen of 256 bit ECC keys 1001
TC11 non-wrapping -> mbedtls_pk_copy_public_from_psa positive: mbedtls_pk_get_bitlen() for EC curve#2 test_bitlen_setup Test bitlen of 521 bit ECC keys 1001
TC12 non-wrapping -> mbedtls_pk_copy_public_from_psa positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 test_bitlen_setup Test bitlen of 2048 bit RSA keys 1001
TC13 non-wrapping -> mbedtls_pk_copy_public_from_psa positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 test_bitlen_setup Test bitlen of 1024 bit RSA keys 1001
TC14 non-wrapping -> mbedtls_pk_parse_public_key positive: mbedtls_pk_get_bitlen() for EC curve#1 test_bitlen_setup Test bitlen of 256 bit ECC keys 994
TC15 non-wrapping -> mbedtls_pk_parse_public_key positive: mbedtls_pk_get_bitlen() for EC curve#2 test_bitlen_setup Test bitlen of 521 bit ECC keys 994
TC16 non-wrapping -> mbedtls_pk_parse_public_key positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 test_bitlen_setup Test bitlen of 2048 bit RSA keys 994
TC17 non-wrapping -> mbedtls_pk_parse_public_key positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 test_bitlen_setup Test bitlen of 1024 bit RSA keys 994
TC18 wrapping -> mbedtls_pk_wrap_psa positive: mbedtls_pk_get_bitlen() for EC curve#1 test_bitlen_setup Test bitlen of 256 bit ECC keys 1009
TC19 wrapping -> mbedtls_pk_wrap_psa positive: mbedtls_pk_get_bitlen() for EC curve#2 test_bitlen_setup Test bitlen of 521 bit ECC keys 1009
TC20 wrapping -> mbedtls_pk_wrap_psa positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 test_bitlen_setup Test bitlen of 2048 bit RSA keys 1009
TC21 wrapping -> mbedtls_pk_wrap_psa positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 test_bitlen_setup Test bitlen of 1024 bit RSA keys 1009
TC22 wrapping -> mbedtls_pk_wrap_psa negative: verify that a NULL PK context pointer returns 0 length valid_parameters   913
</html>

Let me know what people think.

So, I believe I've now implemented the requested test plan as per the below verification matrix <!DOCTYPE html> <html> <body> Test Case | Generating Function | Test Case | Test function | Test case | Line -- | -- | -- | -- | -- | -- TC1 | non-wrapping -> mbedtls_pk_parse_key | positive: mbedtls_pk_get_bitlen() for EC curve#1 | test_bitlen_setup | Test bitlen of 256 bit ECC keys | [991](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L991) TC2 | non-wrapping -> mbedtls_pk_parse_key | positive: mbedtls_pk_get_bitlen() for EC curve#2 | test_bitlen_setup | Test bitlen of 521 bit ECC keys | [991](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L991) TC3 | non-wrapping -> mbedtls_pk_parse_key | positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 | test_bitlen_setup | Test bitlen of 2048 bit RSA keys | [991](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L991) TC4 | non-wrapping -> mbedtls_pk_parse_key | positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 | test_bitlen_setup | Test bitlen of 1024 bit RSA keys | [991](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L991) TC6 | non-wrapping -> mbedtls_pk_copy_from_psa | positive: mbedtls_pk_get_bitlen() for EC curve#1 | test_bitlen_setup | Test bitlen of 256 bit ECC keys | [999](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/34573e843f140d691fc39ff297b7729f27d3b4d6/tests/suites/test_suite_pk.function#L999) TC7 | non-wrapping -> mbedtls_pk_copy_from_psa | positive: mbedtls_pk_get_bitlen() for EC curve#2 | test_bitlen_setup | Test bitlen of 521 bit ECC keys | [999](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L999) TC8 | non-wrapping -> mbedtls_pk_copy_from_psa | positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 | test_bitlen_setup | Test bitlen of 2048 bit RSA keys | [999](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L999) TC9 | non-wrapping -> mbedtls_pk_copy_from_psa | positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 | test_bitlen_setup | Test bitlen of 1024 bit RSA keys | [999](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L999) TC10 | non-wrapping -> mbedtls_pk_copy_public_from_psa | positive: mbedtls_pk_get_bitlen() for EC curve#1 | test_bitlen_setup | Test bitlen of 256 bit ECC keys | [1001](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L1001) TC11 | non-wrapping -> mbedtls_pk_copy_public_from_psa | positive: mbedtls_pk_get_bitlen() for EC curve#2 | test_bitlen_setup | Test bitlen of 521 bit ECC keys | [1001](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L1001) TC12 | non-wrapping -> mbedtls_pk_copy_public_from_psa | positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 | test_bitlen_setup | Test bitlen of 2048 bit RSA keys | [1001](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L1001) TC13 | non-wrapping -> mbedtls_pk_copy_public_from_psa | positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 | test_bitlen_setup | Test bitlen of 1024 bit RSA keys | [1001](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L1001) TC14 | non-wrapping -> mbedtls_pk_parse_public_key | positive: mbedtls_pk_get_bitlen() for EC curve#1 | test_bitlen_setup | Test bitlen of 256 bit ECC keys | [994](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L994) TC15 | non-wrapping -> mbedtls_pk_parse_public_key | positive: mbedtls_pk_get_bitlen() for EC curve#2 | test_bitlen_setup | Test bitlen of 521 bit ECC keys | [994](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L994) TC16 | non-wrapping -> mbedtls_pk_parse_public_key | positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 | test_bitlen_setup | Test bitlen of 2048 bit RSA keys | [994](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L994) TC17 | non-wrapping -> mbedtls_pk_parse_public_key | positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 | test_bitlen_setup | Test bitlen of 1024 bit RSA keys | [994](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L994) TC18 | wrapping -> mbedtls_pk_wrap_psa | positive: mbedtls_pk_get_bitlen() for EC curve#1 | test_bitlen_setup | Test bitlen of 256 bit ECC keys | [1009](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L1009) TC19 | wrapping -> mbedtls_pk_wrap_psa | positive: mbedtls_pk_get_bitlen() for EC curve#2 | test_bitlen_setup | Test bitlen of 521 bit ECC keys | [1009](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L1009) TC20 | wrapping -> mbedtls_pk_wrap_psa | positive: mbedtls_pk_get_bitlen() for RSA bit-size#1 | test_bitlen_setup | Test bitlen of 2048 bit RSA keys | [1009](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L1009) TC21 | wrapping -> mbedtls_pk_wrap_psa | positive: mbedtls_pk_get_bitlen() for RSA bit-size#2 | test_bitlen_setup | Test bitlen of 1024 bit RSA keys | [1009](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/c73ef17c121cfdd3784c4e421dab157058ecd97f/tests/suites/test_suite_pk.function#L1009) TC22 | wrapping -> mbedtls_pk_wrap_psa | negative: verify that a NULL PK context pointer returns 0 length | valid_parameters |   | [913](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/34573e843f140d691fc39ff297b7729f27d3b4d6/tests/suites/test_suite_pk.function#L913) </body> </html> Let me know what people think.
valeriosetti (Migrated from github.com) requested changes 2025-12-19 08:26:12 -06:00
valeriosetti (Migrated from github.com) commented 2025-12-10 03:45:12 -06:00

Oops, I was about to ask for the very same change that @mpg suggested here when I saw it was already discussed. To me it would make sense as it aligns with what is being done in https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/577#discussion_r2597951282.
Since we're going to test mbedtls_pk_get_bitlen() with all the possible ways of generating a PK context nowadays, we should be granted that we're not forgetting anything.

I'm deeply sorry to ask for this change since I was the one who proposed the patch for it initially, but the comment I linked above made me realize that this is indeed the right path to follow.
However if we think that this PR is becoming too large, we can address this with a small follow-up PR specifically targeted for this change.

Oops, I was about to ask for the very same change that @mpg suggested here when I saw it was already discussed. To me it would make sense as it aligns with what is being done in https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/577#discussion_r2597951282. Since we're going to test `mbedtls_pk_get_bitlen()` with all the possible ways of generating a PK context nowadays, we should be granted that we're not forgetting anything. I'm deeply sorry to ask for this change since I was the one who proposed the patch for it initially, but the comment I linked above made me realize that this is indeed the right path to follow. However if we think that this PR is becoming too large, we can address this with a small follow-up PR specifically targeted for this change.
valeriosetti (Migrated from github.com) reviewed 2025-12-19 08:29:52 -06:00
valeriosetti (Migrated from github.com) commented 2025-12-19 08:29:52 -06:00

I'm super sorry, I just realized that I had this unsent message that was pending. I don´t know why it wasn't sent.
I think it's mostly directed to @mpg because the goal is to find a "shared design pattern" in PK that should be used in all the function.

Anyway in case @mpg agrees with my point, the following patch should be enough to make tests_suite_pk testing happy again:

diff --git a/drivers/builtin/src/pk.c b/drivers/builtin/src/pk.c
index 2f1dfa893..13eb7adab 100644
--- a/drivers/builtin/src/pk.c
+++ b/drivers/builtin/src/pk.c
@@ -163,6 +163,7 @@ int mbedtls_pk_wrap_psa(mbedtls_pk_context *ctx,
     const mbedtls_pk_info_t *info = NULL;
     psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
     psa_key_type_t type;
+    psa_key_bits_t bits;
     int ret;
 
     if (ctx == NULL || ctx->pk_info != NULL) {
@@ -173,6 +174,7 @@ int mbedtls_pk_wrap_psa(mbedtls_pk_context *ctx,
         return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
     }
     type = psa_get_key_type(&attributes);
+    bits = psa_get_key_bits(&attributes);
     psa_reset_key_attributes(&attributes);
 
 #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
@@ -196,6 +198,7 @@ int mbedtls_pk_wrap_psa(mbedtls_pk_context *ctx,
 
     ctx->pk_info = info;
     ctx->psa_type = type;
+    ctx->bits = bits;
 
     return 0;
 }
@@ -1360,24 +1363,13 @@ int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
  */
 size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
 {
-    size_t bits;
-    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
-
     /* For backward compatibility, accept NULL or a context that
      * isn't set up yet, and return a fake value that should be safe. */
     if (ctx == NULL || ctx->pk_info == NULL) {
         return 0;
     }
-    if (ctx->bits != 0) {
-        return ctx->bits;
-    } else {
-        if (PSA_SUCCESS != psa_get_key_attributes(ctx->priv_id, &attributes)) {
-            return 0;
-        }
-        bits = psa_get_key_bits(&attributes);
-        psa_reset_key_attributes(&attributes);
-        return bits;
-    }
+
+    return ctx->bits;
 }
 
 /*

But please wait for @mpg feedback before trying to integrate this. Meanwhile I will take a look at tests

I'm super sorry, I just realized that I had this unsent message that was pending. I don´t know why it wasn't sent. I think it's mostly directed to @mpg because the goal is to find a "shared design pattern" in PK that should be used in all the function. Anyway in case @mpg agrees with my point, the following patch should be enough to make `tests_suite_pk` testing happy again: ``` diff --git a/drivers/builtin/src/pk.c b/drivers/builtin/src/pk.c index 2f1dfa893..13eb7adab 100644 --- a/drivers/builtin/src/pk.c +++ b/drivers/builtin/src/pk.c @@ -163,6 +163,7 @@ int mbedtls_pk_wrap_psa(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info = NULL; psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; psa_key_type_t type; + psa_key_bits_t bits; int ret; if (ctx == NULL || ctx->pk_info != NULL) { @@ -173,6 +174,7 @@ int mbedtls_pk_wrap_psa(mbedtls_pk_context *ctx, return MBEDTLS_ERR_PK_BAD_INPUT_DATA; } type = psa_get_key_type(&attributes); + bits = psa_get_key_bits(&attributes); psa_reset_key_attributes(&attributes); #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) @@ -196,6 +198,7 @@ int mbedtls_pk_wrap_psa(mbedtls_pk_context *ctx, ctx->pk_info = info; ctx->psa_type = type; + ctx->bits = bits; return 0; } @@ -1360,24 +1363,13 @@ int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, */ size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx) { - size_t bits; - psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; - /* For backward compatibility, accept NULL or a context that * isn't set up yet, and return a fake value that should be safe. */ if (ctx == NULL || ctx->pk_info == NULL) { return 0; } - if (ctx->bits != 0) { - return ctx->bits; - } else { - if (PSA_SUCCESS != psa_get_key_attributes(ctx->priv_id, &attributes)) { - return 0; - } - bits = psa_get_key_bits(&attributes); - psa_reset_key_attributes(&attributes); - return bits; - } + + return ctx->bits; } /* ``` But please wait for @mpg feedback before trying to integrate this. Meanwhile I will take a look at tests
valeriosetti (Migrated from github.com) requested changes 2025-12-19 11:01:49 -06:00
valeriosetti (Migrated from github.com) left a comment

The newly introduced test function is pretty compact, but it covers most of the cases. The only missing one is the PK context generated with mbedtls_pk_wrap_psa for which you linked tests to be here but it seems to me that that function is testing check_pair and not get_bitlen. Am I missing something?

The newly introduced test function is pretty compact, but it covers most of the cases. The only missing one is the PK context generated with `mbedtls_pk_wrap_psa` for which you linked tests to be [here](https://github.com/bjwtaylor/TF-PSA-Crypto/blob/34573e843f140d691fc39ff297b7729f27d3b4d6/tests/suites/test_suite_pk.function#L1093) but it seems to me that that function is testing `check_pair` and not `get_bitlen`. Am I missing something?
@ -8,6 +8,22 @@ PK write valid parameters
depends_on:PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY
valeriosetti (Migrated from github.com) commented 2025-12-19 10:47:38 -06:00

Since we're not going to perform any operation why do we need to ask for ECDSA? Isn't this enough:

depends_on:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT

The same holds also for the other EC test below

Since we're not going to perform any operation why do we need to ask for ECDSA? Isn't this enough: ```suggestion depends_on:PSA_WANT_ECC_SECP_R1_256:PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT ``` The same holds also for the other EC test below
valeriosetti (Migrated from github.com) commented 2025-12-19 10:48:54 -06:00

Since we're importing a key pair I think that

depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT:MBEDTLS_PKCS5_C

would be better. Wdyt?
The same holds also for the other RSA test below

Since we're importing a key pair I think that ```suggestion depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT:MBEDTLS_PKCS5_C ``` would be better. Wdyt? The same holds also for the other RSA test below
@ -964,6 +965,61 @@ void valid_parameters()
}
valeriosetti (Migrated from github.com) commented 2025-12-19 08:57:28 -06:00

Invert the order:

    mbedtls_pk_init(&pk);
    mbedtls_pk_init(&pk_pub);
    mbedtls_pk_init(&new_pk);
    
    PSA_INIT();

The reason is that PSA_INIT can fail and if this happen you are going to free resources with mbedtls_pk_free that were never initialized.

Invert the order: ```suggestion mbedtls_pk_init(&pk); mbedtls_pk_init(&pk_pub); mbedtls_pk_init(&new_pk); PSA_INIT(); ``` The reason is that `PSA_INIT` can fail and if this happen you are going to free resources with `mbedtls_pk_free` that were never initialized.
valeriosetti (Migrated from github.com) commented 2025-12-19 09:01:48 -06:00

Wait, it seems to me that here you are initializing pk twice in a row (pk_setup + mbedtls_pk_parse_key). I'm a bit surprised that this actually works since the 2nd call should definitely fail because pk is already initialized. @mpg what am I missing here?

Wait, it seems to me that here you are initializing `pk` twice in a row (`pk_setup` + `mbedtls_pk_parse_key`). I'm a bit surprised that this actually works since the 2nd call should definitely fail because `pk` is already initialized. @mpg what am I missing here?
valeriosetti (Migrated from github.com) commented 2025-12-19 09:11:10 -06:00

I think this should be

    TEST_ASSERT(mbedtls_pk_get_bitlen(&pk_pub) == bitlen);

because otherwise it's the same as in line 992

I think this should be ```suggestion TEST_ASSERT(mbedtls_pk_get_bitlen(&pk_pub) == bitlen); ``` because otherwise it's the same as in line 992
valeriosetti (Migrated from github.com) commented 2025-12-19 09:45:24 -06:00

You don't need pub_key_id (i.e. a PSA public key) to call mbedtls_pk_copy_public_from_psa. You can use pk.priv_id directly and it will work the same. I think you can just remove pub_key_id from this function.

You don't need `pub_key_id` (i.e. a PSA public key) to call `mbedtls_pk_copy_public_from_psa`. You can use `pk.priv_id` directly and it will work the same. I think you can just remove `pub_key_id` from this function.
@ -967,0 +1005,4 @@
TEST_EQUAL(mbedtls_pk_get_psa_attributes(&pk, PSA_KEY_USAGE_SIGN_HASH,
&wrapped_key_attr), 0);
TEST_EQUAL(mbedtls_pk_import_into_psa(&pk, &wrapped_key_attr, &wrapped_key_id), 0);
valeriosetti (Migrated from github.com) commented 2025-12-19 09:14:43 -06:00

Wait, I think here you re-initializing pk_pub (previous initialization is on line 994 with mbedtls_pk_parse_public_key) without mbedtls_pk_free in between. This is fine for now because init is simply going to clear the PK structure and we're not leaking open keys in PSA, but if in the future we want to have PSA key IDs for public keys in PK in the same way we're doing with key pairs then this code would become problematic I think.

Wait, I think here you re-initializing `pk_pub` (previous initialization is on line 994 with `mbedtls_pk_parse_public_key`) without `mbedtls_pk_free` in between. This is fine for now because `init` is simply going to clear the PK structure and we're not leaking open keys in PSA, but if in the future we want to have PSA key IDs for public keys in PK in the same way we're doing with key pairs then this code would become problematic I think.
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 01:57:54 -06:00
bjwtaylor (Migrated from github.com) commented 2025-12-22 01:57:54 -06:00

This is a relatively small change, so if we think we need it we can add it to this PR. Not sure if this is what @mpg is also after, though if he agrees this solves the issue we can add this.

This is a relatively small change, so if we think we need it we can add it to this PR. Not sure if this is what @mpg is also after, though if he agrees this solves the issue we can add this.
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 02:02:57 -06:00
@ -964,6 +965,61 @@ void valid_parameters()
}
bjwtaylor (Migrated from github.com) commented 2025-12-22 02:02:57 -06:00

Good point, should now be corrected.

Good point, should now be corrected.
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 02:11:51 -06:00
@ -964,6 +965,61 @@ void valid_parameters()
}
bjwtaylor (Migrated from github.com) commented 2025-12-22 02:11:51 -06:00

As for why it works, that's one for @mpg. I have however removed the setup though, so at least the duplication isn't present any more and we can hopefully close this one?

As for why it works, that's one for @mpg. I have however removed the setup though, so at least the duplication isn't present any more and we can hopefully close this one?
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 02:15:57 -06:00
@ -964,6 +965,61 @@ void valid_parameters()
}
bjwtaylor (Migrated from github.com) commented 2025-12-22 02:15:57 -06:00

Yes, good spot this is a copy paste error. Should now be corrected.

Yes, good spot this is a copy paste error. Should now be corrected.
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 02:21:39 -06:00
@ -967,0 +1005,4 @@
TEST_EQUAL(mbedtls_pk_get_psa_attributes(&pk, PSA_KEY_USAGE_SIGN_HASH,
&wrapped_key_attr), 0);
TEST_EQUAL(mbedtls_pk_import_into_psa(&pk, &wrapped_key_attr, &wrapped_key_id), 0);
bjwtaylor (Migrated from github.com) commented 2025-12-22 02:21:39 -06:00

So I have added the additional free, as I agree this should be required. I guess we also have a general architectural question for @mpg about whether this use of these functions should currently or in the future behave differently?

So I have added the additional free, as I agree this should be required. I guess we also have a general architectural question for @mpg about whether this use of these functions should currently or in the future behave differently?
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 02:27:44 -06:00
@ -964,6 +965,61 @@ void valid_parameters()
}
bjwtaylor (Migrated from github.com) commented 2025-12-22 02:27:44 -06:00

Thanks for pointing this out, I assumed you would need a separate ID to store the public key. Though I've removed it and everything still works, so I think the change should work.

Thanks for pointing this out, I assumed you would need a separate ID to store the public key. Though I've removed it and everything still works, so I think the change should work.
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 02:34:04 -06:00
@ -8,6 +8,22 @@ PK write valid parameters
depends_on:PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY
bjwtaylor (Migrated from github.com) commented 2025-12-22 02:34:03 -06:00

Should now be updated.

Should now be updated.
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 02:37:51 -06:00
@ -8,6 +8,22 @@ PK write valid parameters
depends_on:PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY
bjwtaylor (Migrated from github.com) commented 2025-12-22 02:37:51 -06:00

Agree, should now be updated.

Agree, should now be updated.
bjwtaylor (Migrated from github.com) reviewed 2025-12-22 03:16:58 -06:00
@ -967,0 +1016,4 @@
mbedtls_pk_free(&pk);
mbedtls_pk_free(&pk_pub);
mbedtls_pk_free(&new_pk);
PSA_DONE();
bjwtaylor (Migrated from github.com) commented 2025-12-22 03:16:50 -06:00

@valeriosetti, good spot on the wrap. I'm not sure how it got missed, probably in the pre-christmas rush to get something out before everyone went on holiday. I've added the test here now though. I'll close the previous comment and update the verification matrix. Then lets see what the CI thinks on this change.

@valeriosetti, good spot on the wrap. I'm not sure how it got missed, probably in the pre-christmas rush to get something out before everyone went on holiday. I've added the test here now though. I'll close the previous comment and update the verification matrix. Then lets see what the CI thinks on this change.
bjwtaylor (Migrated from github.com) reviewed 2025-12-23 09:24:44 -06:00
@ -967,0 +1016,4 @@
mbedtls_pk_free(&pk);
mbedtls_pk_free(&pk_pub);
mbedtls_pk_free(&new_pk);
PSA_DONE();
bjwtaylor (Migrated from github.com) commented 2025-12-23 09:24:44 -06:00

Looks like the CI is happy, so hopefully this is good to go.

Looks like the CI is happy, so hopefully this is good to go.
valeriosetti (Migrated from github.com) requested changes 2026-01-08 07:07:23 -06:00
valeriosetti (Migrated from github.com) left a comment

Mostly look good to me. I only left one possible improvement to the test function.
There is however one unresolved comment that is not addressed and I think it needs @mpg feedback.

Mostly look good to me. I only left one possible improvement to the test function. There is however one [unresolved comment](https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/575#discussion_r2635276687) that is not addressed and I think it needs @mpg feedback.
@ -967,0 +994,4 @@
TEST_ASSERT(mbedtls_pk_get_bitlen(&pk) == bitlen);
TEST_ASSERT(mbedtls_pk_parse_public_key(&pk_pub, pub_key->x, pub_key->len) == 0);
TEST_ASSERT(mbedtls_pk_get_bitlen(&pk_pub) == bitlen);
valeriosetti (Migrated from github.com) commented 2026-01-08 07:05:37 -06:00

I think you can simplify this now: pk_type and curve_or_keybits are overkilling, you just need to know the bit size to use as check below. So why not just using that as input parameter and getting rid of this block?

I think you can simplify this now: `pk_type` and `curve_or_keybits` are overkilling, you just need to know the bit size to use as check below. So why not just using that as input parameter and getting rid of this block?
mpg (Migrated from github.com) reviewed 2026-01-09 02:21:49 -06:00
mpg (Migrated from github.com) commented 2026-01-09 02:21:49 -06:00

Yes, this patch looks like exactly what I was suggesting, and I still think we should go in this direction. I wouldn't block this PR on it, but the patch looks small enough, so @bjwtaylor if you're still happy to integrate this here, that would also be my preference.

Yes, this patch looks like exactly what I was suggesting, and I still think we should go in this direction. I wouldn't block this PR on it, but the patch looks small enough, so @bjwtaylor if you're still happy to integrate this here, that would also be my preference.
bjwtaylor (Migrated from github.com) reviewed 2026-01-09 08:13:59 -06:00
@ -967,0 +994,4 @@
TEST_ASSERT(mbedtls_pk_get_bitlen(&pk) == bitlen);
TEST_ASSERT(mbedtls_pk_parse_public_key(&pk_pub, pub_key->x, pub_key->len) == 0);
TEST_ASSERT(mbedtls_pk_get_bitlen(&pk_pub) == bitlen);
bjwtaylor (Migrated from github.com) commented 2026-01-09 08:13:59 -06:00

So the aim of the current architecture is to move the intelligence of how the keybits is calculated into the function, to reduce maintenance. Yes we could add another parameter to the function, though this would then mean every time we need to add a new call in the data file you would need to add this to it. We'd also need to change each one individually if we needed to refactor anything. So I think the extra complexity is justified, though happy to change it if you feel strongly that it's not worth it. What do you think?

So the aim of the current architecture is to move the intelligence of how the keybits is calculated into the function, to reduce maintenance. Yes we could add another parameter to the function, though this would then mean every time we need to add a new call in the data file you would need to add this to it. We'd also need to change each one individually if we needed to refactor anything. So I think the extra complexity is justified, though happy to change it if you feel strongly that it's not worth it. What do you think?
valeriosetti (Migrated from github.com) reviewed 2026-01-09 08:21:07 -06:00
@ -967,0 +994,4 @@
TEST_ASSERT(mbedtls_pk_get_bitlen(&pk) == bitlen);
TEST_ASSERT(mbedtls_pk_parse_public_key(&pk_pub, pub_key->x, pub_key->len) == 0);
TEST_ASSERT(mbedtls_pk_get_bitlen(&pk_pub) == bitlen);
valeriosetti (Migrated from github.com) commented 2026-01-09 08:21:07 -06:00

Actually my proposal was to remove pk_type and curve_or_keybits and replace them with bitsize parameter, so it's not adding a new parameter, but reducing them. In any case, whenever you add a new line to the data file using this function, you need to specify either the bitsize (my solution) or curve_or_keybits (your solution), because in both cases you need to have a "known good value" for comparison.

Anyway I don´t have a strong preference, both solution work just fine IMO, so feel free to ignore this comment ;)

Actually my proposal was to remove `pk_type` and `curve_or_keybits` and replace them with `bitsize` parameter, so it's not adding a new parameter, but reducing them. In any case, whenever you add a new line to the data file using this function, you need to specify either the bitsize (my solution) or `curve_or_keybits` (your solution), because in both cases you need to have a "known good value" for comparison. Anyway I don´t have a strong preference, both solution work just fine IMO, so feel free to ignore this comment ;)
bjwtaylor (Migrated from github.com) reviewed 2026-01-09 08:29:46 -06:00
bjwtaylor (Migrated from github.com) commented 2026-01-09 08:29:46 -06:00

So, some of this was already in. Though I've also now added the bits that weren't.

So, some of this was already in. Though I've also now added the bits that weren't.
valeriosetti (Migrated from github.com) reviewed 2026-01-09 09:21:21 -06:00
valeriosetti (Migrated from github.com) commented 2026-01-09 09:21:21 -06:00

I'm currently checking the latest version of this PR, but I don't see the changes to mbedtls_pk_wrap_psa that I made in my patch above. Weren't they necessary to pass tests?

I'm currently checking the latest version of this PR, but I don't see the changes to `mbedtls_pk_wrap_psa` that I made in my patch above. Weren't they necessary to pass tests?
bjwtaylor (Migrated from github.com) reviewed 2026-01-09 09:27:47 -06:00
bjwtaylor (Migrated from github.com) commented 2026-01-09 09:27:47 -06:00

Apologies should now be there.

Apologies should now be there.
valeriosetti (Migrated from github.com) reviewed 2026-01-09 09:28:40 -06:00
valeriosetti (Migrated from github.com) left a comment
No description provided.
LGTM. Thanks!
valeriosetti (Migrated from github.com) reviewed 2026-01-12 02:19:23 -06:00
bjwtaylor (Migrated from github.com) reviewed 2026-01-12 04:25:27 -06:00
bjwtaylor (Migrated from github.com) commented 2026-01-12 04:25:27 -06:00

So, I think the bits variable of the patch needs to be of type size_t to prevent a failure in the windows compiler. I've pushed the change let's see what the CI thinks and then we can decide what we think to this change.

So, I think the bits variable of the patch needs to be of type size_t to prevent a failure in the windows compiler. I've pushed the change let's see what the CI thinks and then we can decide what we think to this change.
valeriosetti (Migrated from github.com) reviewed 2026-01-12 05:01:26 -06:00
valeriosetti (Migrated from github.com) commented 2026-01-12 05:01:26 -06:00

Given the prototype of psa_get_key_bits is:
static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
I think your change is fine. Sorry for the mistake in the patch.

Given the prototype of `psa_get_key_bits` is: `static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);` I think your change is fine. Sorry for the mistake in the patch.
valeriosetti (Migrated from github.com) reviewed 2026-01-12 05:02:14 -06:00
valeriosetti (Migrated from github.com) left a comment
No description provided.
The last commit [looks fine](https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/575#discussion_r2681776743), so I'm re-approving
mpg (Migrated from github.com) requested changes 2026-01-13 02:55:55 -06:00
mpg (Migrated from github.com) left a comment

Looks pretty good to me, just two nits in the test function.

I like how tests are structured. In the data file it's easy to see which key types & sizes are tested, and in the test function it's easy to see we're testing all 5 methods of populating a context. This makes the (positive part of the) test matrix both complete and, perhaps more importantly easy to understand and extend in the future (eg when we add ML-DSA support). Thank you for that.

Just one point though: is there any place where we're testing that mbedtls_pk_get_bitlen(&pk) on a context that was just initialised (pk_init()) but never populated successfully returns 0? I couldn't find it. If there is non, I suggest adding one in valid_parameters() (just call pk_get_bitlen right after the call to pk_init()).

Looks pretty good to me, just two nits in the test function. I like how tests are structured. In the data file it's easy to see which key types & sizes are tested, and in the test function it's easy to see we're testing all 5 methods of populating a context. This makes the (positive part of the) test matrix both complete and, perhaps more importantly easy to understand and extend in the future (eg when we add ML-DSA support). Thank you for that. Just one point though: is there any place where we're testing that `mbedtls_pk_get_bitlen(&pk)` on a context that was just initialised (`pk_init()`) but never populated successfully returns 0? I couldn't find it. If there is non, I suggest adding one in `valid_parameters()` (just call `pk_get_bitlen` right after the call to `pk_init()`).
@ -964,6 +965,61 @@ void valid_parameters()
}
mpg (Migrated from github.com) commented 2026-01-13 02:35:16 -06:00

This function looks like it is common to RSA and ECC, should it really depend on PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC? I think the whole function shouldn't, on only the ECC-specific parts within the function should be guarded with #if defined(...). (And the specific test cases that use ECC need the appropriate dependencies, but that's already the case.)

This function looks like it is common to RSA and ECC, should it really depend on `PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC`? I think the whole function shouldn't, on only the ECC-specific parts within the function should be guarded with `#if defined(...)`. (And the specific test cases that use ECC need the appropriate dependencies, but that's already the case.)
mpg (Migrated from github.com) commented 2026-01-13 02:45:43 -06:00

All calls to _init() that have a corresponding _free() call in the exit block need to go before the first possible failure. Otherwise we might jump to exit: and call _free() on uninitialised data, which may result in all manner of unpleasant outcomes. It's not a huge deal since it's test code, but we should still demonstrate good practices.

[rant]I've been advocating for years that every structure should come with an _INIT macro so that we can always do mbedtls_pk_context pk = MBEDTLS_PK_INIT; and not have to think. We keep placing the _init call too late in test code, and if we make this mistake in test code, then it's likely some users are making the same mistake in places where it matters much more than test code. IMO initialisation during declaration is a much more robust pattern, and I think most PSA types allows it, so only the legacy types from PK, X.509 and TLS are missing it. Guess it's just never really the time to do it...[/rant]

All calls to `_init()` that have a corresponding `_free()` call in the exit block need to go before the first possible failure. Otherwise we might jump to `exit:` and call `_free()` on uninitialised data, which may result in all manner of unpleasant outcomes. It's not a huge deal since it's test code, but we should still demonstrate good practices. [rant]I've been advocating for years that every structure should come with an `_INIT` macro so that we can always do `mbedtls_pk_context pk = MBEDTLS_PK_INIT;` and not have to think. We keep placing the `_init` call too late in test code, and if we make this mistake in test code, then it's likely some users are making the same mistake in places where it matters much more than test code. IMO initialisation during declaration is a much more robust pattern, and I think most PSA types allows it, so only the legacy types from PK, X.509 and TLS are missing it. Guess it's just never really the time to do it...[/rant]
bjwtaylor commented 2026-01-13 07:34:18 -06:00 (Migrated from github.com)

@mpg in response to your question on the freshly initialised context bitlen=0, if I've understood what you;re after correctly we do that here github.com/bjwtaylor/TF-PSA-Crypto@acde1fd416/tests/suites/test_suite_pk.function (L914)

@mpg in response to your question on the freshly initialised context bitlen=0, if I've understood what you;re after correctly we do that here https://github.com/bjwtaylor/TF-PSA-Crypto/blob/acde1fd41679f121bd55264cb20b4d5a1c61053f/tests/suites/test_suite_pk.function#L914
bjwtaylor (Migrated from github.com) reviewed 2026-01-13 07:41:10 -06:00
@ -964,6 +965,61 @@ void valid_parameters()
}
bjwtaylor (Migrated from github.com) commented 2026-01-13 07:41:10 -06:00

I've updated this to remove the dependency on PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC, from what I can tell the dependencies on the individual test cases should be sufficient to cover it. Let me know what you think and we will see if the CI agrees.

I've updated this to remove the dependency on PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC, from what I can tell the dependencies on the individual test cases should be sufficient to cover it. Let me know what you think and we will see if the CI agrees.
valeriosetti (Migrated from github.com) reviewed 2026-01-13 07:46:20 -06:00
bjwtaylor (Migrated from github.com) reviewed 2026-01-13 07:48:01 -06:00
@ -964,6 +965,61 @@ void valid_parameters()
}
bjwtaylor (Migrated from github.com) commented 2026-01-13 07:48:01 -06:00

I've moved them above the first test, hopefully this resolves the issue. Though let me know if you were after something else.

I've moved them above the first test, hopefully this resolves the issue. Though let me know if you were after something else.
mpg (Migrated from github.com) reviewed 2026-01-14 02:27:07 -06:00
@ -964,6 +965,61 @@ void valid_parameters()
}
mpg (Migrated from github.com) commented 2026-01-14 02:27:07 -06:00

I strongly suspect those two lines

    } else if (pk_type == MBEDTLS_PK_ECDSA) {
        TEST_ASSERT(mbedtls_ecc_group_to_psa(curve_or_keybits, &bitlen) != 0);

need to be guarded with something to ensure mbedtls_ecc_group_to_psa() exists in this build. That might be why depends.py failed (haven't checked yet).

I strongly suspect those two lines ``` } else if (pk_type == MBEDTLS_PK_ECDSA) { TEST_ASSERT(mbedtls_ecc_group_to_psa(curve_or_keybits, &bitlen) != 0); ``` need to be guarded with something to ensure `mbedtls_ecc_group_to_psa()` exists in this build. That might be why `depends.py` failed (haven't checked yet).
mpg (Migrated from github.com) reviewed 2026-01-14 02:31:53 -06:00
@ -964,6 +965,61 @@ void valid_parameters()
}
mpg (Migrated from github.com) commented 2026-01-14 02:31:53 -06:00

Bingo! In depends.py !PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC when compiling this file:

[2026-01-13T20:13:28.011Z] suites/test_suite_pk.function:984:21: error: implicit declaration of function 'mbedtls_ecc_group_to_psa' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
[2026-01-13T20:13:28.011Z]         TEST_ASSERT(mbedtls_ecc_group_to_psa(curve_or_keybits, &bitlen) != 0);
[2026-01-13T20:13:28.011Z]                     ^

This function's declaration is guarded by #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) so we should use the same guard when using it.

Bingo! In `depends.py !PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC` when compiling this file: ``` [2026-01-13T20:13:28.011Z] suites/test_suite_pk.function:984:21: error: implicit declaration of function 'mbedtls_ecc_group_to_psa' is invalid in C99 [-Werror,-Wimplicit-function-declaration] [2026-01-13T20:13:28.011Z] TEST_ASSERT(mbedtls_ecc_group_to_psa(curve_or_keybits, &bitlen) != 0); [2026-01-13T20:13:28.011Z] ^ ``` This function's declaration is guarded by `#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)` so we should use the same guard when using it.
mpg (Migrated from github.com) reviewed 2026-01-14 02:40:42 -06:00
mpg (Migrated from github.com) left a comment

At e44f2ed5cc LGTM except for the CI failure in depends.py.

I checked and the other CI failure, in the memsan component, looks like a flaky test and unrelated to this PR, so it can be ignored (unless/until it repeats).

At e44f2ed5cc7e6c6fb1b797c0bdb834f6dc54ee21 LGTM except for the CI failure in `depends.py`. I checked and the other CI failure, in the memsan component, looks like a flaky test and unrelated to this PR, so it can be ignored (unless/until it repeats).
mpg commented 2026-01-14 03:32:00 -06:00 (Migrated from github.com)

@mpg in response to your question on the freshly initialised context bitlen=0, if I've understood what you;re after correctly we do that here github.com/bjwtaylor/TF-PSA-Crypto@acde1fd416/tests/suites/test_suite_pk.function (L914)

Ah, I looked there and I missed it as I my attention was drawn to the line above where we pass NULL. Yep, you've understood what I'm after correctly, I had just missed it :)

> @mpg in response to your question on the freshly initialised context bitlen=0, if I've understood what you;re after correctly we do that here https://github.com/bjwtaylor/TF-PSA-Crypto/blob/acde1fd41679f121bd55264cb20b4d5a1c61053f/tests/suites/test_suite_pk.function#L914 Ah, I looked there and I missed it as I my attention was drawn to the line above where we pass NULL. Yep, you've understood what I'm after correctly, I had just missed it :)
mpg (Migrated from github.com) approved these changes 2026-01-14 03:32:49 -06:00
mpg (Migrated from github.com) left a comment

At 7d20c6b352 LGTM, hoping the CI agrees.

At 7d20c6b3520a09e2ca108b7800e4ece25ac583a4 LGTM, hoping the CI agrees.
valeriosetti (Migrated from github.com) approved these changes 2026-01-14 05:09:11 -06:00
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!575
No description provided.