Skip to content

Commit f9b7604

Browse files
committed
[crypto] PSA API: enable native ITS in build system
This commit enables PSA native ITS implementation in the build system for simulation and test platforms. Signed-off-by: Łukasz Duda <lukasz.duda@nordicsemi.no>
1 parent 92324fd commit f9b7604

File tree

13 files changed

+70
-6
lines changed

13 files changed

+70
-6
lines changed

examples/apps/cli/ftd.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ target_link_libraries(ot-cli-ftd PRIVATE
4848
openthread-cli-ftd
4949
${OT_PLATFORM_LIB_FTD}
5050
openthread-ftd
51-
${OT_PLATFORM_LIB_FTD}
5251
openthread-cli-ftd
5352
${OT_MBEDTLS}
53+
${OT_PLATFORM_LIB_FTD}
5454
ot-config-ftd
5555
ot-config
5656
)

examples/apps/cli/mtd.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ target_link_libraries(ot-cli-mtd PRIVATE
4848
openthread-cli-mtd
4949
${OT_PLATFORM_LIB_MTD}
5050
openthread-mtd
51-
${OT_PLATFORM_LIB_MTD}
5251
openthread-cli-mtd
5352
${OT_MBEDTLS}
53+
${OT_PLATFORM_LIB_MTD}
5454
ot-config-mtd
5555
ot-config
5656
)

examples/apps/ncp/ftd.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ target_link_libraries(ot-ncp-ftd PRIVATE
4141
openthread-ncp-ftd
4242
${OT_PLATFORM_LIB_FTD}
4343
openthread-ftd
44-
${OT_PLATFORM_LIB_FTD}
4544
openthread-ncp-ftd
4645
${OT_MBEDTLS}
46+
${OT_PLATFORM_LIB_FTD}
4747
ot-config-ftd
4848
ot-config
4949
)

examples/apps/ncp/mtd.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ target_link_libraries(ot-ncp-mtd PRIVATE
4141
openthread-ncp-mtd
4242
${OT_PLATFORM_LIB_MTD}
4343
openthread-mtd
44-
${OT_PLATFORM_LIB_MTD}
4544
openthread-ncp-mtd
4645
${OT_MBEDTLS}
46+
${OT_PLATFORM_LIB_MTD}
4747
ot-config-mtd
4848
ot-config
4949
)

examples/platforms/simulation/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ endif()
103103

104104
target_link_libraries(openthread-simulation PRIVATE
105105
openthread-platform
106+
mbedtls
107+
openthread-native-its-file
106108
ot-simulation-config
107109
ot-config
108-
mbedtls
109110
)
110111

111112
target_compile_options(openthread-simulation PRIVATE

examples/platforms/simulation/system.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ extern otRadioCaps gRadioCaps;
6464

6565
static volatile bool gTerminate = false;
6666

67+
#if OPENTHREAD_PSA_CRYPTO_NATIVE_ITS_FILE && (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
68+
static char sNativeItsFileNamePrefix[256];
69+
extern const char *gItsFileNamePrefix;
70+
#endif
71+
6772
static void handleSignal(int aSignal)
6873
{
6974
OT_UNUSED_VARIABLE(aSignal);
@@ -196,6 +201,12 @@ void otSysInit(int aArgCount, char *aArgVector[])
196201
signal(SIGTERM, &handleSignal);
197202
signal(SIGHUP, &handleSignal);
198203

204+
#if OPENTHREAD_PSA_CRYPTO_NATIVE_ITS_FILE && (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
205+
snprintf(sNativeItsFileNamePrefix, sizeof(sNativeItsFileNamePrefix), "%s/%s_%d_",
206+
OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH, getenv("PORT_OFFSET") ? getenv("PORT_OFFSET") : "0", gNodeId);
207+
gItsFileNamePrefix = sNativeItsFileNamePrefix;
208+
#endif
209+
199210
platformLoggingInit(basename(aArgVector[0]));
200211
platformAlarmInit(speedUpFactor);
201212
platformRadioInit();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ static bool sUseUnixSocket = false;
6464
int gArgumentsCount = 0;
6565
char **gArguments = NULL;
6666

67+
#if OPENTHREAD_PSA_CRYPTO_NATIVE_ITS_FILE && (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
68+
static char sNativeItsFileNamePrefix[256];
69+
extern const char *gItsFileNamePrefix;
70+
#endif
71+
6772
uint64_t sNow = 0; // microseconds
6873
int sSockFd;
6974
uint16_t sPortBase = 9000;
@@ -273,6 +278,12 @@ void otSysInit(int argc, char *argv[])
273278
DieNow(OT_EXIT_FAILURE);
274279
}
275280

281+
#if OPENTHREAD_PSA_CRYPTO_NATIVE_ITS_FILE && (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
282+
snprintf(sNativeItsFileNamePrefix, sizeof(sNativeItsFileNamePrefix), "%s/%s_%d_",
283+
OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH, getenv("PORT_OFFSET") ? getenv("PORT_OFFSET") : "0", gNodeId);
284+
gItsFileNamePrefix = sNativeItsFileNamePrefix;
285+
#endif
286+
276287
socket_init();
277288

278289
platformAlarmInit(1);

src/core/instance/instance.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ Instance::Instance(void)
309309
, mIsInitialized(false)
310310
, mId(Random::NonCrypto::GetUint32())
311311
{
312+
#if OPENTHREAD_MTD || OPENTHREAD_FTD
312313
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE && OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
313314
#if OPENTHREAD_CONFIG_MULTIPLE_STATIC_INSTANCE_ENABLE
314315
mCryptoStorageKeyRefManager.SetKeyRefExtraOffset(Crypto::Storage::KeyRefManager::kKeyRefExtraOffset * GetIdx(this));
@@ -317,6 +318,7 @@ Instance::Instance(void)
317318
"The `KeyRef` values will be shared across different `Instance` objects"
318319
#endif
319320
#endif
321+
#endif
320322
}
321323

322324
#if (OPENTHREAD_MTD || OPENTHREAD_FTD) && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE

tests/gtest/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ add_library(ot-fake-platform
4242
)
4343
target_link_libraries(ot-fake-platform
4444
ot-config
45+
${OT_MBEDTLS}
46+
openthread-native-its-ram
4547
)
4648

4749
add_library(ot-fake-ftd INTERFACE)

tests/gtest/fake_platform.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
#include <openthread/platform/trel.h>
5151
#include <openthread/platform/udp.h>
5252

53+
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA)
54+
#include <psa/crypto.h>
55+
#endif
56+
5357
using namespace ot;
5458

5559
bool operator<(const otExtAddress &aLeft, const otExtAddress &aRight)
@@ -482,6 +486,36 @@ otError otPlatEntropyGet(uint8_t *aOutput, uint16_t aOutputLength)
482486
return error;
483487
}
484488

489+
#if (OPENTHREAD_CONFIG_CRYPTO_LIB == OPENTHREAD_CONFIG_CRYPTO_LIB_PSA) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
490+
/**
491+
* When OpenThread is compiled with the PSA Crypto backend using Mbed TLS 3.x, there is no
492+
* API to configure a dedicated non-default entropy source. It is documented that a future version of
493+
* Mbed TLS (likely 4.x) will include a PSA interface for configuring entropy sources.
494+
*
495+
* For now, we need to define the external RNG. Since the implementation of `otPlatEntropyGet` already
496+
* uses CSPRNG, we will call it here as well.
497+
*/
498+
extern "C" psa_status_t mbedtls_psa_external_get_random(mbedtls_psa_external_random_context_t *context,
499+
uint8_t *output,
500+
size_t output_size,
501+
size_t *output_length)
502+
{
503+
OT_UNUSED_VARIABLE(context);
504+
505+
otError error;
506+
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
507+
508+
error = otPlatEntropyGet(output, (uint16_t)output_size);
509+
if (error == OT_ERROR_NONE)
510+
{
511+
*output_length = output_size;
512+
status = PSA_SUCCESS;
513+
}
514+
515+
return status;
516+
}
517+
#endif
518+
485519
void otPlatDiagSetOutputCallback(otInstance *, otPlatDiagOutputCallback, void *) {}
486520

487521
void otPlatDiagModeSet(bool) {}

0 commit comments

Comments
 (0)