Um2262 Getting Started With The Xcubesbsfu Stm32cube Expansion Package Stmicroelectronics
Um2262 Getting Started With The Xcubesbsfu Stm32cube Expansion Package Stmicroelectronics
Um2262 Getting Started With The Xcubesbsfu Stm32cube Expansion Package Stmicroelectronics
User manual
Getting started with the X-CUBE-SBSFU
STM32Cube Expansion Package
Introduction
This user manual describes how to get started with the X-CUBE-SBSFU STM32Cube
Expansion Package.
The X-CUBE-SBSFU secure boot and secure firmware update solution allows the update of
the STM32 microcontroller built-in program with new firmware versions, adding new
features and correcting potential issues. The update process is performed in a secure way
to prevent unauthorized updates and access to confidential on-device data.
The secure boot (Root of Trust services) is an immutable code, always executed after a
system reset, that checks STM32 static protections, activates STM32 runtime protections,
and then verifies the authenticity and integrity of user application code before every
execution to ensure that invalid or malicious code cannot be run.
The Secure Firmware Update application receives the firmware image via a UART interface
with the Ymodem protocol, checks its authenticity, and checks the integrity of the code
before installing it. The firmware update is done on the complete firmware image, or only on
a portion of the firmware image. Examples are provided for single-slot configuration to
maximize firmware image size, and for dual-slot configuration to ensure safe image
installation and enable over-the-air firmware update capability commonly used in IoT
devices. For a complex system, the firmware image configuration can be extended up to
three images. Examples can be configured to use asymmetric or symmetric cryptographic
schemes with or without firmware encryption.
The secure key management services provide cryptographic services to the user
application through the PKCS #11 APIs (KEY ID-based APIs) that are executed inside a
protected and isolated environment. User application keys are stored in the protected and
isolated environment for their secured update: authenticity check, data decryption, and data
integrity check.
STSAFE-A110 is a tamper-resistant secure element (Hardware Common Criteria EAL5+
certified) used to host X509 certificates and keys and perform verifications that are used for
firmware image authentication during secure boot and secure firmware update procedures.
X-CUBE-SBSFU is built on top of STM32Cube software technology, making the portability
across different STM32 microcontrollers easy. It is provided as a reference code to
demonstrate the best use of STM32 security protections.
X-CUBE-SBSFU is classified ECCN 5D002.
Contents
1 General information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1 Terms and definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
2 STM32Cube overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
6 Package description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.2 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.2.1 STM32CubeHAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.2.2 Board support package (BSP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.2.3 Cryptographic Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.2.4 Secure engine (SE) middleware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.2.5 Key management services (KMS) middleware . . . . . . . . . . . . . . . . . . . 38
6.2.6 STSAFE-A middleware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
6.2.7 Secure boot and secure firmware upgrade (SBSFU) application . . . . . 39
6.2.8 User application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6.3 Folder structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.4 APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.5 Application compilation process with IAR Systems® toolchain . . . . . . . . 43
8 Step-by-step execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.1 STM32 board preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.2 Application compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
8.3 Tera Term connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
8.3.1 ST-LINK disable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
8.3.2 Tera Term launch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
8.3.3 Tera Term configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
8.3.4 Welcome screen display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
8.4 SBSFU application execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
8.4.1 Download request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
8.4.2 Send firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
8.4.3 File transfer completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8.4.4 System restart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8.5 User application execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.5.1 Download a new firmware image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
8.5.2 Test protections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.5.3 Test secure engine user code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.5.4 Multiple downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.5.5 Firmware image validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
8.6 Programming a new software when the securities are activated . . . . . . . 61
Appendix F KMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
F.1 Key update process description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
F.2 SBSFU static keys generation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
F.3 Using KMS & X509 cryptographic scheme . . . . . . . . . . . . . . . . . . . . . . . . 88
List of tables
List of figures
1 General information
The X-CUBE-SBSFU Expansion Package comes with examples running on the STM32F4
Series, STM32F7 Series, STM32G0 Series, STM32G4 Series, STM32H7 Series, STM32L0
Series, STM32L1 Series, STM32L4 Series, STM32L4+ Series, and STM32WB Series. An
example combining STM32 microcontroller and STSAFE-A110 is also provided for the
STM32L4+ Series.
X-CUBE-SBSFU is provided as a reference code for standalone STM32 system solution
examples demonstrating the best use of STM32 protections to protect assets against
unauthorized external and internal access. X-CUBE-SBSFU proposes also a system
solution example combining STM32 and STSAFE-A110, which demonstrates hardware
secure element protections for secure authentication services and secure data storage.
X-CUBE-SBSFU is a starting point for OEMs to develop their SBSFU as a function of their
product security requirement levels.
The X-CUBE-SBSFU secure boot and secure firmware update Expansion Package runs on
STM32 32-bit microcontrollers based on the Arm®(a) Cortex®-M processor.
a. Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
Table 2 presents the definition of terms that are relevant for a better understanding of this
document.
Firmware image A binary image (executable) is run by the device as a user application.
Bundle of meta-data describing the firmware image to be installed. It contains
Firmware header
firmware information and cryptographic information.
mbed implementation of the TLS and SSL protocols and the respective
mbedTLS
cryptographic algorithms.
sfb file Binary file packing the firmware header and the firmware image.
1.2 References
STMicroelectronics related documents
Public documents are available online from the STMicroelectronics website at www.st.com.
Contact STMicroelectronics when more information is needed.
1. Application note Integration guide for the X-CUBE-SBSFU STM32Cube Expansion
Package (AN5056)
2. Application note Introduction to STM32 microcontrollers security (AN5156)
3. User manual STM32CubeProgrammer software description (UM2237)
4. Datasheet Authentication, state-of-the-art security for peripherals and IoT devices
(DS12911)
Other documents
5. PKCS #11 Cryptographic Token Interface Base Specification Version 2.40 Plus Errata
http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/os/pkcs11-curr-v2.40-os.html
2 STM32Cube overview
What is STM32Cube?
STM32Cube is an STMicroelectronics original initiative to significantly improve designer's
productivity by reducing development effort, time, and cost. STM32Cube covers the whole
STM32 portfolio.
STM32Cube includes:
• A set of user-friendly software development tools to cover project development from
conception to realization, among which are:
– STM32CubeMX, a graphical software configuration tool that allows the automatic
generation of C initialization code using graphical wizards
– STM32CubeIDE, an all-in-one development tool with peripheral configuration,
code generation, code compilation, and debug features
– STM32CubeProgrammer (STM32CubeProg), a programming tool available in
graphical and command-line versions
– STM32CubeMonitor (STM32CubeMonitor, STM32CubeMonPwr,
STM32CubeMonRF, STM32CubeMonUCPD) powerful monitoring tools to fine-
tune the behavior and performance of STM32 applications in real-time
• STM32Cube MCU and MPU Packages, comprehensive embedded-software platforms
specific to each microcontroller and microprocessor series (such as STM32CubeL4 for
the STM32L4 Series and STM32L4+ Series), which include:
– STM32Cube hardware abstraction layer (HAL), ensuring maximized portability
across the STM32 portfolio
– STM32Cube low-layer APIs, ensuring the best performance and footprints with a
high degree of user control over hardware
– A consistent set of middleware components such as FAT file system, RTOS,
OpenBootloader, USB Host, USB Device, TCP/IP, Touch library, and Graphics
– All embedded software utilities with full sets of peripheral and applicative
examples
• STM32Cube Expansion Packages, which contain embedded software components
that complement the functionalities of the STM32Cube MCU and MPU Packages with:
– Middleware extensions and applicative layers
– Examples running on some specific STMicroelectronics development boards
The package includes different sample applications to provide a complete SBSFU solution:
• SE_CoreBin application: provides a binary including all the ‘trusted’ code.
• Secure boot and secure firmware upgrade (SBSFU) application:
– Secure boot (Root of Trust)
– Local download via UART Virtual COM
– Firmware installation management
• User application:
– Downloads a new firmware in the dual-slot mode of operation
– Provides examples of testing protection mechanisms
– Provides examples using KMS APIs
The sample applications are delivered in dual-slot and single-slot modes of operation and
can be configured in different cryptographic schemes.
Note: The single-slot configuration is demonstrated in examples named 1_Image.
The dual-slot configuration is demonstrated in examples named 2_Images.
This user manual describes the typical use of the package:
• Based on the NUCLEO-L476RG board
• With sample applications operating in dual-slot mode and configured with asymmetric
authentication and symmetric firmware encryption
More information about the configuration options and the single-slot mode of operation are
provided in the appendices of this document.
Note: The KMS feature is available on the STM32L4 Series and STM32L4+ Series with the
example provided on the B-L475E-IOT01A and B-L4S5I-IOT01A boards.
Note: The STSAFE-A110 feature is available on the STM32L4+ Series with an example provided
on the B-L4S5I-IOT01A board.
Reset Code
2 3
Secure Boot
1
Trusted
In-field device
Encrypted
Firmware
Server
1 STM32
2 3
SBSFU Firmware
Communication channel
AES-CBC encryption,
or AES-CTR
encryption for STM32 AES-GCM encryption
Confidentiality No, the user firmware is in a clear format.
MCUs supporting (Firmware binary)
OTFDEC processing
(Firmware binary)
Integrity SHA256 (Firmware header and binary firmware) AES-GCM Tag
– SHA256 of the firmware header is ECDSA signed (Firmware header
Authentication and firmware binary)
– SHA256 of the firmware binary stored in the firmware header
Key management services (KMS) middleware provides cryptographic services through the
standard PKCS #11 APIs (specified by OASIS) allowing to abstract the key value to the
caller (using object ID and not directly the key-value). KMS is executed inside a
protected/isolated environment to ensure that the key value cannot be accessed by an
unauthorized code running outside the protected/isolated environment.
KMS also offers the possibility to use cryptographic services with keys that are managed
securely outside the STM32 microcontroller, such as by an STSAFE-A110 secure element
for example (rooting based on token ID).
KMS only supports a subset of PKCS #11 APIs:
• Object management functions: creation / update / deletion
• AES encryption functions
• AES decryption functions
• Digesting functions
• RSA and ECDSA Signing/Verifying functions
• Key management functions: key generation/derivation
KMS manages three types of keys:
• Static Embedded keys:
– Predefined keys are embedded within the code. Such keys can't be modified.
• Updatable keys with Static ID:
– Keys IDs are predefined in the system
– The key value can be updated in an NVM storage via a secure procedure using
static embedded root keys (authenticity check, data integrity check, and data
decryption)
– Key cannot be deleted
• Updatable keys with dynamic ID:
– Key IDs are defined when creating the keys
– The key value is created using internal functions. Typically, the DeriveKey()
function creates dynamic objects.
– Key can be deleted
Data a
file
User Application
NVM storage
Pairing keys Static
Isolated/protected environment
For more details regarding the OASIS PKCS #11 standard, refer to [5].
Figure 6. STM32L4, STM32L4+, and STM32L0 protection overview during SBSFU execution
SBS FU
Code and constan ts SBS FU Vdata/Stack/Heap
---
Acti ve image
Download Ima ge heade r Vda ta/Stack/Heap
Download Ima ge
Leg end
R: Read
Acti ve image hea der WRP + MPU-RX W: Write
X: eXecute
Figure 7. STM32F4, STM32F7 and STM32L1 protection overview during SBSFU execution
SBS FU
Code and constan ts SBS FU Vdata/Stack/Heap
---
Acti ve image
SWAP are a
Vda ta/Stack/Heap
Acti ve image hea der
Acti ve Ima ge
Download Ima ge
hea der Leg end
R: Read
MPU-RX privileged + WRP W: Write
Download Ima ge X: eXecute
MPU-RX + WRP
Table 4. MPU regions in the STM32F4 Series, STM32F7 Series, and STM32L1 Series
Region content Privileged permission Unprivileged permission
Read-only
Secure engine code and constants No access
(execution allowed)
Read/write
Secure engine stack and VDATA No access
(not executable)
Besides, the MPU also ensures that only authorized code is granted execution permission
when the secure boot and secure firmware update processes are running. This is the
reason why the MPU configuration is updated before launching the user application to
authorize its execution. Nevertheless, the secure engine isolation settings and supervisor
call mechanisms still apply when running the user application (not only when running the
SBSFU code).
Figure 8. STM32G0, STM32G4, and STM32H7 protection overview during SBSFU execution
Acti ve Ima ge
Leg end
SWAP are a
MPU-RX privileged + WRP + PCROP
Download Ima ge heade r
MPU-RX privileged + WRP
MPU-RX + WRP
Download Ima ge
MPU-RW then
MPU-RX during HDP a ctivatio n
Tamper: The anti-tamper protection is used to detect physical tampering actions on the
device and to take related countermeasures. In the case of tampering detection, the SBSFU
application example forces a reboot.
DAP (Debug Access Port): The DAP protection consists of de-activating the DAP (Debug
Access Port). Once de-activated, JTAG pins are no longer connected to the STM32 internal
bus. DAP is automatically disabled with RDP Level 2.
1. Not possible on the STM32H7 Series. Refer to Appendix I for more details.
Protections against inner attacks: Inner attacks refer to attacks triggered by code
running in the STM32. Attacks may be due to either malicious firmware exploiting bugs or
security breaches, or unwanted operations.
In the SBSFU application example, PCROP, WRP, and MPU protections preserve the
product from inner attacks:
• PCROP (proprietary code readout protection): a section of Flash is defined as
execute-only through PCROP protection. It is not possible to access this section in
reading or writing. Being an execute-only area, a key is protected with PCROP only if it
is ‘embedded’ in a piece of code: executing this code moves the key to a specific
pointer in RAM. Placed behind the firewall, its execution is impossible from the outside.
• WRP (write protection): write protection is used to protect trusted code from external
attacks or even internal modifications such as unwanted writings/erase operations on
critical code/data.
• MPU (memory protection unit): the protected environment managing all critical data
and operations (Secure engine) is isolated from the other software components by
leveraging the Memory Protection Unit (MPU). The secure engine code and data can
be accessed only through a privileged level of software execution. Therefore, software
running at a non-privileged level cannot call secure engine services or access critical
data. This strict access control to secure engine services and resources is
implemented by defining specific MPU regions described in Table 5.
Table 5. MPU regions in the STM32G0 Series, STM32G4 Series, and STM32H7 Series
Region content Privileged permission Unprivileged permission
Read-only
Secure engine code and constants No access
(execution allowed)
Read/write
Secure engine stack and VDATA No access
(not executable)
Besides, the MPU also ensures that only authorized code is granted execution
permission when the secure boot and secure firmware update processes are running.
Before launching the user application, the MPU protection is disabled but the secure
user memory protection is activated.
• Secure user memory: when the secure user memory protection is activated, any
access to the securable memory area (fetch, read, programming, erase) is rejected,
generating a bus error. All the code and secrets located inside the secure user memory
(a protected environment) are fully hidden. Secure engine stack and data are cleared
when launching the user application as not under secure user memory protection.
The code executed to activate the secure user memory must be located outside the
protected memory. In SBSFU, this code is located in RAM.
Figure 9 illustrates the closure of secure user memory when starting the user application.
Secure En gine
Vda ta
Secure
User
Memory HDP RAM activation
Acti ve image
Vda ta/Stack/Heap
Acti ve Ima ge
SWAP are a
No access
Download Ima ge
MPU-RX during HDP a ctivatio n
Acti ve Ima ge
Other registers:
AES_CR, AES_SR,
AES_IVR, AES DINR,
Download Ima ge heade r
AES_DOUTR Leg end
R: Read
MPU-RX + WRP W: Write
Download Ima ge X: eXecute
MPU-RW
leaving the SBSFU application, the MPU configuration is updated to authorize also the
execution of the user application code.
Figure 11. STM32L4+ with STSAFE-A110 protection overview during SBSFU execution
6 Package description
This section details the X-CUBE-SBSFU package content and the way to use it.
offload the host MCU of ECDSA cryptographic operations. More information about
STSAFE-A110 can be found at www.st.com/stsafe-a110.
• Availability of the user application example source code.
• The firmware image configuration can be extended up to three images for a complex
system with multiple firmware (such as protocol stack, middleware, and user
application.)
• User application can validate the installation of the new active image(s) in case of
successful validation of new image through “self-test”.
• Management of interruption during code execution inside the firewall is now supported
for applications requiring low latency on interruption handling.
• Availability of the firmware image preparation tool provided both as executable and
source code.
X-CUBE-SBSFU is ported on the STM32F4 Series, STM32F7 Series, STM32G0 Series,
STM32G4 Series, STM32H7 Series, STM32L0 Series, STM32L1 Series, STM32L4 Series,
STM32L4+ Series, and STM32WB Series. X-CUBE-SBSFU is also ported on the
STM32L4+ Series combined with STSAFE-A110 mounted on the B-L4S5I-IOT01A board.
The package includes sample applications that the developer can use to start experimenting
with the code.
The package is provided as a zip archive containing the source code.
The following integrated development environments are supported:
• IAR Systems® - IAR Embedded Workbench®(a)
• Keil® - MDK-ARM(a)
• STMicroelectronics - STM32CubeIDE
Note: The KMS feature is available on the STM32L4 Series and STM32L4+ Series with an
example provided on the B-L475E-IOT01A and B-L4S5I-IOT01A boards.
The STSAFE-A110 feature is available on the STM32L4+ Series with an example provided
on the B-L4S5I-IOT01A board.
®
a. On Windows only.
6.2 Architecture
This section describes the software components of the X-CUBE-SBSFU package illustrated
in Figure 12.
Application level
Middleware level
Drivers
6.2.1 STM32CubeHAL
The HAL driver layer provides a generic multi-instance simple set of APIs (application
programming interfaces) to interact with the upper layers (application, libraries and
stacks). It is composed of generic and extension APIs. It is directly built around a
generic architecture and allows the layers that are built upon, such as the middleware
layer, implementing their functionalities without dependencies on the specific hardware
configuration for a given microcontroller unit (MCU).
This structure improves the library code re-usability and guarantees an easy portability
onto other devices.
For STM32WB Series, the standalone YMODEM loader is replaced by the BLE_Ota loader.
Refer to Appendix H for details.
BSP drivers
HAL drivers
ST cryptographic library
KMS middleware
STSAFE-A middleware
6.4 APIs
Detailed technical information about the APIs is provided in a compiled HTML file located in
the STM32_Secure_Engine, STM32_Key_Management_Services, and STSAFE_A1xx
folders of the software package where all the functions and parameters are described.
STM32CubeProgrammer
STM32CubeProgrammer (STM32CubeProg) is an all-in-one multi-OS software tool for
programming STM32 microcontrollers. It provides an easy-to-use and efficient environment
for reading, writing, and verifying device memory through both the debug interface (JTAG
and SWD) and the bootloader interface (UART and USB).
STM32CubeProgrammer offers a wide range of features to program STM32 microcontroller
internal memories (such as Flash, RAM, and OTP) as well as external memories.
STM32CubeProgrammer also allows option programming and upload, programming
content verification, and microcontroller programming automation through scripting.
STM32CubeProgrammer is delivered in GUI (graphical user interface) and CLI (command-
line interface) versions.
More information about the preparation tool is provided in Appendix E: Firmware image
preparation tool.
8 Step-by-step execution
The following steps describe a dual-slot SBSFU scenario executed on the NUCLEO-
L476RG board with the default cryptographic scheme, further illustrated in Figure 17:
1. Download the SBSFU application
2. SBSFU is running: download UserApp #A
3. UserApp #A is installed
4. UserApp #A is running, download UserApp #B
5. UserApp #B is installed then running
The UserApp#A and UserApp#B binaries are generated based on the user application
example project. Defining the application as #A or #B is done by changing the value of the
UserAppId variable declared in the main.c of the application.
a. Automatically done when switching from RDP level 1 to RDP level 0 except on STM32H7 Series.
b. Automatically done when switching from RDP level 1 to RDP level 0.
Option bytes setting is verified using the STM32CubeProgrammer through the following four
steps:
4. Disconnect
a. Make sure the ST-LINK debugger/programmer embedded on the board runs the proper firmware version
(V2J29 or higher). If this is not the case, upgrade this firmware first.
Once the UserApp.sfb file is selected, the Ymodem transfer starts. Transfer progress is
reported as shown in Figure 27.
The progress gauge stalls for a short time at the beginning of the procedure while SBSFU
verifies the firmware header validity and erases the Flash slot where the firmware image is
downloaded.
The system status that is printed as shown in Figure 28 consequently provides the following
information:
• There is no firmware to download.
• The firmware is detected as encrypted. The user firmware is decrypted.
• If the decryption is OK, the user firmware is installed.
• If the installation is OK, the user firmware signature is verified.
• If the verification is OK, the user firmware is executed.
The test protection menu is printed at each test attempt of a prohibited operation or error
injection as a function of the test run:
• CORRUPT IMAGE test (#1)
– Causes a signature verification failure at the next boot.
• Firewall tests (#2, #3)
– Causes a reset trying to access protected code or data (either in RAM or Flash)
• PCROP test (#4)
– Causes an error trying to access the PCROP region protecting the keys
• WRP test (#5)
– Causes an error trying to erase write protected code
• IWDG test (#6)
– Causes a reset simulating a deadlock by not refreshing the watchdog
• TAMPER test (#7)
– Causes a reset if a tamper event is detected
– to generate a tamper event, the user must connect PA0 (CN7.28) to GND (It may
be enough to put a finger close to CN7.28).
Returning to the previous menu is obtained by pressing the x key.
Update the fields as follows. Set RDP to level 0 and set the WRP size to 0 and the start
address as 0xff. Then click on Apply for mass deletion of the FLASH to occur:
After this step, your FLASH is fully erased and you can program the software again.
Table 6 lists the main error messages together with their explanation.
Trailer erasing error Flash error detected when erasing the trailer area
Trailer update error Flash error detected when updating the trailer area
Magic tag update error Flash error detected when updating the clean tag inside the trailer area
Error encountered during an installation procedure: the firmware version
Firmware version rejected
cannot be accepted (newer firmware already installed or lower version than
(anti-rollback)
min. allowed version)
Error detected during firmware upgrade service (FUS) or wireless stack
CM0 update process error
update performed by CM0+
Undocumented error (unexpected exception or unexpected state machine
Unknown error.
issue)
The secure engine (SE) concept defines a protected enclave exporting a set of secure
functions executed in a trusted environment.
The following functionalities are provided by SE to the SBSFU application example:
• Secure engine initialization function
• Secure cryptographic functions
– AES-GCM and AES-CBC decryption
– SHA256 hash and ECDSA verification
– Sensitive data (secret key, AES context) never leaves the protected environment
and cannot be accessed from unprotected code
• Secure read/write access to firmware image Information
– Read and write operation on a protected Flash area
– Access to this area is allowed only to protected code
• Secure image state handling
• Secure service to lock some functions in the secure engine
– One-way lock mechanism: once locked, no way to unlock it except via a system
reset
– Once locked, functions execution is no more possible via the call gate mechanism
– Functionalities that are locked via the lock mechanism in secure engine example:
- Secure engine initialization function
- Secure encryption functions with OEM key
- Secure read/write access to firmware image Information
- Secure service to lock some functions in a secure engine
Note: Functionalities exported by SE can be extended depending on final user application needs.
For the STM32WB Series, SE provides two additional services:
• Drive wireless stack/FUS update performed by CM0+ core.
• Lock SBSFU symmetric key stored inside CM0+ Flash area before leaving SBSFU
execution program.
In the KMS variant, SE functionalities are extended with the secure key management
services providing cryptographic services to the user application through the PKCS #11
APIs (KEY ID-based APIs) are executed inside the secure-engine protected environment.
User application keys are stored in this protected/isolated environment.
In the STSAFE-A variant, KMS is extended with STSAFE-A middleware, to provide access
to keys and services provided by the secure element through a standard interface.
Communication with the STSAFE-A110 is secured with symmetric keys stored in the
protected/isolated environment.
To deal with the firewall call gate mechanism and to provide the user with a set of secure
APIs, SE is designed with a two-level architecture, composed of SE Core and SE Interface.
The call gate concept and the two-level architecture apply also when using the MPU to
protect the secure engine, as described in appendix A.2: MPU-based secure engine
isolation.
Caution: The code section must include all the code executed when the firewall is open. For instance,
if the call sequence is callgate->f1()->f1a()->f1b(), all the three functions f1(), f1a() and f1b()
must be included in the code section.
Figure 35 shows the steps to perform cryptographic operations (that require access to the
key) to respect the call gate mechanism.
For the cryptographic functions:
1. The SBSFU code calls the call gate function to open the firewall and to execute
protected code
2. The call gate function checks parameters and securities and then calls the requested
Crypto function
3. The SE Crypto function calls an internal ReadKey function that moves the keys into the
protected section of SRAM1 and then uses them in the cryptographic operations.
1 SBSFU
Code and constants
SBSFU Vdata/Stack/Heap
---
Download Image header
Active image Vdata/Stack/
Heap
Download Image
SE NVData segment
SWAP area
SE Code segment
A.1.2 SE interface
Code protected by the firewall must be non-interruptible and it is up to the user code to
disable interrupts before opening the firewall.
SE interface provides a user-friendly wrapper handling the entrance and exit to a protected
enclave where the actual SE call gate function is executed as illustrated in Figure 36.
SE interface mechanism simplifies the control access to the call gate independent from user
implementation. SE interface APIs are shared with the user application, which therefore
executes sensitive operations (if not locked via the secure engine lock service) in a secure
way using the services provided by SE.
Interruption management inside the firewall isolated environment can be activated when low
latency on interruption handling is required. Examples are provided in the 2_Images_OSC
variant for 32L496GDISCOVERY and B-L475E-IOT01A boards. Section 7.3 of the
application note How to activate interruption management inside firewall isolated
environment (AN5056) describes in detail all the steps required to activate this option.
A.2.1 Principle
The MPU-based secure engine isolation relies on the concept of privileged and unprivileged
levels of software execution. The software must run in an unprivileged level of execution by
default (when SBSFU or the User application is running), except for very specific actions like
platform initialization or interrupt handling. This is described in Figure 37.
Figure 37. SBSFU running in the unprivileged level of software execution for standard operations
When the software runs in an unprivileged mode, any attempt to access the secure engine
code or data results in an MPU fault: this ensures the isolation of the critical assets.
This isolation of the secure engine is implemented thanks to specific MPU regions as shown
in Table 7.
Read-only
Secure engine code and constants No access
(execution allowed)
Read/write
Secure engine stack and VDATA No access
(not executable)
To run a secure engine service, the caller must first enter the privileged level of software
execution through a controlled access point. This is done using the concept of SE interface
(refer to Section A.1.2: SE interface, keeping in mind that MPU protection replaces the
firewall protection). It abstracts the request to get the privileged level of software execution:
this request consists of triggering a supervisor (SVC) call.
In the SBSFU example delivered in the X-CUBE-SBSFU Expansion Package, the SBSFU
application implements an SVC handler to catch this SVC call and process it with another
SE interface service to enter the secure engine via its call gate as shown in Figure 38.
Note: The SVC handler must be trusted because it is a key element of secure engine access
control.
The SE call gate mechanism uses the concepts described in Section A.1.1: SE core call
gate mechanism to provide a unique entry point to secure engine services. The difference
with Section A.1.1 is that the MPU protection replaces the firewall protection; the constraints
for the placement of the call gate code are only the MPU region constraints (the call gate
must be located in the privileged code region).
When the secure engine processing ends, the call gate concept provides a single exit point
and the SVC call return sequence applies. This return sequence brings the software back to
the unprivileged level of execution. Then, any further direct access to the secure engine
code and data generates an MPU fault.
The secure engine service exit is described in Figure 39.
A.2.2 Constraints
The MPU-based secure engine isolation relies fully on the fact that a privileged level of
software execution is required to access the secure engine services. The SVC handler is
the controlled access point to a privileged level of execution (this must be trusted code).
Additionally, any piece of code running in privileged mode must be trusted also (interrupt
routines, initialization code, and others) so that the controlled access point is not bypassed.
It is key to partition the software very carefully and avoid granting a privileged level of
execution when not required (the software must run in an unprivileged mode as much as
possible).
The MPU controls the Cortex®-M access to the memory. Any peripheral acting as a master
on the bus may access the secure engine code and data without triggering an MPU fault (for
instance a DMA peripheral). It is therefore required to make sure that only trusted code can
program these peripherals. For instance, in the X-CUBE-SBSFU example for
32F413HDISCOVERY, an MPU region covers the DMA registers to make sure it is not
possible to program these peripherals in the unprivileged mode of execution: only the
privileged code can configure the DMAs.
Last but not least, for the STM32F4 Series and the STM32F7 Series, secure engine
protection is ensured only as long as the required MPU settings are maintained. If the user
application code is not fully trusted or if a bug can be exploited by a hacker, the MPU
configuration must be maintained during User application execution.
This latter constraint does not exist for the STM32 Series with secure user memory. Before
launching the user application, the MPU protection is disabled and the secure user memory
protection is activated. When secured, any access to the securable memory area (fetch,
read, programming, erase) is rejected, generating a bus error. All the code and secrets
located inside secure user memory (a protected environment) are fully hidden.
Some SBSFU application examples handle two slots located in internal Flash memory.
Figure 40. Internal user Flash mapping: Example of the NUCLEO-L476RG with 512-byte headers
Figure 41. User application vector table (example of the STM32L4 Series)
The X509 certificate-based asymmetric scheme makes use of a chain of X509 certificates to
deliver the public key used to verify the firmware header signature.
In the example provided, two certificates (the Root CA and OEM CA (first intermediate CA))
are embedded in secure element STSAFE-A110, or KMS non-volatile memory, while the
second intermediate CA and leaf certificate (firmware signing certificate) are delivered as
part of the firmware header.
To use the public key contained in the leaf certificate, the certificate chain is first verified by
the SBSFU code to ensure that the delivered firmware signing public key is authentic. Once
the certificate chain is verified, the firmware signing public key is used to verify the firmware
header signature.
E.2 Inputs
The package is delivered with some default keys and cryptography settings in folder
Applications\2_Images\2_Images SECoreBin\Binary.
Each of the following files can be used as such, or modified to take the user settings into
account:
• ECCKEY1.txt: private ECC key in PEM format. It is used to sign the firmware header.
This key is not embedded in the SECoreBin, only the corresponding public key is
generated by the tools in the se_key.s file
• nonce.bin: this is either a nonce (when AES-GCM is used) or an IV (when AES-CBC is
used). This value is added automatically by the tools to the firmware header.
• OEM_KEY_COMPANY1_key_AES_CBC.bin: symmetric AES-CBC key. This key is
used for the AES-CBC encryption and decryption operations and is embedded in the
se_key.s file. This file is exclusive with OEM_KEY_COMPANY1_key_AES_GCM.bin
• OEM_KEY_COMPANY1_key_AES_GCM.bin: symmetric AES-GCM key. This key is
used for all AES-GCM operations and is embedded in the se_key.s file. This file is
exclusive with OEM_KEY_COMPANY1_key_AES_CBC.bin
The tool uses the appropriate set of files based on the cryptographic scheme selected using
SECBOOT_CRYPTO_SCHEME in
Applications\2_Images\2_Images SECoreBin\Inc\se_crypto_config.h.
E.3 Outputs
The tool generates:
• The se_key.s file compiled in the SECoreBin project: this file contains the keys (private
symmetric key and public ECC key when applicable) embedded in the device and the
code to access them. When running the tool from the IDE, this file is located in
Applications\2_Images\2_Images SECoreBin\EWARM. In the case of multiple-image
configuration, one set of keys is generated per image.
• An sfb file packing the user firmware header and the encrypted user firmware image
(when the selected cryptographic scheme enables user firmware encryption). When
running the tool from the IDE, this file is generated in
Applications\2_Images\2_Images UserApp\Binary.
• A bin file concatenating the SBSFU binary, UserApp binary, and active firmware image
header. Flashing this file into the device with a flasher tool makes the UserApp
installation process simple since the firmware header and firmware image are already
correctly installed. It is not needed to use the SBSFU application for installing the
UserApp.
For STM32 devices with OTFDEC support and external Flash, two separate binary files
are generated:
– A first binary concatenating SBSFU binary and active firmware image header
(SBSFU_UserApp_Header.bin) to flash into the internal Flash memory.
– A second binary (UserApp.sfb) to flash into the external memory at the active slot
start address.
Refer to Appendix I and Appendix H for the specificities of STM32H7B3, STM32H750,
STM32WB55, and STM32WB5M devices.
Caution:
– Before programming the bin file into the device, a mass deletion must be
performed. to detect any malicious software, SBSFU verifies at startup that there
is no additional code after UserApp in the active slot.
– When the active slot is located in external Flash, the active slot must be erased
before programming the bin file.
• Two log files, output.txt, located in
Applications\2_Images\2_Images SECoreBin\EWARM\
and in
Applications\2_Images\2_Images UserApp\EWARM
to trace the executions of prebuild.bat and postbuild.bat.
When compiling with the IDE, the keys and the firmware image are handled. No extra action
is required from the user. At the end of the compilation steps:
• The required keys are embedded in the SECoreBin binary
• The firmware image to be installed is generated in the proper format, with the
appropriate firmware header, as an sfb file. This sfb file can be transferred over the
Ymodem protocol for installation by SBSFU.
• The bin file that can be flashed for the test of UserApp (SBFU_UserApp.bin).
The batch files integrating the tool in the IDE are located in the folder
Applications\2_Images\2_Images SECoreBin\EWARM:
• prebuild.bat: invoking the tool to perform the pre-build actions when compiling the
SECoreBin project
• postbuild.bat: invoking the tool to perform the post-build actions when compiling the
UserApp project
These batch files allow seamless switching from the Windows® executable variant to the
Python™ script variant of the prepareimage tool. The procedure is described in the files
themselves.
Appendix F KMS
As explained above, the STM32, STSAFE-A110, and firmware image must be provisioned
with some keys and or certificates:
• STM32: pairing keys must be inserted inside the SBSFU application code (inside the
part that is executed inside the protected environment) to be able to communicate
securely with an STSAFE-A110 component(a).
• STSAFE-A110:
– Pairing keys must be provisioned inside the STSAFE-A110 to be able to
communicate securely with an STM32 component(a). Pairing keys are stored in
se_key.s (section SE_ReadKey), as executable code, located in the project
2_Images_STSAFE\2_Images_SECoreBin.
– Root CA Cert and OEM CA Cert must be provisioned inside the STSAFE-A110 to
be able to verify OEM Divisional CA Cert and Firmware Signing Cert that are
received as part of the header of the new firmware image to be installed on the
STM32(a).
• Firmware image: OEM Divisional CA Cert and Firmware Signing Cert must be inserted
in the header of the new firmware image to be installed on the STM32(a).
a. A full code example for the provisioning of keys and certificates inside STSAFE-A110 is
provided. The project is called ‘STSAFE-A Provisioning’. More information in
Section G.3.
BLE_ota
Loader
Step 1 – SECoreBin compilation Step 2 – SBSFU compilation Step 3 – UserApp creation and loading via SFU
se_interface_appli.o
prebuild.bat postbuild.bat
SECoreBin SBSFU
UserApp
UserApp.bin UserApp.sfb
SE Callgate
Entry point
STM32 Internal Flash
STM32 Internal Flash
SBSFU Vector table
SBSFU Vector table
SE call gate SE call gate
SE call gate
SE Key Read functions
s SE Key Read functions
SE Key Read functions
SE Code SE Code
(Crypto library, HAL...) (Crypto library, HAL...) SE Code
(Crypto library, HAL...)
SE Core Vector SE Core Vector
SE Core Vector
SE Interface
SE Interface
SE_Core.bin
SBSFU
SBSFU
generates BLE_ota
BLE_ota
Download Image header
Input for Download Image header
Download Image
placed in
Download Image
upgrade completed
Immutable
BLE Loader BLE Loader BLE Loader BLE Loader BLE Loader BLE Loader
download
installatio
n
Cortex-M0 domain
BLE Stack BLE Stack BLE Stack BLE Stack BLE Stack BLE Stack
7 8
(secure)
V1 V1 V2 V2 V2 V2
3 4
standard installation
FUS FUS FUS FUS FUS FUS
Download slot
V1 V1
(encrypted) (encrypted) 10
Copy
Back-up slot
User Application User Application User Application User Application User Application User Application
V1 V1 V1 V1 V1 V1
(encrypted) (encrypted) (encrypted) (encrypted) (encrypted) 9 (encrypted)
Figure 61. JTAG connection capability on STM32H7B3 Series and STM32H753 Series
Figure 62. STM32H7B3: MPU isolation and secure user memory with external Flash
During the installation process, active slot firmware remains encrypted to ensure
confidentiality. On-the-fly decryption is performed by OTFDEC peripheral during the
execution of the user application from the external Flash memory.
A specific cryptographic scheme (SECBOOT_ECCDSA_WITH_AES128_CTR_SHA256) is
provided to illustrate the cryptographic operations required for this configuration: This
cryptographic scheme uses AES-CTR encryption and asymmetric (ECDSA) cryptography.
In such configuration with an external Flash, the loader functionality may be executed either
from internal Flash or from RAM to store downloaded firmware in external Flash during
program execution.
To offer the same level of security as other STM32 microcontrollers, the header of the active
slot must not be accessible during user application execution. For this reason, the header of
the active slot is stored in the internal Flash to be protected through the secure user
memory.
To illustrate this capability, an application called 2 images ExtFlash is provided for the
STM32H7B3I-DK board in the X-CUBE-SBSFU Expansion Package. Figure 63 shows the
typical memory mapping for such a configuration.
Figure 63. Memory mapping for STM32H7B3 devices with external Flash
As a consequence, two separate binary files are generated at the end of the user
application compilation:
• A first binary concatenating SBSFU binary and active firmware image header
(SBSFU_UserApp_Header.bin) to be programmed into the internal Flash memory.
• A second binary (UserApp.sfb) to be programmed into the external memory at the
active slot start address.
Both binary files must be flashed to start SBSFU with an already installed firmware image.
Secure Engine
Secure User Memory
Secure Engine
Vdata Decrypted Active Image
Code and constants
Leg end
As a consequence two separate binary files are generated at the end of the user application
compilation:
• SBSFU binary to be programmed into the internal Flash memory
• A second binary concatenating the active firmware image header and the user
application binary to be programmed into the external memory at the active slot start
address
Both binary files must be flashed to start SBSFU with an already installed firmware image.
UserApp_Loader
Loader
Step 3 – UserApp creation and loading via SFU
Step 0 bis – SBSFU Loader Step 2 – SBSFU compilation
compilation
se_interface_appli.o
UserApp.sfb
Loader
UserApp
Loader SBSFU STM32 Internal Flash
STM32 Internal Flash
SBSFU Vector table SBSFU Vector table
SE call gate SE call gate
Step 1 – SECoreBin compilation SE Key Read functions SE Key Read functions
SE startup code SE startup code
SE SE Code SE Code
Callgate (Crypto library, HAL...) (Crypto library, HAL...)
Entry SE Core Vector SE Core Vector
SECoreBin point
SE Interface SE Interface
SE call gate
SBSFU SBSFU
SE Key Read functions
s
SFU
SE startup code Loader Loader process
SE Code Counter Counter
(Crypto library, HAL...)
SE Core Vector SBSFU.bin SBSFU.bin
generates
Active image Active image
Input for
placed in
Download Image header Download Image header
Before the completion of the firmware update process, the user may need to control the
validity of the new image via the execution of the self-test. To enable this mechanism, the
switch ENABLE_IMAGE_STATE_HANDLING must be activated.
This feature can be activated only on a dual-slot configuration example with the swap
installation process selected.
A parameter called FwImageState is stored in the header of the image to indicate the state
of the firmware image and is updated according to the process of installation and validation
of the new image.
At the first user application start-up, if the execution is correct (for example after self-tests
execution) the user application must call a running service SE_APP_Validate(slot_id) if
available or update dedicated flags in RAM otherwise to validate the firmware image. If not
done a rollback on the previous firmware image is performed by SBSFU at the next reset.
The flow is described in Figure 66.
SBSFU
Reset
State = Yes
FWIMG_STATE_SELFTEST
No No
Is previous
Local Download
Yes firmware valid
Installation
to resume
Yes
Resume
No Roll-Back
FWIMG_STATE_VALID
FWIMG_STATE_VALID
Yes Installation
Reset trigged *
to perform Reset trigged *
Install No
Yes
Jump into UserApp
User Application
State = FWIMG_STATE_SELFTEST
FW IMG_STATE_NEW Start self-tests
Legend: Yes
No
Test State = No
UserApp execution FW IMG_STATE_NEW Reset trigged *
Action Yes
Continue User App
FwImageState value
execution
In a multiple image configuration, the slot identification parameter can be either 1,2,3, or
255. The value 255 indicates that all new firmware images are validated through a single
request. The objective is to ensure firmware compatibility between all new images in case of
interruption during the validation phase.
Revision history
Updated:
– Former figures updated to clearer versions
Added:
– Section 8.5.4: Multiple downloads
– Section 8.5.5: Firmware image validation
22-Jun-2021 9 – Section I.3: STM32H750B devices specificities
– Figure 59: Firmware upgrade services panel
– Figure 62: STM32H7B3: MPU isolation and secure
user memory with external Flash
– Figure 64: STM32H750 - MPU isolation
– Figure 65: STM32H750 - Image preparation
Updated:
– Section 2: STM32Cube overview
– Figure 4, Figure 5, Figure 11, Figure 14, Figure 18,
Figure 55, and Figure 56
14-Dec-2021 10 – Figure 58 title
Added:
– References to STM32L4+ Series
– Figure 60: Wireless stack update scenario
– Section H.3: Wireless stack/FUS update
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgment.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. For additional information about ST trademarks, please refer to www.st.com/trademarks. All other
product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.