Skip to content

Commit 9ce54b5

Browse files
committed
STM32: guard mbedTLS 3 entropy integration
Keep the STM32 mbedTLS integration on its existing MbedTLS 3 path while allowing the mbedTLS 4 support series to build around it. The STM32 platform does not enable MbedTLS 4 here; this only hides the legacy entropy and CTR_DRBG context code, hooks, and config disables when building against MbedTLS 4 headers. Signed-off-by: Peter M <petermm@gmail.com>
1 parent d033e9b commit 9ce54b5

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

src/platforms/stm32/src/lib/mbedtls_stm32_user_config.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@
2121
#ifndef MBEDTLS_STM32_USER_CONFIG_H
2222
#define MBEDTLS_STM32_USER_CONFIG_H
2323

24-
#define MBEDTLS_NO_PLATFORM_ENTROPY
2524
#define MBEDTLS_PLATFORM_MS_TIME_ALT
2625

2726
#undef MBEDTLS_HAVE_TIME_DATE
2827
#undef MBEDTLS_TIMING_C
2928

29+
#if !defined(MBEDTLS_VERSION_NUMBER) || MBEDTLS_VERSION_NUMBER < 0x04000000
30+
#define MBEDTLS_NO_PLATFORM_ENTROPY
31+
3032
#undef MBEDTLS_PSA_CRYPTO_C
3133
#undef MBEDTLS_PSA_CRYPTO_STORAGE_C
3234
#undef MBEDTLS_PSA_ITS_FILE_C
3335
#undef MBEDTLS_PSA_CRYPTO_CLIENT
3436
#undef MBEDTLS_PSA_INJECT_ENTROPY
3537
#undef MBEDTLS_LMS_C
38+
#endif
3639

3740
#undef MBEDTLS_FS_IO
3841

src/platforms/stm32/src/lib/stm_sys.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
#include "stm32_hal_platform.h"
2828

2929
#ifdef ATOMVM_HAS_MBEDTLS
30+
#include <mbedtls/version.h>
31+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
3032
#include <mbedtls/ctr_drbg.h>
3133
#include <mbedtls/entropy.h>
3234
#endif
35+
#endif
3336

3437
#define STM32_ATOM globalcontext_make_atom(ctx->global, ATOM_STR("\x5", "stm32"))
3538

@@ -53,11 +56,13 @@ struct STM32PlatformData
5356
struct ListHead locked_pins;
5457
#ifdef ATOMVM_HAS_MBEDTLS
5558
RNG_HandleTypeDef rng;
59+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
5660
mbedtls_entropy_context entropy_ctx;
5761
mbedtls_ctr_drbg_context random_ctx;
5862
bool entropy_is_initialized;
5963
bool random_is_initialized;
6064
#endif
65+
#endif
6166
};
6267

6368
void sys_init_icache(void);

src/platforms/stm32/src/lib/sys.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,18 @@
2727
#include <defaultatoms.h>
2828
#include <scheduler.h>
2929
#include <sys.h>
30-
#if ATOMVM_HAS_MBEDTLS
31-
#include <sys_mbedtls.h>
32-
#endif
3330

3431
#ifdef ATOMVM_HAS_MBEDTLS
3532
#include <mbedtls/platform_time.h>
3633
#include <sys_mbedtls.h>
3734

35+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
3836
/* Minimum bytes the entropy pool collects from a hardware source before
3937
* it's considered seeded. mbedTLS defines this as MBEDTLS_ENTROPY_MIN_HARDWARE
4038
* in the private library/entropy_poll.h header; use the same value. */
4139
#define STM32_ENTROPY_MIN_HARDWARE 32
4240
#endif
41+
#endif
4342

4443
// #define ENABLE_TRACE
4544
#include <trace.h>
@@ -226,7 +225,7 @@ void sys_init_platform(GlobalContext *glb)
226225
AVM_LOGE(TAG, "Out of memory!");
227226
AVM_ABORT();
228227
}
229-
#if ATOMVM_HAS_MBEDTLS
228+
#ifdef ATOMVM_HAS_MBEDTLS
230229
#if MBEDTLS_VERSION_NUMBER >= 0x04000000
231230
psa_status_t status = psa_crypto_init();
232231
if (status != PSA_SUCCESS) {
@@ -237,8 +236,10 @@ void sys_init_platform(GlobalContext *glb)
237236
glb->platform_data = platform;
238237
list_init(&platform->locked_pins);
239238
#ifdef ATOMVM_HAS_MBEDTLS
239+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
240240
platform->entropy_is_initialized = false;
241241
platform->random_is_initialized = false;
242+
#endif
242243
if (stm32_rng_hw_init(platform) != 0) {
243244
AVM_LOGE(TAG, "Failed to initialize RNG peripheral");
244245
AVM_ABORT();
@@ -250,12 +251,14 @@ void sys_free_platform(GlobalContext *glb)
250251
{
251252
struct STM32PlatformData *platform = glb->platform_data;
252253
#ifdef ATOMVM_HAS_MBEDTLS
254+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
253255
if (platform->random_is_initialized) {
254256
mbedtls_ctr_drbg_free(&platform->random_ctx);
255257
}
256258
if (platform->entropy_is_initialized) {
257259
mbedtls_entropy_free(&platform->entropy_ctx);
258260
}
261+
#endif
259262
// HAL_RNG_Init succeeded in stm32_rng_hw_init or we aborted
260263
HAL_RNG_DeInit(&platform->rng);
261264
#endif
@@ -486,6 +489,7 @@ static int stm32_rng_hw_init(struct STM32PlatformData *platform)
486489
return 0;
487490
}
488491

492+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
489493
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen)
490494
{
491495
GlobalContext *global = data;
@@ -511,12 +515,14 @@ int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t
511515
*olen = written;
512516
return 0;
513517
}
518+
#endif
514519

515520
mbedtls_ms_time_t mbedtls_ms_time(void)
516521
{
517522
return (mbedtls_ms_time_t) HAL_GetTick();
518523
}
519524

525+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
520526
int sys_mbedtls_entropy_func(void *entropy, unsigned char *buf, size_t size)
521527
{
522528
return mbedtls_entropy_func(entropy, buf, size);
@@ -572,4 +578,5 @@ void sys_mbedtls_ctr_drbg_context_unlock(GlobalContext *global)
572578
UNUSED(global);
573579
}
574580

581+
#endif
575582
#endif /* ATOMVM_HAS_MBEDTLS */

0 commit comments

Comments
 (0)