esp_flash_encrypt.h 6.4 KB

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