Skip to content

Commit d1afad0

Browse files
maulik-armadeaarm
authored andcommitted
BL2: Add Key ID to image for the built in keys
When MCUBOOT_BUILTIN_KEY is enabled, add the key id to the image so that it can be parsed by mcuboot to identify the key used to sign the image. Signed-off-by: Maulik Patel <maulik.patel@arm.com> Change-Id: I6872b8a88e0292f7964a57a0219e913d599f9f95
1 parent 7474c75 commit d1afad0

File tree

3 files changed

+60
-40
lines changed

3 files changed

+60
-40
lines changed

bl2/ext/mcuboot/CMakeLists.txt

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -194,30 +194,39 @@ if (PLATFORM_DEFAULT_IMAGE_SIGNING)
194194
add_custom_target(tfm_s_signed_bin
195195
SOURCES tfm_s_signed.bin
196196
)
197+
198+
set(wrapper_args
199+
-v ${MCUBOOT_IMAGE_VERSION_S}
200+
--layout $<TARGET_OBJECTS:signing_layout_s>
201+
-k ${MCUBOOT_KEY_S}
202+
--public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
203+
--align ${MCUBOOT_ALIGN_VAL}
204+
--pad
205+
--pad-header
206+
-H ${BL2_HEADER_SIZE}
207+
-s ${MCUBOOT_SECURITY_COUNTER_S}
208+
-L ${MCUBOOT_ENC_KEY_LEN}
209+
-d \"\(1,${MCUBOOT_NS_IMAGE_MIN_VER}\)\"
210+
$<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
211+
$<$<BOOL:${MCUBOOT_CONFIRM_IMAGE}>:--confirm>
212+
$<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${MCUBOOT_KEY_ENC}>
213+
$<$<BOOL:${MCUBOOT_MEASURED_BOOT}>:--measured-boot-record>
214+
$<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
215+
${CMAKE_CURRENT_BINARY_DIR}/tfm_s_signed.bin
216+
)
217+
218+
if(MCUBOOT_BUILTIN_KEY)
219+
set(TFM_S_KEY_ID 0)
220+
set(wrapper_args ${wrapper_args} --psa-key-ids ${TFM_S_KEY_ID})
221+
endif()
222+
197223
add_custom_command(OUTPUT tfm_s_signed.bin
198224
DEPENDS tfm_s_bin signing_layout_s
199225
DEPENDS $<IF:$<BOOL:${MCUBOOT_GENERATE_SIGNING_KEYPAIR}>,generated_private_key,>
200226
WORKING_DIRECTORY ${MCUBOOT_PATH}/scripts
201227
202228
#Sign secure binary image with provided secret key
203-
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/wrapper/wrapper.py
204-
-v ${MCUBOOT_IMAGE_VERSION_S}
205-
--layout $<TARGET_OBJECTS:signing_layout_s>
206-
-k ${MCUBOOT_KEY_S}
207-
--public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
208-
--align ${MCUBOOT_ALIGN_VAL}
209-
--pad
210-
--pad-header
211-
-H ${BL2_HEADER_SIZE}
212-
-s ${MCUBOOT_SECURITY_COUNTER_S}
213-
-L ${MCUBOOT_ENC_KEY_LEN}
214-
-d \"\(1,${MCUBOOT_NS_IMAGE_MIN_VER}\)\"
215-
$<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
216-
$<$<BOOL:${MCUBOOT_CONFIRM_IMAGE}>:--confirm>
217-
$<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${MCUBOOT_KEY_ENC}>
218-
$<$<BOOL:${MCUBOOT_MEASURED_BOOT}>:--measured-boot-record>
219-
$<TARGET_FILE_DIR:tfm_s>/tfm_s.bin
220-
${CMAKE_CURRENT_BINARY_DIR}/tfm_s_signed.bin
229+
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/wrapper/wrapper.py ${wrapper_args}
221230
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/tfm_s_signed.bin $<TARGET_FILE_DIR:bl2>
222231
)
223232

bl2/ext/mcuboot/scripts/wrapper/wrapper.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#! /usr/bin/env python3
22
#
3-
# -----------------------------------------------------------------------------
4-
# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
3+
#-------------------------------------------------------------------------------
4+
# SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
55
#
66
# SPDX-License-Identifier: BSD-3-Clause
77
#
8-
# -----------------------------------------------------------------------------
8+
#-------------------------------------------------------------------------------
99

1010
import re
1111
import os
@@ -86,14 +86,16 @@
8686
@click.option('--public-key-format', type=click.Choice(['hash', 'full']),
8787
default='hash', help='In what format to add the public key to '
8888
'the image manifest: full key or hash of the key.')
89+
@click.option('--psa-key-ids', multiple=True, type=int, required=False,
90+
help='List of integer key IDs for each signature.')
8991
@click.option('-k', '--key', metavar='filename')
9092
@click.command(help='''Create a signed or unsigned image\n
9193
INFILE and OUTFILE are parsed as Intel HEX if the params have
9294
.hex extension, otherwise binary format is used''')
9395
def wrap(key, align, version, header_size, pad_header, layout, pad, confirm,
9496
max_sectors, overwrite_only, endian, encrypt, infile, outfile,
9597
dependencies, hex_addr, erased_val, save_enctlv, public_key_format,
96-
security_counter, encrypt_keylen, measured_boot_record):
98+
security_counter, encrypt_keylen, measured_boot_record, psa_key_ids):
9799

98100
slot_size = macro_parser.evaluate_macro(layout, sign_bin_size_re, 0, 1)
99101
load_addr = macro_parser.evaluate_macro(layout, load_addr_re, 0, 1)
@@ -128,6 +130,7 @@ def wrap(key, align, version, header_size, pad_header, layout, pad, confirm,
128130
max_align=max_align)
129131

130132
img.load(infile)
133+
img.set_key_ids(psa_key_ids)
131134
key = imgtool.main.load_key(key) if key else None
132135
enckey = imgtool.main.load_key(encrypt) if encrypt else None
133136
if enckey and key:

cmake/spe-CMakeLists.cmake

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,31 @@ if(BL2 AND PLATFORM_DEFAULT_IMAGE_SIGNING)
163163

164164
if (MCUBOOT_IMAGE_NUMBER GREATER 1)
165165

166+
set(wrapper_args
167+
--version ${MCUBOOT_IMAGE_VERSION_NS}
168+
--layout ${CMAKE_CURRENT_SOURCE_DIR}/image_signing/layout_files/signing_layout_ns.o
169+
--key ${CMAKE_CURRENT_SOURCE_DIR}/image_signing/keys/image_ns_signing_private_key.pem
170+
--public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
171+
--align ${MCUBOOT_ALIGN_VAL}
172+
--pad
173+
--pad-header
174+
-H ${BL2_HEADER_SIZE}
175+
-s ${MCUBOOT_SECURITY_COUNTER_NS}
176+
-L ${MCUBOOT_ENC_KEY_LEN}
177+
-d \"\(0, ${MCUBOOT_S_IMAGE_MIN_VER}\)\"
178+
$<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
179+
$<$<BOOL:${MCUBOOT_CONFIRM_IMAGE}>:--confirm>
180+
$<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${CMAKE_CURRENT_SOURCE_DIR}/image_signing/keys/image_enc_key.pem>
181+
$<$<BOOL:${MCUBOOT_MEASURED_BOOT}>:--measured-boot-record>
182+
$<TARGET_FILE_DIR:${NS_TARGET_NAME}>/${NS_TARGET_NAME}.bin
183+
${CMAKE_BINARY_DIR}/bin/${NS_TARGET_NAME}_signed.bin
184+
)
185+
186+
if(MCUBOOT_BUILTIN_KEY)
187+
set(TFM_NS_KEY_ID 1)
188+
set(wrapper_args ${wrapper_args} --psa-key-ids ${TFM_NS_KEY_ID})
189+
endif()
190+
166191
add_custom_target(${NS_TARGET_NAME}_signed_bin
167192
SOURCES ${CMAKE_BINARY_DIR}/bin/${NS_TARGET_NAME}_signed.bin
168193
)
@@ -172,24 +197,7 @@ if(BL2 AND PLATFORM_DEFAULT_IMAGE_SIGNING)
172197
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/image_signing/scripts
173198
174199
#Sign non-secure binary image with provided secret key
175-
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/image_signing/scripts/wrapper/wrapper.py
176-
--version ${MCUBOOT_IMAGE_VERSION_NS}
177-
--layout ${CMAKE_CURRENT_SOURCE_DIR}/image_signing/layout_files/signing_layout_ns.o
178-
--key ${CMAKE_CURRENT_SOURCE_DIR}/image_signing/keys/image_ns_signing_private_key.pem
179-
--public-key-format $<IF:$<BOOL:${MCUBOOT_HW_KEY}>,full,hash>
180-
--align ${MCUBOOT_ALIGN_VAL}
181-
--pad
182-
--pad-header
183-
-H ${BL2_HEADER_SIZE}
184-
-s ${MCUBOOT_SECURITY_COUNTER_NS}
185-
-L ${MCUBOOT_ENC_KEY_LEN}
186-
-d \"\(0, ${MCUBOOT_S_IMAGE_MIN_VER}\)\"
187-
$<$<STREQUAL:${MCUBOOT_UPGRADE_STRATEGY},OVERWRITE_ONLY>:--overwrite-only>
188-
$<$<BOOL:${MCUBOOT_CONFIRM_IMAGE}>:--confirm>
189-
$<$<BOOL:${MCUBOOT_ENC_IMAGES}>:-E${CMAKE_CURRENT_SOURCE_DIR}/image_signing/keys/image_enc_key.pem>
190-
$<$<BOOL:${MCUBOOT_MEASURED_BOOT}>:--measured-boot-record>
191-
$<TARGET_FILE_DIR:${NS_TARGET_NAME}>/${NS_TARGET_NAME}.bin
192-
${CMAKE_BINARY_DIR}/bin/${NS_TARGET_NAME}_signed.bin
200+
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/image_signing/scripts/wrapper/wrapper.py ${wrapper_args}
193201
)
194202
195203
# Create concatenated binary image from the two independently signed

0 commit comments

Comments
 (0)