Skip to content

Commit 194e742

Browse files
ahasztagnordicjm
authored andcommitted
bootloader: Lock KMU keys
This commit adds locking of KMU keys used by NSIB for signature verification when they are no longer needed. This way, the next stage will not be able to use these keys. Signed-off-by: Artur Hadasz <[email protected]>
1 parent 85816ce commit 194e742

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

include/bl_crypto.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ int bl_root_of_trust_verify_external(const uint8_t *public_key,
101101
const uint8_t *firmware,
102102
const uint32_t firmware_len);
103103

104+
/**
105+
* @brief Perform root of trust housekeeping operations.
106+
*
107+
* This function performs cleanup and security housekeeping tasks for the
108+
* root of trust crypto subsystems. It ensures that cryptographic keys are
109+
* properly secured and non-essential key material is removed from volatile
110+
* memory after the bootloader no longer needs access to them.
111+
*/
112+
void bl_root_of_trust_housekeeping(void);
113+
104114
/**
105115
* @brief Initialize a sha256 operation context variable.
106116
*
@@ -271,6 +281,17 @@ int bl_ed25519_validate(const uint8_t *hash,
271281
uint32_t hash_len,
272282
const uint8_t *signature);
273283

284+
/**
285+
* @brief Perform ED25519 key storage housekeeping operations.
286+
*
287+
* This function performs crypto key storage housekeeping for ED25519 keys.
288+
* It iterates through KMU (Key Management Unit) keys, applies security
289+
* policies by locking them, and purges key material from volatile memory.
290+
* This ensures keys are secured and memory is cleaned up after the bootloader
291+
* completes its cryptographic operations.
292+
*/
293+
void bl_ed25519_keys_housekeeping(void);
294+
274295
/**
275296
* @brief Structure describing the BL_ROT_VERIFY EXT_API.
276297
*/

include/bl_validation.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ int get_monotonic_version(counter_t *version_out);
9393
*/
9494
int get_monotonic_slot(counter_t *slot_out);
9595

96+
/**
97+
* @brief Perform validation of trust housekeeping operations.
98+
*
99+
* This function performs validation-related housekeeping tasks
100+
* It ensures that cryptographic keys and validation-related resources
101+
* are properly secured and cleaned up after validation operations are completed.
102+
*
103+
* Call this function only if you are certain that either the currently
104+
* validated S0/S1 image will be booted, or that neither image will be booted.
105+
* Otherwise, invoking this function could prevent the alternate image
106+
* from booting in the event the current validation fails at a later stage.
107+
*/
108+
void bl_validate_housekeeping(void);
109+
96110
/** @} */
97111

98112
#ifdef __cplusplus

samples/bootloader/src/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ static void validate_and_boot(const struct fw_info *fw_info, counter_t slot)
122122
}
123123
}
124124

125+
/*
126+
* We can lock the keys and other resources now, as any failures
127+
* in the bl_boot function are considered fatal and would prevent
128+
* the alternate image from booting as well.
129+
* Thus, we meet the criteria for calling bl_validate_housekeeping.
130+
*/
131+
bl_validate_housekeeping();
132+
125133
bl_boot(fw_info);
126134
}
127135

@@ -156,5 +164,6 @@ int main(void)
156164
}
157165

158166
printk("No bootable image found. Aborting boot.\r\n");
167+
bl_validate_housekeeping();
159168
return 0;
160169
}

subsys/bootloader/bl_crypto/bl_crypto.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ int bl_root_of_trust_verify_external(
129129
firmware, firmware_len, true);
130130
}
131131

132+
void bl_root_of_trust_housekeeping(void)
133+
{
134+
#if defined(CONFIG_SB_CRYPTO_PSA_ED25519)
135+
bl_ed25519_keys_housekeeping();
136+
#endif
137+
}
138+
132139
#if !defined(CONFIG_BL_SHA256_EXT_API_REQUIRED) && !defined(CONFIG_SB_CRYPTO_NONE)
133140
int bl_sha256_verify(const uint8_t *data, uint32_t data_len, const uint8_t *expected)
134141
{

subsys/bootloader/bl_crypto/bl_crypto_ed25519.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ static psa_key_id_t kmu_key_ids[] = {
2828
BUILD_ASSERT(INT8_MAX >= ARRAY_SIZE(kmu_key_ids),
2929
"Number of KMU keys too big");
3030

31+
void bl_ed25519_keys_housekeeping(void)
32+
{
33+
psa_status_t status;
34+
35+
/* We will continue through all keys, even if we have error while
36+
* processing any of it. Only doing BOOT_LOG_DBG, as we do not
37+
* really want to inform on failures to lock.
38+
*/
39+
for (int i = 0; i < ARRAY_SIZE(kmu_key_ids); ++i) {
40+
psa_key_attributes_t attr;
41+
42+
status = psa_get_key_attributes(kmu_key_ids[i], &attr);
43+
LOG_DBG("KMU key 0x%x(%d) attr query status == %d",
44+
kmu_key_ids[i], i, status);
45+
46+
if (status == PSA_SUCCESS) {
47+
status = cracen_kmu_block(&attr);
48+
LOG_DBG("KMU key lock status == %d", status);
49+
}
50+
51+
status = psa_purge_key(kmu_key_ids[i]);
52+
LOG_DBG("KMU key 0x%x(%d) purge status == %d",
53+
kmu_key_ids[i], i, status);
54+
}
55+
}
56+
3157
int bl_ed25519_validate(const uint8_t *data, uint32_t data_len, const uint8_t *signature)
3258
{
3359
psa_status_t status = PSA_ERROR_BAD_STATE;

subsys/bootloader/bl_validation/bl_validation.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,11 @@ bool bl_validate_firmware_local(uint32_t fw_address, const struct fw_info *fwinf
524524
{
525525
return validate_firmware(fw_address, fw_address, fwinfo, false);
526526
}
527+
528+
void bl_validate_housekeeping(void)
529+
{
530+
bl_root_of_trust_housekeeping();
531+
}
527532
#endif
528533

529534
bool bl_validate_firmware_available(void)

0 commit comments

Comments
 (0)