flash_encrypt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_WR_DIS_FLASH_CRYPT_CNT
  22. #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT
  23. #elif CONFIG_IDF_TARGET_ESP32S2
  24. #define CRYPT_CNT ESP_EFUSE_WR_DIS_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. // First check is: if Release mode flash encryption & secure boot are enabled then
  33. // FLASH_CRYPT_CNT *must* be write protected. This will have happened automatically
  34. // if bootloader is IDF V4.0 or newer but may not have happened for previous ESP-IDF bootloaders.
  35. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  36. #ifdef CONFIG_SECURE_BOOT
  37. if (esp_secure_boot_enabled() && esp_flash_encryption_enabled()) {
  38. uint8_t flash_crypt_cnt_wr_dis = 0;
  39. esp_efuse_read_field_blob(CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1);
  40. if (!flash_crypt_cnt_wr_dis) {
  41. ESP_EARLY_LOGE(TAG, "Flash encryption & Secure Boot together requires FLASH_CRYPT_CNT efuse to be write protected. Fixing now...");
  42. esp_flash_write_protect_crypt_cnt();
  43. }
  44. }
  45. #endif // CONFIG_SECURE_BOOT
  46. #endif // CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  47. // Second check is to print a warning or error if the current running flash encryption mode
  48. // doesn't match the expectation from project config (due to mismatched bootloader and app, probably)
  49. mode = esp_get_flash_encryption_mode();
  50. if (mode == ESP_FLASH_ENC_MODE_DEVELOPMENT) {
  51. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  52. ESP_EARLY_LOGE(TAG, "Flash encryption settings error: app is configured for RELEASE but efuses are set for DEVELOPMENT");
  53. ESP_EARLY_LOGE(TAG, "Mismatch found in security options in bootloader menuconfig and efuse settings. Device is not secure.");
  54. #else
  55. ESP_EARLY_LOGW(TAG, "Flash encryption mode is DEVELOPMENT (not secure)");
  56. #endif
  57. } else if (mode == ESP_FLASH_ENC_MODE_RELEASE) {
  58. ESP_EARLY_LOGI(TAG, "Flash encryption mode is RELEASE");
  59. }
  60. }
  61. #endif
  62. void esp_flash_write_protect_crypt_cnt(void)
  63. {
  64. uint8_t flash_crypt_cnt_wr_dis = 0;
  65. esp_efuse_read_field_blob(CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1);
  66. if (!flash_crypt_cnt_wr_dis) {
  67. esp_efuse_write_field_cnt(WR_DIS_CRYPT_CNT, 1);
  68. }
  69. }
  70. esp_flash_enc_mode_t esp_get_flash_encryption_mode(void)
  71. {
  72. uint8_t efuse_flash_crypt_cnt_wr_protected = 0;
  73. #if CONFIG_IDF_TARGET_ESP32
  74. uint8_t dis_dl_enc = 0, dis_dl_dec = 0, dis_dl_cache = 0;
  75. #elif CONFIG_IDF_TARGET_ESP32S2
  76. uint8_t dis_dl_enc = 0;
  77. uint8_t dis_dl_icache = 0;
  78. uint8_t dis_dl_dcache = 0;
  79. #endif
  80. esp_flash_enc_mode_t mode = ESP_FLASH_ENC_MODE_DEVELOPMENT;
  81. if (esp_flash_encryption_enabled()) {
  82. /* Check if FLASH CRYPT CNT is write protected */
  83. esp_efuse_read_field_blob(WR_DIS_CRYPT_CNT, &efuse_flash_crypt_cnt_wr_protected, 1);
  84. if (efuse_flash_crypt_cnt_wr_protected) {
  85. #if CONFIG_IDF_TARGET_ESP32
  86. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_CACHE, &dis_dl_cache, 1);
  87. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_ENCRYPT, &dis_dl_enc, 1);
  88. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_DECRYPT, &dis_dl_dec, 1);
  89. /* Check if DISABLE_DL_DECRYPT, DISABLE_DL_ENCRYPT & DISABLE_DL_CACHE are set */
  90. if ( dis_dl_cache && dis_dl_enc && dis_dl_dec ) {
  91. mode = ESP_FLASH_ENC_MODE_RELEASE;
  92. }
  93. #elif CONFIG_IDF_TARGET_ESP32S2
  94. esp_efuse_read_field_blob(ESP_EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT, &dis_dl_enc, 1);
  95. esp_efuse_read_field_blob(ESP_EFUSE_DIS_DOWNLOAD_ICACHE, &dis_dl_icache, 1);
  96. esp_efuse_read_field_blob(ESP_EFUSE_DIS_DOWNLOAD_DCACHE, &dis_dl_dcache, 1);
  97. if (dis_dl_enc && dis_dl_icache && dis_dl_dcache) {
  98. mode = ESP_FLASH_ENC_MODE_RELEASE;
  99. }
  100. #endif
  101. }
  102. } else {
  103. mode = ESP_FLASH_ENC_MODE_DISABLED;
  104. }
  105. return mode;
  106. }