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

Open
wants to merge 35 commits into
base: development
Choose a base branch
from

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
This is necessary to let entropy being able to gather entropy data from
the native platform source.

Signed-off-by: Valerio Setti <[email protected]>
Since this is now an internal function, output_size always comes from our
library code, and we can assume that it has a “reasonable” size — tens of
bytes, definitely less than ULONG_MAX which is 2^32. So the loop is no
longer needed.

Signed-off-by: Valerio Setti <[email protected]>
Signed-off-by: Valerio Setti <[email protected]>
…lt()

Test also the case in which the callback is returning less data than
requested. In this case the entropy module will call the callback until
it reaches the minimum required amount of data.

Signed-off-by: Valerio Setti <[email protected]>
…alt()

Ensure that all forced values on mbedtls_platform_get_entropy() are
reset before quitting the test functions. This prevents weird failures
on subsequent tests that might be executed.

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

@gilles-peskine-arm @bjwtaylor I just updated the framework reference (since Mbed-TLS/mbedtls-framework#165 was merged) and rebased in top of development branch to solve CI issues with main repo.

@valeriosetti valeriosetti force-pushed the issue9618-tfpsacrypto branch from 4320b04 to ced4965 Compare April 29, 2025 13:42
@valeriosetti
Copy link
Contributor Author

Remaining CI failures are:

[2025-04-29T14:35:20.193Z] test/selftest.c:229:14: error: implicit declaration of function 'mbedtls_platform_entropy_poll' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
[2025-04-29T14:35:20.193Z]     result = mbedtls_platform_entropy_poll(NULL,

which is reasonable since the main repo is tested on the development branch. We can rely on the CI of Mbed-TLS/mbedtls#10090 to validate this PR.

@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.

…py_poll()

This is temporary and going to be removed by
Mbed-TLS#273

Signed-off-by: Valerio Setti <[email protected]>
Signed-off-by: Valerio Setti <[email protected]>
@valeriosetti valeriosetti force-pushed the issue9618-tfpsacrypto branch from ced4965 to 88c818c Compare April 30, 2025 09:51
@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.,,

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)
Projects
Development

Successfully merging this pull request may close these issues.

4 participants