From f9ecfab2c1cd1ff99e615e91d42cba59bb6d0979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20H=C3=BC=C3=9Fler?= Date: Tue, 28 Mar 2023 10:43:48 +0200 Subject: [PATCH] pkg/mbedtls/init: add dedicated mbedtls auto-init module --- pkg/mbedtls/Makefile.dep | 2 ++ pkg/mbedtls/Makefile.include | 5 +++ pkg/mbedtls/contrib/threading_alt.c | 2 +- pkg/mbedtls/include/threading_alt.h | 5 +++ pkg/mbedtls/init/Makefile | 3 ++ pkg/mbedtls/init/auto_init_mbedtls.c | 49 ++++++++++++++++++++++++++++ sys/auto_init/auto_init.c | 2 +- 7 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 pkg/mbedtls/init/Makefile create mode 100644 pkg/mbedtls/init/auto_init_mbedtls.c diff --git a/pkg/mbedtls/Makefile.dep b/pkg/mbedtls/Makefile.dep index ef023a8770..6a3409bb49 100644 --- a/pkg/mbedtls/Makefile.dep +++ b/pkg/mbedtls/Makefile.dep @@ -1,3 +1,5 @@ +DEFAULT_MODULE += auto_init_mbedtls + ifneq (,$(filter mbedtls_%,$(USEMODULE))) USEMODULE += mbedtls_contrib endif diff --git a/pkg/mbedtls/Makefile.include b/pkg/mbedtls/Makefile.include index 5db4d6c1f5..8ec724f7a5 100644 --- a/pkg/mbedtls/Makefile.include +++ b/pkg/mbedtls/Makefile.include @@ -35,6 +35,11 @@ ifneq (,$(filter mbedtls_self_test,$(USEMODULE))) CFLAGS += -DCONFIG_MBEDTLS_SELF_TEST=1 endif +NO_PSEUDOMODULES += auto_init_mbedtls +ifneq (,$(filter auto_init_mbedtls,$(USEMODULE))) + DIRS += $(RIOTBASE)/pkg/mbedtls/init +endif + ifneq (,$(filter mbedtls_crypto,$(USEMODULE))) include $(RIOTPKG)/mbedtls/contrib/crypto/Makefile.include endif diff --git a/pkg/mbedtls/contrib/threading_alt.c b/pkg/mbedtls/contrib/threading_alt.c index 4de02e0829..eed3c9f392 100644 --- a/pkg/mbedtls/contrib/threading_alt.c +++ b/pkg/mbedtls/contrib/threading_alt.c @@ -47,7 +47,7 @@ int mbedtls_platform_mutex_unlock(mbedtls_threading_mutex_t *mutex) return 0; } -void auto_init_mbedtls(void) +void threading_mbedtls_riot_init(void) { /* Configure mbedTLS to use RIOT specific threading functions. */ mbedtls_threading_set_alt( mbedtls_platform_mutex_init, diff --git a/pkg/mbedtls/include/threading_alt.h b/pkg/mbedtls/include/threading_alt.h index 3d442127a3..046b25ed69 100644 --- a/pkg/mbedtls/include/threading_alt.h +++ b/pkg/mbedtls/include/threading_alt.h @@ -35,6 +35,11 @@ typedef struct { mutex_t riot_mutex; /**< RIOT mutex structure */ } mbedtls_threading_mutex_t; +/** + * @brief Initialize alternative threading API of RIOT + */ +void threading_mbedtls_riot_init(void); + #endif /* MBEDTLS_THREADING_ALT */ #ifdef __cplusplus diff --git a/pkg/mbedtls/init/Makefile b/pkg/mbedtls/init/Makefile new file mode 100644 index 0000000000..489e68f609 --- /dev/null +++ b/pkg/mbedtls/init/Makefile @@ -0,0 +1,3 @@ +MODULE = auto_init_mbedtls + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/mbedtls/init/auto_init_mbedtls.c b/pkg/mbedtls/init/auto_init_mbedtls.c new file mode 100644 index 0000000000..28caa0cd69 --- /dev/null +++ b/pkg/mbedtls/init/auto_init_mbedtls.c @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2021 HAW Hamburg + * 2023 ML!PA Consulting Gmbh + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_auto_init + * @{ + * @file + * @brief Initializes RIOT mbedtls modue + * + * @author Peter Kietzmann + * @author Fabian Hüßler + * @} + */ + +#include "modules.h" +#include "log.h" + +#if IS_USED(MODULE_MBEDTLS_THREADING) +#include "mbedtls/threading.h" +#include "threading_alt.h" +#endif +#if IS_USED(MODULE_MBEDTLS_ENTROPY) +#include "entropy_mbedtls_riot.h" +#endif +#if IS_USED(MODULE_MBEDTLS_RANDOM) +#include "random_mbedtls_riot.h" +#endif + +#define ENABLE_DEBUG 0 +#include "debug.h" + +void auto_init_mbedtls(void) +{ +#if IS_USED(MODULE_MBEDTLS_THREADING_ALT) + threading_mbedtls_riot_init(); +#endif +#if IS_USED(MODULE_MBEDTLS_ENTROPY) + entropy_mbedtls_riot_init(); +#endif +#if IS_USED(MODULE_MBEDTLS_RANDOM) + random_mbedtls_riot_init(); +#endif +} diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index 8f2212c336..dcd1c94023 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -272,7 +272,7 @@ extern void suit_init_conditions(void); AUTO_INIT(suit_init_conditions, AUTO_INIT_PRIO_MOD_SUIT); #endif -#if IS_USED(MODULE_MBEDTLS) +#if IS_USED(MODULE_AUTO_INIT_MBEDTLS) extern void auto_init_mbedtls(void); AUTO_INIT(auto_init_mbedtls, AUTO_INIT_PRIO_MOD_MBEDTLS);