esp_hmac.h 3.0 KB

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