esp_flash_encrypt.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #include <stdbool.h>
  15. #include "esp_attr.h"
  16. #include "esp_err.h"
  17. #ifndef BOOTLOADER_BUILD
  18. #include "esp_spi_flash.h"
  19. #endif
  20. #include "soc/efuse_periph.h"
  21. #include "sdkconfig.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* @brief Flash encryption mode based on efuse values
  26. */
  27. typedef enum {
  28. ESP_FLASH_ENC_MODE_DISABLED, // flash encryption is not enabled (flash crypt cnt=0)
  29. ESP_FLASH_ENC_MODE_DEVELOPMENT, // flash encryption is enabled but for Development (reflash over UART allowed)
  30. ESP_FLASH_ENC_MODE_RELEASE // flash encryption is enabled for Release (reflash over UART disabled)
  31. } esp_flash_enc_mode_t;
  32. /**
  33. * @file esp_partition.h
  34. * @brief Support functions for flash encryption features
  35. *
  36. * Can be compiled as part of app or bootloader code.
  37. */
  38. /** @brief Is flash encryption currently enabled in hardware?
  39. *
  40. * Flash encryption is enabled if the FLASH_CRYPT_CNT efuse has an odd number of bits set.
  41. *
  42. * @return true if flash encryption is enabled.
  43. */
  44. static inline /** @cond */ IRAM_ATTR /** @endcond */ bool esp_flash_encryption_enabled(void)
  45. {
  46. uint32_t flash_crypt_cnt = 0;
  47. #if CONFIG_IDF_TARGET_ESP32
  48. flash_crypt_cnt = REG_GET_FIELD(EFUSE_BLK0_RDATA0_REG, EFUSE_RD_FLASH_CRYPT_CNT);
  49. #else
  50. flash_crypt_cnt = REG_GET_FIELD(EFUSE_RD_REPEAT_DATA1_REG, EFUSE_SPI_BOOT_CRYPT_CNT);
  51. #endif
  52. /* __builtin_parity is in flash, so we calculate parity inline */
  53. bool enabled = false;
  54. while (flash_crypt_cnt) {
  55. if (flash_crypt_cnt & 1) {
  56. enabled = !enabled;
  57. }
  58. flash_crypt_cnt >>= 1;
  59. }
  60. return enabled;
  61. }
  62. /* @brief Update on-device flash encryption
  63. *
  64. * Intended to be called as part of the bootloader process if flash
  65. * encryption is enabled in device menuconfig.
  66. *
  67. * If FLASH_CRYPT_CNT efuse parity is 1 (ie odd number of bits set),
  68. * then return ESP_OK immediately (indicating flash encryption is enabled
  69. * and functional).
  70. *
  71. * If FLASH_CRYPT_CNT efuse parity is 0 (ie even number of bits set),
  72. * assume the flash has just been written with plaintext that needs encrypting.
  73. *
  74. * The following regions of flash are encrypted in place:
  75. *
  76. * - The bootloader image, if a valid plaintext image is found.[*]
  77. * - The partition table, if a valid plaintext table is found.
  78. * - Any app partition that contains a valid plaintext app image.
  79. * - Any other partitions with the "encrypt" flag set. [**]
  80. *
  81. * After the re-encryption process completes, a '1' bit is added to the
  82. * FLASH_CRYPT_CNT value (setting the parity to 1) and the EFUSE is re-burned.
  83. *
  84. * [*] If reflashing bootloader with secure boot enabled, pre-encrypt
  85. * the bootloader before writing it to flash or secure boot will fail.
  86. *
  87. * [**] For this reason, if serial re-flashing a previous flashed
  88. * device with secure boot enabled and using FLASH_CRYPT_CNT to
  89. * trigger re-encryption, you must simultaneously re-flash plaintext
  90. * content to all partitions with the "encrypt" flag set or this
  91. * data will be corrupted (encrypted twice).
  92. *
  93. * @note The post-condition of this function is that all
  94. * partitions that should be encrypted are encrypted.
  95. *
  96. * @note Take care not to power off the device while this function
  97. * is running, or the partition currently being encrypted will be lost.
  98. *
  99. * @note RTC_WDT will reset while encryption operations will be performed (if RTC_WDT is configured).
  100. *
  101. * @return ESP_OK if all operations succeeded, ESP_ERR_INVALID_STATE
  102. * if a fatal error occured during encryption of all partitions.
  103. */
  104. esp_err_t esp_flash_encrypt_check_and_update(void);
  105. /** @brief Encrypt-in-place a block of flash sectors
  106. *
  107. * @note This function resets RTC_WDT between operations with sectors.
  108. * @param src_addr Source offset in flash. Should be multiple of 4096 bytes.
  109. * @param data_length Length of data to encrypt in bytes. Will be rounded up to next multiple of 4096 bytes.
  110. *
  111. * @return ESP_OK if all operations succeeded, ESP_ERR_FLASH_OP_FAIL
  112. * if SPI flash fails, ESP_ERR_FLASH_OP_TIMEOUT if flash times out.
  113. */
  114. esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length);
  115. /** @brief Write protect FLASH_CRYPT_CNT
  116. *
  117. * Intended to be called as a part of boot process if flash encryption
  118. * is enabled but secure boot is not used. This should protect against
  119. * serial re-flashing of an unauthorised code in absence of secure boot.
  120. *
  121. * @note On ESP32 V3 only, write protecting FLASH_CRYPT_CNT will also prevent
  122. * disabling UART Download Mode. If both are wanted, call
  123. * esp_efuse_disable_rom_download_mode() before calling this function.
  124. *
  125. */
  126. void esp_flash_write_protect_crypt_cnt(void);
  127. /** @brief Return the flash encryption mode
  128. *
  129. * The API is called during boot process but can also be called by
  130. * application to check the current flash encryption mode of ESP32
  131. *
  132. * @return
  133. */
  134. esp_flash_enc_mode_t esp_get_flash_encryption_mode(void);
  135. /** @brief Check the flash encryption mode during startup
  136. *
  137. * @note This function is called automatically during app startup,
  138. * it doesn't need to be called from the app.
  139. *
  140. * Verifies the flash encryption config during startup:
  141. *
  142. * - Correct any insecure flash encryption settings if hardware
  143. * Secure Boot is enabled.
  144. * - Log warnings if the efuse config doesn't match the project
  145. * config in any way
  146. */
  147. void esp_flash_encryption_init_checks(void);
  148. #ifdef __cplusplus
  149. }
  150. #endif