Skip to content

Commit 35da1e7

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 103b601 commit 35da1e7

File tree

7 files changed

+48
-14
lines changed

7 files changed

+48
-14
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 (571)
55+
#define OPENTHREAD_API_VERSION (572)
5656

5757
/**
5858
* @addtogroup api-instance

include/openthread/platform/crypto.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,31 @@ otError otPlatCryptoDestroyKey(otCryptoKeyRef aKeyRef);
291291
*/
292292
bool otPlatCryptoHasKey(otCryptoKeyRef aKeyRef);
293293

294+
/**
295+
* Dynamically allocates new memory for the Crypto subsystem. On platforms that support it, they should redirect to
296+
* `calloc`. For those that don't support `calloc`, they should implement the standard `calloc` behavior.
297+
*
298+
* See: https://man7.org/linux/man-pages/man3/calloc.3.html
299+
*
300+
* Is required for `OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE`.
301+
*
302+
* @param[in] aNum The number of blocks to allocate
303+
* @param[in] aSize The size of each block to allocate
304+
*
305+
* @retval void* The pointer to the front of the memory allocated
306+
* @retval NULL Failed to allocate the memory requested.
307+
*/
308+
void *otPlatCryptoCAlloc(size_t aNum, size_t aSize);
309+
310+
/**
311+
* Frees memory that was dynamically allocated by `otPlatCryptoCAlloc()`.
312+
*
313+
* Is required for `OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE`.
314+
*
315+
* @param[in] aPtr A pointer the memory blocks to free. The pointer may be NULL.
316+
*/
317+
void otPlatCryptoFree(void *aPtr);
318+
294319
/**
295320
* Initialize the HMAC operation.
296321
*

include/openthread/platform/memory.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,17 @@ extern "C" {
5151
*/
5252

5353
/*
54-
* OpenThread only requires dynamic memory allocation when supporting multiple simultaneous instances, for MbedTls.
54+
* Dynamic memory allocation is primarily needed for Thread Border Router functionalities and protocols
55+
* such as SRP (server), mDNS or DHCPv6 PD. It may also be used for OpenThread message buffers.
5556
*/
5657

5758
/**
58-
* Dynamically allocates new memory. On platforms that support it, should just redirect to calloc. For
59-
* those that don't support calloc, should support the same functionality:
59+
* Dynamically allocates new memory. On platforms that support it, they should redirect to `calloc`. For
60+
* those that don't support `calloc`, they should implement the standard `calloc` behavior.
6061
*
61-
* "The calloc() function contiguously allocates enough space for count objects that are size bytes of
62-
* memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes
63-
* of value zero."
62+
* See: https://man7.org/linux/man-pages/man3/calloc.3.html
6463
*
65-
* Is required for OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE.
64+
* Is required for `OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE`.
6665
*
6766
* @param[in] aNum The number of blocks to allocate
6867
* @param[in] aSize The size of each block to allocate
@@ -73,9 +72,9 @@ extern "C" {
7372
void *otPlatCAlloc(size_t aNum, size_t aSize);
7473

7574
/**
76-
* Frees memory that was dynamically allocated.
75+
* Frees memory that was dynamically allocated by `otPlatCAlloc()`.
7776
*
78-
* Is required for OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE.
77+
* Is required for `OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE`.
7978
*
8079
* @param[in] aPtr A pointer the memory blocks to free. The pointer may be NULL.
8180
*/

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
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)