Skip to content

Commit 6b51018

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 6b51018

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ static int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
473473
}
474474
#endif /* !MCUBOOT_BUILTIN_KEY */
475475

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

496551
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)