Skip to content

Commit f2db28f

Browse files
committed
[crypto] PSA API: remove otPlatCryptoInit API
This commit removes otPlatCryptoInit API and moves responsibility of initializing the Crypto subsystem to the platform. Signed-off-by: Łukasz Duda <lukasz.duda@nordicsemi.no>
1 parent f9450da commit f2db28f

File tree

13 files changed

+66
-53
lines changed

13 files changed

+66
-53
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[])

examples/platforms/simulation/system.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
#include <openthread/platform/radio.h>
5656
#include <openthread/platform/toolchain.h>
5757

58+
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
59+
#include <psa/crypto.h>
60+
#endif
61+
5862
#include "simul_utils.h"
5963

6064
uint32_t gNodeId = 1;
@@ -201,10 +205,13 @@ void otSysInit(int aArgCount, char *aArgVector[])
201205
signal(SIGTERM, &handleSignal);
202206
signal(SIGHUP, &handleSignal);
203207

204-
#if OPENTHREAD_PSA_CRYPTO_NATIVE_ITS_FILE && (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
208+
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
209+
psa_crypto_init();
210+
#if OPENTHREAD_PSA_CRYPTO_NATIVE_ITS_FILE
205211
snprintf(sNativeItsFileNamePrefix, sizeof(sNativeItsFileNamePrefix), "%s/%s_%d_",
206212
OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH, getenv("PORT_OFFSET") ? getenv("PORT_OFFSET") : "0", gNodeId);
207213
gItsFileNamePrefix = sNativeItsFileNamePrefix;
214+
#endif
208215
#endif
209216

210217
platformLoggingInit(basename(aArgVector[0]));

examples/platforms/simulation/virtual_time/platform-sim.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
#include <openthread/platform/alarm-milli.h>
5252

5353
#include "../simul_utils.h"
54+
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
55+
#include <psa/crypto.h>
56+
#endif
57+
5458
#include "lib/platform/exit_code.h"
5559
#include "utils/uart.h"
5660

@@ -278,10 +282,13 @@ void otSysInit(int argc, char *argv[])
278282
DieNow(OT_EXIT_FAILURE);
279283
}
280284

281-
#if OPENTHREAD_PSA_CRYPTO_NATIVE_ITS_FILE && (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
285+
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
286+
psa_crypto_init();
287+
#if OPENTHREAD_PSA_CRYPTO_NATIVE_ITS_FILE
282288
snprintf(sNativeItsFileNamePrefix, sizeof(sNativeItsFileNamePrefix), "%s/%s_%d_",
283289
OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH, getenv("PORT_OFFSET") ? getenv("PORT_OFFSET") : "0", gNodeId);
284290
gItsFileNamePrefix = sNativeItsFileNamePrefix;
291+
#endif
285292
#endif
286293

287294
socket_init();

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 (561)
55+
#define OPENTHREAD_API_VERSION (563)
5656

5757
/**
5858
* @addtogroup api-instance

include/openthread/platform/crypto.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,6 @@ typedef struct otPlatCryptoEcdsaSignature otPlatCryptoEcdsaSignature;
215215
*/
216216
#define OT_CRYPTO_PBDKF2_MAX_SALT_SIZE 30
217217

218-
/**
219-
* Initialize the Crypto module.
220-
*/
221-
void otPlatCryptoInit(void);
222-
223218
/**
224219
* Import a key into PSA ITS.
225220
*
@@ -291,6 +286,33 @@ otError otPlatCryptoDestroyKey(otCryptoKeyRef aKeyRef);
291286
*/
292287
bool otPlatCryptoHasKey(otCryptoKeyRef aKeyRef);
293288

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+
294316
/**
295317
* Initialize the HMAC operation.
296318
*

src/core/crypto/crypto_platform_mbedtls.cpp

Lines changed: 5 additions & 4 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,10 +79,10 @@ static constexpr uint16_t kEntropyMinThreshold = 16;
7879
#endif
7980
#endif
8081

81-
OT_TOOL_WEAK void otPlatCryptoInit(void)
82-
{
83-
// Intentionally empty.
84-
}
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
8586

8687
// AES Implementation
8788
OT_TOOL_WEAK otError otPlatCryptoAesInit(otCryptoContext *aContext)

src/core/crypto/crypto_platform_psa.cpp

Lines changed: 5 additions & 1 deletion
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,7 +201,10 @@ static otError extractPrivateKeyInfo(const uint8_t *aAsn1KeyPair,
200201
return error;
201202
}
202203

203-
OT_TOOL_WEAK void otPlatCryptoInit(void) { psa_crypto_init(); }
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
204208

205209
OT_TOOL_WEAK otError otPlatCryptoImportKey(otCryptoKeyRef *aKeyRef,
206210
otCryptoKeyType aKeyType,

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)

src/core/thread/key_manager.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ KeyManager::KeyManager(Instance &aInstance)
176176
, mKekFrameCounter(0)
177177
, mIsPskcSet(false)
178178
{
179-
otPlatCryptoInit();
180-
181179
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
182180
{
183181
NetworkKey networkKey;

0 commit comments

Comments
 (0)