flash_encrypt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef BOOTLOADER_BUILD
  21. static const char *TAG = "flash_encrypt";
  22. void esp_flash_encryption_init_checks()
  23. {
  24. esp_flash_enc_mode_t mode;
  25. // First check is: if Release mode flash encryption & secure boot are enabled then
  26. // FLASH_CRYPT_CNT *must* be write protected. This will have happened automatically
  27. // if bootloader is IDF V4.0 or newer but may not have happened for previous ESP-IDF bootloaders.
  28. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  29. #ifdef CONFIG_SECURE_BOOT_ENABLED
  30. if (esp_secure_boot_enabled() && esp_flash_encryption_enabled()) {
  31. uint8_t flash_crypt_cnt_wr_dis = 0;
  32. esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1);
  33. if (!flash_crypt_cnt_wr_dis) {
  34. ESP_LOGE(TAG, "Flash encryption & Secure Boot together requires FLASH_CRYPT_CNT efuse to be write protected. Fixing now...");
  35. esp_flash_write_protect_crypt_cnt();
  36. }
  37. }
  38. #endif // CONFIG_SECURE_BOOT_ENABLED
  39. #endif // CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  40. // Second check is to print a warning or error if the current running flash encryption mode
  41. // doesn't match the expectation from project config (due to mismatched bootloader and app, probably)
  42. mode = esp_get_flash_encryption_mode();
  43. if (mode == ESP_FLASH_ENC_MODE_DEVELOPMENT) {
  44. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  45. ESP_LOGE(TAG, "Flash encryption settings error: app is configured for RELEASE but efuses are set for DEVELOPMENT");
  46. ESP_LOGE(TAG, "Mismatch found in security options in bootloader menuconfig and efuse settings. Device is not secure.");
  47. #else
  48. ESP_LOGW(TAG, "Flash encryption mode is DEVELOPMENT (not secure)");
  49. #endif
  50. } else if (mode == ESP_FLASH_ENC_MODE_RELEASE) {
  51. ESP_LOGI(TAG, "Flash encryption mode is RELEASE");
  52. }
  53. }
  54. #endif
  55. void esp_flash_write_protect_crypt_cnt()
  56. {
  57. uint8_t flash_crypt_cnt_wr_dis = 0;
  58. esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1);
  59. if (!flash_crypt_cnt_wr_dis) {
  60. esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, 1);
  61. }
  62. }
  63. esp_flash_enc_mode_t esp_get_flash_encryption_mode()
  64. {
  65. uint8_t flash_crypt_cnt_wr_dis = 0;
  66. uint8_t dis_dl_enc = 0, dis_dl_dec = 0, dis_dl_cache = 0;
  67. esp_flash_enc_mode_t mode = ESP_FLASH_ENC_MODE_DEVELOPMENT;
  68. if (esp_flash_encryption_enabled()) {
  69. /* Check if FLASH CRYPT CNT is write protected */
  70. esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1);
  71. if (!flash_crypt_cnt_wr_dis) {
  72. uint8_t flash_crypt_cnt = 0;
  73. esp_efuse_read_field_blob(ESP_EFUSE_FLASH_CRYPT_CNT, &flash_crypt_cnt, ESP_EFUSE_FLASH_CRYPT_CNT[0]->bit_count);
  74. if (flash_crypt_cnt == (1 << (ESP_EFUSE_FLASH_CRYPT_CNT[0]->bit_count)) - 1) {
  75. flash_crypt_cnt_wr_dis = 1;
  76. }
  77. }
  78. if (flash_crypt_cnt_wr_dis) {
  79. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_CACHE, &dis_dl_cache, 1);
  80. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_ENCRYPT, &dis_dl_enc, 1);
  81. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_DECRYPT, &dis_dl_dec, 1);
  82. /* Check if DISABLE_DL_DECRYPT, DISABLE_DL_ENCRYPT & DISABLE_DL_CACHE are set */
  83. if ( dis_dl_cache && dis_dl_enc && dis_dl_dec ) {
  84. mode = ESP_FLASH_ENC_MODE_RELEASE;
  85. }
  86. }
  87. } else {
  88. mode = ESP_FLASH_ENC_MODE_DISABLED;
  89. }
  90. return mode;
  91. }