secure_boot.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2015-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. #pragma once
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "ets_sys.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. /* Anti-FI measure: use full words for success/fail, instead of
  28. 0/non-zero
  29. */
  30. typedef enum {
  31. SB_SUCCESS = 0x3A5A5AA5,
  32. SB_FAILED = 0x7533885E,
  33. } secure_boot_status_t;
  34. /* Verify bootloader image (reconfigures cache to map,
  35. loads trusted key digests from efuse)
  36. If allow_key_revoke is true and aggressive revoke efuse is set,
  37. any failed signature has its associated key revoked in efuse.
  38. If result is ETS_OK, the "simple hash" of the bootloader
  39. is copied into verified_hash.
  40. */
  41. int ets_secure_boot_verify_bootloader(uint8_t *verified_hash, bool allow_key_revoke);
  42. /* Verify bootloader image (reconfigures cache to map), with
  43. key digests provided as parameters.)
  44. Can be used to verify secure boot status before enabling
  45. secure boot permanently.
  46. If result is ETS_OK, the "simple hash" of the bootloader is
  47. copied into verified_hash.
  48. */
  49. 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);
  50. /* Verify supplied signature against supplied digest, using
  51. supplied trusted key digests.
  52. Doesn't reconfigure cache or any other hardware access.
  53. */
  54. 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);
  55. /* Read key digests from efuse. Any revoked/missing digests will be
  56. marked as NULL
  57. Returns 0 if at least one valid digest was found.
  58. */
  59. ETS_STATUS ets_secure_boot_read_key_digests(ets_secure_boot_key_digests_t *trusted_keys);
  60. #define CRC_SIGN_BLOCK_LEN 1196
  61. #define SIG_BLOCK_PADDING 4096
  62. #define ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC 0xE7
  63. /* Secure Boot V2 signature block (up to 3 can be appended) */
  64. struct ets_secure_boot_sig_block {
  65. uint8_t magic_byte;
  66. uint8_t version;
  67. uint8_t _reserved1;
  68. uint8_t _reserved2;
  69. uint8_t image_digest[32];
  70. ets_rsa_pubkey_t key;
  71. uint8_t signature[384];
  72. uint32_t block_crc;
  73. uint8_t _padding[16];
  74. };
  75. _Static_assert(sizeof(ets_secure_boot_sig_block_t) == 1216, "ets_secure_boot_sig_block_t should occupy 1216 Bytes in memory");
  76. #define SECURE_BOOT_NUM_BLOCKS 3
  77. /* V2 Secure boot signature sector (up to 3 blocks) */
  78. struct ets_secure_boot_signature {
  79. ets_secure_boot_sig_block_t block[SECURE_BOOT_NUM_BLOCKS];
  80. uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_NUM_BLOCKS)];
  81. };
  82. _Static_assert(sizeof(ets_secure_boot_signature_t) == 4096, "ets_secure_boot_signature_t should occupy 4096 Bytes in memory");
  83. #define MAX_KEY_DIGESTS 3
  84. struct ets_secure_boot_key_digests {
  85. const void *key_digests[MAX_KEY_DIGESTS];
  86. bool allow_key_revoke;
  87. };
  88. #ifdef __cplusplus
  89. }
  90. #endif