Skip to content

[tf-psa-crypto] MBEDTLS_PLATFORM_GET_ENTROPY_ALT in 4.0 #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

valeriosetti
Copy link
Contributor

@valeriosetti valeriosetti commented Mar 18, 2025

Description

This helps resolving Mbed-TLS/mbedtls#9618

CI note

Due to dependencies with the main repo PR please evaluate the CI of 10090 as validation of this PR.

PR checklist

@valeriosetti valeriosetti self-assigned this Mar 18, 2025
@valeriosetti valeriosetti added size-s Estimated task size: small (~2d) 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 needs-ci Needs to pass CI tests priority-high High priority - will be reviewed soon labels Mar 18, 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.

I just did a design review. I won't be able to do a full code review.

We just realized this morning that the issue description wasn't very clear, and several things were in Janos's mind and mine but we hadn't written them down. Sorry about this. I added a note to the issue and some comments here, hopefully that should clarify things.

* in such information, so the function must handle
* this.
*/
int mbedtls_entropy_hardware_poll(unsigned char *output, size_t output_size,
Copy link
Contributor

Choose a reason for hiding this comment

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

Non-blocker: since the function is now declared in platform.h, should we call it mbedtls_platform_xxx? Same question about the configuration option. Maybe MBEDTLS_PLATFORM_GET_ENTROPY_ALT and mbedtls_platform_get_entropy()?

Comment on lines 465 to 466
* \param[out] entropy_content Measure of the entropy content (in bits) of the
* data written in the \p output buffer. The pointer
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add the constraint that for the time being, the function must set entropy_content = 8 * *output_len on success. The library will be extended later to support partial entropy.

Comment on lines 467 to 469
* can be \c NULL in case the caller is not interested
* in such information, so the function must handle
* this.
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need to support callers that don't want the value. entropy_content is guaranteed to be non-null.

* \brief User defined callback function that is used from the entropy
* module to gather entropy data from some hardware device.
*
* \warning This is not provided by Mbed TLS.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think “not provided by Mbed TLS” is a useful warning. Also, it's TF-PSA-Crypto now.

On the other hand, the function's documentation should state that this function is only used when MBEDTLS_ENTROPY_HARDWARE_POLL is defined. This is otherwise not visible when you read the Doxygen-rendered documentation.

Comment on lines 9 to 12
`MBEDTLS_ENTROPY_HARDWARE_POLL_MIN` can optionally optinally define the
minimum number of bytes that should be polled using that function before
declaring entropy gathering completed. If not defined, then a default
value of 32 bytes is used.
Copy link
Contributor

Choose a reason for hiding this comment

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

MBEDTLS_ENTROPY_HARDWARE_POLL_MIN should not be a configuration parameter, at least not in this form. The RNG needs a certain amount of entropy. This minimum is up to the system configuration (and in particular the required cryptographic strength for the system), not up to the hardware integration. The person writing the TRNG device driver doesn't get to choose this value.

I think we should just use the existing MBEDTLS_ENTROPY_MIN_PLATFORM here. The distinction between “platform source” and “hardware source” doesn't really make sense as it's currently described in entropy_poll.h. Actually the “platform source” is the operating system integrations that the library supports out of the box, and the “hardware source” is the one for other systems where the integrator needs to provide a driver.

removed.

Features
* The new symbol `MBEDTLS_ENTROPY_HARDWARE_POLL` is added to allow the
Copy link
Contributor

Choose a reason for hiding this comment

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

The changelog entries should probably mention the function names as well as the option names, for easier searching.

void *data, unsigned char *output, size_t len, size_t *olen)
{
(void) data;
return mbedtls_entropy_hardware_poll(output, len, olen, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

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

The wrapper should check the entropy content and fail if it's less than full entropy, i.e. if entropy_output < 8 * *olen. Please add a unit test for that in test_suite_entropy.

Also the entropy collection should fail if *olen is too small and as a result there isn't enough entropy, collected from all sources. I believe this is covered by the existing tests in test_suite_entropy, but please double-check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The wrapper should check the entropy content and fail if it's less than full entropy, i.e. if entropy_output < 8 * *olen. Please add a unit test for that in test_suite_entropy.

While adding the check on the wrapper and testing it with the new component tf_psa_crypto_platform_get_entropy_alt I found that tests are already failing if the returned entropy content is not correct, so there seems to be no need for a new test step. As reference implementation I'm using the one proposed in the companion PR Mbed-TLS/mbedtls-framework#151.

@valeriosetti valeriosetti force-pushed the issue9618-tfpsacrypto branch from 49842b8 to a5de9ab Compare March 24, 2025 12:46
@valeriosetti valeriosetti force-pushed the issue9618-tfpsacrypto branch from a5de9ab to 17e588b Compare March 24, 2025 14:59
@valeriosetti valeriosetti changed the title [tf-psa-crypto] MBEDTLS_ENTROPY_HARDWARE_ALT in 4.0 [tf-psa-crypto] MBEDTLS_PLATFORM_GET_ENTROPY_ALT in 4.0 Mar 24, 2025
@gabor-mezei-arm gabor-mezei-arm self-requested a review March 27, 2025 09:24
@valeriosetti valeriosetti removed the needs-ci Needs to pass CI tests label Mar 27, 2025
bjwtaylor
bjwtaylor previously approved these changes Mar 27, 2025
@valeriosetti valeriosetti force-pushed the issue9618-tfpsacrypto branch 2 times, most recently from dfe34b3 to 92e6f42 Compare April 1, 2025 07:33
@valeriosetti valeriosetti requested a review from bjwtaylor April 1, 2025 07:34
@valeriosetti
Copy link
Contributor Author

I think that CI failures in test_psasim and test_suite_with_psasim can be ignored in this PR as the same tests in the main PR are OK

@valeriosetti valeriosetti force-pushed the issue9618-tfpsacrypto branch from 92e6f42 to fa5d3d3 Compare April 1, 2025 20:07
@valeriosetti
Copy link
Contributor Author

I just forced push the last commit following the merge of Mbed-TLS/mbedtls-framework#151. No other change was made

gabor-mezei-arm
gabor-mezei-arm previously approved these changes Apr 2, 2025
@valeriosetti
Copy link
Contributor Author

I think that CI failures in test_psasim and test_suite_with_psasim can be ignored in this PR as the same tests in the main PR are OK

Thinking on this a bit more I think I can explain why psasim tests fail in this PR. The PR on the main repo modifies config.py (only in the main repo) so that the full config doesn't set MBEDTLS_PLATFORM_GET_ENTROPY_ALT.
The problem is that when Mbed-TLS/mbedtls#10090 is tested then it pick this PR as the tf-psa-crypto version. The opposite is not true: when this PR is tested on the CI, the main repo is checked out at development and there the full config enables MBEDTLS_PLATFORM_GET_ENTROPY_ALT.

bjwtaylor
bjwtaylor previously approved these changes Apr 4, 2025
@valeriosetti valeriosetti dismissed stale reviews from bjwtaylor and gabor-mezei-arm via 0b4ca69 April 7, 2025 08:18
@gilles-peskine-arm
Copy link
Contributor

It's problematic to merge something in the crypto repository that breaks the CI, since it means crypto will be broken on all branches that don't have this PR merged yet.

Can you please add a transition function mbedtls_platform_entropy_poll that's just good enough for how it's used in selftest.c, with a comment stating that it's going to be removed soon? And might as well make a PR that reverts that commit, to be merged in a few weeks.

@valeriosetti
Copy link
Contributor Author

Can you please add a transition function mbedtls_platform_entropy_poll that's just good enough for how it's used in selftest.c, with a comment stating that it's going to be removed soon? And might as well make a PR that reverts that commit, to be merged in a few weeks.

@gilles-peskine-arm I just created #273 to track this

@valeriosetti
Copy link
Contributor Author

@gilles-peskine-arm although I added the stub function for mbedtls_platform_entropy_poll there are remaining failures on the CI related to the fact that the main branch actually needs some more fixes on its codebase to make this PR in (configuration header files, config.py, etc - see Mbed-TLS/mbedtls#10090 for the full list).
I'm not sure I can make the CI fully happy in this PR.,,

@valeriosetti valeriosetti force-pushed the issue9618-tfpsacrypto branch from 88c818c to 996bb07 Compare May 5, 2025 16:00
@valeriosetti
Copy link
Contributor Author

Since we agreed to manually merge this PR and then set Mbed-TLS/mbedtls#10090 on the merge queue immediately after that I removed the temporary commit 26fdaa1 which was added as attempt to make the CI fully happy.
We will rely on CI of 10090 to validate this PR.

Copy link
Contributor

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

Choose a reason for hiding this comment

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

LGTM

component_tf_psa_crypto_test_platform_get_entropy_alt() was missing
the _test_ part in order to be aligned with the standard naming form.
Correct naming is
component_tf_psa_crypto_test_platform_get_entropy_alt().

Signed-off-by: Valerio Setti <[email protected]>
Signed-off-by: Valerio Setti <[email protected]>
@valeriosetti
Copy link
Contributor Author

Sorry for the last minute fix, but since I had to modify the PR on the main repo to make its CI happy, I preferred to fix the name of the test component being added also in this PR (see new commit).

Copy link
Contributor

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

Choose a reason for hiding this comment

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

LGTM at 1294fd2. The CI is still unhappy but that's expected: we are breaking TF-PSA-Crypto, and Mbed-TLS/mbedtls#10090 will fix it.

@gilles-peskine-arm gilles-peskine-arm added needs-work needs-review Every commit must be reviewed by at least two team members and removed needs-review Every commit must be reviewed by at least two team members needs-work labels May 6, 2025
@@ -24,5 +24,6 @@
#define MBEDTLS_PSA_CRYPTO_C
#define MBEDTLS_CTR_DRBG_C
#define MBEDTLS_ENTROPY_C
#define MBEDTLS_PLATFORM_C
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be necessary. MBEDTLS_PLATFORM_C should only be needed to support runtime configuration of platform features. Neither MBEDTLS_PLATFORM_GET_ENTROPY_ALT nor !MBEDTLS_PLATFORM_GET_ENTROPY_ALT should require MBEDTLS_PLATFORM_C.

I think the natural way to fix this is to move the code from platform.c to platform_util.c.

At this point, given that we're planning to do a disruptive combined merge tonight, I propose to merge as is, and do a follow-up that does the move and removes the added enabling of MBEDTLS_PLATFORM_C in both TF-PSA-Crypto and mbedtls.

Copy link
Contributor

Choose a reason for hiding this comment

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

@gilles-peskine-arm
Copy link
Contributor

As previously discussed, I'm going ahead and force-merging this pull request. This will break the CI on TF-PSA-Crypto where it consumes the head of mbedtls, to be fixed by merging Mbed-TLS/mbedtls#10090.

@gilles-peskine-arm gilles-peskine-arm merged commit 5ab6c9c into Mbed-TLS:development May 6, 2025
2 of 5 checks passed
@github-project-automation github-project-automation bot moved this from Has Approval to Done in Roadmap pull requests (new board) May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-review Every commit must be reviewed by at least two team members priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)
Development

Successfully merging this pull request may close these issues.

4 participants