Skip to content

Commit 7419544

Browse files
authored
Merge pull request #2471 from arduino/mcmchris/portenta-h7/secure-boot-revamp
[PXCT-833] Portenta H7 Secure Boot Tutorial update
2 parents c424a3b + fc1fb44 commit 7419544

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
lines changed
668 KB
Loading
1.28 MB
Loading
227 KB
Loading
1.35 MB
Loading

content/hardware/04.pro/boards/portenta-h7/tutorials/secure-boot/secure-boot.md

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,138 @@ description: 'Learn how to use secure boot on the Arduino Portenta H7.'
44
difficulty: beginner
55
tags:
66
- Secure Boot
7-
author: 'Umberto Baldi'
7+
author: 'Umberto Baldi & Christopher Méndez'
88
hardware:
99
- hardware/04.pro/boards/portenta-h7
1010

1111
software:
12-
- ide-v1
1312
- ide-v2
1413
- cli
1514
---
1615
## Introduction
17-
This short tutorial will guide the user through enabling the secure boot on the Portenta H7, generating custom security keys, and using them with the MCUboot bootloader.
16+
This short tutorial will guide the user through enabling the secure boot on the Portenta H7, generating custom security keys and using them with the MCUboot bootloader.
1817

1918
Secure boot is the process where a compiled sketch is authenticated against the hardware before it is authorized to be used in the boot process. The hardware is pre-configured to authenticate code using trusted security credentials.
2019

2120
In other words, secure boot ensures that the boot technology and operating system software are the legitimate manufacturer version and have not been altered or tampered with by any malicious actor or process.
2221

2322
## Hardware & Software Required
24-
- [Portenta H7](https://store.arduino.cc/portenta-h7)
25-
- Arduino IDE 1.8.19+ or Arduino IDE 2.0-rc5+ (https://www.arduino.cc/en/software)
26-
- [Arduino Core for mbed enabled devices](https://github.com/arduino/ArduinoCore-mbed) version 3.1.0+
27-
- [imgtool](https://github.com/arduino/imgtool-packing/releases/latest) (optional)
23+
24+
- [Portenta H7](https://store.arduino.cc/portenta-h7)
25+
- [Arduino IDE 2.0+](https://www.arduino.cc/en/software)
26+
- [Arduino Core for mbed enabled devices](https://github.com/arduino/ArduinoCore-mbed) version 3.1.0+
27+
- [imgtool](https://github.com/arduino/imgtool-packing/releases/latest) (optional)
2828

2929
## Instructions
3030

3131
### Flashing the Latest Bootloader
32-
In order to have secure boot enabled, you must update the bootloader on your Portenta H7 and use [MCUboot](https://www.mcuboot.com/). You can find more info on how to perform the update in [this other tutorial](https://docs.arduino.cc/tutorials/portenta-h7/updating-the-bootloader).
32+
33+
In order to enable the secure boot, you must **update** the bootloader on your Portenta H7. You can find more info on how to perform the update in [this other tutorial](https://docs.arduino.cc/tutorials/portenta-h7/updating-the-bootloader).
34+
35+
Once the bootloader has been updated, it is possible to use [secure boot](https://www.keyfactor.com/blog/what-is-secure-boot-its-where-iot-security-starts/) to have an additional layer of security.
36+
37+
### Format the QSPI Flash
38+
39+
First, before enabling the secure boot feature on your Portenta H7, you might need to format the QSPI flash memory.
40+
41+
For this, navigate to **File > Examples > STM32H747_System > QSPIFormat** and follow the steps below:
42+
43+
![QSPIFormat example code](assets/qspi-format.png)
44+
45+
- Upload the **QSPIFormat** example to your Portenta H7.
46+
- Open the Arduino IDE Serial Monitor.
47+
- Follow the steps shown in the Serial Monitor.
48+
49+
You will be asked for:
50+
51+
- Permission to proceed and **erase** the QSPI flash. Type `Y` in the Serial Monitor and press enter.
52+
- Once done, you will be asked to **restore** the WiFi firmware. Type `Y` in the Serial Monitor and press enter.
53+
- Select between **LitteFS** or **FatFS** to format user partition. Type `Y` for LittleFS or `No` for FatFS.
54+
- Wait for the process to be done. It should say something like "It is now secure to reboot or disconnect the board".
55+
56+
Now you are ready to jump to the following step to enable the secure boot.
3357

3458
### Use Default Security Keys
35-
Once the bootloader has been updated to MCUboot, it is possible to use [secure boot](https://www.keyfactor.com/blog/what-is-secure-boot-its-where-iot-security-starts/) to have an additional layer of security. From that point on, it is required to upload a compiled sketch with the Custom Board Option **"Security settings"** set to **"Signature + Encryption"** (the option can be found under **Tools > Security settings** in the IDE when selecting Portenta H7 as board or you can use `--board-options security=sien` if using the Arduino CLI). Failing to provide such option will cause the bootloader not to run the compiled sketch because it is not trusted.
3659

37-
If the security keys are not overridden, the default ones are used.
38-
Two keys are embedded in the example sketch `STM32H747_manageBootloader`, which can be found in **Files > Examples > STM32H747_System > STM32H747_manageBootloader** and used by the bootloader.
39-
A private 256bit [ECDSA](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm) key is used to extract the encryption key and decrypt the binary update (`ecdsa-p256-encrypt-priv-key.h`), while a public key is used for image verification (`ecdsa-p256-signing-pub-key.h`).
60+
Upload the `enableSecurity` example sketch that can be found under **File > Examples > MCUboot** in the IDE upper menu.
61+
62+
![enableSecurity sketch](assets/sketch.png)
63+
64+
Two keys are embedded in the example sketch:
65+
66+
- A private 256bit [ECDSA](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm) key is used to extract the encryption key and decrypt the binary update (`ecdsa-p256-encrypt-key.h`).
67+
- A public key is used for image verification (`ecdsa-p256-signing-key.h`).
68+
69+
***If the security keys are not overridden, the default ones are used. The default keys are in `C:\Users\<user>\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\<x.x.x>\libraries\MCUboot\default_keys.`***
4070

41-
As counterpart, when building the image update, imgtool uses this private [key](https://github.com/arduino/ArduinoCore-mbed/pull/447/files#diff-f43e4850d60c61854678f6f80c6ddc4b59e3e68ca7e71b02e5ed15288c9aadb4) to sign the image and this public [key](https://github.com/arduino/ArduinoCore-mbed/pull/447/files#diff-95bb7b27de14276896a2bec099dc5a498d5332616458c04263efc8d24810e6a6) for image encryption with elliptic curve integrated encryption scheme.
71+
In the *Serial Monitor* you will be asked for confirmation to load the default keys. Enter `Y` to confirm, `n` to abort.
72+
73+
![Confirmation to load default keys](assets/load.png)
74+
75+
Finally, wait for the keys to be loaded. In the *Serial Monitor* you will see a message saying: *Security features enabled. It's now safe to reboot or disconnect your board.*
76+
77+
From that point on, you must configure your future sketches **Security setting** to **Signature + Encryption** (the option can be found under **Tools > Security settings** in the IDE when selecting *Portenta H7* as board, or use `--board-options security=sien` if working with the Arduino CLI).
78+
79+
![Security setting on Portenta H7](assets/setting.png)
80+
81+
***Not configuring this setting will cause the bootloader not to run the compiled sketch because it is not trusted.***
82+
83+
### Generate Custom Security Keys
4284

43-
### 1. Generate Custom Security Keys
4485
The default keys provided with the mbed platform are obviously only intended for development purposes. In a production environment it is strongly recommended to generate a new key pair (public and private key).
4586
This can be done with **imgtool**. You can download and install it directly from the [release section](https://github.com/arduino/imgtool-packing/releases/latest).
4687

47-
***`imgtool` is already installed by the mbed platform and can be found in the `%LOCALAPPDATA%\Arduino15\packages\arduino\tools\imgtool` directory on Windows, in `~/.arduino15/packages/arduino/tools/imgtool` on Linux and in `~/Library/Arduino15/packages/arduino/tools/imgtool` on macOS.***
88+
***`imgtool` is already installed by the mbed platform.***
89+
90+
The tool can be found in:
4891

49-
To generate the new keys you can use this command line:
92+
- `%LOCALAPPDATA%\Arduino15\packages\arduino\tools\imgtool` on Windows.
93+
- `~/.arduino15/packages/arduino/tools/imgtool` on Linux.
94+
- `~/Library/Arduino15/packages/arduino/tools/imgtool` on macOS.
95+
96+
To **generate** the new keys you can use this command line:
5097

5198
```bash
5299
imgtool keygen --key my-sign-keyfile.pem -t ecdsa-p256
53100
imgtool keygen --key my-encrypt-keyfile.pem -t ecdsa-p256
54101
```
55102
This command line will generate two private PEM encoded security keys and save them in the current directory with `my-sign-keyfile.pem` and `my-encrypt-keyfile.pem` names. The algorithm used to generate the keys is ECDSA 256bit.
56103

57-
Remember to **save the keys and keep them in a secure location** and not to lose them.
104+
***Remember to __save the keys and keep them in a secure location__ and not to lose them.***
58105

59-
### 2. Upload the Custom Keys to the Board
106+
### Upload the Custom Keys to the Board
60107
Once the keys have been generated, they have to be uploaded to the Portenta H7. This procedure has to be done only once, because it is persistent. To extract the public\private key and encode it in to a "C" byte array inside a `.h` header file you can use:
61108

62109
```bash
63110
imgtool getpriv -k my-encrypt-keyfile.pem > ecsda-p256-encrypt-priv-key.h
64111
imgtool getpub -k my-sign-keyfile.pem > ecsda-p256-signing-pub-key.h
65112
```
66113

67-
Now you have to replace the keys inside the Sketch to update the bootloader(**STM32H747_manageBootloader**).
68-
To do so, just save the sketch to another location and replace the `ecsda-p256-encrypt-priv-key.h` and `ecsda-p256-signing-pub-key.h` files with the newly generated ones and then [update the bootloader](https://docs.arduino.cc/tutorials/portenta-h7/updating-the-bootloader) again.
114+
Now you have to replace the keys inside the `enableSecurity` sketch found under **File > Examples > MCUboot** in the IDE upper menu.
115+
116+
To do so, just save the sketch to another location and replace the `ecsda-p256-encrypt-priv-key.h` and `ecsda-p256-signing-pub-key.h` files with the newly generated ones and then upload the sketch again.
69117

70-
***NOTE: In case the keys are compromised, this process can be performed again with a new set of keys, but any firmware signed with the previous pair will no longer work.***
118+
***__NOTE:__ In case the keys are compromised, this process can be performed again with a new set of keys, but any firmware signed with the previous pair will no longer work.***
71119

72-
### 3. Use the Custom Keys when Compiling
120+
### Use the Custom Keys when Compiling
73121
Since the default keys have been changed in favour of custom generated ones, the new ones have to be used when compiling and uploading a sketch, because the compiled sketch is signed and encrypted using such keys.
74122

75-
To override the security keys used during the compile, you have to use the Arduino CLI and specify the keys with:
123+
To override the security keys used during the compilation, we recommend using the **Arduino CLI** and specify the keys with:
124+
125+
```bash
126+
arduino-cli compile -b arduino:mbed_portenta:envie_m7 --board-options security=sien --keys-keychain <path-to-your-keys> --sign-key ecdsa-p256-signing-priv-key.pem --encrypt-key ecdsa-p256-encrypt-pub-key.pem <directory-of-sketch-to-compile>
127+
```
128+
129+
You can also navigate to your `mbed_portenta` core found in:
130+
131+
- `%LOCALAPPDATA%\Arduino15\packages\arduino\hardware\mbed_portenta\x.x.x\boards.txt` (this may vary depending on the operating system).
132+
- Then, change the default location your IDE search for the security keys by updating these lines inside the `boards.txt` file:
76133

77134
```bash
78-
arduino-cli compile -b arduino:mbed_portenta:envie_m7 --board-options security=sien --keys-keychain <path-to-your-keys> --sign-key ecdsa-p256-signing-priv-key.pem --encrypt-key ecdsa-p256-encrypt-pub-key.pem /home/user/Arduino/MySketch
135+
envie_m7.menu.security.sien.build.keys.keychain={runtime.platform.path}/libraries/MCUboot/default_keys
136+
envie_m7.menu.security.sien.build.keys.sign_key=ecdsa-p256-signing-priv-key.pem
137+
envie_m7.menu.security.sien.build.keys.encrypt_key=ecdsa-p256-encrypt-pub-key.pem
79138
```
80139

81140
## Learn More
82-
If you want to implement secure boot for your platform, have a look at [this article](https://arduino.github.io/arduino-cli/latest/guides/secure-boot/).
141+
If you want to implement secure boot for your platform, take a look at [this article](https://arduino.github.io/arduino-cli/latest/guides/secure-boot/).

0 commit comments

Comments
 (0)