nvs_sec_provider.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include "esp_err.h"
  9. #include "soc/soc_caps.h"
  10. #include "nvs_flash.h"
  11. #include "esp_partition.h"
  12. #if SOC_HMAC_SUPPORTED
  13. #include "esp_hmac.h"
  14. #endif
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define ESP_ERR_NVS_SEC_BASE 0xF000 /*!< Starting number of error codes */
  19. #define ESP_ERR_NVS_SEC_HMAC_KEY_NOT_FOUND (ESP_ERR_NVS_SEC_BASE + 0x01) /*!< HMAC Key required to generate the NVS encryption keys not found */
  20. #define ESP_ERR_NVS_SEC_HMAC_KEY_BLK_ALREADY_USED (ESP_ERR_NVS_SEC_BASE + 0x02) /*!< Provided eFuse block for HMAC key generation is already in use */
  21. #define ESP_ERR_NVS_SEC_HMAC_KEY_GENERATION_FAILED (ESP_ERR_NVS_SEC_BASE + 0x03) /*!< Failed to generate/write the HMAC key to eFuse */
  22. #define ESP_ERR_NVS_SEC_HMAC_XTS_KEYS_DERIV_FAILED (ESP_ERR_NVS_SEC_BASE + 0x04) /*!< Failed to derive the NVS encryption keys based on the HMAC-based scheme */
  23. /**
  24. * @brief NVS Encryption Keys Protection Scheme
  25. */
  26. typedef enum {
  27. NVS_SEC_SCHEME_FLASH_ENC = 0, /*!< Protect NVS encryption keys using Flash Encryption */
  28. NVS_SEC_SCHEME_HMAC, /*!< Protect NVS encryption keys using HMAC peripheral */
  29. NVS_SEC_SCHEME_MAX
  30. } nvs_sec_scheme_id_t;
  31. /**
  32. * @brief Flash encryption-based scheme specific configuration data
  33. */
  34. typedef struct {
  35. const esp_partition_t *nvs_keys_part; /*!< Partition of subtype `nvs_keys` holding the NVS encryption keys */
  36. } nvs_sec_config_flash_enc_t;
  37. /**
  38. * @brief Helper for populating the Flash encryption-based scheme specific configuration data
  39. */
  40. #define NVS_SEC_PROVIDER_CFG_FLASH_ENC_DEFAULT() { \
  41. .nvs_keys_part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, \
  42. ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS, \
  43. NULL), \
  44. }
  45. #if SOC_HMAC_SUPPORTED
  46. /**
  47. * @brief HMAC-based scheme specific configuration data
  48. */
  49. typedef struct {
  50. hmac_key_id_t hmac_key_id; /*!< HMAC Key ID used for generating the NVS encryption keys */
  51. } nvs_sec_config_hmac_t;
  52. /**
  53. * @brief Helper for populating the HMAC-based scheme specific configuration data
  54. */
  55. #define NVS_SEC_PROVIDER_CFG_HMAC_DEFAULT() { \
  56. .hmac_key_id = (hmac_key_id_t)(CONFIG_NVS_SEC_HMAC_EFUSE_KEY_ID), \
  57. }
  58. #endif /* SOC_HMAC_SUPPORTED */
  59. /**
  60. * @brief Register the Flash-Encryption based scheme for NVS Encryption
  61. *
  62. * @param[in] sec_scheme_cfg Security scheme specific configuration data
  63. * @param[out] sec_scheme_handle_out Security scheme specific configuration handle
  64. *
  65. * @return
  66. * - ESP_OK, if `sec_scheme_handle_out` was populated successfully with the scheme configuration;
  67. * - ESP_ERR_INVALID_ARG, if `scheme_cfg_hmac` is NULL;
  68. * - ESP_ERR_NO_MEM, No memory for the scheme-specific handle `sec_scheme_handle_out`
  69. * - ESP_ERR_NOT_FOUND, if no `nvs_keys` partition is found
  70. */
  71. esp_err_t nvs_sec_provider_register_flash_enc(const nvs_sec_config_flash_enc_t *sec_scheme_cfg, nvs_sec_scheme_t **sec_scheme_handle_out);
  72. #if SOC_HMAC_SUPPORTED
  73. /**
  74. * @brief Register the HMAC-based scheme for NVS Encryption
  75. *
  76. * @param[in] sec_scheme_cfg Security scheme specific configuration data
  77. * @param[out] sec_scheme_handle_out Security scheme specific configuration handle
  78. *
  79. * @return
  80. * - ESP_OK, if `sec_scheme_handle_out` was populated successfully with the scheme configuration;
  81. * - ESP_ERR_INVALID_ARG, if `scheme_cfg_hmac` is NULL;
  82. * - ESP_ERR_NO_MEM, No memory for the scheme-specific handle `sec_scheme_handle_out`
  83. */
  84. esp_err_t nvs_sec_provider_register_hmac(const nvs_sec_config_hmac_t *sec_scheme_cfg, nvs_sec_scheme_t **sec_scheme_handle_out);
  85. #endif /* SOC_HMAC_SUPPORTED */
  86. /**
  87. * @brief Deregister the NVS encryption scheme registered with the given handle
  88. *
  89. * @param[in] sec_scheme_handle Security scheme specific configuration handle
  90. * @return
  91. * - ESP_OK, if the scheme registered with `sec_scheme_handle` was deregistered successfully
  92. * - ESP_ERR_INVALID_ARG, if `sec_scheme_handle` is NULL;
  93. */
  94. esp_err_t nvs_sec_provider_deregister(nvs_sec_scheme_t *sec_scheme_handle);
  95. #ifdef __cplusplus
  96. }
  97. #endif