esp_flash_encrypt.h 6.5 KB

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