esp_flash_encrypt.h 6.6 KB

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