From 9c94e4c5efbedd9e8197ee6048454cf392d91492 Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Wed, 6 Oct 2021 15:01:27 +0300 Subject: [PATCH] crypto: define XMALLOC & XFREE for sw_crypto Use the same memory allocation in sw_crypto driver as what is used in src/lib/crypto libraries In addition, in nuttx protected build, allocate all memory from kernel heap Signed-off-by: Jukka Laitinen --- src/drivers/sw_crypto/crypto.c | 8 ++++---- src/lib/crypto/CMakeLists.txt | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/drivers/sw_crypto/crypto.c b/src/drivers/sw_crypto/crypto.c index 8d8e0cd7ed..a706aa9216 100644 --- a/src/drivers/sw_crypto/crypto.c +++ b/src/drivers/sw_crypto/crypto.c @@ -52,11 +52,11 @@ extern void libtomcrypt_init(void); #define KEY_CACHE_LEN 16 #ifndef SECMEM_ALLOC -#define SECMEM_ALLOC malloc +#define SECMEM_ALLOC XMALLOC #endif #ifndef SECMEM_FREE -#define SECMEM_FREE free +#define SECMEM_FREE XFREE #endif /* @@ -156,7 +156,7 @@ crypto_session_handle_t crypto_open(px4_crypto_algorithm_t algorithm) switch (algorithm) { case CRYPTO_XCHACHA20: { - chacha20_context_t *context = malloc(sizeof(chacha20_context_t)); + chacha20_context_t *context = XMALLOC(sizeof(chacha20_context_t)); if (!context) { ret.handle = 0; @@ -188,7 +188,7 @@ void crypto_close(crypto_session_handle_t *handle) crypto_open_count--; handle->handle = 0; keystore_close(&handle->keystore_handle); - free(handle->context); + XFREE(handle->context); handle->context = NULL; } diff --git a/src/lib/crypto/CMakeLists.txt b/src/lib/crypto/CMakeLists.txt index 63f55e8a3d..90936d29b4 100644 --- a/src/lib/crypto/CMakeLists.txt +++ b/src/lib/crypto/CMakeLists.txt @@ -99,4 +99,14 @@ target_link_libraries(libtomcrypt # "der_encode_asn1_identifier.c:39:18: error: comparison is always false due to limited range of data type" target_compile_options(libtomcrypt PRIVATE -Wno-type-limits) +# Re-define memory allocation macros if we are building for +# memory protected build in nuttx. All allocations happen in +# kernel heap there + +if (NOT DEFINED CONFIG_BUILD_FLAT AND "${PX4_PLATFORM}" MATCHES "nuttx") + target_compile_options(libtomcrypt PUBLIC -DXMALLOC=kmm_malloc -DXFREE=kmm_free -DXREALLOC=kmm_realloc -DXCALLOC=kmm_calloc) + target_compile_options(libtommath PUBLIC -DMP_MALLOC=kmm_malloc -DMP_FREE\(p,s\)=kmm_free\(p\) -DMP_REALLOC\(p,o,n\)=kmm_realloc\(p,n\) -DMP_CALLOC=kmm_calloc) + target_link_libraries(libtommath PRIVATE nuttx_kmm) +endif() + endif()