Skip to content

Commit 26192ca

Browse files
de-nordicrlubos
authored andcommitted
[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]> (cherry picked from commit e28f5e9)
1 parent a9e70e4 commit 26192ca

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
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_REGISTER(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])
@@ -71,3 +86,39 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
7186

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

boot/bootutil/src/image_ed25519.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern int ED25519_verify(const uint8_t *message, size_t message_len,
3434
const uint8_t signature[EDDSA_SIGNATURE_LENGTH],
3535
const uint8_t public_key[NUM_ED25519_BYTES]);
3636

37+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
3738
#if !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN)
3839
/*
3940
* Parse the public key used for signing.
@@ -76,6 +77,7 @@ bootutil_import_key(uint8_t **cp, uint8_t *end)
7677
return 0;
7778
}
7879
#endif /* !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN) */
80+
#endif
7981

8082
/* Signature verification base function.
8183
* The function takes buffer of specified length and tries to verify
@@ -90,8 +92,10 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
9092
{
9193
int rc;
9294
FIH_DECLARE(fih_rc, FIH_FAILURE);
93-
uint8_t *pubkey;
95+
uint8_t *pubkey = NULL;
96+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
9497
uint8_t *end;
98+
#endif
9599

96100
BOOT_LOG_DBG("bootutil_verify: ED25519 key_id %d", (int)key_id);
97101

@@ -102,6 +106,7 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
102106
goto out;
103107
}
104108

109+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
105110
pubkey = (uint8_t *)bootutil_keys[key_id].key;
106111
end = pubkey + *bootutil_keys[key_id].len;
107112

@@ -125,6 +130,8 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
125130
}
126131

127132
pubkey = end - NUM_ED25519_BYTES;
133+
#endif
134+
128135
#endif
129136

130137
rc = ED25519_verify(buf, blen, sig, pubkey);

boot/bootutil/src/image_validate.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ bootutil_img_hash(struct boot_loader_state *state,
292292
# define KEY_BUF_SIZE (SIG_BUF_SIZE + 24)
293293
#endif /* !MCUBOOT_HW_KEY */
294294

295+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
295296
#if !defined(MCUBOOT_HW_KEY)
296297
static int
297298
bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
@@ -360,6 +361,7 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
360361
}
361362
#endif /* !MCUBOOT_HW_KEY */
362363
#endif /* !MCUBOOT_BUILTIN_KEY */
364+
#endif /* !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) */
363365
#endif /* EXPECTED_SIG_TLV */
364366

365367
/**
@@ -729,6 +731,7 @@ bootutil_img_validate(struct boot_loader_state *state,
729731
break;
730732
}
731733
#endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
734+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
732735
#ifdef EXPECTED_KEY_TLV
733736
case EXPECTED_KEY_TLV:
734737
{
@@ -760,15 +763,18 @@ bootutil_img_validate(struct boot_loader_state *state,
760763
break;
761764
}
762765
#endif /* EXPECTED_KEY_TLV */
766+
#endif /* !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) */
763767
#ifdef EXPECTED_SIG_TLV
764768
case EXPECTED_SIG_TLV:
765769
{
766770
BOOT_LOG_DBG("bootutil_img_validate: EXPECTED_SIG_TLV == %d", EXPECTED_SIG_TLV);
771+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
767772
/* Ignore this signature if it is out of bounds. */
768773
if (key_id < 0 || key_id >= bootutil_key_cnt) {
769774
key_id = -1;
770775
continue;
771776
}
777+
#endif /* !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) */
772778
if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
773779
rc = -1;
774780
goto out;
@@ -925,7 +931,7 @@ bootutil_img_validate(struct boot_loader_state *state,
925931
}
926932

927933
#ifdef EXPECTED_SIG_TLV
928-
#ifdef EXPECTED_KEY_TLV
934+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) && defined(EXPECTED_KEY_TLV)
929935
rc = bootutil_tlv_iter_begin(&it, hdr, fap, EXPECTED_KEY_TLV, false);
930936
if (rc) {
931937
goto out;
@@ -971,7 +977,7 @@ bootutil_img_validate(struct boot_loader_state *state,
971977
*/
972978
}
973979
}
974-
#endif /* EXPECTED_KEY_TLV */
980+
#endif /* !CONFIG_BOOT_SIGNATURE_USING_KMU && EXPECTED_KEY_TLV */
975981

976982
rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_DECOMP_SIGNATURE, true);
977983
if (rc) {
@@ -994,10 +1000,12 @@ bootutil_img_validate(struct boot_loader_state *state,
9941000

9951001
if (type == IMAGE_TLV_DECOMP_SIGNATURE) {
9961002
/* Ignore this signature if it is out of bounds. */
1003+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
9971004
if (key_id < 0 || key_id >= bootutil_key_cnt) {
9981005
key_id = -1;
9991006
continue;
10001007
}
1008+
#endif
10011009

10021010
if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
10031011
rc = -1;

boot/zephyr/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ if(CONFIG_MCUBOOT_SERIAL)
355355
endif()
356356
endif()
357357

358-
if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
358+
if(NOT CONFIG_BOOT_SIGNATURE_USING_KMU AND NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
359359
# CONF_FILE points to the KConfig configuration files of the bootloader.
360360
foreach (filepath ${CONF_FILE})
361361
file(READ ${filepath} temp_text)

boot/zephyr/Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,22 @@ endif
384384

385385
endchoice
386386

387+
config BOOT_SIGNATURE_USING_KMU
388+
bool "Use KMU stored keys for signature verification"
389+
depends on NRF_SECURITY
390+
depends on CRACEN_LIB_KMU
391+
select PSA_WANT_ALG_GCM
392+
select PSA_WANT_KEY_TYPE_AES
393+
select PSA_WANT_AES_KEY_SIZE_256
394+
select PSA_WANT_ALG_SP800_108_COUNTER_CMAC
395+
select PSA_WANT_ALG_CMAC
396+
select PSA_WANT_ALG_ECB_NO_PADDING
397+
help
398+
MCUboot will use keys provisioned to the device key management unit for signature
399+
verification instead of compiling in key data from a file.
400+
401+
if !BOOT_SIGNATURE_USING_KMU
402+
387403
config BOOT_SIGNATURE_KEY_FILE
388404
string "PEM key file"
389405
default "root-ec-p256.pem" if BOOT_SIGNATURE_TYPE_ECDSA_P256
@@ -401,6 +417,8 @@ config BOOT_SIGNATURE_KEY_FILE
401417
with the public key information will be written in a format expected by
402418
MCUboot.
403419

420+
endif
421+
404422
config MCUBOOT_CLEANUP_ARM_CORE
405423
bool "Perform core cleanup before chain-load the application"
406424
depends on CPU_CORTEX_M
@@ -430,6 +448,14 @@ config MCUBOOT_INFINITE_LOOP_AFTER_RAM_CLEANUP
430448
Verification option that keeps execution in infinite loop after
431449
RAM cleanup has been performed.
432450

451+
# Disable MBEDTLS from being selected if NRF_SECURITY is enabled, and use default NRF_SECURITY
452+
# configuration file for MBEDTLS
453+
config MBEDTLS
454+
depends on !NRF_SECURITY
455+
456+
config NRF_SECURITY
457+
select MBEDTLS_PROMPTLESS
458+
433459
config MBEDTLS_CFG_FILE
434460
# It might be awkward to define an Mbed TLS header file when TinyCrypt
435461
# is used, but the fact is that Mbed TLS' ASN1 parse module is used

0 commit comments

Comments
 (0)