esp_secure_boot.h 11 KB

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