Skip to content

Commit

Permalink
zephyr: Enable building ed25519 PSA variant with Zephyr
Browse files Browse the repository at this point in the history
Adds Kconfig option CONFIG_BOOT_ED25519_PSA that allows to switch
ed25519 to PSA backend.

Signed-off-by: Dominik Ermel <[email protected]>
  • Loading branch information
de-nordic committed Jan 24, 2025
1 parent e27cff6 commit 378743e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 14 deletions.
14 changes: 10 additions & 4 deletions boot/bootutil/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ zephyr_library_link_libraries(MCUBOOT_BOOTUTIL)
target_link_libraries(MCUBOOT_BOOTUTIL INTERFACE zephyr_interface)

if(CONFIG_BOOT_USE_TINYCRYPT)
target_include_directories(MCUBOOT_BOOTUTIL INTERFACE
../../../ext/tinycrypt/lib/include
)
target_include_directories(MCUBOOT_BOOTUTIL INTERFACE
../../../ext/tinycrypt/lib/include
)
endif()

if(CONFIG_BOOT_USE_PSA_CRYPTO)
target_include_directories(MCUBOOT_BOOTUTIL INTERFACE
${ZEPHYR_MBEDTLS_MODULE_DIR}/include
)
endif()

if(CONFIG_BOOT_USE_MBEDTLS)
if(CONFIG_BOOT_USE_MBEDTLS OR CONFIG_BOOT_USE_PSA_CRYPTO)
zephyr_link_libraries(mbedTLS)
endif()
endif()
39 changes: 29 additions & 10 deletions boot/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ zephyr_library_include_directories(
include
)

if(DEFINED CONFIG_MBEDTLS)
zephyr_library_include_directories(
${ZEPHYR_MBEDTLS_MODULE_DIR}/include
)
endif()

# Zephyr port-specific sources.
zephyr_library_sources(
main.c
Expand Down Expand Up @@ -103,6 +109,10 @@ zephyr_library_sources(
${BOOT_DIR}/bootutil/src/fault_injection_hardening.c
)

if(DEFINED CONFIG_BOOT_ENCRYPT_X25519 AND DEFINED CONFIG_BOOT_ED25519_PSA)
zephyr_library_sources(${BOOT_DIR}/bootutil/src/encrypted_psa.c)
endif()

if(DEFINED CONFIG_MEASURED_BOOT OR DEFINED CONFIG_BOOT_SHARE_DATA)
zephyr_library_sources(
${BOOT_DIR}/bootutil/src/boot_record.c
Expand Down Expand Up @@ -243,19 +253,28 @@ elseif(CONFIG_BOOT_SIGNATURE_TYPE_ED25519 OR CONFIG_BOOT_ENCRYPT_X25519)
${FIAT_DIR}/include/
)

zephyr_library_sources(
${FIAT_DIR}/src/curve25519.c
)
if(NOT CONFIG_BOOT_ED25519_PSA)
zephyr_library_sources(
${FIAT_DIR}/src/curve25519.c
)
else()
zephyr_library_sources(
${MBEDTLS_ASN1_DIR}/src/asn1parse.c
${BOOT_DIR}/bootutil/src/ed25519_psa.c
)
endif()
endif()

if(CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_X25519)
zephyr_library_sources(
${TINYCRYPT_DIR}/source/aes_encrypt.c
${TINYCRYPT_DIR}/source/aes_decrypt.c
${TINYCRYPT_DIR}/source/ctr_mode.c
${TINYCRYPT_DIR}/source/hmac.c
${TINYCRYPT_DIR}/source/ecc_dh.c
if(NOT CONFIG_BOOT_ED25519_PSA)
if(CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_X25519)
zephyr_library_sources(
${TINYCRYPT_DIR}/source/aes_encrypt.c
${TINYCRYPT_DIR}/source/aes_decrypt.c
${TINYCRYPT_DIR}/source/ctr_mode.c
${TINYCRYPT_DIR}/source/hmac.c
${TINYCRYPT_DIR}/source/ecc_dh.c
)
endif()
endif()

if(CONFIG_BOOT_ENCRYPT_EC256)
Expand Down
69 changes: 69 additions & 0 deletions boot/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,60 @@ config NRF_CC310_BL
bool
default n

if BOOT_USE_PSA_CRYPTO

config BOOT_PSA_IMG_HASH_ALG_SHA256_DEPENDENCIES
bool
default y if BOOT_IMG_HASH_ALG_SHA256
select PSA_WANT_ALG_SHA_256
help
Dependencies for hashing with SHA256

config BOOT_ED25519_PSA_DEPENDENCIES
bool
select PSA_WANT_ALG_SHA_256
select PSA_WANT_ALG_SHA_512
select PSA_WANT_ALG_PURE_EDDSA
# Seems that upstream mbedTLS does not have TE
#select PSA_WANT_ECC_TWISTED_EDWARDS_255
select PSA_WANT_ECC_MONTGOMERY_255
select PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT
help
Dependencies for ed25519 signature

if BOOT_ENCRYPT_IMAGE

config BOOT_X25519_PSA_DEPENDENCIES
bool
select PSA_WANT_ALG_ECDH
select PSA_WANT_ALG_HMAC
select PSA_WANT_ALG_HKDF
select PSA_WANT_ALG_CTR
select PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT
select PSA_WANT_KEY_TYPE_DERIVE
select PSA_WANT_KEY_TYPE_AES
select PSA_WANT_ECC_MONTGOMERY_255
help
Dependencies for x25519 shared-random key encryption and AES
encryption. The PSA_WANT_ALG_CTR and PSA_WANT_KEY_TYPE_AES
enable Counter based block cipher and AES key, and algorithm support,
to use with it; the others are used for shared key decryption
and derivation.

endif # BOOT_ENCRYPT_IMAGE

if MBEDTLS_ENABLE_HEAP

config MBEDTLS_HEAP_SIZE
default 2048 if BOOT_USE_PSA_CRYPTO
help
The PSA internals need to be able to allocate memory for operation
and it uses mbedTLS heap for that.

endif # MBEDTLS_ENABLE_HEAP

endif # BOOT_USE_PSA_CRYPTO

menu "MCUBoot settings"

config SINGLE_APPLICATION_SLOT
Expand Down Expand Up @@ -120,6 +174,7 @@ endchoice # BOOT_IMG_HASH_ALG

choice BOOT_SIGNATURE_TYPE
prompt "Signature type"
default BOOT_SIGNATURE_TYPE_ED25519 if SOC_NRF54L15_CPUAPP
default BOOT_SIGNATURE_TYPE_RSA

config BOOT_SIGNATURE_TYPE_NONE
Expand Down Expand Up @@ -176,15 +231,28 @@ if BOOT_SIGNATURE_TYPE_ED25519
choice BOOT_ED25519_IMPLEMENTATION
prompt "Ecdsa implementation"
default BOOT_ED25519_TINYCRYPT

config BOOT_ED25519_TINYCRYPT
bool "Use tinycrypt"
select BOOT_USE_TINYCRYPT

config BOOT_ED25519_MBEDTLS
bool "Use mbedTLS"
select BOOT_USE_MBEDTLS
select MBEDTLS
select MBEDTLS_ASN1_PARSE_C if MBEDTLS_BUILTIN

config BOOT_ED25519_PSA
bool "Use PSA crypto"
select MBEDTLS
select BOOT_USE_PSA_CRYPTO
select MBEDTLS_PSA_CRYPTO_C
select MBEDTLS_ASN1_PARSE_C if MBEDTLS_BUILTIN
select PSA_CRYPTO_CLIENT
select PSA_CRYPTO_C
select BOOT_ED25519_PSA_DEPENDENCIES
select BOOT_X25519_PSA_DEPENDENCIES if BOOT_ENCRYPT_IMAGE

endchoice
endif

Expand Down Expand Up @@ -233,6 +301,7 @@ if MBEDTLS

config MBEDTLS_CFG_FILE
default "config-tls-generic.h" if MBEDTLS_BUILTIN
default "config-tls-generic.h" if BOOT_USE_PSA_CRYPTO
default "mcuboot-mbedtls-cfg.h" if BOOT_USE_MBEDTLS

endif
Expand Down

0 comments on commit 378743e

Please sign in to comment.