1 /*
2 * SPDX-FileCopyrightText: 2015-2022 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 #ifdef CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
19 #include "soc/sensitive_reg.h"
20 #endif
21
22 #ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
23
24 #if CONFIG_IDF_TARGET_ESP32
25 #define CRYPT_CNT ESP_EFUSE_FLASH_CRYPT_CNT
26 #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT
27 #else
28 #define CRYPT_CNT ESP_EFUSE_SPI_BOOT_CRYPT_CNT
29 #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_SPI_BOOT_CRYPT_CNT
30 #endif
31
32 #define FLASH_ENC_CNT_MAX (CRYPT_CNT[0]->bit_count)
33
34 /* This file implements FLASH ENCRYPTION related APIs to perform
35 * various operations such as programming necessary flash encryption
36 * eFuses, detect whether flash encryption is enabled (by reading eFuse)
37 * and if required encrypt the partitions in flash memory
38 */
39
40 static const char *TAG = "flash_encrypt";
41
42 /* Static functions for stages of flash encryption */
43 static esp_err_t encrypt_bootloader(void);
44 static esp_err_t encrypt_and_load_partition_table(esp_partition_info_t *partition_table, int *num_partitions);
45 static esp_err_t encrypt_partition(int index, const esp_partition_info_t *partition);
46 static size_t get_flash_encrypt_cnt_value(void);
47
get_flash_encrypt_cnt_value(void)48 static size_t get_flash_encrypt_cnt_value(void)
49 {
50 size_t flash_crypt_cnt = 0;
51 esp_efuse_read_field_cnt(CRYPT_CNT, &flash_crypt_cnt);
52 return flash_crypt_cnt;
53 }
54
esp_flash_encrypt_initialized_once(void)55 bool esp_flash_encrypt_initialized_once(void)
56 {
57 return get_flash_encrypt_cnt_value() != 0;
58 }
59
esp_flash_encrypt_is_write_protected(bool print_error)60 bool esp_flash_encrypt_is_write_protected(bool print_error)
61 {
62 if (esp_efuse_read_field_bit(WR_DIS_CRYPT_CNT)) {
63 if (print_error) {
64 ESP_LOGE(TAG, "Flash Encryption cannot be enabled (CRYPT_CNT (%d) is write protected)", get_flash_encrypt_cnt_value());
65 }
66 return true;
67 }
68 return false;
69 }
70
esp_flash_encrypt_state(void)71 bool esp_flash_encrypt_state(void)
72 {
73 size_t flash_crypt_cnt = get_flash_encrypt_cnt_value();
74 bool flash_crypt_wr_dis = esp_flash_encrypt_is_write_protected(false);
75
76 ESP_LOGV(TAG, "CRYPT_CNT %d, write protection %d", flash_crypt_cnt, flash_crypt_wr_dis);
77
78 if (flash_crypt_cnt % 2 == 1) {
79 /* Flash is already encrypted */
80 int left = (FLASH_ENC_CNT_MAX - flash_crypt_cnt) / 2;
81 if (flash_crypt_wr_dis) {
82 left = 0; /* can't update FLASH_CRYPT_CNT, no more flashes */
83 }
84 ESP_LOGI(TAG, "flash encryption is enabled (%d plaintext flashes left)", left);
85 return true;
86 }
87 return false;
88 }
89
esp_flash_encrypt_check_and_update(void)90 esp_err_t esp_flash_encrypt_check_and_update(void)
91 {
92 bool flash_encryption_enabled = esp_flash_encrypt_state();
93 if (!flash_encryption_enabled) {
94 #ifndef CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED
95 if (esp_flash_encrypt_is_write_protected(true)) {
96 return ESP_FAIL;
97 }
98
99 esp_err_t err = esp_flash_encrypt_init();
100 if (err != ESP_OK) {
101 ESP_LOGE(TAG, "Initialization of Flash encryption key failed (%d)", err);
102 return err;
103 }
104
105 err = esp_flash_encrypt_contents();
106 if (err != ESP_OK) {
107 ESP_LOGE(TAG, "Encryption flash contents failed (%d)", err);
108 return err;
109 }
110
111 err = esp_flash_encrypt_enable();
112 if (err != ESP_OK) {
113 ESP_LOGE(TAG, "Enabling of Flash encryption failed (%d)", err);
114 return err;
115 }
116 #else
117 ESP_LOGE(TAG, "flash encryption is not enabled, and SECURE_FLASH_REQUIRE_ALREADY_ENABLED "
118 "is set, refusing to boot.");
119 return ESP_ERR_INVALID_STATE;
120 #endif // CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED
121 }
122 return ESP_OK;
123 }
124
check_and_generate_encryption_keys(void)125 static esp_err_t check_and_generate_encryption_keys(void)
126 {
127 size_t key_size = 32;
128 #ifdef CONFIG_IDF_TARGET_ESP32
129 enum { BLOCKS_NEEDED = 1 };
130 esp_efuse_purpose_t purposes[BLOCKS_NEEDED] = {
131 ESP_EFUSE_KEY_PURPOSE_FLASH_ENCRYPTION,
132 };
133 esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(EFUSE_BLK_ENCRYPT_FLASH);
134 if (coding_scheme != EFUSE_CODING_SCHEME_NONE && coding_scheme != EFUSE_CODING_SCHEME_3_4) {
135 ESP_LOGE(TAG, "Unknown/unsupported CODING_SCHEME value 0x%x", coding_scheme);
136 return ESP_ERR_NOT_SUPPORTED;
137 }
138 if (coding_scheme == EFUSE_CODING_SCHEME_3_4) {
139 key_size = 24;
140 }
141 #else
142 #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_AES256
143 enum { BLOCKS_NEEDED = 2 };
144 esp_efuse_purpose_t purposes[BLOCKS_NEEDED] = {
145 ESP_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_1,
146 ESP_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_2,
147 };
148 if (esp_efuse_find_purpose(ESP_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY, NULL)) {
149 ESP_LOGE(TAG, "XTS_AES_128_KEY is already in use, XTS_AES_256_KEY_1/2 can not be used");
150 return ESP_ERR_INVALID_STATE;
151 }
152 #else
153 #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_AES128_DERIVED
154 enum { BLOCKS_NEEDED = 1 };
155 esp_efuse_purpose_t purposes[BLOCKS_NEEDED] = {
156 ESP_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY_DERIVED_FROM_128_EFUSE_BITS,
157 };
158 key_size = 16;
159 #else
160 enum { BLOCKS_NEEDED = 1 };
161 esp_efuse_purpose_t purposes[BLOCKS_NEEDED] = {
162 ESP_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY,
163 };
164 #endif // CONFIG_SECURE_FLASH_ENCRYPTION_AES128_DERIVED
165 #endif // CONFIG_SECURE_FLASH_ENCRYPTION_AES256
166 #endif // CONFIG_IDF_TARGET_ESP32
167
168 /* Initialize all efuse block entries to invalid (max) value */
169 esp_efuse_block_t blocks[BLOCKS_NEEDED] = {[0 ... BLOCKS_NEEDED-1] = EFUSE_BLK_KEY_MAX};
170 bool has_key = true;
171 for (unsigned i = 0; i < BLOCKS_NEEDED; i++) {
172 bool tmp_has_key = esp_efuse_find_purpose(purposes[i], &blocks[i]);
173 if (tmp_has_key) { // For ESP32: esp_efuse_find_purpose() always returns True, need to check whether the key block is used or not.
174 tmp_has_key &= !esp_efuse_key_block_unused(blocks[i]);
175 }
176 if (i == 1 && tmp_has_key != has_key) {
177 ESP_LOGE(TAG, "Invalid efuse key blocks: Both AES-256 key blocks must be set.");
178 return ESP_ERR_INVALID_STATE;
179 }
180 has_key &= tmp_has_key;
181 }
182
183 if (!has_key) {
184 /* Generate key */
185 uint8_t keys[BLOCKS_NEEDED][32] = { 0 };
186 ESP_LOGI(TAG, "Generating new flash encryption key...");
187 for (unsigned i = 0; i < BLOCKS_NEEDED; ++i) {
188 bootloader_fill_random(keys[i], key_size);
189 }
190 ESP_LOGD(TAG, "Key generation complete");
191
192 esp_err_t err = esp_efuse_write_keys(purposes, keys, BLOCKS_NEEDED);
193 if (err != ESP_OK) {
194 if (err == ESP_ERR_NOT_ENOUGH_UNUSED_KEY_BLOCKS) {
195 ESP_LOGE(TAG, "Not enough free efuse key blocks (need %d) to continue", BLOCKS_NEEDED);
196 } else {
197 ESP_LOGE(TAG, "Failed to write efuse block with purpose (err=0x%x). Can't continue.", err);
198 }
199 return err;
200 }
201 } else {
202 for (unsigned i = 0; i < BLOCKS_NEEDED; i++) {
203 if (!esp_efuse_get_key_dis_write(blocks[i])
204 || !esp_efuse_get_key_dis_read(blocks[i])
205 || !esp_efuse_get_keypurpose_dis_write(blocks[i])) { // For ESP32: no keypurpose, it returns always True.
206 ESP_LOGE(TAG, "Invalid key state, check read&write protection for key and keypurpose(if exists)");
207 return ESP_ERR_INVALID_STATE;
208 }
209 }
210 ESP_LOGI(TAG, "Using pre-loaded flash encryption key in efuse");
211 }
212 return ESP_OK;
213 }
214
esp_flash_encrypt_init(void)215 esp_err_t esp_flash_encrypt_init(void)
216 {
217 if (esp_flash_encryption_enabled() || esp_flash_encrypt_initialized_once()) {
218 return ESP_OK;
219 }
220
221 /* Very first flash encryption pass: generate keys, etc. */
222
223 esp_efuse_batch_write_begin(); /* Batch all efuse writes at the end of this function */
224
225 /* Before first flash encryption pass, need to initialise key & crypto config */
226 esp_err_t err = check_and_generate_encryption_keys();
227 if (err != ESP_OK) {
228 esp_efuse_batch_write_cancel();
229 return err;
230 }
231
232 err = esp_flash_encryption_enable_secure_features();
233 if (err != ESP_OK) {
234 esp_efuse_batch_write_cancel();
235 return err;
236 }
237
238 err = esp_efuse_batch_write_commit();
239 if (err != ESP_OK) {
240 ESP_LOGE(TAG, "Error programming security eFuses (err=0x%x).", err);
241 return err;
242 }
243
244 return ESP_OK;
245 }
246
247 /* Encrypt all flash data that should be encrypted */
esp_flash_encrypt_contents(void)248 esp_err_t esp_flash_encrypt_contents(void)
249 {
250 esp_err_t err;
251 esp_partition_info_t partition_table[ESP_PARTITION_TABLE_MAX_ENTRIES];
252 int num_partitions;
253
254 #ifdef CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
255 REG_WRITE(SENSITIVE_XTS_AES_KEY_UPDATE_REG, 1);
256 #endif
257
258 err = encrypt_bootloader();
259 if (err != ESP_OK) {
260 return err;
261 }
262
263 err = encrypt_and_load_partition_table(partition_table, &num_partitions);
264 if (err != ESP_OK) {
265 return err;
266 }
267
268 /* Now iterate the just-loaded partition table, looking for entries to encrypt
269 */
270
271 /* Go through each partition and encrypt if necessary */
272 for (int i = 0; i < num_partitions; i++) {
273 err = encrypt_partition(i, &partition_table[i]);
274 if (err != ESP_OK) {
275 return err;
276 }
277 }
278
279 ESP_LOGD(TAG, "All flash regions checked for encryption pass");
280
281 return ESP_OK;
282 }
283
esp_flash_encrypt_enable(void)284 esp_err_t esp_flash_encrypt_enable(void)
285 {
286 esp_err_t err = ESP_OK;
287 if (!esp_flash_encryption_enabled()) {
288
289 if (esp_flash_encrypt_is_write_protected(true)) {
290 return ESP_FAIL;
291 }
292
293 size_t flash_crypt_cnt = get_flash_encrypt_cnt_value();
294
295 #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
296 // Go straight to max, permanently enabled
297 ESP_LOGI(TAG, "Setting CRYPT_CNT for permanent encryption");
298 size_t new_flash_crypt_cnt = FLASH_ENC_CNT_MAX - flash_crypt_cnt;
299 #else
300 /* Set least significant 0-bit in flash_crypt_cnt */
301 size_t new_flash_crypt_cnt = 1;
302 #endif
303 ESP_LOGD(TAG, "CRYPT_CNT %d -> %d", flash_crypt_cnt, new_flash_crypt_cnt);
304 err = esp_efuse_write_field_cnt(CRYPT_CNT, new_flash_crypt_cnt);
305
306 #if defined(CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE) && defined(CONFIG_SOC_FLASH_ENCRYPTION_XTS_AES_128_DERIVED)
307 // For AES128_DERIVED, FE key is 16 bytes and XTS_KEY_LENGTH_256 is 0.
308 // It is important to protect XTS_KEY_LENGTH_256 from further changing it to 1. Set write protection for this bit.
309 // Burning WR_DIS_CRYPT_CNT, blocks further changing of eFuses: DOWNLOAD_DIS_MANUAL_ENCRYPT, SPI_BOOT_CRYPT_CNT, [XTS_KEY_LENGTH_256], SECURE_BOOT_EN.
310 esp_efuse_write_field_bit(WR_DIS_CRYPT_CNT);
311 #endif
312 }
313
314 ESP_LOGI(TAG, "Flash encryption completed");
315
316 #ifdef CONFIG_EFUSE_VIRTUAL
317 ESP_LOGW(TAG, "Flash encryption not really completed. Must disable virtual efuses");
318 #endif
319
320 return err;
321 }
322
encrypt_bootloader(void)323 static esp_err_t encrypt_bootloader(void)
324 {
325 esp_err_t err;
326 uint32_t image_length;
327 /* Check for plaintext bootloader (verification will fail if it's already encrypted) */
328 if (esp_image_verify_bootloader(&image_length) == ESP_OK) {
329 ESP_LOGD(TAG, "bootloader is plaintext. Encrypting...");
330
331 #if CONFIG_SECURE_BOOT_V2_ENABLED
332 /* The image length obtained from esp_image_verify_bootloader includes the sector boundary padding and the signature block lengths */
333 if (ESP_BOOTLOADER_OFFSET + image_length > ESP_PARTITION_TABLE_OFFSET) {
334 ESP_LOGE(TAG, "Bootloader is too large to fit Secure Boot V2 signature sector and partition table (configured offset 0x%x)", ESP_PARTITION_TABLE_OFFSET);
335 return ESP_ERR_INVALID_SIZE;
336 }
337 #endif // CONFIG_SECURE_BOOT_V2_ENABLED
338
339 err = esp_flash_encrypt_region(ESP_BOOTLOADER_OFFSET, image_length);
340 if (err != ESP_OK) {
341 ESP_LOGE(TAG, "Failed to encrypt bootloader in place: 0x%x", err);
342 return err;
343 }
344
345 #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
346 /* If secure boot is enabled and bootloader was plaintext, also
347 * need to encrypt secure boot IV+digest.
348 */
349 ESP_LOGD(TAG, "Encrypting secure bootloader IV & digest...");
350 err = esp_flash_encrypt_region(FLASH_OFFS_SECURE_BOOT_IV_DIGEST, FLASH_SECTOR_SIZE);
351 if (err != ESP_OK) {
352 ESP_LOGE(TAG, "Failed to encrypt bootloader IV & digest in place: 0x%x", err);
353 return err;
354 }
355 #endif
356 ESP_LOGI(TAG, "bootloader encrypted successfully");
357 } else {
358 ESP_LOGW(TAG, "no valid bootloader was found");
359 return ESP_ERR_NOT_FOUND;
360 }
361 return ESP_OK;
362 }
363
encrypt_and_load_partition_table(esp_partition_info_t * partition_table,int * num_partitions)364 static esp_err_t encrypt_and_load_partition_table(esp_partition_info_t *partition_table, int *num_partitions)
365 {
366 esp_err_t err;
367 /* Check for plaintext partition table */
368 err = bootloader_flash_read(ESP_PARTITION_TABLE_OFFSET, partition_table, ESP_PARTITION_TABLE_MAX_LEN, false);
369 if (err != ESP_OK) {
370 ESP_LOGE(TAG, "Failed to read partition table data");
371 return err;
372 }
373 if (esp_partition_table_verify(partition_table, false, num_partitions) == ESP_OK) {
374 ESP_LOGD(TAG, "partition table is plaintext. Encrypting...");
375 esp_err_t err = esp_flash_encrypt_region(ESP_PARTITION_TABLE_OFFSET,
376 FLASH_SECTOR_SIZE);
377 if (err != ESP_OK) {
378 ESP_LOGE(TAG, "Failed to encrypt partition table in place. %x", err);
379 return err;
380 }
381 } else {
382 ESP_LOGE(TAG, "Failed to read partition table data - not plaintext?");
383 return ESP_ERR_INVALID_STATE;
384 }
385
386 /* Valid partition table loaded */
387 ESP_LOGI(TAG, "partition table encrypted and loaded successfully");
388 return ESP_OK;
389 }
390
391
encrypt_partition(int index,const esp_partition_info_t * partition)392 static esp_err_t encrypt_partition(int index, const esp_partition_info_t *partition)
393 {
394 esp_err_t err;
395 bool should_encrypt = (partition->flags & PART_FLAG_ENCRYPTED);
396
397 if (partition->type == PART_TYPE_APP) {
398 /* check if the partition holds a valid unencrypted app */
399 esp_image_metadata_t data_ignored;
400 err = esp_image_verify(ESP_IMAGE_VERIFY,
401 &partition->pos,
402 &data_ignored);
403 should_encrypt = (err == ESP_OK);
404 } else if ((partition->type == PART_TYPE_DATA && partition->subtype == PART_SUBTYPE_DATA_OTA)
405 || (partition->type == PART_TYPE_DATA && partition->subtype == PART_SUBTYPE_DATA_NVS_KEYS)) {
406 /* check if we have ota data partition and the partition should be encrypted unconditionally */
407 should_encrypt = true;
408 }
409
410 if (!should_encrypt) {
411 return ESP_OK;
412 } else {
413 /* should_encrypt */
414 ESP_LOGI(TAG, "Encrypting partition %d at offset 0x%x (length 0x%x)...", index, partition->pos.offset, partition->pos.size);
415
416 err = esp_flash_encrypt_region(partition->pos.offset, partition->pos.size);
417 ESP_LOGI(TAG, "Done encrypting");
418 if (err != ESP_OK) {
419 ESP_LOGE(TAG, "Failed to encrypt partition %d", index);
420 }
421 return err;
422 }
423 }
424
425
esp_flash_encrypt_region(uint32_t src_addr,size_t data_length)426 esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length)
427 {
428 esp_err_t err;
429 uint32_t buf[FLASH_SECTOR_SIZE / sizeof(uint32_t)];
430
431 if (src_addr % FLASH_SECTOR_SIZE != 0) {
432 ESP_LOGE(TAG, "esp_flash_encrypt_region bad src_addr 0x%x", src_addr);
433 return ESP_FAIL;
434 }
435
436 wdt_hal_context_t rtc_wdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
437
438 for (size_t i = 0; i < data_length; i += FLASH_SECTOR_SIZE) {
439 wdt_hal_write_protect_disable(&rtc_wdt_ctx);
440 wdt_hal_feed(&rtc_wdt_ctx);
441 wdt_hal_write_protect_enable(&rtc_wdt_ctx);
442 uint32_t sec_start = i + src_addr;
443 err = bootloader_flash_read(sec_start, buf, FLASH_SECTOR_SIZE, false);
444 if (err != ESP_OK) {
445 goto flash_failed;
446 }
447 err = bootloader_flash_erase_sector(sec_start / FLASH_SECTOR_SIZE);
448 if (err != ESP_OK) {
449 goto flash_failed;
450 }
451 err = bootloader_flash_write(sec_start, buf, FLASH_SECTOR_SIZE, true);
452 if (err != ESP_OK) {
453 goto flash_failed;
454 }
455 }
456 return ESP_OK;
457
458 flash_failed:
459 ESP_LOGE(TAG, "flash operation failed: 0x%x", err);
460 return err;
461 }
462
463 #endif // CONFIG_SECURE_FLASH_ENC_ENABLED
464