esp_secure_boot.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "sdkconfig.h"
  18. #if CONFIG_IDF_TARGET_ESP32S2BETA
  19. #include "esp32s2beta/rom/efuse.h"
  20. #endif
  21. #ifdef CONFIG_SECURE_BOOT_ENABLED
  22. #if !defined(CONFIG_SECURE_SIGNED_ON_BOOT) || !defined(CONFIG_SECURE_SIGNED_ON_UPDATE) || !defined(CONFIG_SECURE_SIGNED_APPS)
  23. #error "internal sdkconfig error, secure boot should always enable all signature options"
  24. #endif
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* Support functions for secure boot features.
  30. Can be compiled as part of app or bootloader code.
  31. */
  32. /** @brief Is secure boot currently enabled in hardware?
  33. *
  34. * This means that the ROM bootloader code will only boot
  35. * a verified secure bootloader from now on.
  36. *
  37. * @return true if secure boot is enabled.
  38. */
  39. static inline bool esp_secure_boot_enabled(void)
  40. {
  41. #if CONFIG_IDF_TARGET_ESP32
  42. return REG_READ(EFUSE_BLK0_RDATA6_REG) & EFUSE_RD_ABS_DONE_0;
  43. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  44. return ets_efuse_secure_boot_enabled();
  45. #endif
  46. }
  47. /** @brief Generate secure digest from bootloader image
  48. *
  49. * @important This function is intended to be called from bootloader code only.
  50. *
  51. * If secure boot is not yet enabled for bootloader, this will:
  52. * 1) generate the secure boot key and burn it on EFUSE
  53. * (without enabling R/W protection)
  54. * 2) generate the digest from bootloader and save it
  55. * to flash address 0x0
  56. *
  57. * If first boot gets interrupted after calling this function
  58. * but before esp_secure_boot_permanently_enable() is called, then
  59. * the key burned on EFUSE will not be regenerated, unless manually
  60. * done using espefuse.py tool
  61. *
  62. * @return ESP_OK if secure boot digest is generated
  63. * successfully or found to be already present
  64. */
  65. esp_err_t esp_secure_boot_generate_digest(void);
  66. /** @brief Enable secure boot if it is not already enabled.
  67. *
  68. * @important If this function succeeds, secure boot is permanently
  69. * enabled on the chip via efuse.
  70. *
  71. * @important This function is intended to be called from bootloader code only.
  72. *
  73. * @important This will enable r/w protection of secure boot key on EFUSE,
  74. * therefore it is to be ensured that esp_secure_boot_generate_digest()
  75. * is called before this
  76. *
  77. * If secure boot is not yet enabled for bootloader, this will
  78. * 1) enable R/W protection of secure boot key on EFUSE
  79. * 2) enable secure boot by blowing the EFUSE_RD_ABS_DONE_0 efuse.
  80. *
  81. * This function does not verify secure boot of the bootloader (the
  82. * ROM bootloader does this.)
  83. *
  84. * Will fail if efuses have been part-burned in a way that indicates
  85. * secure boot should not or could not be correctly enabled.
  86. *
  87. * @return ESP_ERR_INVALID_STATE if efuse state doesn't allow
  88. * secure boot to be enabled cleanly. ESP_OK if secure boot
  89. * is enabled on this chip from now on.
  90. */
  91. esp_err_t esp_secure_boot_permanently_enable(void);
  92. /** @brief Verify the secure boot signature (determinstic ECDSA w/ SHA256) appended to some binary data in flash.
  93. *
  94. * Public key is compiled into the calling program. See docs/security/secure-boot.rst for details.
  95. *
  96. * @param src_addr Starting offset of the data in flash.
  97. * @param length Length of data in bytes. Signature is appended -after- length bytes.
  98. *
  99. * If flash encryption is enabled, the image will be transparently decrypted while being verified.
  100. *
  101. * @return ESP_OK if signature is valid, ESP_ERR_INVALID_STATE if
  102. * signature fails, ESP_FAIL for other failures (ie can't read flash).
  103. */
  104. esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length);
  105. /** @brief Verify the secure boot signature block (deterministic ECDSA w/ SHA256) based on the SHA256 hash of some data.
  106. *
  107. * Similar to esp_secure_boot_verify_signature(), but can be used when the digest is precalculated.
  108. * @param sig_block Pointer to signature block data
  109. * @param image_digest Pointer to 32 byte buffer holding SHA-256 hash.
  110. *
  111. */
  112. /** @brief Secure boot verification block, on-flash data format. */
  113. typedef struct {
  114. uint32_t version;
  115. uint8_t signature[64];
  116. } esp_secure_boot_sig_block_t;
  117. esp_err_t esp_secure_boot_verify_signature_block(const esp_secure_boot_sig_block_t *sig_block, const uint8_t *image_digest);
  118. #define FLASH_OFFS_SECURE_BOOT_IV_DIGEST 0
  119. /** @brief Secure boot IV+digest header */
  120. typedef struct {
  121. uint8_t iv[128];
  122. uint8_t digest[64];
  123. } esp_secure_boot_iv_digest_t;
  124. #ifdef __cplusplus
  125. }
  126. #endif