esp_hmac.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _ESP_HMAC_H_
  14. #define _ESP_HMAC_H_
  15. #include "esp_err.h"
  16. #include "stdbool.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * The possible efuse keys for the HMAC peripheral
  22. */
  23. typedef enum {
  24. HMAC_KEY0 = 0,
  25. HMAC_KEY1,
  26. HMAC_KEY2,
  27. HMAC_KEY3,
  28. HMAC_KEY4,
  29. HMAC_KEY5,
  30. HMAC_KEY_MAX
  31. } hmac_key_id_t;
  32. /**
  33. * @brief
  34. * Calculate the HMAC of a given message.
  35. *
  36. * Calculate the HMAC \c hmac of a given message \c message with length \c message_len.
  37. * SHA256 is used for the calculation (fixed on ESP32S2).
  38. *
  39. * @note Uses the HMAC peripheral in "upstream" mode.
  40. *
  41. * @param key_id Determines which of the 6 key blocks in the efuses should be used for the HMAC calcuation.
  42. * The corresponding purpose field of the key block in the efuse must be set to the HMAC upstream purpose value.
  43. * @param message the message for which to calculate the HMAC
  44. * @param message_len message length
  45. * return ESP_ERR_INVALID_STATE if unsuccessful
  46. * @param [out] hmac the hmac result; the buffer behind the provided pointer must be 32 bytes long
  47. *
  48. * @return
  49. * * ESP_OK, if the calculation was successful,
  50. * * ESP_FAIL, if the hmac calculation failed
  51. */
  52. esp_err_t esp_hmac_calculate(hmac_key_id_t key_id,
  53. const void *message,
  54. size_t message_len,
  55. uint8_t *hmac);
  56. #ifdef __cplusplus
  57. }
  58. #endif
  59. #endif // _ESP_HMAC_H_