Skip to content

Commit 7f49ee7

Browse files
committed
[crypto] PSA API: introduce platform API for crypto dynamic memory mgmt
This commit introduces two new platform functions: - otPlatCryptoCAlloc() - otPlatCryptoFree() It also provides a default implementation using the OpenThread Heap. This API is necessary for the upcoming work related to PSA API Signed-off-by: Łukasz Duda <lukasz.duda@nordicsemi.no>
1 parent c966588 commit 7f49ee7

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

include/openthread/instance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extern "C" {
5252
*
5353
* @note This number versions both OpenThread platform and user APIs.
5454
*/
55-
#define OPENTHREAD_API_VERSION (570)
55+
#define OPENTHREAD_API_VERSION (571)
5656

5757
/**
5858
* @addtogroup api-instance

include/openthread/platform/crypto.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,33 @@ otError otPlatCryptoDestroyKey(otCryptoKeyRef aKeyRef);
288288
*/
289289
bool otPlatCryptoHasKey(otCryptoKeyRef aKeyRef);
290290

291+
/**
292+
* Dynamically allocates new memory for Crypto subsystem. On platforms that support it, should just redirect to calloc.
293+
* For those that don't support calloc, should support the same functionality:
294+
*
295+
* "The calloc() function contiguously allocates enough space for count objects that are size bytes of
296+
* memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes
297+
* of value zero."
298+
*
299+
* Is required for OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE.
300+
*
301+
* @param[in] aNum The number of blocks to allocate
302+
* @param[in] aSize The size of each block to allocate
303+
*
304+
* @retval void* The pointer to the front of the memory allocated
305+
* @retval NULL Failed to allocate the memory requested.
306+
*/
307+
void *otPlatCryptoCAlloc(size_t aNum, size_t aSize);
308+
309+
/**
310+
* Frees memory that was dynamically allocated by otPlatCryptoCAlloc.
311+
*
312+
* Is required for OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE.
313+
*
314+
* @param[in] aPtr A pointer the memory blocks to free. The pointer may be NULL.
315+
*/
316+
void otPlatCryptoFree(void *aPtr);
317+
291318
/**
292319
* Initialize the HMAC operation.
293320
*

src/core/crypto/crypto_platform.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <openthread/instance.h>
4848
#include <openthread/platform/crypto.h>
4949
#include <openthread/platform/entropy.h>
50+
#include <openthread/platform/memory.h>
5051
#include <openthread/platform/time.h>
5152

5253
#include "common/code_utils.hpp"
@@ -78,6 +79,11 @@ static constexpr uint16_t kEntropyMinThreshold = 16;
7879
#endif
7980
#endif
8081

82+
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
83+
OT_TOOL_WEAK void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return otPlatCAlloc(aNum, aSize); }
84+
OT_TOOL_WEAK void otPlatCryptoFree(void *aPtr) { otPlatFree(aPtr); }
85+
#endif
86+
8187
OT_TOOL_WEAK void otPlatCryptoInit(void)
8288
{
8389
// Intentionally empty.

src/core/crypto/mbedtls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ MbedTls::MbedTls(void)
5757
// mbedTLS's debug level is almost the same as OpenThread's
5858
mbedtls_debug_set_threshold(OPENTHREAD_CONFIG_LOG_LEVEL);
5959
#endif
60-
#if OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT
60+
#if OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
6161
mbedtls_platform_set_calloc_free(Heap::CAlloc, Heap::Free);
62-
#endif // OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT
62+
#endif // OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
6363
}
6464

6565
Error MbedTls::MapError(int aMbedTlsError)

tests/unit/test_platform.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ extern "C" {
106106
OT_TOOL_WEAK void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
107107

108108
OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); }
109+
110+
OT_TOOL_WEAK void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
111+
112+
OT_TOOL_WEAK void otPlatCryptoFree(void *aPtr) { free(aPtr); }
109113
#endif
110114

111115
OT_TOOL_WEAK void otTaskletsSignalPending(otInstance *) {}

third_party/mbedtls/mbedtls-config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@
134134
#define MBEDTLS_ENTROPY_MAX_SOURCES 1 /**< Maximum number of sources supported */
135135

136136
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
137-
#define MBEDTLS_PLATFORM_STD_CALLOC otPlatCAlloc /**< Default allocator to use, can be undefined */
138-
#define MBEDTLS_PLATFORM_STD_FREE otPlatFree /**< Default free to use, can be undefined */
137+
#define MBEDTLS_PLATFORM_STD_CALLOC otPlatCryptoCAlloc /**< Default allocator to use, can be undefined */
138+
#define MBEDTLS_PLATFORM_STD_FREE otPlatCryptoFree /**< Default free to use, can be undefined */
139139
#else
140140
#define MBEDTLS_MEMORY_BUFFER_ALLOC_C
141141
#endif

0 commit comments

Comments
 (0)