secure_boot.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "sdkconfig.h"
  14. #ifndef _ROM_SECURE_BOOT_H_
  15. #define _ROM_SECURE_BOOT_H_
  16. #include <stdint.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. void ets_secure_boot_start(void);
  21. void ets_secure_boot_finish(void);
  22. void ets_secure_boot_hash(const uint32_t *buf);
  23. void ets_secure_boot_obtain(void);
  24. int ets_secure_boot_check(uint32_t *buf);
  25. void ets_secure_boot_rd_iv(uint32_t *buf);
  26. void ets_secure_boot_rd_abstract(uint32_t *buf);
  27. bool ets_secure_boot_check_start(uint8_t abs_index, uint32_t iv_addr);
  28. int ets_secure_boot_check_finish(uint32_t *abstract);
  29. #ifdef CONFIG_ESP32_REV_MIN_3
  30. #define SECURE_BOOT_NUM_BLOCKS 1
  31. typedef enum {
  32. SBV2_SUCCESS = 0x3A5A5AA5,
  33. SBV2_FAILED = 0xA533885A,
  34. } secure_boot_v2_status_t;
  35. /* Secure Boot Version 2 - Public Key format */
  36. typedef struct {
  37. uint8_t n[384]; /* Public key modulus */
  38. uint32_t e; /* Public key exponent */
  39. uint8_t rinv[384];
  40. uint32_t mdash;
  41. } ets_rsa_pubkey_t;
  42. /* Secure Boot Version 2 signature format for ESP32 ECO3 */
  43. typedef struct {
  44. uint8_t magic_byte;
  45. uint8_t version;
  46. uint8_t _reserved1;
  47. uint8_t _reserved2;
  48. uint8_t image_digest[32];
  49. ets_rsa_pubkey_t key;
  50. uint8_t signature[384];
  51. uint32_t block_crc;
  52. uint8_t _padding[16];
  53. } ets_secure_boot_sig_block_t;
  54. /* Multiple key block support */
  55. struct ets_secure_boot_signature {
  56. ets_secure_boot_sig_block_t block[SECURE_BOOT_NUM_BLOCKS];
  57. uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_NUM_BLOCKS)];
  58. };
  59. typedef struct ets_secure_boot_signature ets_secure_boot_signature_t;
  60. /** @brief Verifies the signature block appended to a firmware image. Implemented in the ROM.
  61. *
  62. * This function is used to verify the bootloader before burning its public key hash into Efuse.
  63. * Also, it is used to verify the app on loading the image on boot and on OTA.
  64. *
  65. * @param sig The signature block flashed aligned 4096 bytes from the firmware.
  66. * @param image_digest The SHA-256 Digest of the firmware to be verified
  67. * @param trusted_key_digest The SHA-256 Digest of the public key (ets_rsa_pubkey_t) of a single signature block.
  68. * @param verified_digest RSA-PSS signature of image_digest. Pass an uninitialised array.
  69. *
  70. * @return SBV2_SUCCESS if signature is valid
  71. * SBV2_FAILED for failures.
  72. */
  73. secure_boot_v2_status_t ets_secure_boot_verify_signature(const ets_secure_boot_signature_t *sig, const uint8_t *image_digest, const uint8_t *trusted_key_digest, uint8_t *verified_digest);
  74. /** @brief This function verifies the 1st stage bootloader. Implemented in the ROM.
  75. * Reboots post verification. It reads the Efuse key for verification of the public key.
  76. *
  77. * This function is not used in the current workflow.
  78. *
  79. */
  80. void ets_secure_boot_verify_boot_bootloader(void);
  81. /** @brief Confirms if the secure boot V2 has been enabled. Implemented in the ROM.
  82. *
  83. * In ESP32-ECO3 - It checks the value of ABS_DONE_1 in EFuse.
  84. *
  85. * @return true if is Secure boot v2 has been enabled
  86. * False if Secure boot v2 has not been enabled.
  87. */
  88. bool ets_use_secure_boot_v2();
  89. #endif /* CONFIG_ESP32_REV_MIN_3 */
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif /* _ROM_SECURE_BOOT_H_ */