flash_encrypt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 "esp_image_format.h"
  16. #include "esp_flash_encrypt.h"
  17. #include "esp_flash_partitions.h"
  18. #include "esp_secure_boot.h"
  19. #include "esp_efuse.h"
  20. #include "esp_log.h"
  21. #include "rom/secure_boot.h"
  22. #include "soc/rtc_wdt.h"
  23. #include "rom/cache.h"
  24. #include "rom/spi_flash.h" /* TODO: Remove this */
  25. static const char *TAG = "flash_encrypt";
  26. /* Static functions for stages of flash encryption */
  27. static esp_err_t initialise_flash_encryption(void);
  28. static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_crypt_wr_dis);
  29. static esp_err_t encrypt_bootloader();
  30. static esp_err_t encrypt_and_load_partition_table(esp_partition_info_t *partition_table, int *num_partitions);
  31. static esp_err_t encrypt_partition(int index, const esp_partition_info_t *partition);
  32. esp_err_t esp_flash_encrypt_check_and_update(void)
  33. {
  34. uint32_t efuse_blk0 = REG_READ(EFUSE_BLK0_RDATA0_REG);
  35. ESP_LOGV(TAG, "efuse_blk0 raw value %08x", efuse_blk0);
  36. uint32_t flash_crypt_cnt = (efuse_blk0 & EFUSE_RD_FLASH_CRYPT_CNT_M) >> EFUSE_RD_FLASH_CRYPT_CNT_S;
  37. bool flash_crypt_wr_dis = efuse_blk0 & EFUSE_WR_DIS_FLASH_CRYPT_CNT;
  38. ESP_LOGV(TAG, "efuse FLASH_CRYPT_CNT 0x%x WR_DIS_FLASH_CRYPT_CNT 0x%x", flash_crypt_cnt, flash_crypt_wr_dis);
  39. if (__builtin_parity(flash_crypt_cnt) == 1) {
  40. /* Flash is already encrypted */
  41. int left = (7 - __builtin_popcount(flash_crypt_cnt)) / 2;
  42. if (flash_crypt_wr_dis) {
  43. left = 0; /* can't update FLASH_CRYPT_CNT, no more flashes */
  44. }
  45. ESP_LOGI(TAG, "flash encryption is enabled (%d plaintext flashes left)", left);
  46. return ESP_OK;
  47. }
  48. else {
  49. /* Flash is not encrypted, so encrypt it! */
  50. return encrypt_flash_contents(flash_crypt_cnt, flash_crypt_wr_dis);
  51. }
  52. }
  53. static esp_err_t initialise_flash_encryption(void)
  54. {
  55. uint32_t coding_scheme = REG_GET_FIELD(EFUSE_BLK0_RDATA6_REG, EFUSE_CODING_SCHEME);
  56. if (coding_scheme != EFUSE_CODING_SCHEME_VAL_NONE && coding_scheme != EFUSE_CODING_SCHEME_VAL_34) {
  57. ESP_LOGE(TAG, "Unknown/unsupported CODING_SCHEME value 0x%x", coding_scheme);
  58. return ESP_ERR_NOT_SUPPORTED;
  59. }
  60. /* Before first flash encryption pass, need to initialise key & crypto config */
  61. /* Generate key */
  62. uint32_t dis_reg = REG_READ(EFUSE_BLK0_RDATA0_REG);
  63. bool efuse_key_read_protected = dis_reg & EFUSE_RD_DIS_BLK1;
  64. bool efuse_key_write_protected = dis_reg & EFUSE_WR_DIS_BLK1;
  65. if (efuse_key_read_protected == false
  66. && efuse_key_write_protected == false
  67. && REG_READ(EFUSE_BLK1_RDATA0_REG) == 0
  68. && REG_READ(EFUSE_BLK1_RDATA1_REG) == 0
  69. && REG_READ(EFUSE_BLK1_RDATA2_REG) == 0
  70. && REG_READ(EFUSE_BLK1_RDATA3_REG) == 0
  71. && REG_READ(EFUSE_BLK1_RDATA4_REG) == 0
  72. && REG_READ(EFUSE_BLK1_RDATA5_REG) == 0
  73. && REG_READ(EFUSE_BLK1_RDATA6_REG) == 0
  74. && REG_READ(EFUSE_BLK1_RDATA7_REG) == 0) {
  75. ESP_LOGI(TAG, "Generating new flash encryption key...");
  76. esp_efuse_write_random_key(EFUSE_BLK1_WDATA0_REG);
  77. esp_efuse_burn_new_values();
  78. ESP_LOGI(TAG, "Read & write protecting new key...");
  79. REG_WRITE(EFUSE_BLK0_WDATA0_REG, EFUSE_WR_DIS_BLK1 | EFUSE_RD_DIS_BLK1);
  80. esp_efuse_burn_new_values();
  81. } else {
  82. if(!(efuse_key_read_protected && efuse_key_write_protected)) {
  83. ESP_LOGE(TAG, "Flash encryption key has to be either unset or both read and write protected");
  84. return ESP_ERR_INVALID_STATE;
  85. }
  86. ESP_LOGW(TAG, "Using pre-loaded flash encryption key in EFUSE block 1");
  87. }
  88. /* CRYPT_CONFIG determines which bits of the AES block key are XORed
  89. with bits from the flash address, to provide the key tweak.
  90. CRYPT_CONFIG == 0 is effectively AES ECB mode (NOT SUPPORTED)
  91. For now this is hardcoded to XOR all 256 bits of the key.
  92. If you need to override it, you can pre-burn this efuse to the
  93. desired value and then write-protect it, in which case this
  94. operation does nothing. Please note this is not recommended!
  95. */
  96. ESP_LOGI(TAG, "Setting CRYPT_CONFIG efuse to 0xF");
  97. REG_WRITE(EFUSE_BLK0_WDATA5_REG, EFUSE_FLASH_CRYPT_CONFIG_M);
  98. esp_efuse_burn_new_values();
  99. uint32_t new_wdata6 = 0;
  100. #ifndef CONFIG_FLASH_ENCRYPTION_UART_BOOTLOADER_ALLOW_ENCRYPT
  101. ESP_LOGI(TAG, "Disable UART bootloader encryption...");
  102. new_wdata6 |= EFUSE_DISABLE_DL_ENCRYPT;
  103. #else
  104. ESP_LOGW(TAG, "Not disabling UART bootloader encryption");
  105. #endif
  106. #ifndef CONFIG_FLASH_ENCRYPTION_UART_BOOTLOADER_ALLOW_DECRYPT
  107. ESP_LOGI(TAG, "Disable UART bootloader decryption...");
  108. new_wdata6 |= EFUSE_DISABLE_DL_DECRYPT;
  109. #else
  110. ESP_LOGW(TAG, "Not disabling UART bootloader decryption - SECURITY COMPROMISED");
  111. #endif
  112. #ifndef CONFIG_FLASH_ENCRYPTION_UART_BOOTLOADER_ALLOW_CACHE
  113. ESP_LOGI(TAG, "Disable UART bootloader MMU cache...");
  114. new_wdata6 |= EFUSE_DISABLE_DL_CACHE;
  115. #else
  116. ESP_LOGW(TAG, "Not disabling UART bootloader MMU cache - SECURITY COMPROMISED");
  117. #endif
  118. #ifndef CONFIG_SECURE_BOOT_ALLOW_JTAG
  119. ESP_LOGI(TAG, "Disable JTAG...");
  120. new_wdata6 |= EFUSE_RD_DISABLE_JTAG;
  121. #else
  122. ESP_LOGW(TAG, "Not disabling JTAG - SECURITY COMPROMISED");
  123. #endif
  124. #ifndef CONFIG_SECURE_BOOT_ALLOW_ROM_BASIC
  125. ESP_LOGI(TAG, "Disable ROM BASIC interpreter fallback...");
  126. new_wdata6 |= EFUSE_RD_CONSOLE_DEBUG_DISABLE;
  127. #else
  128. ESP_LOGW(TAG, "Not disabling ROM BASIC fallback - SECURITY COMPROMISED");
  129. #endif
  130. if (new_wdata6 != 0) {
  131. REG_WRITE(EFUSE_BLK0_WDATA6_REG, new_wdata6);
  132. esp_efuse_burn_new_values();
  133. }
  134. return ESP_OK;
  135. }
  136. /* Encrypt all flash data that should be encrypted */
  137. static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_crypt_wr_dis)
  138. {
  139. esp_err_t err;
  140. esp_partition_info_t partition_table[ESP_PARTITION_TABLE_MAX_ENTRIES];
  141. int num_partitions;
  142. /* If the last flash_crypt_cnt bit is burned or write-disabled, the
  143. device can't re-encrypt itself. */
  144. if (flash_crypt_wr_dis || flash_crypt_cnt == 0xFF) {
  145. ESP_LOGE(TAG, "Cannot re-encrypt data (FLASH_CRYPT_CNT 0x%02x write disabled %d", flash_crypt_cnt, flash_crypt_wr_dis);
  146. return ESP_FAIL;
  147. }
  148. if (flash_crypt_cnt == 0) {
  149. /* Very first flash of encrypted data: generate keys, etc. */
  150. err = initialise_flash_encryption();
  151. if (err != ESP_OK) {
  152. return err;
  153. }
  154. }
  155. err = encrypt_bootloader();
  156. if (err != ESP_OK) {
  157. return err;
  158. }
  159. err = encrypt_and_load_partition_table(partition_table, &num_partitions);
  160. if (err != ESP_OK) {
  161. return err;
  162. }
  163. /* Now iterate the just-loaded partition table, looking for entries to encrypt
  164. */
  165. /* Go through each partition and encrypt if necessary */
  166. for (int i = 0; i < num_partitions; i++) {
  167. err = encrypt_partition(i, &partition_table[i]);
  168. if (err != ESP_OK) {
  169. return err;
  170. }
  171. }
  172. ESP_LOGD(TAG, "All flash regions checked for encryption pass");
  173. /* Set least significant 0-bit in flash_crypt_cnt */
  174. int ffs_inv = __builtin_ffs((~flash_crypt_cnt) & 0xFF);
  175. /* ffs_inv shouldn't be zero, as zero implies flash_crypt_cnt == 0xFF */
  176. uint32_t new_flash_crypt_cnt = flash_crypt_cnt + (1 << (ffs_inv - 1));
  177. ESP_LOGD(TAG, "FLASH_CRYPT_CNT 0x%x -> 0x%x", flash_crypt_cnt, new_flash_crypt_cnt);
  178. REG_SET_FIELD(EFUSE_BLK0_WDATA0_REG, EFUSE_FLASH_CRYPT_CNT, new_flash_crypt_cnt);
  179. esp_efuse_burn_new_values();
  180. ESP_LOGI(TAG, "Flash encryption completed");
  181. return ESP_OK;
  182. }
  183. static esp_err_t encrypt_bootloader()
  184. {
  185. esp_err_t err;
  186. uint32_t image_length;
  187. /* Check for plaintext bootloader (verification will fail if it's already encrypted) */
  188. if (esp_image_verify_bootloader(&image_length) == ESP_OK) {
  189. ESP_LOGD(TAG, "bootloader is plaintext. Encrypting...");
  190. err = esp_flash_encrypt_region(ESP_BOOTLOADER_OFFSET, image_length);
  191. if (err != ESP_OK) {
  192. ESP_LOGE(TAG, "Failed to encrypt bootloader in place: 0x%x", err);
  193. return err;
  194. }
  195. if (esp_secure_boot_enabled()) {
  196. /* If secure boot is enabled and bootloader was plaintext, also
  197. need to encrypt secure boot IV+digest.
  198. */
  199. ESP_LOGD(TAG, "Encrypting secure bootloader IV & digest...");
  200. err = esp_flash_encrypt_region(FLASH_OFFS_SECURE_BOOT_IV_DIGEST,
  201. FLASH_SECTOR_SIZE);
  202. if (err != ESP_OK) {
  203. ESP_LOGE(TAG, "Failed to encrypt bootloader IV & digest in place: 0x%x", err);
  204. return err;
  205. }
  206. }
  207. }
  208. else {
  209. ESP_LOGW(TAG, "no valid bootloader was found");
  210. }
  211. return ESP_OK;
  212. }
  213. static esp_err_t encrypt_and_load_partition_table(esp_partition_info_t *partition_table, int *num_partitions)
  214. {
  215. esp_err_t err;
  216. /* Check for plaintext partition table */
  217. err = bootloader_flash_read(ESP_PARTITION_TABLE_OFFSET, partition_table, ESP_PARTITION_TABLE_MAX_LEN, false);
  218. if (err != ESP_OK) {
  219. ESP_LOGE(TAG, "Failed to read partition table data");
  220. return err;
  221. }
  222. if (esp_partition_table_verify(partition_table, false, num_partitions) == ESP_OK) {
  223. ESP_LOGD(TAG, "partition table is plaintext. Encrypting...");
  224. esp_err_t err = esp_flash_encrypt_region(ESP_PARTITION_TABLE_OFFSET,
  225. FLASH_SECTOR_SIZE);
  226. if (err != ESP_OK) {
  227. ESP_LOGE(TAG, "Failed to encrypt partition table in place. %x", err);
  228. return err;
  229. }
  230. }
  231. else {
  232. ESP_LOGE(TAG, "Failed to read partition table data - not plaintext?");
  233. return ESP_ERR_INVALID_STATE;
  234. }
  235. /* Valid partition table loded */
  236. return ESP_OK;
  237. }
  238. static esp_err_t encrypt_partition(int index, const esp_partition_info_t *partition)
  239. {
  240. esp_err_t err;
  241. bool should_encrypt = (partition->flags & PART_FLAG_ENCRYPTED);
  242. if (partition->type == PART_TYPE_APP) {
  243. /* check if the partition holds a valid unencrypted app */
  244. esp_image_metadata_t data_ignored;
  245. err = esp_image_verify(ESP_IMAGE_VERIFY,
  246. &partition->pos,
  247. &data_ignored);
  248. should_encrypt = (err == ESP_OK);
  249. } else if ((partition->type == PART_TYPE_DATA && partition->subtype == PART_SUBTYPE_DATA_OTA)
  250. || (partition->type == PART_TYPE_DATA && partition->subtype == PART_SUBTYPE_DATA_NVS_KEYS)) {
  251. /* check if we have ota data partition and the partition should be encrypted unconditionally */
  252. should_encrypt = true;
  253. }
  254. if (!should_encrypt) {
  255. return ESP_OK;
  256. }
  257. else {
  258. /* should_encrypt */
  259. ESP_LOGI(TAG, "Encrypting partition %d at offset 0x%x...", index, partition->pos.offset);
  260. err = esp_flash_encrypt_region(partition->pos.offset, partition->pos.size);
  261. if (err != ESP_OK) {
  262. ESP_LOGE(TAG, "Failed to encrypt partition %d", index);
  263. }
  264. return err;
  265. }
  266. }
  267. esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length)
  268. {
  269. esp_err_t err;
  270. uint32_t buf[FLASH_SECTOR_SIZE / sizeof(uint32_t)];
  271. if (src_addr % FLASH_SECTOR_SIZE != 0) {
  272. ESP_LOGE(TAG, "esp_flash_encrypt_region bad src_addr 0x%x",src_addr);
  273. return ESP_FAIL;
  274. }
  275. for (size_t i = 0; i < data_length; i += FLASH_SECTOR_SIZE) {
  276. rtc_wdt_feed();
  277. uint32_t sec_start = i + src_addr;
  278. err = bootloader_flash_read(sec_start, buf, FLASH_SECTOR_SIZE, false);
  279. if (err != ESP_OK) {
  280. goto flash_failed;
  281. }
  282. err = bootloader_flash_erase_sector(sec_start / FLASH_SECTOR_SIZE);
  283. if (err != ESP_OK) {
  284. goto flash_failed;
  285. }
  286. err = bootloader_flash_write(sec_start, buf, FLASH_SECTOR_SIZE, true);
  287. if (err != ESP_OK) {
  288. goto flash_failed;
  289. }
  290. }
  291. return ESP_OK;
  292. flash_failed:
  293. ESP_LOGE(TAG, "flash operation failed: 0x%x", err);
  294. return err;
  295. }
  296. void esp_flash_write_protect_crypt_cnt()
  297. {
  298. uint32_t efuse_blk0 = REG_READ(EFUSE_BLK0_RDATA0_REG);
  299. bool flash_crypt_wr_dis = efuse_blk0 & EFUSE_WR_DIS_FLASH_CRYPT_CNT;
  300. if(!flash_crypt_wr_dis) {
  301. REG_WRITE(EFUSE_BLK0_WDATA0_REG, EFUSE_WR_DIS_FLASH_CRYPT_CNT);
  302. esp_efuse_burn_new_values();
  303. }
  304. }