esp_hmac.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2015-2020 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 _ESP_HMAC_H_
  14. #define _ESP_HMAC_H_
  15. #include "esp_err.h"
  16. #include "stdbool.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * The possible efuse keys for the HMAC peripheral
  22. */
  23. typedef enum {
  24. HMAC_KEY0 = 0,
  25. HMAC_KEY1,
  26. HMAC_KEY2,
  27. HMAC_KEY3,
  28. HMAC_KEY4,
  29. HMAC_KEY5,
  30. HMAC_KEY_MAX
  31. } hmac_key_id_t;
  32. /**
  33. * @brief
  34. * Calculate the HMAC of a given message.
  35. *
  36. * Calculate the HMAC \c hmac of a given message \c message with length \c message_len.
  37. * SHA256 is used for the calculation (fixed on ESP32S2).
  38. *
  39. * @note Uses the HMAC peripheral in "upstream" mode.
  40. *
  41. * @param key_id Determines which of the 6 key blocks in the efuses should be used for the HMAC calcuation.
  42. * The corresponding purpose field of the key block in the efuse must be set to the HMAC upstream purpose value.
  43. * @param message the message for which to calculate the HMAC
  44. * @param message_len message length
  45. * return ESP_ERR_INVALID_STATE if unsuccessful
  46. * @param [out] hmac the hmac result; the buffer behind the provided pointer must be 32 bytes long
  47. *
  48. * @return
  49. * * ESP_OK, if the calculation was successful,
  50. * * ESP_FAIL, if the hmac calculation failed
  51. */
  52. esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
  53. const void *message,
  54. size_t message_len,
  55. uint8_t *hmac);
  56. /**
  57. * @brief
  58. * Use HMAC peripheral in Downstream mode to re-enable the JTAG, if it is not permanently disable by HW.
  59. * In downstream mode HMAC calculations perfomred by peripheral used internally and not provided back to user.
  60. *
  61. * @param key_id Determines which of the 6 key blocks in the efuses should be used for the HMAC calculation.
  62. * The corresponding purpose field of the key block in the efuse must be set to HMAC downstream purpose.
  63. *
  64. * @param token Pre calculated HMAC value of the 32-byte 0x00 using SHA-256 and the known private HMAC key. The key is already
  65. * programmed to a eFuse key block. The key block number is provided as the first parameter to this function.
  66. *
  67. * @return
  68. * * ESP_OK, if the calculation was successful,
  69. * if the calculated HMAC value matches with provided token,
  70. * JTAG will be re-enable otherwise JTAG will remain disabled.
  71. * Return value does not indicate the JTAG status.
  72. * * ESP_FAIL, if the hmac calculation failed or JTAG is permanently disabled by EFUSE_HARD_DIS_JTAG eFuse parameter.
  73. * * ESP_ERR_INVALID_ARG, invalid input arguments
  74. */
  75. esp_err_t esp_hmac_jtag_enable(hmac_key_id_t key_id,
  76. const uint8_t *token);
  77. /**
  78. * @brief
  79. * Disable the JTAG which might be enable using the HMAC downstream mode. This function just clear the result generated by
  80. * JTAG key by calling esp_hmac_jtag_enable() API.
  81. *
  82. * @return
  83. * * ESP_OK return ESP_OK after writing the HMAC_SET_INVALIDATE_JTAG_REG with value 1.
  84. */
  85. esp_err_t esp_hmac_jtag_disable(void);
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif // _ESP_HMAC_H_