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 <[email protected]>
This commit is contained in:
Gilles Peskine
2025-07-08 15:54:03 +02:00
parent 8f0eeca1f2
commit 92dc026d0c
+23 -3
View File
@@ -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 */