Skip to content

Commit f39ffc3

Browse files
committed
[nrf noup] Added BOOT_SIGNATURE_USING_ITS for ecdsa configuration
This configuration has the purpose of using keys provisioned to the internal trusted storage (ITS). It makes use of the already existing parts of code for MCUBOOT_BUILTIN_KEY Signed-off-by: Artur Hadasz <[email protected]>
1 parent d24b28f commit f39ffc3

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

boot/bootutil/include/bootutil/crypto/ecdsa.h

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,20 @@ static const uint8_t Secp384r1[] = {0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22};
397397

398398
static inline void bootutil_ecdsa_init(bootutil_ecdsa_context *ctx)
399399
{
400-
#if !defined(MCUBOOT_BUILTIN_KEY)
400+
#if !defined(MCUBOOT_BUILTIN_KEY) && !defined(CONFIG_BOOT_SIGNATURE_USING_ITS)
401401
ctx->key_id = PSA_KEY_ID_NULL;
402402
ctx->curve_byte_count = 0;
403403
ctx->required_algorithm = 0;
404404

405-
#else /* !MCUBOOT_BUILTIN_KEY */
405+
#else /* !MCUBOOT_BUILTIN_KEY && !CONFIG_BOOT_SIGNATURE_USING_ITS */
406406
/* The incoming key ID is equal to the image index. The key ID value must be
407407
* shifted (by one in this case) because zero is reserved (PSA_KEY_ID_NULL)
408408
* and considered invalid.
409409
*/
410+
#if defined(MCUBOOT_BUILTIN_KEY)
410411
ctx->key_id++; /* Make sure it is not equal to 0. */
412+
#endif
413+
411414
#if defined(MCUBOOT_SIGN_EC256)
412415
ctx->curve_byte_count = 32;
413416
ctx->required_algorithm = PSA_ALG_SHA_256;
@@ -426,7 +429,7 @@ static inline void bootutil_ecdsa_drop(bootutil_ecdsa_context *ctx)
426429
}
427430
}
428431

429-
#if !defined(MCUBOOT_BUILTIN_KEY)
432+
#if !defined(MCUBOOT_BUILTIN_KEY) && !defined(CONFIG_BOOT_SIGNATURE_USING_ITS)
430433
/*
431434
* Parse a ECDSA public key with format specified in RFC5280 et al.
432435
*
@@ -471,8 +474,9 @@ static int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
471474

472475
return (int)psa_import_key(&key_attributes, *cp, key_size, &ctx->key_id);
473476
}
474-
#endif /* !MCUBOOT_BUILTIN_KEY */
477+
#endif /* !MCUBOOT_BUILTIN_KEY && !CONFIG_BOOT_SIGNATURE_USING_ITS */
475478

479+
#if !defined(CONFIG_BOOT_SIGNATURE_USING_ITS)
476480
/* Verify the signature against the provided hash. The signature gets parsed from
477481
* the encoding first, then PSA Crypto has a dedicated API for ECDSA verification
478482
*/
@@ -491,6 +495,60 @@ static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx,
491495
return (int) psa_verify_hash(ctx->key_id, PSA_ALG_ECDSA(ctx->required_algorithm),
492496
hash, hlen, reformatted_signature, 2*ctx->curve_byte_count);
493497
}
498+
#else /* !CONFIG_BOOT_SIGNATURE_USING_ITS */
499+
500+
struct boot_ecdsa_key_info {
501+
psa_key_id_t key_id;
502+
psa_algorithm_t algorithm;
503+
};
504+
505+
static psa_key_id_t builtin_key_ids[] = {
506+
0x40022100,
507+
0x40022101,
508+
0x40022102,
509+
0x40022103
510+
};
511+
512+
#define BOOT_SIGNATURE_BUILTIN_KEY_SLOTS ARRAY_SIZE(builtin_key_ids)
513+
514+
static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx,
515+
uint8_t *pk, size_t pk_len,
516+
uint8_t *hash, size_t hlen,
517+
uint8_t *sig, size_t slen)
518+
{
519+
(void)pk;
520+
(void)pk_len;
521+
(void)slen;
522+
psa_status_t status = PSA_ERROR_BAD_STATE;
523+
524+
/* Initialize PSA Crypto */
525+
status = psa_crypto_init();
526+
if (status != PSA_SUCCESS) {
527+
BOOT_LOG_ERR("PSA crypto init failed %d", status);
528+
return 1;
529+
}
530+
531+
uint8_t reformatted_signature[96] = {0}; /* Enough for P-384 signature sizes */
532+
parse_signature_from_rfc5480_encoding(sig, ctx->curve_byte_count, reformatted_signature);
533+
534+
status = PSA_ERROR_BAD_STATE;
535+
536+
for (int i = 0; i < BOOT_SIGNATURE_BUILTIN_KEY_SLOTS; ++i) {
537+
psa_key_id_t kid = builtin_key_ids[i];
538+
539+
status = psa_verify_hash(kid, PSA_ALG_ECDSA(ctx->required_algorithm),
540+
hash, hlen, reformatted_signature, 2*ctx->curve_byte_count);
541+
if (status == PSA_SUCCESS) {
542+
break;
543+
}
544+
BOOT_LOG_ERR("ECDSA signature verification failed %d", status);
545+
}
546+
547+
return (int) status;
548+
}
549+
550+
#endif /* !CONFIG_BOOT_SIGNATURE_USING_ITS */
551+
494552
#elif defined(MCUBOOT_USE_MBED_TLS)
495553

496554
typedef mbedtls_ecdsa_context bootutil_ecdsa_context;

boot/bootutil/src/image_validate.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ bootutil_img_validate(struct boot_loader_state *state,
514514
#endif
515515
)
516516
{
517-
#if (defined(EXPECTED_KEY_TLV) && defined(MCUBOOT_HW_KEY)) || defined(MCUBOOT_HW_ROLLBACK_PROT) || defined(MCUBOOT_DECOMPRESS_IMAGES)
517+
#if (defined(EXPECTED_KEY_TLV) && defined(MCUBOOT_HW_KEY)) || defined(MCUBOOT_HW_ROLLBACK_PROT) || defined(MCUBOOT_DECOMPRESS_IMAGES) \
518+
|| defined(MCUBOOT_BUILTIN_KEY)
518519
int image_index = (state == NULL ? 0 : BOOT_CURR_IMG(state));
519520
#endif
520521
uint32_t off;

boot/zephyr/Kconfig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,14 @@ config BOOT_KMU_KEYS_REVOCATION
422422
help
423423
Enabling KMU key revocation backend.
424424

425-
if !BOOT_SIGNATURE_USING_KMU
425+
config BOOT_SIGNATURE_USING_ITS
426+
bool "Use KMU stored keys for signature verification"
427+
depends on NRF_SECURITY
428+
help
429+
MCUboot will use keys provisioned to the internal trusted storage for signature
430+
verification instead of compiling in key data from a file.
431+
432+
if !BOOT_SIGNATURE_USING_KMU && !BOOT_SIGNATURE_USING_ITS
426433

427434
config BOOT_SIGNATURE_KEY_FILE
428435
string "PEM key file"

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
#define MCUBOOT_HW_KEY
6969
#endif
7070

71+
#ifdef CONFIG_BOOT_SIGNATURE_USING_ITS
72+
#define MCUBOOT_BUILTIN_KEY
73+
#endif
74+
7175
#ifdef CONFIG_BOOT_VALIDATE_SLOT0
7276
#define MCUBOOT_VALIDATE_PRIMARY_SLOT
7377
#endif

0 commit comments

Comments
 (0)