secure_boot.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2015-2016 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_attr.h"
  15. #include "esp_types.h"
  16. #include "esp_log.h"
  17. #include "rom/cache.h"
  18. #include "rom/ets_sys.h"
  19. #include "rom/secure_boot.h"
  20. #include "soc/dport_reg.h"
  21. #include "soc/io_mux_reg.h"
  22. #include "soc/efuse_reg.h"
  23. #include "soc/rtc_cntl_reg.h"
  24. #include "sdkconfig.h"
  25. #include "bootloader_flash.h"
  26. #include "bootloader_random.h"
  27. #include "esp_image_format.h"
  28. #include "esp_secure_boot.h"
  29. #include "esp_flash_encrypt.h"
  30. #include "esp_efuse.h"
  31. static const char* TAG = "secure_boot";
  32. /**
  33. * @function : secure_boot_generate
  34. * @description: generate boot digest (aka "abstract") & iv
  35. *
  36. * @inputs: image_len - length of image to calculate digest for
  37. */
  38. static bool secure_boot_generate(uint32_t image_len){
  39. esp_err_t err;
  40. esp_secure_boot_iv_digest_t digest;
  41. const uint32_t *image;
  42. /* hardware secure boot engine only takes full blocks, so round up the
  43. image length. The additional data should all be 0xFF.
  44. */
  45. if (image_len % sizeof(digest.iv) != 0) {
  46. image_len = (image_len / sizeof(digest.iv) + 1) * sizeof(digest.iv);
  47. }
  48. ets_secure_boot_start();
  49. ets_secure_boot_rd_iv((uint32_t *)digest.iv);
  50. ets_secure_boot_hash(NULL);
  51. /* iv stored in sec 0 */
  52. err = bootloader_flash_erase_sector(0);
  53. if (err != ESP_OK)
  54. {
  55. ESP_LOGE(TAG, "SPI erase failed: 0x%x", err);
  56. return false;
  57. }
  58. /* generate digest from image contents */
  59. image = bootloader_mmap(0x1000, image_len);
  60. if (!image) {
  61. ESP_LOGE(TAG, "bootloader_mmap(0x1000, 0x%x) failed", image_len);
  62. return false;
  63. }
  64. for (int i = 0; i < image_len; i+= sizeof(digest.iv)) {
  65. ets_secure_boot_hash(&image[i/sizeof(uint32_t)]);
  66. }
  67. bootloader_munmap(image);
  68. ets_secure_boot_obtain();
  69. ets_secure_boot_rd_abstract((uint32_t *)digest.digest);
  70. ets_secure_boot_finish();
  71. ESP_LOGD(TAG, "write iv+digest to flash");
  72. err = bootloader_flash_write(FLASH_OFFS_SECURE_BOOT_IV_DIGEST, &digest,
  73. sizeof(digest), esp_flash_encryption_enabled());
  74. if (err != ESP_OK) {
  75. ESP_LOGE(TAG, "SPI write failed: 0x%x", err);
  76. return false;
  77. }
  78. Cache_Read_Enable(0);
  79. return true;
  80. }
  81. /* Burn values written to the efuse write registers */
  82. static inline void burn_efuses()
  83. {
  84. #ifdef CONFIG_SECURE_BOOT_TEST_MODE
  85. ESP_LOGE(TAG, "SECURE BOOT TEST MODE. Not really burning any efuses! NOT SECURE");
  86. #else
  87. esp_efuse_burn_new_values();
  88. #endif
  89. }
  90. esp_err_t esp_secure_boot_permanently_enable(void) {
  91. esp_err_t err;
  92. uint32_t image_len = 0;
  93. if (esp_secure_boot_enabled())
  94. {
  95. ESP_LOGI(TAG, "bootloader secure boot is already enabled, continuing..");
  96. return ESP_OK;
  97. }
  98. err = esp_image_basic_verify(0x1000, true, &image_len);
  99. if (err != ESP_OK) {
  100. ESP_LOGE(TAG, "bootloader image appears invalid! error %d", err);
  101. return err;
  102. }
  103. uint32_t dis_reg = REG_READ(EFUSE_BLK0_RDATA0_REG);
  104. bool efuse_key_read_protected = dis_reg & EFUSE_RD_DIS_BLK2;
  105. bool efuse_key_write_protected = dis_reg & EFUSE_WR_DIS_BLK2;
  106. if (efuse_key_read_protected == false
  107. && efuse_key_write_protected == false
  108. && REG_READ(EFUSE_BLK2_RDATA0_REG) == 0
  109. && REG_READ(EFUSE_BLK2_RDATA1_REG) == 0
  110. && REG_READ(EFUSE_BLK2_RDATA2_REG) == 0
  111. && REG_READ(EFUSE_BLK2_RDATA3_REG) == 0
  112. && REG_READ(EFUSE_BLK2_RDATA4_REG) == 0
  113. && REG_READ(EFUSE_BLK2_RDATA5_REG) == 0
  114. && REG_READ(EFUSE_BLK2_RDATA6_REG) == 0
  115. && REG_READ(EFUSE_BLK2_RDATA7_REG) == 0) {
  116. /* On-device key generation is temporarily disabled, until
  117. * RNG operation during bootloader is qualified.
  118. * See docs/security/secure-boot.rst for details. */
  119. ESP_LOGE(TAG, "On-device key generation is not yet available.");
  120. return ESP_ERR_NOT_SUPPORTED;
  121. } else {
  122. ESP_LOGW(TAG, "Using pre-loaded secure boot key in EFUSE block 2");
  123. }
  124. ESP_LOGI(TAG, "Generating secure boot digest...");
  125. if (false == secure_boot_generate(image_len)){
  126. ESP_LOGE(TAG, "secure boot generation failed");
  127. return ESP_FAIL;
  128. }
  129. ESP_LOGI(TAG, "Digest generation complete.");
  130. #ifndef CONFIG_SECURE_BOOT_TEST_MODE
  131. if (!efuse_key_read_protected) {
  132. ESP_LOGE(TAG, "Pre-loaded key is not read protected. Refusing to blow secure boot efuse.");
  133. return ESP_ERR_INVALID_STATE;
  134. }
  135. if (!efuse_key_write_protected) {
  136. ESP_LOGE(TAG, "Pre-loaded key is not write protected. Refusing to blow secure boot efuse.");
  137. return ESP_ERR_INVALID_STATE;
  138. }
  139. #endif
  140. ESP_LOGI(TAG, "blowing secure boot efuse...");
  141. ESP_LOGD(TAG, "before updating, EFUSE_BLK0_RDATA6 %x", REG_READ(EFUSE_BLK0_RDATA6_REG));
  142. uint32_t new_wdata6 = EFUSE_RD_ABS_DONE_0;
  143. #ifndef CONFIG_SECURE_BOOT_ALLOW_JTAG
  144. ESP_LOGI(TAG, "Disable JTAG...");
  145. new_wdata6 |= EFUSE_RD_DISABLE_JTAG;
  146. #else
  147. ESP_LOGW(TAG, "Not disabling JTAG - SECURITY COMPROMISED");
  148. #endif
  149. #ifndef CONFIG_SECURE_BOOT_ALLOW_ROM_BASIC
  150. ESP_LOGI(TAG, "Disable ROM BASIC interpreter fallback...");
  151. new_wdata6 |= EFUSE_RD_CONSOLE_DEBUG_DISABLE;
  152. #else
  153. ESP_LOGW(TAG, "Not disabling ROM BASIC fallback - SECURITY COMPROMISED");
  154. #endif
  155. REG_WRITE(EFUSE_BLK0_WDATA6_REG, new_wdata6);
  156. burn_efuses();
  157. uint32_t after = REG_READ(EFUSE_BLK0_RDATA6_REG);
  158. ESP_LOGD(TAG, "after updating, EFUSE_BLK0_RDATA6 %x", after);
  159. if (after & EFUSE_RD_ABS_DONE_0) {
  160. ESP_LOGI(TAG, "secure boot is now enabled for bootloader image");
  161. return ESP_OK;
  162. } else {
  163. #ifdef CONFIG_SECURE_BOOT_TEST_MODE
  164. ESP_LOGE(TAG, "secure boot not enabled due to test mode");
  165. #else
  166. ESP_LOGE(TAG, "secure boot not enabled for bootloader image, EFUSE_RD_ABS_DONE_0 is probably write protected!");
  167. #endif
  168. return ESP_ERR_INVALID_STATE;
  169. }
  170. }