blob: 434a3891cbb62ca5aea7fa225dab333074e52ac6 [file] [log] [blame]
Raef Coles40ff13f2022-02-24 14:33:15 +00001########################
2BL1 Immutable bootloader
3########################
4
5:Author: Raef Coles
6:Organization: Arm Limited
7:Contact: raef.coles@arm.com
8
9************
10Introduction
11************
12
13Some devices that use TF-M will require initial boot code that is stored in ROM.
14There are a variety of reasons that this might happen:
15
16- The device cannot access flash memory without a driver, so needs some setup
17 to be done before main images on flash can be booted.
18- The device has no on-chip secure flash, and therefore cannot otherwise
19 maintain a tamper-resistant root of trust.
20- The device has a security model that requires an immutable root of trust
21
22Henceforth any bootloader stored in ROM will be referred to as BL1, as it would
23necessarily be the first stage in the boot chain.
24
25TF-M provides a reference second-stage flash bootloader BL2, in order to allow
26easier integration. This bootloader implements all secure boot functionality
27needed to provide a secure chain of trust.
28
29A reference ROM bootloader BL1 has now being added with the same motivation -
30allowing easier integration of TF-M for platforms that do not have their own
31BL1 and require one.
32
33****************************
34BL1 Features and Motivations
35****************************
36
37The reference ROM bootloader provides the following features:
38
39- A split between code being stored in ROM and in other non-volatile memory.
40
41 - This can allow significant cost reduction in fixing bugs compared to
42 ROM-only bootloaders.
43
44- A secure boot mechanism that allows upgrading the next boot stage (which
45 would usually be BL2).
46
47 - This allows for the fixing of any bugs in the BL2 image.
48 - Alternately, this could allow the removal of BL2 in some devices that are
49 constrained in flash space but have ROM.
50
51- A post-quantum resistant asymmetric signature scheme for verifying the next
52 boot stage image.
53
54 - This can allow devices to be securely updated even if attacks
55 involving quantum computers become viable. This could extend the lifespans
56 of devices that might be deployed in the field for many years.
57
58- A mechanism for passing boot measurements to the TF-M runtime so that they
59 can be attested.
60- Tooling to create and sign images.
61- Fault Injection (FI) and Differential Power Analysis (DPA) mitigations.
62
63*********************************
64BL1_1 and BL1_2 split bootloaders
65*********************************
66
67BL1 is split into two distinct boot stages, BL1_1 which is stored in ROM and
68BL1_2 which is stored in other non-volatile storage. This would usually be
69either trusted or untrusted flash, but on platforms without flash memory can be
70OTP. As BL1_2 is verified against a hash stored in OTP, it is immutable after
71provisioning even if stored in mutable storage.
72
73Bugs in ROM bootloaders usually cannot be fixed once a device is provisioned /
74in the field, as ROM code is immutable the only option is fixing the bug in
75newly manufactured devices.
76
77However, it can be very expensive to change the ROM code of devices once
78manufacturing has begun, as it requires changes to the photolithography masks
79that are used to create the device. This cost varies depending on the complexity
80of the device and of the process node that it is being fabricated on, but can be
81large, both in engineering time and material/process costs.
82
83By placing the majority of the immutable bootloader in other storage, we can
84mitigate the costs associated with changing ROM code, as a new BL1_2 image can
85be used at provisioning time with minimal changeover cost. BL1_1 contains a
86minimal codebase responsible mainly for the verification of the BL1_2 image.
87
88The bootflow is as follows. For simplicity this assumes that the boot stage
89after BL1 is BL2, though this is not necessarily the case:
90
911) BL1_1 begins executing in place from ROM
922) BL1_1 copies BL1_2 into RAM
933) BL1_1 verifies BL1_2 against the hash stored in OTP
944) BL1_1 jumps to BL1_2, if the hash verification has succeeded
955) BL1_2 copies the primary BL2 image from flash into RAM
966) BL1_2 verifies the BL2 image using asymmetric cryptography
977) If verification fails, BL1_2 repeats 5 and 6 with the secondary BL2 image
988) BL1_2 jumps to BL2, if either image has successfully verified
99
100.. Note::
101 The BL1_2 image is not encrypted, so if it is placed in untrusted flash it
102 will be possible to read the data in the image.
103
104Some optimizations have been made specifically for the case where BL1_2 has been
105stored in OTP:
106
107OTP can be very expensive in terms of chip area, though new technologies like
108antifuse OTP decrease this cost. Because of this, the code size of BL1_2 has
109been minimized. Code-sharing has been configured so that BL1_2 can call
110functions stored in ROM. Care should be taken that OTP is sized such that it is
111possible to include versions of the functions used via code-sharing, in case the
112ROM functions contain bugs, though less space is needed than if all code is
113duplicated as it is assumed that most functions will not contain bugs.
114
115As OTP memory frequently has low performance, BL1_2 is copied into RAM before it
116it is executed. It also copies the next image stage into RAM before
117authenticating it, which allows the next stage to be stored in untrusted flash.
118This requires that the device have sufficient RAM to contain both the BL1_2
119image and the next stage image at the same time. Note that this is done even if
120BL1_2 is located in XIP-capable flash, as it both allows the use of untrusted
121flash and simplifies the image upgrade logic.
122
123.. Note::
124 BL1_2 enables TF-M to be used on devices that contain no secure flash, though
125 the ITS service will not be available. Other services that depend on ITS will
126 not be available without modification.
127
128*************************************
129Secure boot / Image upgrade mechanism
130*************************************
131
132BL1_2 verifies the authenticity of the next stage image via asymmetric
133cryptography, using a public key that is provisioned into OTP.
134
135BL1_2 implements a rollback protection counter in OTP, which is used to prevent
136the next stage image being downgraded to a less secure version.
137
138BL1_2 has two image slots, which allows image upgrades to be performed. The
139primary slot is always booted first, and then if verification of this fails
140(either due to an invalid signature or due to a version lower than the rollback
141protection counter) the secondary slot is then booted (subject to the same
142checks).
143
144BL1_2 contains no image upgrade logic, in order for OTA of the next stage image
145to be implemented, a later stage in the system must handle downloading new
146images and placing them in the required slot.
147
148********************************************
149Post-Quantum signature verification in BL1_2
150********************************************
151
152BL1_2 uses a post-quantum asymmetric signature scheme to verify the next stage.
153The scheme used is Leighton-Michaeli Signatures (henceforth LMS). LMS is
154standardised in `NIST SP800-208
155<https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf>`_
156and `IETF RFC8554. <https://datatracker.ietf.org/doc/html/rfc8554>`_
157
158LMS is a stateful-hash signature scheme, meaning that:
159
160 1) It is constructed from a cryptographic hash function, in this case SHA256.
161
162 - This function can be accelerated by existing hardware accelerators, which
163 can make LMS verification relatively fast compared to other post-quantum
164 signature schemes that cannot be accelerated in hardware yet.
165
166 2) Each private key can only be used to sign a certain number of images.
167
168 - BL1_2 uses the SHA256_H10 parameter set, meaning each key can sign 1024
169 images.
170
171The main downside, the limited amount of possible signatures, can be mitigated
172by limiting the amount of image upgrades that are done. As BL2 is often
173currently not upgradable, it is not anticipated that this limit will be
174problematic. If BL1 is being used to directly boot a TF-M/NS combined image, the
175limit is more likely to be problematic, and care should be taken to examine the
176likely update amount.
177
178LMS public keys are 32 bytes in size, and LMS signatures are 1912 bytes in size.
179The signature size is larger than some asymmetric schemes, though most devices
180should have enough space in flash to accommodate this.
181
182The main upside of LMS, aside from the security against attacks involving
183quantum computers, is that it is relatively simple to implement. The software
184implementation that is used by BL1 is ~3KiB in size, which is considerably
185smaller than the corresponding RSA implementation which is at least 6.5K. This
186simplicity of implementation is useful to avoid bugs.
187
188BL1 will use MbedTLS as the source for its implementation of LMS.
189
190.. Note::
191 As of the time of writing, the LMS code is still in the process of being
192 merged into MbedTLS, so BL1 currently does not support asymmetric
193 verification of the next boot stage. Currently, the next boot stage is
194 hash-locked, so cannot be upgraded.
195
196 The Github pull request for LMS can be found `here
197 <https://github.com/ARMmbed/mbedtls/pull/4826>`_
198
199*********************
200BL1 boot measurements
201*********************
202
203BL1 outputs boot measurements in the same format as BL2, utilising the same
204shared memory area. These measurements can then be included in the attestation
205token, allowing the attestation of the version of the boot stage after BL1.
206
207***********
208BL1 tooling
209***********
210
211Image signing scripts are provided for BL1_1 and BL1_2. While the script is
212named ``create_bl2_img.py``, it can be used for any next stage image.
213
214- ``bl1/bl1_1/scripts/create_bl1_2_img.py``
215- ``bl1/bl1_2/scripts/create_bl2_img.py``
216
217These sign (and encrypt in the case of ``create_bl2_img.py``) a given image file
218and append the required headers.
219
220**************************
221BL1 FI and DPA mitigations
222**************************
223
224BL1 reuses the FI countermeasures used in the TF-M runtime, which are found in
225``lib/fih/``.
226
Raef Colesa5d031b2023-07-04 10:45:33 +0100227BL1 implements countermeasures against fault injection. The functions with these
228countermeasures are found in ``bl1/bl1_1/shared_lib/util.c``
Raef Coles40ff13f2022-02-24 14:33:15 +0000229
Raef Colesa5d031b2023-07-04 10:45:33 +0100230``bl_fih_memeql`` tests if memory regions have the same value
Raef Coles40ff13f2022-02-24 14:33:15 +0000231
Raef Colesa5d031b2023-07-04 10:45:33 +0100232- It inserts random delays to improve resilience to FIH attacks
Raef Coles40ff13f2022-02-24 14:33:15 +0000233- It performs loop integrity checks
234- It uses FIH constructs
235
236**************************
237Using BL1 on new platforms
238**************************
239
240New platforms must define the following macros in their ``region_defs.h``:
241
242- ``BL1_1_HEAP_SIZE``
243- ``BL1_1_STACK_SIZE``
244- ``BL1_2_HEAP_SIZE``
245- ``BL1_2_STACK_SIZE``
246- ``BL1_1_CODE_START``
247- ``BL1_1_CODE_LIMIT``
248- ``BL1_1_CODE_SIZE``
249- ``BL1_2_CODE_START``
250- ``BL1_2_CODE_LIMIT``
251- ``BL1_2_CODE_SIZE``
252- ``PROVISIONING_DATA_START``
253- ``PROVISIONING_DATA_LIMIT``
254- ``PROVISIONING_DATA_SIZE``
255
256The ``PROVISIONING_DATA_*`` defines are used to locate where the data to be
257provisioned into OTP can be found. These are required as the provisioning bundle
258needs to contain the entire BL1_2 image, usually >= 8KiB in size, which is too
259large to be placed in the static data area as is done for all other dummy
260provisioning data. On development platforms with reprogrammable ROM, this is
261often placed in unused ROM. On production platforms, this should be located in
262RAM and then filled with provisioning data. The format of the provisioning data
263that should be located in the ``PROVISIONING_DATA_*`` region can be found in
264``bl1/bl1_1/lib/provisioning.c`` in the struct
265``bl1_assembly_and_test_provisioning_data_t``
266
267If the platform is storing BL1_2 in flash, it must set
268``BL1_2_IMAGE_FLASH_OFFSET`` to the flash offset of the start of BL1_2.
269
270The platform must also implement the HAL functions defined in the following
271headers:
272
273- ``bl1/bl1_1/shared_lib/interface/trng.h``
274- ``bl1/bl1_1/shared_lib/interface/crypto.h``
275- ``bl1/bl1_1/shared_lib/interface/otp.h``
276
277If the platform integrates a CryptoCell-312, then it can reuse the existing
278implementation.
279
280***********
281BL1 Testing
282***********
283
284New tests have been written to test both the HAL implementation, and the
285integration of those functions for verifying images. These tests are stored in
286the ``tf-m-tests`` repository, under the ``test/bl1/`` directory, and further
287subdivided into BL1_1 and BL1_2 tests.
288
289--------------
290
Raef Colesa5d031b2023-07-04 10:45:33 +0100291*Copyright (c) 2022-2023, Arm Limited. All rights reserved.*