sha.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /****** SHA mode ******/
  25. typedef enum {
  26. SHA_MODE_SHA1 = 1U, ///< SHA_1 mode
  27. SHA_MODE_256, ///< SHA_256 mode
  28. SHA_MODE_224, ///< SHA_224 mode
  29. SHA_MODE_512, ///< SHA_512 mode
  30. SHA_MODE_384, ///< SHA_384 mode
  31. SHA_MODE_512_256, ///< SHA_512_256 mode
  32. SHA_MODE_512_224, ///< SHA_512_224 mode
  33. SHA_MODE_MD5 ///< MD5 mode
  34. } csi_sha_mode_t;
  35. /****** SHA State ******/
  36. typedef struct {
  37. uint32_t busy : 1; ///< Calculate busy flag
  38. uint32_t error : 1; ///< Calculate error flag
  39. } csi_sha_state_t;
  40. typedef struct {
  41. csi_sha_mode_t mode; ///< SHA mode
  42. uint32_t total[2]; ///< Number of bytes processed
  43. uint32_t state[16]; ///< Intermediate digest state
  44. uint8_t buffer[128]; ///< Data block being processed
  45. uint8_t result[64]; ///< Data block has processed
  46. uint32_t process_len;
  47. uint32_t digest_len;
  48. } csi_sha_context_t;
  49. /****** SHA Event ******/
  50. typedef enum {
  51. SHA_EVENT_COMPLETE = 0U, ///< Calculate completed
  52. SHA_EVENT_UPDATE,
  53. SHA_EVENT_START,
  54. SHA_EVENT_ERROR ///< Calculate error
  55. } csi_sha_event_t;
  56. typedef struct csi_sha csi_sha_t;
  57. struct csi_sha {
  58. csi_dev_t dev; ///< SHA hw-device info
  59. void (*callback)(csi_sha_t *sha, csi_sha_event_t event, void *arg); ///< SHA event callback for user
  60. void *arg; ///< SHA custom designed param passed to evt_cb
  61. csi_sha_state_t state; ///< SHA state
  62. void *priv;
  63. };
  64. /**
  65. \brief Initialize SHA Interface. Initializes the resources needed for the SHA interface
  66. \param[in] sha Operate handle
  67. \param[in] idx Index of SHA
  68. \return Error code \ref csi_error_t
  69. */
  70. csi_error_t csi_sha_init(csi_sha_t *sha, uint32_t idx);
  71. /**
  72. \brief De-initialize SHA Interface. Stops operation and releases the software resources used by the interface
  73. \param[in] sha SHA handle to operate
  74. \return None
  75. */
  76. void csi_sha_uninit(csi_sha_t *sha);
  77. /**
  78. \brief Attach the callback handler to SHA
  79. \param[in] sha Handle to operate
  80. \param[in] callback Callback function
  81. \param[in] arg Callback's param
  82. \return Error code \ref csi_error_t
  83. */
  84. csi_error_t csi_sha_attach_callback(csi_sha_t *sha, void *callback, void *arg);
  85. /**
  86. \brief Detach the callback handler
  87. \param[in] sha Handle to operate
  88. \return None
  89. */
  90. void csi_sha_detach_callback(csi_sha_t *sha);
  91. /**
  92. \brief Start the engine
  93. \param[in] sha Handle to operate
  94. \param[in] context Pointer to the SHA context \ref csi_sha_context_t
  95. \param[in] mode SHA mode \ref csi_sha_mode_t
  96. \return Error code \ref csi_error_t
  97. */
  98. csi_error_t csi_sha_start(csi_sha_t *sha, csi_sha_context_t *context, csi_sha_mode_t mode);
  99. /**
  100. \brief Update the engine
  101. \param[in] sha Handle to operate
  102. \param[in] context Pointer to the SHA context \ref csi_sha_context_t
  103. \param[in] input Pointer to the Source data
  104. \param[in] size The data size
  105. \return Error code \ref csi_error_t
  106. */
  107. csi_error_t csi_sha_update(csi_sha_t *sha, csi_sha_context_t *context, const void *input, uint32_t size);
  108. /**
  109. \brief Accumulate the engine (async mode)
  110. \param[in] sha Handle to operate
  111. \param[in] context Pointer to the SHA context \ref csi_sha_context_t
  112. \param[in] input Pointer to the Source data
  113. \param[in] size The data size
  114. \return Error code \ref csi_error_t
  115. */
  116. csi_error_t csi_sha_update_async(csi_sha_t *sha, csi_sha_context_t *context, const void *input, uint32_t size);
  117. /**
  118. \brief Finish the engine
  119. \param[in] sha Handle to operate
  120. \param[in] context Pointer to the SHA context \ref csi_sha_context_t
  121. \param[out] output Pointer to the result data
  122. \param[out] out_size Pointer to the result data size(bytes)
  123. \return Error code \ref csi_error_t
  124. */
  125. csi_error_t csi_sha_finish(csi_sha_t *sha, csi_sha_context_t *context, void *output, uint32_t *out_size);
  126. /**
  127. \brief Get SHA state
  128. \param[in] sha Handle to operate
  129. \param[out] state SHA state \ref csi_sha_state_t
  130. \return Error code \ref csi_error_t
  131. */
  132. csi_error_t csi_sha_get_state(csi_sha_t *sha, csi_sha_state_t *state);
  133. /**
  134. \brief Enable SHA power manage
  135. \param[in] sha Handle to operate
  136. \return Error code \ref csi_error_t
  137. */
  138. csi_error_t csi_sha_enable_pm(csi_sha_t *sha);
  139. /**
  140. \brief Disable SHA power manage
  141. \param[in] sha Handle to operate
  142. \return None
  143. */
  144. void csi_sha_disable_pm(csi_sha_t *sha);
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif /* _DRV_SHA_H_ */