Skip to content

Commit d69621e

Browse files
ahasztagrlubos
authored andcommitted
[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]> (cherry picked from commit 6837984)
1 parent 2f7059e commit d69621e

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

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

Lines changed: 50 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_NRF_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,55 @@ 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_NRF_BOOT_SIGNATURE_USING_ITS */
496+
497+
static const psa_key_id_t builtin_key_ids[] = {
498+
0x40022100,
499+
0x40022101,
500+
0x40022102,
501+
0x40022103
502+
};
503+
504+
#define BOOT_SIGNATURE_BUILTIN_KEY_SLOTS ARRAY_SIZE(builtin_key_ids)
505+
506+
static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx,
507+
uint8_t *pk, size_t pk_len,
508+
uint8_t *hash, size_t hlen,
509+
uint8_t *sig, size_t slen)
510+
{
511+
(void)pk;
512+
(void)pk_len;
513+
(void)slen;
514+
psa_status_t status = PSA_ERROR_BAD_STATE;
515+
516+
/* Initialize PSA Crypto */
517+
status = psa_crypto_init();
518+
if (status != PSA_SUCCESS) {
519+
BOOT_LOG_ERR("PSA crypto init failed %d", status);
520+
return 1;
521+
}
522+
523+
uint8_t reformatted_signature[96] = {0}; /* Enough for P-384 signature sizes */
524+
parse_signature_from_rfc5480_encoding(sig, ctx->curve_byte_count, reformatted_signature);
525+
526+
status = PSA_ERROR_BAD_STATE;
527+
528+
for (int i = 0; i < BOOT_SIGNATURE_BUILTIN_KEY_SLOTS; ++i) {
529+
psa_key_id_t kid = builtin_key_ids[i];
530+
531+
status = psa_verify_hash(kid, PSA_ALG_ECDSA(ctx->required_algorithm),
532+
hash, hlen, reformatted_signature, 2*ctx->curve_byte_count);
533+
if (status == PSA_SUCCESS) {
534+
break;
535+
}
536+
BOOT_LOG_ERR("ECDSA signature verification failed %d", status);
537+
}
538+
539+
return status == PSA_SUCCESS ? 0 : 2;
540+
}
541+
542+
#endif /* !CONFIG_NRF_BOOT_SIGNATURE_USING_ITS */
543+
494544
#elif defined(MCUBOOT_USE_MBED_TLS)
495545

496546
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 NRF_BOOT_SIGNATURE_USING_ITS
426+
bool "Use ITS 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 && !NRF_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_NRF_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)