Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit e1c50ca

Browse files
authored
Merge pull request #55 from tropicsquare/ETR01SDK-379-Fix-MbedTLS
ETR01SDK-379: Fix MbedTLS
2 parents 567fe20 + 644f4ab commit e1c50ca

File tree

7 files changed

+3299
-3
lines changed

7 files changed

+3299
-3
lines changed

NUCLEO_F439ZI/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ endif()
105105
set(LT_USE_TREZOR_CRYPTO 0 CACHE INTERNAL "")
106106
set(LT_USE_MBEDTLS_V4 0 CACHE INTERNAL "")
107107

108+
# Additional implementation files required by some CFPs.
109+
# E.g., MbedTLS needs user-provided implementation of mbedtls_ms_time() and other
110+
# platform-dependent functions.
111+
set(CFP_PORT_SRCS "")
112+
108113
# Handle LT_CAL
109114
if(LT_CAL STREQUAL "trezor_crypto")
110115
message(STATUS "Crypto provider set to trezor_crypto")
@@ -129,11 +134,33 @@ elseif(LT_CAL STREQUAL "mbedtls_v4")
129134
# It's used in main.c to switch crypto contexts without manual changes
130135
set(LT_USE_MBEDTLS_V4 1)
131136

137+
# We configure MbedTLS using config file with following configuration:
138+
# - config.py preset "crypto_baremetal"
139+
# - following options enabled:
140+
# MBEDTLS_PLATFORM_MS_TIME_ALT
141+
# MBEDTLS_HAVE_TIME
142+
# MBEDTLS_PSA_DRIVER_GET_ENTROPY
143+
# - following options disabled:
144+
# MBEDTLS_PSA_BUILTIN_GET_ENTROPY
145+
# MBEDTLS_TEST_HOOKS
146+
# MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS
147+
# We need to set both CMake variables and compile definitions for MbedTLS to pick up our config file.
148+
set(MBEDTLS_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/Src/mbedtls_v4/mbedtls_config.h")
149+
set(TF_PSA_CRYPTO_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/Src/mbedtls_v4/crypto_config.h")
150+
add_compile_definitions(MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}")
151+
add_compile_definitions(TF_PSA_CRYPTO_CONFIG_FILE="${TF_PSA_CRYPTO_CONFIG_FILE}")
132152
set(ENABLE_TESTING OFF CACHE BOOL "Disable mbedtls_v4 test building.")
133153
set(ENABLE_PROGRAMS OFF CACHE BOOL "Disable mbedtls_v4 examples building.")
134154
add_subdirectory("${PATH_VENDOR}mbedtls_v4/" "mbedtls_v4")
135155

136156
target_link_libraries(tropic PUBLIC mbedtls)
157+
158+
# MbedTLS needs platform-specific implementations of some functions,
159+
# as it does not provide them for STM32.
160+
# Here we provide time function implementation for STM32 using standard STM32 HAL.
161+
set(CFP_PORT_SRCS
162+
${CMAKE_CURRENT_SOURCE_DIR}/Src/mbedtls_v4/mbedtls_platform.c
163+
)
137164
else()
138165
get_property(lt_cal_choices CACHE LT_CAL PROPERTY STRINGS)
139166
message(FATAL_ERROR "Incorrect CAL set to LT_CAL!\nSupported CALs: ${lt_cal_choices}")
@@ -175,6 +202,9 @@ set(SOURCES
175202

176203
# Libtropic HAL sources
177204
${LT_HAL_SRCS}
205+
206+
# CFP port sources
207+
${CFP_PORT_SRCS}
178208
)
179209

180210
# Include path for directories containing header files

NUCLEO_F439ZI/Inc/main.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <stdio.h>
2525

2626
#include "stm32f4xx_hal.h"
27+
#include "stm32f4xx_hal_rng.h"
2728
#include "stm32f4xx_nucleo_144.h"
2829

2930
/* Exported types ------------------------------------------------------------*/
@@ -106,4 +107,7 @@
106107
/* Exported macro ------------------------------------------------------------*/
107108
/* Exported functions ------------------------------------------------------- */
108109

110+
/* Exported variables ------------------------------------------------------- */
111+
extern RNG_HandleTypeDef RNGHandle;
112+
109113
#endif /* __MAIN_H */

NUCLEO_F439ZI/Src/main.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/* Includes ------------------------------------------------------------------*/
2121
#include "main.h"
2222

23+
#include <inttypes.h>
2324
#include <string.h>
2425

2526
#include "libtropic_examples.h"
@@ -66,6 +67,9 @@ static void Error_Handler(void);
6667
/* UART handle declaration */
6768
static UART_HandleTypeDef UartHandle;
6869

70+
/* RNG handle declaration */
71+
RNG_HandleTypeDef RNGHandle;
72+
6973
/**
7074
* @brief Configures the UART peripheral
7175
* Put the USART peripheral in the Asynchronous mode (UART Mode)
@@ -140,6 +144,14 @@ int main(void)
140144
Error_Handler();
141145
}
142146

147+
// IMPORTANT: Initialize RNG peripheral.
148+
// Do not forget to do this in your application, as the
149+
// Libtropic HAL uses RNG for entropy source!
150+
RNGHandle.Instance = RNG;
151+
if (HAL_RNG_Init(&RNGHandle) != HAL_OK) {
152+
Error_Handler();
153+
}
154+
143155
// libtropic related code BEGIN
144156
// libtropic related code BEGIN
145157
// libtropic related code BEGIN
@@ -157,7 +169,7 @@ int main(void)
157169
#if LT_USE_MBEDTLS_V4
158170
psa_status_t status = psa_crypto_init();
159171
if (status != PSA_SUCCESS) {
160-
LT_LOG_ERROR("PSA Crypto initialization failed, status=%d (psa_status_t)", status);
172+
LT_LOG_ERROR("PSA Crypto initialization failed, status=%" PRId32 " (psa_status_t)", status);
161173
Error_Handler();
162174
}
163175
#endif
@@ -193,7 +205,9 @@ int main(void)
193205
device.spi_cs_gpio_bank = LT_SPI_CS_BANK;
194206
device.spi_cs_gpio_pin = LT_SPI_CS_PIN;
195207

196-
device.rng_handle.Instance = RNG;
208+
// IMPORTANT: Do not forget to initialize RNG peripheral
209+
// at the beginning of your application using HAL_RNG_Init()!
210+
device.rng_handle = &RNGHandle;
197211

198212
#ifdef LT_USE_INT_PIN
199213
device.int_gpio_bank = LT_INT_BANK;
@@ -253,6 +267,12 @@ int main(void)
253267
// libtropic related code END
254268
// libtropic related code END
255269

270+
// Not strictly necessary, but we deinitialize RNG here to
271+
// demonstrate proper usage.
272+
if (HAL_RNG_DeInit(&RNGHandle) != HAL_OK) {
273+
Error_Handler();
274+
}
275+
256276
while (1) {
257277
BSP_LED_On(LED2);
258278
HAL_Delay(100);

0 commit comments

Comments
 (0)