tests: add functions to force behavior of mbedtls_platform_get_entropy_alt()

Signed-off-by: Valerio Setti <[email protected]>
This commit is contained in:
Valerio Setti
2025-04-16 11:24:11 +02:00
parent f4662dbaf6
commit 566659e20e
2 changed files with 36 additions and 2 deletions
@@ -37,4 +37,21 @@ 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)
#include <mbedtls/platform.h>
/* Force return value or entropy content in mbedtls_platform_get_entropy_alt()
* as follows:
* - if fail == 0 && forced_entropy_content == 0 then
* mbedtls_platform_get_entropy_alt() behaves properly.
* - if fail != 0 then MBEDTLS_ERR_ENTROPY_SOURCE_FAILED is returned.
* - if forced_entropy_content != 0 then
* - return value is success (0) but
* - returned entropy_content will be equal to forced_entropy_content.
*/
void mbedtls_test_get_entropy_alt_force(int fail, size_t forced_entropy_content);
#endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT */
#endif /* FAKE_EXTERNAL_RNG_FOR_TEST_H */
+19 -2
View File
@@ -54,16 +54,33 @@ psa_status_t mbedtls_psa_external_get_random(
#if defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT)
#include <test/random.h>
# include <mbedtls/platform.h>
#include <mbedtls/entropy.h>
static int get_entropy_alt_force_failure = 0;
static size_t get_entropy_alt_forced_entropy_content = 0;
void mbedtls_test_get_entropy_alt_force(int fail, size_t forced_entropy_content)
{
get_entropy_alt_force_failure = fail;
get_entropy_alt_forced_entropy_content = forced_entropy_content;
}
int mbedtls_platform_get_entropy_alt(unsigned char *output, size_t output_size,
size_t *output_len, size_t *entropy_content)
{
if (get_entropy_alt_force_failure != 0) {
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
}
mbedtls_test_rnd_std_rand(NULL, output, output_size);
*output_len = output_size;
if (entropy_content != NULL) {
*entropy_content = output_size * 8;
if (get_entropy_alt_forced_entropy_content != 0) {
*entropy_content = get_entropy_alt_forced_entropy_content;
} else {
*entropy_content = output_size * 8;
}
}
return 0;