8000 Merge pull request #161 from valeriosetti/issue9618-framework · Mbed-TLS/mbedtls-framework@bb8e457 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb8e457

Browse files
Merge pull request #161 from valeriosetti/issue9618-framework
[framework] MBEDTLS_PLATFORM_GET_ENTROPY_ALT in 4.0
2 parents 4a3be27 + dbf62a5 commit bb8e457

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

scripts/generate_config_tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ def single_setting_case(setting: config_common.Setting, when_on: bool,
5858
'MBEDTLS_ERROR_STRERROR_DUMMY': '!MBEDTLS_ERROR_C',
5959
'MBEDTLS_GENPRIME': 'MBEDTLS_RSA_C',
6060
'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES': 'MBEDTLS_ENTROPY_C',
61-
'MBEDTLS_NO_PLATFORM_ENTROPY': 'MBEDTLS_ENTROPY_C',
6261
'MBEDTLS_PKCS1_V15': 'MBEDTLS_RSA_C',
6362
'MBEDTLS_PKCS1_V21': 'MBEDTLS_RSA_C',
6463
'MBEDTLS_PSA_CRYPTO_CLIENT': '!MBEDTLS_PSA_CRYPTO_C',
6564
'MBEDTLS_PSA_INJECT_ENTROPY': 'MBEDTLS_PSA_CRYPTO_C',
6665
'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS': 'MBEDTLS_PSA_CRYPTO_C',
6766
}
6867

68+
if build_tree.is_mbedtls_3_6():
69+
SIMPLE_DEPENDENCIES['MBEDTLS_NO_PLATFORM_ENTROPY'] = 'MBEDTLS_ENTROPY_C'
70+
6971
def dependencies_of_setting(cfg: config_common.Config,
7072
setting: config_common.Setting) -> Optional[str]:
7173
"""Return dependencies without which a setting is not meaningful.

tests/include/test/fake_external_rng_for_test.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,21 @@ void mbedtls_test_enable_insecure_external_rng(void);
3737
void mbedtls_test_disable_insecure_external_rng(void);
3838
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
3939

40+
#if defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT)
41+
42+
#include <mbedtls/platform.h>
43+
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);
54+
55+
#endif /* MBEDTLS_PLATFORM_GET_ENTROPY_ALT */
56+
4057
#endif /* FAKE_EXTERNAL_RNG_FOR_TEST_H */

tests/src/fake_external_rng_for_test.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* Helper functions to test external functions:
44
* - mbedtls_psa_external_get_random()
5-
* - mbedtls_platform_get_entropy_alt()
5+
* - mbedtls_platform_get_entropy()
66
*
77
* These functions are provided only for test purposes and they should not be
88
* used for production.
@@ -54,16 +54,33 @@ psa_status_t mbedtls_psa_external_get_random(
5454
#if defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT)
5555

5656
#include <test/random.h>
57-
# include <mbedtls/platform.h>
57+
#include <mbedtls/entropy.h>
5858

59-
int mbedtls_platform_get_entropy_alt(unsigned char *output, size_t output_size,
60-
size_t *output_len, size_t *entropy_content)
59+
static int get_entropy_alt_force_failure = 0;
60+
static size_t get_entropy_alt_forced_entropy_content = SIZE_MAX;
61+
62+
void mbedtls_test_get_entropy_force(int fail, size_t forced_entropy_content)
63+
{
64+
get_entropy_alt_force_failure = fail;
65+
get_entropy_alt_forced_entropy_content = forced_entropy_content;
66+
}
67+
68+
int mbedtls_platform_get_entropy(unsigned char *output, size_t output_size,
69+
size_t *output_len, size_t *entropy_content)
6170
{
71+
if (get_entropy_alt_force_failure != 0) {
72+
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
73+
}
74+
6275
mbedtls_test_rnd_std_rand(NULL, output, output_size);
6376

6477
*output_len = output_size;
6578
if (entropy_content != NULL) {
66-
*entropy_content = output_size * 8;
79+
if (get_entropy_alt_forced_entropy_content < SIZE_MAX) {
80+
*entropy_content = get_entropy_alt_forced_entropy_content;
81+
} else {
82+
*entropy_content = output_size * 8;
83+
}
6784
}
6885

6986
return 0;

0 commit comments

Comments
 (0)
0