@@ -391,57 +391,59 @@ if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
391391endif ()
392392
393393if (CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQUAL "" )
394- # CONF_FILE points to the KConfig configuration files of the bootloader.
395- unset (CONF_DIR)
396- foreach (filepath ${CONF_FILE} )
397- file (READ ${filepath} temp_text)
398- string (FIND "${temp_text} " ${CONFIG_BOOT_ENCRYPTION_KEY_FILE} match)
399- if (${match} GREATER_EQUAL 0)
400- if (NOT DEFINED CONF_DIR)
401- get_filename_component (CONF_DIR ${filepath} DIRECTORY )
402- else ()
403- message (FATAL_ERROR "Encryption key file defined in multiple conf files" )
394+ if (NOT CONFIG_BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY)
395+ # CONF_FILE points to the KConfig configuration files of the bootloader.
396+ unset (CONF_DIR)
397+ foreach (filepath ${CONF_FILE} )
398+ file (READ ${filepath} temp_text)
399+ string (FIND "${temp_text} " ${CONFIG_BOOT_ENCRYPTION_KEY_FILE} match)
400+ if (${match} GREATER_EQUAL 0)
401+ if (NOT DEFINED CONF_DIR)
402+ get_filename_component (CONF_DIR ${filepath} DIRECTORY )
403+ else ()
404+ message (FATAL_ERROR "Encryption key file defined in multiple conf files" )
405+ endif ()
404406 endif ()
405- endif ()
406- endforeach ()
407+ endforeach ()
407408
408- if (IS_ABSOLUTE ${CONFIG_BOOT_ENCRYPTION_KEY_FILE} )
409- set (KEY_FILE ${CONFIG_BOOT_ENCRYPTION_KEY_FILE} )
410- elseif ((DEFINED CONF_DIR) AND
411- (EXISTS ${CONF_DIR} /${CONFIG_BOOT_ENCRYPTION_KEY_FILE} ))
412- set (KEY_FILE ${CONF_DIR} /${CONFIG_BOOT_ENCRYPTION_KEY_FILE} )
413- else ()
414- set (KEY_FILE ${MCUBOOT_DIR} /${CONFIG_BOOT_ENCRYPTION_KEY_FILE} )
415- endif ()
416- message ("MCUBoot bootloader encryption key file: ${KEY_FILE} " )
409+ if (IS_ABSOLUTE ${CONFIG_BOOT_ENCRYPTION_KEY_FILE} )
410+ set (KEY_FILE ${CONFIG_BOOT_ENCRYPTION_KEY_FILE} )
411+ elseif ((DEFINED CONF_DIR) AND
412+ (EXISTS ${CONF_DIR} /${CONFIG_BOOT_ENCRYPTION_KEY_FILE} ))
413+ set (KEY_FILE ${CONF_DIR} /${CONFIG_BOOT_ENCRYPTION_KEY_FILE} )
414+ else ()
415+ set (KEY_FILE ${MCUBOOT_DIR} /${CONFIG_BOOT_ENCRYPTION_KEY_FILE} )
416+ endif ()
417+ message ("MCUBoot bootloader encryption key file: ${KEY_FILE} " )
418+
419+ # Emit a warning if using one of the default MCUboot key files
420+ set (mcuboot_default_encryption_files
421+ ${MCUBOOT_DIR} /enc-ec256-priv.pem
422+ ${MCUBOOT_DIR} /enc-ec256-pub.pem
423+ ${MCUBOOT_DIR} /enc-rsa2048-priv.pem
424+ ${MCUBOOT_DIR} /enc-rsa2048-pub.pem
425+ ${MCUBOOT_DIR} /enc-x25519-priv.pem
426+ ${MCUBOOT_DIR} /enc-x25519-pub.pem
427+ )
417428
418- # Emit a warning if using one of the default MCUboot key files
419- set (mcuboot_default_encryption_files
420- ${MCUBOOT_DIR} /enc-ec256-priv.pem
421- ${MCUBOOT_DIR} /enc-ec256-pub.pem
422- ${MCUBOOT_DIR} /enc-rsa2048-priv.pem
423- ${MCUBOOT_DIR} /enc-rsa2048-pub.pem
424- ${MCUBOOT_DIR} /enc-x25519-priv.pem
425- ${MCUBOOT_DIR} /enc-x25519-pub.pem
426- )
429+ if (${KEY_FILE} IN_LIST mcuboot_default_encryption_files)
430+ message (WARNING "WARNING: Using default MCUboot encryption key file, this file is for debug use only and is not secure!" )
431+ endif ()
427432
428- if (${KEY_FILE} IN_LIST mcuboot_default_encryption_files)
429- message (WARNING "WARNING: Using default MCUboot encryption key file, this file is for debug use only and is not secure!" )
433+ set (GENERATED_ENCKEY ${ZEPHYR_BINARY_DIR} /autogen-enckey.c)
434+ add_custom_command (
435+ OUTPUT ${GENERATED_ENCKEY}
436+ COMMAND
437+ ${PYTHON_EXECUTABLE}
438+ ${MCUBOOT_DIR} /scripts/imgtool.py
439+ getpriv
440+ -k
441+ ${KEY_FILE}
442+ > ${GENERATED_ENCKEY}
443+ DEPENDS ${KEY_FILE}
444+ )
445+ zephyr_library_sources(${GENERATED_ENCKEY} )
430446 endif ()
431-
432- set (GENERATED_ENCKEY ${ZEPHYR_BINARY_DIR} /autogen-enckey.c)
433- add_custom_command (
434- OUTPUT ${GENERATED_ENCKEY}
435- COMMAND
436- ${PYTHON_EXECUTABLE}
437- ${MCUBOOT_DIR} /scripts/imgtool.py
438- getpriv
439- -k
440- ${KEY_FILE}
441- > ${GENERATED_ENCKEY}
442- DEPENDS ${KEY_FILE}
443- )
444- zephyr_library_sources(${GENERATED_ENCKEY} )
445447endif ()
446448
447449if (CONFIG_MCUBOOT_CLEANUP_ARM_CORE)
@@ -729,3 +731,30 @@ if(SYSBUILD)
729731 set (mcuboot_image_footer_size ${required_size} CACHE INTERNAL "Estimated MCUboot image trailer size" FORCE)
730732 set (mcuboot_image_upgrade_footer_size ${required_upgrade_size} CACHE INTERNAL "Estimated MCUboot update image trailer size" FORCE)
731733endif ()
734+
735+ if (${CONFIG_BOOT_ENCRYPT_IMAGE_WITH_EMBEDDED_KEY} )
736+ if (${CONFIG_BOOT_ENCRYPT_IMAGE_USE_CUSTOM_KEY_PROVIDER} )
737+ # User provides own code for key source
738+ set (CUSTOM_AES_KEY_PROVIDER_DIR ${PROJECT_BINARY_DIR} /custom_aes_provider)
739+
740+ message (STATUS "Building AES Custom key provider from " ${CONFIG_BOOT_ENCRYPT_IMAGE_EMBEDDED_CUSTOM_KEY_PROVIDER_DIR} )
741+ message (STATUS "AES Custom key provider bin dir is " ${CUSTOM_AES_KEY_PROVIDER_DIR} )
742+
743+ add_subdirectory (${CONFIG_BOOT_ENCRYPT_IMAGE_EMBEDDED_CUSTOM_KEY_PROVIDER_DIR} ${CUSTOM_AES_KEY_PROVIDER_DIR} )
744+ elseif (${CONFIG_BOOT_ENCRYPT_IMAGE_GENERATE_BASIC_KEY_PROVIDER} )
745+ # Need to generate single key provider source, from template.
746+ # Take provided key, in form of a string and make it into C array, BOOT_AES_RAW_KEY_HEX_ARRAY,
747+ # of byte size hex values.
748+ set (BOOT_AES_RAW_KEY_HEX_STRING ${BOOT_ENCRYPT_IMAGE_EMBEDDED_RAW_KEY} )
749+ string (REGEX REPLACE "(..)" "0x\\ 1, " BOOT_AES_RAW_KEY_HEX_ARRAY "${BOOT_AES_RAW_KEY_HEX_STRING} " )
750+
751+ # The tamplate references BOOT_AES_RAW_KEY_HEX_ARRAY where it expects the array to be substituted.
752+ set (OUTPUT_BOOT_AES_RAW_KEY_SRC ${ZEPHYR_BINARY_DIR} /mcuboot_generated/builtin_aes_key_provider.c)
753+ configure_file (templates/single_builtin_aes_key_provider.c.template ${OUTPUT_BOOT_AES_RAW_KEY_SRC} @ONLY)
754+
755+ # Add generated source file to build
756+ zephyr_library_sources(${OUTPUT_BOOT_AES_RAW_KEY_SRC} )
757+ else ()
758+ message (FATAL_ERROR "Unsupported embedded key configuration" )
759+ endif ()
760+ endif ()
0 commit comments