esp_hmac.h 3.0 KB

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