Skip to content

Commit fe5f682

Browse files
Vge0rgerlubos
authored andcommitted
nrf_security: Refactor writing of SICR keys
This refactors how SICR keys are writen into MRAM. This refactors the code but it should not change any funcionality. The purpose of this is to make the MRAM writing part reusable so it can be used by the revocation functionality later. Signed-off-by: Georgios Vasilakis <[email protected]>
1 parent 7291ac8 commit fe5f682

File tree

1 file changed

+36
-30
lines changed
  • subsys/nrf_security/src/drivers/cracen/cracenpsa/src/platform_keys

1 file changed

+36
-30
lines changed

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

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,39 @@ static key_type find_key(uint32_t id, platform_key *key)
286286
return INVALID;
287287
}
288288

289+
static void write_sicr_key_to_mram(platform_key *key, uint32_t sicr_attr, const uint8_t *key_buffer,
290+
size_t key_buffer_size)
291+
{
292+
NRF_MRAMC_Type *mramc = (NRF_MRAMC_Type *)DT_REG_ADDR(DT_NODELABEL(mramc));
293+
nrf_mramc_config_t mramc_config, mramc_config_write_enabled;
294+
nrf_mramc_readynext_timeout_t readynext_timeout, short_readynext_timeout;
295+
296+
nrf_mramc_config_get(mramc, &mramc_config);
297+
mramc_config_write_enabled = mramc_config;
298+
299+
/* Ensure MRAMC is configured for SICR writing */
300+
mramc_config_write_enabled.mode_write = NRF_MRAMC_MODE_WRITE_DIRECT;
301+
302+
nrf_mramc_config_set(mramc, &mramc_config_write_enabled);
303+
304+
memcpy(key->sicr.attr_addr, &sicr_attr, sizeof(sicr_attr));
305+
memcpy(key->sicr.key_buffer, key_buffer, key_buffer_size);
306+
307+
nrf_mramc_readynext_timeout_get(mramc, &readynext_timeout);
308+
309+
/* Ensure that nonce is committed to MRAM by setting MRAMC READYNEXT timeout to 0 */
310+
short_readynext_timeout.value = 0;
311+
short_readynext_timeout.direct_write = true;
312+
nrf_mramc_readynext_timeout_set(mramc, &short_readynext_timeout);
313+
314+
/* Only store the 4 first bytes of the nonce, the rest are padded with zeros */
315+
memcpy(key->sicr.nonce_addr, &key->sicr.nonce, sizeof(key->sicr.nonce[0]));
316+
317+
/* Restore MRAMC config */
318+
nrf_mramc_config_set(mramc, &mramc_config);
319+
nrf_mramc_readynext_timeout_set(mramc, &readynext_timeout);
320+
}
321+
289322
/**
290323
* @brief Checks whether key usage from a certain domain can access key.
291324
*
@@ -669,40 +702,13 @@ psa_status_t cracen_platform_keys_provision(const psa_key_attributes_t *attribut
669702
return status;
670703
}
671704

672-
uint32_t attr = (key.sicr.bits << 16) | key.sicr.type;
673-
674-
NRF_MRAMC_Type *mramc = (NRF_MRAMC_Type *)DT_REG_ADDR(DT_NODELABEL(mramc));
675-
nrf_mramc_config_t mramc_config, mramc_config_write_enabled;
676-
nrf_mramc_readynext_timeout_t readynext_timeout, short_readynext_timeout;
677-
678-
nrf_mramc_config_get(mramc, &mramc_config);
679-
mramc_config_write_enabled = mramc_config;
680-
681-
/* Ensure MRAMC is configured for SICR writing */
682-
mramc_config_write_enabled.mode_write = NRF_MRAMC_MODE_WRITE_DIRECT;
683-
684-
nrf_mramc_config_set(mramc, &mramc_config_write_enabled);
705+
uint32_t sicr_attr = (key.sicr.bits << 16) | key.sicr.type;
685706

686-
memcpy(key.sicr.attr_addr, &attr, sizeof(attr));
687707
if (key.sicr.type == PSA_KEY_TYPE_AES) {
688-
memcpy(key.sicr.key_buffer, encrypted_key, key_buffer_size);
708+
write_sicr_key_to_mram(&key, sicr_attr, encrypted_key, key_buffer_size);
689709
} else {
690-
memcpy(key.sicr.key_buffer, key_buffer, key_buffer_size);
710+
write_sicr_key_to_mram(&key, sicr_attr, key_buffer, key_buffer_size);
691711
}
692712

693-
nrf_mramc_readynext_timeout_get(mramc, &readynext_timeout);
694-
695-
/* Ensure that nonce is committed to MRAM by setting MRAMC READYNEXT timeout to 0 */
696-
short_readynext_timeout.value = 0;
697-
short_readynext_timeout.direct_write = true;
698-
nrf_mramc_readynext_timeout_set(mramc, &short_readynext_timeout);
699-
700-
/* Only store the 4 first bytes of the nonce, the rest are padded with zeros */
701-
memcpy(key.sicr.nonce_addr, &key.sicr.nonce, sizeof(key.sicr.nonce[0]));
702-
703-
/* Restore MRAMC config */
704-
nrf_mramc_config_set(mramc, &mramc_config);
705-
nrf_mramc_readynext_timeout_set(mramc, &readynext_timeout);
706-
707713
return status;
708714
}

0 commit comments

Comments
 (0)