Skip to content

Commit 3f23e6d

Browse files
committed
[nrf noup] 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 <[email protected]>
1 parent 9de171d commit 3f23e6d

File tree

4 files changed

+156
-44
lines changed

4 files changed

+156
-44
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ zephyr_library_sources(
129129
${BOOT_DIR}/bootutil/src/fault_injection_hardening.c
130130
)
131131

132-
if((CONFIG_BOOT_ENCRYPT_X25519 AND CONFIG_BOOT_ED25519_PSA)
133-
OR (CONFIG_BOOT_ENCRYPT_EC256 AND CONFIG_BOOT_ECDSA_PSA))
132+
if(CONFIG_BOOT_ENCRYPT_IMAGE AND CONFIG_BOOT_USE_PSA_CRYPTO)
134133
zephyr_library_sources(${BOOT_DIR}/bootutil/src/encrypted_psa.c)
135134
endif()
136135

@@ -453,45 +452,48 @@ if(NOT CONFIG_BOOT_SIGNATURE_USING_KMU AND NOT CONFIG_BOOT_SIGNATURE_TYPE_NONE A
453452
endif()
454453

455454
if(CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQUAL "")
456-
set(key_file "${CONFIG_BOOT_ENCRYPTION_KEY_FILE}")
457-
string(CONFIGURE "${key_file}" key_file)
455+
if(CONFIG_BOOT_ENCRYPT_IMAGE_WITH_SHARED_KEY)
456+
set(key_file "${CONFIG_BOOT_ENCRYPTION_KEY_FILE}")
457+
string(CONFIGURE "${key_file}" key_file)
458+
459+
if(IS_ABSOLUTE ${key_file})
460+
set(encryption_key_file ${key_file})
461+
elseif(EXISTS ${APPLICATION_CONFIG_DIR}/${key_file})
462+
set(encryption_key_file ${APPLICATION_CONFIG_DIR}/${key_file})
463+
else()
464+
set(encryption_key_file ${MCUBOOT_DIR}/${key_file})
465+
endif()
466+
message("MCUBoot bootloader encryption key file: ${encryption_key_file}")
467+
468+
# Emit a warning if using one of the default MCUboot key files
469+
set(mcuboot_default_encryption_files
470+
${MCUBOOT_DIR}/enc-ec256-priv.pem
471+
${MCUBOOT_DIR}/enc-ec256-pub.pem
472+
${MCUBOOT_DIR}/enc-rsa2048-priv.pem
473+
${MCUBOOT_DIR}/enc-rsa2048-pub.pem
474+
${MCUBOOT_DIR}/enc-x25519-priv.pem
475+
${MCUBOOT_DIR}/enc-x25519-pub.pem
476+
)
458477

459-
if(IS_ABSOLUTE ${key_file})
460-
set(encryption_key_file ${key_file})
461-
elseif(EXISTS ${APPLICATION_CONFIG_DIR}/${key_file})
462-
set(encryption_key_file ${APPLICATION_CONFIG_DIR}/${key_file})
463-
else()
464-
set(encryption_key_file ${MCUBOOT_DIR}/${key_file})
465-
endif()
466-
message("MCUBoot bootloader encryption key file: ${encryption_key_file}")
478+
if(${encryption_key_file} IN_LIST mcuboot_default_encryption_files)
479+
message(WARNING "WARNING: Using default MCUboot encryption key file, this file is for debug use only and is not secure!")
480+
endif()
467481

468-
# Emit a warning if using one of the default MCUboot key files
469-
set(mcuboot_default_encryption_files
470-
${MCUBOOT_DIR}/enc-ec256-priv.pem
471-
${MCUBOOT_DIR}/enc-ec256-pub.pem
472-
${MCUBOOT_DIR}/enc-rsa2048-priv.pem
473-
${MCUBOOT_DIR}/enc-rsa2048-pub.pem
474-
${MCUBOOT_DIR}/enc-x25519-priv.pem
475-
${MCUBOOT_DIR}/enc-x25519-pub.pem
476-
)
482+
set(GENERATED_ENCKEY ${ZEPHYR_BINARY_DIR}/autogen-enckey.c)
483+
add_custom_command(
484+
OUTPUT ${GENERATED_ENCKEY}
485+
COMMAND
486+
${PYTHON_EXECUTABLE}
487+
${MCUBOOT_DIR}/scripts/imgtool.py
488+
getpriv
489+
-k
490+
${encryption_key_file}
491+
> ${GENERATED_ENCKEY}
492+
DEPENDS ${encryption_key_file}
493+
)
477494

478-
if(${encryption_key_file} IN_LIST mcuboot_default_encryption_files)
479-
message(WARNING "WARNING: Using default MCUboot encryption key file, this file is for debug use only and is not secure!")
495+
zephyr_library_sources(${GENERATED_ENCKEY})
480496
endif()
481-
482-
set(GENERATED_ENCKEY ${ZEPHYR_BINARY_DIR}/autogen-enckey.c)
483-
add_custom_command(
484-
OUTPUT ${GENERATED_ENCKEY}
485-
COMMAND
486-
${PYTHON_EXECUTABLE}
487-
${MCUBOOT_DIR}/scripts/imgtool.py
488-
getpriv
489-
-k
490-
${encryption_key_file}
491-
> ${GENERATED_ENCKEY}
492-
DEPENDS ${encryption_key_file}
493-
)
494-
zephyr_library_sources(${GENERATED_ENCKEY})
495497
endif()
496498

497499
if(CONFIG_MCUBOOT_CLEANUP_ARM_CORE)
@@ -801,3 +803,18 @@ if(SYSBUILD AND CONFIG_PCD_APP)
801803
set(RAM_FLASH_ADDR "${ram_flash_addr}" CACHE STRING "" FORCE)
802804
set(RAM_FLASH_SIZE "${ram_flash_size}" CACHE STRING "" FORCE)
803805
endif()
806+
807+
if(${CONFIG_BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER})
808+
# Need to generate single key provider source, from template.
809+
# Take provided key, in form of a string and make it into C array, BOOT_AES_RAW_KEY_HEX_ARRAY,
810+
# of byte size hex values.
811+
set(BOOT_AES_RAW_KEY_HEX_STRING ${CONFIG_BOOT_ENCRYPT_IMAGE_EMBEDDED_RAW_KEY})
812+
string(REGEX REPLACE "(..)" "0x\\1, " BOOT_AES_RAW_KEY_HEX_ARRAY "${BOOT_AES_RAW_KEY_HEX_STRING}")
813+
814+
# The tamplate references BOOT_AES_RAW_KEY_HEX_ARRAY where it expects the array to be substituted.
815+
set(OUTPUT_BOOT_AES_RAW_KEY_SRC ${ZEPHYR_BINARY_DIR}/mcuboot_generated/builtin_aes_key_provider.c)
816+
configure_file(templates/single_builtin_aes_key_provider.c.template ${OUTPUT_BOOT_AES_RAW_KEY_SRC} @ONLY)
817+
818+
# Add generated source file to build
819+
zephyr_library_sources(${OUTPUT_BOOT_AES_RAW_KEY_SRC})
820+
endif()

boot/zephyr/Kconfig

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,26 @@ config BOOT_ED25519_PSA_DEPENDENCIES
9696

9797
if BOOT_ENCRYPT_IMAGE
9898

99+
config BOOT_AES_DEPENDENCIES
100+
bool
101+
default y if BOOT_USE_PSA_CRYPTO
102+
select PSA_WANT_ALG_CTR
103+
select PSA_WANT_KEY_TYPE_AES
104+
select PSA_WANT_AES_KEY_SIZE_256 if BOOT_ENCRYPT_ALG_AES_256
105+
select PSA_WANT_AES_KEY_SIZE_128 if BOOT_ENCRYPT_ALG_AES_128
106+
help
107+
PSA Dependencies for image encryption. At this point they select
108+
AES CTR mode with support for key as selected by user.
109+
110+
if !BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
111+
99112
config BOOT_X25519_PSA_DEPENDENCIES
100113
bool
101114
select PSA_WANT_ALG_ECDH
102115
select PSA_WANT_ALG_HMAC
103116
select PSA_WANT_ALG_HKDF
104-
select PSA_WANT_ALG_CTR
105117
select PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT
106118
select PSA_WANT_KEY_TYPE_DERIVE
107-
select PSA_WANT_KEY_TYPE_AES
108119
select PSA_WANT_ECC_MONTGOMERY_255
109120
help
110121
Dependencies for x25519 shared-random key encryption and AES
@@ -113,6 +124,8 @@ config BOOT_X25519_PSA_DEPENDENCIES
113124
to use with it; the others are used for shared key decryption
114125
and derivation.
115126

127+
endif # !BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
128+
116129
endif # BOOT_ENCRYPT_IMAGE
117130

118131
config BOOT_ECDSA_PSA_DEPENDENCIES
@@ -368,7 +381,7 @@ config BOOT_ED25519_PSA
368381
select BOOT_IMG_HASH_ALG_SHA256_ALLOW if !PSA_CORE_LITE
369382
select BOOT_IMG_HASH_ALG_SHA512_ALLOW if !PSA_CORE_LITE
370383
select BOOT_ED25519_PSA_DEPENDENCIES
371-
select BOOT_X25519_PSA_DEPENDENCIES if BOOT_ENCRYPT_IMAGE
384+
select BOOT_X25519_PSA_DEPENDENCIES if BOOT_ENCRYPT_IMAGE_WITH_SHARED_KEY
372385

373386
endchoice
374387

@@ -698,7 +711,8 @@ config BOOT_BOOTSTRAP
698711

699712
config BOOT_SWAP_SAVE_ENCTLV
700713
bool "Save encrypted key TLVs instead of plaintext keys in swap metadata"
701-
depends on BOOT_ENCRYPT_IMAGE
714+
depends on BOOT_ENCRYPT_IMAGE_WITH_SHARED_KEY
715+
depends on !BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
702716
help
703717
If y, instead of saving the encrypted image keys in plaintext in the
704718
swap resume metadata, save the encrypted image TLVs. This should be used
@@ -766,12 +780,62 @@ config BOOT_ENCRYPTION_SUPPORT
766780
help
767781
Hidden option used to check if image encryption is supported.
768782

769-
config BOOT_ENCRYPT_IMAGE
770-
bool "Support for encrypted image updates"
771-
depends on BOOT_ENCRYPTION_SUPPORT
783+
config BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
784+
bool "Use key that is already on board with MCUboot [EXPERIMENTAL]"
785+
depends on BOOT_ENCRYPT_IMAGE
786+
help
787+
The key is supposed to be either compiled in or on board.
788+
User is responsible for providing boot_enc_take_key
789+
function that will be able to retrieve the key.
790+
791+
if BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
792+
793+
choice BOOT_ENCRYPT_IMAGE_EMBEDDED_KEY_PROVIDER
794+
prompt "Embedded AES key provider"
795+
default BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER
796+
797+
config BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER
798+
bool "Generate basic boot_enc_take_key"
799+
depends on BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
800+
help
801+
Basic implementation of boot_enc_take_key will be implemented,
802+
that will have single key built in, used for all images and
803+
slots.
804+
805+
config BOOT_ENCRYPT_IMAGE_USE_CUSTOM_KEY_PROVIDER
806+
bool "User provides source code for key provider"
807+
help
808+
User is required to provide implementation for
809+
the boot_enc_take_key function.
810+
811+
endchoice # BOOT_ENCRYPT_IMAGE_EMBEDDED_KEY_PROVIDER
812+
813+
config BOOT_ENCRYPT_IMAGE_EMBEDDED_RAW_KEY
814+
string "Hexadecimal string representing AES key [EXPERIMENTAL]"
815+
depends on BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER
816+
help
817+
AES key in form of hexadecimal string that will be used to
818+
generate boot_enc_take_key function, returning the key for
819+
decryption and encryption of image.
820+
The key character length should be the double of expected
821+
AES key length in bytes.
822+
823+
endif # BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
824+
825+
config BOOT_ENCRYPT_IMAGE_WITH_SHARED_KEY
826+
bool
827+
default y if !BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
828+
depends on BOOT_ENCRYPT_IMAGE
772829
select BOOT_ENCRYPT_RSA if BOOT_SIGNATURE_TYPE_RSA
773830
select BOOT_ENCRYPT_EC256 if BOOT_SIGNATURE_TYPE_ECDSA_P256
774831
select BOOT_ENCRYPT_X25519 if BOOT_SIGNATURE_TYPE_ED25519
832+
help
833+
Hidden option for default behaviour where AES encryption key
834+
is derived from Public Key Cryptography key exchange.
835+
836+
config BOOT_ENCRYPT_IMAGE
837+
bool "Support for encrypted image updates"
838+
depends on BOOT_ENCRYPTION_SUPPORT
775839
depends on !SINGLE_APPLICATION_SLOT || MCUBOOT_SERIAL
776840
help
777841
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
@@ -157,6 +157,11 @@
157157
#define MCUBOOT_USE_TLV_ALLOW_LIST 1
158158
#endif
159159

160+
#ifdef CONFIG_BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY
161+
#define MCUBOOT_ENC_IMAGES
162+
#define MCUBOOT_EMBEDDED_ENC_KEY
163+
#endif
164+
160165
#ifdef CONFIG_BOOT_ENCRYPT_RSA
161166
#define MCUBOOT_ENC_IMAGES
162167
#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)