Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 1 | <!-- |
| 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 | # |
| 20 | --> |
| 21 | |
| 22 | ## Rationale |
| 23 | |
| 24 | To provide confidentiality of image data while in transport to the |
| 25 | device or while residing on an external flash, `MCUBoot` has support |
| 26 | for encrypting/decrypting images on-the-fly while upgrading. |
| 27 | |
| 28 | The image header needs to flag this image as `ENCRYPTED` (0x04) and |
| 29 | 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^] | 30 | image from the `secondary slot` to the `primary slot` it is automatically |
| 31 | decrypted (after validation). If swap upgrades are enabled, the image |
| 32 | located in the `primary slot`, also having the `ENCRYPTED` flag set and the |
| 33 | TLV present, is re-encrypted while swapping to the `secondary slot`. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 34 | |
| 35 | ## Threat model |
| 36 | |
| 37 | The encrypted image support is supposed to allow for confidentiality |
| 38 | 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^] | 39 | storage, eg a SPI flash being used for the secondary slot. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 40 | |
| 41 | It does not protect against the possibility of attaching a JTAG and |
| 42 | reading the internal flash memory, or using some attack vector that |
| 43 | enables dumping the internal flash in any way. |
| 44 | |
| 45 | Since decrypting requires a private key (or secret if using symetric |
| 46 | crypto) to reside inside the device, it is the responsibility of the |
| 47 | device manufacturer to guarantee that this key is already in the device |
| 48 | and not possible to extract. |
| 49 | |
| 50 | ## Design |
| 51 | |
| 52 | When encrypting an image, only the payload (FW) is encrypted. The header, |
| 53 | TLVs are still sent as plain data. |
| 54 | |
| 55 | Hashing and signing also remain functionally the same way as before, |
| 56 | applied over the un-encrypted data. Validation on encrypted images, checks |
| 57 | that the encrypted flag is set and TLV data is OK, then it decrypts each |
| 58 | image block before sending the data to the hash routines. |
| 59 | |
| 60 | The image is encrypted using AES-CTR-128, with a counter that starts |
| 61 | from zero (over the payload blocks) and increments by 1 for each 16-byte |
| 62 | block. AES-CTR-128 was chosen for speed/simplicity and allowing for any |
| 63 | block to be encrypted/decrypted without requiring knowledge of any other |
| 64 | block (allowing for simple resume operations on swap interruptions). |
| 65 | |
| 66 | The key used is a randomized when creating a new image, by `imgtool` or |
| 67 | `newt`. This key should never be reused and no checks are done for this, |
| 68 | but randomizing a 16-byte block with a TRNG should make it highly |
| 69 | improbable that duplicates ever happen. |
| 70 | |
| 71 | To distribute this AES-CTR-128 key, new TLVs were defined. The key can be |
| 72 | encrypted using either RSA-OAEP or AES-KW-128. Also in the future support |
| 73 | for EICES (using EC) can be added. |
| 74 | |
| 75 | For RSA-OAEP a new TLV with value `0x30` is added to the image, for |
| 76 | AES-KW-128 a new TLV with value `0x31` is added to the image. The contents |
| 77 | of both TLVs are the results of applying the given operations over the |
| 78 | AES-CTR-128 key. |
| 79 | |
| 80 | ## Upgrade process |
| 81 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 82 | When starting a new upgrade process, `MCUBoot` checks that the image in the |
| 83 | `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] | 84 | encrypted key. It then uses its internal private/secret key to decrypt |
| 85 | the TLV containing the key. Given that no errors are found, it will then |
| 86 | start the validation process, decrypting the blocks before check. A good |
| 87 | image being determined, the upgrade consists in reading the blocks from |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 88 | the `secondary slot`, decrypting and writing to the `primary slot`. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 89 | |
| 90 | If swap is used for the upgrade process, the encryption happens when |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 91 | copying the sectors of the `secondary slot` to the scratch area. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 92 | |
| 93 | The `scratch` area is not encrypted, so it must reside in the internal |
| 94 | flash of the MCU to avoid attacks that could interrupt the upgrade and |
| 95 | dump the data. |
| 96 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame^] | 97 | Also when swap is used, the image in the `primary slot` is checked for |
| 98 | presence of the `ENCRYPTED` flag and the key TLV. If those are present the |
| 99 | sectors are re-encrypted when copying from the `primary slot` to |
| 100 | the `secondary slot`. |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 101 | |
| 102 | PS: Each encrypted image must have its own key TLV that should be unique |
| 103 | and used only for this particular image. |
| 104 | |
| 105 | Also when swap method is employed, the sizes of both images are saved to |
| 106 | the status area just before starting the upgrade process, because it |
| 107 | would be very hard to determine this information when an interruption |
| 108 | occurs and the information is spread across multiple areas. |
| 109 | |
| 110 | ## Creating your keys |
| 111 | |
| 112 | <!-- |
| 113 | TODO: expand this section or add specific docs to imgtool, newt... |
| 114 | |
| 115 | XXX: add current key access method (reverse direction from sign) |
| 116 | --> |
| 117 | |
| 118 | * If using RSA-OAEP, generating a keypair follows steps similar to those |
| 119 | described in [signed_images](signed_images.md) |
| 120 | * If using AES-KW-128 (`newt` only), the `kek` can be generated with a |
| 121 | command like `dd if=/dev/urandom bs=1 count=16 | base64 > my_kek.b64` |