flash_encrypt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <strings.h>
  14. #include "bootloader_flash.h"
  15. #include "bootloader_random.h"
  16. #include "bootloader_utility.h"
  17. #include "esp_image_format.h"
  18. #include "esp_flash_encrypt.h"
  19. #include "esp_flash_partitions.h"
  20. #include "esp_secure_boot.h"
  21. #include "esp_log.h"
  22. #include "esp32s2/rom/secure_boot.h"
  23. #include "esp32s2/rom/cache.h"
  24. #include "esp32s2/rom/efuse.h"
  25. #include "esp_efuse.h"
  26. #include "esp_efuse_table.h"
  27. #include "hal/wdt_hal.h"
  28. static const char *TAG = "flash_encrypt";
  29. /* Static functions for stages of flash encryption */
  30. static esp_err_t initialise_flash_encryption(void);
  31. static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_crypt_wr_dis) __attribute__((unused));
  32. static esp_err_t encrypt_bootloader(void);
  33. static esp_err_t encrypt_and_load_partition_table(esp_partition_info_t *partition_table, int *num_partitions);
  34. static esp_err_t encrypt_partition(int index, const esp_partition_info_t *partition);
  35. esp_err_t esp_flash_encrypt_check_and_update(void)
  36. {
  37. uint8_t flash_crypt_wr_dis = 0;
  38. uint32_t flash_crypt_cnt = 0;
  39. esp_efuse_read_field_blob(ESP_EFUSE_SPI_BOOT_CRYPT_CNT, &flash_crypt_cnt, 3);
  40. esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_SPI_BOOT_CRYPT_CNT, &flash_crypt_wr_dis, 1);
  41. ESP_LOGV(TAG, "SPI_BOOT_CRYPT_CNT 0x%x", flash_crypt_cnt);
  42. ESP_LOGV(TAG, "EFUSE_WR_DIS_SPI_BOOT_CRYPT_CNT 0x%x", flash_crypt_wr_dis);
  43. _Static_assert(EFUSE_SPI_BOOT_CRYPT_CNT == 0x7, "assuming CRYPT_CNT is only 3 bits wide");
  44. if (__builtin_parity(flash_crypt_cnt) == 1) {
  45. /* Flash is already encrypted */
  46. int left = (flash_crypt_cnt == 1) ? 1 : 0;
  47. if (flash_crypt_wr_dis) {
  48. left = 0; /* can't update FLASH_CRYPT_CNT, no more flashes */
  49. }
  50. ESP_LOGI(TAG, "flash encryption is enabled (%d plaintext flashes left)", left);
  51. return ESP_OK;
  52. } else {
  53. #ifndef CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED
  54. /* Flash is not encrypted, so encrypt it! */
  55. return encrypt_flash_contents(flash_crypt_cnt, flash_crypt_wr_dis);
  56. #else
  57. ESP_LOGE(TAG, "flash encryption is not enabled, and SECURE_FLASH_REQUIRE_ALREADY_ENABLED "
  58. "is set, refusing to boot.");
  59. return ESP_ERR_INVALID_STATE;
  60. #endif // CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED
  61. }
  62. }
  63. static bool s_key_dis_read(ets_efuse_block_t block)
  64. {
  65. unsigned key_num = block - ETS_EFUSE_BLOCK_KEY0;
  66. return REG_GET_FIELD(EFUSE_RD_REPEAT_DATA0_REG, EFUSE_RD_DIS) & (EFUSE_RD_DIS_KEY0 << key_num);
  67. }
  68. static bool s_key_dis_write(ets_efuse_block_t block)
  69. {
  70. unsigned key_num = block - ETS_EFUSE_BLOCK_KEY0;
  71. return REG_GET_FIELD(EFUSE_RD_WR_DIS_REG, EFUSE_WR_DIS) & (EFUSE_WR_DIS_KEY0 << key_num);
  72. }
  73. static esp_err_t check_and_generate_encryption_keys(void)
  74. {
  75. esp_err_t err = ESP_ERR_INVALID_STATE;
  76. ets_efuse_block_t aes_128_key_block;
  77. ets_efuse_block_t aes_256_key_block_1;
  78. ets_efuse_block_t aes_256_key_block_2;
  79. bool has_aes128 = ets_efuse_find_purpose(ETS_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY, &aes_128_key_block);
  80. bool has_aes256_1 = ets_efuse_find_purpose(ETS_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_1, &aes_256_key_block_1);
  81. bool has_aes256_2 = ets_efuse_find_purpose(ETS_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_2, &aes_256_key_block_2);
  82. bool has_key = has_aes128 || (has_aes256_1 && has_aes256_2);
  83. bool dis_write = false;
  84. bool dis_read = false;
  85. // If there are keys set, they must be write and read protected!
  86. if(has_key && has_aes128) {
  87. dis_write = s_key_dis_write(aes_128_key_block);
  88. dis_read = s_key_dis_read(aes_128_key_block);
  89. } else if (has_key && has_aes256_1 && has_aes256_2) {
  90. dis_write = s_key_dis_write(aes_256_key_block_1) && s_key_dis_write(aes_256_key_block_2);
  91. dis_read = s_key_dis_read(aes_256_key_block_1) && s_key_dis_read(aes_256_key_block_2);
  92. }
  93. if (!has_key && (has_aes256_1 || has_aes256_2)) {
  94. ESP_LOGE(TAG, "Invalid efuse key blocks: Both AES-256 key blocks must be set.");
  95. return ESP_ERR_INVALID_STATE;
  96. }
  97. if(has_key && (!dis_read || !dis_write)) {
  98. ESP_LOGE(TAG, "Invalid key state, a key was set but not read and write protected.");
  99. return ESP_ERR_INVALID_STATE;
  100. }
  101. if(!has_key && !dis_write && !dis_read) {
  102. ESP_LOGI(TAG, "Generating new flash encryption key...");
  103. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_AES256
  104. const unsigned BLOCKS_NEEDED = 2;
  105. const ets_efuse_purpose_t PURPOSE_START = ETS_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_1;
  106. const ets_efuse_purpose_t PURPOSE_END = ETS_EFUSE_KEY_PURPOSE_XTS_AES_256_KEY_2;
  107. #else
  108. const unsigned BLOCKS_NEEDED = 1;
  109. const ets_efuse_purpose_t PURPOSE_START = ETS_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY;
  110. const ets_efuse_purpose_t PURPOSE_END = ETS_EFUSE_KEY_PURPOSE_XTS_AES_128_KEY;
  111. #endif
  112. if (ets_efuse_count_unused_key_blocks() < BLOCKS_NEEDED) {
  113. ESP_LOGE(TAG, "Not enough free efuse key blocks (need %d) to continue", BLOCKS_NEEDED);
  114. return ESP_ERR_INVALID_STATE;
  115. }
  116. for(ets_efuse_purpose_t purpose = PURPOSE_START; purpose <= PURPOSE_END; purpose++) {
  117. uint32_t buf[8] = {0};
  118. bootloader_fill_random(buf, sizeof(buf));
  119. ets_efuse_block_t block = ets_efuse_find_unused_key_block();
  120. ESP_LOGD(TAG, "Writing ETS_EFUSE_BLOCK_KEY%d with purpose %d",
  121. block - ETS_EFUSE_BLOCK_KEY0, purpose);
  122. /* Note: everything else in this function is deferred as a batch write, but we write the
  123. key (and write protect it) immediately as it's too fiddly to manage unused key blocks, etc.
  124. in bootloader size footprint otherwise. */
  125. int r = ets_efuse_write_key(block, purpose, buf, sizeof(buf));
  126. if (r != 0) {
  127. ESP_LOGE(TAG, "Failed to write efuse block %d with purpose %d. Can't continue.",
  128. block, purpose);
  129. return ESP_FAIL;
  130. }
  131. /* assuming numbering of esp_efuse_block_t matches ets_efuse_block_t */
  132. _Static_assert((int)EFUSE_BLK_KEY0 == (int)ETS_EFUSE_BLOCK_KEY0, "esp_efuse_block_t doesn't match ets_efuse_block_t");
  133. _Static_assert((int)EFUSE_BLK_KEY1 == (int)ETS_EFUSE_BLOCK_KEY1, "esp_efuse_block_t doesn't match ets_efuse_block_t");
  134. _Static_assert((int)EFUSE_BLK_KEY2 == (int)ETS_EFUSE_BLOCK_KEY2, "esp_efuse_block_t doesn't match ets_efuse_block_t");
  135. _Static_assert((int)EFUSE_BLK_KEY3 == (int)ETS_EFUSE_BLOCK_KEY3, "esp_efuse_block_t doesn't match ets_efuse_block_t");
  136. _Static_assert((int)EFUSE_BLK_KEY4 == (int)ETS_EFUSE_BLOCK_KEY4, "esp_efuse_block_t doesn't match ets_efuse_block_t");
  137. _Static_assert((int)EFUSE_BLK_KEY5 == (int)ETS_EFUSE_BLOCK_KEY5, "esp_efuse_block_t doesn't match ets_efuse_block_t");
  138. // protect this block against reading after key is set (writing is done by ets_efuse_write_key)
  139. err = esp_efuse_set_read_protect(block);
  140. if(err != ESP_OK) {
  141. ESP_LOGE(TAG, "Failed to set read protect to efuse block %d. Can't continue.", block);
  142. return err;
  143. }
  144. }
  145. ESP_LOGD(TAG, "Key generation complete");
  146. return ESP_OK;
  147. } else {
  148. ESP_LOGI(TAG, "Using pre-existing key in efuse");
  149. return ESP_OK;
  150. }
  151. }
  152. static esp_err_t initialise_flash_encryption(void)
  153. {
  154. esp_efuse_batch_write_begin(); /* Batch all efuse writes at the end of this function */
  155. esp_err_t key_state = check_and_generate_encryption_keys();
  156. if(key_state != ESP_OK) {
  157. esp_efuse_batch_write_cancel();
  158. return key_state;
  159. }
  160. #ifndef CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC
  161. ESP_LOGI(TAG, "Disable UART bootloader encryption...");
  162. esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT);
  163. #else
  164. ESP_LOGW(TAG, "Not disabling UART bootloader encryption");
  165. #endif
  166. #ifndef CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE
  167. ESP_LOGI(TAG, "Disable UART bootloader cache...");
  168. esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_DCACHE);
  169. esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_ICACHE);
  170. #else
  171. ESP_LOGW(TAG, "Not disabling UART bootloader cache - SECURITY COMPROMISED");
  172. #endif
  173. #ifndef CONFIG_SECURE_BOOT_ALLOW_JTAG
  174. ESP_LOGI(TAG, "Disable JTAG...");
  175. esp_efuse_write_field_bit(ESP_EFUSE_HARD_DIS_JTAG);
  176. #else
  177. ESP_LOGW(TAG, "Not disabling JTAG - SECURITY COMPROMISED");
  178. #endif
  179. esp_efuse_write_field_bit(ESP_EFUSE_DIS_BOOT_REMAP);
  180. esp_efuse_write_field_bit(ESP_EFUSE_DIS_LEGACY_SPI_BOOT);
  181. esp_err_t err = esp_efuse_batch_write_commit();
  182. return err;
  183. }
  184. /* Encrypt all flash data that should be encrypted */
  185. static esp_err_t encrypt_flash_contents(uint32_t spi_boot_crypt_cnt, bool flash_crypt_wr_dis)
  186. {
  187. esp_err_t err;
  188. esp_partition_info_t partition_table[ESP_PARTITION_TABLE_MAX_ENTRIES];
  189. int num_partitions;
  190. /* If the last spi_boot_crypt_cnt bit is burned or write-disabled, the
  191. device can't re-encrypt itself. */
  192. if (flash_crypt_wr_dis || spi_boot_crypt_cnt == EFUSE_SPI_BOOT_CRYPT_CNT) {
  193. ESP_LOGE(TAG, "Cannot re-encrypt data SPI_BOOT_CRYPT_CNT 0x%02x write disabled %d", spi_boot_crypt_cnt, flash_crypt_wr_dis);
  194. return ESP_FAIL;
  195. }
  196. if (spi_boot_crypt_cnt == 0) {
  197. /* Very first flash of encrypted data: generate keys, etc. */
  198. err = initialise_flash_encryption();
  199. if (err != ESP_OK) {
  200. return err;
  201. }
  202. }
  203. err = encrypt_bootloader();
  204. if (err != ESP_OK) {
  205. return err;
  206. }
  207. err = encrypt_and_load_partition_table(partition_table, &num_partitions);
  208. if (err != ESP_OK) {
  209. return err;
  210. }
  211. /* Now iterate the just-loaded partition table, looking for entries to encrypt */
  212. for (int i = 0; i < num_partitions; i++) {
  213. err = encrypt_partition(i, &partition_table[i]);
  214. if (err != ESP_OK) {
  215. return err;
  216. }
  217. }
  218. ESP_LOGD(TAG, "All flash regions checked for encryption pass");
  219. /* Set least significant 0-bit in spi_boot_crypt_cnt */
  220. int ffs_inv = __builtin_ffs((~spi_boot_crypt_cnt) & 0x7);
  221. /* ffs_inv shouldn't be zero, as zero implies spi_boot_crypt_cnt == 0xFF */
  222. uint32_t new_spi_boot_crypt_cnt = (1 << (ffs_inv - 1));
  223. ESP_LOGD(TAG, "SPI_BOOT_CRYPT_CNT 0x%x -> 0x%x", spi_boot_crypt_cnt, new_spi_boot_crypt_cnt + spi_boot_crypt_cnt);
  224. esp_efuse_write_field_blob(ESP_EFUSE_SPI_BOOT_CRYPT_CNT, &new_spi_boot_crypt_cnt, 3);
  225. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  226. //Secure SPI boot cnt after its update if needed.
  227. const uint32_t spi_boot_cnt_wr_dis = 1;
  228. ESP_LOGI(TAG, "Write protecting SPI_CRYPT_CNT eFuse");
  229. esp_efuse_write_field_blob(ESP_EFUSE_WR_DIS_SPI_BOOT_CRYPT_CNT, &spi_boot_cnt_wr_dis, 1);
  230. #endif
  231. ESP_LOGI(TAG, "Flash encryption completed");
  232. return ESP_OK;
  233. }
  234. static esp_err_t encrypt_bootloader(void)
  235. {
  236. esp_err_t err;
  237. uint32_t image_length;
  238. /* Check for plaintext bootloader (verification will fail if it's already encrypted) */
  239. if (esp_image_verify_bootloader(&image_length) == ESP_OK) {
  240. ESP_LOGD(TAG, "bootloader is plaintext. Encrypting...");
  241. #if CONFIG_SECURE_BOOT_V2_ENABLED
  242. // Account for the signature sector after the bootloader
  243. image_length = (image_length + FLASH_SECTOR_SIZE - 1) & ~(FLASH_SECTOR_SIZE - 1);
  244. image_length += FLASH_SECTOR_SIZE;
  245. if (ESP_BOOTLOADER_OFFSET + image_length > ESP_PARTITION_TABLE_OFFSET) {
  246. 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);
  247. return ESP_ERR_INVALID_SIZE;
  248. }
  249. #endif // CONFIG_SECURE_BOOT_V2_ENABLED
  250. err = esp_flash_encrypt_region(ESP_BOOTLOADER_OFFSET, image_length);
  251. if (err != ESP_OK) {
  252. ESP_LOGE(TAG, "Failed to encrypt bootloader in place: 0x%x", err);
  253. return err;
  254. }
  255. ESP_LOGI(TAG, "bootloader encrypted successfully");
  256. return err;
  257. }
  258. else {
  259. ESP_LOGW(TAG, "no valid bootloader was found");
  260. return ESP_ERR_NOT_FOUND;
  261. }
  262. }
  263. static esp_err_t encrypt_and_load_partition_table(esp_partition_info_t *partition_table, int *num_partitions)
  264. {
  265. esp_err_t err;
  266. /* Check for plaintext partition table */
  267. err = bootloader_flash_read(ESP_PARTITION_TABLE_OFFSET, partition_table, ESP_PARTITION_TABLE_MAX_LEN, false);
  268. if (err != ESP_OK) {
  269. ESP_LOGE(TAG, "Failed to read partition table data");
  270. return err;
  271. }
  272. if (esp_partition_table_verify(partition_table, false, num_partitions) == ESP_OK) {
  273. ESP_LOGD(TAG, "partition table is plaintext. Encrypting...");
  274. esp_err_t err = esp_flash_encrypt_region(ESP_PARTITION_TABLE_OFFSET,
  275. FLASH_SECTOR_SIZE);
  276. if (err != ESP_OK) {
  277. ESP_LOGE(TAG, "Failed to encrypt partition table in place. %x", err);
  278. return err;
  279. }
  280. }
  281. else {
  282. ESP_LOGE(TAG, "Failed to read partition table data - not plaintext?");
  283. return ESP_ERR_INVALID_STATE;
  284. }
  285. /* Valid partition table loded */
  286. ESP_LOGI(TAG, "partition table encrypted and loaded successfully");
  287. return ESP_OK;
  288. }
  289. static esp_err_t encrypt_partition(int index, const esp_partition_info_t *partition)
  290. {
  291. esp_err_t err;
  292. bool should_encrypt = (partition->flags & PART_FLAG_ENCRYPTED);
  293. if (partition->type == PART_TYPE_APP) {
  294. /* check if the partition holds a valid unencrypted app */
  295. esp_image_metadata_t data_ignored;
  296. err = esp_image_verify(ESP_IMAGE_VERIFY,
  297. &partition->pos,
  298. &data_ignored);
  299. should_encrypt = (err == ESP_OK);
  300. } else if (partition->type == PART_TYPE_DATA && partition->subtype == PART_SUBTYPE_DATA_OTA) {
  301. /* check if we have ota data partition and the partition should be encrypted unconditionally */
  302. should_encrypt = true;
  303. }
  304. if (!should_encrypt) {
  305. return ESP_OK;
  306. }
  307. else {
  308. /* should_encrypt */
  309. ESP_LOGI(TAG, "Encrypting partition %d at offset 0x%x (length 0x%x)...", index, partition->pos.offset, partition->pos.size);
  310. err = esp_flash_encrypt_region(partition->pos.offset, partition->pos.size);
  311. ESP_LOGI(TAG, "Done encrypting");
  312. if (err != ESP_OK) {
  313. ESP_LOGE(TAG, "Failed to encrypt partition %d", index);
  314. }
  315. return err;
  316. }
  317. }
  318. esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length)
  319. {
  320. esp_err_t err;
  321. uint32_t buf[FLASH_SECTOR_SIZE / sizeof(uint32_t)];
  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. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  327. for (size_t i = 0; i < data_length; i += FLASH_SECTOR_SIZE) {
  328. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  329. wdt_hal_feed(&rtc_wdt_ctx);
  330. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  331. uint32_t sec_start = i + src_addr;
  332. err = bootloader_flash_read(sec_start, buf, FLASH_SECTOR_SIZE, false);
  333. if (err != ESP_OK) {
  334. goto flash_failed;
  335. }
  336. err = bootloader_flash_erase_sector(sec_start / FLASH_SECTOR_SIZE);
  337. if (err != ESP_OK) {
  338. goto flash_failed;
  339. }
  340. err = bootloader_flash_write(sec_start, buf, FLASH_SECTOR_SIZE, true);
  341. if (err != ESP_OK) {
  342. goto flash_failed;
  343. }
  344. }
  345. return ESP_OK;
  346. flash_failed:
  347. ESP_LOGE(TAG, "flash operation failed: 0x%x", err);
  348. return err;
  349. }