From 92dc026d0cdb39c08be564bd92930b8465680a6f Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 8 Jul 2025 15:42:28 +0200 Subject: [PATCH] Refactor entropy callback to prepare for the new prototype Refactor `mbedtls_platform_get_entropy()` into a core function `fake_get_entropy()` and a wrapper that's specific to the current interface of the callback. A subsequent commit will introduce the new interface of the callback, and later we will remove the current, never released form. Signed-off-by: Gilles Peskine --- tests/src/fake_external_rng_for_test.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/src/fake_external_rng_for_test.c b/tests/src/fake_external_rng_for_test.c index 21da1414a..7b2ea4d67 100644 --- a/tests/src/fake_external_rng_for_test.c +++ b/tests/src/fake_external_rng_for_test.c @@ -89,8 +89,8 @@ size_t mbedtls_test_platform_get_entropy_get_call_count() return platform_get_entropy_call_count; } -int mbedtls_platform_get_entropy(unsigned char *output, size_t output_size, - size_t *output_len, size_t *entropy_content) +static int fake_get_entropy(unsigned char *output, size_t output_size, + size_t *entropy_content) { platform_get_entropy_call_count++; @@ -110,7 +110,6 @@ int mbedtls_platform_get_entropy(unsigned char *output, size_t output_size, mbedtls_test_rnd_std_rand(NULL, output, output_size); - *output_len = output_size; if (entropy_content != NULL) { if (platform_get_entropy_forced_entropy_content < SIZE_MAX) { *entropy_content = platform_get_entropy_forced_entropy_content; @@ -123,3 +122,24 @@ int mbedtls_platform_get_entropy(unsigned char *output, size_t output_size, } #endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT */ + +/* Form of the callback introduced in + * https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/212 , + * never released, superseded by + * https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/307 . + */ +#if defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT) +int mbedtls_platform_get_entropy(unsigned char *output, size_t output_size, + size_t *output_len, size_t *entropy_content) +{ + int ret = fake_get_entropy(output, output_size, entropy_content); + if (ret == 0) { + if (platform_get_entropy_forced_output_len == SIZE_MAX) { + *output_len = output_size; + } else { + *output_len = platform_get_entropy_forced_output_len; + } + } + return ret; +} +#endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT */