secure_boot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <strings.h>
  7. #include "sdkconfig.h"
  8. #include "esp_log.h"
  9. #include "esp_efuse.h"
  10. #include "esp_efuse_table.h"
  11. #include "esp_secure_boot.h"
  12. #include "hal/efuse_hal.h"
  13. #ifndef BOOTLOADER_BUILD
  14. static __attribute__((unused)) const char *TAG = "secure_boot";
  15. #ifdef CONFIG_SECURE_BOOT
  16. static void efuse_batch_write_begin(bool *need_fix)
  17. {
  18. if (*need_fix == false) {
  19. esp_efuse_batch_write_begin();
  20. }
  21. *need_fix = true;
  22. }
  23. static void update_efuses(bool need_fix, esp_err_t err)
  24. {
  25. if (need_fix) {
  26. if (err != ESP_OK) {
  27. ESP_LOGE(TAG, "Can not be fixed (err=0x%x).", err);
  28. esp_efuse_batch_write_cancel();
  29. } else {
  30. err = esp_efuse_batch_write_commit();
  31. if (err != ESP_OK) {
  32. ESP_LOGE(TAG, "Error programming eFuses (err=0x%x)", err);
  33. return;
  34. } else {
  35. ESP_LOGI(TAG, "Fixed");
  36. }
  37. }
  38. }
  39. }
  40. #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
  41. static esp_err_t secure_boot_v1_check(bool *need_fix)
  42. {
  43. esp_err_t err = ESP_OK;
  44. esp_efuse_block_t block = EFUSE_BLK_SECURE_BOOT;
  45. if (!esp_efuse_get_key_dis_read(block)) {
  46. efuse_batch_write_begin(need_fix);
  47. ESP_LOGW(TAG, "eFuse BLOCK%d should not be readable. Fixing..", block);
  48. err = esp_efuse_set_key_dis_read(block);
  49. }
  50. if (!esp_efuse_get_key_dis_write(block)) {
  51. efuse_batch_write_begin(need_fix);
  52. ESP_LOGW(TAG, "eFuse BLOCK%d should not be writeable. Fixing..", block);
  53. if (err == ESP_OK) {
  54. err = esp_efuse_set_key_dis_write(block);
  55. }
  56. }
  57. return err;
  58. }
  59. #elif SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS == 1 && CONFIG_SECURE_BOOT_V2_ENABLED
  60. static esp_err_t secure_boot_v2_check(bool *need_fix)
  61. {
  62. esp_err_t err = ESP_OK;
  63. esp_efuse_block_t block = EFUSE_BLK_SECURE_BOOT;
  64. #ifndef CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
  65. if (esp_efuse_get_key_dis_read(block)) {
  66. ESP_LOGE(TAG, "eFuse BLOCK%d should be readable", block);
  67. abort();
  68. // This code is not achievable because the bootloader will not boot an app in this state.
  69. // But we keep it here just in case (any unexpected behavior).
  70. }
  71. #endif
  72. if (esp_efuse_block_is_empty(block)) {
  73. ESP_LOGE(TAG, "eFuse BLOCK%d should not be empty", block);
  74. abort();
  75. // This code is not achievable because the bootloader will not boot an app in this state.
  76. // But we keep it here just in case (any unexpected behavior).
  77. }
  78. if (!esp_efuse_get_key_dis_write(block)) {
  79. efuse_batch_write_begin(need_fix);
  80. ESP_LOGW(TAG, "eFuse BLOCK%d should not be writeable. Fixing..", block);
  81. err = esp_efuse_set_key_dis_write(block);
  82. }
  83. return err;
  84. }
  85. #elif SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS > 1 && CONFIG_SECURE_BOOT_V2_ENABLED
  86. static esp_err_t secure_boot_v2_check(bool *need_fix)
  87. {
  88. esp_err_t err = ESP_OK;
  89. esp_efuse_purpose_t purpose[SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS] = {
  90. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST0,
  91. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST1,
  92. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST2,
  93. };
  94. for (unsigned i = 0; i < SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS; ++i) {
  95. esp_efuse_block_t block;
  96. if (esp_efuse_find_purpose(purpose[i], &block)) {
  97. if (!esp_efuse_get_digest_revoke(i)) {
  98. if (esp_efuse_get_key_dis_read(block)) {
  99. ESP_LOGE(TAG, "eFuse BLOCK%d should be readable", block);
  100. abort();
  101. // This state is not expected unless the eFuses have been manually misconfigured.
  102. }
  103. if (esp_efuse_block_is_empty(block)) {
  104. ESP_LOGE(TAG, "eFuse BLOCK%d should not be empty", block);
  105. abort();
  106. // This state is not expected unless the eFuses have been manually misconfigured.
  107. }
  108. if (!esp_efuse_get_key_dis_write(block)) {
  109. efuse_batch_write_begin(need_fix);
  110. ESP_LOGW(TAG, "eFuse BLOCK%d should not be writeable. Fixing..", block);
  111. if (err == ESP_OK) {
  112. err = esp_efuse_set_key_dis_write(block);
  113. }
  114. }
  115. }
  116. if (!esp_efuse_get_keypurpose_dis_write(block)) {
  117. efuse_batch_write_begin(need_fix);
  118. ESP_LOGW(TAG, "The KEY_PURPOSE_SECURE_BOOT_DIGEST%d should be write-protected. Fixing..", block);
  119. if (err == ESP_OK) {
  120. err = esp_efuse_set_keypurpose_dis_write(block);
  121. }
  122. }
  123. } else {
  124. if (!esp_efuse_get_digest_revoke(i)) {
  125. #ifndef CONFIG_SECURE_BOOT_ALLOW_UNUSED_DIGEST_SLOTS
  126. efuse_batch_write_begin(need_fix);
  127. ESP_LOGW(TAG, "Unused SECURE_BOOT_DIGEST%d should be revoked. Fixing..", i);
  128. if (err == ESP_OK) {
  129. err = esp_efuse_set_digest_revoke(i);
  130. }
  131. #else
  132. ESP_LOGW(TAG, "Unused SECURE_BOOT_DIGEST%d should be revoked. It will not be fixed due to the config", i);
  133. #endif
  134. }
  135. }
  136. }
  137. return err;
  138. }
  139. #endif
  140. #endif // CONFIG_SECURE_BOOT
  141. #if (CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME || CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME) && CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
  142. static void check_signature_on_update_check(void)
  143. {
  144. // We rely on the keys used to sign this app to verify the next app on OTA, so make sure there is at
  145. // least one to avoid a stuck firmware
  146. esp_image_sig_public_key_digests_t digests = { 0 };
  147. esp_err_t err = esp_secure_boot_get_signature_blocks_for_running_app(false, &digests);
  148. if (err != ESP_OK || digests.num_digests == 0) {
  149. ESP_LOGE(TAG, "This app is not signed, but check signature on update is enabled in config. It won't be possible to verify any update.");
  150. abort();
  151. }
  152. #if CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT && SECURE_BOOT_NUM_BLOCKS > 1
  153. if (digests.num_digests > 1) {
  154. ESP_LOGW(TAG, "App has %d signatures. Only the first position of signature blocks is used to verify any update", digests.num_digests);
  155. }
  156. #endif
  157. }
  158. #endif // (CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME || CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME) && CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
  159. void esp_secure_boot_init_checks(void)
  160. {
  161. #ifdef CONFIG_SECURE_BOOT
  162. if (esp_secure_boot_enabled()) {
  163. bool need_fix = false;
  164. #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
  165. esp_err_t err = secure_boot_v1_check(&need_fix);
  166. #else
  167. esp_err_t err = secure_boot_v2_check(&need_fix);
  168. #endif
  169. update_efuses(need_fix, err);
  170. } else {
  171. ESP_LOGE(TAG, "Mismatch in secure boot settings: the app config is enabled but eFuse not");
  172. }
  173. #endif // CONFIG_SECURE_BOOT
  174. #if (CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME || CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME) && CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
  175. check_signature_on_update_check();
  176. #endif // (CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME || CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME) && CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
  177. }
  178. #ifdef CONFIG_IDF_TARGET_ESP32
  179. bool esp_secure_boot_cfg_verify_release_mode(void)
  180. {
  181. bool result = false;
  182. bool secure;
  183. bool secure_boot_v1 = esp_efuse_read_field_bit(ESP_EFUSE_ABS_DONE_0);
  184. bool chip_supports_sbv2 = efuse_hal_chip_revision() >= 300;
  185. bool secure_boot_v2 = (chip_supports_sbv2) ? esp_efuse_read_field_bit(ESP_EFUSE_ABS_DONE_1) : false;
  186. result = secure_boot_v1 || secure_boot_v2;
  187. if (secure_boot_v1 && secure_boot_v2) {
  188. ESP_LOGI(TAG, "ABS_DONE_0=1 (V1) and ABS_DONE_1=1 (V2)");
  189. ESP_LOGI(TAG, "Secure boot V2 shall take the precedence");
  190. } else if (!secure_boot_v1 && !secure_boot_v2) {
  191. result = false;
  192. ESP_LOGE(TAG, "Not enabled Secure Boot V1 (set ABS_DONE_0->1)");
  193. if (chip_supports_sbv2) {
  194. ESP_LOGE(TAG, "Not enabled Secure Boot V2 (set ABS_DONE_1->1)");
  195. }
  196. }
  197. if (secure_boot_v1 && !secure_boot_v2) {
  198. secure = esp_efuse_read_field_bit(ESP_EFUSE_RD_DIS_BLK2);
  199. result &= secure;
  200. if (!secure) {
  201. ESP_LOGW(TAG, "Not read-protected secure boot key (set RD_DIS_BLK2->1)");
  202. }
  203. }
  204. secure = esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_BLK2);
  205. result &= secure;
  206. if (!secure) {
  207. ESP_LOGW(TAG, "Not write-protected secure boot key (set WR_DIS_BLK2->1)");
  208. }
  209. secure = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_JTAG);
  210. result &= secure;
  211. if (!secure) {
  212. ESP_LOGW(TAG, "Not disabled JTAG (set DISABLE_JTAG->1)");
  213. }
  214. secure = esp_efuse_read_field_bit(ESP_EFUSE_CONSOLE_DEBUG_DISABLE);
  215. result &= secure;
  216. if (!secure) {
  217. ESP_LOGW(TAG, "Not disabled ROM BASIC interpreter fallback (set CONSOLE_DEBUG_DISABLE->1)");
  218. }
  219. if (secure_boot_v2) {
  220. secure = esp_efuse_read_field_bit(ESP_EFUSE_UART_DOWNLOAD_DIS);
  221. result &= secure;
  222. if (!secure) {
  223. ESP_LOGW(TAG, "Not disabled UART ROM Download mode (set UART_DOWNLOAD_DIS->1)");
  224. }
  225. secure = esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_EFUSE_RD_DISABLE);
  226. result &= secure;
  227. if (!secure) {
  228. ESP_LOGW(TAG, "Not disabled write-protection for read-protection (set WR_DIS_EFUSE_RD_DISABLE->1)");
  229. }
  230. }
  231. return result;
  232. }
  233. #else // not CONFIG_IDF_TARGET_ESP32
  234. bool esp_secure_boot_cfg_verify_release_mode(void)
  235. {
  236. bool result = false;
  237. bool secure;
  238. secure = esp_secure_boot_enabled();
  239. result = secure;
  240. if (!secure) {
  241. ESP_LOGW(TAG, "Not enabled Secure Boot (SECURE_BOOT_EN->1)");
  242. }
  243. secure = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MODE);
  244. bool en_secure_download = esp_efuse_read_field_bit(ESP_EFUSE_ENABLE_SECURITY_DOWNLOAD);
  245. if (!secure && !en_secure_download) {
  246. result &= false;
  247. ESP_LOGW(TAG, "Download mode has not been changed, disable it or set security mode:");
  248. ESP_LOGW(TAG, "Not disabled ROM Download mode (DIS_DOWNLOAD_MODE->1)");
  249. ESP_LOGW(TAG, "Not enabled Security download mode (ENABLE_SECURITY_DOWNLOAD->1)");
  250. }
  251. #if SOC_EFUSE_DIS_BOOT_REMAP
  252. secure = esp_efuse_read_field_bit(ESP_EFUSE_DIS_BOOT_REMAP);
  253. result &= secure;
  254. if (!secure) {
  255. ESP_LOGW(TAG, "Not disabled boot from RAM (set DIS_BOOT_REMAP->1)");
  256. }
  257. #endif
  258. #if SOC_EFUSE_DIS_LEGACY_SPI_BOOT
  259. secure = esp_efuse_read_field_bit(ESP_EFUSE_DIS_LEGACY_SPI_BOOT);
  260. result &= secure;
  261. if (!secure) {
  262. ESP_LOGW(TAG, "Not disabled Legcy SPI boot (set DIS_LEGACY_SPI_BOOT->1)");
  263. }
  264. #endif
  265. #if SOC_EFUSE_DIS_DIRECT_BOOT
  266. secure = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DIRECT_BOOT);
  267. result &= secure;
  268. if (!secure) {
  269. ESP_LOGW(TAG, "Not disabled direct boot mode (set DIS_DIRECT_BOOT->1)");
  270. }
  271. #endif
  272. #if SOC_EFUSE_HARD_DIS_JTAG
  273. secure = esp_efuse_read_field_bit(ESP_EFUSE_HARD_DIS_JTAG);
  274. result &= secure;
  275. if (!secure) {
  276. ESP_LOGW(TAG, "Not disabled JTAG (set HARD_DIS_JTAG->1)");
  277. }
  278. #endif
  279. #if SOC_EFUSE_SOFT_DIS_JTAG
  280. size_t soft_dis_jtag_cnt_val = 0;
  281. esp_efuse_read_field_cnt(ESP_EFUSE_SOFT_DIS_JTAG, &soft_dis_jtag_cnt_val);
  282. if (soft_dis_jtag_cnt_val != ESP_EFUSE_SOFT_DIS_JTAG[0]->bit_count) {
  283. result &= secure;
  284. ESP_LOGW(TAG, "Not disabled JTAG in the soft way (set SOFT_DIS_JTAG->max)");
  285. }
  286. #endif
  287. #if SOC_EFUSE_DIS_PAD_JTAG
  288. secure = esp_efuse_read_field_bit(ESP_EFUSE_DIS_PAD_JTAG);
  289. result &= secure;
  290. if (!secure) {
  291. ESP_LOGW(TAG, "Not disabled JTAG PADs (set DIS_PAD_JTAG->1)");
  292. }
  293. #endif
  294. #if SOC_EFUSE_DIS_USB_JTAG
  295. secure = esp_efuse_read_field_bit(ESP_EFUSE_DIS_USB_JTAG);
  296. result &= secure;
  297. if (!secure) {
  298. ESP_LOGW(TAG, "Not disabled USB JTAG (set DIS_USB_JTAG->1)");
  299. }
  300. #endif
  301. #ifdef CONFIG_SECURE_BOOT_ENABLE_AGGRESSIVE_KEY_REVOKE
  302. secure = esp_efuse_read_field_bit(ESP_EFUSE_SECURE_BOOT_AGGRESSIVE_REVOKE);
  303. result &= secure;
  304. if (!secure) {
  305. ESP_LOGW(TAG, "Not enabled AGGRESSIVE KEY REVOKE (set SECURE_BOOT_AGGRESSIVE_REVOKE->1)");
  306. }
  307. #endif
  308. secure = esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_RD_DIS);
  309. result &= secure;
  310. if (!secure) {
  311. ESP_LOGW(TAG, "Not disabled write-protection for read-protection (set WR_DIS_RD_DIS->1)");
  312. }
  313. #if SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS == 1
  314. unsigned purpose = ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_V2;
  315. #else
  316. unsigned purpose = ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST0; // DIGEST0, DIGEST1 and DIGEST2
  317. #endif
  318. secure = false;
  319. unsigned num_keys = 0;
  320. for (unsigned i = 0; i < SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS; ++i) {
  321. esp_efuse_block_t block;
  322. if (esp_efuse_find_purpose(purpose + i, &block)) {
  323. // if chip has a few secure boot slots then we check all
  324. #if SOC_SUPPORT_SECURE_BOOT_REVOKE_KEY
  325. bool revoke = esp_efuse_get_digest_revoke(i);
  326. if (revoke) {
  327. continue;
  328. }
  329. #endif
  330. ++num_keys;
  331. #if SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
  332. secure = !esp_efuse_read_field_bit(ESP_EFUSE_RD_DIS_KEY0_HI);
  333. #else
  334. secure = !esp_efuse_get_key_dis_read(block);
  335. #endif // !SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
  336. result &= secure;
  337. if (!secure) {
  338. ESP_LOGE(TAG, "Secure boot key in BLOCK%d must NOT be read-protected (can not be used)", block);
  339. #if SOC_SUPPORT_SECURE_BOOT_REVOKE_KEY
  340. ESP_LOGE(TAG, "Revoke this secure boot key (set SECURE_BOOT_KEY_REVOKE%d->1)", i);
  341. #endif
  342. }
  343. secure = !esp_efuse_block_is_empty(block);
  344. result &= secure;
  345. if (!secure) {
  346. ESP_LOGE(TAG, "Secure boot key in BLOCK%d must NOT be empty (can not be used)", block);
  347. #if SOC_SUPPORT_SECURE_BOOT_REVOKE_KEY
  348. ESP_LOGE(TAG, "Revoke this secure boot key (set SECURE_BOOT_KEY_REVOKE%d->1)", i);
  349. #endif
  350. }
  351. secure = esp_efuse_get_key_dis_write(block);
  352. result &= secure;
  353. if (!secure) {
  354. ESP_LOGW(TAG, "Not write-protected secure boot key in BLOCK%d (set WR_DIS_KEY%d->1)", block, block - EFUSE_BLK_KEY0);
  355. }
  356. #if SOC_EFUSE_KEY_PURPOSE_FIELD
  357. secure = esp_efuse_get_keypurpose_dis_write(block);
  358. result &= secure;
  359. if (!secure) {
  360. ESP_LOGW(TAG, "Not write-protected KEY_PURPOSE for BLOCK%d (set WR_DIS_KEY_PURPOSE%d->1)", block, block - EFUSE_BLK_KEY0);
  361. }
  362. #endif
  363. }
  364. }
  365. result &= secure;
  366. secure = (num_keys != 0);
  367. result &= secure;
  368. if (!secure) {
  369. ESP_LOGE(TAG, "No secure boot key found");
  370. }
  371. return result;
  372. }
  373. #endif // not CONFIG_IDF_TARGET_ESP32
  374. #endif // not BOOTLOADER_BUILD