Check out my next generation bootloader and firmware update system stm32-secure-patching-bootloader.
IntroductionThis protip describes and demonstrates an industry leading secure firmware update system for the popular ultra-low-power STM32L0 microcontroller line found at the heart of so many IoT devices.
If you are building a connected product you need a Secure Bootloader and Secure Firmware Update (SBSFU) solution. There's no way around that. The solution I am describing here is a premium IoT and embedded firmware update system - offering a plethora of features including digitally signed and encrypted patches - that's right - patches, saving you the costs of both battery life and bandwidth for performing an in-field over-the-air update. This becomes very important as you deploy thousands of devices that must contend with a limited IoT data plan from a typical cellular or LPWAN service provider.
Get this SBSFU solution integrated into your project. Find out how.
In a NutshellA firmware preparation tool adapted to the build environment will compute the difference between the new build and a specified previous one (that is running on the fielded devices). The difference - a patch - is then digitally signed with ECDSA algorithms and encrypted with AES. A header is pre-pended and then it's ready to download to your devices.
On your devices, the patch data is delivered to the on-board patching engine service through APIs. The device validates the signature and decrypts the patch, then constructs the new firmware image in a temporary and safe download area in internal flash. When the patch is fully downloaded and the new firmware construction is complete, it is fully verified and the application can reboot to perform the new firmware installation into the active slot area.
On each boot, the bootloader checks if there is a valid new firmware image in the download area. If the signature checks out, the bootloader performs a firmware install. The install process swaps the active application in slot #0 with the new firmware image ready in slot #1. It does this in such a way that if it is interrupted, it can recover, and if a rollback is desired that is possible too. The installation on STM32L0 devices is performed using half-page writes and takes just a few seconds.
The infographic below describes the flash layout. Like previous bootloader and OTA update projects I've done, this solution also partitions the flash into two equal sized application images. I've used the terms APP1 and APP2 before; now using ST nomenclature I use Slot 0 and Slot 1. Same thing.
Security is a big deal in IoT these days. How will you respond to eventual questions from your customers and suppliers about the security of your devices? There are many facets to device security, but this bootloader and FOTA solution I'm presenting here will satisfy the highest security standards and will give you peace mind knowing your deployed IoT fleet is secured.
This secure boot and FOTA update solution uses both authenticity and confidentiality to protect your IP and customer's investment. Only firmware patches signed by your ECDSA private key will be accepted by the devices through any access ports you may open. This ensures that no malicious firmware can get onto the devices. Next, the firmware patches are encrypted, which ensures that no-one in the firmware deployment chain (e.g. cloud provider/hosting service) can copy, clone or otherwise deduce anything about your application.
Lastly, all of this is only possible when a root of trust is established, and that is easily and simply done by enabling RDP Level 2 on STM32 devices. RDP Level 2 disables external access via the debug port so there is no way for anyone to circumvent these security features. Note that only your public ECDSA key is stored on the device internally, so even if an advanced organization were to physically deconstruct the chip (!) they could only attack that one device.
This is the kind of security you need to be thinking about in your next IoT deployment, whether you are developing a solution or buying one. Ask these questions:
- Is the IoT device's debug port locked down / disabled?
- Are firmware updates digitally signed by the producer?
- Are firmware updates encrypted?
Yes, there is a secure firmware update with delta patching demo that you can run on a Nucleo-L073RZ board. I'll get into some more detail on how the patching update works.
The STM32L0 "Z" device variant is highly recommended with its 192 KB flash, allowing for a reasonable application size to be deployed to a flash layout that hosts two application slots and a bootloader.
This demo supports simulating a firmware update "OTA" over UART via the YMODEM transfer protocol of a terminal program like TeraTerm on your PC.
The firmware update is delivered as a secure and signed patch file that modifies a string in the application banner message and increments the version from 0.1 to 0.2. The signed and encrypted patch file is 304 bytes and applied against an original firmware image size of 14768 bytes. The compression in this admittedly trivial case is extremely high, over 99%. The patch file actually has a fixed-size header of 256 bytes containing various meta data including signatures and SHA256 hashes of the patch and the expected reconstructed updated firmware. The actual patch data is limited to almost exactly what has changed, owing to the efficiency of JojoDiff - the tool that creates the raw patch data.
This is what you need on hand to setup the demo:
- NUCLEO-L073RZ
- STM32CubeProgrammer
- TeraTerm (or similar)
- Combined "manufacturing" image with bootloader and application: SBSFU_DemoApp_v0.1.bin
- 0.1 to 0.2 firmware update patch file: DemoApp_v0.1_v0.2.sfbp
Use STM32CubeProgrammer to program the combined image (bin file) onto the NUCLEO. Important!! When programming is complete, dismiss the dialog box then click DISCONNECT target in the STM32CubeProgrammer window, otherwise the target may suspend operation before loading the application and appear to hang.
Setup TeraTerm for 115200 8N1 to the Nucleo's STLINK virtual COM port.
Press the black (reset) button on the Nucleo and observe the bootloader and application startup trace messages.
= [SBOOT] RuntimeProtections: 0
= [SBOOT] System Security Check successfully passed. Starting...
= [FWIMG] Slot #0 @: 801f000 / Slot #1 @: 800f000 / Swap @: 802f000
======================================================================
= Firmware Modules =
= =
= Secure Boot and Secure Firmware Update =
= With Delta Patching for STM32L0 =
= =
= *** Bootloader *** =
======================================================================
Date: Sep 24 2019 18:30:09
Version: 4aeaf12-dirty
Patch Mode: Enabled
= [SBOOT] STATE: WARNING: SECURE ENGINE INITIALIZATION WITH FACTORY DEFAULT VALUES!
========= End of Execution ==========
= [SBOOT] RuntimeProtections: 0
= [SBOOT] System Security Check successfully passed. Starting...
= [FWIMG] Slot #0 @: 801f000 / Slot #1 @: 800f000 / Swap @: 802f000
======================================================================
= Firmware Modules =
= =
= Secure Boot and Secure Firmware Update =
= With Delta Patching for STM32L0 =
= =
= *** Bootloader *** =
======================================================================
Date: Sep 24 2019 18:30:09
Version: 4aeaf12-dirty
Patch Mode: Enabled
= [SBOOT] SECURE ENGINE INITIALIZATION SUCCESSFUL
= [SBOOT] STATE: CHECK STATUS ON RESET
INFO: A Reboot has been triggered by a Software reset!
Consecutive Boot on error counter reset
Consecutive Boot on error counter = 0
Consecutive Boot on error counter updated
INFO: Last execution status before Reboot was:Checking Status on Reset.
INFO: Last execution detected error was:No error. Success.
= [SBOOT] STATE: CHECK NEW FIRMWARE TO DOWNLOAD
= [SBOOT] STATE: CHECK USER FW STATUS
Check MAGIC @801ef00 trailer size:512
A valid FW is installed in the active slot - version: 1
= [SBOOT] STATE: VERIFY USER FW SIGNATURE
= [SBOOT] STATE: EXECUTE USER FIRMWARE
======================================================================
= Firmware Modules =
= =
= Secure Boot and Secure Firmware Update =
= With Delta Patching for STM32L0 =
= =
= *** Demo App *** =
= =
= Demo App v0.1 =
= FW Tag: 0x42D5BD81 =
======================================================================
Date: Sep 24 2019 18:38:23
=================== Main Menu ============================
Download a new Fw Image ------------------------------- 1
Test Protections -------------------------------------- 2
Test SE User Code ------------------------------------- 3
At this point, the version 0.1 application has booted and is running.
Select option 1 "Download a new Fw Image". Then in TeraTerm start a YMODEM transfer of the patch file: File->Transfer->YMODEM
You will see a transfer window as shown below.
Subsequently, the application firmware will automatically reboot and will show trace messages detailing the installation process and finally the new application will boot and run.
================ New Fw Download =========================
-- Send Firmware
-- -- File> Transfer> YMODEM> Send ..........
-- -- Programming Completed Successfully!
-- -- Bytes: 304
-- Image correctly downloaded - reboot
= [SBOOT] RuntimeProtections: 0
= [SBOOT] System Security Check successfully passed. Starting...
= [FWIMG] Slot #0 @: 801f000 / Slot #1 @: 800f000 / Swap @: 802f000
======================================================================
= Firmware Modules =
= =
= Secure Boot and Secure Firmware Update =
= With Delta Patching for STM32L0 =
= =
= *** Bootloader *** =
======================================================================
Date: Sep 24 2019 18:30:09
Version: 4aeaf12-dirty
Patch Mode: Enabled
= [SBOOT] SECURE ENGINE INITIALIZATION SUCCESSFUL
= [SBOOT] STATE: CHECK STATUS ON RESET
INFO: A Reboot has been triggered by a Software reset!
Consecutive Boot on error counter reset
Consecutive Boot on error counter = 0
Consecutive Boot on error counter updated
INFO: Last execution status before Reboot was:Executing Fw Image.
INFO: Last execution detected error was:No error. Success.
= [SBOOT] STATE: CHECK NEW FIRMWARE TO DOWNLOAD
= [SBOOT] STATE: CHECK USER FW STATUS
Check MAGIC @801ef00 trailer size:512
New Clear Fw, to be re-ordered in FLASH as expected by the swap procedure
= [SBOOT] STATE: INSTALL NEW USER FIRMWARE
14768 bytes of plaintext processed.
Swapping the Firmware Images (16 blocks): ................
= [FWIMG] FW installation succeeded.
= [SBOOT] STATE: VERIFY USER FW SIGNATURE
= [SBOOT] STATE: EXECUTE USER FIRMWARE
======================================================================
= Firmware Modules =
= =
= Secure Boot and Secure Firmware Update =
= With Delta Patching for STM32L0 =
= =
= *** Demo App !!PATCHED!!*** =
= =
= Demo App v0.2 =
= FW Tag: 0x4354438E =
======================================================================
Date: Sep 24 2019 18:39:35
This update demonstration uses a UART and YMODEM protocol. It is very easy to adapt the patch download process to use a cellular or WiFi modem and any number of IoT protocols like HTTP(S) or MQTT, or just about any other LPWAN (e.g. LoRa/WAN) or PAN (e.g. BLE) binary data transfer service. The patching engine is completely automatic and quite smart - requiring only a stream of the binary patch data while automatically ensuring authenticity and knowing how many bytes it needs.
CreditsI'd like to thank ST for creating a very high quality secure boot and secure bootloader reference design called X-CUBE-SBSFU on which I based this solution. The package has detailed design documentation that describes various features of the secure boot system.
I'd also like to thank Jan Jongboom for creating "JojoDiff Alternative Patch library - portable C library for memory-efficient binary patching" on which I based the on-target firmware delta patching engine.
Last WordThis solution works on any STM32 device line, but I chose to demonstrate STM32L0 because X-CUBE-SBSFU does not support STM32L0 out of the box at this time. [Update: ST-SBSFU 2.3.0 does officially support L0 now. However my solution employs half-page writes when doing the image swap which makes the update process 16x faster!]
If you represent a startup or scale-up looking to integrate a solution like this into your project or product, contact me here on Hackster, checkout https://www.firmwaremodules.com/pages/bootloader or send a note to contact@firmwaremodules.com to learn how.
Comments