-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not Review: Move CRACEN mutexes to threading_alt.c #18140
Changes from all commits
e2f7767
0bbe275
2ad6175
71dd95f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,7 +14,7 @@ | |||||||||||
|
||||||||||||
#include "common.h" | ||||||||||||
#include "microcode_binary.h" | ||||||||||||
#include <nrf_security_mutexes.h> | ||||||||||||
#include <threading_alt.h> | ||||||||||||
|
||||||||||||
#if !defined(CONFIG_BUILD_WITH_TFM) | ||||||||||||
#define LOG_ERR_MSG(msg) LOG_ERR(msg) | ||||||||||||
|
@@ -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); | ||||||||||||
|
||||||||||||
|
@@ -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)"); | ||||||||||||
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separate asserts from the actual action, so that the action is still performed even when asserts are disabled.
Suggested change
|
||||||||||||
|
||||||||||||
if (users++ == 0) { | ||||||||||||
nrf_cracen_module_enable(NRF_CRACEN, CRACEN_ENABLE_CRYPTOMASTER_Msk | | ||||||||||||
|
@@ -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)"); | ||||||||||||
Comment on lines
+65
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are asserts on unlock actually needed? Because your assert message suggests that no (they both only fail if the mutex is not initialized). If yes, update the assert message. |
||||||||||||
} | ||||||||||||
|
||||||||||||
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. | ||||||||||||
|
@@ -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 | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -10,7 +10,7 @@ | |||
#include <cracen/statuscodes.h> | ||||
#include <security/cracen.h> | ||||
#include <zephyr/kernel.h> | ||||
#include <nrf_security_mutexes.h> | ||||
#include <threading_alt.h> | ||||
|
||||
/* We want to avoid reserving excessive RAM and invoking | ||||
* the PRNG too often. 32 was arbitrarily chosen here | ||||
|
@@ -24,13 +24,16 @@ static uint32_t prng_pool[PRNG_POOL_SIZE]; | |||
static uint32_t prng_pool_remaining; | ||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
NRF_SECURITY_MUTEX_DEFINE(cracen_prng_pool_mutex); | ||||
extern mbedtls_threading_mutex_t cracen_mutex_prng_pool; | ||||
|
||||
|
||||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too many newlines in here. |
||||
|
||||
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 = | ||||
|
@@ -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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
#include <zephyr/kernel.h> | ||
|
||
#include <cracen/membarriers.h> | ||
#include "../hw/ba414/regs_addr.h" | ||
#include <silexpk/core.h> | ||
#include "../hw/ba414/pkhardware_ba414e.h" | ||
|
@@ -20,7 +21,7 @@ | |
|
||
#include <hal/nrf_cracen.h> | ||
#include <security/cracen.h> | ||
#include <nrf_security_mutexes.h> | ||
#include <threading_alt.h> | ||
|
||
#ifndef ADDR_BA414EP_REGS_BASE | ||
#define ADDR_BA414EP_REGS_BASE CRACEN_ADDR_BA414EP_REGS_BASE | ||
|
@@ -52,7 +53,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) | ||
{ | ||
|
@@ -111,15 +112,19 @@ void sx_pk_wrreg(struct sx_regs *regs, uint32_t addr, uint32_t v) | |
printk("sx_pk_wrreg(addr=0x%x, sum=0x%x, val=0x%x);\r\n", addr, (uint32_t)p, v); | ||
#endif | ||
|
||
wmb(); /* comment for compliance */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems weird to me. |
||
*p = v; | ||
rmb(); /* comment for compliance */ | ||
} | ||
|
||
uint32_t sx_pk_rdreg(struct sx_regs *regs, uint32_t addr) | ||
{ | ||
volatile uint32_t *p = (uint32_t *)(regs->base + addr); | ||
uint32_t v; | ||
|
||
wmb(); /* comment for compliance */ | ||
v = *p; | ||
rmb(); /* comment for compliance */ | ||
|
||
#ifdef INSTRUMENT_MMIO_WITH_PRINTFS | ||
printk("sx_pk_rdreg(addr=0x%x, sum=0x%x);\r\n", addr, (uint32_t)p); | ||
|
@@ -183,7 +188,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; | ||
|
@@ -220,7 +227,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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could those be declared in a header file instead?