Skip to content

Commit 1902783

Browse files
committed
zephyr: Add support for embedded AES key
The commit provides Kconfig options that allow to configure MCUboot to use embedded AES key. Primary option is CONFIG_BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY that allows to select usage of embedded key in the code. After it follow sets of Kconfigs: - CONFIG_BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER - CONFIG_BOOT_ENCRYPT_IMAGE_USE_CUSTOM_KEY_PROVIDER The above set allows to select source of the key. The first option will choose to generate default key provider, with a single embedded key, where the key is provided as a string assigned to CONFIG_BOOOT_ENCRYPT_IMAGE_EMBEDDED_RAW_KEY. The second option selects user provided code as source of key(s). Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent 2516adb commit 1902783

File tree

4 files changed

+144
-40
lines changed

4 files changed

+144
-40
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -383,45 +383,48 @@ if(NOT CONFIG_BOOT_SIGNATURE_TYPE_NONE AND NOT CONFIG_BOOT_SIGNATURE_KEY_FILE ST
383383
endif()
384384

385385
if(CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQUAL "")
386-
set(key_file "${CONFIG_BOOT_ENCRYPTION_KEY_FILE}")
387-
string(CONFIGURE "${key_file}" key_file)
386+
if(CONFIG_BOOT_ENCRYPT_IMAGE_WITH_SHARED_KEY)
387+
set(key_file "${CONFIG_BOOT_ENCRYPTION_KEY_FILE}")
388+
string(CONFIGURE "${key_file}" key_file)
389+
390+
if(IS_ABSOLUTE ${key_file})
391+
set(encryption_key_file ${key_file})
392+
elseif(EXISTS ${APPLICATION_CONFIG_DIR}/${key_file})
393+
set(encryption_key_file ${APPLICATION_CONFIG_DIR}/${key_file})
394+
else()
395+
set(encryption_key_file ${MCUBOOT_DIR}/${key_file})
396+
endif()
397+
message("MCUBoot bootloader encryption key file: ${encryption_key_file}")
398+
399+
# Emit a warning if using one of the default MCUboot key files
400+
set(mcuboot_default_encryption_files
401+
${MCUBOOT_DIR}/enc-ec256-priv.pem
402+
${MCUBOOT_DIR}/enc-ec256-pub.pem
403+
${MCUBOOT_DIR}/enc-rsa2048-priv.pem
404+
${MCUBOOT_DIR}/enc-rsa2048-pub.pem
405+
${MCUBOOT_DIR}/enc-x25519-priv.pem
406+
${MCUBOOT_DIR}/enc-x25519-pub.pem
407+
)
388408

389-
if(IS_ABSOLUTE ${key_file})
390-
set(encryption_key_file ${key_file})
391-
elseif(EXISTS ${APPLICATION_CONFIG_DIR}/${key_file})
392-
set(encryption_key_file ${APPLICATION_CONFIG_DIR}/${key_file})
393-
else()
394-
set(encryption_key_file ${MCUBOOT_DIR}/${key_file})
395-
endif()
396-
message("MCUBoot bootloader encryption key file: ${encryption_key_file}")
409+
if(${encryption_key_file} IN_LIST mcuboot_default_encryption_files)
410+
message(WARNING "WARNING: Using default MCUboot encryption key file, this file is for debug use only and is not secure!")
411+
endif()
397412

398-
# Emit a warning if using one of the default MCUboot key files
399-
set(mcuboot_default_encryption_files
400-
${MCUBOOT_DIR}/enc-ec256-priv.pem
401-
${MCUBOOT_DIR}/enc-ec256-pub.pem
402-
${MCUBOOT_DIR}/enc-rsa2048-priv.pem
403-
${MCUBOOT_DIR}/enc-rsa2048-pub.pem
404-
${MCUBOOT_DIR}/enc-x25519-priv.pem
405-
${MCUBOOT_DIR}/enc-x25519-pub.pem
406-
)
413+
set(GENERATED_ENCKEY ${ZEPHYR_BINARY_DIR}/autogen-enckey.c)
414+
add_custom_command(
415+
OUTPUT ${GENERATED_ENCKEY}
416+
COMMAND
417+
${PYTHON_EXECUTABLE}
418+
${MCUBOOT_DIR}/scripts/imgtool.py
419+
getpriv
420+
-k
421+
${encryption_key_file}
422+
> ${GENERATED_ENCKEY}
423+
DEPENDS ${encryption_key_file}
424+
)
407425

408-
if(${encryption_key_file} IN_LIST mcuboot_default_encryption_files)
409-
message(WARNING "WARNING: Using default MCUboot encryption key file, this file is for debug use only and is not secure!")
426+
zephyr_library_sources(${GENERATED_ENCKEY})
410427
endif()
411-
412-
set(GENERATED_ENCKEY ${ZEPHYR_BINARY_DIR}/autogen-enckey.c)
413-
add_custom_command(
414-
OUTPUT ${GENERATED_ENCKEY}
415-
COMMAND
416-
${PYTHON_EXECUTABLE}
417-
${MCUBOOT_DIR}/scripts/imgtool.py
418-
getpriv
419-
-k
420-
${encryption_key_file}
421-
> ${GENERATED_ENCKEY}
422-
DEPENDS ${encryption_key_file}
423-
)
424-
zephyr_library_sources(${GENERATED_ENCKEY})
425428
endif()
426429

427430
if(CONFIG_MCUBOOT_CLEANUP_ARM_CORE)
@@ -709,3 +712,18 @@ if(SYSBUILD)
709712
set(mcuboot_image_footer_size ${required_size} CACHE INTERNAL "Estimated MCUboot image trailer size" FORCE)
710713
set(mcuboot_image_upgrade_footer_size ${required_upgrade_size} CACHE INTERNAL "Estimated MCUboot update image trailer size" FORCE)
711714
endif()
715+
716+
if(${CONFIG_BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER})
717+
# Need to generate single key provider source, from template.
718+
# Take provided key, in form of a string and make it into C array, BOOT_AES_RAW_KEY_HEX_ARRAY,
719+
# of byte size hex values.
720+
set(BOOT_AES_RAW_KEY_HEX_STRING ${CONFIG_BOOT_ENCRYPT_IMAGE_EMBEDDED_RAW_KEY})
721+
string(REGEX REPLACE "(..)" "0x\\1, " BOOT_AES_RAW_KEY_HEX_ARRAY "${BOOT_AES_RAW_KEY_HEX_STRING}")
722+
723+
# The tamplate references BOOT_AES_RAW_KEY_HEX_ARRAY where it expects the array to be substituted.
724+
set(OUTPUT_BOOT_AES_RAW_KEY_SRC ${ZEPHYR_BINARY_DIR}/mcuboot_generated/builtin_aes_key_provider.c)
725+
configure_file(templates/single_builtin_aes_key_provider.c.template ${OUTPUT_BOOT_AES_RAW_KEY_SRC} @ONLY)
726+
727+
# Add generated source file to build
728+
zephyr_library_sources(${OUTPUT_BOOT_AES_RAW_KEY_SRC})
729+
endif()

boot/zephyr/Kconfig

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ config BOOT_ED25519_PSA_DEPENDENCIES
9595

9696
if BOOT_ENCRYPT_IMAGE
9797

98+
if !BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
99+
98100
config BOOT_X25519_PSA_DEPENDENCIES
99101
bool
100102
select PSA_WANT_ALG_ECDH
@@ -112,6 +114,8 @@ config BOOT_X25519_PSA_DEPENDENCIES
112114
to use with it; the others are used for shared key decryption
113115
and derivation.
114116

117+
endif # !BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
118+
115119
endif # BOOT_ENCRYPT_IMAGE
116120

117121
config BOOT_ECDSA_PSA_DEPENDENCIES
@@ -353,7 +357,7 @@ config BOOT_ED25519_PSA
353357
select BOOT_IMG_HASH_ALG_SHA256_ALLOW
354358
select BOOT_IMG_HASH_ALG_SHA512_ALLOW
355359
select BOOT_ED25519_PSA_DEPENDENCIES
356-
select BOOT_X25519_PSA_DEPENDENCIES if BOOT_ENCRYPT_IMAGE
360+
select BOOT_X25519_PSA_DEPENDENCIES if BOOT_ENCRYPT_IMAGE_WITH_SHARED_KEY
357361

358362
endchoice
359363

@@ -609,7 +613,8 @@ config BOOT_BOOTSTRAP
609613

610614
config BOOT_SWAP_SAVE_ENCTLV
611615
bool "Save encrypted key TLVs instead of plaintext keys in swap metadata"
612-
depends on BOOT_ENCRYPT_IMAGE
616+
depends on BOOT_ENCRYPT_IMAGE_WITH_SHARED_KEY
617+
depends on !BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
613618
help
614619
If y, instead of saving the encrypted image keys in plaintext in the
615620
swap resume metadata, save the encrypted image TLVs. This should be used
@@ -669,12 +674,62 @@ config BOOT_ENCRYPTION_SUPPORT
669674
help
670675
Hidden option used to check if image encryption is supported.
671676

672-
config BOOT_ENCRYPT_IMAGE
673-
bool "Support for encrypted image updates"
674-
depends on BOOT_ENCRYPTION_SUPPORT
677+
config BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
678+
bool "Use key that is already on board with MCUboot"
679+
depends on BOOT_ENCRYPT_IMAGE
680+
help
681+
The key is supposed to be either compiled in or on board.
682+
User is responsible for providing boot_enc_take_key
683+
function that will be able to retrieve the key.
684+
685+
if BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
686+
687+
choice BOOT_ENCRYPT_IMAGE_EMBEDDED_KEY_PROVIDER
688+
prompt "Embedded AES key provider"
689+
default BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER
690+
691+
config BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER
692+
bool "Generate basic boot_enc_take_key"
693+
depends on BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
694+
help
695+
Basic implementation of boot_enc_take_key will be implemented,
696+
that will have single key built in, used for all images and
697+
slots.
698+
699+
config BOOT_ENCRYPT_IMAGE_USE_CUSTOM_KEY_PROVIDER
700+
bool "User provides source code for key provider"
701+
help
702+
User is required to provide implementation for
703+
the boot_enc_take_key function.
704+
705+
endchoice # BOOT_ENCRYPT_IMAGE_EMBEDDED_KEY_PROVIDER
706+
707+
config BOOT_ENCRYPT_IMAGE_EMBEDDED_RAW_KEY
708+
string "Hexadecimal string representing AES key"
709+
depends on BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER
710+
help
711+
AES key in form of hexadecimal string that will be used to
712+
generate boot_enc_take_key function, returning the key for
713+
decryption and encryption of image.
714+
The key character length should be the double of expected
715+
AES key length in bytes.
716+
717+
endif # BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
718+
719+
config BOOT_ENCRYPT_IMAGE_WITH_SHARED_KEY
720+
bool
721+
default y if !BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
722+
depends on BOOT_ENCRYPT_IMAGE
675723
select BOOT_ENCRYPT_RSA if BOOT_SIGNATURE_TYPE_RSA
676724
select BOOT_ENCRYPT_EC256 if BOOT_SIGNATURE_TYPE_ECDSA_P256
677725
select BOOT_ENCRYPT_X25519 if BOOT_SIGNATURE_TYPE_ED25519
726+
help
727+
Hidden option for default behaviour where AES encryption key
728+
is derived from Public Key Cryptography key exchange.
729+
730+
config BOOT_ENCRYPT_IMAGE
731+
bool "Support for encrypted image updates"
732+
depends on BOOT_ENCRYPTION_SUPPORT
678733
depends on !SINGLE_APPLICATION_SLOT || MCUBOOT_SERIAL
679734
help
680735
If y, images in the secondary slot can be encrypted and are decrypted

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@
152152
#define MCUBOOT_USE_TLV_ALLOW_LIST 1
153153
#endif
154154

155+
#ifdef CONFIG_BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
156+
#define MCUBOOT_ENC_IMAGES
157+
#define MCUBOOT_EMBEDDED_ENC_KEY
158+
#endif
159+
155160
#ifdef CONFIG_BOOT_ENCRYPT_RSA
156161
#define MCUBOOT_ENC_IMAGES
157162
#define MCUBOOT_ENCRYPT_RSA
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Copyright (c) 2025 Nordic Semiconductor ASA
5+
*
6+
*/
7+
8+
#include <stddef.h>
9+
#include <stdbool.h>
10+
#include <inttypes.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
14+
#include "mcuboot_config/mcuboot_config.h"
15+
#include "bootutil/enc_key.h"
16+
17+
int boot_take_enc_key(uint8_t *key, int image, int slot)
18+
{
19+
const unsigned char array[] = {
20+
@BOOT_AES_RAW_KEY_HEX_ARRAY@
21+
};
22+
23+
memcpy(key, array, sizeof(array));
24+
25+
return 0;
26+
}

0 commit comments

Comments
 (0)