esp_secure_boot.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "sdkconfig.h"
  19. #if CONFIG_IDF_TARGET_ESP32S2
  20. #include "esp32s2/rom/efuse.h"
  21. #else
  22. #include "esp32/rom/secure_boot.h"
  23. #endif
  24. typedef struct ets_secure_boot_signature ets_secure_boot_signature_t;
  25. #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
  26. #if !defined(CONFIG_SECURE_SIGNED_ON_BOOT) || !defined(CONFIG_SECURE_SIGNED_ON_UPDATE) || !defined(CONFIG_SECURE_SIGNED_APPS)
  27. #error "internal sdkconfig error, secure boot should always enable all signature options"
  28. #endif
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* Support functions for secure boot features.
  34. Can be compiled as part of app or bootloader code.
  35. */
  36. /** @brief Is secure boot currently enabled in hardware?
  37. *
  38. * This means that the ROM bootloader code will only boot
  39. * a verified secure bootloader from now on.
  40. *
  41. * @return true if secure boot is enabled.
  42. */
  43. static inline bool esp_secure_boot_enabled(void)
  44. {
  45. #if CONFIG_IDF_TARGET_ESP32
  46. #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
  47. return REG_READ(EFUSE_BLK0_RDATA6_REG) & EFUSE_RD_ABS_DONE_0;
  48. #elif CONFIG_SECURE_BOOT_V2_ENABLED
  49. return ets_use_secure_boot_v2();
  50. #endif
  51. #elif CONFIG_IDF_TARGET_ESP32S2
  52. return ets_efuse_secure_boot_enabled();
  53. #endif
  54. return false; /* Secure Boot not enabled in menuconfig */
  55. }
  56. /** @brief Generate secure digest from bootloader image
  57. *
  58. * @important This function is intended to be called from bootloader code only.
  59. *
  60. * This function is only used in the context of the Secure Boot V1 scheme.
  61. *
  62. * If secure boot is not yet enabled for bootloader, this will:
  63. * 1) generate the secure boot key and burn it on EFUSE
  64. * (without enabling R/W protection)
  65. * 2) generate the digest from bootloader and save it
  66. * to flash address 0x0
  67. *
  68. * If first boot gets interrupted after calling this function
  69. * but before esp_secure_boot_permanently_enable() is called, then
  70. * the key burned on EFUSE will not be regenerated, unless manually
  71. * done using espefuse.py tool
  72. *
  73. * @return ESP_OK if secure boot digest is generated
  74. * successfully or found to be already present
  75. */
  76. esp_err_t esp_secure_boot_generate_digest(void);
  77. /** @brief Enable secure boot V1 if it is not already enabled.
  78. *
  79. * @important If this function succeeds, secure boot V1 is permanently
  80. * enabled on the chip via efuse.
  81. *
  82. * @important This function is intended to be called from bootloader code only.
  83. *
  84. * @important In case of Secure Boot V1, this will enable r/w protection
  85. * of secure boot key on EFUSE, therefore it is to be ensured that
  86. * esp_secure_boot_generate_digest() is called before this .If secure boot is not
  87. * yet enabled for bootloader, this will
  88. * 1) enable R/W protection of secure boot key on EFUSE
  89. * 2) enable secure boot by blowing the EFUSE_RD_ABS_DONE_0 efuse.
  90. *
  91. * This function does not verify secure boot of the bootloader (the
  92. * ROM bootloader does this.)
  93. *
  94. * Will fail if efuses have been part-burned in a way that indicates
  95. * secure boot should not or could not be correctly enabled.
  96. *
  97. * @return ESP_ERR_INVALID_STATE if efuse state doesn't allow
  98. * secure boot to be enabled cleanly. ESP_OK if secure boot
  99. * is enabled on this chip from now on.
  100. */
  101. esp_err_t esp_secure_boot_permanently_enable(void);
  102. /** @brief Enables secure boot V2 if it is not already enabled.
  103. *
  104. * @important If this function succeeds, secure boot V2 is permanently
  105. * enabled on the chip via efuse.
  106. *
  107. * @important This function is intended to be called from bootloader code only.
  108. *
  109. * @important In case of Secure Boot V2, this will enable write protection
  110. * of secure boot key on EFUSE in BLK2. .If secure boot is not
  111. * yet enabled for bootloader, this will
  112. * 1) enable W protection of secure boot key on EFUSE
  113. * 2) enable secure boot by blowing the EFUSE_RD_ABS_DONE_1 efuse.
  114. *
  115. * This function does not verify secure boot of the bootloader (the
  116. * ROM bootloader does this.)
  117. *
  118. * @param image_data Image metadata of the application to be loaded.
  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_v2_permanently_enable(const esp_image_metadata_t *image_data);
  128. /** @brief Verify the secure boot signature appended to some binary data in flash.
  129. *
  130. * For ECDSA Scheme (Secure Boot V1) - deterministic ECDSA w/ SHA256 image
  131. * For RSA Scheme (Secure Boot V2) - RSA-PSS Verification of the SHA-256 image
  132. *
  133. * Public key is compiled into the calling program in the ECDSA Scheme.
  134. * See the apt docs/security/secure-boot-v1.rst or docs/security/secure-boot-v2.rst for details.
  135. *
  136. * @param src_addr Starting offset of the data in flash.
  137. * @param length Length of data in bytes. Signature is appended -after- length bytes.
  138. *
  139. * If flash encryption is enabled, the image will be transparently decrypted while being verified.
  140. *
  141. * @note This function doesn't have any fault injection resistance so should not be called
  142. * during a secure boot itself (but can be called when verifying an update, etc.)
  143. *
  144. * @return ESP_OK if signature is valid, ESP_ERR_INVALID_STATE if
  145. * signature fails, ESP_FAIL for other failures (ie can't read flash).
  146. */
  147. esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length);
  148. /** @brief Secure boot verification block, on-flash data format. */
  149. typedef struct {
  150. uint32_t version;
  151. uint8_t signature[64];
  152. } esp_secure_boot_sig_block_t;
  153. /** @brief Verify the ECDSA secure boot signature block for Secure Boot V1.
  154. *
  155. * Calculates Deterministic ECDSA w/ SHA256 based on the SHA256 hash of the image. ECDSA signature
  156. * verification must be enabled in project configuration to use this function.
  157. *
  158. * Similar to esp_secure_boot_verify_signature(), but can be used when the digest is precalculated.
  159. * @param sig_block Pointer to ECDSA signature block data
  160. * @param image_digest Pointer to 32 byte buffer holding SHA-256 hash.
  161. * @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.)
  162. *
  163. */
  164. 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);
  165. /** @brief Verify the RSA secure boot signature block for Secure Boot V2.
  166. *
  167. * Performs RSA-PSS Verification of the SHA-256 image based on the public key
  168. * in the signature block, compared against the public key digest stored in efuse.
  169. *
  170. * Similar to esp_secure_boot_verify_signature(), but can be used when the digest is precalculated.
  171. * @param sig_block Pointer to RSA signature block data
  172. * @param image_digest Pointer to 32 byte buffer holding SHA-256 hash.
  173. * @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.)
  174. *
  175. */
  176. 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);
  177. /** @brief Legacy ECDSA verification function
  178. *
  179. * @note Deprecated, call either esp_secure_boot_verify_ecdsa_signature_block() or esp_secure_boot_verify_rsa_signature_block() instead.
  180. *
  181. * @param sig_block Pointer to ECDSA signature block data
  182. * @param image_digest Pointer to 32 byte buffer holding SHA-256 hash.
  183. */
  184. esp_err_t esp_secure_boot_verify_signature_block(const esp_secure_boot_sig_block_t *sig_block, const uint8_t *image_digest)
  185. __attribute__((deprecated("use esp_secure_boot_verify_ecdsa_signature_block instead")));
  186. #define FLASH_OFFS_SECURE_BOOT_IV_DIGEST 0
  187. /** @brief Secure boot IV+digest header */
  188. typedef struct {
  189. uint8_t iv[128];
  190. uint8_t digest[64];
  191. } esp_secure_boot_iv_digest_t;
  192. #ifdef __cplusplus
  193. }
  194. #endif