Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 1 | <!-- |
David Vincze | 4e3c47b | 2020-04-21 17:11:33 +0200 | [diff] [blame] | 2 | - |
| 3 | - Licensed to the Apache Software Foundation (ASF) under one |
| 4 | - or more contributor license agreements. See the NOTICE file |
| 5 | - distributed with this work for additional information |
| 6 | - regarding copyright ownership. The ASF licenses this file |
| 7 | - to you under the Apache License, Version 2.0 (the |
| 8 | - "License"); you may not use this file except in compliance |
| 9 | - with the License. You may obtain a copy of the License at |
| 10 | - |
| 11 | - http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | - |
| 13 | - Unless required by applicable law or agreed to in writing, |
| 14 | - software distributed under the License is distributed on an |
| 15 | - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | - KIND, either express or implied. See the License for the |
| 17 | - specific language governing permissions and limitations |
| 18 | - under the License. |
| 19 | - |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 20 | --> |
| 21 | |
Ruth Fuchss | 721cdbc | 2019-11-01 15:24:39 +0100 | [diff] [blame] | 22 | # Encrypted images |
| 23 | |
Fabio Utzig | 478ad24 | 2019-11-26 08:32:19 -0300 | [diff] [blame] | 24 | ## [Rationale](#rationale) |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 25 | |
| 26 | To provide confidentiality of image data while in transport to the |
Francesco Servidio | 4ff0c18 | 2021-10-20 15:27:16 +0200 | [diff] [blame] | 27 | device or while residing on an external flash, `MCUboot` has support |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 28 | for encrypting/decrypting images on-the-fly while upgrading. |
| 29 | |
| 30 | The image header needs to flag this image as `ENCRYPTED` (0x04) and |
| 31 | a TLV with the key must be present in the image. When upgrading the |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 32 | image from the `secondary slot` to the `primary slot` it is automatically |
| 33 | decrypted (after validation). If swap upgrades are enabled, the image |
| 34 | located in the `primary slot`, also having the `ENCRYPTED` flag set and the |
| 35 | TLV present, is re-encrypted while swapping to the `secondary slot`. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 36 | |
Fabio Utzig | 478ad24 | 2019-11-26 08:32:19 -0300 | [diff] [blame] | 37 | ## [Threat model](#threat-model) |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 38 | |
| 39 | The encrypted image support is supposed to allow for confidentiality |
| 40 | if the image is not residing on the device or is written to external |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 41 | storage, eg a SPI flash being used for the secondary slot. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 42 | |
| 43 | It does not protect against the possibility of attaching a JTAG and |
| 44 | reading the internal flash memory, or using some attack vector that |
| 45 | enables dumping the internal flash in any way. |
| 46 | |
Sam Bristow | d0ca0ff | 2019-10-30 20:51:35 +1300 | [diff] [blame] | 47 | Since decrypting requires a private key (or secret if using symmetric |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 48 | crypto) to reside inside the device, it is the responsibility of the |
| 49 | device manufacturer to guarantee that this key is already in the device |
| 50 | and not possible to extract. |
| 51 | |
Fabio Utzig | 478ad24 | 2019-11-26 08:32:19 -0300 | [diff] [blame] | 52 | ## [Design](#design) |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 53 | |
| 54 | When encrypting an image, only the payload (FW) is encrypted. The header, |
| 55 | TLVs are still sent as plain data. |
| 56 | |
| 57 | Hashing and signing also remain functionally the same way as before, |
| 58 | applied over the un-encrypted data. Validation on encrypted images, checks |
| 59 | that the encrypted flag is set and TLV data is OK, then it decrypts each |
| 60 | image block before sending the data to the hash routines. |
| 61 | |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 62 | The image is encrypted using AES-CTR-128 or AES-CTR-256, with a counter |
| 63 | that starts from zero (over the payload blocks) and increments by 1 for each |
| 64 | 16-byte block. AES-CTR was chosen for speed/simplicity and allowing for any |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 65 | block to be encrypted/decrypted without requiring knowledge of any other |
| 66 | block (allowing for simple resume operations on swap interruptions). |
| 67 | |
| 68 | The key used is a randomized when creating a new image, by `imgtool` or |
| 69 | `newt`. This key should never be reused and no checks are done for this, |
| 70 | but randomizing a 16-byte block with a TRNG should make it highly |
| 71 | improbable that duplicates ever happen. |
| 72 | |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 73 | To distribute this AES-CTR key, new TLVs were defined. The key can be |
| 74 | encrypted using either RSA-OAEP, AES-KW (128 or 256 bits depending on the |
| 75 | AES-CTR key length), ECIES-P256 or ECIES-X25519. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 76 | |
| 77 | For RSA-OAEP a new TLV with value `0x30` is added to the image, for |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 78 | AES-KW a new TLV with value `0x31` is added to the image, for |
Fabio Utzig | 5eaa576 | 2020-04-02 13:30:43 -0300 | [diff] [blame] | 79 | ECIES-P256 a new TLV with value `0x32` is added, and for ECIES-X25519 a |
| 80 | newt TLV with value `0x33` is added. The contents of those TLVs |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 81 | are the results of applying the given operations over the AES-CTR key. |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 82 | |
Fabio Utzig | 5eaa576 | 2020-04-02 13:30:43 -0300 | [diff] [blame] | 83 | ## [ECIES encryption](#ecies-encryption) |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 84 | |
| 85 | ECIES follows a well defined protocol to generate an encryption key. There are |
| 86 | multiple standards which differ only on which building blocks are used; for |
Francesco Servidio | 4ff0c18 | 2021-10-20 15:27:16 +0200 | [diff] [blame] | 87 | MCUboot we settled on some primitives that are easily found on our crypto |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 88 | libraries. The whole key encryption can be summarized as: |
| 89 | |
Fabio Utzig | 5eaa576 | 2020-04-02 13:30:43 -0300 | [diff] [blame] | 90 | * Generate a new private key and derive the public key; when using ECIES-P256 |
| 91 | this is a secp256r1 keypair, when using ECIES-X25519 this will be a x25519 |
| 92 | keypair. Those keys will be our ephemeral keys. |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 93 | * Generate a new secret (DH) using the ephemeral private key and the public key |
| 94 | that corresponds to the private key embedded in the HW. |
| 95 | * Derive the new keys from the secret using HKDF (built on HMAC-SHA256). We |
| 96 | are not using a `salt` and using an `info` of `MCUBoot_ECIES_v1`, generating |
| 97 | 48 bytes of key material. |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 98 | * A new random encryption key is generated (for AES). This is |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 99 | the AES key used to encrypt the images. |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 100 | * The key is encrypted with AES-128-CTR or AES-256-CTR and a `nonce` of 0 using |
| 101 | the first 16 bytes of key material generated previously by the HKDF. |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 102 | * The encrypted key now goes through a HMAC-SHA256 using the remaining 32 |
| 103 | bytes of key material from the HKDF. |
| 104 | |
Fabio Utzig | 5eaa576 | 2020-04-02 13:30:43 -0300 | [diff] [blame] | 105 | The final TLV is built from the 65 bytes for ECIES-P256 or 32 bytes for |
| 106 | ECIES-X25519, which correspond to the ephemeral public key, followed by the |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 107 | 32 bytes of MAC tag and the 16 or 32 bytes of the encrypted key, resulting in |
| 108 | a TLV of 113 or 129 bytes for ECIES-P256 and 80 or 96 bytes for ECIES-X25519. |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 109 | |
Fabio Utzig | 5eaa576 | 2020-04-02 13:30:43 -0300 | [diff] [blame] | 110 | The implemenation of ECIES-P256 is named ENC_EC256 in the source code and |
| 111 | artifacts while ECIES-X25519 is named ENC_X25519. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 112 | |
Fabio Utzig | 478ad24 | 2019-11-26 08:32:19 -0300 | [diff] [blame] | 113 | ## [Upgrade process](#upgrade-process) |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 114 | |
Francesco Servidio | 4ff0c18 | 2021-10-20 15:27:16 +0200 | [diff] [blame] | 115 | When starting a new upgrade process, `MCUboot` checks that the image in the |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 116 | `secondary slot` has the `ENCRYPTED` flag set and has the required TLV with the |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 117 | encrypted key. It then uses its internal private/secret key to decrypt |
| 118 | the TLV containing the key. Given that no errors are found, it will then |
| 119 | start the validation process, decrypting the blocks before check. A good |
| 120 | image being determined, the upgrade consists in reading the blocks from |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 121 | the `secondary slot`, decrypting and writing to the `primary slot`. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 122 | |
Thomas Altenbach | 44a935e | 2024-04-30 17:19:55 +0200 | [diff] [blame^] | 123 | If swap using scratch is used for the upgrade process, the decryption happens |
| 124 | when copying the content of the scratch area to the `primary slot`, which means |
| 125 | the scratch area does not contain the image unencrypted. However, unless |
| 126 | `MCUBOOT_SWAP_SAVE_ENCTLV` is enabled, the decryption keys are stored in |
| 127 | plaintext in the scratch area. Therefore, `MCUBOOT_SWAP_SAVE_ENCTLV` must be |
| 128 | enabled if the scratch area does not reside in the internal flash memory of the |
| 129 | MCU, to avoid attacks that could interrupt the upgrade and read the plaintext |
| 130 | decryption keys from external flash memory. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 131 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 132 | Also when swap is used, the image in the `primary slot` is checked for |
| 133 | presence of the `ENCRYPTED` flag and the key TLV. If those are present the |
| 134 | sectors are re-encrypted when copying from the `primary slot` to |
| 135 | the `secondary slot`. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 136 | |
Francesco Servidio | 2fe449d | 2021-10-21 12:38:36 +0200 | [diff] [blame] | 137 | --- |
| 138 | ***Note*** |
| 139 | |
| 140 | *Each encrypted image must have its own key TLV that should be unique* |
| 141 | *and used only for this particular image.* |
| 142 | |
| 143 | --- |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 144 | |
| 145 | Also when swap method is employed, the sizes of both images are saved to |
| 146 | the status area just before starting the upgrade process, because it |
| 147 | would be very hard to determine this information when an interruption |
| 148 | occurs and the information is spread across multiple areas. |
| 149 | |
Fabio Utzig | 478ad24 | 2019-11-26 08:32:19 -0300 | [diff] [blame] | 150 | ## [Creating your keys with imgtool](#creating-your-keys-with-imgtool) |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 151 | |
Harry Jiang | 6828151 | 2020-07-06 16:12:02 +0800 | [diff] [blame] | 152 | `imgtool` can generate keys by using `imgtool keygen -k <output.pem> -t <type>`, |
Roland Mikhel | 3d92a6c | 2023-02-23 15:35:44 +0100 | [diff] [blame] | 153 | where type can be one of `rsa-2048`, `rsa-3072`, `ecdsa-p256` |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 154 | or `ed25519`. This will generate a keypair or private key. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 155 | |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 156 | To extract the public key in source file form, use |
Fabio Utzig | 4e2cdfe | 2022-09-28 17:44:01 -0300 | [diff] [blame] | 157 | `imgtool getpub -k <input.pem> -e <encoding>`, where `encoding` can be one of |
| 158 | `lang-c` or `lang-rust` (defaults to `lang-c`). To extract a public key in PEM |
| 159 | format, use `imgtool getpub -k <input.pem> -e pem`. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 160 | |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 161 | If using AES-KW, follow the steps in the next section to generate the |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 162 | required keys. |
| 163 | |
Fabio Utzig | 478ad24 | 2019-11-26 08:32:19 -0300 | [diff] [blame] | 164 | ## [Creating your keys with Unix tooling](#creating-your-keys-with-unix-tooling) |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 165 | |
| 166 | * If using RSA-OAEP, generate a keypair following steps similar to those |
| 167 | described in [signed_images](signed_images.md) to create RSA keys. |
| 168 | * If using ECIES-P256, generate a keypair following steps similar to those |
| 169 | described in [signed_images](signed_images.md) to create ECDSA256 keys. |
Fabio Utzig | 5eaa576 | 2020-04-02 13:30:43 -0300 | [diff] [blame] | 170 | * If using ECIES-X25519, generate a private key passing the option `-t x25519` |
| 171 | to `imgtool keygen` command. To generate public key PEM file the following |
| 172 | command can be used: `openssl pkey -in <generated-private-key.pem> -pubout` |
Salome Thirot | 0f64197 | 2021-05-14 11:19:55 +0100 | [diff] [blame] | 173 | * If using AES-KW (`newt` only), the `kek` can be generated with a |
| 174 | command like (change count to 32 for a 256 bit key) |
| 175 | `dd if=/dev/urandom bs=1 count=16 | base64 > my_kek.b64` |