Skip to content

Commit 1d15083

Browse files
de-nordicnvlsianpu
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 6e0c2b8) (cherry picked from commit b647a94)
1 parent d88f72a commit 1d15083

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])
@@ -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[EDDSA_SIGNAGURE_LENGTH],
90+
const uint8_t public_key[EDDSA_KEY_LENGTH])
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern int ED25519_verify(const uint8_t *message, size_t message_len,
3131
const uint8_t signature[EDDSA_SIGNATURE_LENGTH],
3232
const uint8_t public_key[NUM_ED25519_BYTES]);
3333

34+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
3435
#if !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN)
3536
/*
3637
* Parse the public key used for signing.
@@ -73,6 +74,7 @@ bootutil_import_key(uint8_t **cp, uint8_t *end)
7374
return 0;
7475
}
7576
#endif /* !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN) */
77+
#endif
7678

7779
/* Signature verification base function.
7880
* The function takes buffer of specified length and tries to verify
@@ -87,14 +89,17 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
8789
{
8890
int rc;
8991
FIH_DECLARE(fih_rc, FIH_FAILURE);
90-
uint8_t *pubkey;
92+
uint8_t *pubkey = NULL;
93+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
9194
uint8_t *end;
95+
#endif
9296

9397
if (slen != EDDSA_SIGNATURE_LENGTH) {
9498
FIH_SET(fih_rc, FIH_FAILURE);
9599
goto out;
96100
}
97101

102+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
98103
pubkey = (uint8_t *)bootutil_keys[key_id].key;
99104
end = pubkey + *bootutil_keys[key_id].len;
100105

@@ -116,6 +121,8 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
116121
}
117122

118123
pubkey = end - NUM_ED25519_BYTES;
124+
#endif
125+
119126
#endif
120127

121128
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
@@ -245,6 +245,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
245245
# define KEY_BUF_SIZE (SIG_BUF_SIZE + 24)
246246
#endif /* !MCUBOOT_HW_KEY */
247247

248+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
248249
#if !defined(MCUBOOT_HW_KEY)
249250
static int
250251
bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
@@ -310,6 +311,7 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
310311
}
311312
#endif /* !MCUBOOT_HW_KEY */
312313
#endif /* !MCUBOOT_BUILTIN_KEY */
314+
#endif /* !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) */
313315
#endif /* EXPECTED_SIG_TLV */
314316

315317
/**
@@ -627,6 +629,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
627629
break;
628630
}
629631
#endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
632+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
630633
#ifdef EXPECTED_KEY_TLV
631634
case EXPECTED_KEY_TLV:
632635
{
@@ -657,14 +660,17 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
657660
break;
658661
}
659662
#endif /* EXPECTED_KEY_TLV */
663+
#endif /* !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) */
660664
#ifdef EXPECTED_SIG_TLV
661665
case EXPECTED_SIG_TLV:
662666
{
667+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
663668
/* Ignore this signature if it is out of bounds. */
664669
if (key_id < 0 || key_id >= bootutil_key_cnt) {
665670
key_id = -1;
666671
continue;
667672
}
673+
#endif /* !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) */
668674
if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
669675
rc = -1;
670676
goto out;
@@ -810,7 +816,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
810816
}
811817

812818
#ifdef EXPECTED_SIG_TLV
813-
#ifdef EXPECTED_KEY_TLV
819+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) && defined(EXPECTED_KEY_TLV)
814820
rc = bootutil_tlv_iter_begin(&it, hdr, fap, EXPECTED_KEY_TLV, false);
815821
if (rc) {
816822
goto out;
@@ -856,7 +862,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
856862
*/
857863
}
858864
}
859-
#endif /* EXPECTED_KEY_TLV */
865+
#endif /* !CONFIG_BOOT_SIGNATURE_USING_KMU && EXPECTED_KEY_TLV */
860866

861867
rc = bootutil_tlv_iter_begin(&it, hdr, fap, IMAGE_TLV_DECOMP_SIGNATURE, true);
862868
if (rc) {
@@ -879,10 +885,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
879885

880886
if (type == IMAGE_TLV_DECOMP_SIGNATURE) {
881887
/* Ignore this signature if it is out of bounds. */
888+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
882889
if (key_id < 0 || key_id >= bootutil_key_cnt) {
883890
key_id = -1;
884891
continue;
885892
}
893+
#endif
886894

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

boot/zephyr/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ if(CONFIG_MCUBOOT_SERIAL)
315315
endif()
316316
endif()
317317

318-
if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
318+
if(NOT CONFIG_BOOT_SIGNATURE_USING_KMU AND NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
319319
# CONF_FILE points to the KConfig configuration files of the bootloader.
320320
foreach (filepath ${CONF_FILE})
321321
file(READ ${filepath} temp_text)

boot/zephyr/Kconfig

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

332332
endchoice
333333

334+
config BOOT_SIGNATURE_USING_KMU
335+
bool "Use KMU stored keys for signature verification"
336+
depends on NRF_SECURITY
337+
depends on CRACEN_LIB_KMU
338+
select PSA_WANT_ALG_GCM
339+
select PSA_WANT_KEY_TYPE_AES
340+
select PSA_WANT_AES_KEY_SIZE_256
341+
select PSA_WANT_ALG_SP800_108_COUNTER_CMAC
342+
select PSA_WANT_ALG_CMAC
343+
select PSA_WANT_ALG_ECB_NO_PADDING
344+
help
345+
MCUboot will use keys provisioned to the device key management unit for signature
346+
verification instead of compiling in key data from a file.
347+
348+
if !BOOT_SIGNATURE_USING_KMU
349+
334350
config BOOT_SIGNATURE_KEY_FILE
335351
string "PEM key file"
336352
default "root-ec-p256.pem" if BOOT_SIGNATURE_TYPE_ECDSA_P256
@@ -348,6 +364,8 @@ config BOOT_SIGNATURE_KEY_FILE
348364
with the public key information will be written in a format expected by
349365
MCUboot.
350366

367+
endif
368+
351369
config MCUBOOT_CLEANUP_ARM_CORE
352370
bool "Perform core cleanup before chain-load the application"
353371
depends on CPU_CORTEX_M
@@ -370,6 +388,14 @@ config MCUBOOT_CLEANUP_RAM
370388
help
371389
Sets contents of memory to 0 before jumping to application.
372390

391+
# Disable MBEDTLS from being selected if NRF_SECURITY is enabled, and use default NRF_SECURITY
392+
# configuration file for MBEDTLS
393+
config MBEDTLS
394+
depends on !NRF_SECURITY
395+
396+
config NRF_SECURITY
397+
select MBEDTLS_PROMPTLESS
398+
373399
if MBEDTLS || NRF_SECURITY
374400

375401
config MBEDTLS_CFG_FILE

0 commit comments

Comments
 (0)