secure_boot.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "rsa_pss.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. struct ets_secure_boot_sig_block;
  23. struct ets_secure_boot_signature_t;
  24. typedef struct ets_secure_boot_sig_block ets_secure_boot_sig_block_t;
  25. typedef struct ets_secure_boot_signature ets_secure_boot_signature_t;
  26. typedef struct ets_secure_boot_key_digests ets_secure_boot_key_digests_t;
  27. /* Verify bootloader image (reconfigures cache to map,
  28. loads trusted key digests from efuse)
  29. If allow_key_revoke is true and aggressive revoke efuse is set,
  30. any failed signature has its associated key revoked in efuse.
  31. If result is ETS_OK, the "simple hash" of the bootloader
  32. is copied into verified_hash.
  33. */
  34. int ets_secure_boot_verify_bootloader(uint8_t *verified_hash, bool allow_key_revoke);
  35. /* Verify bootloader image (reconfigures cache to map), with
  36. key digests provided as parameters.)
  37. Can be used to verify secure boot status before enabling
  38. secure boot permanently.
  39. If result is ETS_OK, the "simple hash" of the bootloader is
  40. copied into verified_hash.
  41. */
  42. int ets_secure_boot_verify_bootloader_with_keys(uint8_t *verified_hash, const ets_secure_boot_key_digests_t *trusted_keys);
  43. /* Verify supplied signature against supplied digest, using
  44. supplied trusted key digests.
  45. Doesn't reconfigure cache or any other hardware access.
  46. */
  47. int 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);
  48. /* Read key digests from efuse. Any revoked/missing digests will be
  49. marked as NULL
  50. Returns 0 if at least one valid digest was found.
  51. */
  52. int ets_secure_boot_read_key_digests(ets_secure_boot_key_digests_t *trusted_keys);
  53. #define ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC 0xE7
  54. /* Secure Boot V2 signature block (up to 3 can be appended) */
  55. struct ets_secure_boot_sig_block {
  56. uint8_t magic_byte;
  57. uint8_t version;
  58. uint8_t _reserved1;
  59. uint8_t _reserved2;
  60. uint8_t image_digest[32];
  61. ets_rsa_pubkey_t key;
  62. uint8_t signature[384];
  63. uint32_t block_crc;
  64. uint8_t _padding[16];
  65. };
  66. _Static_assert(sizeof(ets_secure_boot_sig_block_t) == 1216, "invalid sig block size");
  67. #define SECURE_BOOT_NUM_BLOCKS 3
  68. /* V2 Secure boot signature sector (up to 3 blocks) */
  69. struct ets_secure_boot_signature {
  70. ets_secure_boot_sig_block_t block[SECURE_BOOT_NUM_BLOCKS];
  71. uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_NUM_BLOCKS)];
  72. };
  73. _Static_assert(sizeof(ets_secure_boot_signature_t) == 4096, "invalid sig sector size");
  74. struct ets_secure_boot_key_digests {
  75. const void *key_digests[3];
  76. bool allow_key_revoke;
  77. };
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif /* _ROM_SECURE_BOOT_H_ */