From d824cf7dbd7659ec6e2f4b55648e56778b7affe3 Mon Sep 17 00:00:00 2001 From: cpq Date: Tue, 25 Jul 2023 15:03:01 +0100 Subject: [PATCH 1/2] Add mbedtls to H7 baremetal --- .../Makefile | 3 +- .../mbedtls_config.h | 2 - .../Makefile | 16 ++++-- .../mbedtls_config.h | 53 +++++++++++++++++++ 4 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 examples/stm32/nucleo-h743zi-make-baremetal-builtin/mbedtls_config.h diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/Makefile b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/Makefile index 1ac58e7c..5b0145ce 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/Makefile +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/Makefile @@ -11,7 +11,8 @@ SOURCES += cmsis_f7/Source/Templates/gcc/startup_stm32f746xx.s # ST startup file # Mongoose-specific. See https://mongoose.ws/documentation/#build-options SOURCES += mongoose.c net.c packed_fs.c CFLAGS += -DMG_ENABLE_TCPIP=1 -DMG_ARCH=MG_ARCH_NEWLIB -DMG_ENABLE_CUSTOM_MILLIS=1 -CFLAGS += -DMG_ENABLE_CUSTOM_RANDOM=1 -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_DRIVER_STM32=1 $(CFLAGS_EXTRA) +CFLAGS += -DMG_ENABLE_CUSTOM_RANDOM=1 -DMG_ENABLE_PACKED_FS=1 +CFLAGS += -DMG_ENABLE_DRIVER_STM32=1 $(CFLAGS_EXTRA) # Example specific build options. See README.md CFLAGS += -DHTTP_URL=\"http://0.0.0.0/\" -DHTTPS_URL=\"https://0.0.0.0/\" diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/mbedtls_config.h b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/mbedtls_config.h index 6e93f56d..3e8d768f 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/mbedtls_config.h +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/mbedtls_config.h @@ -7,7 +7,6 @@ #define MBEDTLS_ALLOW_PRIVATE_ACCESS #define MBEDTLS_HAVE_TIME #define MBEDTLS_SSL_SESSION_TICKETS -#define MBEDTLS_SSL_KEEP_PEER_CERTIFICATE #define MBEDTLS_CIPHER_MODE_CBC #define MBEDTLS_ECP_DP_SECP256R1_ENABLED @@ -45,7 +44,6 @@ #define MBEDTLS_SSL_TICKET_C #define MBEDTLS_SSL_PROTO_TLS1_2 -#define MBEDTLS_SSL_PROTO_TLS1_3 #define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED #define MBEDTLS_GCM_C #define MBEDTLS_ECDH_C diff --git a/examples/stm32/nucleo-h743zi-make-baremetal-builtin/Makefile b/examples/stm32/nucleo-h743zi-make-baremetal-builtin/Makefile index 25f6d308..45df45e7 100644 --- a/examples/stm32/nucleo-h743zi-make-baremetal-builtin/Makefile +++ b/examples/stm32/nucleo-h743zi-make-baremetal-builtin/Makefile @@ -10,12 +10,12 @@ SOURCES += cmsis_h7/Source/Templates/gcc/startup_stm32h743xx.s # ST startup file # Mongoose-specific. See https://mongoose.ws/documentation/#build-options SOURCES += mongoose.c net.c packed_fs.c -CFLAGS += -DMG_ENABLE_TCPIP=1 -DMG_ARCH=MG_ARCH_NEWLIB -DMG_ENABLE_PACKED_FS=1 -CFLAGS += -DMG_ENABLE_CUSTOM_RANDOM=1 -DMG_ENABLE_CUSTOM_MILLIS=1 +CFLAGS += -DMG_ENABLE_TCPIP=1 -DMG_ARCH=MG_ARCH_NEWLIB -DMG_ENABLE_CUSTOM_MILLIS=1 +CFLAGS += -DMG_ENABLE_CUSTOM_RANDOM=1 -DMG_ENABLE_PACKED_FS=1 CFLAGS += -DMG_ENABLE_DRIVER_STM32H=1 $(CFLAGS_EXTRA) # Example specific build options. See README.md -CFLAGS += -DHTTP_URL=\"http://0.0.0.0/\" +CFLAGS += -DHTTP_URL=\"http://0.0.0.0/\" -DHTTPS_URL=\"https://0.0.0.0/\" ifeq ($(OS),Windows_NT) RM = cmd /C del /Q /F /S @@ -28,7 +28,7 @@ all build example: firmware.bin firmware.bin: firmware.elf arm-none-eabi-objcopy -O binary $< $@ -firmware.elf: cmsis_core cmsis_h7 $(SOURCES) hal.h link.ld +firmware.elf: cmsis_core cmsis_h7 $(SOURCES) hal.h link.ld Makefile arm-none-eabi-gcc $(SOURCES) $(CFLAGS) $(LDFLAGS) -o $@ flash: firmware.bin @@ -38,6 +38,14 @@ cmsis_core: # ARM CMSIS core headers git clone --depth 1 -b 5.9.0 https://github.com/ARM-software/CMSIS_5 $@ cmsis_h7: # ST CMSIS headers for STM32H7 series git clone --depth 1 -b v1.10.3 https://github.com/STMicroelectronics/cmsis_device_h7 $@ +mbedtls: # mbedTLS library + git clone --depth 1 -b v2.28.2 https://github.com/mbed-tls/mbedtls $@ + +ifeq ($(TLS), mbedtls) +CFLAGS += -DMG_TLS=MG_TLS_MBED -Wno-conversion -Imbedtls/include +CFLAGS += -DMBEDTLS_CONFIG_FILE=\"mbedtls_config.h\" mbedtls/library/*.c +firmware.elf: mbedtls +endif # Automated remote test. Requires env variable VCON_API_KEY set. See https://vcon.io/automated-firmware-tests/ DEVICE_URL ?= https://dash.vcon.io/api/v3/devices/6 diff --git a/examples/stm32/nucleo-h743zi-make-baremetal-builtin/mbedtls_config.h b/examples/stm32/nucleo-h743zi-make-baremetal-builtin/mbedtls_config.h new file mode 100644 index 00000000..3e8d768f --- /dev/null +++ b/examples/stm32/nucleo-h743zi-make-baremetal-builtin/mbedtls_config.h @@ -0,0 +1,53 @@ +/* Workaround for some mbedtls source files using INT_MAX without including limits.h */ +#include + +#define MBEDTLS_NO_PLATFORM_ENTROPY +#define MBEDTLS_ENTROPY_HARDWARE_ALT +#define MBEDTLS_SSL_OUT_CONTENT_LEN 2048 +#define MBEDTLS_ALLOW_PRIVATE_ACCESS +#define MBEDTLS_HAVE_TIME +#define MBEDTLS_SSL_SESSION_TICKETS + +#define MBEDTLS_CIPHER_MODE_CBC +#define MBEDTLS_ECP_DP_SECP256R1_ENABLED +#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED +#define MBEDTLS_PKCS1_V15 +#define MBEDTLS_SHA256_SMALLER +#define MBEDTLS_SSL_SERVER_NAME_INDICATION +#define MBEDTLS_AES_C +#define MBEDTLS_ASN1_PARSE_C +#define MBEDTLS_BIGNUM_C +#define MBEDTLS_CIPHER_C +#define MBEDTLS_CTR_DRBG_C +#define MBEDTLS_ENTROPY_C +#define MBEDTLS_ERROR_C +#define MBEDTLS_MD_C +#define MBEDTLS_MD5_C +#define MBEDTLS_OID_C +#define MBEDTLS_PKCS5_C +#define MBEDTLS_PK_C +#define MBEDTLS_PK_PARSE_C +#define MBEDTLS_PLATFORM_C +#define MBEDTLS_RSA_C +#define MBEDTLS_SHA1_C +#define MBEDTLS_SHA224_C +#define MBEDTLS_SHA256_C +#define MBEDTLS_SHA512_C +#define MBEDTLS_SSL_CLI_C +#define MBEDTLS_SSL_SRV_C +#define MBEDTLS_SSL_TLS_C +#define MBEDTLS_X509_CRT_PARSE_C +#define MBEDTLS_X509_USE_C +#define MBEDTLS_AES_FEWER_TABLES +#define MBEDTLS_PEM_PARSE_C +#define MBEDTLS_BASE64_C +#define MBEDTLS_SSL_TICKET_C + +#define MBEDTLS_SSL_PROTO_TLS1_2 +#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED +#define MBEDTLS_GCM_C +#define MBEDTLS_ECDH_C +#define MBEDTLS_ECP_C +#define MBEDTLS_ECDSA_C +#define MBEDTLS_ASN1_WRITE_C + From 5e50b7104ce2db67c240ee5c3777380aed48aeba Mon Sep 17 00:00:00 2001 From: cpq Date: Tue, 25 Jul 2023 15:26:47 +0100 Subject: [PATCH 2/2] Unit test fixed --- .../stm32/nucleo-h743zi-make-baremetal-builtin/Makefile | 2 +- test/unit_test.c | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/stm32/nucleo-h743zi-make-baremetal-builtin/Makefile b/examples/stm32/nucleo-h743zi-make-baremetal-builtin/Makefile index 45df45e7..d5ea6335 100644 --- a/examples/stm32/nucleo-h743zi-make-baremetal-builtin/Makefile +++ b/examples/stm32/nucleo-h743zi-make-baremetal-builtin/Makefile @@ -59,4 +59,4 @@ test: update # grep 'MQTT connected' /tmp/output.txt # Check for MQTT connection success clean: - $(RM) firmware.* *.su cmsis_core cmsis_h7 + $(RM) firmware.* *.su cmsis_core cmsis_h7 mbedtls diff --git a/test/unit_test.c b/test/unit_test.c index 17dd956b..aebbbfd0 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -1183,10 +1183,10 @@ static void test_http_404(void) { static void test_tls(void) { #if MG_TLS - struct mg_tls_opts opts = {}; - opts.client_ca = s_tls_ca; - opts.server_cert = s_tls_cert; - opts.server_key = s_tls_key; + struct mg_tls_opts opts = {0}; + opts.client_ca = mg_str(s_tls_ca); + opts.server_cert = mg_str(s_tls_cert); + opts.server_key = mg_str(s_tls_key); struct mg_mgr mgr; struct mg_connection *c; const char *url = "https://127.0.0.1:12347"; @@ -1242,7 +1242,6 @@ static void test_http_client(void) { #if MG_TLS { const char *url = "https://cesanta.com"; - struct mg_str host = mg_url_host(url); c = mg_http_connect(&mgr, url, f3, &ok); ASSERT(c != NULL); for (i = 0; i < 1500 && ok <= 0; i++) mg_mgr_poll(&mgr, 1000);