esp_flash_encrypt.h 6.2 KB

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