Skip to content

Commit 16654ce

Browse files
Vge0rgerlubos
authored andcommitted
nrf_security: Refactor mutexes
This refactors the mutexes in nrf_security. The main issue with the current design is that that it doesn't use the Zephyr macro K_MUTEX_DEFINE to define the mutexes. If this macro is not used the Zephyr mutexes need to be initialized at runtime, but the Cracen code doesn't initalize them and so they don't work properly. This simplifies the nrf_security mutexes by removing the flag. This keep the design simpler and less error prone. An extra flag added to the Zephyr mutexes doesn't seem to be necessary. The nrf_security mutexes were added in order to be used by the Cracen driver and reduce the ifdef conditions inside the driver code for tfm/non tfm builds. No initialization is required for these mutexes because there are always meant to be statically defined and this removes the need for initialization as long as we use the Zephyr macros for this. The mbedtls prefixed mutexes used by the PSA core are now changed to direcly use the zephyr mutexes. This makes the design simpler again, since we remove the connection with the nrf_security mutexes. The mbedlts prefixed mutexes are always under ifdef conditions inside the PSA core. So using the mechanism of the nrf_security mutexes doesn't improve anything. Since both the nrf_security mutexes and the mbedtls prefixed mutexes indide PSA core are basically just a front end which use the Zephyr mutexes its better to keep them separated. Signed-off-by: Georgios Vasilakis <[email protected]>
1 parent 2272c54 commit 16654ce

File tree

11 files changed

+69
-141
lines changed

11 files changed

+69
-141
lines changed

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/cracen.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void cracen_load_microcode(void)
5151

5252
void cracen_acquire(void)
5353
{
54-
nrf_security_mutex_lock(&cracen_mutex);
54+
nrf_security_mutex_lock(cracen_mutex);
5555

5656
if (users++ == 0) {
5757
nrf_cracen_module_enable(NRF_CRACEN, CRACEN_ENABLE_CRYPTOMASTER_Msk |
@@ -61,12 +61,12 @@ void cracen_acquire(void)
6161
LOG_DBG_MSG("Powered on CRACEN.");
6262
}
6363

64-
nrf_security_mutex_unlock(&cracen_mutex);
64+
nrf_security_mutex_unlock(cracen_mutex);
6565
}
6666

6767
void cracen_release(void)
6868
{
69-
nrf_security_mutex_lock(&cracen_mutex);
69+
nrf_security_mutex_lock(cracen_mutex);
7070

7171
if (--users == 0) {
7272
/* Disable IRQs in the ARM NVIC as the first operation to be
@@ -102,7 +102,7 @@ void cracen_release(void)
102102
LOG_DBG_MSG("Powered off CRACEN.");
103103
}
104104

105-
nrf_security_mutex_unlock(&cracen_mutex);
105+
nrf_security_mutex_unlock(cracen_mutex);
106106
}
107107

108108
#define CRACEN_NOT_INITIALIZED 0x207467

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/ctr_drbg.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ psa_status_t cracen_init_random(cracen_prng_context_t *context)
129129
return PSA_SUCCESS;
130130
}
131131

132-
nrf_security_mutex_lock(&cracen_prng_context_mutex);
132+
nrf_security_mutex_lock(cracen_prng_context_mutex);
133133
safe_memset(&prng, sizeof(prng), 0, sizeof(prng));
134134

135135
/* Get the entropy used to seed the DRBG */
@@ -153,7 +153,7 @@ psa_status_t cracen_init_random(cracen_prng_context_t *context)
153153
prng.initialized = CRACEN_PRNG_INITIALIZED;
154154

155155
exit:
156-
nrf_security_mutex_unlock(&cracen_prng_context_mutex);
156+
nrf_security_mutex_unlock(cracen_prng_context_mutex);
157157

158158
return silex_statuscodes_to_psa(sx_err);
159159
}
@@ -173,9 +173,13 @@ psa_status_t cracen_get_random(cracen_prng_context_t *context, uint8_t *output,
173173
return PSA_ERROR_INVALID_ARGUMENT;
174174
}
175175

176-
nrf_security_mutex_lock(&cracen_prng_context_mutex);
176+
nrf_security_mutex_lock(cracen_prng_context_mutex);
177177

178178
if (prng.reseed_counter == 0) {
179+
/* Zephyr mutexes allow the same thread to lock a
180+
* mutex multiple times. So we can call cracen_init_random
181+
* here even though we hold the mutex.
182+
*/
179183
status = cracen_init_random(context);
180184

181185
if (status != PSA_SUCCESS) {
@@ -238,7 +242,7 @@ psa_status_t cracen_get_random(cracen_prng_context_t *context, uint8_t *output,
238242
prng.reseed_counter += 1;
239243

240244
exit:
241-
nrf_security_mutex_unlock(&cracen_prng_context_mutex);
245+
nrf_security_mutex_unlock(cracen_prng_context_mutex);
242246
return status;
243247
}
244248

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/key_management.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
extern const uint8_t cracen_N3072[384];
3333

34-
extern mbedtls_threading_mutex_t cracen_mutex_symmetric;
34+
extern nrf_security_mutex_t cracen_mutex_symmetric;
3535

3636
#define DEFAULT_KEY_SIZE(bits) (bits), PSA_BITS_TO_BYTES(bits), (1 + 2 * PSA_BITS_TO_BYTES(bits))
3737
static struct {
@@ -1341,15 +1341,15 @@ psa_status_t cracen_export_key(const psa_key_attributes_t *attributes, const uin
13411341
* use case. Here the decision was to avoid defining another mutex to handle the
13421342
* push buffer for the rest of the use cases.
13431343
*/
1344-
nrf_security_mutex_lock(&cracen_mutex_symmetric);
1344+
nrf_security_mutex_lock(cracen_mutex_symmetric);
13451345
status = cracen_kmu_prepare_key(key_buffer);
13461346
if (status == SX_OK) {
13471347
memcpy(data, kmu_push_area, key_out_size);
13481348
*data_length = key_out_size;
13491349
}
13501350

13511351
(void)cracen_kmu_clean_key(key_buffer);
1352-
nrf_security_mutex_unlock(&cracen_mutex_symmetric);
1352+
nrf_security_mutex_unlock(cracen_mutex_symmetric);
13531353

13541354
return silex_statuscodes_to_psa(status);
13551355
}
@@ -1385,7 +1385,7 @@ psa_status_t cracen_copy_key(psa_key_attributes_t *attributes, const uint8_t *so
13851385
psa_status_t psa_status;
13861386
size_t key_size = PSA_BITS_TO_BYTES(psa_get_key_bits(attributes));
13871387

1388-
nrf_security_mutex_lock(&cracen_mutex_symmetric);
1388+
nrf_security_mutex_lock(cracen_mutex_symmetric);
13891389
status = cracen_kmu_prepare_key(source_key);
13901390

13911391
if (status == SX_OK) {
@@ -1397,7 +1397,7 @@ psa_status_t cracen_copy_key(psa_key_attributes_t *attributes, const uint8_t *so
13971397
}
13981398

13991399
(void)cracen_kmu_clean_key(source_key);
1400-
nrf_security_mutex_unlock(&cracen_mutex_symmetric);
1400+
nrf_security_mutex_unlock(cracen_mutex_symmetric);
14011401

14021402
if (status != SX_OK) {
14031403
return silex_statuscodes_to_psa(status);

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/kmu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#define SECONDARY_SLOT_METADATA_VALUE UINT32_MAX
3131

32-
extern mbedtls_threading_mutex_t cracen_mutex_symmetric;
32+
extern nrf_security_mutex_t cracen_mutex_symmetric;
3333

3434
/* The section .nrf_kmu_reserved_push_area is placed at the top RAM address
3535
* by the linker scripts. We do that for both the secure and non-secure builds.
@@ -844,13 +844,13 @@ static psa_status_t push_kmu_key_to_ram(uint8_t *key_buffer, size_t key_buffer_s
844844
* Here the decision was to avoid defining another mutex to handle the push buffer for the
845845
* rest of the use cases.
846846
*/
847-
nrf_security_mutex_lock(&cracen_mutex_symmetric);
847+
nrf_security_mutex_lock(cracen_mutex_symmetric);
848848
status = silex_statuscodes_to_psa(cracen_kmu_prepare_key(key_buffer));
849849
if (status == PSA_SUCCESS) {
850850
memcpy(key_buffer, kmu_push_area, key_buffer_size);
851851
safe_memzero(kmu_push_area, sizeof(kmu_push_area));
852852
}
853-
nrf_security_mutex_unlock(&cracen_mutex_symmetric);
853+
nrf_security_mutex_unlock(cracen_mutex_symmetric);
854854

855855
return status;
856856
}

subsys/nrf_security/src/drivers/cracen/cracenpsa/src/prng_pool.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int cracen_prng_value_from_pool(uint32_t *prng_value)
3030
{
3131
int status = SX_OK;
3232

33-
nrf_security_mutex_lock(&cracen_prng_pool_mutex);
33+
nrf_security_mutex_lock(cracen_prng_pool_mutex);
3434

3535
if (prng_pool_remaining == 0) {
3636
psa_status_t psa_status =
@@ -47,6 +47,6 @@ int cracen_prng_value_from_pool(uint32_t *prng_value)
4747
prng_pool_remaining--;
4848

4949
exit:
50-
nrf_security_mutex_unlock(&cracen_prng_pool_mutex);
50+
nrf_security_mutex_unlock(cracen_prng_pool_mutex);
5151
return status;
5252
}

subsys/nrf_security/src/drivers/cracen/silexpk/target/baremetal_ba414e_with_ik/pk_baremetal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ struct sx_pk_acq_req sx_pk_acquire_req(const struct sx_pk_cmd_def *cmd)
183183
{
184184
struct sx_pk_acq_req req = {NULL, SX_OK};
185185

186-
nrf_security_mutex_lock(&cracen_mutex_asymmetric);
186+
nrf_security_mutex_lock(cracen_mutex_asymmetric);
187187
req.req = &silex_pk_engine.instance;
188188
req.req->cmd = cmd;
189189
req.req->cnx = &silex_pk_engine;
@@ -220,7 +220,7 @@ void sx_pk_release_req(sx_pk_req *req)
220220
cracen_release();
221221
req->cmd = NULL;
222222
req->userctxt = NULL;
223-
nrf_security_mutex_unlock(&cracen_mutex_asymmetric);
223+
nrf_security_mutex_unlock(cracen_mutex_asymmetric);
224224
}
225225

226226
struct sx_regs *sx_pk_get_regs(void)

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/platform/baremetal/cmdma_hw.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_symmetric);
2929
void sx_hw_reserve(struct sx_dmactl *dma)
3030
{
3131
cracen_acquire();
32-
nrf_security_mutex_lock(&cracen_mutex_symmetric);
32+
nrf_security_mutex_lock(cracen_mutex_symmetric);
3333

3434
if (dma) {
3535
dma->hw_acquired = true;
@@ -48,7 +48,7 @@ void sx_cmdma_release_hw(struct sx_dmactl *dma)
4848
{
4949
if (dma == NULL || dma->hw_acquired) {
5050
cracen_release();
51-
nrf_security_mutex_unlock(&cracen_mutex_symmetric);
51+
nrf_security_mutex_unlock(cracen_mutex_symmetric);
5252
if (dma) {
5353
dma->hw_acquired = false;
5454
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/*
2-
* Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
33
*
4-
* SPDX-License-Identifier: BSD-3-Clause
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

77
#ifndef MBEDTLS_THREADING_ALT_H
88
#define MBEDTLS_THREADING_ALT_H
99

10-
#include "mbedtls/build_info.h"
11-
#include "nrf_security_mutexes.h"
10+
#include <zephyr/kernel.h>
11+
12+
typedef struct k_mutex mbedtls_threading_mutex_t;
1213

1314
#endif /* MBEDTLS_THREADING_ALT_H */
Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
21
/*
3-
* Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
43
*
5-
* SPDX-License-Identifier: BSD-3-Clause OR Arm’s non-OSI source license
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
65
*/
76

87
#include "threading_alt.h"
@@ -11,62 +10,32 @@
1110
#include <string.h>
1211
#include <zephyr/init.h>
1312
#include <zephyr/kernel.h>
13+
#include <zephyr/sys/__assert.h>
1414

15-
#include "nrf_security_mutexes.h"
16-
17-
#if !defined(MBEDTLS_CONFIG_FILE)
18-
#include "mbedtls/config.h"
19-
#else
20-
#include MBEDTLS_CONFIG_FILE
21-
#endif
15+
K_MUTEX_DEFINE(mbedtls_threading_key_slot_mutex);
16+
K_MUTEX_DEFINE(mbedtls_threading_psa_globaldata_mutex);
17+
K_MUTEX_DEFINE(mbedtls_threading_psa_rngdata_mutex);
2218

23-
NRF_SECURITY_MUTEX_DEFINE(mbedtls_threading_key_slot_mutex);
24-
NRF_SECURITY_MUTEX_DEFINE(mbedtls_threading_psa_globaldata_mutex);
25-
NRF_SECURITY_MUTEX_DEFINE(mbedtls_threading_psa_rngdata_mutex);
26-
27-
static void mbedtls_mutex_init_fn(mbedtls_threading_mutex_t * mutex)
19+
void mbedtls_mutex_init_fn(mbedtls_threading_mutex_t *mutex)
2820
{
29-
if(!k_is_pre_kernel() && !k_is_in_isr()) {
30-
nrf_security_mutex_init(mutex);
31-
}
21+
k_mutex_init(mutex);
3222
}
3323

34-
static void mbedtls_mutex_free_fn(mbedtls_threading_mutex_t * mutex)
24+
void mbedtls_mutex_free_fn(mbedtls_threading_mutex_t *mutex)
3525
{
36-
if(!k_is_pre_kernel() && !k_is_in_isr()) {
37-
nrf_security_mutex_free(mutex);
38-
}
3926
}
4027

41-
static int mbedtls_mutex_lock_fn(mbedtls_threading_mutex_t * mutex)
28+
int mbedtls_mutex_lock_fn(mbedtls_threading_mutex_t *mutex)
4229
{
43-
if(!k_is_pre_kernel() && !k_is_in_isr()) {
44-
return nrf_security_mutex_lock(mutex);
45-
} else {
46-
return 0;
47-
}
30+
return k_mutex_lock(mutex, K_FOREVER);
4831
}
4932

50-
static int mbedtls_mutex_unlock_fn(mbedtls_threading_mutex_t * mutex)
33+
int mbedtls_mutex_unlock_fn(mbedtls_threading_mutex_t *mutex)
5134
{
52-
if(!k_is_pre_kernel() && !k_is_in_isr()) {
53-
return nrf_security_mutex_unlock(mutex);
54-
} else {
55-
return 0;
56-
}
35+
return k_mutex_unlock(mutex);
5736
}
5837

5938
void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *mutex) = mbedtls_mutex_init_fn;
6039
void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *mutex) = mbedtls_mutex_free_fn;
6140
int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *mutex) = mbedtls_mutex_lock_fn;
6241
int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *mutex) = mbedtls_mutex_unlock_fn;
63-
64-
static int post_kernel_init(void)
65-
{
66-
mbedtls_mutex_init(&mbedtls_threading_key_slot_mutex);
67-
mbedtls_mutex_init(&mbedtls_threading_psa_globaldata_mutex);
68-
mbedtls_mutex_init(&mbedtls_threading_psa_rngdata_mutex);
69-
return 0;
70-
}
71-
72-
SYS_INIT(post_kernel_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

subsys/nrf_security/src/utils/nrf_security_mutexes.c

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,35 @@
1111
#include <stdbool.h>
1212
#include "nrf_security_mutexes.h"
1313

14-
#if !defined(__NRF_TFM__)
14+
#ifdef NRF_SECURITY_MUTEX_IMPLEMENTATION
1515
#include <zephyr/kernel.h>
1616
#endif
1717

18-
#if defined(CONFIG_MULTITHREADING) && !defined(__NRF_TFM__)
19-
20-
void nrf_security_mutex_init(mbedtls_threading_mutex_t * mutex)
18+
#ifdef NRF_SECURITY_MUTEX_IMPLEMENTATION
19+
int nrf_security_mutex_lock(nrf_security_mutex_t mutex)
2120
{
22-
if ((mutex->flags & NRF_SECURITY_MUTEX_FLAGS_INITIALIZED) == 0) {
23-
k_mutex_init(&mutex->mutex);
24-
}
25-
26-
mutex->flags |= NRF_SECURITY_MUTEX_FLAGS_INITIALIZED;
27-
}
21+
int ret = k_mutex_lock(mutex, K_FOREVER);
2822

29-
void nrf_security_mutex_free(mbedtls_threading_mutex_t * mutex)
30-
{
31-
(void)mutex;
23+
__ASSERT_NO_MSG(ret == 0);
24+
return ret;
3225
}
3326

34-
int nrf_security_mutex_lock(mbedtls_threading_mutex_t * mutex)
27+
int nrf_security_mutex_unlock(nrf_security_mutex_t mutex)
3528
{
36-
if ((mutex->flags & NRF_SECURITY_MUTEX_FLAGS_INITIALIZED) != 0) {
37-
return k_mutex_lock(&mutex->mutex, K_FOREVER);
38-
} else {
39-
return -EINVAL;
40-
}
41-
}
29+
int ret = k_mutex_unlock(mutex);
4230

43-
int nrf_security_mutex_unlock(mbedtls_threading_mutex_t * mutex)
44-
{
45-
if ((mutex->flags & NRF_SECURITY_MUTEX_FLAGS_INITIALIZED) != 0) {
46-
return k_mutex_unlock(&mutex->mutex);
47-
} else {
48-
return -EINVAL;
49-
}
31+
__ASSERT_NO_MSG(ret == 0);
32+
return ret;
5033
}
5134

5235
#else
53-
54-
void nrf_security_mutex_init(mbedtls_threading_mutex_t * mutex)
55-
{
56-
(void)mutex;
57-
}
58-
59-
void nrf_security_mutex_free(mbedtls_threading_mutex_t * mutex)
60-
{
61-
(void)mutex;
62-
}
63-
64-
int nrf_security_mutex_lock(mbedtls_threading_mutex_t * mutex)
36+
int nrf_security_mutex_lock(nrf_security_mutex_t mutex)
6537
{
6638
(void)mutex;
6739
return 0;
6840
}
6941

70-
int nrf_security_mutex_unlock(mbedtls_threading_mutex_t * mutex)
42+
int nrf_security_mutex_unlock(nrf_security_mutex_t mutex)
7143
{
7244
(void)mutex;
7345
return 0;

0 commit comments

Comments
 (0)