secure_boot.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2020 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef _ROM_SECURE_BOOT_H_
  15. #define _ROM_SECURE_BOOT_H_
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "ets_sys.h"
  19. #include "rsa_pss.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. struct ets_secure_boot_sig_block;
  24. struct ets_secure_boot_signature_t;
  25. typedef struct ets_secure_boot_sig_block ets_secure_boot_sig_block_t;
  26. typedef struct ets_secure_boot_signature ets_secure_boot_signature_t;
  27. typedef struct ets_secure_boot_key_digests ets_secure_boot_key_digests_t;
  28. /* Anti-FI measure: use full words for success/fail, instead of
  29. 0/non-zero
  30. */
  31. typedef enum {
  32. SB_SUCCESS = 0x3A5A5AA5,
  33. SB_FAILED = 0x7533885E,
  34. } ets_secure_boot_status_t;
  35. /* Verify and stage-load the bootloader image
  36. (reconfigures cache to map, loads trusted key digests from efuse,
  37. copies the bootloader into the staging buffer.)
  38. If allow_key_revoke is true and aggressive revoke efuse is set,
  39. any failed signature has its associated key revoked in efuse.
  40. If result is SB_SUCCESS, the "simple hash" of the bootloader
  41. is copied into verified_hash.
  42. */
  43. ets_secure_boot_status_t ets_secure_boot_verify_stage_bootloader(uint8_t *verified_hash, bool allow_key_revoke);
  44. /* Verify bootloader image (reconfigures cache to map),
  45. with key digests provided as parameters.)
  46. Can be used to verify secure boot status before enabling
  47. secure boot permanently.
  48. If stage_load parameter is true, bootloader is copied into staging
  49. buffer in RAM at the same time.
  50. If result is SB_SUCCESS, the "simple hash" of the bootloader is
  51. copied into verified_hash.
  52. */
  53. 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);
  54. /* Read key digests from efuse. Any revoked/missing digests will be
  55. marked as NULL
  56. */
  57. ETS_STATUS ets_secure_boot_read_key_digests(ets_secure_boot_key_digests_t *trusted_keys);
  58. /* Verify supplied signature against supplied digest, using
  59. supplied trusted key digests.
  60. Doesn't reconfigure cache or any other hardware access except for RSA peripheral.
  61. If result is SB_SUCCESS, the image_digest value is copied into verified_digest.
  62. */
  63. 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);
  64. /* Revoke a public key digest in efuse.
  65. @param index Digest to revoke. Must be 0, 1 or 2.
  66. */
  67. void ets_secure_boot_revoke_public_key_digest(int index);
  68. #define CRC_SIGN_BLOCK_LEN 1196
  69. #define SIG_BLOCK_PADDING 4096
  70. #define ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC 0xE7
  71. /* Secure Boot V2 signature block
  72. (Up to 3 in a signature sector are appended to the image)
  73. */
  74. struct ets_secure_boot_sig_block {
  75. uint8_t magic_byte;
  76. uint8_t version;
  77. uint8_t _reserved1;
  78. uint8_t _reserved2;
  79. uint8_t image_digest[32];
  80. ets_rsa_pubkey_t key;
  81. uint8_t signature[384];
  82. uint32_t block_crc;
  83. uint8_t _padding[16];
  84. };
  85. _Static_assert(sizeof(ets_secure_boot_sig_block_t) == 1216, "invalid sig block size");
  86. #define SECURE_BOOT_NUM_BLOCKS 3
  87. /* V2 Secure boot signature sector (up to 3 blocks) */
  88. struct ets_secure_boot_signature {
  89. ets_secure_boot_sig_block_t block[SECURE_BOOT_NUM_BLOCKS];
  90. uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_NUM_BLOCKS)];
  91. };
  92. _Static_assert(sizeof(ets_secure_boot_signature_t) == 4096, "invalid sig sector size");
  93. #define MAX_KEY_DIGESTS 3
  94. struct ets_secure_boot_key_digests {
  95. const void *key_digests[MAX_KEY_DIGESTS];
  96. bool allow_key_revoke;
  97. };
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. #endif /* _ROM_SECURE_BOOT_H_ */