diff --git a/boot/bootutil/src/image_validate.c b/boot/bootutil/src/image_validate.c index 61cbf4de04..121411dac4 100644 --- a/boot/bootutil/src/image_validate.c +++ b/boot/bootutil/src/image_validate.c @@ -271,6 +271,8 @@ bootutil_img_hash(struct boot_loader_state *state, # define KEY_BUF_SIZE (SIG_BUF_SIZE + 24) #endif /* !MCUBOOT_HW_KEY */ +#if !defined(MCUBOOT_BYPASS_KEY_MATCH) +/* Find functions are only needed when key is checked first */ #if !defined(MCUBOOT_HW_KEY) static int bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len) @@ -337,6 +339,7 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len) #endif /* !MCUBOOT_HW_KEY */ #endif /* !MCUBOOT_BUILTIN_KEY */ #endif /* EXPECTED_SIG_TLV */ +#endif /* !MCUBOOT_BYPASS_KEY_MATCH */ /** * Reads the value of an image's security counter. @@ -631,7 +634,12 @@ bootutil_img_validate(struct boot_loader_state *state, if (rc) { goto out; } +#if !defined(MCUBOOT_BYPASS_KEY_MATCH) key_id = bootutil_find_key(buf, len); +#else + /* There is only one key */ + key_id = 0; +#endif #else rc = LOAD_IMAGE_DATA(hdr, fap, off, key_buf, len); if (rc) { diff --git a/boot/zephyr/Kconfig b/boot/zephyr/Kconfig index acc0314a66..f59bc45ed9 100644 --- a/boot/zephyr/Kconfig +++ b/boot/zephyr/Kconfig @@ -325,6 +325,19 @@ endif endchoice +config BOOT_BYPASS_KEY_MATCH + bool "Do not match TLV key hash against built in key" + depends on !BOOT_SIGNATURE_TYPE_NONE + depends on !BOOT_HW_KEY + help + MCUboot reads, from TLV, hash of key thath should be used to verify + signature and tries to match it against list of keys, to select the + key from known keys. This pointless when there is only single key + compiled in, as the key can be used whether it is the right one + or not, the signature verification process will verify the key. + Enabling this option turns off key matching, slightly reducing + MCUboot code and boot time. + config BOOT_SIGNATURE_KEY_FILE string "PEM key file" default "root-ec-p256.pem" if BOOT_SIGNATURE_TYPE_ECDSA_P256 diff --git a/boot/zephyr/include/mcuboot_config/mcuboot_config.h b/boot/zephyr/include/mcuboot_config/mcuboot_config.h index fd003565a1..de10a8a245 100644 --- a/boot/zephyr/include/mcuboot_config/mcuboot_config.h +++ b/boot/zephyr/include/mcuboot_config/mcuboot_config.h @@ -153,6 +153,15 @@ #define MCUBOOT_ENCRYPT_X25519 #endif +/* Turn off check of public key hash against compiled in key + * before attempting signature verification. When there is only + * one key, matching is pointless, the signature may just be + * verified with the only key that there is. + */ +#ifdef CONFIG_BOOT_BYPASS_KEY_MATCH +#define MCUBOOT_BYPASS_KEY_MATCH +#endif + #ifdef CONFIG_BOOT_DECOMPRESSION #define MCUBOOT_DECOMPRESS_IMAGES #endif