Skip to content

Commit 5370225

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. Change the return type of boot_verify_key_id_for_image to reflect its use as an FIH call. Add new bootutil source files to the Zephyr and espressif CMakeLists.txt files to fix undefined symbols. Update FIH tests to use the latest TFM version. This also requires updating the toolchain version to 14.2 in the Docker image. Signed-off-by: Maulik Patel <maulik.patel@arm.com> Change-Id: Ie75e6c533631b2696a4a41d86b64d4009fac0c54
1 parent 10b2b13 commit 5370225

File tree

11 files changed

+106
-14
lines changed

11 files changed

+106
-14
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

ci/fih-tests_run.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
set -e
1818

1919
source $(dirname "$0")/fih-tests_version.sh
20+
TFM_TAG="958b54427156e66480489e53df6de085d62aef3a"
2021

2122
# Note that we are pulling from a github mirror of these repos, not direct upstream. If the sha
2223
# checked out below changes, the mirrors might need to be updated.
2324
pushd ..
2425
git clone https://github.com/mcu-tools/trusted-firmware-m
2526
pushd trusted-firmware-m
26-
git checkout eb8ff0db7d657b77abcd0262d5bf7f38eb1e1cdc
27+
git checkout $TFM_TAG
2728
source lib/ext/tf-m-tests/version.txt
2829
popd
2930
git clone https://github.com/mcu-tools/tf-m-tests.git

ci/fih_test_docker/execute_test.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,63 @@
1616

1717
set -e
1818

19+
# Function to update/install native GCC inside the Docker container
20+
update_native_gcc() {
21+
REQUIRED_MAJOR=12
22+
INSTALLED_MAJOR=$(gcc -dumpversion | cut -d. -f1 || echo 0)
23+
24+
if [[ "$INSTALLED_MAJOR" -lt "$REQUIRED_MAJOR" ]]; then
25+
echo "Installing native GCC $REQUIRED_MAJOR..."
26+
apt-get update
27+
apt-get install -y --no-install-recommends gcc-$REQUIRED_MAJOR g++-$REQUIRED_MAJOR \
28+
cpp-$REQUIRED_MAJOR libgcc-$REQUIRED_MAJOR-dev libstdc++-$REQUIRED_MAJOR-dev
29+
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$REQUIRED_MAJOR 60
30+
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-$REQUIRED_MAJOR 60
31+
rm -rf /var/lib/apt/lists/*
32+
else
33+
echo "Native GCC is already version $INSTALLED_MAJOR; skipping installation."
34+
fi
35+
}
36+
37+
# Function to update/install ARM Embedded GCC inside the Docker container
38+
update_cross_gcc() {
39+
ARM_GCC_URL="https://developer.arm.com/-/media/Files/downloads/gnu/14.2.rel1/binrel/arm-gnu-toolchain-14.2.rel1-x86_64-arm-none-eabi.tar.xz"
40+
TOOLCHAIN_DIR="/opt/arm-gcc"
41+
42+
# Install prerequisites
43+
echo "Installing prerequisites for ARM toolchain..."
44+
apt-get update
45+
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
46+
curl libncurses5 xz-utils file
47+
rm -rf /var/lib/apt/lists/*
48+
49+
# Download and extract
50+
echo "Downloading and extracting ARM Embedded GCC..."
51+
mkdir -p "$TOOLCHAIN_DIR"
52+
curl -SLf "$ARM_GCC_URL" -o /tmp/arm-gcc.tar.xz
53+
tar -xJf /tmp/arm-gcc.tar.xz -C "$TOOLCHAIN_DIR" --strip-components=1
54+
rm -f /tmp/arm-gcc.tar.xz
55+
56+
# Symlink into PATH
57+
echo "Symlinking ARM toolchain into /usr/local/bin..."
58+
ln -sf "$TOOLCHAIN_DIR/bin/"* /usr/local/bin/
59+
}
60+
61+
# Ensure we have the proper compiler before running tests
62+
update_native_gcc
63+
update_cross_gcc
64+
1965
source $(dirname "$0")/paths.sh
2066

2167
SKIP_SIZE=$1
2268
BUILD_TYPE=$2
2369
DAMAGE_TYPE=$3
2470
FIH_LEVEL=$4
2571

72+
# Required for git am to apply patches under TF-M
73+
git config --global user.email "docker@fih-test.com"
74+
git config --global user.name "fih-test docker"
75+
2676
if test -z "$FIH_LEVEL"; then
2777
# Use the default level
2878
CMAKE_FIH_LEVEL=""

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,

0 commit comments

Comments
 (0)