flash_encrypt.c 13 KB

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