flash-encryption.rst 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. Flash Encryption
  2. ================
  3. :link_to_translation:`zh_CN:[中文]`
  4. This document provides introduction to Flash encryption concept on {IDF_TARGET_NAME} and demonstrates how this feature can be used during development as well as production by the user using a sample example. The primary intention of the document is to act as a quick start guide to test and verify flash encryption operations. The details of the flash encryption block can be found in the `{IDF_TARGET_NAME} Technical reference manual`_.
  5. .. _{IDF_TARGET_NAME} Technical Reference Manual: {IDF_TARGET_TRM_EN_URL}
  6. Introduction
  7. ------------
  8. Flash encryption is a feature for encrypting the contents of the {IDF_TARGET_NAME}'s attached SPI flash. When flash encryption is enabled, physical readout of the SPI flash is not sufficient to recover most flash contents. Encryption is applied by flashing the {IDF_TARGET_NAME} with plaintext data, and (if encryption is enabled) the bootloader encrypts the data in place on first boot.
  9. With flash encryption enabled, following kinds of flash data are encrypted by default:
  10. - Bootloader
  11. - Partition Table
  12. - All "app" type partitions
  13. Other types of flash data are encrypted conditionally:
  14. - Secure boot bootloader digest (if secure boot is enabled)
  15. - Any partition marked with the "encrypted" flag in the partition table
  16. .. only:: esp32
  17. Flash encryption is separate from the :doc:`Secure Boot <secure-boot-v2>` feature, and you can use flash encryption without enabling secure boot. However, for a secure environment both should be used simultaneously.
  18. .. important::
  19. For production use, flash encryption should be enabled in the "Release" mode only.
  20. .. important::
  21. Enabling flash encryption limits the options for further updates of the {IDF_TARGET_NAME}. Make sure to read this document (including :ref:`flash-encryption-limitations`) and understand the implications of enabling flash encryption.
  22. .. _flash-encryption-efuse:
  23. eFuse Used During Flash Encryption Process
  24. -------------------------------------------
  25. The flash encryption operation is controlled by various eFuses available on {IDF_TARGET_NAME}. Below is the list of eFuse and their description:
  26. ::
  27. eFuse Description Can be locked for Default
  28. reading/writing Value
  29. .. code-block:: none
  30. Coding scheme This 2 bit wide eFuse controls the Yes 0
  31. actual number of bits to be used
  32. from BLOCK1 to derive final 256 bit
  33. AES key. The coding scheme value is
  34. decoded as below:
  35. 0: 256 bits
  36. 1: 192 bits
  37. 2: 128 bits
  38. Final AES key is derived based on the
  39. FLASH_CRYPT_CONFIG value
  40. BLOCK1 256 bit wide eFuse block for storing Yes x
  41. AES key
  42. FLASH_CRYPT_CONFIG 4 bit wide eFuse which controls the Yes 0xF
  43. AES encryption process
  44. download_dis_encrypt When set, disables the flash encryption Yes 0
  45. operation while running in UART
  46. download mode
  47. download_dis_decrypt When set, disables the flash decryption Yes 0
  48. operation while running in UART
  49. download mode
  50. FLASH_CRYPT_CNT 7 bit eFuse which enables/disables Yes 0
  51. encryption at boot time
  52. Even number of bits set (0, 2, 4, 6):
  53. encrypt flash at boot time
  54. Odd number of bits set (1, 3, 5, 7): do
  55. not encrypt flash at boot time
  56. Read and write access to above bits is controlled by appropriate bits in ``efuse_wr_disable`` and ``efuse_rd_disable`` registers. More information about {IDF_TARGET_NAME} eFuse can be found at :doc:`eFuse manager <../api-reference/system/efuse>`.
  57. Flash Encryption Process
  58. ------------------------
  59. Assuming the eFuse values are in default state and second stage bootloader is compiled to support flash encryption, the flash encryption process executes as below:
  60. - On first power-on reset, all data in flash is un-encrypted (plaintext). First stage loader (ROM) will load the second stage loader in IRAM.
  61. - Second stage bootloader will read the flash_crypt_cnt (=00000000b) eFuse value and since the value is 0 (even number of bits set) it will configure and enable the flash encryption block. It will also program ``FLASH_CRYPT_CFG`` eFuse to value 0xF.
  62. - The flash encryption block will generate AES-256 bit key and store into BLOCK1 eFuse. This operation is performed in hardware and the key can not be accessed by software.
  63. - Next the flash encryption block will encrypt the flash contents (based on partition table flag value). Encrypting in-place can take some time (up to a minute for large partitions).
  64. - Second stage bootloader then sets the first available bit in flash_crypt_cnt (=00000001b) to mark the flash contents as encrypted (odd number of bits set).
  65. - For :ref:`flash_enc_release_mode` second stage bootloader will program ``download_dis_encrypt``, ``download_dis_decrypt`` & ``download_dis_cache`` eFuse bits to 1 to prevent UART bootloader from decrypting the flash contents. It will also write protect the ``FLASH_CRYPT_CNT`` eFuse bits.
  66. - For :ref:`flash_enc_development_mode` second stage bootloader will program only ``download_dis_decrypt`` & ``download_dis_cache`` eFuse bits to allow UART bootloader reflashing of encrypted binaries. Also ``FLASH_CRYPT_CNT`` eFuse bits will NOT be write protected.
  67. - The second stage bootloader then reboots the device to start executing encrypted image. It will transparently decrypt the flash contents and load into IRAM.
  68. During development stage there is a frequent need to program different plaintext flash images and test the flash encryption process. This requires UART download mode to be able to load new plaintext images as many number of times as required. However during manufacturing or production UART download mode should not be allowed to access flash contents due to security reason. Hence this requires two different {IDF_TARGET_NAME} configurations: one for development and other for production. Following section describes :ref:`flash_enc_development_mode` and :ref:`flash_enc_release_mode` for flash encryption and a step by step process to use them.
  69. .. important::
  70. Development mode as the name suggests should be used ONLY DURING DEVELOPMENT as it does not prevent modification and possible read back of encrypted flash contents.
  71. Steps to Setup Flash Encryption
  72. -------------------------------
  73. .. _flash_enc_development_mode:
  74. Development Mode
  75. ^^^^^^^^^^^^^^^^
  76. It is possible to run flash encryption process for development using either {IDF_TARGET_NAME} internally generated key or external host generated keys.
  77. Using {IDF_TARGET_NAME} Generated Flash Encryption Key
  78. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  79. As mentioned above :ref:`flash_enc_development_mode` allows user to download as many plaintext images using UART download mode. Following steps needs to be done to test flash encryption process:
  80. - Ensure you have a {IDF_TARGET_NAME} device with default flash encryption eFuse settings as shown in :ref:`flash-encryption-efuse`.
  81. - Navigate to flash encryption sample application in ``$IDF_PATH/examples/security/flash_encryption`` folder. This sample application will print the status of flash encryption: enabled or disabled. It will print the ``FLASH_CRYPT_CNT`` eFuse value.
  82. - Enable flash encryption support in second stage bootloader. In :ref:`project-configuration-menu`, navigate to "Security Features".
  83. - Select :ref:`Enable flash encryption on boot <CONFIG_SECURE_FLASH_ENC_ENABLED>`.
  84. - By default the mode is set for **Development**.
  85. - Select appropriate Bootloader log verbosity under Bootloader config.
  86. .. only:: esp32
  87. - Update to the partition table offset may be required since after enabling flash encryption the size of bootloader is increased. See :ref:`secure-boot-bootloader-size`
  88. - Save the configuration and exit.
  89. Build and flash the complete image including: bootloader, partition table and app. These partitions are initially written to the flash unencrypted.
  90. ::
  91. idf.py flash monitor
  92. Once the flashing is complete device will reset and on next boot second stage bootloader will encrypt the flash app partition and then reset. Now the sample application would get decrypted at runtime and executed. Below is a sample output when {IDF_TARGET_NAME} boots after flash encryption is enabled for the first time.
  93. ::
  94. --- idf_monitor on /dev/cu.SLAB_USBtoUART 115200 ---
  95. --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
  96. ets Jun 8 2016 00:22:57
  97. rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  98. configsip: 0, SPIWP:0xee
  99. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  100. mode:DIO, clock div:2
  101. load:0x3fff0018,len:4
  102. load:0x3fff001c,len:8452
  103. load:0x40078000,len:13608
  104. load:0x40080400,len:6664
  105. entry 0x40080764
  106. I (28) boot: ESP-IDF v4.0-dev-850-gc4447462d-dirty 2nd stage bootloader
  107. I (29) boot: compile time 15:37:14
  108. I (30) boot: Enabling RNG early entropy source...
  109. I (35) boot: SPI Speed : 40MHz
  110. I (39) boot: SPI Mode : DIO
  111. I (43) boot: SPI Flash Size : 4MB
  112. I (47) boot: Partition Table:
  113. I (51) boot: ## Label Usage Type ST Offset Length
  114. I (58) boot: 0 nvs WiFi data 01 02 0000a000 00006000
  115. I (66) boot: 1 phy_init RF data 01 01 00010000 00001000
  116. I (73) boot: 2 factory factory app 00 00 00020000 00100000
  117. I (81) boot: End of partition table
  118. I (85) esp_image: segment 0: paddr=0x00020020 vaddr=0x3f400020 size=0x0808c ( 32908) map
  119. I (105) esp_image: segment 1: paddr=0x000280b4 vaddr=0x3ffb0000 size=0x01ea4 ( 7844) load
  120. I (109) esp_image: segment 2: paddr=0x00029f60 vaddr=0x40080000 size=0x00400 ( 1024) load
  121. 0x40080000: _WindowOverflow4 at esp-idf/esp-idf/components/freertos/xtensa_vectors.S:1778
  122. I (114) esp_image: segment 3: paddr=0x0002a368 vaddr=0x40080400 size=0x05ca8 ( 23720) load
  123. I (132) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x126a8 ( 75432) map
  124. 0x400d0018: _flash_cache_start at ??:?
  125. I (159) esp_image: segment 5: paddr=0x000426c8 vaddr=0x400860a8 size=0x01f4c ( 8012) load
  126. 0x400860a8: prvAddNewTaskToReadyList at esp-idf/esp-idf/components/freertos/tasks.c:4561
  127. I (168) boot: Loaded app from partition at offset 0x20000
  128. I (168) boot: Checking flash encryption...
  129. I (168) flash_encrypt: Generating new flash encryption key...
  130. I (187) flash_encrypt: Read & write protecting new key...
  131. I (187) flash_encrypt: Setting CRYPT_CONFIG efuse to 0xF
  132. W (188) flash_encrypt: Not disabling UART bootloader encryption
  133. I (195) flash_encrypt: Disable UART bootloader decryption...
  134. I (201) flash_encrypt: Disable UART bootloader MMU cache...
  135. I (208) flash_encrypt: Disable JTAG...
  136. I (212) flash_encrypt: Disable ROM BASIC interpreter fallback...
  137. I (219) esp_image: segment 0: paddr=0x00001020 vaddr=0x3fff0018 size=0x00004 ( 4)
  138. I (227) esp_image: segment 1: paddr=0x0000102c vaddr=0x3fff001c size=0x02104 ( 8452)
  139. I (239) esp_image: segment 2: paddr=0x00003138 vaddr=0x40078000 size=0x03528 ( 13608)
  140. I (249) esp_image: segment 3: paddr=0x00006668 vaddr=0x40080400 size=0x01a08 ( 6664)
  141. I (657) esp_image: segment 0: paddr=0x00020020 vaddr=0x3f400020 size=0x0808c ( 32908) map
  142. I (669) esp_image: segment 1: paddr=0x000280b4 vaddr=0x3ffb0000 size=0x01ea4 ( 7844)
  143. I (672) esp_image: segment 2: paddr=0x00029f60 vaddr=0x40080000 size=0x00400 ( 1024)
  144. 0x40080000: _WindowOverflow4 at esp-idf/esp-idf/components/freertos/xtensa_vectors.S:1778
  145. I (676) esp_image: segment 3: paddr=0x0002a368 vaddr=0x40080400 size=0x05ca8 ( 23720)
  146. I (692) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x126a8 ( 75432) map
  147. 0x400d0018: _flash_cache_start at ??:?
  148. I (719) esp_image: segment 5: paddr=0x000426c8 vaddr=0x400860a8 size=0x01f4c ( 8012)
  149. 0x400860a8: prvAddNewTaskToReadyList at esp-idf/esp-idf/components/freertos/tasks.c:4561
  150. I (722) flash_encrypt: Encrypting partition 2 at offset 0x20000...
  151. I (13229) flash_encrypt: Flash encryption completed
  152. I (13229) boot: Resetting with flash encryption enabled...
  153. Once the flash encryption is enabled, on subsequent boot the output would mention that flash encryption is already enabled.
  154. ::
  155. rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  156. configsip: 0, SPIWP:0xee
  157. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  158. mode:DIO, clock div:2
  159. load:0x3fff0018,len:4
  160. load:0x3fff001c,len:8452
  161. load:0x40078000,len:13652
  162. ho 0 tail 12 room 4
  163. load:0x40080400,len:6664
  164. entry 0x40080764
  165. I (30) boot: ESP-IDF v4.0-dev-850-gc4447462d-dirty 2nd stage bootloader
  166. I (30) boot: compile time 16:32:53
  167. I (31) boot: Enabling RNG early entropy source...
  168. I (37) boot: SPI Speed : 40MHz
  169. I (41) boot: SPI Mode : DIO
  170. I (45) boot: SPI Flash Size : 4MB
  171. I (49) boot: Partition Table:
  172. I (52) boot: ## Label Usage Type ST Offset Length
  173. I (60) boot: 0 nvs WiFi data 01 02 0000a000 00006000
  174. I (67) boot: 1 phy_init RF data 01 01 00010000 00001000
  175. I (75) boot: 2 factory factory app 00 00 00020000 00100000
  176. I (82) boot: End of partition table
  177. I (86) esp_image: segment 0: paddr=0x00020020 vaddr=0x3f400020 size=0x0808c ( 32908) map
  178. I (107) esp_image: segment 1: paddr=0x000280b4 vaddr=0x3ffb0000 size=0x01ea4 ( 7844) load
  179. I (111) esp_image: segment 2: paddr=0x00029f60 vaddr=0x40080000 size=0x00400 ( 1024) load
  180. 0x40080000: _WindowOverflow4 at esp-idf/esp-idf/components/freertos/xtensa_vectors.S:1778
  181. I (116) esp_image: segment 3: paddr=0x0002a368 vaddr=0x40080400 size=0x05ca8 ( 23720) load
  182. I (134) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x126a8 ( 75432) map
  183. 0x400d0018: _flash_cache_start at ??:?
  184. I (162) esp_image: segment 5: paddr=0x000426c8 vaddr=0x400860a8 size=0x01f4c ( 8012) load
  185. 0x400860a8: prvAddNewTaskToReadyList at esp-idf/esp-idf/components/freertos/tasks.c:4561
  186. I (171) boot: Loaded app from partition at offset 0x20000
  187. I (171) boot: Checking flash encryption...
  188. I (171) flash_encrypt: flash encryption is enabled (3 plaintext flashes left)
  189. I (178) boot: Disabling RNG early entropy source...
  190. I (184) cpu_start: Pro cpu up.
  191. I (188) cpu_start: Application information:
  192. I (193) cpu_start: Project name: flash-encryption
  193. I (198) cpu_start: App version: v4.0-dev-850-gc4447462d-dirty
  194. I (205) cpu_start: Compile time: Jun 17 2019 16:32:52
  195. I (211) cpu_start: ELF file SHA256: 8770c886bdf561a7...
  196. I (217) cpu_start: ESP-IDF: v4.0-dev-850-gc4447462d-dirty
  197. I (224) cpu_start: Starting app cpu, entry point is 0x40080e4c
  198. 0x40080e4c: call_start_cpu1 at esp-idf/esp-idf/components/{IDF_TARGET_PATH_NAME}/cpu_start.c:265
  199. I (0) cpu_start: App cpu up.
  200. I (235) heap_init: Initializing. RAM available for dynamic allocation:
  201. I (241) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
  202. I (247) heap_init: At 3FFB2EC8 len 0002D138 (180 KiB): DRAM
  203. I (254) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
  204. I (260) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
  205. I (266) heap_init: At 40087FF4 len 0001800C (96 KiB): IRAM
  206. I (273) cpu_start: Pro cpu start user code
  207. I (291) cpu_start: Starting scheduler on PRO CPU.
  208. I (0) cpu_start: Starting scheduler on APP CPU.
  209. Sample program to check Flash Encryption
  210. This is {IDF_TARGET_NAME} chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
  211. Flash encryption feature is enabled
  212. Flash encryption mode is DEVELOPMENT
  213. Flash in encrypted mode with flash_crypt_cnt = 1
  214. Halting...
  215. At this stage if user wants to update modified plaintext application image to flash in encrypted format it can be done using following command:
  216. ::
  217. idf.py encrypted-app-flash monitor
  218. .. _encrypt_partitions:
  219. Encrypt Multiple Partitions
  220. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  221. If all partitions needs to be updated in encrypted format, it can be done as
  222. ::
  223. idf.py encrypted-flash monitor
  224. .. _pregenerated-flash-encryption-key:
  225. Using Host Generated Flash Encryption Key
  226. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  227. It is possible to pregenerate the flash encryption key on the host computer and burn it into the {IDF_TARGET_NAME}'s eFuse key block. This allows data to be pre-encrypted on the host and flashed to the {IDF_TARGET_NAME} without needing a plaintext flash update. This feature allows encrypted flashing in both :ref:`flash_enc_development_mode` and :ref:`flash_enc_release_mode` modes.
  228. .. note:: This option is not recommended for production unless a separate key is generated for each individual device.
  229. - Ensure you have a {IDF_TARGET_NAME} device with default flash encryption eFuse settings as shown in :ref:`flash-encryption-efuse`.
  230. - Generate a random key with espsecure.py::
  231. espsecure.py generate_flash_encryption_key my_flash_encryption_key.bin
  232. - Burn the key to the device (one time only). **This must be done before first encrypted boot**, otherwise the {IDF_TARGET_NAME} will generate a random key that software can't access or modify::
  233. espefuse.py --port PORT burn_key flash_encryption my_flash_encryption_key.bin
  234. - Enable flash encryption support in second stage bootloader. In :ref:`project-configuration-menu`, navigate to "Security Features".
  235. - Select :ref:`Enable flash encryption on boot <CONFIG_SECURE_FLASH_ENC_ENABLED>`.
  236. - By default the mode is set for **Development**.
  237. - Select appropriate Bootloader log verbosity under Bootloader config.
  238. .. only:: esp32
  239. - Update to the partition table offset may be required since after enabling flash encryption the size of bootloader is increased. See :ref:`secure-boot-bootloader-size`
  240. - Save the configuration and exit.
  241. Build and flash the complete image including: bootloader, partition table and app. These partitions are initially written to the flash unencrypted
  242. ::
  243. idf.py flash monitor
  244. On next boot second stage bootloader will encrypt the flash app partition and then reset. Now the sample application would get decrypted at runtime and executed.
  245. At this stage if user wants to update new plaintext application image to flash they should issue following command
  246. ::
  247. idf.py encrypted-app-flash monitor
  248. For reprogramming all partitions in encrypted format follow :ref:`encrypt_partitions`.
  249. .. _flash_enc_release_mode:
  250. Release Mode
  251. ^^^^^^^^^^^^
  252. In Release mode UART bootloader can not perform flash encryption operations and new plaintext images can be downloaded ONLY using OTA scheme which will encrypt the plaintext image before writing to flash.
  253. - Ensure you have a {IDF_TARGET_NAME} device with default flash encryption eFuse settings as shown in :ref:`flash-encryption-efuse`.
  254. - Enable flash encryption support in second stage bootloader. In :ref:`project-configuration-menu`, navigate to "Security Features".
  255. - Select :ref:`Enable flash encryption on boot <CONFIG_SECURE_FLASH_ENC_ENABLED>`.
  256. - Select **Release Mode**, by default the mode is set for **Development**. Please note **once the Release mode is selected the ``download_dis_encrypt`` and ``download_dis_decrypt`` eFuse bits will be programmed to disable UART bootloader access to flash contents**.
  257. - Select appropriate Bootloader log verbosity under Bootloader config.
  258. .. only:: esp32
  259. - Update to the partition table offset may be required since after enabling flash encryption the size of bootloader is increased. See :ref:`secure-boot-bootloader-size`
  260. - Save the configuration and exit.
  261. Build and flash the complete image including: bootloader, partition table and app. These partitions are initially written to the flash unencrypted
  262. ::
  263. idf.py flash monitor
  264. On next boot second stage bootloader will encrypt the flash app partition and then reset. Now the sample application should execute correctly.
  265. Once the flash encryption is enabled in Release mode the bootloader will write protect the ``FLASH_CRYPT_CNT`` eFuse.
  266. For subsequent plaintext update in field :ref:`OTA scheme <updating-encrypted-flash-ota>` should be used.
  267. Possible Failures
  268. ^^^^^^^^^^^^^^^^^
  269. Once flash encryption is enabled and if the ``FLASH_CRYPT_CNT`` eFuse value has an odd number of bits set then all the partitions (which are marked with encryption flag) are expected to contain encrypted ciphertext. Below are three typical failure cases if the {IDF_TARGET_NAME} is loaded with plaintext data:
  270. 1. In case the bootloader partition is re-updated with plaintext bootloader image the ROM loader will fail to load the bootloader and following type of failure will be displayed:
  271. ::
  272. rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  273. flash read err, 1000
  274. ets_main.c 371
  275. ets Jun 8 2016 00:22:57
  276. rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  277. flash read err, 1000
  278. ets_main.c 371
  279. ets Jun 8 2016 00:22:57
  280. rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  281. flash read err, 1000
  282. ets_main.c 371
  283. ets Jun 8 2016 00:22:57
  284. rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  285. flash read err, 1000
  286. ets_main.c 371
  287. ets Jun 8 2016 00:22:57
  288. rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  289. flash read err, 1000
  290. ets_main.c 371
  291. ets Jun 8 2016 00:22:57
  292. .. note:: This error also appears in the flash contents is erased or corrupted.
  293. 2. In case the bootloader is encrypted but partition table is re-updated with plaintext partition table image the bootloader will fail to read the partition table and following type of failure will be displayed:
  294. ::
  295. rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  296. configsip: 0, SPIWP:0xee
  297. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  298. mode:DIO, clock div:2
  299. load:0x3fff0018,len:4
  300. load:0x3fff001c,len:10464
  301. ho 0 tail 12 room 4
  302. load:0x40078000,len:19168
  303. load:0x40080400,len:6664
  304. entry 0x40080764
  305. I (60) boot: ESP-IDF v4.0-dev-763-g2c55fae6c-dirty 2nd stage bootloader
  306. I (60) boot: compile time 19:15:54
  307. I (62) boot: Enabling RNG early entropy source...
  308. I (67) boot: SPI Speed : 40MHz
  309. I (72) boot: SPI Mode : DIO
  310. I (76) boot: SPI Flash Size : 4MB
  311. E (80) flash_parts: partition 0 invalid magic number 0x94f6
  312. E (86) boot: Failed to verify partition table
  313. E (91) boot: load partition table error!
  314. 3. In case the bootloader & partition table are encrypted but application is re-updated with plaintext application image the bootloader will fail load the new application and following type of failure will be displayed:
  315. ::
  316. rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  317. configsip: 0, SPIWP:0xee
  318. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  319. mode:DIO, clock div:2
  320. load:0x3fff0018,len:4
  321. load:0x3fff001c,len:8452
  322. load:0x40078000,len:13616
  323. load:0x40080400,len:6664
  324. entry 0x40080764
  325. I (56) boot: ESP-IDF v4.0-dev-850-gc4447462d-dirty 2nd stage bootloader
  326. I (56) boot: compile time 15:37:14
  327. I (58) boot: Enabling RNG early entropy source...
  328. I (64) boot: SPI Speed : 40MHz
  329. I (68) boot: SPI Mode : DIO
  330. I (72) boot: SPI Flash Size : 4MB
  331. I (76) boot: Partition Table:
  332. I (79) boot: ## Label Usage Type ST Offset Length
  333. I (87) boot: 0 nvs WiFi data 01 02 0000a000 00006000
  334. I (94) boot: 1 phy_init RF data 01 01 00010000 00001000
  335. I (102) boot: 2 factory factory app 00 00 00020000 00100000
  336. I (109) boot: End of partition table
  337. E (113) esp_image: image at 0x20000 has invalid magic byte
  338. W (120) esp_image: image at 0x20000 has invalid SPI mode 108
  339. W (126) esp_image: image at 0x20000 has invalid SPI size 11
  340. E (132) boot: Factory app partition is not bootable
  341. E (138) boot: No bootable app partitions in the partition table
  342. Key Points About Flash Encryption
  343. ---------------------------------
  344. - The contents of the flash are encrypted using AES-256. The flash encryption key is stored in eFuse internal to the chip, and is (by default) protected from software access.
  345. - The `flash encryption algorithm` is AES-256, where the key is "tweaked" with the offset address of each 32 byte block of flash. This means every 32 byte block (two consecutive 16 byte AES blocks) is encrypted with a unique key derived from the flash encryption key.
  346. - Flash access is transparent via the flash cache mapping feature of {IDF_TARGET_NAME} - any flash regions which are mapped to the address space will be transparently decrypted when read.
  347. It may be desirable for some data partitions to remain unencrypted for ease of access, or to use flash-friendly update algorithms that are ineffective if the data is encrypted. NVS partitions for non-volatile storage cannot be encrypted since NVS library is not directly compatible with flash encryption. Refer to :ref:`NVS Encryption <nvs_encryption>` for more details.
  348. - If flash encryption may be enabled, the programmer must take certain precautions when writing code that :ref:`uses encrypted flash <using-encrypted-flash>`.
  349. - If secure boot is enabled, reflashing the bootloader of an encrypted device requires a "Reflashable" secure boot digest (see :ref:`flash-encryption-and-secure-boot`).
  350. .. only:: esp32
  351. .. note:: The bootloader app binary ``bootloader.bin`` may become too large when both secure boot and flash encryption are enabled. See :ref:`secure-boot-bootloader-size`.
  352. .. important::
  353. Do not interrupt power to the {IDF_TARGET_NAME} while the first boot encryption pass is running. If power is interrupted, the flash contents will be corrupted and require flashing with unencrypted data again. A reflash like this will not count towards the flashing limit.
  354. .. _using-encrypted-flash:
  355. Using Encrypted Flash
  356. ---------------------
  357. {IDF_TARGET_NAME} app code can check if flash encryption is currently enabled by calling :cpp:func:`esp_flash_encryption_enabled`. Also, device can identify the flash encryption mode by calling :cpp:func:`esp_get_flash_encryption_mode`.
  358. Once flash encryption is enabled, some care needs to be taken when accessing flash contents from code.
  359. Scope of Flash Encryption
  360. ^^^^^^^^^^^^^^^^^^^^^^^^^
  361. Whenever the ``FLASH_CRYPT_CNT`` eFuse is set to a value with an odd number of bits set, all flash content which is accessed via the MMU's flash cache is transparently decrypted. This includes:
  362. - Executable application code in flash (IROM).
  363. - All read-only data stored in flash (DROM).
  364. - Any data accessed via :cpp:func:`spi_flash_mmap`.
  365. - The software bootloader image when it is read by the ROM bootloader.
  366. .. important::
  367. The MMU flash cache unconditionally decrypts all data. Data which is stored unencrypted in the flash will be "transparently decrypted" via the flash cache and appear to software like random garbage.
  368. Reading Encrypted Flash
  369. ^^^^^^^^^^^^^^^^^^^^^^^
  370. To read data without using a flash cache MMU mapping, we recommend using the partition read function :cpp:func:`esp_partition_read`. When using this function, data will only be decrypted when it is read from an encrypted partition. Other partitions will be read unencrypted. In this way, software can access encrypted and non-encrypted flash in the same way.
  371. Data which is read via other SPI read APIs are not decrypted:
  372. - Data read via :cpp:func:`spi_flash_read` is not decrypted.
  373. - Data read via ROM function :cpp:func:`SPIRead` is not decrypted (this function is not supported in esp-idf apps).
  374. - Data stored using the Non-Volatile Storage (NVS) API is always stored and read decrypted from the perspective of flash encryption. It is up to the library to provide encryption feature if required. Refer to :ref:`NVS Encryption <nvs_encryption>` for more details.
  375. Writing Encrypted Flash
  376. ^^^^^^^^^^^^^^^^^^^^^^^
  377. Where possible, we recommend using the partition write function ``esp_partition_write``. When using this function, data will only be encrypted when writing to encrypted partitions. Data will be written to other partitions unencrypted. In this way, software can access encrypted and non-encrypted flash in the same way.
  378. The ``esp_spi_flash_write`` function will write data when the write_encrypted parameter is set to true. Otherwise, data will be written unencrypted.
  379. The ROM function ``esp_rom_spiflash_write_encrypted`` will write encrypted data to flash, the ROM function ``SPIWrite`` will write unencrypted to flash. (these function are not supported in esp-idf apps).
  380. Because data is encrypted in blocks, the minimum write size for encrypted data is 16 bytes (and the alignment is 16 bytes).
  381. .. _updating-encrypted-flash:
  382. Updating Encrypted Flash
  383. ------------------------
  384. .. _updating-encrypted-flash-ota:
  385. OTA Updates
  386. ^^^^^^^^^^^
  387. OTA updates to encrypted partitions will automatically write encrypted, as long as the ``esp_partition_write`` function is used.
  388. Any app image which will be OTA updated onto a device with flash encryption enabled requires :ref:`Enable flash encryption on boot <CONFIG_SECURE_FLASH_ENC_ENABLED>` option to be enabled in the app configuration as well, when building the app.
  389. Please refer to :doc:`OTA <../api-reference/system/ota>` for general information about ESP-IDF OTA updates.
  390. .. _updating-encrypted-flash-serial:
  391. Disabling Flash Encryption
  392. --------------------------
  393. If you've accidentally enabled flash encryption for some reason, the next flash of plaintext data will soft-brick the {IDF_TARGET_NAME} (the device will reboot continuously, printing the error ``flash read err, 1000``).
  394. If flash encryption is enabled in Development mode, you can disable flash encryption again by writing ``FLASH_CRYPT_CNT`` eFuse. This can only be done three times per chip.
  395. - First, open :ref:`project-configuration-menu` and disable :ref:`Enable flash encryption boot <CONFIG_SECURE_FLASH_ENC_ENABLED>` under "Security Features".
  396. - Exit menuconfig and save the new configuration.
  397. - Run ``idf.py menuconfig`` again and double-check you really disabled this option! *If this option is left enabled, the bootloader will immediately re-enable encryption when it boots*.
  398. - Run ``idf.py flash`` to build and flash a new bootloader and app, without flash encryption enabled.
  399. - Run ``espefuse.py`` (in ``components/esptool_py/esptool``) to disable the FLASH_CRYPT_CNT::
  400. espefuse.py burn_efuse FLASH_CRYPT_CNT
  401. Reset the {IDF_TARGET_NAME} and flash encryption should be disabled, the bootloader will boot as normal.
  402. .. _flash-encryption-limitations:
  403. Limitations of Flash Encryption
  404. -------------------------------
  405. Flash encryption prevents plaintext readout of the encrypted flash, to protect firmware against unauthorised readout and modification. It is important to understand the limitations of the flash encryption system:
  406. - Flash encryption is only as strong as the key. For this reason, we recommend keys are generated on the device during first boot (default behaviour). If generating keys off-device, ensure proper procedure is followed and don't share the same key between all production devices.
  407. - Not all data is stored encrypted. If storing data on flash, check if the method you are using (library, API, etc.) supports flash encryption.
  408. - Flash encryption does not prevent an attacker from understanding the high-level layout of the flash. This is because the same AES key is used for every pair of adjacent 16 byte AES blocks. When these adjacent 16 byte blocks contain identical content (such as empty or padding areas), these blocks will encrypt to produce matching pairs of encrypted blocks. This may allow an attacker to make high-level comparisons between encrypted devices (i.e. to tell if two devices are probably running the same firmware version).
  409. - For the same reason, an attacker can always tell when a pair of adjacent 16 byte blocks (32 byte aligned) contain two identical 16 byte sequences. Keep this in mind if storing sensitive data on the flash, design your flash storage so this doesn't happen (using a counter byte or some other non-identical value every 16 bytes is sufficient). :ref:`NVS Encryption <nvs_encryption>` deals with this and is suitable for many uses.
  410. .. only:: esp32
  411. - Flash encryption alone may not prevent an attacker from modifying the firmware of the device. To prevent unauthorised firmware from running on the device, use flash encryption in combination with :doc:`Secure Boot <secure-boot-v2>`.
  412. .. _flash-encryption-and-secure-boot:
  413. Flash Encryption and Secure Boot
  414. ---------------------------------
  415. It is recommended to use flash encryption and secure boot together. However, if Secure Boot is enabled then additional restrictions apply to reflashing the device:
  416. - :ref:`updating-encrypted-flash-ota` are not restricted (provided the new app is signed correctly with the Secure Boot signing key).
  417. .. only:: esp32
  418. - :ref:`Plaintext serial flash updates <updating-encrypted-flash-serial>` are only possible if the :ref:`Reflashable <CONFIG_SECURE_BOOTLOADER_MODE>` Secure Boot mode is selected and a Secure Boot key was pre-generated and burned to the {IDF_TARGET_NAME} (refer to :ref:`Secure Boot <secure-boot-reflashable>` docs.). In this configuration, ``idf.py bootloader`` will produce a pre-digested bootloader and secure boot digest file for flashing at offset 0x0. When following the plaintext serial reflashing steps it is necessary to re-flash this file before flashing other plaintext data.
  419. - :ref:`Reflashing via Pregenerated Flash Encryption Key <pregenerated-flash-encryption-key>` is still possible, provided the bootloader is not reflashed. Reflashing the bootloader requires the same :ref:`Reflashable <CONFIG_SECURE_BOOTLOADER_MODE>` option to be enabled in the Secure Boot config.
  420. .. _flash-encryption-advanced-features:
  421. Advanced Features
  422. -----------------
  423. The following information is useful for advanced use of flash encryption:
  424. Encrypted Partition Flag
  425. ^^^^^^^^^^^^^^^^^^^^^^^^
  426. Some partitions are encrypted by default. Otherwise, it is possible to mark any partition as requiring encryption:
  427. In the :doc:`partition table <../api-guides/partition-tables>` description CSV files, there is a field for flags.
  428. Usually left blank, if you write "encrypted" in this field then the partition will be marked as encrypted in the partition table, and data written here will be treated as encrypted (same as an app partition)::
  429. # Name, Type, SubType, Offset, Size, Flags
  430. nvs, data, nvs, 0x9000, 0x6000
  431. phy_init, data, phy, 0xf000, 0x1000
  432. factory, app, factory, 0x10000, 1M
  433. secret_data, 0x40, 0x01, 0x20000, 256K, encrypted
  434. - None of the default partition tables include any encrypted data partitions.
  435. - It is not necessary to mark "app" partitions as encrypted, they are always treated as encrypted.
  436. - The "encrypted" flag does nothing if flash encryption is not enabled.
  437. - It is possible to mark the optional ``phy`` partition with ``phy_init`` data as encrypted, if you wish to protect this data from physical access readout or modification.
  438. - It is not possible to mark the ``nvs`` partition as encrypted.
  439. .. _uart-bootloader-encryption:
  440. Enabling UART Bootloader Encryption/Decryption
  441. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  442. By default, on first boot the flash encryption process will burn eFuses ``DISABLE_DL_ENCRYPT``, ``DISABLE_DL_DECRYPT`` and ``DISABLE_DL_CACHE``:
  443. - ``DISABLE_DL_ENCRYPT`` disables the flash encryption operations when running in UART bootloader boot mode.
  444. - ``DISABLE_DL_DECRYPT`` disables transparent flash decryption when running in UART bootloader mode, even if FLASH_CRYPT_CNT is set to enable it in normal operation.
  445. - ``DISABLE_DL_CACHE`` disables the entire MMU flash cache when running in UART bootloader mode.
  446. It is possible to burn only some of these eFuses, and write-protect the rest (with unset value 0) before the first boot, in order to preserve them. For example::
  447. espefuse.py --port PORT burn_efuse DISABLE_DL_DECRYPT
  448. espefuse.py --port PORT write_protect_efuse DISABLE_DL_ENCRYPT
  449. (Note that all 3 of these eFuses are disabled via one write protect bit, so write protecting one will write protect all of them. For this reason, it's necessary to set any bits before write-protecting.)
  450. .. important::
  451. Write protecting these eFuses to keep them unset is not currently very useful, as ``esptool.py`` does not support reading encrypted flash.
  452. .. important::
  453. If ``DISABLE_DL_DECRYPT`` is left unset (0) this effectively makes flash encryption useless, as an attacker with physical access can use UART bootloader mode (with custom stub code) to read out the flash contents.
  454. .. _setting-flash-crypt-config:
  455. Setting FLASH_CRYPT_CONFIG
  456. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  457. The ``FLASH_CRYPT_CONFIG`` eFuse determines the number of bits in the flash encryption key which are "tweaked" with the block offset. See :ref:`flash-encryption-algorithm` for details.
  458. First boot of the bootloader always sets this value to the maximum `0xF`.
  459. It is possible to write these eFuse manually, and write protect it before first boot in order to select different tweak values. This is not recommended.
  460. It is strongly recommended to never write protect ``FLASH_CRYPT_CONFIG`` when it the value is zero. If this eFuse is set to zero, no bits in the flash encryption key are tweaked and the flash encryption algorithm is equivalent to AES ECB mode.
  461. JTAG Debugging
  462. ^^^^^^^^^^^^^^
  463. By default, when Flash Encryption is enabled (in either Development or Release mode) then JTAG debugging is disabled via eFuse. The bootloader does this on first boot, at the same time it enables flash encryption.
  464. See :ref:`jtag-debugging-security-features` for more information about using JTAG Debugging with Flash Encryption.
  465. Technical Details
  466. -----------------
  467. The following sections provide some reference information about the operation of flash encryption.
  468. .. _flash-encryption-algorithm:
  469. Flash Encryption Algorithm
  470. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  471. - AES-256 operates on 16 byte blocks of data. The flash encryption engine encrypts and decrypts data in 32 byte blocks, two AES blocks in series.
  472. - The main flash encryption key is stored in eFuse (BLOCK1) and by default is protected from further writes or software readout.
  473. - AES-256 key size is 256 bits (32 bytes), read from eFuse block 1. The hardware AES engine uses the key in reversed byte order to the order stored in the eFuse block.
  474. - If ``CODING_SCHEME`` eFuse is set to 0 (default "None" Coding Scheme) then the eFuse key block is 256 bits and the key is stored as-is (in reversed byte order).
  475. - If ``CODING_SCHEME`` eFuse is set to 1 (3/4 Encoding) then the eFuse key block is 192 bits (in reversed byte order), so overall entropy is reduced. The hardware flash encryption still operates on a 256-bit key, after being read (and un-reversed), the key is extended by as ``key = key[0:255] + key[64:127]``.
  476. - AES algorithm is used inverted in flash encryption, so the flash encryption "encrypt" operation is AES decrypt and the "decrypt" operation is AES encrypt. This is for performance reasons and does not alter the effectiveness of the algorithm.
  477. - Each 32 byte block (two adjacent 16 byte AES blocks) is encrypted with a unique key. The key is derived from the main flash encryption key in eFuse, XORed with the offset of this block in the flash (a "key tweak").
  478. - The specific tweak depends on the setting of ``FLASH_CRYPT_CONFIG`` eFuse. This is a 4 bit eFuse, where each bit enables XORing of a particular range of the key bits:
  479. - Bit 1, bits 0-66 of the key are XORed.
  480. - Bit 2, bits 67-131 of the key are XORed.
  481. - Bit 3, bits 132-194 of the key are XORed.
  482. - Bit 4, bits 195-256 of the key are XORed.
  483. It is recommended that ``FLASH_CRYPT_CONFIG`` is always left to set the default value `0xF`, so that all key bits are XORed with the block offset. See :ref:`setting-flash-crypt-config` for details.
  484. - The high 19 bits of the block offset (bit 5 to bit 23) are XORed with the main flash encryption key. This range is chosen for two reasons: the maximum flash size is 16MB (24 bits), and each block is 32 bytes so the least significant 5 bits are always zero.
  485. - There is a particular mapping from each of the 19 block offset bits to the 256 bits of the flash encryption key, to determine which bit is XORed with which. See the variable ``_FLASH_ENCRYPTION_TWEAK_PATTERN`` in the ``espsecure.py`` source code for the complete mapping.
  486. - To see the full flash encryption algorithm implemented in Python, refer to the `_flash_encryption_operation()` function in the ``espsecure.py`` source code.