Skip to content

Commit 5d86c9b

Browse files
ahasztagjukkar
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 d69621e)
1 parent 1084f26 commit 5d86c9b

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
@@ -475,6 +475,7 @@ static int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
475475
}
476476
#endif /* !MCUBOOT_BUILTIN_KEY */
477477

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

498548
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
int seed_len, uint8_t *out_hash
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
@@ -435,7 +435,14 @@ config BOOT_KMU_KEYS_REVOCATION
435435
help
436436
Enabling KMU key revocation backend.
437437

438-
if !BOOT_SIGNATURE_USING_KMU
438+
config NRF_BOOT_SIGNATURE_USING_ITS
439+
bool "Use ITS stored keys for signature verification"
440+
depends on NRF_SECURITY
441+
help
442+
MCUboot will use keys provisioned to the internal trusted storage for signature
443+
verification instead of compiling in key data from a file.
444+
445+
if !BOOT_SIGNATURE_USING_KMU && !NRF_BOOT_SIGNATURE_USING_ITS
439446

440447
config BOOT_SIGNATURE_KEY_FILE
441448
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)