Skip to content

Commit 1bbce9c

Browse files
robert-hhdpgeorge
authored andcommitted
extmod/mbedtls: Factor out mbedtls_hardware_poll() to common code.
Using the new, common function `mp_hal_get_random()`. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 8a1f050 commit 1bbce9c

7 files changed

Lines changed: 21 additions & 66 deletions

File tree

extmod/mbedtls/mbedtls_alt.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,20 @@ int __wrap_mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
8686
return ret;
8787
}
8888
#endif
89+
90+
#include "py/mpconfig.h"
91+
92+
#if MICROPY_PY_SSL && MICROPY_SSL_MBEDTLS
93+
#include "mbedtls/mbedtls_config_port.h"
94+
95+
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
96+
#include "py/mphal.h"
97+
98+
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
99+
*olen = len;
100+
mp_hal_get_random(len, output);
101+
return 0;
102+
}
103+
#endif
104+
105+
#endif

ports/alif/mbedtls/mbedtls_port.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@
2828
#include "se_services.h"
2929
#include "mbedtls_config_port.h"
3030

31-
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
32-
uint32_t val = 0;
33-
int n = 0;
34-
*olen = len;
35-
while (len--) {
36-
if (!n) {
37-
val = se_services_rand64();
38-
n = 4;
39-
}
40-
*output++ = val;
41-
val >>= 8;
42-
--n;
43-
}
44-
return 0;
45-
}
46-
4731
#if defined(MBEDTLS_HAVE_TIME)
4832

4933
time_t alif_mbedtls_time(time_t *timer) {

ports/mimxrt/mbedtls/mbedtls_port.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@
3636
#include "mbedtls/platform_time.h"
3737
#endif
3838

39-
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
40-
*olen = len;
41-
mp_hal_get_random(len, output);
42-
43-
return 0;
44-
}
45-
4639
#if defined(MBEDTLS_HAVE_TIME)
4740
time_t mimxrt_rtctime_seconds(time_t *timer) {
4841
// Get date and date in CPython order.

ports/renesas-ra/mbedtls/mbedtls_port.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@
3333
#include "mbedtls/platform_time.h"
3434
#endif
3535

36-
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
37-
uint32_t val = 0;
38-
int n = 0;
39-
*olen = len;
40-
while (len--) {
41-
if (!n) {
42-
val = rng_read();
43-
n = 4;
44-
}
45-
*output++ = val;
46-
val >>= 8;
47-
--n;
48-
}
49-
return 0;
50-
}
51-
5236
#if defined(MBEDTLS_HAVE_TIME)
5337
time_t ra_rtctime_seconds(time_t *timer) {
5438
rtc_init_finalise();

ports/rp2/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ if (MICROPY_PY_NETWORK_WIZNET5K)
477477
)
478478
endif()
479479
480+
list(APPEND MICROPY_SOURCE_EXTMOD
481+
${MICROPY_EXTMOD_DIR}/mbedtls/mbedtls_alt.c
482+
)
483+
480484
# Add qstr sources for extmod and usermod, in case they are modified by components above.
481485
list(APPEND MICROPY_SOURCE_QSTR
482486
${MICROPY_SOURCE_EXTMOD}

ports/rp2/mbedtls/mbedtls_port.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@
3535
#include "mbedtls/platform_time.h"
3636
#include "pico/aon_timer.h"
3737

38-
#include "pico/rand.h"
39-
40-
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
41-
*olen = len;
42-
for (size_t i = 0; i < len; i += 8) {
43-
uint64_t rand64 = get_rand_64();
44-
memcpy(output + i, &rand64, MIN(len - i, 8));
45-
}
46-
return 0;
47-
}
48-
4938
time_t rp2_rtctime_seconds(time_t *timer) {
5039
struct timespec ts;
5140
aon_timer_get_time(&ts);

ports/stm32/mbedtls/mbedtls_port.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@
3333
#include "mbedtls/platform_time.h"
3434
#endif
3535

36-
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
37-
uint32_t val = 0;
38-
int n = 0;
39-
*olen = len;
40-
while (len--) {
41-
if (!n) {
42-
val = rng_get();
43-
n = 4;
44-
}
45-
*output++ = val;
46-
val >>= 8;
47-
--n;
48-
}
49-
return 0;
50-
}
51-
5236
#if defined(MBEDTLS_HAVE_TIME)
5337
time_t stm32_rtctime_seconds(time_t *timer) {
5438
rtc_init_finalise();

0 commit comments

Comments
 (0)