secure_boot.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #pragma once
  8. #include <stdint.h>
  9. #include "ets_sys.h"
  10. #include "esp_assert.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. void ets_secure_boot_start(void);
  15. void ets_secure_boot_finish(void);
  16. void ets_secure_boot_hash(const uint32_t *buf);
  17. void ets_secure_boot_obtain(void);
  18. int ets_secure_boot_check(uint32_t *buf);
  19. void ets_secure_boot_rd_iv(uint32_t *buf);
  20. void ets_secure_boot_rd_abstract(uint32_t *buf);
  21. bool ets_secure_boot_check_start(uint8_t abs_index, uint32_t iv_addr);
  22. int ets_secure_boot_check_finish(uint32_t *abstract);
  23. #if CONFIG_ESP32_REV_MIN_FULL >= 300
  24. #include "rsa_pss.h"
  25. #define SECURE_BOOT_NUM_BLOCKS 1
  26. #define CRC_SIGN_BLOCK_LEN 1196
  27. #define SIG_BLOCK_PADDING 4096
  28. #define ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC 0xE7
  29. // Anti-FI measure: use full words for success/fail internally, instead of 0/non-zero
  30. typedef enum {
  31. SBV2_SUCCESS = 0x3A5A5AA5,
  32. SB_SUCCESS = 0x3A5A5AA5,
  33. SBV2_FAILED = 0xA533885A,
  34. SB_FAILED = 0xA533885A,
  35. } secure_boot_v2_status_t;
  36. /* Secure Boot Version 2 signature format for ESP32 ECO3 */
  37. typedef struct {
  38. uint8_t magic_byte;
  39. uint8_t version;
  40. uint8_t _reserved1;
  41. uint8_t _reserved2;
  42. uint8_t image_digest[32];
  43. ets_rsa_pubkey_t key;
  44. uint8_t signature[384];
  45. uint32_t block_crc;
  46. uint8_t _padding[16];
  47. } ets_secure_boot_sig_block_t;
  48. ESP_STATIC_ASSERT(sizeof(ets_secure_boot_sig_block_t) == 1216, "invalid sig block size");
  49. /* ROM supports up to 3, but IDF only checks the first one (SECURE_BOOT_NUM_BLOCKS) */
  50. #define SECURE_BOOT_MAX_APPENDED_SIGN_BLOCKS_TO_IMAGE 3
  51. /* Multiple key block support */
  52. typedef struct {
  53. ets_secure_boot_sig_block_t block[SECURE_BOOT_MAX_APPENDED_SIGN_BLOCKS_TO_IMAGE];
  54. uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_MAX_APPENDED_SIGN_BLOCKS_TO_IMAGE)];
  55. } ets_secure_boot_signature_t;
  56. ESP_STATIC_ASSERT(sizeof(ets_secure_boot_signature_t) == 4096, "invalid sig sector size");
  57. typedef struct {
  58. const void *key_digests[SECURE_BOOT_NUM_BLOCKS];
  59. } ets_secure_boot_key_digests_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. (ROM implementation expects 3 blocks, sig->block[3]).
  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(void);
  89. #else
  90. #define SECURE_BOOT_NUM_BLOCKS 0
  91. #endif /* CONFIG_ESP32_REV_MIN_FULL >= 300 */
  92. #ifdef __cplusplus
  93. }
  94. #endif