secure_boot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include "esp_log.h"
  15. #include "esp_secure_boot.h"
  16. #include "soc/efuse_reg.h"
  17. #include "bootloader_flash.h"
  18. #include "bootloader_sha.h"
  19. #include "bootloader_utility.h"
  20. #include "esp_rom_crc.h"
  21. #include "esp_efuse.h"
  22. #include "esp_efuse_table.h"
  23. #include "esp32s2/rom/secure_boot.h"
  24. static const char *TAG = "secure_boot_v2";
  25. #define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
  26. #define SIG_BLOCK_MAGIC_BYTE 0xe7
  27. #define CRC_SIGN_BLOCK_LEN 1196
  28. #define SIG_BLOCK_PADDING 4096
  29. #define DIGEST_LEN 32
  30. /* A signature block is valid when it has correct magic byte, crc and image digest. */
  31. static esp_err_t validate_signature_block(int block_num, const ets_secure_boot_signature_t *sig_block, uint8_t *digest)
  32. {
  33. uint32_t crc = esp_rom_crc32_le(0, (uint8_t *)&sig_block->block[block_num], CRC_SIGN_BLOCK_LEN);
  34. if (sig_block->block[block_num].magic_byte != SIG_BLOCK_MAGIC_BYTE) {
  35. // All signature blocks have been parsed, no new signature block present.
  36. ESP_LOGD(TAG, "Signature block(%d) invalid/absent.", block_num);
  37. return ESP_FAIL;
  38. }
  39. if (sig_block->block[block_num].block_crc != crc) {
  40. ESP_LOGE(TAG, "Magic byte correct but incorrect crc.");
  41. return ESP_FAIL;
  42. }
  43. if (memcmp(digest, sig_block->block[block_num].image_digest, DIGEST_LEN)) {
  44. ESP_LOGE(TAG, "Magic byte & CRC correct but incorrect image digest.");
  45. return ESP_FAIL;
  46. } else {
  47. ESP_LOGD(TAG, "valid signature block(%d) found", block_num);
  48. return ESP_OK;
  49. }
  50. return ESP_FAIL;
  51. }
  52. // Inputs the flash_offset and length of an image(app or bootloader), validates & verifies its secure boot v2 signature.
  53. // Generates the public key digests of the valid public keys in a signature block and writes it into trusted_keys.
  54. // The key_digests in trusted keys whose signature blocks are invalid will be set to NULL.
  55. static esp_err_t secure_boot_v2_digest_generate(uint32_t flash_offset, uint32_t flash_size, ets_secure_boot_key_digests_t * const trusted_keys)
  56. {
  57. int i = 0;
  58. esp_err_t ret = ESP_FAIL;
  59. uint8_t image_digest[DIGEST_LEN] = {0};
  60. uint8_t public_key_digests[SECURE_BOOT_NUM_BLOCKS][DIGEST_LEN];
  61. size_t sig_block_addr = flash_offset + ALIGN_UP(flash_size, FLASH_SECTOR_SIZE);
  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 block");
  68. const ets_secure_boot_signature_t *sig_block = bootloader_mmap(sig_block_addr, sizeof(ets_secure_boot_signature_t));
  69. if (sig_block == 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. for (i = 0; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  74. ret = validate_signature_block(i, sig_block, image_digest);
  75. if (ret != ESP_OK) {
  76. break;
  77. }
  78. /* Generating the SHA of the public key components in the signature block */
  79. bootloader_sha256_handle_t sig_block_sha;
  80. sig_block_sha = bootloader_sha256_start();
  81. bootloader_sha256_data(sig_block_sha, &sig_block->block[i].key, sizeof(sig_block->block[i].key));
  82. bootloader_sha256_finish(sig_block_sha, public_key_digests[i]);
  83. memcpy((uint8_t *)trusted_keys->key_digests[0], public_key_digests[i], DIGEST_LEN); // Overwriting 0th index to verify each valid signature block
  84. /* A signature block is verified when it is valid and the signature in its signature block can be verified with a valid public key */
  85. uint8_t verified_digest[DIGEST_LEN] = {0};
  86. ets_secure_boot_status_t r = ets_secure_boot_verify_signature(sig_block, image_digest, trusted_keys, verified_digest);
  87. if (r != SB_SUCCESS) {
  88. ESP_LOGE(TAG, "Secure boot key (%d) verification failed.", i);
  89. ret = ESP_FAIL;
  90. goto exit;
  91. }
  92. }
  93. // At least 1 verified signature block found.
  94. if (i > 0) {
  95. // validate_signature_block returns ESP_FAIL when a sig block is absent, which isn't an error.
  96. while (--i) {
  97. trusted_keys->key_digests[i] = public_key_digests[i];
  98. }
  99. ret = ESP_OK;
  100. }
  101. exit:
  102. /* Set the pointer to an invalid/absent block to NULL */
  103. while (i < SECURE_BOOT_NUM_BLOCKS) {
  104. trusted_keys->key_digests[i] = NULL;
  105. i++;
  106. }
  107. ESP_LOGI(TAG, "Secure boot verification success.");
  108. bootloader_munmap(sig_block);
  109. return ret;
  110. }
  111. /* Traverses ets_secure_boot_key_digests_t to find the number of non-null key_digests */
  112. static uint8_t get_signature_block_count(ets_secure_boot_key_digests_t * const trusted_keys) {
  113. uint8_t bootloader_sig_block_count = 0;
  114. for (uint8_t i = 0; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  115. if (trusted_keys->key_digests[i] != NULL) {
  116. bootloader_sig_block_count++;
  117. for (uint8_t j = 0; j < DIGEST_LEN ; j++) {
  118. ESP_LOGD(TAG, "Secure Boot Digest %d: 0x%x", i, *(((uint8_t *)trusted_keys->key_digests[i]) + j));
  119. }
  120. }
  121. }
  122. return bootloader_sig_block_count;
  123. }
  124. esp_err_t esp_secure_boot_v2_permanently_enable(const esp_image_metadata_t *image_data)
  125. {
  126. ESP_LOGI(TAG, "enabling secure boot v2 - ESP32-S2...");
  127. if (esp_secure_boot_enabled()) {
  128. ESP_LOGI(TAG, "secure boot v2 is already enabled, continuing..");
  129. return ESP_OK;
  130. }
  131. esp_err_t ret;
  132. /* Verify the bootloader */
  133. esp_image_metadata_t bootloader_data = { 0 };
  134. ret = esp_image_verify_bootloader_data(&bootloader_data);
  135. if (ret != ESP_OK) {
  136. ESP_LOGE(TAG, "bootloader image appears invalid! error %d", ret);
  137. return ret;
  138. }
  139. /* Check if secure boot digests are present */
  140. bool has_secure_boot_digest = ets_efuse_find_purpose(ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST0, NULL);
  141. has_secure_boot_digest |= ets_efuse_find_purpose(ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST1, NULL);
  142. has_secure_boot_digest |= ets_efuse_find_purpose(ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST2, NULL);
  143. ESP_LOGI(TAG, "Secure boot digests %s", has_secure_boot_digest ? "already present":"absent, generating..");
  144. ets_efuse_clear_program_registers();
  145. uint8_t i, j, bootloader_sig_block_count = 0;
  146. if (!has_secure_boot_digest) {
  147. ets_secure_boot_key_digests_t boot_trusted_keys, app_trusted_keys;
  148. uint8_t boot_trusted_key_data[SECURE_BOOT_NUM_BLOCKS][DIGEST_LEN] = {0}, app_trusted_key_data[SECURE_BOOT_NUM_BLOCKS][DIGEST_LEN] = {0};
  149. for(i = 0; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  150. boot_trusted_keys.key_digests[i] = boot_trusted_key_data[i];
  151. app_trusted_keys.key_digests[i] = app_trusted_key_data[i];
  152. }
  153. /* Generate the bootloader public key digests */
  154. ret = secure_boot_v2_digest_generate(bootloader_data.start_addr, bootloader_data.image_len - SIG_BLOCK_PADDING, &boot_trusted_keys);
  155. if (ret != ESP_OK) {
  156. ESP_LOGE(TAG, "Public key digest generation failed.");
  157. return ret;
  158. }
  159. bootloader_sig_block_count = get_signature_block_count(&boot_trusted_keys);
  160. if (bootloader_sig_block_count <= 0) {
  161. ESP_LOGI(TAG, "No valid signature blocks found. %d signature block(s) found.", bootloader_sig_block_count);
  162. return ESP_FAIL;
  163. }
  164. ESP_LOGI(TAG, "%d signature block(s) found appended to the bootloader.", bootloader_sig_block_count);
  165. int unused_key_slots = ets_efuse_count_unused_key_blocks();
  166. if (bootloader_sig_block_count > unused_key_slots) {
  167. ESP_LOGE(TAG, "Bootloader signatures(%d) more than available key slots(%d).", bootloader_sig_block_count, unused_key_slots);
  168. return ESP_FAIL;
  169. }
  170. for (i = 0; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  171. if (boot_trusted_keys.key_digests[i] == NULL) {
  172. break;
  173. }
  174. const uint32_t secure_boot_key_purpose[SECURE_BOOT_NUM_BLOCKS] = { ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST0,
  175. ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST1, ETS_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST2 };
  176. ets_efuse_block_t block = ets_efuse_find_unused_key_block();
  177. if (block == ETS_EFUSE_BLOCK_MAX) {
  178. ESP_LOGE(TAG, "Key blocks not available.");
  179. return ESP_FAIL;
  180. }
  181. int r = ets_efuse_write_key(block, secure_boot_key_purpose[i], boot_trusted_keys.key_digests[i], DIGEST_LEN);
  182. if (r != 0) {
  183. ESP_LOGE(TAG, "Failed to write efuse block %d with purpose %d. Can't continue.", block, secure_boot_key_purpose[i]);
  184. return ESP_FAIL;
  185. }
  186. r = esp_efuse_set_write_protect(block);
  187. if (r != 0) {
  188. ESP_LOGE(TAG, "Failed to write protect efuse block %d. Can't continue.", block);
  189. return ESP_FAIL;
  190. }
  191. }
  192. /* Generate the application public key digests */
  193. ret = secure_boot_v2_digest_generate(image_data->start_addr, image_data->image_len - SIG_BLOCK_PADDING, &app_trusted_keys);
  194. if (ret != ESP_OK) {
  195. ESP_LOGE(TAG, "Application signature block is invalid.");
  196. return ret;
  197. }
  198. int app_sig_block_count = get_signature_block_count(&app_trusted_keys);
  199. ESP_LOGI(TAG, "%d signature block(s) found appended to the application.", app_sig_block_count);
  200. /* Confirm if atleast one the public key from the application matches a public key in the bootloader
  201. (Also, ensure if that public revoke bit is not set for the matched key) */
  202. bool match = false;
  203. const uint32_t revoke_bits[SECURE_BOOT_NUM_BLOCKS] = { EFUSE_SECURE_BOOT_KEY_REVOKE0,
  204. EFUSE_SECURE_BOOT_KEY_REVOKE1, EFUSE_SECURE_BOOT_KEY_REVOKE2 };
  205. for (i = 0; i < SECURE_BOOT_NUM_BLOCKS && match == false; i++) {
  206. if (REG_GET_BIT(EFUSE_RD_REPEAT_DATA1_REG, revoke_bits[i])) {
  207. ESP_LOGI(TAG, "Key block(%d) has been revoked.", i);
  208. continue; // skip if the key block is revoked
  209. }
  210. for (j = 0; j < SECURE_BOOT_NUM_BLOCKS; j++) {
  211. if (!memcmp(boot_trusted_key_data[i], app_trusted_key_data[j], DIGEST_LEN)) {
  212. ESP_LOGI(TAG, "Application key(%d) matches with bootloader key(%d).", j, i);
  213. match = true;
  214. break;
  215. }
  216. }
  217. }
  218. if (match == false) {
  219. ESP_LOGE(TAG, "No application key digest matches the bootloader key digest.");
  220. return ESP_FAIL;
  221. }
  222. /* Revoke the empty signature blocks */
  223. if (bootloader_sig_block_count < SECURE_BOOT_NUM_BLOCKS) {
  224. /* The revocation index can be 0, 1, 2. Bootloader count can be 1,2,3. */
  225. for (uint8_t i = bootloader_sig_block_count; i < SECURE_BOOT_NUM_BLOCKS; i++) {
  226. ESP_LOGI(TAG, "Revoking empty key digest slot (%d)...", i);
  227. ets_secure_boot_revoke_public_key_digest(i);
  228. }
  229. }
  230. }
  231. esp_err_t err = esp_efuse_batch_write_begin();
  232. if (err != ESP_OK) {
  233. ESP_LOGI(TAG, "Error batch programming security eFuses.");
  234. return err;
  235. }
  236. __attribute__((unused)) static const uint8_t enable = 1;
  237. esp_efuse_write_field_bit(ESP_EFUSE_DIS_BOOT_REMAP);
  238. esp_efuse_write_field_bit(ESP_EFUSE_DIS_LEGACY_SPI_BOOT);
  239. #ifdef CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE
  240. ESP_LOGI(TAG, "Enabling Security download mode...");
  241. esp_efuse_write_field_bit(ESP_EFUSE_ENABLE_SECURITY_DOWNLOAD);
  242. #else
  243. ESP_LOGW(TAG, "Not enabling Security download mode - SECURITY COMPROMISED");
  244. #endif
  245. #ifndef CONFIG_SECURE_BOOT_ALLOW_JTAG
  246. ESP_LOGI(TAG, "Disable hardware & software JTAG...");
  247. esp_efuse_write_field_bit(ESP_EFUSE_HARD_DIS_JTAG);
  248. esp_efuse_write_field_bit(ESP_EFUSE_SOFT_DIS_JTAG);
  249. #else
  250. ESP_LOGW(TAG, "Not disabling JTAG - SECURITY COMPROMISED");
  251. #endif
  252. #ifdef CONFIG_SECURE_BOOT_ENABLE_AGGRESSIVE_KEY_REVOKE
  253. esp_efuse_write_field_bit(ESP_EFUSE_SECURE_BOOT_AGGRESSIVE_REVOKE);
  254. #endif
  255. esp_efuse_write_field_bit(ESP_EFUSE_SECURE_BOOT_EN);
  256. err = esp_efuse_batch_write_commit();
  257. if (err != ESP_OK) {
  258. ESP_LOGI(TAG, "Error programming security eFuses.");
  259. return err;
  260. }
  261. #ifdef CONFIG_SECURE_BOOT_ENABLE_AGGRESSIVE_KEY_REVOKE
  262. assert(ets_efuse_secure_boot_aggressive_revoke_enabled());
  263. #endif
  264. assert(esp_rom_efuse_is_secure_boot_enabled());
  265. ESP_LOGI(TAG, "Secure boot permanently enabled");
  266. return ESP_OK;
  267. }