esp_flash_encrypt.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include "esp_attr.h"
  9. #include "esp_err.h"
  10. #include "soc/soc_caps.h"
  11. #ifndef BOOTLOADER_BUILD
  12. #include "spi_flash_mmap.h"
  13. #endif
  14. #include "hal/efuse_ll.h"
  15. #include "sdkconfig.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* @brief Flash encryption mode based on efuse values
  20. */
  21. typedef enum {
  22. ESP_FLASH_ENC_MODE_DISABLED, // flash encryption is not enabled (flash crypt cnt=0)
  23. ESP_FLASH_ENC_MODE_DEVELOPMENT, // flash encryption is enabled but for Development (reflash over UART allowed)
  24. ESP_FLASH_ENC_MODE_RELEASE // flash encryption is enabled for Release (reflash over UART disabled)
  25. } esp_flash_enc_mode_t;
  26. /**
  27. * @file esp_partition.h
  28. * @brief Support functions for flash encryption features
  29. *
  30. * Can be compiled as part of app or bootloader code.
  31. */
  32. /** @brief Is flash encryption currently enabled in hardware?
  33. *
  34. * Flash encryption is enabled if the FLASH_CRYPT_CNT efuse has an odd number of bits set.
  35. *
  36. * @return true if flash encryption is enabled.
  37. */
  38. bool esp_flash_encryption_enabled(void);
  39. /* @brief Update on-device flash encryption
  40. *
  41. * Intended to be called as part of the bootloader process if flash
  42. * encryption is enabled in device menuconfig.
  43. *
  44. * If FLASH_CRYPT_CNT efuse parity is 1 (ie odd number of bits set),
  45. * then return ESP_OK immediately (indicating flash encryption is enabled
  46. * and functional).
  47. *
  48. * If FLASH_CRYPT_CNT efuse parity is 0 (ie even number of bits set),
  49. * assume the flash has just been written with plaintext that needs encrypting.
  50. *
  51. * The following regions of flash are encrypted in place:
  52. *
  53. * - The bootloader image, if a valid plaintext image is found.[*]
  54. * - The partition table, if a valid plaintext table is found.
  55. * - Any app partition that contains a valid plaintext app image.
  56. * - Any other partitions with the "encrypt" flag set. [**]
  57. *
  58. * After the re-encryption process completes, a '1' bit is added to the
  59. * FLASH_CRYPT_CNT value (setting the parity to 1) and the EFUSE is re-burned.
  60. *
  61. * [*] If reflashing bootloader with secure boot enabled, pre-encrypt
  62. * the bootloader before writing it to flash or secure boot will fail.
  63. *
  64. * [**] For this reason, if serial re-flashing a previous flashed
  65. * device with secure boot enabled and using FLASH_CRYPT_CNT to
  66. * trigger re-encryption, you must simultaneously re-flash plaintext
  67. * content to all partitions with the "encrypt" flag set or this
  68. * data will be corrupted (encrypted twice).
  69. *
  70. * @note The post-condition of this function is that all
  71. * partitions that should be encrypted are encrypted.
  72. *
  73. * @note Take care not to power off the device while this function
  74. * is running, or the partition currently being encrypted will be lost.
  75. *
  76. * @note RTC_WDT will reset while encryption operations will be performed (if RTC_WDT is configured).
  77. *
  78. * @return ESP_OK if all operations succeeded, ESP_ERR_INVALID_STATE
  79. * if a fatal error occured during encryption of all partitions.
  80. */
  81. esp_err_t esp_flash_encrypt_check_and_update(void);
  82. /** @brief Returns the Flash Encryption state and prints it
  83. *
  84. * @return True - Flash Encryption is enabled
  85. * False - Flash Encryption is not enabled
  86. */
  87. bool esp_flash_encrypt_state(void);
  88. /** @brief Checks if the first initialization was done
  89. *
  90. * If the first initialization was done then FLASH_CRYPT_CNT != 0
  91. *
  92. * @return true - the first initialization was done
  93. * false - the first initialization was NOT done
  94. */
  95. bool esp_flash_encrypt_initialized_once(void);
  96. /** @brief The first initialization of Flash Encryption key and related eFuses
  97. *
  98. * @return ESP_OK if all operations succeeded
  99. */
  100. esp_err_t esp_flash_encrypt_init(void);
  101. /** @brief Encrypts flash content
  102. *
  103. * @return ESP_OK if all operations succeeded
  104. */
  105. esp_err_t esp_flash_encrypt_contents(void);
  106. /** @brief Activates Flash encryption on the chip
  107. *
  108. * It burns FLASH_CRYPT_CNT eFuse based on the CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE option.
  109. *
  110. * @return ESP_OK if all operations succeeded
  111. */
  112. esp_err_t esp_flash_encrypt_enable(void);
  113. /** @brief Returns True if the write protection of FLASH_CRYPT_CNT is set
  114. *
  115. * @param print_error Print error if it is write protected
  116. *
  117. * @return true - if FLASH_CRYPT_CNT is write protected
  118. */
  119. bool esp_flash_encrypt_is_write_protected(bool print_error);
  120. /** @brief Encrypt-in-place a block of flash sectors
  121. *
  122. * @note This function resets RTC_WDT between operations with sectors.
  123. * @param src_addr Source offset in flash. Should be multiple of 4096 bytes.
  124. * @param data_length Length of data to encrypt in bytes. Will be rounded up to next multiple of 4096 bytes.
  125. *
  126. * @return ESP_OK if all operations succeeded, ESP_ERR_FLASH_OP_FAIL
  127. * if SPI flash fails, ESP_ERR_FLASH_OP_TIMEOUT if flash times out.
  128. */
  129. esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length);
  130. /** @brief Write protect FLASH_CRYPT_CNT
  131. *
  132. * Intended to be called as a part of boot process if flash encryption
  133. * is enabled but secure boot is not used. This should protect against
  134. * serial re-flashing of an unauthorised code in absence of secure boot.
  135. *
  136. * @note On ESP32 V3 only, write protecting FLASH_CRYPT_CNT will also prevent
  137. * disabling UART Download Mode. If both are wanted, call
  138. * esp_efuse_disable_rom_download_mode() before calling this function.
  139. *
  140. */
  141. void esp_flash_write_protect_crypt_cnt(void);
  142. /** @brief Return the flash encryption mode
  143. *
  144. * The API is called during boot process but can also be called by
  145. * application to check the current flash encryption mode of ESP32
  146. *
  147. * @return
  148. */
  149. esp_flash_enc_mode_t esp_get_flash_encryption_mode(void);
  150. /** @brief Check the flash encryption mode during startup
  151. *
  152. * @note This function is called automatically during app startup,
  153. * it doesn't need to be called from the app.
  154. *
  155. * Verifies the flash encryption config during startup:
  156. *
  157. * - Correct any insecure flash encryption settings if hardware
  158. * Secure Boot is enabled.
  159. * - Log warnings if the efuse config doesn't match the project
  160. * config in any way
  161. */
  162. void esp_flash_encryption_init_checks(void);
  163. /** @brief Set all secure eFuse features related to flash encryption
  164. *
  165. * @return
  166. * - ESP_OK - Successfully
  167. */
  168. esp_err_t esp_flash_encryption_enable_secure_features(void);
  169. /** @brief Returns the verification status for all physical security features of flash encryption in release mode
  170. *
  171. * If the device has flash encryption feature configured in the release mode,
  172. * then it is highly recommended to call this API in the application startup code.
  173. * This API verifies the sanity of the eFuse configuration against
  174. * the release (production) mode of the flash encryption feature.
  175. *
  176. * @return
  177. * - True - all eFuses are configured correctly
  178. * - False - not all eFuses are configured correctly.
  179. */
  180. bool esp_flash_encryption_cfg_verify_release_mode(void);
  181. /** @brief Switches Flash Encryption from "Development" to "Release"
  182. *
  183. * If already in "Release" mode, the function will do nothing.
  184. * If flash encryption efuse is not enabled yet then abort.
  185. * It burns:
  186. * - "disable encrypt in dl mode"
  187. * - set FLASH_CRYPT_CNT efuse to max
  188. */
  189. void esp_flash_encryption_set_release_mode(void);
  190. #ifdef __cplusplus
  191. }
  192. #endif