secure_boot.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * SPDX-FileCopyrightText: 2023 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 "ecdsa.h"
  11. #include "rsa_pss.h"
  12. #include "esp_assert.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #if CONFIG_SECURE_BOOT_V2_ENABLED || CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT
  17. typedef struct ets_secure_boot_sig_block ets_secure_boot_sig_block_t;
  18. typedef struct ets_secure_boot_signature ets_secure_boot_signature_t;
  19. typedef struct ets_secure_boot_key_digests ets_secure_boot_key_digests_t;
  20. /* Anti-FI measure: use full words for success/fail, instead of
  21. 0/non-zero
  22. */
  23. typedef enum {
  24. SB_SUCCESS = 0x3A5A5AA5,
  25. SB_FAILED = 0x7533885E,
  26. } ets_secure_boot_status_t;
  27. /* Verify bootloader image (reconfigures cache to map),
  28. with key digests provided as parameters.)
  29. Can be used to verify secure boot status before enabling
  30. secure boot permanently.
  31. If stage_load parameter is true, bootloader is copied into staging
  32. buffer in RAM at the same time.
  33. If result is SB_SUCCESS, the "simple hash" of the bootloader is
  34. copied into verified_hash.
  35. */
  36. 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);
  37. /* Read key digests from efuse. Any revoked/missing digests will be
  38. marked as NULL
  39. */
  40. ETS_STATUS ets_secure_boot_read_key_digests(ets_secure_boot_key_digests_t *trusted_keys);
  41. /* Verify supplied signature against supplied digest, using
  42. supplied trusted key digests.
  43. Doesn't reconfigure cache or any other hardware access except for RSA peripheral.
  44. If result is SB_SUCCESS, the image_digest value is copied into verified_digest.
  45. */
  46. 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);
  47. /* Revoke a public key digest in efuse.
  48. @param index Digest to revoke. Must be 0, 1 or 2.
  49. */
  50. void ets_secure_boot_revoke_public_key_digest(int index);
  51. #define CRC_SIGN_BLOCK_LEN 1196
  52. #define SIG_BLOCK_PADDING 4096
  53. #define ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC 0xE7
  54. /* Secure Boot V2 signature block
  55. (Up to 3 in a signature sector are appended to the image)
  56. */
  57. #if CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME
  58. struct ets_secure_boot_sig_block {
  59. uint8_t magic_byte;
  60. uint8_t version;
  61. uint8_t _reserved1;
  62. uint8_t _reserved2;
  63. uint8_t image_digest[32];
  64. ets_rsa_pubkey_t key;
  65. uint8_t signature[384];
  66. uint32_t block_crc;
  67. uint8_t _padding[16];
  68. };
  69. #elif CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME
  70. struct __attribute((packed)) ets_secure_boot_sig_block {
  71. uint8_t magic_byte;
  72. uint8_t version;
  73. uint8_t _reserved1;
  74. uint8_t _reserved2;
  75. uint8_t image_digest[32];
  76. struct {
  77. struct {
  78. uint8_t curve_id; /* ETS_ECDSA_CURVE_P192 / ETS_ECDSA_CURVE_P256 */
  79. uint8_t point[64]; /* X followed by Y (both little-endian), plus zero bytes if P192 */
  80. } key;
  81. uint8_t signature[64]; /* r followed by s (both little-endian) */
  82. uint8_t padding[1031];
  83. } ecdsa;
  84. uint32_t block_crc; /* note: crc covers all bytes in the structure before it, regardless of version field */
  85. uint8_t _padding[16];
  86. };
  87. #endif
  88. ESP_STATIC_ASSERT(sizeof(ets_secure_boot_sig_block_t) == 1216, "invalid sig block size");
  89. #define SECURE_BOOT_NUM_BLOCKS 3
  90. /* V2 Secure boot signature sector (up to 3 blocks) */
  91. struct ets_secure_boot_signature {
  92. ets_secure_boot_sig_block_t block[SECURE_BOOT_NUM_BLOCKS];
  93. uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_NUM_BLOCKS)];
  94. };
  95. ESP_STATIC_ASSERT(sizeof(ets_secure_boot_signature_t) == 4096, "invalid sig sector size");
  96. #define MAX_KEY_DIGESTS 3
  97. struct ets_secure_boot_key_digests {
  98. const void *key_digests[MAX_KEY_DIGESTS];
  99. bool allow_key_revoke;
  100. };
  101. #endif /* CONFIG_SECURE_BOOT_V2_ENABLED || CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT */
  102. #ifdef __cplusplus
  103. }
  104. #endif