secure_boot.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifndef BOOTLOADER_BUILD
  13. static __attribute__((unused)) const char *TAG = "secure_boot";
  14. #ifdef CONFIG_SECURE_BOOT
  15. static void efuse_batch_write_begin(bool *need_fix)
  16. {
  17. if (*need_fix == false) {
  18. esp_efuse_batch_write_begin();
  19. }
  20. *need_fix = true;
  21. }
  22. static void update_efuses(bool need_fix, esp_err_t err)
  23. {
  24. if (need_fix) {
  25. if (err != ESP_OK) {
  26. ESP_LOGE(TAG, "Can not be fixed (err=0x%x).", err);
  27. esp_efuse_batch_write_cancel();
  28. } else {
  29. err = esp_efuse_batch_write_commit();
  30. if (err != ESP_OK) {
  31. ESP_LOGE(TAG, "Error programming eFuses (err=0x%x)", err);
  32. return;
  33. } else {
  34. ESP_LOGI(TAG, "Fixed");
  35. }
  36. }
  37. }
  38. }
  39. #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
  40. static esp_err_t secure_boot_v1_check(bool *need_fix)
  41. {
  42. esp_err_t err = ESP_OK;
  43. esp_efuse_block_t block = EFUSE_BLK_SECURE_BOOT;
  44. if (!esp_efuse_get_key_dis_read(block)) {
  45. efuse_batch_write_begin(need_fix);
  46. ESP_LOGW(TAG, "eFuse BLOCK%d should not be readable. Fixing..", block);
  47. err = esp_efuse_set_key_dis_read(block);
  48. }
  49. if (!esp_efuse_get_key_dis_write(block)) {
  50. efuse_batch_write_begin(need_fix);
  51. ESP_LOGW(TAG, "eFuse BLOCK%d should not be writeable. Fixing..", block);
  52. if (err == ESP_OK) {
  53. err = esp_efuse_set_key_dis_write(block);
  54. }
  55. }
  56. return err;
  57. }
  58. #elif SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS == 1 && CONFIG_SECURE_BOOT_V2_ENABLED
  59. static esp_err_t secure_boot_v2_check(bool *need_fix)
  60. {
  61. esp_err_t err = ESP_OK;
  62. esp_efuse_block_t block = EFUSE_BLK_SECURE_BOOT;
  63. #ifndef CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
  64. if (esp_efuse_get_key_dis_read(block)) {
  65. ESP_LOGE(TAG, "eFuse BLOCK%d should be readable", block);
  66. abort();
  67. // This code is not achievable because the bootloader will not boot an app in this state.
  68. // But we keep it here just in case (any unexpected behavior).
  69. }
  70. #endif
  71. if (esp_efuse_block_is_empty(block)) {
  72. ESP_LOGE(TAG, "eFuse BLOCK%d should not be empty", block);
  73. abort();
  74. // This code is not achievable because the bootloader will not boot an app in this state.
  75. // But we keep it here just in case (any unexpected behavior).
  76. }
  77. if (!esp_efuse_get_key_dis_write(block)) {
  78. efuse_batch_write_begin(need_fix);
  79. ESP_LOGW(TAG, "eFuse BLOCK%d should not be writeable. Fixing..", block);
  80. err = esp_efuse_set_key_dis_write(block);
  81. }
  82. return err;
  83. }
  84. #elif SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS > 1 && CONFIG_SECURE_BOOT_V2_ENABLED
  85. static esp_err_t secure_boot_v2_check(bool *need_fix)
  86. {
  87. esp_err_t err = ESP_OK;
  88. esp_efuse_purpose_t purpose[SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS] = {
  89. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST0,
  90. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST1,
  91. ESP_EFUSE_KEY_PURPOSE_SECURE_BOOT_DIGEST2,
  92. };
  93. for (unsigned i = 0; i < SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS; ++i) {
  94. esp_efuse_block_t block;
  95. if (esp_efuse_find_purpose(purpose[i], &block)) {
  96. if (!esp_efuse_get_digest_revoke(i)) {
  97. if (esp_efuse_get_key_dis_read(block)) {
  98. ESP_LOGE(TAG, "eFuse BLOCK%d should be readable", block);
  99. abort();
  100. // This state is not expected unless the eFuses have been manually misconfigured.
  101. }
  102. if (esp_efuse_block_is_empty(block)) {
  103. ESP_LOGE(TAG, "eFuse BLOCK%d should not be empty", block);
  104. abort();
  105. // This state is not expected unless the eFuses have been manually misconfigured.
  106. }
  107. if (!esp_efuse_get_key_dis_write(block)) {
  108. efuse_batch_write_begin(need_fix);
  109. ESP_LOGW(TAG, "eFuse BLOCK%d should not be writeable. Fixing..", block);
  110. if (err == ESP_OK) {
  111. err = esp_efuse_set_key_dis_write(block);
  112. }
  113. }
  114. }
  115. if (!esp_efuse_get_keypurpose_dis_write(block)) {
  116. efuse_batch_write_begin(need_fix);
  117. ESP_LOGW(TAG, "The KEY_PURPOSE_SECURE_BOOT_DIGEST%d should be write-protected. Fixing..", block);
  118. if (err == ESP_OK) {
  119. err = esp_efuse_set_keypurpose_dis_write(block);
  120. }
  121. }
  122. } else {
  123. if (!esp_efuse_get_digest_revoke(i)) {
  124. #ifndef CONFIG_SECURE_BOOT_ALLOW_UNUSED_DIGEST_SLOTS
  125. efuse_batch_write_begin(need_fix);
  126. ESP_LOGW(TAG, "Unused SECURE_BOOT_DIGEST%d should be revoked. Fixing..", i);
  127. if (err == ESP_OK) {
  128. err = esp_efuse_set_digest_revoke(i);
  129. }
  130. #else
  131. ESP_LOGW(TAG, "Unused SECURE_BOOT_DIGEST%d should be revoked. It will not be fixed due to the config", i);
  132. #endif
  133. }
  134. }
  135. }
  136. return err;
  137. }
  138. #endif
  139. #endif // CONFIG_SECURE_BOOT
  140. #if CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME && CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
  141. static void rsa_check_signature_on_update_check(void)
  142. {
  143. // We rely on the keys used to sign this app to verify the next app on OTA, so make sure there is at
  144. // least one to avoid a stuck firmware
  145. esp_image_sig_public_key_digests_t digests = { 0 };
  146. esp_err_t err = esp_secure_boot_get_signature_blocks_for_running_app(false, &digests);
  147. if (err != ESP_OK || digests.num_digests == 0) {
  148. 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.");
  149. abort();
  150. }
  151. #if CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT && SECURE_BOOT_NUM_BLOCKS > 1
  152. if (digests.num_digests > 1) {
  153. ESP_LOGW(TAG, "App has %d signatures. Only the first position of signature blocks is used to verify any update", digests.num_digests);
  154. }
  155. #endif
  156. }
  157. #endif // CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME && CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
  158. void esp_secure_boot_init_checks(void)
  159. {
  160. #ifdef CONFIG_SECURE_BOOT
  161. if (esp_secure_boot_enabled()) {
  162. bool need_fix = false;
  163. #ifdef CONFIG_SECURE_BOOT_V1_ENABLED
  164. esp_err_t err = secure_boot_v1_check(&need_fix);
  165. #else
  166. esp_err_t err = secure_boot_v2_check(&need_fix);
  167. #endif
  168. update_efuses(need_fix, err);
  169. } else {
  170. ESP_LOGE(TAG, "Mismatch in secure boot settings: the app config is enabled but eFuse not");
  171. }
  172. #endif // CONFIG_SECURE_BOOT
  173. #if CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME && CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
  174. rsa_check_signature_on_update_check();
  175. #endif // CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME && CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT
  176. }
  177. #endif // not BOOTLOADER_BUILD