From b614af3a9146423a91981c34ed767d437e5fee75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Duda?= Date: Wed, 7 May 2025 01:07:03 +0200 Subject: [PATCH] [crypto] PSA API: introduce platform API for crypto dynamic memory mgmt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds two new functions, `otPlatCryptoCAlloc` and `otPlatCryptoFree`, which provide dynamic memory management for the crypto subsystem. They are only enabled when `OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE` is set. Signed-off-by: Ɓukasz Duda --- examples/apps/cli/main.c | 3 +-- examples/apps/ncp/main.c | 3 +-- include/openthread/instance.h | 2 +- include/openthread/platform/crypto.h | 27 ++++++++++++++++++++ src/core/crypto/crypto_platform_mbedtls.cpp | 6 +++++ src/core/crypto/crypto_platform_psa.cpp | 6 +++++ src/core/crypto/mbedtls.cpp | 4 +-- tests/unit/test_dns_client.cpp | 8 +++--- tests/unit/test_srp_server.cpp | 28 +++------------------ third_party/mbedtls/mbedtls-config.h | 4 +-- 10 files changed, 52 insertions(+), 39 deletions(-) diff --git a/examples/apps/cli/main.c b/examples/apps/cli/main.c index 2ede597cd..95d47c712 100644 --- a/examples/apps/cli/main.c +++ b/examples/apps/cli/main.c @@ -56,8 +56,7 @@ extern void otAppCliInit(otInstance *aInstance); #if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE OT_TOOL_WEAK void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); } - -OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); } +OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); } #endif #if OPENTHREAD_POSIX && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) diff --git a/examples/apps/ncp/main.c b/examples/apps/ncp/main.c index 6a45ec848..3930a319c 100644 --- a/examples/apps/ncp/main.c +++ b/examples/apps/ncp/main.c @@ -62,8 +62,7 @@ extern void otAppNcpInitMulti(otInstance **aInstances, uint8_t count); #if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE OT_TOOL_WEAK void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); } - -OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); } +OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); } #endif int main(int argc, char *argv[]) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 3df8a9c71..7780e21c1 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -52,7 +52,7 @@ extern "C" { * * @note This number versions both OpenThread platform and user APIs. */ -#define OPENTHREAD_API_VERSION (550) +#define OPENTHREAD_API_VERSION (551) /** * @addtogroup api-instance diff --git a/include/openthread/platform/crypto.h b/include/openthread/platform/crypto.h index 6ee439979..00737a586 100644 --- a/include/openthread/platform/crypto.h +++ b/include/openthread/platform/crypto.h @@ -286,6 +286,33 @@ otError otPlatCryptoDestroyKey(otCryptoKeyRef aKeyRef); */ bool otPlatCryptoHasKey(otCryptoKeyRef aKeyRef); +/** + * Dynamically allocates new memory for Crypto subsystem. On platforms that support it, should just redirect to calloc. + * For those that don't support calloc, should support the same functionality: + * + * "The calloc() function contiguously allocates enough space for count objects that are size bytes of + * memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes + * of value zero." + * + * Is required for OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE. + * + * @param[in] aNum The number of blocks to allocate + * @param[in] aSize The size of each block to allocate + * + * @retval void* The pointer to the front of the memory allocated + * @retval NULL Failed to allocate the memory requested. + */ +void *otPlatCryptoCAlloc(size_t aNum, size_t aSize); + +/** + * Frees memory that was dynamically allocated by otPlatCryptoCAlloc. + * + * Is required for OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE. + * + * @param[in] aPtr A pointer the memory blocks to free. The pointer may be NULL. + */ +void otPlatCryptoFree(void *aPtr); + /** * Initialize the HMAC operation. * diff --git a/src/core/crypto/crypto_platform_mbedtls.cpp b/src/core/crypto/crypto_platform_mbedtls.cpp index a72c04988..5cd693cdb 100644 --- a/src/core/crypto/crypto_platform_mbedtls.cpp +++ b/src/core/crypto/crypto_platform_mbedtls.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include "common/code_utils.hpp" @@ -78,6 +79,11 @@ static constexpr uint16_t kEntropyMinThreshold = 16; #endif #endif +#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE +OT_TOOL_WEAK void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return otPlatCAlloc(aNum, aSize); } +OT_TOOL_WEAK void otPlatCryptoFree(void *aPtr) { otPlatFree(aPtr); } +#endif + // AES Implementation OT_TOOL_WEAK otError otPlatCryptoAesInit(otCryptoContext *aContext) { diff --git a/src/core/crypto/crypto_platform_psa.cpp b/src/core/crypto/crypto_platform_psa.cpp index ce79ca0ab..1b44e040b 100644 --- a/src/core/crypto/crypto_platform_psa.cpp +++ b/src/core/crypto/crypto_platform_psa.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -200,6 +201,11 @@ exit: return error; } +#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE +OT_TOOL_WEAK void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return otPlatCAlloc(aNum, aSize); } +OT_TOOL_WEAK void otPlatCryptoFree(void *aPtr) { otPlatFree(aPtr); } +#endif + OT_TOOL_WEAK otError otPlatCryptoImportKey(otCryptoKeyRef *aKeyRef, otCryptoKeyType aKeyType, otCryptoKeyAlgorithm aKeyAlgorithm, diff --git a/src/core/crypto/mbedtls.cpp b/src/core/crypto/mbedtls.cpp index 11d39dd21..47269ec1c 100644 --- a/src/core/crypto/mbedtls.cpp +++ b/src/core/crypto/mbedtls.cpp @@ -57,9 +57,9 @@ MbedTls::MbedTls(void) // mbedTLS's debug level is almost the same as OpenThread's mbedtls_debug_set_threshold(OPENTHREAD_CONFIG_LOG_LEVEL); #endif -#if OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT +#if OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE mbedtls_platform_set_calloc_free(Heap::CAlloc, Heap::Free); -#endif // OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT +#endif // OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE } Error MbedTls::MapError(int aMbedTlsError) diff --git a/tests/unit/test_dns_client.cpp b/tests/unit/test_dns_client.cpp index f7b82e637..cbded349c 100644 --- a/tests/unit/test_dns_client.cpp +++ b/tests/unit/test_dns_client.cpp @@ -135,6 +135,9 @@ void otPlatFree(void *aPtr) free(aPtr); } + +void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); } +void otPlatCryptoFree(void *aPtr) { free(aPtr); } #endif #if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED @@ -1609,11 +1612,6 @@ void TestDnsClient(void) SuccessOrQuit(otBorderRouterRegister(sInstance)); AdvanceTime(1000); -#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE - // On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation. - heapAllocations += 1; -#endif - VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength()); //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/unit/test_srp_server.cpp b/tests/unit/test_srp_server.cpp index d3c540805..8c6ecb8ae 100644 --- a/tests/unit/test_srp_server.cpp +++ b/tests/unit/test_srp_server.cpp @@ -131,6 +131,9 @@ void otPlatFree(void *aPtr) free(aPtr); } + +void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); } +void otPlatCryptoFree(void *aPtr) { free(aPtr); } #endif #if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED @@ -480,11 +483,6 @@ void TestSrpServerBase(void) srpServer->SetEnabled(false); AdvanceTime(100); -#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE - // On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation. - heapAllocations += 1; -#endif - VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength()); //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -597,11 +595,6 @@ void TestSrpServerReject(void) srpServer->SetEnabled(false); AdvanceTime(100); -#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE - // On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation. - heapAllocations += 1; -#endif - VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength()); //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -714,11 +707,6 @@ void TestSrpServerIgnore(void) srpServer->SetEnabled(false); AdvanceTime(100); -#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE - // On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation. - heapAllocations += 1; -#endif - VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength()); //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -835,11 +823,6 @@ void TestSrpServerClientRemove(bool aShouldRemoveKeyLease) srpServer->SetEnabled(false); AdvanceTime(100); -#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE - // On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation. - heapAllocations += 1; -#endif - VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength()); //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1034,11 +1017,6 @@ void TestUpdateLeaseShortVariant(void) srpServer->SetEnabled(false); AdvanceTime(100); -#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE - // On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation. - heapAllocations += 1; -#endif - VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength()); //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/third_party/mbedtls/mbedtls-config.h b/third_party/mbedtls/mbedtls-config.h index 104b0bcbf..f5bb6ae9f 100644 --- a/third_party/mbedtls/mbedtls-config.h +++ b/third_party/mbedtls/mbedtls-config.h @@ -194,8 +194,8 @@ #define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf #if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE -#define MBEDTLS_PLATFORM_STD_CALLOC otPlatCAlloc /**< Default allocator to use, can be undefined */ -#define MBEDTLS_PLATFORM_STD_FREE otPlatFree /**< Default free to use, can be undefined */ +#define MBEDTLS_PLATFORM_STD_CALLOC otPlatCryptoCAlloc /**< Default allocator to use, can be undefined */ +#define MBEDTLS_PLATFORM_STD_FREE otPlatCryptoFree /**< Default free to use, can be undefined */ #else #define MBEDTLS_MEMORY_BUFFER_ALLOC_C #endif