Skip to content

Commit 90ff9de

Browse files
committed
bootutil: imgtool: Fix CI failures
For some platorms image_validate.c: In function 'bootutil_img_validate': image_validate.c:358:40: error: 'image_index' undeclared (first use in this function); did you mean 'image_header'? 358 | key_id = bootutil_find_key(image_index, buf, len); | ^~~~~~~~~~~ Resolve imgtool CI errors affecting certain signature verification tests. Also, change the return type of boot_verify_key_id_for_image to reflect its use as an FIH call. Also add new bootutil source files to the Zephyr and espressif CMakeLists.txt files to fix undefined symbols. Signed-off-by: Maulik Patel <[email protected]> Change-Id: Ie75e6c533631b2696a4a41d86b64d4009fac0c54
1 parent 29c375a commit 90ff9de

File tree

9 files changed

+54
-13
lines changed

9 files changed

+54
-13
lines changed

boot/bootutil/include/bootutil/sign_key.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#ifdef MCUBOOT_IMAGE_MULTI_SIG_SUPPORT
3131
#include <stdbool.h>
3232
#endif /* MCUBOOT_IMAGE_MULTI_SIG_SUPPORT */
33+
#ifdef MCUBOOT_BUILTIN_KEY
34+
#include "bootutil/fault_injection_hardening.h"
35+
#endif /* MCUBOOT_BUILTIN_KEY */
3336

3437
#ifdef __cplusplus
3538
extern "C" {
@@ -51,7 +54,7 @@ extern const struct bootutil_key bootutil_keys[];
5154
*
5255
* @return 0 if the key ID is valid for the image; nonzero on failure.
5356
*/
54-
int boot_verify_key_id_for_image(uint8_t image_index, uint32_t key_id);
57+
fih_ret boot_verify_key_id_for_image(uint8_t image_index, uint32_t key_id);
5558
#endif /* MCUBOOT_BUILTIN_KEY */
5659
#else
5760
struct bootutil_key {

boot/bootutil/src/bootutil_find_key.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,27 @@
2828

2929
#include <stdint.h>
3030

31-
#include "bootutil/bootutil_log.h"
3231
#include "bootutil/crypto/sha.h"
3332
#include "bootutil/fault_injection_hardening.h"
3433
#include "bootutil/image.h"
3534
#include "bootutil/sign_key.h"
3635
#include "bootutil_priv.h"
3736
#include "mcuboot_config/mcuboot_config.h"
37+
#include "bootutil/bootutil_log.h"
38+
39+
BOOT_LOG_MODULE_DECLARE(mcuboot);
40+
41+
#if defined(MCUBOOT_SIGN_RSA) || \
42+
defined(MCUBOOT_SIGN_EC256) || \
43+
defined(MCUBOOT_SIGN_EC384) || \
44+
defined(MCUBOOT_SIGN_EC) || \
45+
defined(MCUBOOT_SIGN_ED25519)
46+
#define IMAGE_VALIDATION_EXPECTS_KEY
47+
#else
48+
/* no signing, sha256 digest only */
49+
#endif
3850

51+
#ifdef IMAGE_VALIDATION_EXPECTS_KEY
3952
#ifdef MCUBOOT_IMAGE_MULTI_SIG_SUPPORT
4053
#define NUM_OF_KEYS MCUBOOT_ROTPK_MAX_KEYS_PER_IMAGE
4154
#else
@@ -135,3 +148,4 @@ int bootutil_find_key(uint8_t image_index, uint8_t *keyhash, uint8_t keyhash_len
135148
return -1;
136149
}
137150
#endif
151+
#endif /* IMAGE_VALIDATION_EXPECTS_KEY */

boot/bootutil/src/bootutil_img_hash.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
#include <stdint.h>
3030
#include <flash_map_backend/flash_map_backend.h>
3131

32-
#include "bootutil/bootutil_log.h"
3332
#include "bootutil/crypto/sha.h"
3433
#include "bootutil/fault_injection_hardening.h"
3534
#include "bootutil/image.h"
3635
#include "bootutil_priv.h"
3736
#include "mcuboot_config/mcuboot_config.h"
37+
#include "bootutil/bootutil_log.h"
38+
39+
BOOT_LOG_MODULE_DECLARE(mcuboot);
3840

3941
#ifndef MCUBOOT_SIGN_PURE
4042
/*

boot/bootutil/src/image_validate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ bootutil_img_validate(struct boot_loader_state *state,
205205
int seed_len, uint8_t *out_hash
206206
)
207207
{
208-
#if (defined(EXPECTED_KEY_TLV) && defined(MCUBOOT_HW_KEY)) || defined(MCUBOOT_HW_ROLLBACK_PROT)
208+
#if defined(EXPECTED_KEY_TLV) || defined(MCUBOOT_HW_ROLLBACK_PROT)
209209
int image_index = (state == NULL ? 0 : BOOT_CURR_IMG(state));
210210
#endif
211211
uint32_t off;

boot/espressif/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ endif()
236236

237237
set(bootutil_srcs
238238
${BOOTUTIL_DIR}/src/boot_record.c
239+
${BOOTUTIL_DIR}/src/bootutil_find_key.c
240+
${BOOTUTIL_DIR}/src/bootutil_img_hash.c
241+
${BOOTUTIL_DIR}/src/bootutil_img_security_cnt.c
239242
${BOOTUTIL_DIR}/src/bootutil_misc.c
240243
${BOOTUTIL_DIR}/src/bootutil_public.c
241244
${BOOTUTIL_DIR}/src/caps.c

boot/zephyr/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ endif()
105105
# Generic bootutil sources and includes.
106106
zephyr_library_include_directories(${BOOT_DIR}/bootutil/include)
107107
zephyr_library_sources(
108+
${BOOT_DIR}/bootutil/src/bootutil_find_key.c
109+
${BOOT_DIR}/bootutil/src/bootutil_img_hash.c
110+
${BOOT_DIR}/bootutil/src/bootutil_img_security_cnt.c
108111
${BOOT_DIR}/bootutil/src/image_validate.c
109112
${BOOT_DIR}/bootutil/src/tlv.c
110113
${BOOT_DIR}/bootutil/src/encrypted.c

scripts/imgtool/image.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,12 @@ def verify(imgfile, key):
870870
# Locate the first TLV info header
871871
tlv_off = header_size + img_size
872872
tlv_info = b[tlv_off:tlv_off + TLV_INFO_SIZE]
873-
magic, tlv_tot = struct.unpack('HH', tlv_info)
873+
if len(tlv_info) < TLV_INFO_SIZE:
874+
# no protected block present, jump straight to unprotected
875+
magic = TLV_INFO_MAGIC
876+
tlv_tot = len(b) - tlv_off
877+
else:
878+
magic, tlv_tot = struct.unpack('HH', tlv_info)
874879

875880
# If it's the protected-TLV block, skip it
876881
if magic == TLV_PROT_INFO_MAGIC:
@@ -893,8 +898,12 @@ def verify(imgfile, key):
893898
is_pure = False
894899
scan_off = unprot_off
895900
while scan_off < unprot_end:
896-
tlv = b[scan_off:scan_off + TLV_SIZE]
897-
tlv_type, _, tlv_len = struct.unpack('BBH', tlv)
901+
# if fewer than TLV_SIZE bytes remain, break
902+
if scan_off + TLV_SIZE > len(b):
903+
break
904+
tlv_hdr = b[scan_off:scan_off + TLV_SIZE]
905+
tlv_type, _, tlv_len = struct.unpack('BBH', tlv_hdr)
906+
898907
if tlv_type == TLV_VALUES['SIG_PURE']:
899908
is_pure = True
900909
break
@@ -910,8 +919,11 @@ def verify(imgfile, key):
910919

911920
# Verify hash and signatures
912921
while scan_off < unprot_end:
913-
tlv = b[scan_off:scan_off + TLV_SIZE]
914-
tlv_type, _, tlv_len = struct.unpack('BBH', tlv)
922+
# stop if not enough bytes for another TLV header
923+
if scan_off + TLV_SIZE > len(b):
924+
break
925+
tlv_hdr = b[scan_off:scan_off + TLV_SIZE]
926+
tlv_type, _, tlv_len = struct.unpack('BBH', tlv_hdr)
915927
if is_sha_tlv(tlv_type):
916928
if not tlv_matches_key_type(tlv_type, key[0]):
917929
return VerifyResult.KEY_MISMATCH, None, None, None

scripts/imgtool/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,12 @@ def sign(key, public_key_format, align, version, pad_sig, header_size,
576576
compression_tlvs["DECOMP_SHA"] = img.image_hash
577577
compression_tlvs_size = len(compression_tlvs["DECOMP_SIZE"])
578578
compression_tlvs_size += len(compression_tlvs["DECOMP_SHA"])
579-
if img.get_signature():
580-
compression_tlvs["DECOMP_SIGNATURE"] = img.get_signature()
581-
compression_tlvs_size += len(compression_tlvs["DECOMP_SIGNATURE"])
579+
sigs = img.get_signature()
580+
if sigs:
581+
sig = sigs[0] if isinstance(sigs, list) else sigs
582+
compression_tlvs["DECOMP_SIGNATURE"] = sig
583+
compression_tlvs_size += len(sig)
584+
582585
if (compressed_size + compression_tlvs_size) < uncompressed_size:
583586
compression_header = create_lzma2_header(
584587
dictsize = comp_default_dictsize, pb = comp_default_pb,
@@ -588,7 +591,7 @@ def sign(key, public_key_format, align, version, pad_sig, header_size,
588591
keep_comp_size = False;
589592
if enckey:
590593
keep_comp_size = True
591-
compressed_img.create(key, public_key_format, enckey,
594+
compressed_img.create(loaded_keys, public_key_format, enckey,
592595
dependencies, boot_record, custom_tlvs, compression_tlvs,
593596
compression, int(encrypt_keylen), clear, baked_signature,
594597
pub_key, vector_to_sign, user_sha=user_sha, hmac_sha=hmac_sha,

sim/mcuboot-sys/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ fn main() {
450450

451451
conf.file("../../boot/bootutil/src/bootutil_find_key.c");
452452
conf.file("../../boot/bootutil/src/bootutil_img_hash.c");
453+
conf.file("../../boot/bootutil/src/bootutil_img_security_cnt.c");
453454
conf.file("../../boot/bootutil/src/image_validate.c");
454455
if sig_rsa || sig_rsa3072 {
455456
conf.file("../../boot/bootutil/src/image_rsa.c");

0 commit comments

Comments
 (0)