secure_boot.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <stdbool.h>
  9. #include "ets_sys.h"
  10. #include "rsa_pss.h"
  11. #include "esp_assert.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. typedef struct ets_secure_boot_sig_block ets_secure_boot_sig_block_t;
  16. typedef struct ets_secure_boot_signature ets_secure_boot_signature_t;
  17. typedef struct ets_secure_boot_key_digests ets_secure_boot_key_digests_t;
  18. /* Anti-FI measure: use full words for success/fail, instead of
  19. 0/non-zero
  20. */
  21. typedef enum {
  22. SB_SUCCESS = 0x3A5A5AA5,
  23. SB_FAILED = 0x7533885E,
  24. } secure_boot_status_t;
  25. /* Verify bootloader image (reconfigures cache to map), with
  26. key digests provided as parameters.)
  27. Can be used to verify secure boot status before enabling
  28. secure boot permanently.
  29. If result is ETS_OK, the "simple hash" of the bootloader is
  30. copied into verified_hash.
  31. */
  32. 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);
  33. /* Verify supplied signature against supplied digest, using
  34. supplied trusted key digests.
  35. Doesn't reconfigure cache or any other hardware access.
  36. */
  37. 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);
  38. /* Read key digests from efuse. Any revoked/missing digests will be
  39. marked as NULL
  40. Returns 0 if at least one valid digest was found.
  41. */
  42. ETS_STATUS ets_secure_boot_read_key_digests(ets_secure_boot_key_digests_t *trusted_keys);
  43. #define CRC_SIGN_BLOCK_LEN 1196
  44. #define SIG_BLOCK_PADDING 4096
  45. #define ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC 0xE7
  46. /* Secure Boot V2 signature block (up to 3 can be appended) */
  47. struct ets_secure_boot_sig_block {
  48. uint8_t magic_byte;
  49. uint8_t version;
  50. uint8_t _reserved1;
  51. uint8_t _reserved2;
  52. uint8_t image_digest[32];
  53. ets_rsa_pubkey_t key;
  54. uint8_t signature[384];
  55. uint32_t block_crc;
  56. uint8_t _padding[16];
  57. };
  58. ESP_STATIC_ASSERT(sizeof(ets_secure_boot_sig_block_t) == 1216, "ets_secure_boot_sig_block_t should occupy 1216 Bytes in memory");
  59. #define SECURE_BOOT_NUM_BLOCKS 3
  60. /* V2 Secure boot signature sector (up to 3 blocks) */
  61. struct ets_secure_boot_signature {
  62. ets_secure_boot_sig_block_t block[SECURE_BOOT_NUM_BLOCKS];
  63. uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_NUM_BLOCKS)];
  64. };
  65. ESP_STATIC_ASSERT(sizeof(ets_secure_boot_signature_t) == 4096, "ets_secure_boot_signature_t should occupy 4096 Bytes in memory");
  66. #define MAX_KEY_DIGESTS 3
  67. struct ets_secure_boot_key_digests {
  68. const void *key_digests[MAX_KEY_DIGESTS];
  69. bool allow_key_revoke;
  70. };
  71. #ifdef __cplusplus
  72. }
  73. #endif