Skip to content

Commit 5833d1f

Browse files
tejlmandthst-nordic
authored andcommitted
nrf_security: introduces mbedtls heap in nrf_security
This commit introduces mbedtls_heap.c in nrf security and also add globals in compiled libraries to userspace config using zephyr_library_app_memory. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent 2c2d24b commit 5833d1f

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

nrf_security/Kconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ config MBEDTLS_TLS_LIBRARY
4747
Create the mbed SSL/TLS library in addition to the mbed crypto
4848
library.
4949

50+
menu "mbed TLS memory configuration"
51+
52+
config MBEDTLS_ENABLE_HEAP
53+
bool "Enable global heap for mbed TLS"
54+
help
55+
This option enables the mbedtls to use the heap. This setting must
56+
be global so that various applications and libraries in Zephyr do not
57+
try to do this themselves as there can be only one heap defined
58+
in mbedtls. If this is enabled, then the Zephyr will, during the device
59+
startup, initialize the heap automatically.
60+
61+
config MBEDTLS_HEAP_SIZE
62+
int "Heap size for mbed TLS"
63+
default 512
64+
depends on MBEDTLS_ENABLE_HEAP
65+
help
66+
The mbedtls routines will use this heap if enabled.
67+
For streaming communication with arbitrary (HTTPS) servers on the
68+
Internet, 32KB + overheads (up to another 20KB) may be needed.
69+
Ensure to adjust the heap size according to the need of the
70+
application.
71+
72+
endmenu
73+
5074
comment "Backend Selection"
5175

5276
config CC310_BACKEND

nrf_security/src/mbedtls/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ zephyr_library_sources_ifdef(VANILLA_ONLY_MBEDTLS_POLY1305_C
181181
zephyr_library_sources_ifdef(VANILLA_ONLY_MBEDTLS_CHACHAPOLY_C
182182
${ARM_MBEDTLS_PATH}/library/chachapoly.c
183183
)
184-
zephyr_library_sources(${ZEPHYR_BASE}/../modules/crypto/mbedtls/zephyr_init.c)
184+
zephyr_library_sources_ifdef(CONFIG_MBEDTLS_ENABLE_HEAP ${NRF_SECURITY_ROOT}/src/mbedtls/mbedtls_heap.c)
185+
zephyr_library_app_memory(k_mbedtls_partition)
185186

186187
if(CONFIG_SOC_NRF52840 OR CONFIG_SOC_NRF9160)
187188
zephyr_library_sources(${NRF_SECURITY_ROOT}/src/backend/cc310/replacements/entropy.c)
@@ -199,6 +200,7 @@ if (CONFIG_MBEDTLS_X509_LIBRARY)
199200
zephyr_library_sources(${src_x509})
200201
zephyr_library_link_libraries(${IMAGE}mbedtls_common)
201202
nrf_security_debug_list_target_files(${IMAGE}mbedtls_x509_vanilla)
203+
zephyr_library_app_memory(k_mbedtls_partition)
202204
endif()
203205

204206
#
@@ -209,6 +211,7 @@ if (CONFIG_MBEDTLS_TLS_LIBRARY)
209211
zephyr_library_sources(${src_tls} ${src_tls_replacement})
210212
zephyr_library_link_libraries(${IMAGE}mbedtls_common)
211213
nrf_security_debug_list_target_files(${IMAGE}mbedtls_tls_vanilla)
214+
zephyr_library_app_memory(k_mbedtls_partition)
212215
endif()
213216

214217
if(NOT CONFIG_NRF_CRYPTO_BACKEND_COMBINATION_0)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
5+
*/
6+
7+
#include <init.h>
8+
#include <app_memory/app_memdomain.h>
9+
10+
#include "mbedtls/memory_buffer_alloc.h"
11+
12+
#if !defined(CONFIG_MBEDTLS_HEAP_SIZE) || CONFIG_MBEDTLS_HEAP_SIZE == 0
13+
#error "CONFIG_MBEDTLS_HEAP_SIZE must be specified and greater than 0"
14+
#endif
15+
16+
static unsigned char mbedtls_heap[CONFIG_MBEDTLS_HEAP_SIZE];
17+
18+
static int mbedtls_heap_init(struct device *dev)
19+
{
20+
ARG_UNUSED(dev);
21+
22+
mbedtls_memory_buffer_alloc_init(mbedtls_heap, sizeof(mbedtls_heap));
23+
24+
return 0;
25+
}
26+
27+
/* Hw cc310 is initialized with CONFIG_KERNEL_INIT_PRIORITY_DEFAULT and the
28+
* heap must be initialized afterwards.
29+
*/
30+
SYS_INIT(mbedtls_heap_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);

0 commit comments

Comments
 (0)