|
| 1 | +## Encrypted external partitions |
| 2 | + |
| 3 | +wolfBoot offers the possibility to encrypt the content of the entire UPDATE partition, |
| 4 | +by using a pre-shared symmetric key which can be temporarily stored in a safer non-volatile memory area. |
| 5 | + |
| 6 | +SWAP partition is also temporarily encrypted using the same key, so a dump of the external flash won't reveal |
| 7 | +any content of the firmware update packages. |
| 8 | + |
| 9 | +### Rationale |
| 10 | + |
| 11 | +Encryption of external partition works at the level of the external flash interface. |
| 12 | + |
| 13 | +All write calls to external partitions from the bootloader perform an additional encryption step |
| 14 | +to hide the actual content of the external non-volatile memory. |
| 15 | + |
| 16 | +Viceversa, all read operations will decrypt the data stored when the feature is enabled. |
| 17 | + |
| 18 | +An extra option is provided to the `sign.py` sign tool to encrypt the firmware update after signing it, so |
| 19 | +that it can be stored as is in the external memory by the application, and will be decrypted by the bootloader |
| 20 | +in order to verify the update and begin the installation. |
| 21 | + |
| 22 | + |
| 23 | +### Temporary key storage |
| 24 | + |
| 25 | +By default, wolfBoot will store the pre-shared symmetric key used for encryption in a temporary area |
| 26 | +on the internal flash. This allows read-out protections to be used to hide the temporary key. |
| 27 | + |
| 28 | +Alternatively, more secure mechanisms are available to store the temporary key in a different key storage |
| 29 | +(e.g. using a hardware security module or a TPM device). |
| 30 | + |
| 31 | +The temporary key can be set at run time by the application, and will be used exactly once by the bootloader |
| 32 | +to verify and install the next update. The key can be for example received from a back-end during the update |
| 33 | +process using secure communication, and set by the application, using `libwolfboot` API, to be used by |
| 34 | +wolfBoot upon next boot. |
| 35 | + |
| 36 | +Aside from setting the temporary key, the update mechanism remains the same for distributing, uploading and |
| 37 | +installing firmware updates through wolfBoot. |
| 38 | + |
| 39 | +### Libwolfboot API |
| 40 | + |
| 41 | +The API to communicate with the bootloader from the application is expanded when this feature is enabled, |
| 42 | +to allow setting a temporary key to process the next update. |
| 43 | + |
| 44 | +The functions |
| 45 | + |
| 46 | +``` |
| 47 | +int wolfBoot_set_encrypt_key(const uint8_t *key, const uint8_t *nonce); |
| 48 | +int wolfBoot_erase_encrypt_key(void); |
| 49 | +``` |
| 50 | + |
| 51 | +can be used to set a temporary encryption key for the external partition, or erase a key previously set, respectively. |
| 52 | + |
| 53 | +Moreover, using `libwolfboot` to access the external flash with wolfboot hal from the application will not |
| 54 | +use encryption. This way the received update, already encrypted at origin, can be stored in the external |
| 55 | +memory unchanged, and retrieved in its encrypted format, e.g. to verify that the transfer has been successful before |
| 56 | +reboot. |
| 57 | + |
| 58 | +### Symmetric encryption algorithm |
| 59 | + |
| 60 | +The algorithm currently used to encrypt and decrypt data in external partitions |
| 61 | +is Chacha20-256. |
| 62 | + |
| 63 | + - The `key` provided to `wolfBoot_set_encrypt_key()` must be exactly 32 Bytes long. |
| 64 | + - The `nonce` argument must be a 96-bit (12 Bytes) randomly generated buffer, to be used as IV for encryption and decryption. |
| 65 | + |
| 66 | +## Example usage |
| 67 | + |
| 68 | +### Signing and encrypting the update bundle |
| 69 | + |
| 70 | +The `sign.py` tool can sign and encrypt the image with a single command. |
| 71 | +The encryption secret is provided in a binary file that should contain a concatenation of |
| 72 | +a 32B ChaCha-256 key and a 12B nonce. |
| 73 | + |
| 74 | +In the examples provided, the test application uses the following parameters: |
| 75 | + |
| 76 | +``` |
| 77 | +key = "0123456789abcdef0123456789abcdef" |
| 78 | +nonce = "0123456789ab" |
| 79 | +``` |
| 80 | + |
| 81 | +So it is easy to prepare the encryption secret in the test scripts or from the command line using: |
| 82 | + |
| 83 | +``` |
| 84 | +echo -n "0123456789abcdef0123456789abcdef0123456789ab" > enc_key.der |
| 85 | +``` |
| 86 | + |
| 87 | +The `sign.py` script can now be invoked to produce a signed+encrypted image, by using the extra argument `--encrypt` followed by the |
| 88 | +secret file: |
| 89 | + |
| 90 | +``` |
| 91 | +./tools/keytools/sign.py --encrypt enc_key.der test-app/image.bin ecc256.der 24 |
| 92 | +
|
| 93 | +``` |
| 94 | + |
| 95 | +which will produce as output the file `test-app/image_v24_signed_and_encrypted.bin`, that can be transferred to the target's external device. |
| 96 | + |
| 97 | + |
| 98 | +### API usage in the application |
| 99 | + |
| 100 | +When transferring the image, the application can still use the libwolfboot API functions to store the encrypted firmware. When called from the application, |
| 101 | +the function `ext_flash_write` will store the payload unencrypted. |
| 102 | + |
| 103 | +In order to trigger an update, before calling `wolfBoot_update_trigger` it is necessary to set the temporary key used by the bootloader by calling `wolfBoot_set_encrypt_key`. |
| 104 | + |
| 105 | +An example of encrypted update trigger can be found in the [stm32wb test application source code](test-app/app_stm32wb.c). |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
0 commit comments