Skip to content

Commit e26f00f

Browse files
valeriosettinashif
authored andcommitted
mbedtls: move entropy polling functions to a dedicated file
This commit just moves some code around. For sake of cleanliness a new file is added to hold the entropy parsing functions for Mbed TLS. Signed-off-by: Valerio Setti <[email protected]>
1 parent 5ef44fd commit e26f00f

File tree

3 files changed

+78
-70
lines changed

3 files changed

+78
-70
lines changed

modules/mbedtls/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ zephyr_interface_library_named(mbedTLS)
115115
${ZEPHYR_CURRENT_MODULE_DIR}/library/version_features.c
116116
${ZEPHYR_CURRENT_MODULE_DIR}/library/version.c
117117
zephyr_init.c
118+
zephyr_entropy.c
118119
)
119120

120121
zephyr_library_sources(${mbedtls_base_src})

modules/mbedtls/zephyr_entropy.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/random/random.h>
8+
#include <mbedtls/entropy.h>
9+
#include <psa/crypto.h>
10+
11+
12+
#if defined(CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR) || defined(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
13+
static int get_random_data(uint8_t *output, size_t output_size, bool allow_non_cs)
14+
{
15+
int ret = MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED;
16+
17+
#if defined(CONFIG_CSPRNG_ENABLED)
18+
ret = sys_csrand_get(output, output_size);
19+
if (ret == 0) {
20+
return 0;
21+
}
22+
#endif /* CONFIG_CSPRNG_ENABLED */
23+
24+
if (allow_non_cs) {
25+
sys_rand_get(output, output_size);
26+
ret = 0;
27+
}
28+
29+
return ret;
30+
}
31+
#endif /* CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR || CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
32+
33+
#if defined(CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR)
34+
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len,
35+
size_t *olen)
36+
{
37+
int ret;
38+
uint16_t request_len = len > UINT16_MAX ? UINT16_MAX : len;
39+
40+
ARG_UNUSED(data);
41+
42+
if (output == NULL || olen == NULL || len == 0) {
43+
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
44+
}
45+
46+
ret = get_random_data(output, len, true);
47+
if (ret < 0) {
48+
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
49+
}
50+
51+
*olen = request_len;
52+
53+
return 0;
54+
}
55+
#endif /* CONFIG_MBEDTLS_ENTROPY_POLL_ZEPHYR */
56+
57+
#if defined(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
58+
psa_status_t mbedtls_psa_external_get_random(
59+
mbedtls_psa_external_random_context_t *context,
60+
uint8_t *output, size_t output_size, size_t *output_length)
61+
{
62+
(void) context;
63+
int ret;
64+
65+
ret = get_random_data(output, output_size,
66+
IS_ENABLED(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG));
67+
if (ret != 0) {
68+
return PSA_ERROR_GENERIC_ERROR;
69+
}
70+
71+
*output_length = output_size;
72+
73+
return PSA_SUCCESS;
74+
}
75+
#endif /* CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */

modules/mbedtls/zephyr_init.c

+2-70
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66

77
/*
88
* Copyright (c) 2017 Intel Corporation
9+
* Copyright (c) 2024 Nordic Semiconductor ASA
910
*
1011
* SPDX-License-Identifier: Apache-2.0
1112
*/
1213

1314
#include <zephyr/init.h>
1415
#include <zephyr/app_memory/app_memdomain.h>
15-
#include <zephyr/drivers/entropy.h>
16-
#include <zephyr/random/random.h>
17-
#include <mbedtls/entropy.h>
1816
#include <mbedtls/platform_time.h>
1917

20-
2118
#include <mbedtls/debug.h>
2219

2320
#if defined(CONFIG_MBEDTLS)
@@ -29,7 +26,7 @@
2926
#endif
3027

3128
#if defined(CONFIG_MBEDTLS_ENABLE_HEAP) && \
32-
defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
29+
defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
3330
#include <mbedtls/memory_buffer_alloc.h>
3431

3532
#if !defined(CONFIG_MBEDTLS_HEAP_SIZE)
@@ -83,68 +80,3 @@ mbedtls_ms_time_t mbedtls_ms_time(void)
8380
{
8481
return (mbedtls_ms_time_t)k_uptime_get();
8582
}
86-
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-
132-
#if defined(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
133-
psa_status_t mbedtls_psa_external_get_random(
134-
mbedtls_psa_external_random_context_t *context,
135-
uint8_t *output, size_t output_size, size_t *output_length)
136-
{
137-
(void) context;
138-
int ret;
139-
140-
ret = get_random_data(output, output_size,
141-
IS_ENABLED(CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG));
142-
if (ret != 0) {
143-
return PSA_ERROR_GENERIC_ERROR;
144-
}
145-
146-
*output_length = output_size;
147-
148-
return PSA_SUCCESS;
149-
}
150-
#endif /* CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */

0 commit comments

Comments
 (0)