esp_flash_encrypt.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef __ESP32_FLASH_ENCRYPT_H
  14. #define __ESP32_FLASH_ENCRYPT_H
  15. #include <stdbool.h>
  16. #include "esp_attr.h"
  17. #include "esp_err.h"
  18. #include "esp_spi_flash.h"
  19. #include "soc/efuse_reg.h"
  20. /**
  21. * @file esp_partition.h
  22. * @brief Support functions for flash encryption features
  23. *
  24. * Can be compiled as part of app or bootloader code.
  25. */
  26. /** @brief Is flash encryption currently enabled in hardware?
  27. *
  28. * Flash encryption is enabled if the FLASH_CRYPT_CNT efuse has an odd number of bits set.
  29. *
  30. * @return true if flash encryption is enabled.
  31. */
  32. static inline /** @cond */ IRAM_ATTR /** @endcond */ bool esp_flash_encryption_enabled(void) {
  33. uint32_t flash_crypt_cnt = REG_GET_FIELD(EFUSE_BLK0_RDATA0_REG, EFUSE_RD_FLASH_CRYPT_CNT);
  34. /* __builtin_parity is in flash, so we calculate parity inline */
  35. bool enabled = false;
  36. while(flash_crypt_cnt) {
  37. if (flash_crypt_cnt & 1) {
  38. enabled = !enabled;
  39. }
  40. flash_crypt_cnt >>= 1;
  41. }
  42. return enabled;
  43. }
  44. /* @brief Update on-device flash encryption
  45. *
  46. * Intended to be called as part of the bootloader process if flash
  47. * encryption is enabled in device menuconfig.
  48. *
  49. * If FLASH_CRYPT_CNT efuse parity is 1 (ie odd number of bits set),
  50. * then return ESP_OK immediately (indicating flash encryption is enabled
  51. * and functional).
  52. *
  53. * If FLASH_CRYPT_CNT efuse parity is 0 (ie even number of bits set),
  54. * assume the flash has just been written with plaintext that needs encrypting.
  55. *
  56. * The following regions of flash are encrypted in place:
  57. *
  58. * - The bootloader image, if a valid plaintext image is found.[*]
  59. * - The partition table, if a valid plaintext table is found.
  60. * - Any app partition that contains a valid plaintext app image.
  61. * - Any other partitions with the "encrypt" flag set. [**]
  62. *
  63. * After the re-encryption process completes, a '1' bit is added to the
  64. * FLASH_CRYPT_CNT value (setting the parity to 1) and the EFUSE is re-burned.
  65. *
  66. * [*] If reflashing bootloader with secure boot enabled, pre-encrypt
  67. * the bootloader before writing it to flash or secure boot will fail.
  68. *
  69. * [**] For this reason, if serial re-flashing a previous flashed
  70. * device with secure boot enabled and using FLASH_CRYPT_CNT to
  71. * trigger re-encryption, you must simultaneously re-flash plaintext
  72. * content to all partitions with the "encrypt" flag set or this
  73. * data will be corrupted (encrypted twice).
  74. *
  75. * @note The post-condition of this function is that all
  76. * partitions that should be encrypted are encrypted.
  77. *
  78. * @note Take care not to power off the device while this function
  79. * is running, or the partition currently being encrypted will be lost.
  80. *
  81. * @return ESP_OK if all operations succeeded, ESP_ERR_INVALID_STATE
  82. * if a fatal error occured during encryption of all partitions.
  83. */
  84. esp_err_t esp_flash_encrypt_check_and_update(void);
  85. /** @brief Encrypt-in-place a block of flash sectors
  86. *
  87. * @param src_addr Source offset in flash. Should be multiple of 4096 bytes.
  88. * @param data_length Length of data to encrypt in bytes. Will be rounded up to next multiple of 4096 bytes.
  89. *
  90. * @return ESP_OK if all operations succeeded, ESP_ERR_FLASH_OP_FAIL
  91. * if SPI flash fails, ESP_ERR_FLASH_OP_TIMEOUT if flash times out.
  92. */
  93. esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length);
  94. #endif