hmac_hal.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*******************************************************************************
  7. * NOTICE
  8. * The hal is not public api, don't use it in application code.
  9. * See readme.md in soc/include/hal/readme.md
  10. ******************************************************************************/
  11. #pragma once
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /**
  18. * The HMAC peripheral can be configured to deliver its output to the user directly, or to deliver
  19. * the output directly to another peripheral instead, e.g. the Digital Signature peripheral.
  20. */
  21. typedef enum {
  22. HMAC_OUTPUT_USER = 0, /**< Let user provide a message and read the HMAC result */
  23. HMAC_OUTPUT_DS = 1, /**< HMAC is provided to the DS peripheral to decrypt DS private key parameters */
  24. HMAC_OUTPUT_JTAG_ENABLE = 2, /**< HMAC is used to enable JTAG after soft-disabling it */
  25. HMAC_OUTPUT_ALL = 3 /**< HMAC is used for both as DS input for or enabling JTAG */
  26. } hmac_hal_output_t;
  27. /**
  28. * @brief Make the peripheral ready for use.
  29. *
  30. * This triggers any further steps necessary after enabling the device
  31. */
  32. void hmac_hal_start(void);
  33. /**
  34. * @brief Configure which hardware key slot should be used and configure the target of the HMAC output.
  35. *
  36. * @note Writing out-of-range values is undefined behavior. The user has to ensure that the parameters are in range.
  37. *
  38. * @param config The target of the HMAC. Possible targets are described in \c hmac_hal_output_t.
  39. * See the TRM of your target chip for more details.
  40. * @param key_id The ID of the hardware key slot to be used.
  41. *
  42. * @return 0 if the configuration was successful, non-zero if not.
  43. * An unsuccessful configuration means that the purpose value in the eFuse of the corresponding key slot
  44. * doesn't match to supplied value of \c config.
  45. */
  46. uint32_t hmac_hal_configure(hmac_hal_output_t config, uint32_t key_id);
  47. /**
  48. * @brief Write a padded single-block message of 512 bits to the HMAC peripheral.
  49. *
  50. * The message must not be longer than one block (512 bits) and the padding has to be applied by software before
  51. * writing. The padding has to be able to fit into the block after the message.
  52. * For more information on HMAC padding, see the TRM of your target chip.
  53. */
  54. void hmac_hal_write_one_block_512(const void *block);
  55. /**
  56. * @brief Write a message block of 512 bits to the HMAC peripheral.
  57. *
  58. * This function must be used incombination with \c hmac_hal_next_block_normal() or \c hmac_hal_next_block_padding().
  59. * The first message block is written without any prerequisite.
  60. * All message blocks which are not the last one, need a call to \c hmac_hal_next_block_normal() before, indicating
  61. * to the hardware that a "normal", i.e. non-padded block will follow. This is even the case for a block which begins
  62. * padding already but where the padding doesn't fit in (remaining message size > (block size - padding size)).
  63. * Before writing the last block which contains the padding, a call to \c hmac_hal_next_block_padding() is necessary
  64. * to indicate to the hardware that a block with padding will be written.
  65. *
  66. * For more information on HMAC padding, see the TRM of your target chip for more details.
  67. */
  68. void hmac_hal_write_block_512(const void *block);
  69. /**
  70. * @brief Indicate to the hardware that a normal block will be written.
  71. */
  72. void hmac_hal_next_block_normal(void);
  73. /**
  74. * @brief Indicate to the hardware that a block with padding will be written.
  75. */
  76. void hmac_hal_next_block_padding(void);
  77. /**
  78. * @brief Read the 256 bit HMAC result from the hardware.
  79. */
  80. void hmac_hal_read_result_256(void *result);
  81. /**
  82. * @brief Clear (invalidate) the HMAC result provided to other hardware.
  83. */
  84. void hmac_hal_clean(void);
  85. #ifdef __cplusplus
  86. }
  87. #endif