Last edited 3 weeks ago

How to enable firmware encryption in TF-M

Applicable for STM32MP21x lines, STM32MP23x lines, STM32MP25x lines

Trusted domain applicability
This article is only applicable to M33-TD flavor of STM32MP2 series

1. Article purpose[edit | edit source]

This article explains how to enable the encryption of the firmware images loaded by MCUboot, the secure bootloader (BL2) of TF-M.

It only applies to STM32MP2 series with the M33-TD flavor , where MCUboot is the FSBL-M.

The article describes:

  • the encryption principle and the keys involved,
  • the generation and the provisioning of the decryption key,
  • the TF-M build configuration,
  • the checks to perform on the generated images and on the target.
Information
Firmware encryption protects the confidentiality of the stored firmware. It does not replace the image authentication described in How to enable secure boot on STM32 MPU.

2. Overview[edit | edit source]

2.1. Principle[edit | edit source]

MCUboot supports encrypted images[1]. With M33-TD flavor , the images are decrypted by BL2 while they are loaded in RAM.

BL2 relies on ECIES-P256 (Elliptic Curve Integrated Encryption Scheme on the NIST P-256 curve). This hybrid scheme combines asymmetric and symmetric cryptography: an ECDH key agreement between an ephemeral key pair generated by imgtool[2] and the device key pair establishes a shared secret, from which symmetric keys are derived to encrypt and protect the AES key of the image.

For each image:

  • at build time, imgtool generates a random AES key, encrypts the image payload with AES-CTR, and protects the AES key with the ECIES-P256 public key. The protected AES key is stored in the image metadata (TLV).
  • at boot time, BL2 reads the ECIES-P256 private key from the BSEC OTP, recovers the AES key, decrypts the image in RAM and authenticates it.
Important
This mechanism is independent from:

2.2. Encrypted images[edit | edit source]

Image Binary Decrypted by MCUboot
TF-M secure + nonsecure firmware (M33FW) tfm_s_ns_signed.bin Yes
DDR PHY firmware (M33DDR) ddr_phy_signed.bin Yes
Optional Cortex-A35 32 bit bare metal firmware (STM32_CA35_FW) ca35_fw_signed.bin Yes
MCUboot (FSBL-M) bl2.stm32 No, see ROM code secure boot
Information
The _signed.bin suffix is kept for encrypted images: the file name does not indicate whether an image is encrypted.

2.3. Keys[edit | edit source]

The table below summarizes the keys used for an authenticated and encrypted boot on the Cortex-M33 side.

Key Usage Location
M33FW and M33DDR signature private keys Sign the images Host (MCUBOOT_KEY_S, MCUBOOT_KEY_DDRFW)
Signature public key hashes Authenticate the images OTP (FSBLM-M33-FW-PKH, FSBLM-DDR-FW-PKH)
ECIES-P256 public key Protect the AES key of each image Host (MCUBOOT_KEY_ENC)
ECIES-P256 private key Recover the AES key at boot time OTP (TFM_PRVK_ECIES_KEY[0-7])
AES key Encrypt the image payload Generated by imgtool for each image, stored protected in the image
Warning
The ECIES-P256 key pair must be different from the signature keys. It is also different from the HUK and from the ROM code and FIP encryption keys (EDMK, FIP-EDMK).

3. Implementation[edit | edit source]

The private key is retrieved by the platform function boot_enc_retrieve_private_key() in boot_enc_key.c . This function overrides the MCUboot default implementation, which embeds the key in the BL2 binary. It relies on a patch of MCUboot delivered with TF-M in lib/ext/mcuboot .

The key is read through the OTP framework described in How to manage keys and secrets in TF-M, with the PLAT_OTP_ID_KEY_BL2_ENCRYPTION identifier, mapped to the key_bl2_encryption NVMEM cell. For example, in stm32mp251.dtsi :

bsec: efuse@54000000 {
    ...
    key_bl2_encryption: otp328@520 {
        reg = <0x520 0x20>;
    };
};

The key_bl2_encryption cell must be listed in the nvmem-cells and nvmem-cell-names properties of the otp node of the BL2 device tree.

BL2 uses the SAES peripheral selected by the tfm,skcipher property of the BL2 device tree to accelerate the AES decryption. The SAES clock, reset and RIF configuration must allow its use by BL2.

The TF-M documentation also describes this feature[3].

4. Generate the ECIES-P256 key pair[edit | edit source]

Generate the key pair on a protected host, in a private directory:

umask 077
openssl ecparam -genkey -name prime256v1 -noout -out tfm_privk_ecies.pem
openssl pkey -in tfm_privk_ecies.pem -pubout -out tfm_pubk_ecies.pem
  • tfm_pubk_ecies.pem is the public key given to the TF-M build.
  • tfm_privk_ecies.pem is the embedded private key.

The OTP contains the raw 32-byte private scalar, not the PEM or DER file. With the commands above, the scalar is located at offset 7 of the SEC1 DER encoding.

It can be extracted with the following command:

openssl ec -in tfm_privk_ecies.pem -outform DER | dd bs=1 skip=7 count=32 status=none > tfm_privk_ecies.bin

Check that the bin file size is 32 bytes and that its content matches the priv field displayed by:

openssl pkey -in tfm_privk_ecies.pem -text -noout
Warning
These commands create unencrypted private key files. For production, use an approved key management process.

5. Provision the ECIES-P256 private key[edit | edit source]

5.1. OTP location[edit | edit source]

Device OTP words BL2 device tree cell
STM32MP21x lines  324 to 331, see STM32MP21 OTP mapping otp324@510
STM32MP23x lines  and STM32MP25x lines  328 to 335, see STM32MP23-25 OTP mapping otp328@520

BL2 reads the 8 OTP words as a 32-byte buffer, in memory order (each 32-bit word in little-endian), and uses this buffer as the big-endian private scalar. The provisioning tool must write the scalar with this byte order.

For the provisioning procedure, refer to How to update OTP or to SSP.

Warning
OTP programming is irreversible. Validate the encrypted boot on an open device before locking the OTP or closing the device.

5.2. Dummy provisioning for development[edit | edit source]

As described in How to manage keys and secrets in TF-M, TF-M provides dummy values to test the feature without programming the OTP:

Mode TFM_DUMMY_PROVISIONING STM32_OVERRIDE_OTP ECIES private key used by BL2
Development (default) ON OFF OTP value, or demonstration key if the OTP is not programmed (all zeros)
Development, OTP ignored ON ON Demonstration key
Production OFF OFF OTP value

The demonstration private key is defined in otp.c . It matches the MCUboot enc-ec256-pub.pem public key, used by default when MCUBOOT_KEY_ENC is not set.

Warning
The demonstration key is public: it provides no confidentiality. For production software, TFM_DUMMY_PROVISIONING and STM32_OVERRIDE_OTP must be set to OFF.

5.3. Provisioning with the U-Boot stm32key command[edit | edit source]

For development, the key can be programmed with the U-Boot stm32key command, after copying tfm_privk_ecies.bin in the bootfs partition:

stm32key select TFM-PRVK-ECIES
load mmc 0#bootfs 0x84000000 tfm_privk_ecies.bin
stm32key read 0x84000000
stm32key fuse 0x84000000

6. Build TF-M with encrypted images[edit | edit source]

6.1. Build options[edit | edit source]

Option Default value Description
MCUBOOT_ENC_IMAGES OFF Set to ON to enable the image encryption
MCUBOOT_KEY_ENC MCUboot enc-ec256-pub.pem Absolute path to the ECIES-P256 public key
MCUBOOT_ENCRYPT_EC256 ON Use ECIES-P256 to protect the AES key
MCUBOOT_CUSTOM_ENC_KEY ON Retrieve the private key from the platform (OTP)
MCUBOOT_ENC_KEY_LEN 128 AES key length, in bits
Important
MCUBOOT_KEY_ENC must match the private key provisioned in OTP. Otherwise BL2 cannot decrypt the images.

6.2. Build command lines[edit | edit source]

Start from the build described in How to configure, build and deploy TF-M, and add the options highlighted in the following example.

Example for the STM32MP257F-EV1 board, booting from SD card:

cmake -S ${TF-M-TESTS_DIRECTORY}/tests_reg/spe -B build_spe \
       -DTFM_PLATFORM=stm/stm32mp257f_ev1 \
       -DCONFIG_TFM_SOURCE_PATH=${TF-M_DIRECTORY} \
       -DTFM_TOOLCHAIN_FILE=${TF-M_DIRECTORY}/toolchain_GNUARM.cmake \
       -DTFM_PROFILE=profile_medium \
       -DSTM32_M33TDCID=ON -DTEST_S=OFF -DTEST_NS=OFF \
       -DDTS_EXT_DIR=${DT_PATH}/stm32mp2/m33-td \
       -DDTS_BOARD_S=tfm/stm32mp257f-ev1-cm33tdcid-ostl-sdcard-s.dts \
       -DDTS_BOARD_BL2=mcuboot/stm32mp257f-ev1-cm33tdcid-ostl-sdcard-bl2.dts \
       -DSTM32_BOOT_DEV=sdmmc1 \
       -DMCUBOOT_KEY_S=${KEYS_DIR}/m33fw_privateKey.pem \
       -DMCUBOOT_KEY_DDRFW=${KEYS_DIR}/m33ddr_privateKey.pem \
       -DMCUBOOT_ENC_IMAGES=ON \
       -DMCUBOOT_KEY_ENC=${KEYS_DIR}/tfm_pubk_ecies.pem \
       -DTFM_DUMMY_PROVISIONING=OFF
cmake --build build_spe --target install
cmake -S ${TF-M-TESTS_DIRECTORY}/tests_reg -B build_ns \
       -DCONFIG_SPE_PATH=${PWD}/build_spe/api_ns \
       -DSTM32_M33TDCID=ON \
       -DDTS_EXT_DIR=${DT_PATH}/stm32mp2/m33-td \
       -DDTS_BOARD_NS=tfm/stm32mp257f-ev1-cm33tdcid-ostl-sdcard-ns.dts
cmake --build build_ns
Information
The public key is exported in api_ns/image_signing/keys/image_enc_key.pem and used by the nonsecure build to encrypt the combined image. After any change of key or configuration, rebuild and install the secure build, then rebuild the nonsecure build. Use new build directories to avoid keeping old values in the CMake cache.
Warning
In this development flow, api_ns folder also contains the signature private keys. Do not distribute it.

7. Check the generated images[edit | edit source]

Check that the images are encrypted with the imgtool script of the MCUboot sources used by the build:

python3 ${MCUBOOT_DIR}/scripts/imgtool.py dumpinfo build_ns/tfm_s_ns_signed.bin

For an encrypted image with the default AES-128 key length:

  • the header flags contain ENCRYPTED_AES128 (0x4),
  • the TLV area contains an ENCEC256 entry (type 0x32).

Perform the same check on ddr_phy_signed.bin.

Information
This check confirms the image format. It does not confirm that the image can be decrypted with the key provisioned on the target.

8. Deploy and test on the target[edit | edit source]

  1. Sign the MCUboot binary (bl2.stm32) for the ROM code as described in How to enable secure boot on STM32 MPU. MCUBOOT_ENC_IMAGES has no effect on this binary.
  2. Flash the binaries generated by the same build, using the FlashLayout corresponding to the board, the M33-TD flavor and the boot device. See Modify, rebuild and reload the STM32CubeMP2 firmware for M33-TD.
  3. Make sure that no previous image remains in another slot: MCUboot could boot the old image instead of the new one.
  4. Boot the board: TF-M and the nonsecure application start.

The following negative tests can be performed on an open device, by changing the images and not the OTP:

Test Expected result
Image encrypted with another ECIES-P256 public key Image not started
Encrypted image modified after generation Image not started
Image with an invalid signature Image not started
Important
Enabling the encryption support does not reject images that are signed but not encrypted. If your product requires encrypted images only, this policy must be checked and enforced separately.

9. References[edit | edit source]


[[Category:Trusted Firmware-M|06]] [[Category:Secure boot]]