nvs_flash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef nvs_flash_h
  7. #define nvs_flash_h
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include "nvs.h"
  12. #include "esp_partition.h"
  13. #define NVS_KEY_SIZE 32 // AES-256
  14. /**
  15. * @brief Key for encryption and decryption
  16. */
  17. typedef struct {
  18. uint8_t eky[NVS_KEY_SIZE]; /*!< XTS encryption and decryption key*/
  19. uint8_t tky[NVS_KEY_SIZE]; /*!< XTS tweak key */
  20. } nvs_sec_cfg_t;
  21. /**
  22. * @brief Callback function prototype for generating the NVS encryption keys
  23. */
  24. typedef esp_err_t (*nvs_flash_generate_keys_t) (const void *scheme_data, nvs_sec_cfg_t* cfg);
  25. /**
  26. * @brief Callback function prototype for reading the NVS encryption keys
  27. */
  28. typedef esp_err_t (*nvs_flash_read_cfg_t) (const void *scheme_data, nvs_sec_cfg_t* cfg);
  29. /**
  30. * @brief NVS encryption: Security scheme configuration structure
  31. */
  32. typedef struct
  33. {
  34. int scheme_id; /*!< Security Scheme ID (E.g. HMAC) */
  35. void *scheme_data; /*!< Scheme-specific data (E.g. eFuse block for HMAC-based key generation) */
  36. nvs_flash_generate_keys_t nvs_flash_key_gen; /*!< Callback for the nvs_flash_key_gen implementation */
  37. nvs_flash_read_cfg_t nvs_flash_read_cfg; /*!< Callback for the nvs_flash_read_keys implementation */
  38. } nvs_sec_scheme_t;
  39. /**
  40. * @brief Initialize the default NVS partition.
  41. *
  42. * This API initialises the default NVS partition. The default NVS partition
  43. * is the one that is labeled "nvs" in the partition table.
  44. *
  45. * When "NVS_ENCRYPTION" is enabled in the menuconfig, this API enables
  46. * the NVS encryption for the default NVS partition as follows
  47. * 1. Read security configurations from the first NVS key
  48. * partition listed in the partition table. (NVS key partition is
  49. * any "data" type partition which has the subtype value set to "nvs_keys")
  50. * 2. If the NVS key partiton obtained in the previous step is empty,
  51. * generate and store new keys in that NVS key partiton.
  52. * 3. Internally call "nvs_flash_secure_init()" with
  53. * the security configurations obtained/generated in the previous steps.
  54. *
  55. * Post initialization NVS read/write APIs
  56. * remain the same irrespective of NVS encryption.
  57. *
  58. * @return
  59. * - ESP_OK if storage was successfully initialized.
  60. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  61. * (which may happen if NVS partition was truncated)
  62. * - ESP_ERR_NOT_FOUND if no partition with label "nvs" is found in the partition table
  63. * - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
  64. * - one of the error codes from the underlying flash storage driver
  65. * - error codes from nvs_flash_read_security_cfg API (when "NVS_ENCRYPTION" is enabled).
  66. * - error codes from nvs_flash_generate_keys API (when "NVS_ENCRYPTION" is enabled).
  67. * - error codes from nvs_flash_secure_init_partition API (when "NVS_ENCRYPTION" is enabled) .
  68. */
  69. esp_err_t nvs_flash_init(void);
  70. /**
  71. * @brief Initialize NVS flash storage for the specified partition.
  72. *
  73. * @param[in] partition_label Label of the partition. Must be no longer than 16 characters.
  74. *
  75. * @return
  76. * - ESP_OK if storage was successfully initialized.
  77. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  78. * (which may happen if NVS partition was truncated)
  79. * - ESP_ERR_NOT_FOUND if specified partition is not found in the partition table
  80. * - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
  81. * - one of the error codes from the underlying flash storage driver
  82. */
  83. esp_err_t nvs_flash_init_partition(const char *partition_label);
  84. /**
  85. * @brief Initialize NVS flash storage for the partition specified by partition pointer.
  86. *
  87. * @param[in] partition pointer to a partition obtained by the ESP partition API.
  88. *
  89. * @return
  90. * - ESP_OK if storage was successfully initialized
  91. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  92. * (which may happen if NVS partition was truncated)
  93. * - ESP_ERR_INVALID_ARG in case partition is NULL
  94. * - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
  95. * - one of the error codes from the underlying flash storage driver
  96. */
  97. esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partition);
  98. /**
  99. * @brief Deinitialize NVS storage for the default NVS partition
  100. *
  101. * Default NVS partition is the partition with "nvs" label in the partition table.
  102. *
  103. * @return
  104. * - ESP_OK on success (storage was deinitialized)
  105. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage was not initialized prior to this call
  106. */
  107. esp_err_t nvs_flash_deinit(void);
  108. /**
  109. * @brief Deinitialize NVS storage for the given NVS partition
  110. *
  111. * @param[in] partition_label Label of the partition
  112. *
  113. * @return
  114. * - ESP_OK on success
  115. * - ESP_ERR_NVS_NOT_INITIALIZED if the storage for given partition was not
  116. * initialized prior to this call
  117. */
  118. esp_err_t nvs_flash_deinit_partition(const char* partition_label);
  119. /**
  120. * @brief Erase the default NVS partition
  121. *
  122. * Erases all contents of the default NVS partition (one with label "nvs").
  123. *
  124. * @note If the partition is initialized, this function first de-initializes it. Afterwards, the partition has to
  125. * be initialized again to be used.
  126. *
  127. * @return
  128. * - ESP_OK on success
  129. * - ESP_ERR_NOT_FOUND if there is no NVS partition labeled "nvs" in the
  130. * partition table
  131. * - different error in case de-initialization fails (shouldn't happen)
  132. */
  133. esp_err_t nvs_flash_erase(void);
  134. /**
  135. * @brief Erase specified NVS partition
  136. *
  137. * Erase all content of a specified NVS partition
  138. *
  139. * @note If the partition is initialized, this function first de-initializes it. Afterwards, the partition has to
  140. * be initialized again to be used.
  141. *
  142. * @param[in] part_name Name (label) of the partition which should be erased
  143. *
  144. * @return
  145. * - ESP_OK on success
  146. * - ESP_ERR_NOT_FOUND if there is no NVS partition with the specified name
  147. * in the partition table
  148. * - different error in case de-initialization fails (shouldn't happen)
  149. */
  150. esp_err_t nvs_flash_erase_partition(const char *part_name);
  151. /**
  152. * @brief Erase custom partition.
  153. *
  154. * Erase all content of specified custom partition.
  155. *
  156. * @note
  157. * If the partition is initialized, this function first de-initializes it.
  158. * Afterwards, the partition has to be initialized again to be used.
  159. *
  160. * @param[in] partition pointer to a partition obtained by the ESP partition API.
  161. *
  162. * @return
  163. * - ESP_OK on success
  164. * - ESP_ERR_NOT_FOUND if there is no partition with the specified
  165. * parameters in the partition table
  166. * - ESP_ERR_INVALID_ARG in case partition is NULL
  167. * - one of the error codes from the underlying flash storage driver
  168. */
  169. esp_err_t nvs_flash_erase_partition_ptr(const esp_partition_t *partition);
  170. /**
  171. * @brief Initialize the default NVS partition.
  172. *
  173. * This API initialises the default NVS partition. The default NVS partition
  174. * is the one that is labeled "nvs" in the partition table.
  175. *
  176. * @param[in] cfg Security configuration (keys) to be used for NVS encryption/decryption.
  177. * If cfg is NULL, no encryption is used.
  178. *
  179. * @return
  180. * - ESP_OK if storage has been initialized successfully.
  181. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  182. * (which may happen if NVS partition was truncated)
  183. * - ESP_ERR_NOT_FOUND if no partition with label "nvs" is found in the partition table
  184. * - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
  185. * - one of the error codes from the underlying flash storage driver
  186. */
  187. esp_err_t nvs_flash_secure_init(nvs_sec_cfg_t* cfg);
  188. /**
  189. * @brief Initialize NVS flash storage for the specified partition.
  190. *
  191. * @param[in] partition_label Label of the partition. Note that internally, a reference to
  192. * passed value is kept and it should be accessible for future operations
  193. *
  194. * @param[in] cfg Security configuration (keys) to be used for NVS encryption/decryption.
  195. * If cfg is null, no encryption/decryption is used.
  196. * @return
  197. * - ESP_OK if storage has been initialized successfully.
  198. * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
  199. * (which may happen if NVS partition was truncated)
  200. * - ESP_ERR_NOT_FOUND if specified partition is not found in the partition table
  201. * - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
  202. * - one of the error codes from the underlying flash storage driver
  203. */
  204. esp_err_t nvs_flash_secure_init_partition(const char *partition_label, nvs_sec_cfg_t* cfg);
  205. /**
  206. * @brief Generate and store NVS keys in the provided esp partition
  207. *
  208. * @param[in] partition Pointer to partition structure obtained using
  209. * esp_partition_find_first or esp_partition_get.
  210. * Must be non-NULL.
  211. * @param[out] cfg Pointer to nvs security configuration structure.
  212. * Pointer must be non-NULL.
  213. * Generated keys will be populated in this structure.
  214. *
  215. *
  216. * @return
  217. * - ESP_OK, if cfg was read successfully;
  218. * - ESP_ERR_INVALID_ARG, if partition or cfg is NULL;
  219. * - or error codes from esp_partition_write/erase APIs.
  220. */
  221. esp_err_t nvs_flash_generate_keys(const esp_partition_t* partition, nvs_sec_cfg_t* cfg);
  222. /**
  223. * @brief Read NVS security configuration from a partition.
  224. *
  225. * @param[in] partition Pointer to partition structure obtained using
  226. * esp_partition_find_first or esp_partition_get.
  227. * Must be non-NULL.
  228. * @param[out] cfg Pointer to nvs security configuration structure.
  229. * Pointer must be non-NULL.
  230. *
  231. * @note Provided partition is assumed to be marked 'encrypted'.
  232. *
  233. * @return
  234. * - ESP_OK, if cfg was read successfully;
  235. * - ESP_ERR_INVALID_ARG, if partition or cfg is NULL
  236. * - ESP_ERR_NVS_KEYS_NOT_INITIALIZED, if the partition is not yet written with keys.
  237. * - ESP_ERR_NVS_CORRUPT_KEY_PART, if the partition containing keys is found to be corrupt
  238. * - or error codes from esp_partition_read API.
  239. */
  240. esp_err_t nvs_flash_read_security_cfg(const esp_partition_t* partition, nvs_sec_cfg_t* cfg);
  241. /**
  242. * @brief Registers the given security scheme for NVS encryption
  243. * The scheme registered with sec_scheme_id by this API be used as
  244. * the default security scheme for the "nvs" partition.
  245. * Users will have to call this API explicitly in their application.
  246. *
  247. * @param[in] scheme_cfg Pointer to the security scheme configuration structure
  248. * that the user (or the nvs_key_provider) wants to register.
  249. *
  250. * @return
  251. * - ESP_OK, if security scheme registration succeeds;
  252. * - ESP_ERR_INVALID_ARG, if scheme_cfg is NULL;
  253. * - ESP_FAIL, if security scheme registration fails
  254. */
  255. esp_err_t nvs_flash_register_security_scheme(nvs_sec_scheme_t *scheme_cfg);
  256. /**
  257. * @brief Fetch the configuration structure for the default active
  258. * security scheme for NVS encryption
  259. *
  260. * @return Pointer to the default active security scheme configuration
  261. * (NULL if no scheme is registered yet i.e. active)
  262. */
  263. nvs_sec_scheme_t *nvs_flash_get_default_security_scheme(void);
  264. /**
  265. * @brief Generate (and store) the NVS keys using the specified key-protection scheme
  266. *
  267. * @param[in] scheme_cfg Security scheme specific configuration
  268. *
  269. * @param[out] cfg Security configuration (encryption keys)
  270. *
  271. * @return
  272. * - ESP_OK, if cfg was populated successfully with generated encryption keys;
  273. * - ESP_ERR_INVALID_ARG, if scheme_cfg or cfg is NULL;
  274. * - ESP_FAIL, if the key generation process fails
  275. */
  276. esp_err_t nvs_flash_generate_keys_v2(nvs_sec_scheme_t *scheme_cfg, nvs_sec_cfg_t* cfg);
  277. /**
  278. * @brief Read NVS security configuration set by the specified security scheme
  279. *
  280. * @param[in] scheme_cfg Security scheme specific configuration
  281. *
  282. * @param[out] cfg Security configuration (encryption keys)
  283. *
  284. * @return
  285. * - ESP_OK, if cfg was read successfully;
  286. * - ESP_ERR_INVALID_ARG, if scheme_cfg or cfg is NULL;
  287. * - ESP_FAIL, if the key reading process fails
  288. */
  289. esp_err_t nvs_flash_read_security_cfg_v2(nvs_sec_scheme_t *scheme_cfg, nvs_sec_cfg_t* cfg);
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif /* nvs_flash_h */