1 /*
2  * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include "ets_sys.h"
11 #include "rsa_pss.h"
12 #include "esp_assert.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 typedef struct ets_secure_boot_sig_block ets_secure_boot_sig_block_t;
19 typedef struct ets_secure_boot_signature ets_secure_boot_signature_t;
20 typedef struct ets_secure_boot_key_digests ets_secure_boot_key_digests_t;
21 
22 /* Anti-FI measure: use full words for success/fail, instead of
23    0/non-zero
24 */
25 typedef enum {
26     SB_SUCCESS = 0x3A5A5AA5,
27     SB_FAILED = 0x7533885E,
28 } ets_secure_boot_status_t;
29 
30 
31 /* Verify and stage-load the bootloader image
32    (reconfigures cache to map, loads trusted key digests from efuse,
33    copies the bootloader into the staging buffer.)
34 
35    If allow_key_revoke is true and aggressive revoke efuse is set,
36    any failed signature has its associated key revoked in efuse.
37 
38    If result is SB_SUCCESS, the "simple hash" of the bootloader
39    is copied into verified_hash.
40 */
41 ets_secure_boot_status_t ets_secure_boot_verify_stage_bootloader(uint8_t *verified_hash, bool allow_key_revoke);
42 
43 /* Verify bootloader image (reconfigures cache to map),
44    with key digests provided as parameters.)
45 
46    Can be used to verify secure boot status before enabling
47    secure boot permanently.
48 
49    If stage_load parameter is true, bootloader is copied into staging
50    buffer in RAM at the same time.
51 
52    If result is SB_SUCCESS, the "simple hash" of the bootloader is
53    copied into verified_hash.
54 */
55 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);
56 
57 /* Read key digests from efuse. Any revoked/missing digests will be
58    marked as NULL
59 */
60 ETS_STATUS ets_secure_boot_read_key_digests(ets_secure_boot_key_digests_t *trusted_keys);
61 
62 /* Verify supplied signature against supplied digest, using
63    supplied trusted key digests.
64 
65    Doesn't reconfigure cache or any other hardware access except for RSA peripheral.
66 
67    If result is SB_SUCCESS, the image_digest value is copied into verified_digest.
68 */
69 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);
70 
71 /* Revoke a public key digest in efuse.
72    @param index Digest to revoke. Must be 0, 1 or 2.
73  */
74 void ets_secure_boot_revoke_public_key_digest(int index);
75 
76 #define CRC_SIGN_BLOCK_LEN 1196
77 #define SIG_BLOCK_PADDING 4096
78 #define ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC 0xE7
79 
80 /* Secure Boot V2 signature block
81 
82    (Up to 3 in a signature sector are appended to the image)
83  */
84 struct ets_secure_boot_sig_block {
85     uint8_t magic_byte;
86     uint8_t version;
87     uint8_t _reserved1;
88     uint8_t _reserved2;
89     uint8_t image_digest[32];
90     ets_rsa_pubkey_t key;
91     uint8_t signature[384];
92     uint32_t block_crc;
93     uint8_t _padding[16];
94 };
95 
96 ESP_STATIC_ASSERT(sizeof(ets_secure_boot_sig_block_t) == 1216, "invalid sig block size");
97 
98 #define SECURE_BOOT_NUM_BLOCKS 3
99 
100 /* V2 Secure boot signature sector (up to 3 blocks) */
101 struct ets_secure_boot_signature {
102     ets_secure_boot_sig_block_t block[SECURE_BOOT_NUM_BLOCKS];
103     uint8_t _padding[4096 - (sizeof(ets_secure_boot_sig_block_t) * SECURE_BOOT_NUM_BLOCKS)];
104 };
105 
106 ESP_STATIC_ASSERT(sizeof(ets_secure_boot_signature_t) == 4096, "invalid sig sector size");
107 
108 #define MAX_KEY_DIGESTS 3
109 
110 struct ets_secure_boot_key_digests {
111     const void *key_digests[MAX_KEY_DIGESTS];
112     bool allow_key_revoke;
113 };
114 
115 #ifdef __cplusplus
116 }
117 #endif
118