esp_hmac.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 a writeable buffer of 32 bytes
  40. *
  41. * @return
  42. * * ESP_OK, if the calculation was successful,
  43. * * ESP_ERR_INVALID_ARG if message or hmac is a nullptr or if key_id out of range
  44. * * ESP_FAIL, if the hmac calculation failed
  45. */
  46. esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
  47. const void *message,
  48. size_t message_len,
  49. uint8_t *hmac);
  50. /**
  51. * @brief
  52. * Use HMAC peripheral in Downstream mode to re-enable the JTAG, if it is not permanently disable by HW.
  53. * In downstream mode HMAC calculations perfomred by peripheral used internally and not provided back to user.
  54. *
  55. * @param key_id Determines which of the 6 key blocks in the efuses should be used for the HMAC calculation.
  56. * The corresponding purpose field of the key block in the efuse must be set to HMAC downstream purpose.
  57. *
  58. * @param token Pre calculated HMAC value of the 32-byte 0x00 using SHA-256 and the known private HMAC key. The key is already
  59. * programmed to a eFuse key block. The key block number is provided as the first parameter to this function.
  60. *
  61. * @return
  62. * * ESP_OK, if the key_purpose of the key_id matches to HMAC downstread mode,
  63. * The API returns success even if calculated HMAC does not match with the provided token.
  64. * However, The JTAG will be re-enabled only if the calculated HMAC value matches with provided token,
  65. * otherwise JTAG will remain disabled.
  66. * * ESP_FAIL, if the key_purpose of the key_id is not set to HMAC downstream purpose
  67. * or JTAG is permanently disabled by EFUSE_HARD_DIS_JTAG eFuse parameter.
  68. * * ESP_ERR_INVALID_ARG, invalid input arguments
  69. *
  70. * @note Return value of the API does not indicate the JTAG status.
  71. */
  72. esp_err_t esp_hmac_jtag_enable(hmac_key_id_t key_id,
  73. const uint8_t *token);
  74. /**
  75. * @brief
  76. * Disable the JTAG which might be enable using the HMAC downstream mode. This function just clear the result generated by
  77. * JTAG key 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
  86. #endif // _ESP_HMAC_H_