Skip to content

Conversation

@valeriosetti
Copy link
Contributor

Description

Prerequisite for Mbed-TLS/TF-PSA-Crypto#600

PR checklist

@valeriosetti valeriosetti added needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review priority-high High priority - will be reviewed soon size-xs Estimated task size: extra small (a few hours at most) labels Dec 17, 2025
@valeriosetti valeriosetti force-pushed the issue598-framework branch 4 times, most recently from 0408ec1 to 8532c59 Compare December 19, 2025 08:34
@valeriosetti valeriosetti force-pushed the issue598-framework branch 4 times, most recently from 29b9600 to 267f3d2 Compare December 19, 2025 14:09
…ader

- declare all arrays and structures as static
- add guards to the header file

This allows multiple inclusions of the generated header file.

Signed-off-by: Valerio Setti <[email protected]>
@valeriosetti valeriosetti added needs-review Every commit must be reviewed by at least two team members, and removed needs-ci Needs to pass CI tests labels Dec 22, 2025
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

A few remarks and suggestions

#ifndef TEST_TEST_KEYS_H
#define TEST_TEST_KEYS_H
#if !defined(MBEDTLS_VERSION_MAJOR) || MBEDTLS_VERSION_MAJOR >= 4
Copy link
Contributor

Choose a reason for hiding this comment

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

That works, but mbedtls/private/ecp.h never existed in Mbed TLS, so it's a strange way to put it. How about

Suggested change
#if !defined(MBEDTLS_VERSION_MAJOR) || MBEDTLS_VERSION_MAJOR >= 4
#if TF_PSA_CRYPTO_VERSION_MAJOR >= 1

Same in pk_helpers.c.

Comment on lines +121 to +125
#if defined(__GNUC__) || defined(__clang__)
#define UNUSED __attribute__((unused))
#else
#define UNUSED
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

We already have MBEDTLS_MAYBE_UNUSED

#define UNUSED
#endif
static struct predefined_key_element predefined_keys[] UNUSED = {{
Copy link
Contributor

Choose a reason for hiding this comment

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

We normally put MBEDTLS_MAYBE_UNUSED at the very beginning of the definition, before static and the type. It doesn't matter with GCC-like compilers, but it might matter with other compilers.

TEST_PK_COPY_PUBLIC_FROM_PSA,
} pk_context_populate_method_t;

int pk_helpers_get_predefined_key_data(int is_ec, int group_id_or_keybits,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please document any function in a public header of the subproject. (It's a good idea to document all declarations in headers, really.) framework/tests/include is public for the framework.

TEST_PK_COPY_PUBLIC_FROM_PSA,
} pk_context_populate_method_t;

int pk_helpers_get_predefined_key_data(int is_ec, int group_id_or_keybits,
Copy link
Contributor

Choose a reason for hiding this comment

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

All linkable functions should have one of the prefixes mbedtls_, psa_ or tf_psa_crypto_.

@@ -0,0 +1,38 @@
/*
* Helper functions for PK
Copy link
Contributor

Choose a reason for hiding this comment

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

Please document that this is only for TF-PSA-Crypto 1.0 and above.

if (group_id_or_keybits == predefined_keys[i].group_id) {
predefined_key = &predefined_keys[i];
}
} else if (group_id_or_keybits == predefined_keys[i].keybits) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing: check that it's an ECC key, and not an RSA key that happens to have the right size (or in the future ML-DSA, etc.).

On a related note, the check for the EC case above assumes that predefined_keys[i].group_id for a non-EC key cannot be equal to group_id_or_keybits for an EC key, which is currently true, but is fragile.

return 0;
}

TEST_FAIL("Unsupported key");
Copy link
Contributor

Choose a reason for hiding this comment

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

If this failure point is reached, the only feedback you get from the test output is:

  • that this point was reached;
  • the message “Unsupported key”;
  • the test step, if set with mbedtls_test_set_step();
  • which test case is currently executing.

In particular, there's no indication of the key type and size that was sought.

Unfortunately, the argument to TEST_FAIL must be a string literal, because the memory containing the string has to still be alive when the test function returns. But there's a way to smuggle some extra information in two additional lines with a length limit. The functions to do that directly aren't exposed and aren't thread-safe. But you can at least easily smuggle an extra integer (or even two) with TEST_EQUAL. So you can write something like

if (ec) {
    int ec_group_found = 0;
    TEST_EQUAL(group_id_or_key_bits, found);
} else {
    int rsa_bits_found = 0;
    TEST_EQUAL(group_id_or_key_bits, rsa_bits_found);
}

if (PSA_KEY_TYPE_IS_RSA(key_type)) {
ret = pk_helpers_get_predefined_key_data(0, key_bits, &priv_key, &priv_key_len,
&pub_key, &pub_key_len);
TEST_EQUAL(ret, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

If pk_helpers_get_predefined_key_data returns nonzero, it already marks the test case as failed. So if (ret != 0) goto exit would have the same effect.

TEST_FAIL("Unknown method");
}

exit:; /* Needed to make compiler happy */
Copy link
Contributor

Choose a reason for hiding this comment

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

If pk_helpers_populate_context fails, you probably want to let the caller know so that it can goto exit.

@gilles-peskine-arm gilles-peskine-arm added needs-work and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Dec 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-work priority-high High priority - will be reviewed soon size-xs Estimated task size: extra small (a few hours at most)

Projects

Development

Successfully merging this pull request may close these issues.

3 participants