host-based-security-workflows.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. Host Based Security Workflows
  2. =============================
  3. {IDF_TARGET_CRYPT_CNT:default="SPI_BOOT_CRYPT_CNT",esp32="FLASH_CRYPT_CNT"}
  4. {IDF_TARGET_CRYPT_CNT_MAX_VAL:default="7",esp32="127"}
  5. Introduction
  6. ------------
  7. It is recommended to have an uninterrupted power supply while enabling security features on ESP32 SoCs. Power failures during secure manufacturing process could cause issues that are hard to debug and, in some cases, may cause permanent boot-up failures.
  8. This guide highlights an approach where security features are enabled with the assistance of an external host machine. Security workflows are broken down into various stages and key material is generated on the host machine; thus, allowing greater recovery chances in case of power or other failures. It also offers better timings for secure manufacturing, e.g., case of encryption of firmware on host machine vs on the device.
  9. Goals
  10. -----
  11. #. Simplify the traditional workflow with stepwise instructions.
  12. #. Design a more flexible workflow as compared to the traditional firmware based workflow.
  13. #. Improve reliability by dividing the workflow in small operations.
  14. #. Eliminate dependency on :ref:`second-stage-bootloader` (firmware bootloader).
  15. Pre-requisite
  16. -------------
  17. * ``esptool``: Please make sure the ``esptool`` has been installed. It can be installed with help of the following command:
  18. .. code:: bash
  19. pip install esptool
  20. Scope
  21. -----
  22. * :ref:`enable-flash-encryption-externally`
  23. Security Workflows
  24. ------------------
  25. .. _enable-flash-encryption-externally:
  26. Enable Flash Encryption Externally
  27. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  28. In this case all the eFuses related to flash encryption are written with help of the espefuse tool. More details about flash encryption can process can be found in the :doc:`Flash Encryption Guide </security/flash-encryption>`
  29. 1. Ensure that you have an {IDF_TARGET_NAME} device with default flash encryption eFuse settings as shown in :ref:`flash-encryption-efuse`.
  30. See how to check :ref:`flash-encryption-status`.
  31. In this case the Flash on the chip must be erased and flash encryption must not be enabled.
  32. The chip can be erased using the following command:
  33. .. code:: bash
  34. esptool.py --port PORT erase_flash
  35. 2. Generate a random flash encryption key by running:
  36. .. only:: not SOC_FLASH_ENCRYPTION_XTS_AES
  37. .. code-block:: bash
  38. espsecure.py generate_flash_encryption_key my_flash_encryption_key.bin
  39. .. only:: SOC_FLASH_ENCRYPTION_XTS_AES_256
  40. If :ref:`Size of generated AES-XTS key <CONFIG_SECURE_FLASH_ENCRYPTION_KEYSIZE>` is AES-128 (256-bit key):
  41. .. code-block:: bash
  42. espsecure.py generate_flash_encryption_key my_flash_encryption_key.bin
  43. else if :ref:`Size of generated AES-XTS key <CONFIG_SECURE_FLASH_ENCRYPTION_KEYSIZE>` is AES-256 (512-bit key):
  44. .. code-block:: bash
  45. espsecure.py generate_flash_encryption_key --keylen 512 my_flash_encryption_key.bin
  46. .. only:: SOC_FLASH_ENCRYPTION_XTS_AES_128 and not SOC_FLASH_ENCRYPTION_XTS_AES_256 and not SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
  47. .. code-block:: bash
  48. espsecure.py generate_flash_encryption_key my_flash_encryption_key.bin
  49. .. only:: SOC_FLASH_ENCRYPTION_XTS_AES_128 and SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
  50. If :ref:`Size of generated AES-XTS key <CONFIG_SECURE_FLASH_ENCRYPTION_KEYSIZE>` is AES-128 (256-bit key):
  51. .. code-block:: bash
  52. espsecure.py generate_flash_encryption_key my_flash_encryption_key.bin
  53. else if :ref:`Size of generated AES-XTS key <CONFIG_SECURE_FLASH_ENCRYPTION_KEYSIZE>` is AES-128 key derived from 128 bits (SHA256(128 bits)):
  54. .. code-block:: bash
  55. espsecure.py generate_flash_encryption_key --keylen 128 my_flash_encryption_key.bin
  56. 3. Burn the key into your device's eFuse using the command below. This action can be done **only once**.
  57. .. only:: not SOC_FLASH_ENCRYPTION_XTS_AES
  58. .. code-block:: bash
  59. espefuse.py --port PORT burn_key flash_encryption my_flash_encryption_key.bin
  60. .. only:: SOC_FLASH_ENCRYPTION_XTS_AES_256
  61. .. code-block:: bash
  62. espefuse.py --port PORT burn_key BLOCK my_flash_encryption_key.bin KEYPURPOSE
  63. where ``BLOCK`` is a free keyblock between ``BLOCK_KEY0`` and ``BLOCK_KEY5``. And ``KEYPURPOSE`` is either ``AES_256_KEY_1``, ``XTS_AES_256_KEY_2``, ``XTS_AES_128_KEY``. See `{IDF_TARGET_NAME} Technical Reference Manual <{IDF_TARGET_TRM_EN_URL}>`_ for a description of the key purposes.
  64. For AES-128 (256-bit key) - ``XTS_AES_128_KEY``:
  65. .. code-block:: bash
  66. espefuse.py --port PORT burn_key BLOCK my_flash_encryption_key.bin XTS_AES_128_KEY
  67. For AES-256 (512-bit key) - ``XTS_AES_256_KEY_1`` and ``XTS_AES_256_KEY_2``. ``espefuse.py`` supports burning both these two key purposes together with a 512 bit key to two separate key blocks via the virtual key purpose ``XTS_AES_256_KEY``. When this is used ``espefuse.py`` will burn the first 256 bit of the key to the specified ``BLOCK`` and burn the corresponding block key purpose to ``XTS_AES_256_KEY_1``. The last 256 bit of the key will be burned to the first free key block after ``BLOCK`` and the corresponding block key purpose to ``XTS_AES_256_KEY_2``
  68. .. code-block:: bash
  69. espefuse.py --port PORT burn_key BLOCK my_flash_encryption_key.bin XTS_AES_256_KEY
  70. If you wish to specify exactly which two blocks are used then it is possible to divide key into two 256 bit keys, and manually burn each half with ``XTS_AES_256_KEY_1`` and ``XTS_AES_256_KEY_2`` as key purposes:
  71. .. code-block:: bash
  72. split -b 32 my_flash_encryption_key.bin my_flash_encryption_key.bin
  73. espefuse.py --port PORT burn_key BLOCK my_flash_encryption_key.bin.aa XTS_AES_256_KEY_1
  74. espefuse.py --port PORT burn_key BLOCK+1 my_flash_encryption_key.bin.ab XTS_AES_256_KEY_2
  75. .. only:: SOC_FLASH_ENCRYPTION_XTS_AES_128 and not SOC_FLASH_ENCRYPTION_XTS_AES_256 and not SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
  76. .. code-block:: bash
  77. espefuse.py --port PORT burn_key BLOCK my_flash_encryption_key.bin XTS_AES_128_KEY
  78. where ``BLOCK`` is a free keyblock between ``BLOCK_KEY0`` and ``BLOCK_KEY5``.
  79. .. only:: SOC_FLASH_ENCRYPTION_XTS_AES_128 and SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
  80. For AES-128 (256-bit key) - ``XTS_AES_128_KEY`` (the ``XTS_KEY_LENGTH_256`` eFuse will be burn to 1):
  81. .. code-block:: bash
  82. espefuse.py --port PORT burn_key BLOCK_KEY0 flash_encryption_key256.bin XTS_AES_128_KEY
  83. For AES-128 key derived from 128 bits (SHA256(128 bits)) - ``XTS_AES_128_KEY_DERIVED_FROM_128_EFUSE_BITS``. The FE key will be written in the lower part of eFuse BLOCK_KEY0. The upper 128 bits are not used and will remain available for reading by software. Using the special mode of the espefuse tool, shown in the ``For burning both keys together`` section below, the user can write their data to it using any espefuse commands.
  84. .. code-block:: bash
  85. espefuse.py --port PORT burn_key BLOCK_KEY0 flash_encryption_key128.bin XTS_AES_128_KEY_DERIVED_FROM_128_EFUSE_BITS
  86. For burning both keys together (Secure Boot and Flash Encryption):
  87. .. code-block:: bash
  88. espefuse.py --port PORT --chip esp32c2 burn_key_digest secure_boot_signing_key.pem \
  89. burn_key BLOCK_KEY0 flash_encryption_key128.bin XTS_AES_128_KEY_DERIVED_FROM_128_EFUSE_BITS
  90. .. only:: SOC_EFUSE_BLOCK9_KEY_PURPOSE_QUIRK
  91. .. note::
  92. **WARNING**: For the {IDF_TARGET_NAME} BLOCK9 (BLOCK_KEY5) can not be used by XTS_AES keys.
  93. 4. Burn the ``{IDF_TARGET_CRYPT_CNT}`` eFuse.
  94. If you only want to enable flash encryption in **Development** mode and want to keep the ability to disable it in future, Update the {IDF_TARGET_CRYPT_CNT} value in the below command from {IDF_TARGET_CRYPT_CNT_MAX_VAL} to 0x1. (not recommended for production)
  95. .. code-block:: bash
  96. espefuse.py --port PORT --chip {IDF_TARGET_PATH_NAME} burn_efuse {IDF_TARGET_CRYPT_CNT} {IDF_TARGET_CRYPT_CNT_MAX_VAL}
  97. .. only:: esp32
  98. In case of {IDF_TARGET_NAME}, you also need to burn the ``FLASH_CRYPT_CONFIG``. It can be done with help of the following command:
  99. .. code-block:: bash
  100. espefuse.py --port PORT --chip {IDF_TARGET_PATH_NAME} burn_efuse FLASH_CRYPT_CONFIG 0xF
  101. .. note::
  102. At this point the flash encryption on the device has been enabled. You may test the flash encryption process as given in step 5. Please note that the security related eFuses have not been burned at this point. It is recommended that they should be burned in production use-cases as explained in step 6.
  103. 5. Encrypt and flash the binaries
  104. The bootloader and the application binaries for the project must be built with Flash Encryption Release mode with default configurations.
  105. Flash encryption Release mode can be set in the menuconfig as follows:
  106. .. list::
  107. - :ref:`Enable flash encryption on boot <CONFIG_SECURE_FLASH_ENC_ENABLED>`
  108. :esp32: - :ref:`Select Release mode <CONFIG_SECURE_FLASH_ENCRYPTION_MODE>` (Note that once Release mode is selected, the ``DISABLE_DL_ENCRYPT`` and ``DISABLE_DL_DECRYPT`` eFuse bits will be burned to disable flash encryption hardware in ROM Download Mode.)
  109. :esp32: - :ref:`Select UART ROM download mode (Permanently disabled (recommended)) <CONFIG_SECURE_UART_ROM_DL_MODE>` (Note that this option is only available when :ref:`CONFIG_ESP32_REV_MIN` is set to 3 (ESP32 V3).) The default choice is to keep UART ROM download mode enabled, however it's recommended to permanently disable this mode to reduce the options available to an attacker.
  110. :not esp32: - :ref:`Select Release mode <CONFIG_SECURE_FLASH_ENCRYPTION_MODE>` (Note that once Release mode is selected, the ``EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT`` eFuse bit will be burned to disable flash encryption hardware in ROM Download Mode.)
  111. :not esp32: - :ref:`Select UART ROM download mode (Permanently switch to Secure mode (recommended)) <CONFIG_SECURE_UART_ROM_DL_MODE>`. This is the default option, and is recommended. It is also possible to change this configuration setting to permanently disable UART ROM download mode, if this mode is not needed.
  112. - :ref:`Select the appropriate bootloader log verbosity <CONFIG_BOOTLOADER_LOG_LEVEL>`
  113. - Save the configuration and exit.
  114. The binaries can be encrypted on the host machine using the following command
  115. .. only:: esp32
  116. .. code-block:: bash
  117. espsecure.py encrypt_flash_data --keyfile my_flash_encryption_key.bin --address 0x1000 --output bootloader-enc.bin build/bootloader/bootloader.bin
  118. espsecure.py encrypt_flash_data --keyfile my_flash_encryption_key.bin --address 0x8000 --output partition-table-enc.bin build/partition_table/partition-table.bin
  119. espsecure.py encrypt_flash_data --keyfile my_flash_encryption_key.bin --address 0x10000 --output my-app-enc.bin build/my-app.bin
  120. .. only:: not esp32
  121. .. code-block:: bash
  122. espsecure.py encrypt_flash_data --aes_xts --keyfile my_flash_encryption_key.bin --address 0x0 --output bootloader-enc.bin build/my-app.bin
  123. espsecure.py encrypt_flash_data --aes_xts --keyfile my_flash_encryption_key.bin --address 0x8000 --output partition-table-enc.bin build/partition_table/partition-table.bin
  124. espsecure.py encrypt_flash_data --aes_xts --keyfile my_flash_encryption_key.bin --address 0x10000 --output my-app-enc.bin build/my-app.bin
  125. The above files can then be flashed to their respective offset using ``esptool.py``. To see all of the command line options recommended for ``esptool.py``, see the output printed when ``idf.py build`` succeeds. In the above command the offsets are used for a sample firmware, the actual offset for your firmware can be obtained by checking the partition table entry or by running `idf.py partition-table`. When the application contains following partition: ``otadata``, ``nvs_encryption_keys`` they need to be encrypted as well. Please refer to :ref:`encrypted-partitions` for more details about encrypted partitions.
  126. .. note::
  127. If the flashed ciphertext file is not recognized by the {IDF_TARGET_NAME} when it boots, check that the keys match and that the command line arguments match exactly, including the correct offset. It is important to provide the correct offset as the ciphertext changes when the offset changes.
  128. .. only:: esp32
  129. If your ESP32 uses non-default :ref:`FLASH_CRYPT_CONFIG value in eFuse <setting-flash-crypt-config>` then you will need to pass the ``--flash_crypt_conf`` argument to ``espsecure.py`` to set the matching value. This will not happen if the device configured flash encryption by itself, but may happen if burning eFuses manually to enable flash encryption.
  130. The command ``espsecure.py decrypt_flash_data`` can be used with the same options (and different input/output files), to decrypt ciphertext flash contents or a previously encrypted file.
  131. 6. Burn flash encryption related security eFuses as listed below:
  132. **A) Burn security eFuses:**
  133. .. list::
  134. :esp32: - ``DISABLE_DL_ENCRYPT``: Disable the UART bootloader encryption access.
  135. :esp32: - ``DISABLE_DL_DECRYPT``: Disable the UART bootloader decryption access.
  136. :esp32: - ``DISABLE_DL_CACHE``: Disable the UART bootloader flash cache access.
  137. :esp32: - ``JTAG_DISABLE``: Disable the JTAG
  138. :SOC_EFUSE_DIS_BOOT_REMAP: - ``DIS_BOOT_REMAP``: Disable capability to Remap ROM to RAM address space
  139. :SOC_EFUSE_DIS_DOWNLOAD_ICACHE: - ``DIS_DOWNLOAD_ICACHE``: Disable UART cache
  140. :SOC_EFUSE_DIS_DOWNLOAD_DCACHE: - ``DIS_DOWNLOAD_DCACHE``: Disable UART cache.
  141. :SOC_EFUSE_HARD_DIS_JTAG: - ``HARD_DIS_JTAG``: Hard disable JTAG peripheral
  142. :SOC_EFUSE_DIS_DIRECT_BOOT:- ``DIS_DIRECT_BOOT``: Disable direct boot (legacy SPI boot mode)
  143. :SOC_EFUSE_DIS_LEGACY_SPI_BOOT: - ``DIS_LEGACY_SPI_BOOT``: Disable legacy SPI boot mode
  144. :SOC_EFUSE_DIS_USB_JTAG: - ``DIS_USB_JTAG``: Disable USB switch to JTAG
  145. :SOC_EFUSE_DIS_PAD_JTAG: - ``DIS_PAD_JTAG``: Disable JTAG permanently
  146. :not esp32: - ``DIS_DOWNLOAD_MANUAL_ENCRYPT``: Disable UART bootloader encryption access
  147. **Burning all of the eFuses listed above is recommended for production use-case.**
  148. The respective eFuses can be burned with help of the following command:
  149. .. code:: bash
  150. espefuse.py burn_efuse --port PORT EFUSE_NAME 0x1
  151. .. note:: Please update the EFUSE_NAME with the eFuse that you need to burn. Multiple eFuses can be burned at the same time by appending them to the above command (e.g., EFUSE_NAME VAL EFUSE_NAME2 VAL2). More documentation about `espefuse.py` can be found `here <https://docs.espressif.com/projects/esptool/en/latest/esp32/espefuse/index.html>`_
  152. **B) Write protect security eFuses:**
  153. After burning the respective eFuses we need to write_protect the security configurations
  154. .. only:: esp32
  155. .. code:: bash
  156. espefuse.py --port PORT write_protect_efuse MAC
  157. .. note::
  158. The write disable bit for MAC also write disables DIS_CACHE which is required to prevent accidental burning of this bit.
  159. **C) Disable UART ROM DL mode**
  160. .. only:: not esp32
  161. .. code:: bash
  162. espefuse.py --port PORT write_protect_efuse DIS_ICACHE
  163. .. note::
  164. The write protection of above eFuse also write protects multiple other eFuses, Please refer to the {IDF_TARGET_NAME} eFuse table for more details.
  165. **C) Enable Security Download mode:**
  166. **WARNING:** Please burn the following bit at the very end. After this bit is burned, the espefuse tool can no longer be used to burn additional eFuses.
  167. .. list::
  168. :esp32: - ``UART_DOWNLOAD_DIS`` : Disable the UART ROM Download mode.
  169. :not esp32: - ``ENABLE_SECURITY_DOWNLOAD``: Enable Secure ROM download mode
  170. .. only:: esp32
  171. The eFuse can be burned with following command:
  172. .. code:: bash
  173. espefuse.py --port PORT burn_efuse UART_DOWNLOAD_DIS
  174. .. only:: not esp32
  175. The eFuse can be burned with following command:
  176. .. code:: bash
  177. espefuse.py --port PORT burn_efuse ENABLE_SECURITY_DOWNLOAD
  178. .. important::
  179. 7. Delete flash encryption key on host:
  180. * Once the flash encryption has been enabled for the device, the key **must be deleted immediately**. This ensures that the host cannot produce encrypted binaries for the same device going forward. This step is important to reduce the vulnerability of the flash encryption key.
  181. * It is recommended to generate a unique flash encryption key for each device for production use-cases.