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