Skip to content

Commit b614af3

Browse files
committed
[crypto] PSA API: introduce platform API for crypto dynamic memory mgmt
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 <lukasz.duda@nordicsemi.no>
1 parent 6b167f1 commit b614af3

File tree

10 files changed

+52
-39
lines changed

10 files changed

+52
-39
lines changed

examples/apps/cli/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ extern void otAppCliInit(otInstance *aInstance);
5656

5757
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
5858
OT_TOOL_WEAK void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
59-
60-
OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); }
59+
OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); }
6160
#endif
6261

6362
#if OPENTHREAD_POSIX && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)

examples/apps/ncp/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ extern void otAppNcpInitMulti(otInstance **aInstances, uint8_t count);
6262

6363
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
6464
OT_TOOL_WEAK void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
65-
66-
OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); }
65+
OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); }
6766
#endif
6867

6968
int main(int argc, char *argv[])

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 (550)
55+
#define OPENTHREAD_API_VERSION (551)
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
@@ -286,6 +286,33 @@ otError otPlatCryptoDestroyKey(otCryptoKeyRef aKeyRef);
286286
*/
287287
bool otPlatCryptoHasKey(otCryptoKeyRef aKeyRef);
288288

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

src/core/crypto/crypto_platform_mbedtls.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
// AES Implementation
8288
OT_TOOL_WEAK otError otPlatCryptoAesInit(otCryptoContext *aContext)
8389
{

src/core/crypto/crypto_platform_psa.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <openthread/instance.h>
4141
#include <openthread/platform/crypto.h>
4242
#include <openthread/platform/entropy.h>
43+
#include <openthread/platform/memory.h>
4344

4445
#include "common/code_utils.hpp"
4546
#include "common/debug.hpp"
@@ -200,6 +201,11 @@ static otError extractPrivateKeyInfo(const uint8_t *aAsn1KeyPair,
200201
return error;
201202
}
202203

204+
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
205+
OT_TOOL_WEAK void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return otPlatCAlloc(aNum, aSize); }
206+
OT_TOOL_WEAK void otPlatCryptoFree(void *aPtr) { otPlatFree(aPtr); }
207+
#endif
208+
203209
OT_TOOL_WEAK otError otPlatCryptoImportKey(otCryptoKeyRef *aKeyRef,
204210
otCryptoKeyType aKeyType,
205211
otCryptoKeyAlgorithm aKeyAlgorithm,

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_dns_client.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ void otPlatFree(void *aPtr)
135135

136136
free(aPtr);
137137
}
138+
139+
void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
140+
void otPlatCryptoFree(void *aPtr) { free(aPtr); }
138141
#endif
139142

140143
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
@@ -1609,11 +1612,6 @@ void TestDnsClient(void)
16091612
SuccessOrQuit(otBorderRouterRegister(sInstance));
16101613
AdvanceTime(1000);
16111614

1612-
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
1613-
// On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation.
1614-
heapAllocations += 1;
1615-
#endif
1616-
16171615
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
16181616

16191617
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

tests/unit/test_srp_server.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ void otPlatFree(void *aPtr)
131131

132132
free(aPtr);
133133
}
134+
135+
void *otPlatCryptoCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
136+
void otPlatCryptoFree(void *aPtr) { free(aPtr); }
134137
#endif
135138

136139
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED
@@ -480,11 +483,6 @@ void TestSrpServerBase(void)
480483
srpServer->SetEnabled(false);
481484
AdvanceTime(100);
482485

483-
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
484-
// On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation.
485-
heapAllocations += 1;
486-
#endif
487-
488486
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
489487

490488
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -597,11 +595,6 @@ void TestSrpServerReject(void)
597595
srpServer->SetEnabled(false);
598596
AdvanceTime(100);
599597

600-
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
601-
// On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation.
602-
heapAllocations += 1;
603-
#endif
604-
605598
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
606599

607600
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -714,11 +707,6 @@ void TestSrpServerIgnore(void)
714707
srpServer->SetEnabled(false);
715708
AdvanceTime(100);
716709

717-
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
718-
// On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation.
719-
heapAllocations += 1;
720-
#endif
721-
722710
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
723711

724712
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -835,11 +823,6 @@ void TestSrpServerClientRemove(bool aShouldRemoveKeyLease)
835823
srpServer->SetEnabled(false);
836824
AdvanceTime(100);
837825

838-
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
839-
// On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation.
840-
heapAllocations += 1;
841-
#endif
842-
843826
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
844827

845828
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -1034,11 +1017,6 @@ void TestUpdateLeaseShortVariant(void)
10341017
srpServer->SetEnabled(false);
10351018
AdvanceTime(100);
10361019

1037-
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
1038-
// On a first attempt, SRP Client generates the SRP Key which adds additional heap allocation.
1039-
heapAllocations += 1;
1040-
#endif
1041-
10421020
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
10431021

10441022
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

third_party/mbedtls/mbedtls-config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@
194194
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf
195195

196196
#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
197-
#define MBEDTLS_PLATFORM_STD_CALLOC otPlatCAlloc /**< Default allocator to use, can be undefined */
198-
#define MBEDTLS_PLATFORM_STD_FREE otPlatFree /**< Default free to use, can be undefined */
197+
#define MBEDTLS_PLATFORM_STD_CALLOC otPlatCryptoCAlloc /**< Default allocator to use, can be undefined */
198+
#define MBEDTLS_PLATFORM_STD_FREE otPlatCryptoFree /**< Default free to use, can be undefined */
199199
#else
200200
#define MBEDTLS_MEMORY_BUFFER_ALLOC_C
201201
#endif

0 commit comments

Comments
 (0)