Skip to content

Commit

Permalink
crypto: cracen: Move mutex to Mbed TLS threading_alt.c
Browse files Browse the repository at this point in the history
-This commit ensures that the check if we are calling from
 ISR and/or kernel is used for CRACEN (which doesn't allow
 mutex-interaction).
-This commit checks for failures to lock with asserts
-Change cracen to use mbedtls_mutex_lock/unlock
-Change cracen mutexs init to happen in threading_alt.c through
 the post-kernel SYS_INIT.

Signed-off-by: Frank Audun Kvamtrø <[email protected]>
  • Loading branch information
frkv committed Oct 24, 2024
1 parent cbcfa64 commit 5f6a719
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 27 deletions.
15 changes: 9 additions & 6 deletions subsys/nrf_security/src/drivers/cracen/cracenpsa/src/cracen.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

static int users;

NRF_SECURITY_MUTEX_DEFINE(cracen_mutex);
extern mbedtls_threading_mutex_t cracen_mutex;

LOG_MODULE_REGISTER(cracen, CONFIG_CRACEN_LOG_LEVEL);

Expand All @@ -51,7 +51,8 @@ static void cracen_load_microcode(void)

void cracen_acquire(void)
{
nrf_security_mutex_lock(&cracen_mutex);
__ASSERT(mbedtls_mutex_lock(&cracen_mutex) == 0,
"cracen_mutex not initialized (lock)");

if (users++ == 0) {
nrf_cracen_module_enable(NRF_CRACEN, CRACEN_ENABLE_CRYPTOMASTER_Msk |
Expand All @@ -61,13 +62,14 @@ void cracen_acquire(void)
LOG_DBG_MSG("Powered on CRACEN.");
}

nrf_security_mutex_unlock(&cracen_mutex);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex) == 0,
"cracen_mutex not initialized (unlock)");
}

void cracen_release(void)
{
nrf_security_mutex_lock(&cracen_mutex);

__ASSERT(mbedtls_mutex_lock(&cracen_mutex) == 0,
"cracen_mutex not initialized (lock)");
if (--users == 0) {
/* Disable IRQs in the ARM NVIC as the first operation to be
* sure no IRQs fire while we are turning CRACEN off.
Expand Down Expand Up @@ -102,7 +104,8 @@ void cracen_release(void)
LOG_DBG_MSG("Powered off CRACEN.");
}

nrf_security_mutex_unlock(&cracen_mutex);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex) == 0,
"cracen_mutex not initialized (unlock)");
}

#define CRACEN_NOT_INITIALIZED 0x207467
Expand Down
14 changes: 9 additions & 5 deletions subsys/nrf_security/src/drivers/cracen/cracenpsa/src/ctr_drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
static cracen_prng_context_t prng;

NRF_SECURITY_MUTEX_DEFINE(cracen_prng_context_mutex);
extern mbedtls_threading_mutex_t cracen_mutex_prng_context;

/*
* @brief Internal function to enable TRNG and get entropy for initial seed and
Expand Down Expand Up @@ -129,7 +129,8 @@ psa_status_t cracen_init_random(cracen_prng_context_t *context)
return PSA_SUCCESS;
}

nrf_security_mutex_lock(&cracen_prng_context_mutex);
__ASSERT(mbedtls_mutex_lock(&cracen_mutex_prng_context) == 0,
"cracen_mutex_prng_context not initialized (lock)");
safe_memset(&prng, sizeof(prng), 0, sizeof(prng));

/* Get the entropy used to seed the DRBG */
Expand All @@ -153,7 +154,8 @@ psa_status_t cracen_init_random(cracen_prng_context_t *context)
prng.initialized = CRACEN_PRNG_INITIALIZED;

exit:
nrf_security_mutex_unlock(&cracen_prng_context_mutex);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex_prng_context) == 0,
"cracen_mutex_prng_context not initialized (unlock)");

return silex_statuscodes_to_psa(sx_err);
}
Expand All @@ -173,7 +175,8 @@ psa_status_t cracen_get_random(cracen_prng_context_t *context, uint8_t *output,
return PSA_ERROR_INVALID_ARGUMENT;
}

nrf_security_mutex_lock(&cracen_prng_context_mutex);
__ASSERT(mbedtls_mutex_lock(&cracen_mutex_prng_context) == 0,
"cracen_mutex_prng_context not initialized (lock)");

if (prng.reseed_counter == 0) {
status = cracen_init_random(context);
Expand Down Expand Up @@ -238,7 +241,8 @@ psa_status_t cracen_get_random(cracen_prng_context_t *context, uint8_t *output,
prng.reseed_counter += 1;

exit:
nrf_security_mutex_unlock(&cracen_prng_context_mutex);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex_prng_context) == 0,
"cracen_mutex_prng_context not initialized (unlock)");
return status;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1341,15 +1341,17 @@ psa_status_t cracen_export_key(const psa_key_attributes_t *attributes, const uin
* use case. Here the decision was to avoid defining another mutex to handle the
* push buffer for the rest of the use cases.
*/
nrf_security_mutex_lock(&cracen_mutex_symmetric);
__ASSERT(mbedtls_mutex_lock(&cracen_mutex_symmetric) == 0,
"cracen_mutex_symmetric not initialized (lock)");
status = cracen_kmu_prepare_key(key_buffer);
if (status == SX_OK) {
memcpy(data, kmu_push_area, key_out_size);
*data_length = key_out_size;
}

(void)cracen_kmu_clean_key(key_buffer);
nrf_security_mutex_unlock(&cracen_mutex_symmetric);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex_symmetric) == 0,
"cracen_mutex_symmetric not initialized (unlock)");

return silex_statuscodes_to_psa(status);
}
Expand Down Expand Up @@ -1385,7 +1387,8 @@ psa_status_t cracen_copy_key(psa_key_attributes_t *attributes, const uint8_t *so
psa_status_t psa_status;
size_t key_size = PSA_BITS_TO_BYTES(psa_get_key_bits(attributes));

nrf_security_mutex_lock(&cracen_mutex_symmetric);
__ASSERT(mbedtls_mutex_lock(&cracen_mutex_symmetric) == 0,
"cracen_mutex_symmetric not initialized (lock)");
status = cracen_kmu_prepare_key(source_key);

if (status == SX_OK) {
Expand All @@ -1397,7 +1400,8 @@ psa_status_t cracen_copy_key(psa_key_attributes_t *attributes, const uint8_t *so
}

(void)cracen_kmu_clean_key(source_key);
nrf_security_mutex_unlock(&cracen_mutex_symmetric);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex_symmetric) == 0,
"cracen_mutex_symmetric not initialized (unlock)");

if (status != SX_OK) {
return silex_statuscodes_to_psa(status);
Expand Down
6 changes: 4 additions & 2 deletions subsys/nrf_security/src/drivers/cracen/cracenpsa/src/kmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,15 @@ static psa_status_t push_kmu_key_to_ram(uint8_t *key_buffer, size_t key_buffer_s
* Here the decision was to avoid defining another mutex to handle the push buffer for the
* rest of the use cases.
*/
nrf_security_mutex_lock(&cracen_mutex_symmetric);
__ASSERT(mbedtls_mutex_lock(&cracen_mutex_symmetric) == 0,
"cracen_mutex_symmetric not initialized (lock)");
status = silex_statuscodes_to_psa(cracen_kmu_prepare_key(key_buffer));
if (status == PSA_SUCCESS) {
memcpy(key_buffer, kmu_push_area, key_buffer_size);
safe_memzero(kmu_push_area, sizeof(kmu_push_area));
}
nrf_security_mutex_unlock(&cracen_mutex_symmetric);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex_symmetric) == 0,
"cracen_mutex_symmetric not initialized (unlock)");

return status;
}
Expand Down
10 changes: 7 additions & 3 deletions subsys/nrf_security/src/drivers/cracen/cracenpsa/src/prng_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ static uint32_t prng_pool[PRNG_POOL_SIZE];
static uint32_t prng_pool_remaining;


NRF_SECURITY_MUTEX_DEFINE(cracen_prng_pool_mutex);
extern mbedtls_threading_mutex_t cracen_mutex_prng_pool;



int cracen_prng_value_from_pool(uint32_t *prng_value)
{
int status = SX_OK;

nrf_security_mutex_lock(&cracen_prng_pool_mutex);
__ASSERT(mbedtls_mutex_lock(&cracen_mutex_prng_pool) == 0,
"cracen_mutex_prng_pool not initialized (lock)");

if (prng_pool_remaining == 0) {
psa_status_t psa_status =
Expand All @@ -47,6 +50,7 @@ int cracen_prng_value_from_pool(uint32_t *prng_value)
prng_pool_remaining--;

exit:
nrf_security_mutex_unlock(&cracen_prng_pool_mutex);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex_prng_pool) == 0,
"cracen_mutex_prng_pool not initialized (unlock)");
return status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct sx_pk_cnx {

struct sx_pk_cnx silex_pk_engine;

NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_asymmetric);
extern mbedtls_threading_mutex_t cracen_mutex_asymmetric;

bool ba414ep_is_busy(sx_pk_req *req)
{
Expand Down Expand Up @@ -183,7 +183,9 @@ struct sx_pk_acq_req sx_pk_acquire_req(const struct sx_pk_cmd_def *cmd)
{
struct sx_pk_acq_req req = {NULL, SX_OK};

nrf_security_mutex_lock(&cracen_mutex_asymmetric);
__ASSERT(mbedtls_mutex_lock(&cracen_mutex_asymmetric) == 0,
"cracen_mutex_asymmetric not initialized (lock)");

req.req = &silex_pk_engine.instance;
req.req->cmd = cmd;
req.req->cnx = &silex_pk_engine;
Expand Down Expand Up @@ -220,7 +222,8 @@ void sx_pk_release_req(sx_pk_req *req)
cracen_release();
req->cmd = NULL;
req->userctxt = NULL;
nrf_security_mutex_unlock(&cracen_mutex_asymmetric);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex_asymmetric) == 0,
"cracen_mutex_asymmetric not initialized (unlock)");
}

struct sx_regs *sx_pk_get_regs(void)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
*/
#define CMDMA_INTMASK_EN ((1 << 2) | (1 << 5) | (1 << 4))

NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_symmetric);
extern mbedtls_threading_mutex_t cracen_mutex_symmetric;

void sx_hw_reserve(struct sx_dmactl *dma)
{
cracen_acquire();
nrf_security_mutex_lock(&cracen_mutex_symmetric);

__ASSERT(mbedtls_mutex_lock(&cracen_mutex_symmetric) == 0,
"cracen_mutex_symmetric not initialized (lock)");
if (dma) {
dma->hw_acquired = true;
}
Expand All @@ -48,7 +48,8 @@ void sx_cmdma_release_hw(struct sx_dmactl *dma)
{
if (dma == NULL || dma->hw_acquired) {
cracen_release();
nrf_security_mutex_unlock(&cracen_mutex_symmetric);
__ASSERT(mbedtls_mutex_unlock(&cracen_mutex_symmetric) == 0,
"cracen_mutex_symmetric not initialized (unlock)");
if (dma) {
dma->hw_acquired = false;
}
Expand Down
15 changes: 15 additions & 0 deletions subsys/nrf_security/src/threading/threading_alt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ NRF_SECURITY_MUTEX_DEFINE(mbedtls_threading_key_slot_mutex);
NRF_SECURITY_MUTEX_DEFINE(mbedtls_threading_psa_globaldata_mutex);
NRF_SECURITY_MUTEX_DEFINE(mbedtls_threading_psa_rngdata_mutex);

#if defined(CONFIG_PSA_CRYPTO_DRIVER_CRACEN)
NRF_SECURITY_MUTEX_DEFINE(cracen_mutex);
NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_prng_context);
NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_prng_pool);
NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_asymmetric);
NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_symmetric);
#endif

static void mbedtls_mutex_init_fn(mbedtls_threading_mutex_t * mutex)
{
if(!k_is_pre_kernel() && !k_is_in_isr()) {
Expand Down Expand Up @@ -66,6 +74,13 @@ static int post_kernel_init(void)
mbedtls_mutex_init(&mbedtls_threading_key_slot_mutex);
mbedtls_mutex_init(&mbedtls_threading_psa_globaldata_mutex);
mbedtls_mutex_init(&mbedtls_threading_psa_rngdata_mutex);
#if defined(CONFIG_PSA_CRYPTO_DRIVER_CRACEN)
mbedtls_mutex_init(&cracen_mutex);
mbedtls_mutex_init(&cracen_mutex_prng_context);
mbedtls_mutex_init(&cracen_mutex_prng_pool);
mbedtls_mutex_init(&cracen_mutex_asymmetric);
mbedtls_mutex_init(&cracen_mutex_symmetric);
#endif
return 0;
}

Expand Down

0 comments on commit 5f6a719

Please sign in to comment.