Skip to content

Commit 77552dd

Browse files
committed
Merge branch 'feature/mbedtls_dynamic_memory_v3.3' into 'release/v3.3'
feat(mbedtls): Add dynamic buffer and local resource managment to decrease SSL heap cost (backport v3.3) See merge request sdk/ESP8266_RTOS_SDK!1412
2 parents 633322b + cfebde2 commit 77552dd

File tree

8 files changed

+1234
-2
lines changed

8 files changed

+1234
-2
lines changed

components/mbedtls/CMakeLists.txt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ set_property(TARGET mbedtls PROPERTY SOURCES ${src_tls})
1919
set(mbedtls_targets mbedtls mbedcrypto mbedx509)
2020

2121
# Add port files to mbedtls targets
22-
target_sources(mbedtls PRIVATE "${COMPONENT_DIR}/port/mbedtls_debug.c"
23-
"${COMPONENT_DIR}/port/net_sockets.c")
22+
set(mbedtls_target_sources "${COMPONENT_DIR}/port/mbedtls_debug.c"
23+
"${COMPONENT_DIR}/port/net_sockets.c")
24+
25+
if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
26+
set(mbedtls_target_sources ${mbedtls_target_sources}
27+
"${COMPONENT_DIR}/port/dynamic/esp_mbedtls_dynamic_impl.c"
28+
"${COMPONENT_DIR}/port/dynamic/esp_ssl_cli.c"
29+
"${COMPONENT_DIR}/port/dynamic/esp_ssl_srv.c"
30+
"${COMPONENT_DIR}/port/dynamic/esp_ssl_tls.c")
31+
endif()
32+
33+
target_sources(mbedtls PRIVATE ${mbedtls_target_sources})
2434

2535
target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_hardware.c"
2636
"${COMPONENT_DIR}/port/esp_mem.c"
@@ -30,9 +40,27 @@ target_sources(mbedcrypto PRIVATE "${COMPONENT_DIR}/port/esp_hardware.c"
3040
"${COMPONENT_DIR}/port/esp8266/sha256.c"
3141
"${COMPONENT_DIR}/port/esp8266/sha512.c")
3242

43+
3344
foreach(target ${mbedtls_targets})
3445
target_compile_definitions(${target} PUBLIC -DMBEDTLS_CONFIG_FILE="mbedtls/esp_config.h" -DCONFIG_SSL_USING_MBEDTLS)
3546
endforeach()
3647

48+
if(CONFIG_MBEDTLS_DYNAMIC_BUFFER)
49+
set(WRAP_FUNCTIONS
50+
mbedtls_ssl_handshake_client_step
51+
mbedtls_ssl_handshake_server_step
52+
mbedtls_ssl_read
53+
mbedtls_ssl_write
54+
mbedtls_ssl_session_reset
55+
mbedtls_ssl_free
56+
mbedtls_ssl_setup
57+
mbedtls_ssl_send_alert_message
58+
mbedtls_ssl_close_notify)
59+
60+
foreach(wrap ${WRAP_FUNCTIONS})
61+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=${wrap}")
62+
endforeach()
63+
endif()
64+
3765
# Link mbedtls libraries to component library
3866
target_link_libraries(${COMPONENT_LIB} INTERFACE ${mbedtls_targets})

components/mbedtls/Kconfig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ menu "mbedTLS"
8181
This defines maximum outgoing fragment length, overriding default
8282
maximum content length (MBEDTLS_SSL_MAX_CONTENT_LEN).
8383

84+
config MBEDTLS_DYNAMIC_BUFFER
85+
bool "Using dynamic TX/RX buffer"
86+
default n
87+
select MBEDTLS_ASYMMETRIC_CONTENT_LEN
88+
help
89+
Using dynamic TX/RX buffer. After enabling this option, mbedTLS will
90+
allocate TX buffer when need to send data and then free it if all data
91+
is sent, allocate RX buffer when need to receive data and then free it
92+
when all data is used or read by upper layer.
93+
94+
By default, when SSL is initialized, mbedTLS also allocate TX and
95+
RX buffer with the default value of "MBEDTLS_SSL_OUT_CONTENT_LEN" or
96+
"MBEDTLS_SSL_IN_CONTENT_LEN", so to save more heap, users can set
97+
the options to be an appropriate value.
98+
99+
config MBEDTLS_DYNAMIC_FREE_PEER_CERT
100+
bool "Free SSL peer certificate after its usage"
101+
default n
102+
depends on MBEDTLS_DYNAMIC_BUFFER
103+
help
104+
Free peer certificate after its usage in handshake process.
105+
106+
config MBEDTLS_DYNAMIC_FREE_CONFIG_DATA
107+
bool "Free certificate, key and DHM data after its usage"
108+
default n
109+
depends on MBEDTLS_DYNAMIC_BUFFER
110+
help
111+
Free certificate, private key and DHM data after its usage in handshake process.
112+
113+
The option will decrease heap cost when handshake, but also lead to problem:
114+
115+
Becasue all certificate, private key and DHM data are freed so users should register
116+
certificate and private key to ssl config object again.
117+
84118
config MBEDTLS_DEBUG
85119
bool "Enable mbedTLS debugging"
86120
default n

components/mbedtls/component.mk

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,22 @@ COMPONENT_OBJEXCLUDE := mbedtls/library/net_sockets.o
1010

1111
COMPONENT_SUBMODULES += mbedtls
1212

13+
ifdef CONFIG_MBEDTLS_DYNAMIC_BUFFER
14+
15+
WRAP_FUNCTIONS = mbedtls_ssl_handshake_client_step \
16+
mbedtls_ssl_handshake_server_step \
17+
mbedtls_ssl_read \
18+
mbedtls_ssl_write \
19+
mbedtls_ssl_session_reset \
20+
mbedtls_ssl_free \
21+
mbedtls_ssl_setup \
22+
mbedtls_ssl_send_alert_message \
23+
mbedtls_ssl_close_notify
24+
25+
WRAP_ARGUMENT := -Wl,--wrap=
26+
27+
COMPONENT_ADD_LDFLAGS = -l$(COMPONENT_NAME) $(addprefix $(WRAP_ARGUMENT),$(WRAP_FUNCTIONS))
28+
29+
COMPONENT_SRCDIRS += port/dynamic
30+
31+
endif

0 commit comments

Comments
 (0)