esp_hmac.h 3.5 KB

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