mirror of
https://github.com/Mbed-TLS/mbedtls-framework.git
synced 2026-10-20 15:37:55 +00:00
test: fake_external_rng_for_test: add more functionalities to mbedtls_platform_get_entropy()
Signed-off-by: Valerio Setti <[email protected]>
This commit is contained in:
@@ -41,16 +41,37 @@ void mbedtls_test_disable_insecure_external_rng(void);
|
|||||||
|
|
||||||
#include <mbedtls/platform.h>
|
#include <mbedtls/platform.h>
|
||||||
|
|
||||||
/* Force return value or entropy content in mbedtls_platform_get_entropy()
|
/* In the following there are some helper functions which allow tests to
|
||||||
* as follows:
|
* modify the behavior of the mbedtls_platform_get_entropy() implementation
|
||||||
* - if fail == 0 && forced_entropy_content == 0 then
|
* provided for test purposes.
|
||||||
* mbedtls_platform_get_entropy() behaves properly.
|
* The following features can be controlled:
|
||||||
* - if fail != 0 then MBEDTLS_ERR_ENTROPY_SOURCE_FAILED is returned.
|
* - force a return value;
|
||||||
* - if forced_entropy_content != 0 then
|
* - force the amount of bytes returned on each call;
|
||||||
* - return value is success (0) but
|
* - force amount of entroy returned on each call;
|
||||||
* - returned entropy_content will be equal to forced_entropy_content.
|
* - get the number of times the callback has been called.
|
||||||
*/
|
*/
|
||||||
void mbedtls_test_get_entropy_force(int fail, size_t forced_entropy_content);
|
|
||||||
|
/* Disable all forced values */
|
||||||
|
void mbedtls_test_platform_get_entropy_reset(void);
|
||||||
|
|
||||||
|
/* Force a failure on mbedtls_platform_get_entropy() as follows
|
||||||
|
* - val = 1 --> returns MBEDTLS_ERR_ENTROPY_SOURCE_FAILED.
|
||||||
|
* - val = 0 --> works normally (other forced values apply if set).
|
||||||
|
*/
|
||||||
|
void mbedtls_test_platform_get_entropy_set_force_failure(int val);
|
||||||
|
|
||||||
|
/* If `val < SIZE_MAX` then forcedly limit the amount of data returned from
|
||||||
|
* mbedtls_platform_get_entropy() to the provided `val` value.
|
||||||
|
*/
|
||||||
|
void mbedtls_test_platform_get_entropy_set_output_len(size_t val);
|
||||||
|
|
||||||
|
/* If `val < SIZE_MAX` then forcedly limit the amount of returned entropy from
|
||||||
|
* mbedtls_platform_get_entropy() to the provided `val` value.
|
||||||
|
*/
|
||||||
|
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 */
|
||||||
|
|
||||||
|
|||||||
@@ -56,28 +56,64 @@ psa_status_t mbedtls_psa_external_get_random(
|
|||||||
#include <test/random.h>
|
#include <test/random.h>
|
||||||
#include <mbedtls/entropy.h>
|
#include <mbedtls/entropy.h>
|
||||||
|
|
||||||
static int get_entropy_alt_force_failure = 0;
|
static int platform_get_entropy_force_failure;
|
||||||
static size_t get_entropy_alt_forced_entropy_content = SIZE_MAX;
|
static size_t platform_get_entropy_forced_entropy_content = SIZE_MAX;
|
||||||
|
static size_t platform_get_entropy_forced_output_len = SIZE_MAX;
|
||||||
|
static size_t platform_get_entropy_call_count;
|
||||||
|
|
||||||
void mbedtls_test_get_entropy_force(int fail, size_t forced_entropy_content)
|
void mbedtls_test_platform_get_entropy_reset()
|
||||||
{
|
{
|
||||||
get_entropy_alt_force_failure = fail;
|
platform_get_entropy_call_count = 0;
|
||||||
get_entropy_alt_forced_entropy_content = forced_entropy_content;
|
platform_get_entropy_force_failure = 0;
|
||||||
|
platform_get_entropy_forced_entropy_content = SIZE_MAX;
|
||||||
|
platform_get_entropy_forced_output_len = SIZE_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mbedtls_test_platform_get_entropy_set_force_failure(int val)
|
||||||
|
{
|
||||||
|
platform_get_entropy_force_failure = (val != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mbedtls_test_platform_get_entropy_set_output_len(size_t val)
|
||||||
|
{
|
||||||
|
platform_get_entropy_forced_output_len = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mbedtls_test_platform_get_entropy_set_entropy_content(size_t val)
|
||||||
|
{
|
||||||
|
platform_get_entropy_forced_entropy_content = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
int mbedtls_platform_get_entropy(unsigned char *output, size_t output_size,
|
||||||
size_t *output_len, size_t *entropy_content)
|
size_t *output_len, size_t *entropy_content)
|
||||||
{
|
{
|
||||||
if (get_entropy_alt_force_failure != 0) {
|
platform_get_entropy_call_count++;
|
||||||
|
|
||||||
|
/* Return a failure if we were requested to. */
|
||||||
|
if (platform_get_entropy_force_failure != 0) {
|
||||||
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Return less data than requested if we were requested to. */
|
||||||
|
if (platform_get_entropy_forced_output_len < SIZE_MAX) {
|
||||||
|
/* Prevent buffer overrun */
|
||||||
|
if (platform_get_entropy_forced_output_len > output_size) {
|
||||||
|
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
||||||
|
}
|
||||||
|
output_size = platform_get_entropy_forced_output_len;
|
||||||
|
}
|
||||||
|
|
||||||
mbedtls_test_rnd_std_rand(NULL, output, output_size);
|
mbedtls_test_rnd_std_rand(NULL, output, output_size);
|
||||||
|
|
||||||
*output_len = output_size;
|
*output_len = output_size;
|
||||||
if (entropy_content != NULL) {
|
if (entropy_content != NULL) {
|
||||||
if (get_entropy_alt_forced_entropy_content < SIZE_MAX) {
|
if (platform_get_entropy_forced_entropy_content < SIZE_MAX) {
|
||||||
*entropy_content = get_entropy_alt_forced_entropy_content;
|
*entropy_content = platform_get_entropy_forced_entropy_content;
|
||||||
} else {
|
} else {
|
||||||
*entropy_content = output_size * 8;
|
*entropy_content = output_size * 8;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user