Skip to content

Commit 8ebbbd3

Browse files
committed
b0_signing: Add support for encrypted MCUboot img
Add a possibility to encrypt MCUboot update candidates. Ref: NCSDK-35089 Signed-off-by: Tomasz Chyrowicz <tomasz.chyrowicz@nordicsemi.no>
1 parent 3f881d6 commit 8ebbbd3

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

cmake/sysbuild/b0_mcuboot_signing.cmake

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ function(ncs_secure_boot_mcuboot_sign application bin_files signed_targets prefi
1616
find_program(IMGTOOL imgtool.py HINTS ${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts/ NAMES imgtool NAMES_PER_DIR)
1717
set(keyfile "${SB_CONFIG_BOOT_SIGNATURE_KEY_FILE}")
1818
string(CONFIGURE "${keyfile}" keyfile)
19+
set(keyfile_enc "${SB_CONFIG_BOOT_ENCRYPTION_KEY_FILE}")
20+
string(CONFIGURE "${keyfile_enc}" keyfile_enc)
1921

2022
# No imgtool, no signed binaries.
2123
if(NOT DEFINED IMGTOOL)
@@ -89,6 +91,31 @@ function(ncs_secure_boot_mcuboot_sign application bin_files signed_targets prefi
8991
set(imgtool_extra -k "${keyfile}" ${imgtool_extra})
9092
endif()
9193

94+
# Check if there is a bootloader configuration in the main image devicetree.
95+
dt_comp_path(mcuboot_configs TARGET "${application}" COMPATIBLE "nordic,mcuboot")
96+
if(mcuboot_configs)
97+
cmake_path(APPEND application_image_dir "zephyr" "edt.pickle" OUTPUT_VARIABLE edt_pickle)
98+
message(STATUS "Passing DTS-based MCUboot configuration: ${edt_pickle}")
99+
set(imgtool_extra ${imgtool_extra} --edt-config "${edt_pickle}")
100+
endif()
101+
102+
if(NOT "${keyfile_enc}" STREQUAL "")
103+
if(SB_CONFIG_BOOT_ENCRYPTION_ALG_AES_256)
104+
# Note: this overrides the default behavior of using AES-128
105+
set(imgtool_extra ${imgtool_extra} --encrypt-keylen 256 --load-addr ${slot_address})
106+
endif()
107+
108+
# Signature type determines key exchange scheme; ED25519 here means
109+
# ECIES-X25519 is used. Default to HMAC-SHA512 for ECIES-X25519.
110+
# Only .encrypted.bin file gets the ENCX25519/ENCX25519_SHA512, the
111+
# just signed one does not.
112+
# Only NRF54L gets the HMAC-SHA512, other remain with previously used
113+
# SHA256.
114+
if(SB_CONFIG_SOC_SERIES_NRF54L AND SB_CONFIG_BOOT_SIGNATURE_TYPE_ED25519)
115+
set(imgtool_extra ${imgtool_extra} --hmac-sha 512)
116+
endif()
117+
endif()
118+
92119
# Extensionless prefix of any output file.
93120
set(output ${CMAKE_BINARY_DIR}/signed_by_mcuboot_and_b0_${application})
94121

@@ -112,6 +139,12 @@ function(ncs_secure_boot_mcuboot_sign application bin_files signed_targets prefi
112139
list(APPEND bin_files ${output}.bin)
113140
set(bin_files ${bin_files} PARENT_SCOPE)
114141

142+
if(NOT "${keyfile_enc}" STREQUAL "")
143+
set(imgtool_extra_bin ${imgtool_extra} --encrypt "${keyfile_enc}")
144+
else()
145+
set(imgtool_extra_bin ${imgtool_extra})
146+
endif()
147+
115148
add_custom_command(
116149
OUTPUT
117150
${output}.bin # Signed hex with IMAGE_MAGIC located at secondary slot
@@ -121,7 +154,7 @@ function(ncs_secure_boot_mcuboot_sign application bin_files signed_targets prefi
121154
# Hence, if a programmer is given this hex file, it will flash it
122155
# to the secondary slot, and upon reboot mcuboot will swap in the
123156
# contents of the hex file.
124-
${imgtool_sign} ${imgtool_extra} ${CMAKE_BINARY_DIR}/signed_by_b0_${application}.bin ${output}.bin
157+
${imgtool_sign} ${imgtool_extra_bin} ${CMAKE_BINARY_DIR}/signed_by_b0_${application}.bin ${output}.bin
125158

126159
DEPENDS
127160
${application}_extra_byproducts

0 commit comments

Comments
 (0)