Skip to content

Commit 4107577

Browse files
committed
[nrf noup] bootutil: Add support for KMU stored ED25519 signature key
The commit adds verification of image using keys stored in KMU. Signed-off-by: Dominik Ermel <[email protected]>
1 parent de524e9 commit 4107577

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

boot/bootutil/src/ed25519_psa.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,28 @@
1212

1313
#include <psa/crypto.h>
1414
#include <psa/crypto_types.h>
15+
#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
16+
#include <cracen_psa_kmu.h>
17+
#endif
1518

1619
BOOT_LOG_MODULE_DECLARE(ed25519_psa);
1720

1821
#define SHA512_DIGEST_LENGTH 64
1922
#define EDDSA_KEY_LENGTH 32
2023
#define EDDSA_SIGNAGURE_LENGTH 64
2124

25+
#if defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
26+
/* List of KMU stored key ids available for MCUboot */
27+
#define MAKE_PSA_KMU_KEY_ID(id) PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW, id)
28+
static psa_key_id_t kmu_key_ids[3] = {
29+
MAKE_PSA_KMU_KEY_ID(226),
30+
MAKE_PSA_KMU_KEY_ID(228),
31+
MAKE_PSA_KMU_KEY_ID(230)
32+
};
33+
#define KMU_KEY_COUNT (sizeof(kmu_key_ids)/sizeof(kmu_key_ids[0]))
34+
#endif
35+
36+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
2237
int ED25519_verify(const uint8_t *message, size_t message_len,
2338
const uint8_t signature[EDDSA_SIGNAGURE_LENGTH],
2439
const uint8_t public_key[EDDSA_KEY_LENGTH])
@@ -69,3 +84,39 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
6984

7085
return ret;
7186
}
87+
#else
88+
int ED25519_verify(const uint8_t *message, size_t message_len,
89+
const uint8_t signature[64],
90+
const uint8_t public_key[32])
91+
{
92+
ARG_UNUSED(public_key);
93+
/* Set to any error */
94+
psa_status_t status = PSA_ERROR_BAD_STATE;
95+
int ret = 0; /* Fail by default */
96+
97+
/* Initialize PSA Crypto */
98+
status = psa_crypto_init();
99+
if (status != PSA_SUCCESS) {
100+
BOOT_LOG_ERR("PSA crypto init failed %d", status);
101+
return 0;
102+
}
103+
104+
status = PSA_ERROR_BAD_STATE;
105+
106+
for (int i = 0; i < KMU_KEY_COUNT; ++i) {
107+
psa_key_id_t kid = kmu_key_ids[i];
108+
109+
status = psa_verify_message(kid, PSA_ALG_PURE_EDDSA, message,
110+
message_len, signature,
111+
EDDSA_SIGNAGURE_LENGTH);
112+
if (status == PSA_SUCCESS) {
113+
ret = 1;
114+
break;
115+
}
116+
117+
BOOT_LOG_ERR("ED25519 signature verification failed %d", status);
118+
}
119+
120+
return ret;
121+
}
122+
#endif

boot/bootutil/src/image_ed25519.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
#define SHA256_LEN 32
2828
#define EDDSA_SIGNAGURE_LENGTH 64
2929

30-
static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
31-
#define NUM_ED25519_BYTES 32
32-
3330
extern int ED25519_verify(const uint8_t *message, size_t message_len,
3431
const uint8_t signature[EDDSA_SIGNAGURE_LENGTH],
3532
const uint8_t public_key[NUM_ED25519_BYTES]);
3633

34+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
35+
36+
static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
37+
#define NUM_ED25519_BYTES 32
38+
3739
/*
3840
* Parse the public key used for signing.
3941
*/
@@ -72,22 +74,26 @@ bootutil_import_key(uint8_t **cp, uint8_t *end)
7274

7375
return 0;
7476
}
77+
#endif
7578

7679
fih_ret
7780
bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
7881
uint8_t key_id)
7982
{
8083
int rc;
8184
FIH_DECLARE(fih_rc, FIH_FAILURE);
82-
uint8_t *pubkey;
85+
uint8_t *pubkey = NULL;
86+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
8387
uint8_t *end;
88+
#endif
8489

8590
if (!(hlen == SHA512_LEN || hlen == SHA256_LEN) ||
8691
slen != EDDSA_SIGNAGURE_LENGTH) {
8792
FIH_SET(fih_rc, FIH_FAILURE);
8893
goto out;
8994
}
9095

96+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
9197
pubkey = (uint8_t *)bootutil_keys[key_id].key;
9298
end = pubkey + *bootutil_keys[key_id].len;
9399

@@ -96,6 +102,7 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
96102
FIH_SET(fih_rc, FIH_FAILURE);
97103
goto out;
98104
}
105+
#endif
99106

100107
rc = ED25519_verify(hash, hlen, sig, pubkey);
101108

@@ -117,14 +124,17 @@ bootutil_verify_img(const uint8_t *img, uint32_t size,
117124
{
118125
int rc;
119126
FIH_DECLARE(fih_rc, FIH_FAILURE);
120-
uint8_t *pubkey;
127+
uint8_t *pubkey = NULL;
128+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
121129
uint8_t *end;
130+
#endif
122131

123132
if (slen != EDDSA_SIGNAGURE_LENGTH) {
124133
FIH_SET(fih_rc, FIH_FAILURE);
125134
goto out;
126135
}
127136

137+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
128138
pubkey = (uint8_t *)bootutil_keys[key_id].key;
129139
end = pubkey + *bootutil_keys[key_id].len;
130140

@@ -133,6 +143,7 @@ bootutil_verify_img(const uint8_t *img, uint32_t size,
133143
FIH_SET(fih_rc, FIH_FAILURE);
134144
goto out;
135145
}
146+
#endif
136147

137148
rc = ED25519_verify(img, size, sig, pubkey);
138149

boot/zephyr/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ if(CONFIG_MCUBOOT_SERIAL)
297297
endif()
298298
endif()
299299

300-
if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
300+
if(NOT CONFIG_BOOT_SIGNATURE_USING_KMU OR NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
301301
# CONF_FILE points to the KConfig configuration files of the bootloader.
302302
foreach (filepath ${CONF_FILE})
303303
file(READ ${filepath} temp_text)

boot/zephyr/Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,21 @@ endif
302302

303303
endchoice
304304

305+
config BOOT_SIGNATURE_USING_KMU
306+
bool "Use KMU stored keys for signature verification"
307+
default n
308+
help
309+
The MCUboot will use keys provisioned to board for signature verification
310+
instead of compiling in a key data.
311+
select PSA_WANT_ALG_GCM
312+
select PSA_WANT_KEY_TYPE_AES
313+
select PSA_WANT_AES_KEY_SIZE_256
314+
select PSA_WANT_ALG_SP800_108_COUNTER_CMAC
315+
select PSA_WANT_ALG_CMAC
316+
select PSA_WANT_ALG_ECB_NO_PADDING
317+
318+
if !BOOT_SIGNATURE_USING_KMU
319+
305320
config BOOT_SIGNATURE_KEY_FILE
306321
string "PEM key file"
307322
default "root-ec-p256.pem" if BOOT_SIGNATURE_TYPE_ECDSA_P256
@@ -319,6 +334,8 @@ config BOOT_SIGNATURE_KEY_FILE
319334
with the public key information will be written in a format expected by
320335
MCUboot.
321336

337+
endif
338+
322339
config MCUBOOT_CLEANUP_ARM_CORE
323340
bool "Perform core cleanup before chain-load the application"
324341
depends on CPU_CORTEX_M

0 commit comments

Comments
 (0)