secure_boot.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include "ets_sys.h"
  9. #include "rsa_pss.h"
  10. #include "esp_assert.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef struct ets_secure_boot_sig_block ets_secure_boot_sig_block_t;
  15. typedef struct ets_secure_boot_signature ets_secure_boot_signature_t;
  16. typedef struct ets_secure_boot_key_digests ets_secure_boot_key_digests_t;
  17. /* Anti-FI measure: use full words for success/fail, instead of
  18. 0/non-zero
  19. */
  20. typedef enum {
  21. SB_SUCCESS = 0x3A5A5AA5,
  22. SB_FAILED = 0x7533885E,
  23. } ets_secure_boot_status_t;
  24. /* Verify and stage-load the bootloader image
  25. (reconfigures cache to map, loads trusted key digests from efuse,
  26. copies the bootloader into the staging buffer.)
  27. If allow_key_revoke is true and aggressive revoke efuse is set,
  28. any failed signature has its associated key revoked in efuse.
  29. If result is SB_SUCCESS, the "simple hash" of the bootloader
  30. is copied into verified_hash.
  31. */
  32. ets_secure_boot_status_t ets_secure_boot_verify_stage_bootloader(uint8_t *verified_hash, bool allow_key_revoke);
  33. /* Verify bootloader image (reconfigures cache to map),
  34. with key digests provided as parameters.)
  35. Can be used to verify secure boot status before enabling
  36. secure boot permanently.
  37. If stage_load parameter is true, bootloader is copied into staging
  38. buffer in RAM at the same time.
  39. If result is SB_SUCCESS, the "simple hash" of the bootloader is
  40. copied into verified_hash.
  41. */
  42. ets_secure_boot_status_t ets_secure_boot_verify_bootloader_with_keys(uint8_t *verified_hash, const ets_secure_boot_key_digests_t *trusted_keys, bool stage_load);
  43. /* Read key digests from efuse. Any revoked/missing digests will be
  44. marked as NULL
  45. */
  46. ETS_STATUS ets_secure_boot_read_key_digests(ets_secure_boot_key_digests_t *trusted_keys);
  47. /* Verify supplied signature against supplied digest, using
  48. supplied trusted key digests.
  49. Doesn't reconfigure cache or any other hardware access except for RSA peripheral.
  50. If result is SB_SUCCESS, the image_digest value is copied into verified_digest.
  51. */
  52. ets_secure_boot_status_t ets_secure_boot_verify_signature(const ets_secure_boot_signature_t *sig, const uint8_t *image_digest, const ets_secure_boot_key_digests_t *trusted_keys, uint8_t *verified_digest);
  53. /* Revoke a public key digest in efuse.
  54. @param index Digest to revoke. Must be 0, 1 or 2.
  55. */
  56. void ets_secure_boot_revoke_public_key_digest(int index);
  57. #define CRC_SIGN_BLOCK_LEN 1196
  58. #define SIG_BLOCK_PADDING 4096
  59. #define ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC 0xE7
  60. /* Secure Boot V2 signature block
  61. (Up to 3 in a signature sector are appended to the image)
  62. */
  63. struct ets_secure_boot_sig_block {
  64. uint8_t magic_byte;
  65. uint8_t version;
  66. uint8_t _reserved1;
  67. uint8_t _reserved2;
  68. uint8_t image_digest[32];
  69. ets_rsa_pubkey_t key;
  70. uint8_t signature[384];
  71. uint32_t block_crc;
  72. uint8_t _padding[16];
  73. };
  74. ESP_STATIC_ASSERT(sizeof(ets_secure_boot_sig_block_t) == 1216, "invalid sig block size");
  75. #define SECURE_BOOT_NUM_BLOCKS 3
  76. /* V2 Secure boot signature sector (up to 3 blocks) */
  77. struct ets_secure_boot_signature {
  78. ets_secure_boot_sig_block_t block[SECURE_BOOT_NUM_BLOCKS];
  79. uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_NUM_BLOCKS)];
  80. };
  81. ESP_STATIC_ASSERT(sizeof(ets_secure_boot_signature_t) == 4096, "invalid sig sector size");
  82. #define MAX_KEY_DIGESTS 3
  83. struct ets_secure_boot_key_digests {
  84. const void *key_digests[MAX_KEY_DIGESTS];
  85. bool allow_key_revoke;
  86. };
  87. #ifdef __cplusplus
  88. }
  89. #endif