From 1552324a64ce4250359287c13e7c9f789f8dcf74 Mon Sep 17 00:00:00 2001 From: Peter Harper <77111776+peterharperuk@users.noreply.github.com> Date: Fri, 27 Jan 2023 17:15:22 +0000 Subject: [PATCH] Add mbedtls_hardware_poll implementation to pico_mbedtls --- src/rp2_common/pico_mbedtls/CMakeLists.txt | 3 ++- src/rp2_common/pico_mbedtls/pico_mbedtls.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/rp2_common/pico_mbedtls/pico_mbedtls.c diff --git a/src/rp2_common/pico_mbedtls/CMakeLists.txt b/src/rp2_common/pico_mbedtls/CMakeLists.txt index 0d9685b..d2c7f3a 100644 --- a/src/rp2_common/pico_mbedtls/CMakeLists.txt +++ b/src/rp2_common/pico_mbedtls/CMakeLists.txt @@ -130,12 +130,13 @@ if (EXISTS ${PICO_MBEDTLS_PATH}/${MBEDTLS_TEST_PATH}) target_sources(pico_mbedtls_tls INTERFACE ${src_tls}) pico_add_impl_library(pico_mbedtls) - target_link_libraries(pico_mbedtls INTERFACE pico_mbedtls_crypto pico_mbedtls_x509 pico_mbedtls_tls) + target_link_libraries(pico_mbedtls INTERFACE pico_mbedtls_crypto pico_mbedtls_x509 pico_mbedtls_tls pico_rand) if (DEFINED PICO_MBEDTLS_CONFIG_FILE) target_compile_definitions(pico_mbedtls INTERFACE MBEDTLS_CONFIG_FILE="${PICO_MBEDTLS_CONFIG_FILE}") else() target_compile_definitions(pico_mbedtls INTERFACE MBEDTLS_CONFIG_FILE="mbedtls_config.h") endif() + target_sources(pico_mbedtls INTERFACE ${CMAKE_CURRENT_LIST_DIR}/pico_mbedtls.c) target_include_directories(pico_mbedtls INTERFACE ${PICO_MBEDTLS_PATH}/include/ ${PICO_MBEDTLS_PATH}/library/) function(suppress_mbedtls_warnings) diff --git a/src/rp2_common/pico_mbedtls/pico_mbedtls.c b/src/rp2_common/pico_mbedtls/pico_mbedtls.c new file mode 100644 index 0000000..5878956 --- /dev/null +++ b/src/rp2_common/pico_mbedtls/pico_mbedtls.c @@ -0,0 +1,15 @@ +#include +#include "pico/platform.h" +#include "pico/rand.h" + +/* Function to feed mbedtls entropy. */ +int mbedtls_hardware_poll(void *data __unused, unsigned char *output, size_t len, size_t *olen) { + *olen = 0; + while(*olen < len) { + uint64_t rand_data = get_rand_64(); + size_t to_copy = MIN(len, sizeof(rand_data)); + memcpy(output + *olen, &rand_data, to_copy); + *olen += to_copy; + } + return 0; +}