esp_secure_boot.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_reg.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* Support functions for secure boot features.
  21. Can be compiled as part of app or bootloader code.
  22. */
  23. /** @brief Is secure boot currently enabled in hardware?
  24. *
  25. * Secure boot is enabled if the ABS_DONE_0 efuse is blown. This means
  26. * that the ROM bootloader code will only boot a verified secure
  27. * bootloader digest from now on.
  28. *
  29. * @return true if secure boot is enabled.
  30. */
  31. static inline bool esp_secure_boot_enabled(void) {
  32. return REG_READ(EFUSE_BLK0_RDATA6_REG) & EFUSE_RD_ABS_DONE_0;
  33. }
  34. /** @brief Enable secure boot if it is not already enabled.
  35. *
  36. * @important If this function succeeds, secure boot is permanently
  37. * enabled on the chip via efuse.
  38. *
  39. * @important This function is intended to be called from bootloader code only.
  40. *
  41. * If secure boot is not yet enabled for bootloader, this will
  42. * generate the secure boot digest and enable secure boot by blowing
  43. * the EFUSE_RD_ABS_DONE_0 efuse.
  44. *
  45. * This function does not verify secure boot of the bootloader (the
  46. * ROM bootloader does this.)
  47. *
  48. * Will fail if efuses have been part-burned in a way that indicates
  49. * secure boot should not or could not be correctly enabled.
  50. *
  51. *
  52. * @return ESP_ERR_INVALID_STATE if efuse state doesn't allow
  53. * secure boot to be enabled cleanly. ESP_OK if secure boot
  54. * is enabled on this chip from now on.
  55. */
  56. esp_err_t esp_secure_boot_permanently_enable(void);
  57. /** @brief Verify the secure boot signature (determinstic ECDSA w/ SHA256) appended to some binary data in flash.
  58. *
  59. * Public key is compiled into the calling program. See docs/security/secure-boot.rst for details.
  60. *
  61. * @param src_addr Starting offset of the data in flash.
  62. * @param length Length of data in bytes. Signature is appended -after- length bytes.
  63. *
  64. * If flash encryption is enabled, the image will be transparently decrypted while being verified.
  65. *
  66. * @return ESP_OK if signature is valid, ESP_ERR_INVALID_STATE if
  67. * signature fails, ESP_FAIL for other failures (ie can't read flash).
  68. */
  69. esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length);
  70. /** @brief Secure boot verification block, on-flash data format. */
  71. typedef struct {
  72. uint32_t version;
  73. uint8_t signature[64];
  74. } esp_secure_boot_sig_block_t;
  75. #define FLASH_OFFS_SECURE_BOOT_IV_DIGEST 0
  76. /** @brief Secure boot IV+digest header */
  77. typedef struct {
  78. uint8_t iv[128];
  79. uint8_t digest[64];
  80. } esp_secure_boot_iv_digest_t;
  81. #ifdef __cplusplus
  82. }
  83. #endif