sha.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file drv/sha.h
  6. * @brief Header File for SHA Driver
  7. * @version V1.0
  8. * @date 9. Oct 2020
  9. * @model sha
  10. ******************************************************************************/
  11. #ifndef _DRV_SHA_H_
  12. #define _DRV_SHA_H_
  13. #include "common.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define HASH_DATAIN_BLOCK_SIZE (64)
  18. #define SHA1_DIGEST_OUT_SIZE (20)
  19. #define SHA224_DIGEST_OUT_SIZE (28)
  20. #define SHA256_DIGEST_OUT_SIZE (32)
  21. #define SHA384_DIGEST_OUT_SIZE (48)
  22. #define SHA512_DIGEST_OUT_SIZE (64)
  23. #define MD5_DIGEST_OUT_SIZE (16)
  24. #define CSI_SHA256_MODE (0x00000008)
  25. #define CSI_SHA224_MODE (0x00000010)
  26. #define CSI_SHA384_MODE (0x00000040)
  27. #define CSI_SHA512_MODE (0x00000020)
  28. #define CSI_MD5_MODE (0x00000002)
  29. #define CSI_SHA1_MODE (0x00000004)
  30. #define CSI_SHA256_NEW_MODE (0x00000009)
  31. #define CSI_SHA224_MEW_MODE (0x00000011)
  32. #define CSI_SHA384_NEW_MODE (0x00000041)
  33. #define CSI_SHA512_NEW_MODE (0x00000021)
  34. #define CSI_MD5_NEW_MODE (0x00000003)
  35. #define CSI_SHA1_NEW_MODE (0x00000005)
  36. /****** SHA mode ******/
  37. typedef enum {
  38. SHA_MODE_SHA1 = 1U, ///< SHA_1 mode
  39. SHA_MODE_256, ///< SHA_256 mode
  40. SHA_MODE_224, ///< SHA_224 mode
  41. SHA_MODE_512, ///< SHA_512 mode
  42. SHA_MODE_384, ///< SHA_384 mode
  43. SHA_MODE_512_256, ///< SHA_512_256 mode
  44. SHA_MODE_512_224, ///< SHA_512_224 mode
  45. SHA_MODE_MD5 ///< MD5 mode
  46. } csi_sha_mode_t;
  47. /****** SHA State ******/
  48. typedef struct {
  49. uint32_t busy : 1; ///< Calculate busy flag
  50. uint32_t error : 1; ///< Calculate error flag
  51. } csi_sha_state_t;
  52. typedef struct {
  53. csi_sha_mode_t mode; ///< SHA mode
  54. uint32_t total[2]; ///< Number of bytes processed
  55. uint32_t state[16]; ///< Intermediate digest state
  56. uint8_t buffer[128]; ///< Data block being processed
  57. uint8_t result[64]; ///< Data block has processed
  58. uint32_t process_len;
  59. uint32_t digest_len;
  60. } csi_sha_context_t;
  61. /****** SHA Event ******/
  62. typedef enum {
  63. SHA_EVENT_COMPLETE = 0U, ///< Calculate completed
  64. SHA_EVENT_UPDATE,
  65. SHA_EVENT_START,
  66. SHA_EVENT_ERROR ///< Calculate error
  67. } csi_sha_event_t;
  68. typedef struct csi_sha csi_sha_t;
  69. struct csi_sha {
  70. csi_dev_t dev; ///< SHA hw-device info
  71. void (*callback)(csi_sha_t *sha, csi_sha_event_t event, void *arg); ///< SHA event callback for user
  72. void *arg; ///< SHA custom designed param passed to evt_cb
  73. csi_sha_state_t state; ///< SHA state
  74. void *priv;
  75. };
  76. /**
  77. \brief Initialize SHA Interface. Initializes the resources needed for the SHA interface
  78. \param[in] sha Operate handle
  79. \param[in] idx Index of SHA
  80. \return Error code \ref csi_error_t
  81. */
  82. csi_error_t csi_sha_init(csi_sha_t *sha, uint32_t idx);
  83. /**
  84. \brief De-initialize SHA Interface. Stops operation and releases the software resources used by the interface
  85. \param[in] sha SHA handle to operate
  86. \return None
  87. */
  88. void csi_sha_uninit(csi_sha_t *sha);
  89. /**
  90. \brief Attach the callback handler to SHA
  91. \param[in] sha Handle to operate
  92. \param[in] callback Callback function
  93. \param[in] arg Callback's param
  94. \return Error code \ref csi_error_t
  95. */
  96. csi_error_t csi_sha_attach_callback(csi_sha_t *sha, void *callback, void *arg);
  97. /**
  98. \brief Detach the callback handler
  99. \param[in] sha Handle to operate
  100. \return None
  101. */
  102. void csi_sha_detach_callback(csi_sha_t *sha);
  103. /**
  104. \brief Start the engine
  105. \param[in] sha Handle to operate
  106. \param[in] context Pointer to the SHA context \ref csi_sha_context_t
  107. \param[in] mode SHA mode \ref csi_sha_mode_t
  108. \return Error code \ref csi_error_t
  109. */
  110. csi_error_t csi_sha_start(csi_sha_t *sha, csi_sha_context_t *context, csi_sha_mode_t mode);
  111. /**
  112. \brief Update the engine
  113. \param[in] sha Handle to operate
  114. \param[in] context Pointer to the SHA context \ref csi_sha_context_t
  115. \param[in] input Pointer to the Source data
  116. \param[in] size The data size
  117. \return Error code \ref csi_error_t
  118. */
  119. csi_error_t csi_sha_update(csi_sha_t *sha, csi_sha_context_t *context, const void *input, uint32_t size);
  120. /**
  121. \brief Accumulate the engine (async mode)
  122. \param[in] sha Handle to operate
  123. \param[in] context Pointer to the SHA context \ref csi_sha_context_t
  124. \param[in] input Pointer to the Source data
  125. \param[in] size The data size
  126. \return Error code \ref csi_error_t
  127. */
  128. csi_error_t csi_sha_update_async(csi_sha_t *sha, csi_sha_context_t *context, const void *input, uint32_t size);
  129. /**
  130. \brief Finish the engine
  131. \param[in] sha Handle to operate
  132. \param[in] context Pointer to the SHA context \ref csi_sha_context_t
  133. \param[out] output Pointer to the result data
  134. \param[out] out_size Pointer to the result data size(bytes)
  135. \return Error code \ref csi_error_t
  136. */
  137. csi_error_t csi_sha_finish(csi_sha_t *sha, csi_sha_context_t *context, void *output, uint32_t *out_size);
  138. /**
  139. \brief Get SHA state
  140. \param[in] sha Handle to operate
  141. \param[out] state SHA state \ref csi_sha_state_t
  142. \return Error code \ref csi_error_t
  143. */
  144. csi_error_t csi_sha_get_state(csi_sha_t *sha, csi_sha_state_t *state);
  145. /**
  146. \brief Enable SHA power manage
  147. \param[in] sha Handle to operate
  148. \return Error code \ref csi_error_t
  149. */
  150. csi_error_t csi_sha_enable_pm(csi_sha_t *sha);
  151. /**
  152. \brief Disable SHA power manage
  153. \param[in] sha Handle to operate
  154. \return None
  155. */
  156. void csi_sha_disable_pm(csi_sha_t *sha);
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif /* _DRV_SHA_H_ */