8000 test: fake_external_rng_for_test: add more functionalities to mbedtls… · Mbed-TLS/mbedtls-framework@c612667 · GitHub
[go: up one dir, main page]

Skip to content

Commit c612667

Browse files
committed
test: fake_external_rng_for_test: add more functionalities to mbedtls_platform_get_entropy()
Signed-off-by: Valerio Setti <vsetti@baylibre.com>
1 parent 1e7b5d5 commit c612667

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

tests/include/test/fake_external_rng_for_test.h

Lines changed: 31 additions & 10 deletions
41
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,37 @@ void mbedtls_test_disable_insecure_external_rng(void);
41

4242
#include <mbedtls/platform.h>
4343

44-
/* Force return value or entropy content in mbedtls_platform_get_entropy()
45-
* as follows:
46-
* - if fail == 0 && forced_entropy_content == 0 then
47-
* mbedtls_platform_get_entropy() behaves properly.
48-
* - if fail != 0 then MBEDTLS_ERR_ENTROPY_SOURCE_FAILED is returned.
49-
* - if forced_entropy_content != 0 then
50-
* - return value is success (0) but
51-
* - returned entropy_content will be equal to forced_entropy_content.
52-
*/
53-
void mbedtls_test_get_entropy_force(int fail, size_t forced_entropy_content);
44+
/* In the following there are some helper functions which allow tests to
45+
* modify the behavior of the mbedtls_platform_get_entropy() implementation
46+
* provided for test purposes.
47+
* The following features can be controlled:
48+
* - force a return value;
49+
* - force the amount of bytes returned on each call;
50+
* - force amount of entroy returned on each call;
51+
* - get the number of times the callback has been called.
52+
*/
53+
54+
/* Disable all forced values */
55+
void mbedtls_test_platform_get_entropy_reset(void);
56+
57+
/* Force a failure on mbedtls_platform_get_entropy() as follows
58+
* - val = 1 --> returns MBEDTLS_ERR_ENTROPY_SOURCE_FAILED.
59+
* - val = 0 --> works normally (other forced values apply if set).
60+
*/
61+
void mbedtls_test_platform_get_entropy_set_force_failure(int val);
62+
63+
/* If `val < SIZE_MAX` then forcedly limit the amount of data returned from
64+
* mbedtls_platform_get_entropy() to the provided `val` value.
65+
*/
66+
void mbedtls_test_platform_get_entropy_set_output_len(size_t val);
67+
68+
/* If `val < SIZE_MAX` then forcedly limit the amount of returned entropy from
69+
* mbedtls_platform_get_entropy() to the provided `val` value.
70+
*/
71+
void mbedtls_test_platform_get_entropy_set_entropy_content(size_t val);
72+
73+
/* Return the number of times mbedtls_platform_get_entropy() was called. */
74+
size_t mbedtls_test_platform_get_entropy_get_call_count(void);
5475

5576
#endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT */
5677

tests/src/fake_external_rng_for_test.c

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,64 @@ psa_status_t mbedtls_psa_external_get_random(
5656
#include <test/random.h>
5757
#include <mbedtls/entropy.h>
5858

59-
static int get_entropy_alt_force_failure = 0;
60-
static size_t get_entropy_alt_forced_entropy_content = SIZE_MAX;
59+
static int platform_get_entropy_force_failure;
60+
static size_t platform_get_entropy_forced_entropy_content = SIZE_MAX;
61+
static size_t platform_get_entropy_forced_output_len = SIZE_MAX;
62+
static size_t platform_get_entropy_call_count;
6163

62-
void mbedtls_test_get_entropy_force(int fail, size_t forced_entropy_content)
64+
void mbedtls_test_platform_get_entropy_reset()
6365
{
64-
get_entropy_alt_force_failure = fail;
65-
get_entropy_alt_forced_entropy_content = forced_entropy_content;
66+
platform_get_entropy_call_count = 0;
67+
platform_get_entropy_force_failure = 0;
68+
platform_get_entropy_forced_entropy_content = SIZE_MAX;
69+
platform_get_entropy_forced_output_len = SIZE_MAX;
70+
}
71+
72+
void mbedtls_test_platform_get_entropy_set_force_failure(int val)
73+
{
74+
platform_get_entropy_force_failure = (val != 0);
75+
}
76+
77+
void mbedtls_test_platform_get_entropy_set_output_len(size_t val)
78+
{
79+
platform_get_entropy_forced_output_len = val;
80+
}
81+
82+
void mbedtls_test_platform_get_entropy_set_entropy_content(size_t val)
83+
{
84+
platform_get_entropy_forced_entropy_content = val;
85+
}
86+
87+
size_t mbedtls_test_platform_get_entropy_get_call_count()
88+
{
89+
return platform_get_entropy_call_count;
6690
}
6791

6892
int mbedtls_platform_get_entropy(unsigned char *output, size_t output_size,
6993
size_t *output_len, size_t *entropy_content)
7094
{
71-
if (get_entropy_alt_force_failure != 0) {
95+
platform_get_entropy_call_count++;
96+
97+
/* Return a failure if we were requested to. */
98+
if (platform_get_entropy_force_failure != 0) {
7299
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
73100
}
74101

102+
/* Return less data than requested if we were requested to. */
103+
if (platform_get_entropy_forced_output_len < SIZE_MAX) {
104+
/* Prevent buffer overrun */
105+
if (platform_get_entropy_forced_output_len > output_size) {
106+
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
107+
}
108+
output_size = platform_get_entropy_forced_output_len;
109+
}
110+
75111
mbedtls_test_rnd_std_rand(NULL, output, output_size);
76112

77113
*output_len = output_size;
78114
if (entropy_content != NULL) {
79-
if (get_entropy_alt_forced_entropy_content < SIZE_MAX) {
80-
*entropy_content = get_entropy_alt_forced_entropy_content;
115+
if (platform_get_entropy_forced_entropy_content < SIZE_MAX) {
116+
*entropy_content = platform_get_entropy_forced_entropy_content;
81117
} else {
82118
*entropy_content = output_size * 8;
83119
}

0 commit comments

Comments
 (0)
0