@@ -56,28 +56,64 @@ psa_status_t mbedtls_psa_external_get_random(
56
56
#include <test/random.h>
57
57
#include <mbedtls/entropy.h>
58
58
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 ;
61
63
62
- void mbedtls_test_get_entropy_force ( int fail , size_t forced_entropy_content )
64
+ void mbedtls_test_platform_get_entropy_reset ( )
63
65
{
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 ;
66
90
}
67
91
68
92
int mbedtls_platform_get_entropy (unsigned char * output , size_t output_size ,
69
93
size_t * output_len , size_t * entropy_content )
70
94
{
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 ) {
72
99
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ;
73
100
}
74
101
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
+
75
111
mbedtls_test_rnd_std_rand (NULL , output , output_size );
76
112
77
113
* output_len = output_size ;
78
114
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 ;
81
117
} else {
82
118
* entropy_content = output_size * 8 ;
83
119
}
0 commit comments