Secure Boot on STM32 with TrustZone:
A Guide to Secure Firmware Updates
With the introduction of the Cyber Resilience Act (CRA), bootloaders and security updates are more relevant than ever.
From the end of 2027, companies must provide security updates throughout the entire lifetime of a product if it falls under the CRA. A secure bootloader is essential to meet this requirement: on the one hand to install firmware packages, and on the other hand to guarantee a secure boot process whenever the device restarts.
Even apart from the CRA, it is always in a company's interest that only firmware from trusted sources is executed on a device.
In this article, you will learn:
- Which risks need to be considered during the boot process
- Which security features of an STM32 microcontroller can be used in a bootloader
- Which steps need to be implemented to ensure the integrity and authenticity of firmware
Potential Security Risks in the Update and Boot Process
Modification of the Boot Process
In this type of attack, the attacker attempts to change the boot mode or the boot address in order to take control of the CPU. This can be done, for example, via a debug or communication interface.
Firmware Updates
The bootloader makes firmware updates possible. However, there is also a risk that updates are carried out with corrupted firmware or from untrusted sources.
Bypassing Security Measures
In this case, the attacker attempts to bypass the integrity and authenticity check of the firmware. This could allow malware to be executed on a device.
Access to the Debug Interface
Access to a debug interface makes it possible to bypass security mechanisms and to access registers, flash and RAM.
Weaknesses in Communication Stacks
Due to the complexity of communication stacks, weaknesses or bugs cannot be ruled out. These can be exploited to gain access to the device via a network.
This is just a selection of possible security risks, and it clearly shows that particular attention must be paid to security when developing software for embedded devices. The bootloader is of central importance, as it is used both during the boot process and during a firmware update. It is therefore responsible for establishing a so-called “Root of Trust” (RoT), on which the rest of the firmware can build its security.
How Can the Security of a Bootloader Be Ensured?
Isolation of Resources
Some of the Arm Cortex processors used in STM32 microcontrollers, including the M23, M33 and M55, feature TrustZone technology. This makes it possible to divide both the bootloader and the application into a secure and a non-secure area. As a result, security-critical operations can be performed in an isolated area.
Trusted Firmware-M
The security features of the processors mentioned above are very extensive. On the one hand, this offers many possibilities; on the other hand, it makes implementation and integration more difficult. The open-source project “Trusted Firmware-M” (TF-M) provides an overview of how these security features can be used. As shown in Fig. 1, there are various secure services, for example for cryptographic operations or secure storage areas, which are available while the firmware is running. In this article, however, we focus on the features that are relevant for the bootloader.
TrustZone is based on separating memory and peripherals into secure and non-secure environments, each of which uses a different address alias. The state of the CPU depends on the address of the code being executed. If the code is located in a secure area of the flash, the CPU is in a secure state; if the code is in a non-secure area, the CPU is also in a non-secure state.
Access to resources is regulated so that everything can be accessed from the secure area, while the non-secure area can only access non-secure memory areas or peripherals. In addition, access can be further restricted from the secure area by configuring the Global TrustZone Controller (GTZC) accordingly.
So-called Non-Secure Callable (NSC) functions can be defined, allowing operations in the secure area to be performed from the non-secure area. NSC functions ensure clearly defined access without the risk of the non-secure application jumping to an invalid entry point. With this approach, for example, a GPIO pin configured as secure can be switched from the non-secure area via an NSC function.
Thanks to these security mechanisms, risks such as weaknesses in the communication stack or unauthorized access to memory areas containing security-critical data can be prevented.
TF-M uses the open-source project “MCUboot” as its bootloader. This provides all the components required for secure firmware, including the bootloader. Building on this, ST has ported TF-M to several of its microcontrollers. This implementation can serve as the basis for new projects that are intended to benefit from this secure foundation.
Immutable Code
In addition to TrustZone, there are security features that protect certain memory areas from unauthorized access. These protection mechanisms, like TrustZone, are configured and activated via non-volatile option bytes, which are stored in a separate area of the flash. The options relevant for the bootloader are briefly explained below.
Hide Protection (HDP)
To prevent the boot process from being modified or security measures from being bypassed, the bootloader's flash area can be protected by HDP. This protection mechanism can be activated immediately before jumping to the application. Until the next reset, it blocks all read and write accesses as well as instruction fetches.
Life Cycle Management
If several companies are involved in developing the software for a specific device, a first part of the firmware can be programmed into the device and this area can then be locked for the other companies. Depending on the stage of the development process, this makes it possible to define which flash areas are accessible (e.g. only the non-secure application). When regressing to a less secure state, the corresponding areas are erased.
Life cycle management is also relevant if a single company develops all of the software. The company can leave the device in the “Open” state during the development phase and thus has access to all memory areas and the debug interface. As soon as development is complete, the device is set to the “Locked” state. From this point on, access via the debugger is no longer possible and no regression can be performed. As a result, updates can only be carried out via the bootloader, and only in the areas defined for this purpose.
This step eliminates the risks associated with access to a debug interface.
Unique Boot Address
A uniquely defined boot address is another measure to prevent the boot process from being modified. When TrustZone is enabled, this address must be located in a secure flash area. This ensures that after a reset, the secure and immutable code that establishes the Root of Trust is always executed first.
In addition, this address can be locked by a further option bit, preventing any changes to the boot address.
Verifying the Integrity and Authenticity of the Firmware
Thanks to the security mechanisms in the bootloader, a Root of Trust is in place that can now be built upon. Before jumping to the firmware, the bootloader must verify that it comes from a trusted source and has not been modified since.
This verification may vary slightly depending on the implementation, but the following checks should be considered to ensure the integrity and authenticity of the firmware.
Version Check
Before a firmware update is performed, the versions of the current and the new image should be compared. If the update image has an older version than the image currently loaded, the update should be aborted. This is intended to prevent an older firmware version with potential security vulnerabilities from being executed.
Firmware Update
During a firmware update, it must be ensured that no data is disclosed during the transfer of the image if someone is eavesdropping on the communication. The data should therefore be encrypted before it is sent. Accordingly, the bootloader must implement a mechanism that decrypts the received data.
Once the firmware image has been fully transferred and decrypted, the following checks can be performed to establish its integrity and authenticity.
Hash Check
When the firmware image is created, a hash must be calculated and the result stored in the image header. The bootloader then calculates the hash over the same area and compares the result with the value stored in the header. If the two values are identical, the image has not been modified since it was created and its integrity is confirmed.
Signature Check
Validating the signature makes it possible to determine whether the firmware image comes from a trusted source. To enable this, an asymmetric cryptosystem is used. When the image is created, a signature is generated with the private key and stored in the header. The bootloader must have access to the corresponding public key and can use it to verify the signature.
Conclusion
To prevent updates with corrupted firmware, devices from being taken over by an attacker, or the loss of security-relevant data, sufficient attention should be paid to a device's bootloader. Apart from the potential risks, the CRA also creates a need for action from a legal perspective.
With TrustZone and the security features of ST microcontrollers, the risks during the boot and update process can be minimized, but only if they are used correctly. By applying these mechanisms in our in-house CSA bootloader, we have gained experience in the field of secure boot. We are happy to draw on this experience to support you in developing your bootloader.
References
-
AN5156 (https://www.st.com/resource/en/application_note/an5156-introduction-to-security-for-stm32-mcus-stmicroelectronics.pdf)
-
AN5447 (https://www.st.com/resource/en/application_note/an5447-overview-of-secure-boot-and-secure-firmware-update-solution-on-arm-trustzone-stm32-microcontrollers-stmicroelectronics.pdf)
-
AN5347 (https://www.st.com/resource/en/application_note/an5347-introduction-to-arm-trustzone-features-on-stm32l5-stm32u5-and-stm32u3-mcus-stmicroelectronics.pdf)
-
TF-M (https://www.trustedfirmware.org/projects/tf-m)
-
MCUboot (https://docs.mcuboot.com/)
-
RM0481 (https://www.st.com/resource/en/reference_manual/rm0481-stm32h52333xx-stm32h56263xx-and-stm32h573xx-armbased-32bit-mcus-stmicroelectronics.pdf)
Luc Probst
BSc BFH in Elektrotechnik and Informationstechnologie
Embedded Software Engineer
About the Author
Luc Probst has been working as an Embedded Software Engineer at CSA Engineering AG for two years, focusing on firmware development in C. He was involved in the further development of the CSA bootloader and carried out its port to an STM32 microcontroller.