esp_secure_boot.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_err.h>
  9. #include "soc/efuse_periph.h"
  10. #include "esp_image_format.h"
  11. #include "esp_rom_efuse.h"
  12. #include "sdkconfig.h"
  13. #include "esp_rom_crc.h"
  14. #if CONFIG_IDF_TARGET_ESP32
  15. #include "esp32/rom/efuse.h"
  16. #include "esp32/rom/secure_boot.h"
  17. #elif CONFIG_IDF_TARGET_ESP32S2
  18. #include "esp32s2/rom/efuse.h"
  19. #include "esp32s2/rom/secure_boot.h"
  20. #elif CONFIG_IDF_TARGET_ESP32C3
  21. #include "esp32c3/rom/efuse.h"
  22. #include "esp32c3/rom/secure_boot.h"
  23. #elif CONFIG_IDF_TARGET_ESP32S3
  24. #include "esp32s3/rom/efuse.h"
  25. #include "esp32s3/rom/secure_boot.h"
  26. #elif CONFIG_IDF_TARGET_ESP32H2
  27. #include "esp32h2/rom/efuse.h"
  28. #include "esp32h2/rom/secure_boot.h"
  29. #endif
  30. #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
  31. #if !defined(CONFIG_SECURE_SIGNED_ON_BOOT) || !defined(CONFIG_SECURE_SIGNED_ON_UPDATE) || !defined(CONFIG_SECURE_SIGNED_APPS)
  32. #error "internal sdkconfig error, secure boot should always enable all signature options"
  33. #endif
  34. #endif
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* Support functions for secure boot features.
  39. Can be compiled as part of app or bootloader code.
  40. */
  41. #define ESP_SECURE_BOOT_DIGEST_LEN 32
  42. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  43. #include "esp_efuse.h"
  44. #include "esp_efuse_table.h"
  45. #endif
  46. /** @brief Is secure boot currently enabled in hardware?
  47. *
  48. * This means that the ROM bootloader code will only boot
  49. * a verified secure bootloader from now on.
  50. *
  51. * @return true if secure boot is enabled.
  52. */
  53. static inline bool esp_secure_boot_enabled(void)
  54. {
  55. #if CONFIG_IDF_TARGET_ESP32
  56. #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
  57. #ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  58. return REG_READ(EFUSE_BLK0_RDATA6_REG) & EFUSE_RD_ABS_DONE_0;
  59. #else
  60. return esp_efuse_read_field_bit(ESP_EFUSE_ABS_DONE_0);
  61. #endif
  62. #elif CONFIG_SECURE_BOOT_V2_ENABLED
  63. #ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  64. return ets_use_secure_boot_v2();
  65. #else
  66. return esp_efuse_read_field_bit(ESP_EFUSE_ABS_DONE_1);
  67. #endif
  68. #endif
  69. #else
  70. #ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  71. return esp_rom_efuse_is_secure_boot_enabled();
  72. #else
  73. return esp_efuse_read_field_bit(ESP_EFUSE_SECURE_BOOT_EN);
  74. #endif
  75. #endif
  76. return false; /* Secure Boot not enabled in menuconfig */
  77. }
  78. /** @brief Generate secure digest from bootloader image
  79. *
  80. * @important This function is intended to be called from bootloader code only.
  81. *
  82. * This function is only used in the context of the Secure Boot V1 scheme.
  83. *
  84. * If secure boot is not yet enabled for bootloader, this will:
  85. * 1) generate the secure boot key and burn it on EFUSE
  86. * (without enabling R/W protection)
  87. * 2) generate the digest from bootloader and save it
  88. * to flash address 0x0
  89. *
  90. * If first boot gets interrupted after calling this function
  91. * but before esp_secure_boot_permanently_enable() is called, then
  92. * the key burned on EFUSE will not be regenerated, unless manually
  93. * done using espefuse.py tool
  94. *
  95. * @return ESP_OK if secure boot digest is generated
  96. * successfully or found to be already present
  97. */
  98. esp_err_t esp_secure_boot_generate_digest(void);
  99. /** @brief Enable secure boot V1 if it is not already enabled.
  100. *
  101. * @important If this function succeeds, secure boot V1 is permanently
  102. * enabled on the chip via efuse.
  103. *
  104. * @important This function is intended to be called from bootloader code only.
  105. *
  106. * @important In case of Secure Boot V1, this will enable r/w protection
  107. * of secure boot key on EFUSE, therefore it is to be ensured that
  108. * esp_secure_boot_generate_digest() is called before this .If secure boot is not
  109. * yet enabled for bootloader, this will
  110. * 1) enable R/W protection of secure boot key on EFUSE
  111. * 2) enable secure boot by blowing the EFUSE_RD_ABS_DONE_0 efuse.
  112. *
  113. * This function does not verify secure boot of the bootloader (the
  114. * ROM bootloader does this.)
  115. *
  116. * Will fail if efuses have been part-burned in a way that indicates
  117. * secure boot should not or could not be correctly enabled.
  118. *
  119. * @return ESP_ERR_INVALID_STATE if efuse state doesn't allow
  120. * secure boot to be enabled cleanly. ESP_OK if secure boot
  121. * is enabled on this chip from now on.
  122. */
  123. esp_err_t esp_secure_boot_permanently_enable(void);
  124. /** @brief Enables secure boot V2 if it is not already enabled.
  125. *
  126. * @important If this function succeeds, secure boot V2 is permanently
  127. * enabled on the chip via efuse.
  128. *
  129. * @important This function is intended to be called from bootloader code only.
  130. *
  131. * @important In case of Secure Boot V2, this will enable write protection
  132. * of secure boot key on EFUSE in BLK2. .If secure boot is not
  133. * yet enabled for bootloader, this will
  134. * 1) enable W protection of secure boot key on EFUSE
  135. * 2) enable secure boot by blowing the EFUSE_RD_ABS_DONE_1 efuse.
  136. *
  137. * This function does not verify secure boot of the bootloader (the
  138. * ROM bootloader does this.)
  139. *
  140. * @param image_data Image metadata of the application to be loaded.
  141. *
  142. * Will fail if efuses have been part-burned in a way that indicates
  143. * secure boot should not or could not be correctly enabled.
  144. *
  145. * @return ESP_ERR_INVALID_STATE if efuse state doesn't allow
  146. * secure boot to be enabled cleanly. ESP_OK if secure boot
  147. * is enabled on this chip from now on.
  148. */
  149. esp_err_t esp_secure_boot_v2_permanently_enable(const esp_image_metadata_t *image_data);
  150. /** @brief Verify the secure boot signature appended to some binary data in flash.
  151. *
  152. * For ECDSA Scheme (Secure Boot V1) - deterministic ECDSA w/ SHA256 image
  153. * For RSA Scheme (Secure Boot V2) - RSA-PSS Verification of the SHA-256 image
  154. *
  155. * Public key is compiled into the calling program in the ECDSA Scheme.
  156. * See the apt docs/security/secure-boot-v1.rst or docs/security/secure-boot-v2.rst for details.
  157. *
  158. * @param src_addr Starting offset of the data in flash.
  159. * @param length Length of data in bytes. Signature is appended -after- length bytes.
  160. *
  161. * If flash encryption is enabled, the image will be transparently decrypted while being verified.
  162. *
  163. * @note This function doesn't have any fault injection resistance so should not be called
  164. * during a secure boot itself (but can be called when verifying an update, etc.)
  165. *
  166. * @return ESP_OK if signature is valid, ESP_ERR_INVALID_STATE if
  167. * signature fails, ESP_FAIL for other failures (ie can't read flash).
  168. */
  169. esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length);
  170. /** @brief Secure boot verification block, on-flash data format. */
  171. typedef struct {
  172. uint32_t version;
  173. uint8_t signature[64];
  174. } esp_secure_boot_sig_block_t;
  175. /** @brief Verify the ECDSA secure boot signature block for Secure Boot V1.
  176. *
  177. * Calculates Deterministic ECDSA w/ SHA256 based on the SHA256 hash of the image. ECDSA signature
  178. * verification must be enabled in project configuration to use this function.
  179. *
  180. * Similar to esp_secure_boot_verify_signature(), but can be used when the digest is precalculated.
  181. * @param sig_block Pointer to ECDSA signature block data
  182. * @param image_digest Pointer to 32 byte buffer holding SHA-256 hash.
  183. * @param verified_digest Pointer to 32 byte buffer that will receive verified digest if verification completes. (Used during bootloader implementation only, result is invalid otherwise.)
  184. *
  185. */
  186. esp_err_t esp_secure_boot_verify_ecdsa_signature_block(const esp_secure_boot_sig_block_t *sig_block, const uint8_t *image_digest, uint8_t *verified_digest);
  187. #if !CONFIG_IDF_TARGET_ESP32 || CONFIG_ESP32_REV_MIN_3
  188. /**
  189. * @brief Structure to hold public key digests calculated from the signature blocks of a single image.
  190. *
  191. * Each image can have one or more signature blocks (up to SECURE_BOOT_NUM_BLOCKS). Each signature block includes a public key.
  192. */
  193. typedef struct {
  194. uint8_t key_digests[SECURE_BOOT_NUM_BLOCKS][ESP_SECURE_BOOT_DIGEST_LEN]; /* SHA of the public key components in the signature block */
  195. unsigned num_digests; /* Number of valid digests, starting at index 0 */
  196. } esp_image_sig_public_key_digests_t;
  197. /** @brief Verify the RSA secure boot signature block for Secure Boot V2.
  198. *
  199. * Performs RSA-PSS Verification of the SHA-256 image based on the public key
  200. * in the signature block, compared against the public key digest stored in efuse.
  201. *
  202. * Similar to esp_secure_boot_verify_signature(), but can be used when the digest is precalculated.
  203. * @param sig_block Pointer to RSA signature block data
  204. * @param image_digest Pointer to 32 byte buffer holding SHA-256 hash.
  205. * @param verified_digest Pointer to 32 byte buffer that will receive verified digest if verification completes. (Used during bootloader implementation only, result is invalid otherwise.)
  206. *
  207. */
  208. esp_err_t esp_secure_boot_verify_rsa_signature_block(const ets_secure_boot_signature_t *sig_block, const uint8_t *image_digest, uint8_t *verified_digest);
  209. #endif // !CONFIG_IDF_TARGET_ESP32 || CONFIG_ESP32_REV_MIN_3
  210. /** @brief Legacy ECDSA verification function
  211. *
  212. * @note Deprecated, call either esp_secure_boot_verify_ecdsa_signature_block() or esp_secure_boot_verify_rsa_signature_block() instead.
  213. *
  214. * @param sig_block Pointer to ECDSA signature block data
  215. * @param image_digest Pointer to 32 byte buffer holding SHA-256 hash.
  216. */
  217. esp_err_t esp_secure_boot_verify_signature_block(const esp_secure_boot_sig_block_t *sig_block, const uint8_t *image_digest)
  218. __attribute__((deprecated("use esp_secure_boot_verify_ecdsa_signature_block instead")));
  219. #define FLASH_OFFS_SECURE_BOOT_IV_DIGEST 0
  220. /** @brief Secure boot IV+digest header */
  221. typedef struct {
  222. uint8_t iv[128];
  223. uint8_t digest[64];
  224. } esp_secure_boot_iv_digest_t;
  225. /** @brief Check the secure boot V2 during startup
  226. *
  227. * @note This function is called automatically during app startup,
  228. * it doesn't need to be called from the app.
  229. *
  230. * Verifies the secure boot config during startup:
  231. *
  232. * - Correct any insecure secure boot settings
  233. */
  234. void esp_secure_boot_init_checks(void);
  235. #if !BOOTLOADER_BUILD && CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME
  236. /** @brief Scan the current running app for signature blocks
  237. *
  238. * @note This function doesn't verify that the signatures are valid or the
  239. * corresponding public keys are trusted, it only reads the number of signature
  240. * blocks present and optionally calculates the digests of the public keys
  241. * provided in the signature blocks.
  242. *
  243. * @param digest_public_keys If true, the key_digests fields in the
  244. * public_key_digests structure will be filled with the digests of the public
  245. * key provided in each signature block. Note that if Secure Boot V2 is enabled,
  246. * each public key will only be trusted if the same digest is also present in
  247. * eFuse (but this is not checked by this function).
  248. *
  249. * @param public_key_digests[out] Structure is initialized with the num_digests
  250. * field set to the number of signatures found. If digest_public_keys is set,
  251. * the public key digests are also calculated and stored here.
  252. *
  253. * @return
  254. * - ESP_OK - At least one signature was found
  255. * - ESP_ERR_NOT_FOUND - No signatures were found, num_digests value will be zero
  256. * - ESP_FAIL - An error occured trying to read the signature blocks from flash
  257. */
  258. esp_err_t esp_secure_boot_get_signature_blocks_for_running_app(bool digest_public_keys, esp_image_sig_public_key_digests_t *public_key_digests);
  259. #endif // !BOOTLOADER_BUILD && CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME
  260. /** @brief Set all secure eFuse features related to secure_boot
  261. *
  262. * @return
  263. * - ESP_OK - Successfully
  264. */
  265. esp_err_t esp_secure_boot_enable_secure_features(void);
  266. #ifdef __cplusplus
  267. }
  268. #endif