Skip to content

Commit 6e0c2b8

Browse files
de-nordicjukkar
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 1dbca8f) (cherry picked from commit 40543f1) (cherry picked from commit 896a16e)
1 parent d09c5a0 commit 6e0c2b8

File tree

5 files changed

+101
-6
lines changed

5 files changed

+101
-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_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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
#include "bootutil/crypto/sha.h"
2626

2727
#define EDDSA_SIGNATURE_LENGTH 64
28-
29-
static const uint8_t ed25519_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG "\x65\x70";
3028
#define NUM_ED25519_BYTES 32
3129

3230
extern int ED25519_verify(const uint8_t *message, size_t message_len,
3331
const uint8_t signature[EDDSA_SIGNATURE_LENGTH],
3432
const uint8_t public_key[NUM_ED25519_BYTES]);
3533

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+
3638
/*
3739
* Parse the public key used for signing.
3840
*/
@@ -71,21 +73,25 @@ bootutil_import_key(uint8_t **cp, uint8_t *end)
7173

7274
return 0;
7375
}
76+
#endif
7477

7578
fih_ret
7679
bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
7780
uint8_t key_id)
7881
{
7982
int rc;
8083
FIH_DECLARE(fih_rc, FIH_FAILURE);
81-
uint8_t *pubkey;
84+
uint8_t *pubkey = NULL;
85+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
8286
uint8_t *end;
87+
#endif
8388

8489
if (hlen != IMAGE_HASH_SIZE || slen != EDDSA_SIGNATURE_LENGTH) {
8590
FIH_SET(fih_rc, FIH_FAILURE);
8691
goto out;
8792
}
8893

94+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
8995
pubkey = (uint8_t *)bootutil_keys[key_id].key;
9096
end = pubkey + *bootutil_keys[key_id].len;
9197

@@ -94,6 +100,7 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
94100
FIH_SET(fih_rc, FIH_FAILURE);
95101
goto out;
96102
}
103+
#endif
97104

98105
rc = ED25519_verify(hash, IMAGE_HASH_SIZE, sig, pubkey);
99106

@@ -115,14 +122,17 @@ bootutil_verify_img(const uint8_t *img, uint32_t size,
115122
{
116123
int rc;
117124
FIH_DECLARE(fih_rc, FIH_FAILURE);
118-
uint8_t *pubkey;
125+
uint8_t *pubkey = NULL;
126+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
119127
uint8_t *end;
128+
#endif
120129

121130
if (slen != EDDSA_SIGNATURE_LENGTH) {
122131
FIH_SET(fih_rc, FIH_FAILURE);
123132
goto out;
124133
}
125134

135+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
126136
pubkey = (uint8_t *)bootutil_keys[key_id].key;
127137
end = pubkey + *bootutil_keys[key_id].len;
128138

@@ -131,6 +141,7 @@ bootutil_verify_img(const uint8_t *img, uint32_t size,
131141
FIH_SET(fih_rc, FIH_FAILURE);
132142
goto out;
133143
}
144+
#endif
134145

135146
rc = ED25519_verify(img, size, sig, pubkey);
136147

boot/bootutil/src/image_validate.c

Lines changed: 6 additions & 0 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
/**
@@ -626,6 +628,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
626628
break;
627629
}
628630
#endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
631+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
629632
#ifdef EXPECTED_KEY_TLV
630633
case EXPECTED_KEY_TLV:
631634
{
@@ -656,14 +659,17 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
656659
break;
657660
}
658661
#endif /* EXPECTED_KEY_TLV */
662+
#endif /* !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) */
659663
#ifdef EXPECTED_SIG_TLV
660664
case EXPECTED_SIG_TLV:
661665
{
666+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_KMU)
662667
/* Ignore this signature if it is out of bounds. */
663668
if (key_id < 0 || key_id >= bootutil_key_cnt) {
664669
key_id = -1;
665670
continue;
666671
}
672+
#endif /* !defined(CONFIG_BOOT_SIGNATURE_USING_KMU) */
667673
if (!EXPECTED_SIG_LEN(len) || len > sizeof(buf)) {
668674
rc = -1;
669675
goto out;

boot/zephyr/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ if(CONFIG_MCUBOOT_SERIAL)
313313
endif()
314314
endif()
315315

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

boot/zephyr/Kconfig

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ config BOOT_ED25519_MBEDTLS
293293

294294
config BOOT_ED25519_PSA
295295
bool "Use PSA crypto"
296+
depends on NRF_SECURITY
296297
select BOOT_USE_PSA_CRYPTO
297298
select BOOT_ED25519_PSA_DEPENDENCIES
298299
select BOOT_X25519_PSA_DEPENDENCIES if BOOT_ENCRYPT_IMAGE
@@ -302,6 +303,22 @@ endif
302303

303304
endchoice
304305

306+
config BOOT_SIGNATURE_USING_KMU
307+
bool "Use KMU stored keys for signature verification"
308+
depends on NRF_SECURITY
309+
depends on CRACEN_LIB_KMU
310+
select PSA_WANT_ALG_GCM
311+
select PSA_WANT_KEY_TYPE_AES
312+
select PSA_WANT_AES_KEY_SIZE_256
313+
select PSA_WANT_ALG_SP800_108_COUNTER_CMAC
314+
select PSA_WANT_ALG_CMAC
315+
select PSA_WANT_ALG_ECB_NO_PADDING
316+
help
317+
MCUboot will use keys provisioned to the device key management unit for signature
318+
verification instead of compiling in key data from a file.
319+
320+
if !BOOT_SIGNATURE_USING_KMU
321+
305322
config BOOT_SIGNATURE_KEY_FILE
306323
string "PEM key file"
307324
default "root-ec-p256.pem" if BOOT_SIGNATURE_TYPE_ECDSA_P256
@@ -319,6 +336,8 @@ config BOOT_SIGNATURE_KEY_FILE
319336
with the public key information will be written in a format expected by
320337
MCUboot.
321338

339+
endif
340+
322341
config MCUBOOT_CLEANUP_ARM_CORE
323342
bool "Perform core cleanup before chain-load the application"
324343
depends on CPU_CORTEX_M
@@ -341,10 +360,18 @@ config MCUBOOT_CLEANUP_RAM
341360
help
342361
Sets contents of memory to 0 before jumping to application.
343362

363+
# Disable MBEDTLS from being selected if NRF_SECURITY is enabled, and use default NRF_SECURITY
364+
# configuration file for MBEDTLS
365+
config MBEDTLS
366+
depends on !NRF_SECURITY
367+
368+
config NRF_SECURITY
369+
select MBEDTLS_PROMPTLESS
370+
344371
if MBEDTLS
345372

346373
config MBEDTLS_CFG_FILE
347-
default "mcuboot-mbedtls-cfg.h"
374+
default "mcuboot-mbedtls-cfg.h" if !NRF_SECURITY
348375

349376
endif
350377

0 commit comments

Comments
 (0)