esp_secure_boot.h 8.5 KB

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