Skip to content

Commit 5703f81

Browse files
committed
[nrf noup] bootutil: Separate KMU implementation from ED25519
Move KMU specific implementation to dedicated unit. Signed-off-by: Dominik Ermel <[email protected]>
1 parent a8161d7 commit 5703f81

File tree

3 files changed

+158
-142
lines changed

3 files changed

+158
-142
lines changed

boot/bootutil/pkg.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pkg.ign_files.BOOTUTIL_SINGLE_APPLICATION_SLOT:
4545
pkg.ign_files:
4646
- "ram_load.c"
4747
- "ed25519_psa.c" # Currently no PSA for mynewet
48+
- "ed25519_psa_kmu.c"
4849
- "encrypted_psa.c"
4950

5051
pkg.deps.BOOTUTIL_USE_MBED_TLS:

boot/bootutil/src/ed25519_psa.c

Lines changed: 0 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,13 @@
1313
#include <psa/crypto.h>
1414
#include <psa/crypto_types.h>
1515
#include <zephyr/sys/util.h>
16-
#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
17-
#include <cracen_psa_kmu.h>
18-
#endif
1916

2017
BOOT_LOG_MODULE_REGISTER(ed25519_psa);
2118

2219
#define SHA512_DIGEST_LENGTH 64
2320
#define EDDSA_KEY_LENGTH 32
2421
#define EDDSA_SIGNAGURE_LENGTH 64
2522

26-
#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
27-
/* List of KMU stored key ids available for MCUboot */
28-
#define PSA_KEY_INDEX_SIZE 2
29-
30-
#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 || \
31-
defined(CONFIG_NCS_BOOT_SIGNATURE_KMU_UROT_MAPPING)
32-
#define PSA_KEY_STARTING_ID 226
33-
#else
34-
#define PSA_KEY_STARTING_ID 242
35-
#endif
36-
37-
#define MAKE_PSA_KMU_KEY_ID(id) PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW, id)
38-
static psa_key_id_t key_ids[] = {
39-
MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID),
40-
MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID + PSA_KEY_INDEX_SIZE),
41-
MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID + (2 * PSA_KEY_INDEX_SIZE))
42-
};
43-
44-
#define KEY_SLOTS_COUNT CONFIG_BOOT_SIGNATURE_KMU_SLOTS
45-
46-
#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION)
47-
#include <bootutil/key_revocation.h>
48-
static psa_key_id_t *validated_with = NULL;
49-
#endif
50-
51-
BUILD_ASSERT(CONFIG_BOOT_SIGNATURE_KMU_SLOTS <= ARRAY_SIZE(key_ids),
52-
"Invalid number of KMU slots, up to 3 are supported on nRF54L15");
53-
#endif
54-
55-
#if defined(CONFIG_NCS_BOOT_SIGNATURE_USING_ITS)
56-
static const psa_key_id_t key_ids[] = {
57-
0x40022100,
58-
0x40022101,
59-
0x40022102,
60-
0x40022103
61-
};
62-
63-
#define KEY_SLOTS_COUNT ARRAY_SIZE(key_ids)
64-
#endif
65-
66-
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) && !defined(CONFIG_NCS_BOOT_SIGNATURE_USING_ITS)
6723
int ED25519_verify(const uint8_t *message, size_t message_len,
6824
const uint8_t signature[EDDSA_SIGNAGURE_LENGTH],
6925
const uint8_t public_key[EDDSA_KEY_LENGTH])
@@ -116,101 +72,3 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
11672

11773
return ret;
11874
}
119-
#else
120-
int ED25519_verify(const uint8_t *message, size_t message_len,
121-
const uint8_t signature[EDDSA_SIGNAGURE_LENGTH],
122-
const uint8_t public_key[EDDSA_KEY_LENGTH])
123-
{
124-
ARG_UNUSED(public_key);
125-
/* Set to any error */
126-
psa_status_t status = PSA_ERROR_BAD_STATE;
127-
128-
/* Initialize PSA Crypto */
129-
status = psa_crypto_init();
130-
if (status != PSA_SUCCESS) {
131-
BOOT_LOG_ERR("PSA crypto init failed %d", status);
132-
return 0;
133-
}
134-
135-
status = PSA_ERROR_BAD_STATE;
136-
137-
for (int i = 0; i < KEY_SLOTS_COUNT; ++i) {
138-
psa_key_id_t kid = key_ids[i];
139-
140-
status = psa_verify_message(kid, PSA_ALG_PURE_EDDSA, message,
141-
message_len, signature,
142-
EDDSA_SIGNAGURE_LENGTH);
143-
if (status == PSA_SUCCESS) {
144-
#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION)
145-
validated_with = key_ids + i;
146-
#endif
147-
return 1;
148-
}
149-
150-
}
151-
152-
BOOT_LOG_ERR("ED25519 signature verification failed %d", status);
153-
154-
return 0;
155-
}
156-
#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION)
157-
int exec_revoke(void)
158-
{
159-
int ret = BOOT_KEY_REVOKE_OK;
160-
psa_status_t status = psa_crypto_init();
161-
162-
if (!validated_with) {
163-
ret = BOOT_KEY_REVOKE_INVALID;
164-
goto out;
165-
}
166-
167-
if (status != PSA_SUCCESS) {
168-
BOOT_LOG_ERR("PSA crypto init failed with error %d", status);
169-
ret = BOOT_KEY_REVOKE_FAILED;
170-
goto out;
171-
}
172-
for (int i = 0; i < CONFIG_BOOT_SIGNATURE_KMU_SLOTS; i++) {
173-
if ((key_ids + i) == validated_with) {
174-
break;
175-
}
176-
BOOT_LOG_DBG("Invalidating key ID %d", i);
177-
178-
status = psa_destroy_key(key_ids[i]);
179-
if (status == PSA_SUCCESS) {
180-
BOOT_LOG_DBG("Success on key ID %d", i);
181-
} else {
182-
BOOT_LOG_ERR("Key invalidation failed with: %d", status);
183-
}
184-
}
185-
out:
186-
return ret;
187-
}
188-
#endif /* CONFIG_BOOT_KMU_KEYS_REVOCATION */
189-
190-
void nrf_crypto_keys_housekeeping(void)
191-
{
192-
psa_status_t status;
193-
194-
/* We will continue through all keys, even if we have error while
195-
* processing any of it. Only doing BOOT_LOG_DBG, as we do not
196-
* really want to inform on failures to lock.
197-
*/
198-
for (int i = 0; i < CONFIG_BOOT_SIGNATURE_KMU_SLOTS; ++i) {
199-
psa_key_attributes_t attr;
200-
201-
status = psa_get_key_attributes(key_ids[i], &attr);
202-
BOOT_LOG_DBG("KMU key 0x%x(%d) attr query status == %d",
203-
key_ids[i], i, status);
204-
205-
if (status == PSA_SUCCESS) {
206-
status = cracen_kmu_block(&attr);
207-
BOOT_LOG_DBG("KMU key lock status == %d", status);
208-
}
209-
210-
status = psa_purge_key(key_ids[i]);
211-
BOOT_LOG_DBG("KMU key 0x%x(%d) purge status == %d",
212-
key_ids[i], i, status);
213-
}
214-
}
215-
216-
#endif
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <assert.h>
7+
#include <string.h>
8+
#include <stdint.h>
9+
10+
#include <mcuboot_config/mcuboot_config.h>
11+
#include "bootutil/bootutil_log.h"
12+
13+
#include <psa/crypto.h>
14+
#include <psa/crypto_types.h>
15+
#include <zephyr/sys/util.h>
16+
17+
BOOT_LOG_MODULE_DECLARE(ed25519_psa);
18+
19+
#define EDDSA_KEY_LENGTH 32
20+
#define EDDSA_SIGNAGURE_LENGTH 64
21+
22+
#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
23+
/* List of KMU stored key ids available for MCUboot */
24+
#define PSA_KEY_INDEX_SIZE 2
25+
26+
#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 || \
27+
defined(CONFIG_NCS_BOOT_SIGNATURE_KMU_UROT_MAPPING)
28+
#define PSA_KEY_STARTING_ID 226
29+
#else
30+
#define PSA_KEY_STARTING_ID 242
31+
#endif
32+
33+
#define MAKE_PSA_KMU_KEY_ID(id) PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW, id)
34+
static psa_key_id_t key_ids[] = {
35+
MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID),
36+
MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID + PSA_KEY_INDEX_SIZE),
37+
MAKE_PSA_KMU_KEY_ID(PSA_KEY_STARTING_ID + (2 * PSA_KEY_INDEX_SIZE))
38+
};
39+
40+
#define KEY_SLOTS_COUNT CONFIG_BOOT_SIGNATURE_KMU_SLOTS
41+
42+
#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION)
43+
#include <bootutil/key_revocation.h>
44+
static psa_key_id_t *validated_with = NULL;
45+
#endif
46+
47+
BUILD_ASSERT(CONFIG_BOOT_SIGNATURE_KMU_SLOTS <= ARRAY_SIZE(key_ids),
48+
"Invalid number of KMU slots, up to 3 are supported on nRF54L15");
49+
#endif
50+
51+
#if defined(CONFIG_NCS_BOOT_SIGNATURE_USING_ITS)
52+
static const psa_key_id_t key_ids[] = {
53+
0x40022100,
54+
0x40022101,
55+
0x40022102,
56+
0x40022103
57+
};
58+
59+
#define KEY_SLOTS_COUNT ARRAY_SIZE(key_ids)
60+
#endif
61+
62+
int ED25519_verify(const uint8_t *message, size_t message_len,
63+
const uint8_t signature[EDDSA_SIGNAGURE_LENGTH],
64+
const uint8_t public_key[EDDSA_KEY_LENGTH])
65+
{
66+
ARG_UNUSED(public_key);
67+
/* Set to any error */
68+
psa_status_t status = PSA_ERROR_BAD_STATE;
69+
70+
/* Initialize PSA Crypto */
71+
status = psa_crypto_init();
72+
if (status != PSA_SUCCESS) {
73+
BOOT_LOG_ERR("PSA crypto init failed %d", status);
74+
return 0;
75+
}
76+
77+
status = PSA_ERROR_BAD_STATE;
78+
79+
for (int i = 0; i < KEY_SLOTS_COUNT; ++i) {
80+
psa_key_id_t kid = key_ids[i];
81+
82+
status = psa_verify_message(kid, PSA_ALG_PURE_EDDSA, message,
83+
message_len, signature,
84+
EDDSA_SIGNAGURE_LENGTH);
85+
if (status == PSA_SUCCESS) {
86+
#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION)
87+
validated_with = key_ids + i;
88+
#endif
89+
return 1;
90+
}
91+
92+
}
93+
94+
BOOT_LOG_ERR("ED25519 signature verification failed %d", status);
95+
96+
return 0;
97+
}
98+
99+
#if defined(CONFIG_BOOT_KMU_KEYS_REVOCATION)
100+
int exec_revoke(void)
101+
{
102+
int ret = BOOT_KEY_REVOKE_OK;
103+
psa_status_t status = psa_crypto_init();
104+
105+
if (!validated_with) {
106+
ret = BOOT_KEY_REVOKE_INVALID;
107+
goto out;
108+
}
109+
110+
if (status != PSA_SUCCESS) {
111+
BOOT_LOG_ERR("PSA crypto init failed with error %d", status);
112+
ret = BOOT_KEY_REVOKE_FAILED;
113+
goto out;
114+
}
115+
for (int i = 0; i < CONFIG_BOOT_SIGNATURE_KMU_SLOTS; i++) {
116+
if ((key_ids + i) == validated_with) {
117+
break;
118+
}
119+
BOOT_LOG_DBG("Invalidating key ID %d", i);
120+
121+
status = psa_destroy_key(key_ids[i]);
122+
if (status == PSA_SUCCESS) {
123+
BOOT_LOG_DBG("Success on key ID %d", i);
124+
} else {
125+
BOOT_LOG_ERR("Key invalidation failed with: %d", status);
126+
}
127+
}
128+
out:
129+
return ret;
130+
}
131+
#endif /* CONFIG_BOOT_KMU_KEYS_REVOCATION */
132+
133+
void nrf_crypto_keys_housekeeping(void)
134+
{
135+
psa_status_t status;
136+
137+
/* We will continue through all keys, even if we have error while
138+
* processing any of it. Only doing BOOT_LOG_DBG, as we do not
139+
* really want to inform on failures to lock.
140+
*/
141+
for (int i = 0; i < CONFIG_BOOT_SIGNATURE_KMU_SLOTS; ++i) {
142+
psa_key_attributes_t attr;
143+
144+
status = psa_get_key_attributes(key_ids[i], &attr);
145+
BOOT_LOG_DBG("KMU key 0x%x(%d) attr query status == %d",
146+
key_ids[i], i, status);
147+
148+
if (status == PSA_SUCCESS) {
149+
status = cracen_kmu_block(&attr);
150+
BOOT_LOG_DBG("KMU key lock status == %d", status);
151+
}
152+
153+
status = psa_purge_key(key_ids[i]);
154+
BOOT_LOG_DBG("KMU key 0x%x(%d) purge status == %d",
155+
key_ids[i], i, status);
156+
}
157+
}

0 commit comments

Comments
 (0)