esp_secure_boot.h 12 KB

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