From 91c400a884a9e11fe05b4bab4b2c17f25a9246bd Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 8 Jul 2025 16:06:43 +0200 Subject: [PATCH] Provide the new entropy callback Provide the new interface for `mbedtls_platform_get_entropy()` requested in https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/307 when `MBEDTLS_PSA_DRIVER_GET_ENTROPY` is enabled. Keep providing the old interface when `MBEDTLS_PLATFORM_GET_ENTROPY_ALT` is enabled. For now, the new interface is identical to the old one. Subsequent commits will change the interface. Signed-off-by: Gilles Peskine --- .../include/test/fake_external_rng_for_test.h | 4 ++-- tests/src/fake_external_rng_for_test.c | 20 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/include/test/fake_external_rng_for_test.h b/tests/include/test/fake_external_rng_for_test.h index e2c46019a..9ab128273 100644 --- a/tests/include/test/fake_external_rng_for_test.h +++ b/tests/include/test/fake_external_rng_for_test.h @@ -37,7 +37,7 @@ void mbedtls_test_enable_insecure_external_rng(void); void mbedtls_test_disable_insecure_external_rng(void); #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ -#if defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT) +#if defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT) || defined(MBEDTLS_PSA_DRIVER_GET_ENTROPY) #include @@ -73,6 +73,6 @@ void mbedtls_test_platform_get_entropy_set_entropy_content(size_t val); /* Return the number of times mbedtls_platform_get_entropy() was called. */ size_t mbedtls_test_platform_get_entropy_get_call_count(void); -#endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT */ +#endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT || MBEDTLS_PSA_DRIVER_GET_ENTROPY */ #endif /* FAKE_EXTERNAL_RNG_FOR_TEST_H */ diff --git a/tests/src/fake_external_rng_for_test.c b/tests/src/fake_external_rng_for_test.c index 7b2ea4d67..a1f75ca2a 100644 --- a/tests/src/fake_external_rng_for_test.c +++ b/tests/src/fake_external_rng_for_test.c @@ -51,7 +51,7 @@ psa_status_t mbedtls_psa_external_get_random( #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ -#if defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT) +#if defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT) || defined(MBEDTLS_PSA_DRIVER_GET_ENTROPY) #include #include @@ -121,7 +121,7 @@ static int fake_get_entropy(unsigned char *output, size_t output_size, return 0; } -#endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT */ +#endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT || MBEDTLS_PSA_DRIVER_GET_ENTROPY */ /* Form of the callback introduced in * https://github.com/Mbed-TLS/TF-PSA-Crypto/pull/212 , @@ -143,3 +143,19 @@ int mbedtls_platform_get_entropy(unsigned char *output, size_t output_size, return ret; } #endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT */ + +#if defined(MBEDTLS_PSA_DRIVER_GET_ENTROPY) +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_PSA_DRIVER_GET_ENTROPY */