flash_encrypt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2015-2019 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 <strings.h>
  14. #include "sdkconfig.h"
  15. #include "esp_log.h"
  16. #include "esp_efuse.h"
  17. #include "esp_efuse_table.h"
  18. #include "esp_flash_encrypt.h"
  19. #include "esp_secure_boot.h"
  20. #if CONFIG_IDF_TARGET_ESP32
  21. #define CRYPT_CNT ESP_EFUSE_FLASH_CRYPT_CNT
  22. #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT
  23. #else
  24. #define CRYPT_CNT ESP_EFUSE_SPI_BOOT_CRYPT_CNT
  25. #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_SPI_BOOT_CRYPT_CNT
  26. #endif
  27. #ifndef BOOTLOADER_BUILD
  28. static const char *TAG = "flash_encrypt";
  29. void esp_flash_encryption_init_checks()
  30. {
  31. esp_flash_enc_mode_t mode;
  32. #ifdef CONFIG_SECURE_FLASH_CHECK_ENC_EN_IN_APP
  33. if (!esp_flash_encryption_enabled()) {
  34. ESP_LOGE(TAG, "Flash encryption eFuse bit was not enabled in bootloader but CONFIG_SECURE_FLASH_ENC_ENABLED is on");
  35. abort();
  36. }
  37. #endif
  38. // First check is: if Release mode flash encryption & secure boot are enabled then
  39. // FLASH_CRYPT_CNT *must* be write protected. This will have happened automatically
  40. // if bootloader is IDF V4.0 or newer but may not have happened for previous ESP-IDF bootloaders.
  41. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  42. #ifdef CONFIG_SECURE_BOOT
  43. if (esp_secure_boot_enabled() && esp_flash_encryption_enabled()) {
  44. bool flash_crypt_cnt_wr_dis = esp_efuse_read_field_bit(WR_DIS_CRYPT_CNT);
  45. if (!flash_crypt_cnt_wr_dis) {
  46. uint8_t flash_crypt_cnt = 0;
  47. esp_efuse_read_field_blob(CRYPT_CNT, &flash_crypt_cnt, CRYPT_CNT[0]->bit_count);
  48. if (flash_crypt_cnt == (1<<(CRYPT_CNT[0]->bit_count))-1) {
  49. // If encryption counter is already max, no need to write protect it
  50. // (this distinction is important on ESP32 ECO3 where write-procted FLASH_CRYPT_CNT also write-protects UART_DL_DIS)
  51. return;
  52. }
  53. ESP_LOGE(TAG, "Flash encryption & Secure Boot together requires FLASH_CRYPT_CNT efuse to be write protected. Fixing now...");
  54. esp_flash_write_protect_crypt_cnt();
  55. }
  56. }
  57. #endif // CONFIG_SECURE_BOOT
  58. #endif // CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  59. // Second check is to print a warning or error if the current running flash encryption mode
  60. // doesn't match the expectation from project config (due to mismatched bootloader and app, probably)
  61. mode = esp_get_flash_encryption_mode();
  62. if (mode == ESP_FLASH_ENC_MODE_DEVELOPMENT) {
  63. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  64. ESP_LOGE(TAG, "Flash encryption settings error: app is configured for RELEASE but efuses are set for DEVELOPMENT");
  65. ESP_LOGE(TAG, "Mismatch found in security options in bootloader menuconfig and efuse settings. Device is not secure.");
  66. #else
  67. ESP_LOGW(TAG, "Flash encryption mode is DEVELOPMENT (not secure)");
  68. #endif
  69. } else if (mode == ESP_FLASH_ENC_MODE_RELEASE) {
  70. ESP_LOGI(TAG, "Flash encryption mode is RELEASE");
  71. }
  72. }
  73. #endif
  74. void esp_flash_write_protect_crypt_cnt(void)
  75. {
  76. esp_efuse_write_field_bit(WR_DIS_CRYPT_CNT);
  77. }
  78. esp_flash_enc_mode_t esp_get_flash_encryption_mode(void)
  79. {
  80. bool flash_crypt_cnt_wr_dis = false;
  81. #if CONFIG_IDF_TARGET_ESP32
  82. uint8_t dis_dl_enc = 0, dis_dl_dec = 0, dis_dl_cache = 0;
  83. #elif CONFIG_IDF_TARGET_ESP32S2
  84. uint8_t dis_dl_enc = 0;
  85. uint8_t dis_dl_icache = 0;
  86. uint8_t dis_dl_dcache = 0;
  87. #elif CONFIG_IDF_TARGET_ESP32C3
  88. uint8_t dis_dl_enc = 0;
  89. uint8_t dis_dl_icache = 0;
  90. #endif
  91. esp_flash_enc_mode_t mode = ESP_FLASH_ENC_MODE_DEVELOPMENT;
  92. if (esp_flash_encryption_enabled()) {
  93. /* Check if FLASH CRYPT CNT is write protected */
  94. flash_crypt_cnt_wr_dis = esp_efuse_read_field_bit(WR_DIS_CRYPT_CNT);
  95. if (!flash_crypt_cnt_wr_dis) {
  96. uint8_t flash_crypt_cnt = 0;
  97. esp_efuse_read_field_blob(CRYPT_CNT, &flash_crypt_cnt, CRYPT_CNT[0]->bit_count);
  98. if (flash_crypt_cnt == (1 << (CRYPT_CNT[0]->bit_count)) - 1) {
  99. flash_crypt_cnt_wr_dis = true;
  100. }
  101. }
  102. if (flash_crypt_cnt_wr_dis) {
  103. #if CONFIG_IDF_TARGET_ESP32
  104. dis_dl_cache = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_CACHE);
  105. dis_dl_enc = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_ENCRYPT);
  106. dis_dl_dec = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_DECRYPT);
  107. /* Check if DISABLE_DL_DECRYPT, DISABLE_DL_ENCRYPT & DISABLE_DL_CACHE are set */
  108. if ( dis_dl_cache && dis_dl_enc && dis_dl_dec ) {
  109. mode = ESP_FLASH_ENC_MODE_RELEASE;
  110. }
  111. #elif CONFIG_IDF_TARGET_ESP32S2
  112. dis_dl_enc = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT);
  113. dis_dl_icache = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_ICACHE);
  114. dis_dl_dcache = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_DCACHE);
  115. if (dis_dl_enc && dis_dl_icache && dis_dl_dcache) {
  116. mode = ESP_FLASH_ENC_MODE_RELEASE;
  117. }
  118. #elif CONFIG_IDF_TARGET_ESP32C3
  119. dis_dl_enc = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT);
  120. dis_dl_icache = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_ICACHE);
  121. if (dis_dl_enc && dis_dl_icache) {
  122. mode = ESP_FLASH_ENC_MODE_RELEASE;
  123. }
  124. #endif
  125. }
  126. } else {
  127. mode = ESP_FLASH_ENC_MODE_DISABLED;
  128. }
  129. return mode;
  130. }