Skip to content

Commit 450b6a5

Browse files
AntonZmarlubos
authored andcommitted
nrf_security: CRACEN: Keep TRNG initialized while fetching entropy
The internal trng_get_entropy() function calls sx_trng_get() iteratively to get the required number of TRNG bytes since it's recommended to not request more than 32 bytes of data. This commit is aimed to increase performance by keeping TRNG initialized between calls to sx_trng_get() till the required number of entropy bytes is not read. For instance, for nRF54LM20A 384-bit seed generation before this optimization took approx. 8.7ms and 5ms after. Signed-off-by: Anton Zyma <anton.zyma@nordicsemi.no>
1 parent aecd6e7 commit 450b6a5

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,17 @@ NRF_SECURITY_MUTEX_DEFINE(cracen_prng_trng_mutex);
6767
*
6868
* @return 0 on success, nonzero on failure.
6969
*/
70-
static int trng_get_entropy(uint8_t *dst, int num_trng_bytes)
70+
static int trng_get_entropy(uint8_t *dst, size_t num_trng_bytes)
7171
{
72-
int sx_err;
72+
int sx_err = SX_ERR_RESET_NEEDED;
7373
struct sx_trng trng;
74-
int trng_chunk_size;
74+
size_t trng_chunk_size;
7575
bool loop_continue;
7676

7777
while (num_trng_bytes) {
7878
/* The sx_trng_get function suggests to return <= 32 bytes at a time */
7979
trng_chunk_size = MIN(num_trng_bytes, 32);
8080

81-
sx_err = SX_ERR_RESET_NEEDED;
8281
loop_continue = true;
8382
while (loop_continue) {
8483
switch (sx_err) {
@@ -104,13 +103,15 @@ static int trng_get_entropy(uint8_t *dst, int num_trng_bytes)
104103
}
105104
}
106105

107-
(void)sx_trng_close(&trng);
108-
if (sx_err != SX_OK) {
109-
return sx_err;
110-
}
111-
112106
num_trng_bytes -= trng_chunk_size;
113107
dst += trng_chunk_size;
108+
109+
if (num_trng_bytes && sx_err == SX_OK) {
110+
sx_err = SX_ERR_HW_PROCESSING;
111+
} else {
112+
(void)sx_trng_close(&trng);
113+
return sx_err;
114+
}
114115
}
115116

116117
return SX_OK;

0 commit comments

Comments
 (0)