esp_hmac.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "esp_err.h"
  9. #include "stdbool.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
  51. * Use HMAC peripheral in Downstream mode to re-enable the JTAG, if it is not permanently disable by HW.
  52. * In downstream mode HMAC calculations perfomred by peripheral used internally and not provided back to user.
  53. *
  54. * @param key_id Determines which of the 6 key blocks in the efuses should be used for the HMAC calculation.
  55. * The corresponding purpose field of the key block in the efuse must be set to HMAC downstream purpose.
  56. *
  57. * @param token Pre calculated HMAC value of the 32-byte 0x00 using SHA-256 and the known private HMAC key. The key is already
  58. * programmed to a eFuse key block. The key block number is provided as the first parameter to this function.
  59. *
  60. * @return
  61. * * ESP_OK, if the calculation was successful,
  62. * if the calculated HMAC value matches with provided token,
  63. * JTAG will be re-enable otherwise JTAG will remain disabled.
  64. * Return value does not indicate the JTAG status.
  65. * * ESP_FAIL, if the hmac calculation failed or JTAG is permanently disabled by EFUSE_HARD_DIS_JTAG eFuse parameter.
  66. * * ESP_ERR_INVALID_ARG, invalid input arguments
  67. */
  68. esp_err_t esp_hmac_jtag_enable(hmac_key_id_t key_id,
  69. const uint8_t *token);
  70. /**
  71. * @brief
  72. * Disable the JTAG which might be enable using the HMAC downstream mode. This function just clear the result generated by
  73. * JTAG key by calling esp_hmac_jtag_enable() API.
  74. *
  75. * @return
  76. * * ESP_OK return ESP_OK after writing the HMAC_SET_INVALIDATE_JTAG_REG with value 1.
  77. */
  78. esp_err_t esp_hmac_jtag_disable(void);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif // _ESP_HMAC_H_