Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include <strings.h> |
| 8 | #include "bootloader_flash_priv.h" |
| 9 | #include "bootloader_random.h" |
| 10 | #include "esp_image_format.h" |
| 11 | #include "esp_flash_encrypt.h" |
| 12 | #include "esp_flash_partitions.h" |
| 13 | #include "esp_secure_boot.h" |
| 14 | #include "esp_efuse.h" |
| 15 | #include "esp_efuse_table.h" |
| 16 | #include "esp_log.h" |
| 17 | #include "hal/wdt_hal.h" |
| 18 | |
| 19 | #include "esp_mcuboot_image.h" |
| 20 | |
| 21 | #if CONFIG_IDF_TARGET_ESP32 |
| 22 | #define CRYPT_CNT ESP_EFUSE_FLASH_CRYPT_CNT |
| 23 | #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT |
| 24 | #else |
| 25 | #define CRYPT_CNT ESP_EFUSE_SPI_BOOT_CRYPT_CNT |
| 26 | #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_SPI_BOOT_CRYPT_CNT |
| 27 | #endif |
| 28 | |
| 29 | /* This file implements FLASH ENCRYPTION related APIs to perform |
| 30 | * various operations such as programming necessary flash encryption |
| 31 | * eFuses, detect whether flash encryption is enabled (by reading eFuse) |
| 32 | * and if required encrypt the partitions in flash memory |
| 33 | */ |
| 34 | |
| 35 | static const char *TAG = "flash_encrypt"; |
| 36 | |
| 37 | /* Static functions for stages of flash encryption */ |
| 38 | static esp_err_t initialise_flash_encryption(void); |
| 39 | static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_crypt_wr_dis) __attribute__((unused)); |
| 40 | static esp_err_t encrypt_bootloader(void); |
| 41 | static esp_err_t encrypt_primary_slot(void); |
| 42 | |
| 43 | esp_err_t esp_flash_encrypt_check_and_update(void) |
| 44 | { |
| 45 | size_t flash_crypt_cnt = 0; |
| 46 | esp_efuse_read_field_cnt(CRYPT_CNT, &flash_crypt_cnt); |
| 47 | bool flash_crypt_wr_dis = esp_efuse_read_field_bit(WR_DIS_CRYPT_CNT); |
| 48 | |
| 49 | ESP_LOGV(TAG, "CRYPT_CNT %d, write protection %d", flash_crypt_cnt, flash_crypt_wr_dis); |
| 50 | |
| 51 | if (flash_crypt_cnt % 2 == 1) { |
| 52 | /* Flash is already encrypted */ |
| 53 | int left = (CRYPT_CNT[0]->bit_count - flash_crypt_cnt) / 2; |
| 54 | if (flash_crypt_wr_dis) { |
| 55 | left = 0; /* can't update FLASH_CRYPT_CNT, no more flashes */ |
| 56 | } |
| 57 | ESP_LOGI(TAG, "flash encryption is enabled (%d plaintext flashes left)", left); |
| 58 | return ESP_OK; |
| 59 | } else { |
| 60 | #ifndef CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED |
| 61 | /* Flash is not encrypted, so encrypt it! */ |
| 62 | return encrypt_flash_contents(flash_crypt_cnt, flash_crypt_wr_dis); |
| 63 | #else |
| 64 | ESP_LOGE(TAG, "flash encryption is not enabled, and SECURE_FLASH_REQUIRE_ALREADY_ENABLED " |
| 65 | "is set, refusing to boot."); |
| 66 | return ESP_ERR_INVALID_STATE; |
| 67 | #endif // CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static esp_err_t check_and_generate_encryption_keys(void) |
| 72 | { |
| 73 | size_t key_size = 32; |
| 74 | #ifdef CONFIG_IDF_TARGET_ESP32 |
| 75 | enum { BLOCKS_NEEDED = 1 }; |
| 76 | esp_efuse_purpose_t purposes[BLOCKS_NEEDED] = { |
| 77 | ESP_EFUSE_KEY_PURPOSE_FLASH_ENCRYPTION, |
| 78 | }; |
| 79 | esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(EFUSE_BLK_ENCRYPT_FLASH); |
| 80 | if (coding_scheme != EFUSE_CODING_SCHEME_NONE && coding_scheme != EFUSE_CODING_SCHEME_3_4) { |
| 81 | ESP_LOGE(TAG, "Unknown/unsupported CODING_SCHEME value 0x%x", coding_scheme); |
| 82 | return ESP_ERR_NOT_SUPPORTED; |
| 83 | } |
| 84 | if (coding_scheme == EFUSE_CODING_SCHEME_3_4) { |
| 85 | key_size = 24; |
| 86 | } |
| 87 | #else |
| 88 | #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_AES256 |
| 89 | enum { BLOCKS_NEEDED = 2 }; |
| 90 | esp_efuse_purpose_t purposes[BLOCKS_NEEDED] = { |
| 91 | ESP_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_1, |
| 92 | ESP_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_2, |
| 93 | }; |
| 94 | if (esp_efuse_find_purpose(ESP_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY, NULL)) { |
| 95 | ESP_LOGE(TAG, "XTS_AES_128_KEY is already in use, XTS_AES_256_KEY_1/2 can not be used"); |
| 96 | return ESP_ERR_INVALID_STATE; |
| 97 | } |
| 98 | #else |
| 99 | enum { BLOCKS_NEEDED = 1 }; |
| 100 | esp_efuse_purpose_t purposes[BLOCKS_NEEDED] = { |
| 101 | ESP_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY, |
| 102 | }; |
| 103 | #endif // CONFIG_SECURE_FLASH_ENCRYPTION_AES256 |
| 104 | #endif // CONFIG_IDF_TARGET_ESP32 |
| 105 | |
| 106 | /* Initialize all efuse block entries to invalid (max) value */ |
| 107 | esp_efuse_block_t blocks[BLOCKS_NEEDED] = {[0 ... BLOCKS_NEEDED-1] = EFUSE_BLK_KEY_MAX}; |
| 108 | bool has_key = true; |
| 109 | for (unsigned i = 0; i < BLOCKS_NEEDED; i++) { |
| 110 | bool tmp_has_key = esp_efuse_find_purpose(purposes[i], &blocks[i]); |
| 111 | if (tmp_has_key) { // For ESP32: esp_efuse_find_purpose() always returns True, need to check whether the key block is used or not. |
| 112 | tmp_has_key &= !esp_efuse_key_block_unused(blocks[i]); |
| 113 | } |
| 114 | if (i == 1 && tmp_has_key != has_key) { |
| 115 | ESP_LOGE(TAG, "Invalid efuse key blocks: Both AES-256 key blocks must be set."); |
| 116 | return ESP_ERR_INVALID_STATE; |
| 117 | } |
| 118 | has_key &= tmp_has_key; |
| 119 | } |
| 120 | |
| 121 | if (!has_key) { |
| 122 | /* Generate key */ |
| 123 | uint8_t keys[BLOCKS_NEEDED][32] = { 0 }; |
| 124 | ESP_LOGI(TAG, "Generating new flash encryption key..."); |
| 125 | for (unsigned i = 0; i < BLOCKS_NEEDED; ++i) { |
| 126 | bootloader_fill_random(keys[i], key_size); |
| 127 | } |
| 128 | ESP_LOGD(TAG, "Key generation complete"); |
| 129 | |
| 130 | esp_err_t err = esp_efuse_write_keys(purposes, keys, BLOCKS_NEEDED); |
| 131 | if (err != ESP_OK) { |
| 132 | if (err == ESP_ERR_NOT_ENOUGH_UNUSED_KEY_BLOCKS) { |
| 133 | ESP_LOGE(TAG, "Not enough free efuse key blocks (need %d) to continue", BLOCKS_NEEDED); |
| 134 | } else { |
| 135 | ESP_LOGE(TAG, "Failed to write efuse block with purpose (err=0x%x). Can't continue.", err); |
| 136 | } |
| 137 | return err; |
| 138 | } |
| 139 | } else { |
| 140 | for (unsigned i = 0; i < BLOCKS_NEEDED; i++) { |
| 141 | if (!esp_efuse_get_key_dis_write(blocks[i]) |
| 142 | || !esp_efuse_get_key_dis_read(blocks[i]) |
| 143 | || !esp_efuse_get_keypurpose_dis_write(blocks[i])) { // For ESP32: no keypurpose, it returns always True. |
| 144 | ESP_LOGE(TAG, "Invalid key state, check read&write protection for key and keypurpose(if exists)"); |
| 145 | return ESP_ERR_INVALID_STATE; |
| 146 | } |
| 147 | } |
| 148 | ESP_LOGI(TAG, "Using pre-loaded flash encryption key in efuse"); |
| 149 | } |
| 150 | return ESP_OK; |
| 151 | } |
| 152 | |
| 153 | static esp_err_t initialise_flash_encryption(void) |
| 154 | { |
| 155 | esp_efuse_batch_write_begin(); /* Batch all efuse writes at the end of this function */ |
| 156 | |
| 157 | /* Before first flash encryption pass, need to initialise key & crypto config */ |
| 158 | esp_err_t err = check_and_generate_encryption_keys(); |
| 159 | if (err != ESP_OK) { |
| 160 | esp_efuse_batch_write_cancel(); |
| 161 | return err; |
| 162 | } |
| 163 | |
| 164 | err = esp_flash_encryption_enable_secure_features(); |
| 165 | if (err != ESP_OK) { |
| 166 | esp_efuse_batch_write_cancel(); |
| 167 | return err; |
| 168 | } |
| 169 | |
| 170 | err = esp_efuse_batch_write_commit(); |
| 171 | if (err != ESP_OK) { |
| 172 | ESP_LOGE(TAG, "Error programming security eFuses (err=0x%x).", err); |
| 173 | return err; |
| 174 | } |
| 175 | |
| 176 | return ESP_OK; |
| 177 | } |
| 178 | |
| 179 | /* Encrypt all flash data that should be encrypted */ |
| 180 | static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_crypt_wr_dis) |
| 181 | { |
| 182 | esp_err_t err; |
| 183 | |
| 184 | /* If all flash_crypt_cnt bits are burned or write-disabled, the |
| 185 | device can't re-encrypt itself. */ |
| 186 | if (flash_crypt_wr_dis || flash_crypt_cnt == CRYPT_CNT[0]->bit_count) { |
| 187 | ESP_LOGE(TAG, "Cannot re-encrypt data CRYPT_CNT %d write disabled %d", flash_crypt_cnt, flash_crypt_wr_dis); |
| 188 | return ESP_FAIL; |
| 189 | } |
| 190 | |
| 191 | if (flash_crypt_cnt == 0) { |
| 192 | /* Very first flash of encrypted data: generate keys, etc. */ |
| 193 | err = initialise_flash_encryption(); |
| 194 | if (err != ESP_OK) { |
| 195 | return err; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | err = encrypt_bootloader(); |
| 200 | if (err != ESP_OK) { |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | /* If the primary slot executable application is not encrypted, |
| 205 | * then encrypt it |
| 206 | */ |
| 207 | err = encrypt_primary_slot(); |
| 208 | if (err != ESP_OK) { |
| 209 | return err; |
| 210 | } |
| 211 | |
| 212 | /* Unconditionally encrypts remaining regions |
| 213 | * This will need changes when implementing multi-slot support |
| 214 | */ |
| 215 | ESP_LOGI(TAG, "Encrypting remaining flash..."); |
| 216 | uint32_t region_addr = CONFIG_ESP_APPLICATION_SECONDARY_START_ADDRESS; |
| 217 | size_t region_size = CONFIG_ESP_APPLICATION_SIZE; |
| 218 | err = esp_flash_encrypt_region(region_addr, region_size); |
| 219 | if (err != ESP_OK) { |
| 220 | return err; |
| 221 | } |
| 222 | region_addr = CONFIG_ESP_SCRATCH_OFFSET; |
| 223 | region_size = CONFIG_ESP_SCRATCH_SIZE; |
| 224 | err = esp_flash_encrypt_region(region_addr, region_size); |
| 225 | if (err != ESP_OK) { |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE |
| 230 | // Go straight to max, permanently enabled |
| 231 | ESP_LOGI(TAG, "Setting CRYPT_CNT for permanent encryption"); |
| 232 | size_t new_flash_crypt_cnt = CRYPT_CNT[0]->bit_count - flash_crypt_cnt; |
| 233 | #else |
| 234 | /* Set least significant 0-bit in flash_crypt_cnt */ |
| 235 | size_t new_flash_crypt_cnt = 1; |
| 236 | #endif |
| 237 | ESP_LOGD(TAG, "CRYPT_CNT %d -> %d", flash_crypt_cnt, new_flash_crypt_cnt); |
| 238 | err = esp_efuse_write_field_cnt(CRYPT_CNT, new_flash_crypt_cnt); |
| 239 | |
| 240 | ESP_LOGI(TAG, "Flash encryption completed"); |
| 241 | |
| 242 | return ESP_OK; |
| 243 | } |
| 244 | |
| 245 | static esp_err_t encrypt_bootloader(void) |
| 246 | { |
| 247 | esp_err_t err; |
| 248 | uint32_t image_length; |
| 249 | /* Check for plaintext bootloader (verification will fail if it's already encrypted) */ |
| 250 | if (esp_image_verify_bootloader(&image_length) == ESP_OK) { |
| 251 | ESP_LOGI(TAG, "Encrypting bootloader..."); |
| 252 | |
| 253 | err = esp_flash_encrypt_region(ESP_BOOTLOADER_OFFSET, CONFIG_ESP_BOOTLOADER_SIZE); |
| 254 | if (err != ESP_OK) { |
| 255 | ESP_LOGE(TAG, "Failed to encrypt bootloader in place: 0x%x", err); |
| 256 | return err; |
| 257 | } |
| 258 | ESP_LOGI(TAG, "Bootloader encrypted successfully"); |
| 259 | } else { |
| 260 | ESP_LOGW(TAG, "No valid bootloader was found"); |
| 261 | return ESP_ERR_NOT_FOUND; |
| 262 | } |
| 263 | |
| 264 | return ESP_OK; |
| 265 | } |
| 266 | |
| 267 | static esp_err_t verify_img_header(uint32_t addr, const esp_image_load_header_t *image, bool silent) |
| 268 | { |
| 269 | esp_err_t err = ESP_OK; |
| 270 | |
| 271 | if (image->header_magic != ESP_LOAD_HEADER_MAGIC) { |
| 272 | if (!silent) { |
| 273 | ESP_LOGE(TAG, "image at 0x%x has invalid magic byte", |
| 274 | addr); |
| 275 | } |
| 276 | err = ESP_ERR_IMAGE_INVALID; |
| 277 | } |
| 278 | |
| 279 | return err; |
| 280 | } |
| 281 | |
| 282 | static esp_err_t encrypt_primary_slot(void) |
| 283 | { |
| 284 | esp_err_t err; |
| 285 | |
| 286 | esp_image_load_header_t img_header; |
| 287 | |
| 288 | /* Check if the slot is plaintext or encrypted, 0x20 offset is for skipping |
| 289 | * MCUboot header |
| 290 | */ |
| 291 | err = bootloader_flash_read(CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS + 0x20, |
| 292 | &img_header, sizeof(esp_image_load_header_t), true); |
| 293 | if (err != ESP_OK) { |
| 294 | ESP_LOGE(TAG, "Failed to read slot img header"); |
| 295 | return err; |
| 296 | } else { |
| 297 | err = verify_img_header(CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS, |
| 298 | &img_header, true); |
| 299 | } |
| 300 | |
| 301 | if (err == ESP_OK) { |
| 302 | ESP_LOGI(TAG, "Encrypting primary slot..."); |
| 303 | |
| 304 | err = esp_flash_encrypt_region(CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS, |
| 305 | CONFIG_ESP_APPLICATION_SIZE); |
| 306 | if (err != ESP_OK) { |
| 307 | ESP_LOGE(TAG, "Failed to encrypt slot in place: 0x%x", err); |
| 308 | return err; |
| 309 | } |
| 310 | } else { |
| 311 | ESP_LOGW(TAG, "Slot already encrypted or no valid image was found"); |
| 312 | } |
| 313 | |
| 314 | return ESP_OK; |
| 315 | } |
| 316 | |
| 317 | esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length) |
| 318 | { |
| 319 | esp_err_t err; |
| 320 | uint32_t buf[FLASH_SECTOR_SIZE / sizeof(uint32_t)]; |
| 321 | |
| 322 | if (src_addr % FLASH_SECTOR_SIZE != 0) { |
| 323 | ESP_LOGE(TAG, "esp_flash_encrypt_region bad src_addr 0x%x", src_addr); |
| 324 | return ESP_FAIL; |
| 325 | } |
| 326 | |
| 327 | wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL}; |
| 328 | for (size_t i = 0; i < data_length; i += FLASH_SECTOR_SIZE) { |
| 329 | wdt_hal_write_protect_disable(&rtc_wdt_ctx); |
| 330 | wdt_hal_feed(&rtc_wdt_ctx); |
| 331 | wdt_hal_write_protect_enable(&rtc_wdt_ctx); |
| 332 | uint32_t sec_start = i + src_addr; |
| 333 | err = bootloader_flash_read(sec_start, buf, FLASH_SECTOR_SIZE, true); |
| 334 | if (err != ESP_OK) { |
| 335 | goto flash_failed; |
| 336 | } |
| 337 | err = bootloader_flash_erase_sector(sec_start / FLASH_SECTOR_SIZE); |
| 338 | if (err != ESP_OK) { |
| 339 | goto flash_failed; |
| 340 | } |
| 341 | err = bootloader_flash_write(sec_start, buf, FLASH_SECTOR_SIZE, true); |
| 342 | if (err != ESP_OK) { |
| 343 | goto flash_failed; |
| 344 | } |
| 345 | } |
| 346 | return ESP_OK; |
| 347 | |
| 348 | flash_failed: |
| 349 | ESP_LOGE(TAG, "flash operation failed: 0x%x", err); |
| 350 | return err; |
| 351 | } |