secure_boot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "sdkconfig.h"
  8. #include "esp_log.h"
  9. #include "esp_secure_boot.h"
  10. #include "bootloader_flash_priv.h"
  11. #include "bootloader_sha.h"
  12. #include "bootloader_utility.h"
  13. #include "esp_image_format.h"
  14. #include "esp_efuse.h"
  15. #include "esp_efuse_table.h"
  16. /* The following API implementations are used only when called
  17. * from the bootloader code.
  18. */
  19. #ifdef CONFIG_SECURE_BOOT_V2_ENABLED
  20. #define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
  21. static const char *TAG = "secure_boot_v2";
  22. /* A signature block is valid when it has correct magic byte, crc and image digest. */
  23. static esp_err_t validate_signature_block(const ets_secure_boot_sig_block_t *block, int block_num, const uint8_t *image_digest)
  24. {
  25. if (block->magic_byte != ETS_SECURE_BOOT_V2_SIGNATURE_MAGIC) {
  26. // All signature blocks have been parsed, no new signature block present.
  27. ESP_LOGD(TAG, "Signature block(%d) invalid/absent.", block_num);
  28. return ESP_FAIL;
  29. }
  30. if (block->block_crc != esp_rom_crc32_le(0, (uint8_t *)block, CRC_SIGN_BLOCK_LEN)) {
  31. ESP_LOGE(TAG, "Magic byte correct but incorrect crc.");
  32. return ESP_FAIL;
  33. }
  34. if (memcmp(image_digest, block->image_digest, ESP_SECURE_BOOT_DIGEST_LEN)) {
  35. ESP_LOGE(TAG, "Magic byte & CRC correct but incorrect image digest.");
  36. return ESP_FAIL;
  37. } else {
  38. ESP_LOGD(TAG, "valid signature block(%d) found", block_num);
  39. return ESP_OK;
  40. }
  41. return ESP_FAIL;
  42. }
  43. /* Generates the public key digests of the valid public keys in an image's
  44. signature block, verifies each signature, and stores the key digests in the
  45. public_key_digests structure.
  46. @param flash_offset Image offset in flash
  47. @param flash_size Image size in flash (not including signature block)
  48. @param[out] public_key_digests Pointer to structure to hold the key digests for valid sig blocks
  49. Note that this function doesn't read any eFuses, so it doesn't know if the
  50. keys are ultimately trusted by the hardware or not
  51. @return - ESP_OK if no signatures failed to verify, or if no valid signature blocks are found at all.
  52. - ESP_FAIL if there's a valid signature block that doesn't verify using the included public key (unexpected!)
  53. */
  54. static esp_err_t s_calculate_image_public_key_digests(uint32_t flash_offset, uint32_t flash_size, esp_image_sig_public_key_digests_t *public_key_digests)
  55. {
  56. esp_err_t ret;
  57. uint8_t image_digest[ESP_SECURE_BOOT_DIGEST_LEN] = {0};
  58. uint8_t __attribute__((aligned(4))) key_digest[ESP_SECURE_BOOT_DIGEST_LEN] = {0};
  59. size_t sig_block_addr = flash_offset + ALIGN_UP(flash_size, FLASH_SECTOR_SIZE);
  60. ESP_LOGD(TAG, "calculating public key digests for sig blocks of image offset 0x%x (sig block offset 0x%x)", flash_offset, sig_block_addr);
  61. bzero(public_key_digests, sizeof(esp_image_sig_public_key_digests_t));
  62. ret = bootloader_sha256_flash_contents(flash_offset, sig_block_addr - flash_offset, image_digest);
  63. if (ret != ESP_OK) {
  64. ESP_LOGE(TAG, "error generating image digest, %d", ret);
  65. return ret;
  66. }
  67. ESP_LOGD(TAG, "reading signature(s)");
  68. const ets_secure_boot_signature_t *signatures = bootloader_mmap(sig_block_addr, sizeof(ets_secure_boot_signature_t));
  69. if (signatures == NULL) {
  70. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", sig_block_addr, sizeof(ets_secure_boot_signature_t));
  71. return ESP_FAIL;
  72. }
  73. /* Validating Signature block */
  74. for (unsigned i = 0; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  75. const ets_secure_boot_sig_block_t *block = &signatures->block[i];
  76. ret = validate_signature_block(block, i, image_digest);
  77. if (ret != ESP_OK) {
  78. ret = ESP_OK; // past the last valid signature block
  79. break;
  80. }
  81. /* Generating the SHA of the public key components in the signature block */
  82. bootloader_sha256_handle_t sig_block_sha;
  83. sig_block_sha = bootloader_sha256_start();
  84. bootloader_sha256_data(sig_block_sha, &block->key, sizeof(block->key));
  85. bootloader_sha256_finish(sig_block_sha, key_digest);
  86. // Check we can verify the image using this signature and this key
  87. uint8_t temp_verified_digest[ESP_SECURE_BOOT_DIGEST_LEN];
  88. bool verified = ets_rsa_pss_verify(&block->key, block->signature, image_digest, temp_verified_digest);
  89. if (!verified) {
  90. /* We don't expect this: the signature blocks before we enable secure boot should all be verifiable or invalid,
  91. so this is a fatal error
  92. */
  93. ret = ESP_FAIL;
  94. ESP_LOGE(TAG, "Secure boot key (%d) verification failed.", i);
  95. break;
  96. }
  97. ESP_LOGD(TAG, "Signature block (%d) is verified", i);
  98. /* Copy the key digest to the buffer provided by the caller */
  99. memcpy((void *)public_key_digests->key_digests[i], key_digest, ESP_SECURE_BOOT_DIGEST_LEN);
  100. public_key_digests->num_digests++;
  101. }
  102. if (ret == ESP_OK && public_key_digests->num_digests > 0) {
  103. ESP_LOGI(TAG, "Digests successfully calculated, %d valid signatures (image offset 0x%x)",
  104. public_key_digests->num_digests, flash_offset);
  105. }
  106. bootloader_munmap(signatures);
  107. return ret;
  108. }
  109. static esp_err_t check_and_generate_secure_boot_keys(const esp_image_metadata_t *image_data)
  110. {
  111. esp_err_t ret;
  112. #ifdef CONFIG_IDF_TARGET_ESP32
  113. esp_efuse_purpose_t secure_boot_key_purpose[SECURE_BOOT_NUM_BLOCKS] = {
  114. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_V2,
  115. };
  116. esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(EFUSE_BLK_SECURE_BOOT);
  117. if (coding_scheme != EFUSE_CODING_SCHEME_NONE) {
  118. ESP_LOGE(TAG, "No coding schemes are supported in secure boot v2.(Detected scheme: 0x%x)", coding_scheme);
  119. return ESP_ERR_NOT_SUPPORTED;
  120. }
  121. #else
  122. esp_efuse_purpose_t secure_boot_key_purpose[SECURE_BOOT_NUM_BLOCKS] = {
  123. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST0,
  124. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST1,
  125. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST2,
  126. };
  127. #endif // CONFIG_IDF_TARGET_ESP32
  128. /* Verify the bootloader */
  129. esp_image_metadata_t bootloader_data = { 0 };
  130. ret = esp_image_verify_bootloader_data(&bootloader_data);
  131. if (ret != ESP_OK) {
  132. ESP_LOGE(TAG, "bootloader image appears invalid! error %d", ret);
  133. return ret;
  134. }
  135. /* Initialize all efuse block entries to invalid (max) value */
  136. esp_efuse_block_t blocks[SECURE_BOOT_NUM_BLOCKS] = {[0 ... SECURE_BOOT_NUM_BLOCKS-1] = EFUSE_BLK_KEY_MAX};
  137. /* Check if secure boot digests are present */
  138. bool has_secure_boot_digest = false;
  139. for (unsigned i = 0; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  140. bool tmp_has_key = esp_efuse_find_purpose(secure_boot_key_purpose[i], &blocks[i]);
  141. if (tmp_has_key) { // For ESP32: esp_efuse_find_purpose() always returns True, need to check whether the key block is used or not.
  142. tmp_has_key &= !esp_efuse_key_block_unused(blocks[i]);
  143. }
  144. has_secure_boot_digest |= tmp_has_key;
  145. }
  146. esp_image_sig_public_key_digests_t boot_key_digests = {0};
  147. esp_image_sig_public_key_digests_t app_key_digests = {0};
  148. ESP_LOGI(TAG, "Secure boot digests %s", has_secure_boot_digest ? "already present":"absent, generating..");
  149. if (!has_secure_boot_digest) {
  150. /* Generate the bootloader public key digests */
  151. ret = s_calculate_image_public_key_digests(bootloader_data.start_addr, bootloader_data.image_len - SIG_BLOCK_PADDING, &boot_key_digests);
  152. if (ret != ESP_OK) {
  153. ESP_LOGE(TAG, "Bootloader signature block is invalid");
  154. return ret;
  155. }
  156. if (boot_key_digests.num_digests == 0) {
  157. ESP_LOGE(TAG, "No valid bootloader signature blocks found.");
  158. return ESP_FAIL;
  159. }
  160. ESP_LOGI(TAG, "%d signature block(s) found appended to the bootloader.", boot_key_digests.num_digests);
  161. ESP_LOGI(TAG, "Burning public key hash to eFuse");
  162. ret = esp_efuse_write_keys(secure_boot_key_purpose, boot_key_digests.key_digests, boot_key_digests.num_digests);
  163. if (ret != ESP_OK) {
  164. if (ret == ESP_ERR_NOT_ENOUGH_UNUSED_KEY_BLOCKS) {
  165. ESP_LOGE(TAG, "Bootloader signatures(%d) more than available key slots.", boot_key_digests.num_digests);
  166. } else {
  167. ESP_LOGE(TAG, "Failed to write efuse block with purpose (err=0x%x). Can't continue.", ret);
  168. }
  169. return ret;
  170. }
  171. } else {
  172. for (unsigned i = 0; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  173. /* Check if corresponding digest slot is used or not */
  174. if (blocks[i] == EFUSE_BLK_KEY_MAX) {
  175. ESP_LOGD(TAG, "SECURE_BOOT_DIGEST%d slot is not used", i);
  176. continue;
  177. }
  178. #if SOC_EFUSE_REVOKE_BOOT_KEY_DIGESTS
  179. if (esp_efuse_get_digest_revoke(i)) {
  180. continue;
  181. }
  182. #endif
  183. if (esp_efuse_get_key_dis_read(blocks[i])) {
  184. ESP_LOGE(TAG, "Key digest (BLK%d) read protected, aborting...", blocks[i]);
  185. return ESP_FAIL;
  186. }
  187. if (esp_efuse_block_is_empty(blocks[i])) {
  188. ESP_LOGE(TAG, "%d eFuse block is empty, aborting...", blocks[i]);
  189. return ESP_FAIL;
  190. }
  191. esp_efuse_set_key_dis_write(blocks[i]);
  192. ret = esp_efuse_read_block(blocks[i], boot_key_digests.key_digests[boot_key_digests.num_digests], 0,
  193. sizeof(boot_key_digests.key_digests[0]) * 8);
  194. if (ret) {
  195. ESP_LOGE(TAG, "Error during reading %d eFuse block (err=0x%x)", blocks[i], ret);
  196. return ret;
  197. }
  198. boot_key_digests.num_digests++;
  199. }
  200. if (boot_key_digests.num_digests == 0) {
  201. ESP_LOGE(TAG, "No valid pre-loaded public key digest in eFuse");
  202. return ESP_FAIL;
  203. }
  204. ESP_LOGW(TAG, "Using pre-loaded public key digest in eFuse");
  205. }
  206. /* Generate the application public key digests */
  207. ret = s_calculate_image_public_key_digests(image_data->start_addr, image_data->image_len - SIG_BLOCK_PADDING, &app_key_digests);
  208. if (ret != ESP_OK) {
  209. ESP_LOGE(TAG, "Application signature block is invalid.");
  210. return ret;
  211. }
  212. if (app_key_digests.num_digests == 0) {
  213. ESP_LOGE(TAG, "No valid applications signature blocks found.");
  214. return ESP_FAIL;
  215. }
  216. ESP_LOGI(TAG, "%d signature block(s) found appended to the app.", app_key_digests.num_digests);
  217. if (app_key_digests.num_digests > boot_key_digests.num_digests) {
  218. ESP_LOGW(TAG, "App has %d signature blocks but bootloader only has %d. Some keys missing from bootloader?");
  219. }
  220. /* Confirm if at least one public key from the application matches a public key in the bootloader
  221. (Also, ensure if that public revoke bit is not set for the matched key) */
  222. bool match = false;
  223. for (unsigned i = 0; i < boot_key_digests.num_digests; i++) {
  224. #if SOC_EFUSE_REVOKE_BOOT_KEY_DIGESTS
  225. if (esp_efuse_get_digest_revoke(i)) {
  226. ESP_LOGI(TAG, "Key block(%d) has been revoked.", i);
  227. continue; // skip if the key block is revoked
  228. }
  229. #endif // SOC_EFUSE_REVOKE_BOOT_KEY_DIGESTS
  230. for (unsigned j = 0; j < app_key_digests.num_digests; j++) {
  231. if (!memcmp(boot_key_digests.key_digests[i], app_key_digests.key_digests[j], ESP_SECURE_BOOT_DIGEST_LEN)) {
  232. ESP_LOGI(TAG, "Application key(%d) matches with bootloader key(%d).", j, i);
  233. match = true;
  234. }
  235. }
  236. }
  237. if (match == false) {
  238. ESP_LOGE(TAG, "No application key digest matches the bootloader key digest.");
  239. return ESP_FAIL;
  240. }
  241. #if SOC_EFUSE_REVOKE_BOOT_KEY_DIGESTS
  242. /* Revoke the empty signature blocks */
  243. if (boot_key_digests.num_digests < SECURE_BOOT_NUM_BLOCKS) {
  244. /* The revocation index can be 0, 1, 2. Bootloader count can be 1,2,3. */
  245. for (unsigned i = boot_key_digests.num_digests; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  246. ESP_LOGI(TAG, "Revoking empty key digest slot (%d)...", i);
  247. esp_efuse_set_digest_revoke(i);
  248. }
  249. }
  250. #endif // SOC_EFUSE_REVOKE_BOOT_KEY_DIGESTS
  251. return ESP_OK;
  252. }
  253. esp_err_t esp_secure_boot_v2_permanently_enable(const esp_image_metadata_t *image_data)
  254. {
  255. ESP_LOGI(TAG, "enabling secure boot v2...");
  256. if (esp_secure_boot_enabled()) {
  257. ESP_LOGI(TAG, "secure boot v2 is already enabled, continuing..");
  258. return ESP_OK;
  259. }
  260. esp_efuse_batch_write_begin(); /* Batch all efuse writes at the end of this function */
  261. esp_err_t err;
  262. err = check_and_generate_secure_boot_keys(image_data);
  263. if (err != ESP_OK) {
  264. esp_efuse_batch_write_cancel();
  265. return err;
  266. }
  267. ESP_LOGI(TAG, "blowing secure boot efuse...");
  268. err = esp_secure_boot_enable_secure_features();
  269. if (err != ESP_OK) {
  270. esp_efuse_batch_write_cancel();
  271. return err;
  272. }
  273. err = esp_efuse_batch_write_commit();
  274. if (err != ESP_OK) {
  275. ESP_LOGE(TAG, "Error programming security eFuses (err=0x%x).", err);
  276. return err;
  277. }
  278. #ifdef CONFIG_SECURE_BOOT_ENABLE_AGGRESSIVE_KEY_REVOKE
  279. assert(esp_efuse_read_field_bit(ESP_EFUSE_SECURE_BOOT_AGGRESSIVE_REVOKE));
  280. #endif
  281. assert(esp_secure_boot_enabled());
  282. ESP_LOGI(TAG, "Secure boot permanently enabled");
  283. return ESP_OK;
  284. }
  285. #endif // CONFIG_SECURE_BOOT_V2_ENABLED