key_mgr.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "soc/soc_caps.h"
  8. #if SOC_KEY_MANAGER_SUPPORTED
  9. #include <stdint.h>
  10. #include "esp_attr.h"
  11. #include "ets_sys.h"
  12. #include "km.h"
  13. #if __cplusplus
  14. extern "C" {
  15. #endif
  16. // store huk info, occupy 96 words
  17. struct huk_info {
  18. #define HUK_INFO_LEN 384
  19. uint8_t info[HUK_INFO_LEN];
  20. uint32_t crc;
  21. } PACKED_ATTR;
  22. // store key info, occupy 512 bits
  23. struct key_info {
  24. #define KEY_INFO_LEN 64
  25. uint8_t info[KEY_INFO_LEN];
  26. uint32_t crc;
  27. } PACKED_ATTR;
  28. struct huk_key_block {
  29. #define KEY_HUK_SECTOR_MAGIC 0xDEA5CE5A
  30. uint32_t magic;
  31. uint32_t version; // for backward compatibility
  32. uint8_t reserved[16];
  33. struct huk_info huk_info;
  34. struct key_info key_info[2]; // at most 2 key info (XTS-512_1 and XTS-512_2), at least use 1
  35. } WORD_ALIGNED_ATTR PACKED_ATTR;
  36. /*
  37. * We define two info sectors "active" and "backup" here
  38. * Most rom code would rely only on the "active" sector for the key information
  39. *
  40. * But there could be a situation where the huk and key information must be regenerated
  41. * based on ageing and other factors. For that scenario, we need a "backup" sector
  42. */
  43. #define KEY_HUK_SECTOR_OFFSET(i) ((i)*0x1000)
  44. #define ACTIVE_SECTOR_OFFSET KEY_HUK_SECTOR_OFFSET(0)
  45. #define BACKUP_SECTOR_OFFSET KEY_HUK_SECTOR_OFFSET(1)
  46. #define KM_PERI_ECDSA (BIT(0))
  47. #define KM_PERI_XTS (BIT(1))
  48. struct km_deploy_ops {
  49. #define KM_KEY_PURPOSE_ECDSA 1
  50. #define KM_KEY_PURPOSE_XTS_256_1 2
  51. #define KM_KEY_PURPOSE_XTS_256_2 3
  52. #define KM_KEY_PURPOSE_XTS_128 4
  53. int km_key_purpose;
  54. #define KM_DEPLOY_MODE_RANDOM 0
  55. #define KM_DEPLOY_MODE_AES 1
  56. #define KM_DEPLOY_MODE_ECDH0 2
  57. #define KM_DEPLOY_MODE_ECDH1 3
  58. #define KM_DEPLOY_MODE_RECOVER 4
  59. #define KM_DEPLOY_MODE_EXPORT 5
  60. int deploy_mode;
  61. uint8_t *init_key; // 256 bits, only used in aes and ecdh1 deploy mode
  62. int deploy_only_once;
  63. int force_use_km_key;
  64. int km_use_efuse_key;
  65. uint32_t efuse_km_rnd_switch_cycle; // 0 means use default
  66. uint32_t km_rnd_switch_cycle; // 0 means use default
  67. int km_use_sw_init_key;
  68. struct huk_info *huk_info;
  69. struct key_info *key_info;
  70. };
  71. /* state of km */
  72. #define KM_STATE_IDLE 0
  73. #define KM_STATE_LOAD 1
  74. #define KM_STATE_GAIN 2
  75. #define KM_STATE_BUSY 3
  76. #define KM_STATE_INVALID 4
  77. /* state of huk generator
  78. * values defined same as km
  79. */
  80. #define HUK_STATE_IDLE 0
  81. #define HUK_STATE_LOAD 1
  82. #define HUK_STATE_GAIN 2
  83. #define HUK_STATE_BUSY 3
  84. #define HUK_NOT_GENERATED 0
  85. #define HUK_GEN_VALID 1
  86. #define HUK_GEN_INVALID 2
  87. #if __cplusplus
  88. }
  89. #endif
  90. #endif