Skip to content

Commit 5ef44fd

Browse files
valeriosettinashif
authored andcommitted
mbedtls: add new kconfig for non-CSPRNG sources in psa_generate_random()
This is meant to be used only for tests on platforms where CSPRNG sources are not available. It should not be used in production. Signed-off-by: Valerio Setti <[email protected]>
1 parent 6523fb3 commit 5ef44fd

File tree

3 files changed

+76
-47
lines changed

3 files changed

+76
-47
lines changed

modules/mbedtls/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ zephyr_interface_library_named(mbedTLS)
1111
message(WARNING "No entropy device on the system, using fake entropy source!")
1212
endif()
1313

14+
if(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
15+
if(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG)
16+
message(WARNING "
17+
Non cryptographycally secure sources are enabled for psa_generate_random().
18+
This is meant to be used only for tests, not in production!")
19+
else()
20+
if(NOT CONFIG_CSPRNG_ENABLED)
21+
message(FATAL_ERROR "
22+
MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is set but there is
23+
no CSPRNG enabled.")
24+
endif()
25+
endif()
26+
endif()
27+
1428
# Add the config-file entry point
1529
target_compile_definitions(mbedTLS INTERFACE
1630
MBEDTLS_CONFIG_FILE="${CONFIG_MBEDTLS_CFG_FILE}"

modules/mbedtls/Kconfig.tls-generic

+12-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ choice MBEDTLS_PSA_CRYPTO_RNG_SOURCE
468468

469469
config MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
470470
bool "Use a cryptographically secure driver as random source"
471-
depends on CSPRNG_ENABLED
472471
help
473472
Use a cryptographically secure random generator to provide random data
474473
instead of legacy Mbed TLS modules. This has a smaller footprint
@@ -500,6 +499,18 @@ config MBEDTLS_PSA_CRYPTO_LEGACY_RNG
500499

501500
endchoice
502501

502+
config MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG
503+
bool "Allow non cryptographically secure random sources (for test only!)"
504+
depends on MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
505+
help
506+
MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is by default limited to rely only
507+
on cryptographically secure random number generators. However, only
508+
for test purposes, it might be useful to enable external random
509+
number generation, but have it using weak random sources (non
510+
cryptographically secure).
511+
Warning: this is meant to be enabled only for tests, not in production
512+
as the generated values are not cryptographically secure!
513+
503514
config MBEDTLS_PSA_CRYPTO_C
504515
bool "Platform Security Architecture cryptography API"
505516
depends on !BUILD_WITH_TFM

modules/mbedtls/zephyr_init.c

+50-46
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,6 @@ static void init_heap(void)
4747
#define init_heap(...)
4848
#endif /* CONFIG_MBEDTLS_ENABLE_HEAP && MBEDTLS_MEMORY_BUFFER_ALLOC_C */
4949

50-
#if defined(CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR)
51-
static const struct device *const entropy_dev =
52-
DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_entropy));
53-
54-
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len,
55-
size_t *olen)
56-
{
57-
int ret;
58-
uint16_t request_len = len > UINT16_MAX ? UINT16_MAX : len;
59-
60-
ARG_UNUSED(data);
61-
62-
if (output == NULL || olen == NULL || len == 0) {
63-
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
64-
}
65-
66-
if (!IS_ENABLED(CONFIG_ENTROPY_HAS_DRIVER)) {
67-
sys_rand_get(output, len);
68-
*olen = len;
69-
70-
return 0;
71-
}
72-
73-
if (!device_is_ready(entropy_dev)) {
74-
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
75-
}
76-
77-
ret = entropy_get_entropy(entropy_dev, (uint8_t *)output, request_len);
78-
if (ret < 0) {
79-
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
80-
}
81-
82-
*olen = request_len;
83-
84-
return 0;
85-
}
86-
#endif /* CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR */
87-
8850
static int _mbedtls_init(void)
8951
{
9052

@@ -122,25 +84,67 @@ mbedtls_ms_time_t mbedtls_ms_time(void)
12284
return (mbedtls_ms_time_t)k_uptime_get();
12385
}
12486

87+
#if defined(CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR) || defined(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
88+
static int get_random_data(uint8_t *output, size_t output_size, bool allow_non_cs)
89+
{
90+
int ret = MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED;
91+
92+
#if defined(CONFIG_CSPRNG_ENABLED)
93+
ret = sys_csrand_get(output, output_size);
94+
if (ret == 0) {
95+
return 0;
96+
}
97+
#endif /* CONFIG_CSPRNG_ENABLED */
98+
99+
if (allow_non_cs) {
100+
sys_rand_get(output, output_size);
101+
ret = 0;
102+
}
103+
104+
return ret;
105+
}
106+
#endif /* CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR || CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
107+
108+
#if defined(CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR)
109+
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len,
110+
size_t *olen)
111+
{
112+
int ret;
113+
uint16_t request_len = len > UINT16_MAX ? UINT16_MAX : len;
114+
115+
ARG_UNUSED(data);
116+
117+
if (output == NULL || olen == NULL || len == 0) {
118+
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
119+
}
120+
121+
ret = get_random_data(output, len, true);
122+
if (ret < 0) {
123+
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
124+
}
125+
126+
*olen = request_len;
127+
128+
return 0;
129+
}
130+
#endif /* CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR */
131+
125132
#if defined(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
126-
/* MBEDTLS_PSA_CRYPTO_C requires a random generator to work and this can
127-
* be achieved through either legacy MbedTLS modules
128-
* (ENTROPY + CTR_DRBG/HMAC_DRBG) or provided externally by enabling the
129-
* CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG. In the latter case the following
130-
* callback functions needs to be defined.
131-
*/
132133
psa_status_t mbedtls_psa_external_get_random(
133134
mbedtls_psa_external_random_context_t *context,
134135
uint8_t *output, size_t output_size, size_t *output_length)
135136
{
136137
(void) context;
138+
int ret;
137139

138-
if (sys_csrand_get(output, output_size) != 0) {
140+
ret = get_random_data(output, output_size,
141+
IS_ENABLED(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG));
142+
if (ret != 0) {
139143
return PSA_ERROR_GENERIC_ERROR;
140144
}
141145

142146
*output_length = output_size;
143147

144148
return PSA_SUCCESS;
145149
}
146-
#endif
150+
#endif /* CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */

0 commit comments

Comments
 (0)