Skip to content

Commit 4cf0b86

Browse files
authored
Rework mbedTLS build system (#2248)
***NO_CI***
1 parent b611c16 commit 4cf0b86

File tree

10 files changed

+116
-70
lines changed

10 files changed

+116
-70
lines changed

CMake/Modules/FindNF_Network.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
# See LICENSE file in the project root for full license information.
44
#
55

6+
include(FetchContent)
7+
FetchContent_GetProperties(mbedtls)
8+
69
# set include directories for nanoFramework network
710
list(APPEND NF_Network_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets)
811
list(APPEND NF_Network_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl)
912
list(APPEND NF_Network_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/PAL/Lwip)
13+
list(APPEND NF_Network_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/PAL)
1014
list(APPEND NF_Network_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/DeviceInterfaces/Networking.Sntp)
1115

1216
if(USE_SECURITY_MBEDTLS_OPTION)
1317
list(APPEND NF_Network_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS)
18+
list(APPEND NF_Network_INCLUDE_DIRS ${mbedtls_SOURCE_DIR}/include)
1419
endif()
1520

1621
if(USE_ENC28J60_DRIVER_OPTION)
@@ -181,15 +186,13 @@ macro(nf_add_lib_network)
181186
add_library(
182187
${LIB_NAME} STATIC
183188
${NF_Network_SOURCES}
184-
${mbedTLS_SOURCES}
185189
${NFALN_EXTRA_SOURCES})
186190

187191
target_include_directories(
188192
${LIB_NAME}
189193
PUBLIC
190194
${NF_Network_INCLUDE_DIRS}
191195
${NF_CoreCLR_INCLUDE_DIRS}
192-
${mbedTLS_INCLUDE_DIRS}
193196
${NFALN_EXTRA_INCLUDES})
194197

195198
# TODO can be removed later

CMake/binutils.ChibiOS.cmake

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ macro(nf_add_platform_packages)
103103
find_package(NF_Network REQUIRED QUIET)
104104
find_package(CHIBIOS_LWIP REQUIRED QUIET)
105105

106-
# security provider is mbedTLS
107-
if(USE_SECURITY_MBEDTLS_OPTION)
108-
find_package(mbedTLS REQUIRED QUIET)
109-
endif()
110-
111106
endif()
112107

113108
endif()
@@ -199,7 +194,12 @@ macro(nf_add_platform_dependencies target)
199194
${${TARGET_STM32_CUBE_PACKAGE}_CubePackage_INCLUDE_DIRS}
200195
EXTRA_COMPILE_DEFINITIONS -DHAL_USE_MAC=TRUE)
201196

202-
add_dependencies(${target}.elf nano::NF_Network)
197+
add_dependencies(${target}.elf nano::NF_Network)
198+
199+
# security provider is mbedTLS
200+
if(USE_SECURITY_MBEDTLS_OPTION)
201+
add_dependencies(NF_Network nano::NF_Network)
202+
endif()
203203

204204
endif()
205205

@@ -244,6 +244,23 @@ macro(nf_add_platform_include_directories target)
244244
${CHIBIOS_FATFS_INCLUDE_DIRS}
245245
)
246246

247+
248+
if(USE_SECURITY_MBEDTLS_OPTION)
249+
250+
# need to add extra include directories for mbedTLS
251+
target_include_directories(
252+
mbedcrypto PUBLIC
253+
${CHIBIOS_HAL_INCLUDE_DIRS}
254+
${CHIBIOS_INCLUDE_DIRS}
255+
${ChibiOSnfOverlay_INCLUDE_DIRS}
256+
${CHIBIOS_CONTRIB_INCLUDE_DIRS}
257+
${${TARGET_STM32_CUBE_PACKAGE}_CubePackage_INCLUDE_DIRS}
258+
${TARGET_CHIBIOS_COMMON_INCLUDE_DIRS}
259+
${CHIBIOS_LWIP_INCLUDE_DIRS}
260+
)
261+
262+
endif()
263+
247264
endif()
248265

249266
endmacro()
@@ -299,15 +316,17 @@ macro(nf_add_platform_sources target)
299316
target_link_libraries(${target}.elf
300317
nano::NF_Network
301318
)
302-
endif()
303319

304-
endif()
320+
if(USE_SECURITY_MBEDTLS_OPTION)
321+
target_link_libraries(${target}.elf
322+
mbedtls
323+
)
324+
325+
add_dependencies(NF_Network mbedtls)
326+
endif()
327+
328+
endif()
305329

306-
# mbed TLS requires a config file
307-
if(USE_SECURITY_MBEDTLS_OPTION)
308-
# this seems to be only option to properly set a compiler define through the command line that needs to be a string literal
309-
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBEDTLS_CONFIG_FILE=\"<${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/nf_mbedtls_config.h>\"")
310-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBEDTLS_CONFIG_FILE=\"<${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/nf_mbedtls_config.h>\"")
311330
endif()
312331

313332
endmacro()

CMake/binutils.ESP32.cmake

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,6 @@ macro(nf_add_platform_packages)
102102

103103
find_package(NF_Network REQUIRED QUIET)
104104

105-
# security provider is mbedTLS
106-
if(USE_SECURITY_MBEDTLS_OPTION)
107-
find_package(mbedTLS REQUIRED QUIET)
108-
endif()
109-
110105
endif()
111106

112107
endif()
@@ -248,13 +243,6 @@ macro(nf_add_platform_sources target)
248243

249244
endif()
250245

251-
# mbed TLS requires a config file
252-
if(USE_SECURITY_MBEDTLS_OPTION)
253-
# this seems to be only option to properly set a compiler define through the command line that needs to be a string literal
254-
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBEDTLS_CONFIG_FILE=\"<${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/nf_mbedtls_config.h>\"")
255-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBEDTLS_CONFIG_FILE=\"<${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/nf_mbedtls_config.h>\"")
256-
endif()
257-
258246
endmacro()
259247

260248
# macro to setup the build for a target

CMake/binutils.FreeRTOS.cmake

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ macro(nf_add_platform_packages)
7070
find_package(NF_Network REQUIRED QUIET)
7171
find_package(LWIP REQUIRED QUIET)
7272

73-
# security provider is mbedTLS
74-
if(USE_SECURITY_MBEDTLS_OPTION)
75-
find_package(mbedTLS REQUIRED QUIET)
76-
endif()
77-
7873
endif()
7974

8075
endif()

CMake/binutils.common.cmake

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,45 @@ macro(nf_setup_target_build_common)
550550
# set compile options
551551
nf_set_compile_options(TARGET ${NANOCLR_PROJECT_NAME}.elf EXTRA_COMPILE_OPTIONS ${NFSTBC_CLR_EXTRA_COMPILE_OPTIONS})
552552

553+
if(USE_SECURITY_MBEDTLS_OPTION)
554+
555+
# mbedTLS requires setting a compiler definition in order to pass a config file
556+
target_compile_definitions(mbedcrypto PUBLIC "-DMBEDTLS_CONFIG_FILE=\"${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/nf_mbedtls_config.h\"")
557+
558+
# need to add extra include directories for mbedTLS
559+
target_include_directories(
560+
mbedcrypto PUBLIC
561+
${CMAKE_SOURCE_DIR}/src/CLR/Include
562+
${CMAKE_SOURCE_DIR}/src/HAL/Include
563+
${CMAKE_SOURCE_DIR}/src/PAL
564+
${CMAKE_SOURCE_DIR}/src/PAL/Include
565+
${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets
566+
${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS
567+
${CMAKE_SOURCE_DIR}/src/DeviceInterfaces/Networking.Sntp
568+
${CMAKE_SOURCE_DIR}/targets/${RTOS}/_include
569+
${CMAKE_SOURCE_DIR}/targets/${RTOS}/${TARGET_BOARD}/nanoCLR
570+
${CMAKE_BINARY_DIR}/targets/${RTOS}/${TARGET_BOARD}
571+
${CMAKE_BINARY_DIR}/targets/${RTOS}/${TARGET_BOARD}/nanoCLR
572+
${TARGET_BASE_LOCATION}
573+
)
574+
575+
# target_sources(mbedcrypto PUBLIC ${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/ssl_generic.cpp)
576+
# target_sources(mbedcrypto PRIVATE ${CMAKE_SOURCE_DIR}/src/PAL/COM/sockets/ssl/mbedTLS/mbed_network.c)
577+
578+
if(NOT RTOS_ESP32_CHECK)
579+
# platform implementation of hardware random provider
580+
target_sources(mbedcrypto PRIVATE ${BASE_PATH_FOR_CLASS_LIBRARIES_MODULES}/mbedtls_entropy_hardware_pool.c)
581+
endif()
582+
583+
nf_set_compile_options(TARGET mbedcrypto BUILD_TARGET ${NANOCLR_PROJECT_NAME})
584+
nf_set_compile_options(TARGET mbedx509 BUILD_TARGET ${NANOCLR_PROJECT_NAME})
585+
nf_set_compile_options(TARGET mbedtls BUILD_TARGET ${NANOCLR_PROJECT_NAME})
586+
nf_set_compile_definitions(TARGET mbedcrypto BUILD_TARGET ${NANOCLR_PROJECT_NAME})
587+
nf_set_compile_definitions(TARGET mbedx509 BUILD_TARGET ${NANOCLR_PROJECT_NAME})
588+
nf_set_compile_definitions(TARGET mbedtls BUILD_TARGET ${NANOCLR_PROJECT_NAME})
589+
590+
endif()
591+
553592
# set compile definitions
554593
nf_set_compile_definitions(TARGET ${NANOCLR_PROJECT_NAME}.elf EXTRA_COMPILE_DEFINITIONS ${NFSTBC_CLR_EXTRA_COMPILE_DEFINITIONS} BUILD_TARGET ${NANOCLR_PROJECT_NAME} )
555594

src/PAL/COM/sockets/ssl/mbedTLS/mbedtls.h

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
#define MBEDTLS_H
88

99
#include <nanoHAL.h>
10+
#include "nf_mbedtls_config.h"
1011

1112
#ifdef PLATFORM_ESP32
1213
#include <esp32_idf.h>
1314
#endif
1415

1516
#include <sockets_lwip.h>
1617

18+
#ifdef __cplusplus
19+
extern "C"
20+
{
21+
#endif
22+
1723
#include "mbedtls/platform.h"
1824
#include "mbedtls/net_sockets.h"
1925
#include "mbedtls/ssl.h"
@@ -22,32 +28,28 @@
2228
#include "mbedtls/error.h"
2329
#include "mbedtls/certs.h"
2430

25-
typedef struct mbedTLS_NFContext
26-
{
27-
mbedtls_entropy_context* entropy;
28-
mbedtls_ctr_drbg_context* ctr_drbg;
29-
mbedtls_ssl_config* conf;
30-
mbedtls_ssl_context* ssl;
31-
mbedtls_net_context* server_fd;
32-
mbedtls_x509_crt* x509_crt;
33-
mbedtls_pk_context* pk;
34-
}mbedTLS_NFContext;
35-
36-
int net_would_block( const mbedtls_net_context *ctx );
37-
int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len );
38-
int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len );
39-
int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, uint32_t timeout );
40-
void mbedtls_net_free( mbedtls_net_context *ctx );
41-
42-
// debug output declaration
43-
void nf_debug( void *ctx, int level, const char *file, int line, const char *str );
44-
45-
#ifdef __cplusplus
46-
extern "C" {
47-
#endif
48-
49-
// function returning the system date and time in Unix Epoch
50-
time_t nf_get_unix_epoch();
31+
typedef struct mbedTLS_NFContext
32+
{
33+
mbedtls_entropy_context *entropy;
34+
mbedtls_ctr_drbg_context *ctr_drbg;
35+
mbedtls_ssl_config *conf;
36+
mbedtls_ssl_context *ssl;
37+
mbedtls_net_context *server_fd;
38+
mbedtls_x509_crt *x509_crt;
39+
mbedtls_pk_context *pk;
40+
} mbedTLS_NFContext;
41+
42+
int net_would_block(const mbedtls_net_context *ctx);
43+
int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len);
44+
int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len);
45+
int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len, uint32_t timeout);
46+
void mbedtls_net_free(mbedtls_net_context *ctx);
47+
48+
// debug output declaration
49+
void nf_debug(void *ctx, int level, const char *file, int line, const char *str);
50+
51+
// function returning the system date and time in Unix Epoch
52+
time_t nf_get_unix_epoch();
5153

5254
#ifdef __cplusplus
5355
}

src/PAL/COM/sockets/ssl/mbedTLS/nf_mbedtls_config.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,13 @@ extern "C"
105105
/* mbed TLS modules */
106106
#define MBEDTLS_AESNI_C
107107
#define MBEDTLS_AES_C
108-
#define MBEDTLS_ARC4_C
109108
#define MBEDTLS_ASN1_PARSE_C
110109
#define MBEDTLS_ASN1_WRITE_C
111110

112111
#define MBEDTLS_BASE64_C
113112

114113
#define MBEDTLS_BIGNUM_C
115114

116-
#define MBEDTLS_BLOWFISH_C
117-
118-
#define MBEDTLS_CAMELLIA_C
119115
#define MBEDTLS_CCM_C
120116

121117
#define MBEDTLS_CIPHER_C
@@ -135,8 +131,6 @@ extern "C"
135131

136132
#define MBEDTLS_GCM_C
137133

138-
#define MBEDTLS_HKDF_C
139-
140134
#define MBEDTLS_HMAC_DRBG_C
141135

142136
#define MBEDTLS_MD_C
@@ -156,8 +150,6 @@ extern "C"
156150

157151
#define MBEDTLS_PLATFORM_C
158152

159-
#define MBEDTLS_POLY1305_C
160-
161153
#define MBEDTLS_RIPEMD160_C
162154

163155
#define MBEDTLS_RSA_C

src/PAL/Include/CPU_GPIO_decl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef DRIVERS_GPIO_DECL_H
88
#define DRIVERS_GPIO_DECL_H
99

10+
#include <nanoPackStruct.h>
11+
1012
#define GPIO_PIN_NONE 0xFFFFFFFF
1113

1214
#define GPIO_ATTRIBUTE_NONE 0x00

targets/ChibiOS/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,12 @@ if(NF_SECURITY_MBEDTLS)
256256
FetchContent_Populate(mbedtls)
257257
endif()
258258

259-
set(mbedTLS_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/3rdparty/humblelogging/include)
260-
set(mbedTLS_LIBRARIES ${CMAKE_SHARED_LIBRARY_PREFIX}mbedTLS${CMAKE_SHARED_LIBRARY_SUFFIX})
259+
# don't include tests or programs, only build libraries
260+
set(ENABLE_TESTING CACHE BOOL OFF)
261+
set(ENABLE_PROGRAMS CACHE BOOL OFF)
262+
263+
cmake_policy(SET CMP0048 NEW)
264+
add_subdirectory(${mbedtls_SOURCE_DIR} mbedtls_build)
261265

262266
endif()
263267

targets/ChibiOS/_nanoCLR/mbedtls_entropy_hardware_pool.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
#include <hal.h>
77
#include <hal_nf_community.h>
88

9+
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen);
10+
911
// Get len bytes of entropy from the hardware RNG.
10-
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
12+
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen)
1113
{
1214
(void)data;
1315

1416
// start random generator
1517
rngStart();
1618

17-
for(size_t i = 0; i < len; i++)
19+
for (size_t i = 0; i < len; i++)
1820
{
1921
// our generator returns 32bits numbers
2022
*output = rngGenerateRandomNumber();

0 commit comments

Comments
 (0)