sm3.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file drv/sm3.h
  6. * @brief Header File for SM3 Driver
  7. * @version V2.0
  8. * @date 9. DEC 2020
  9. * @model SM3
  10. ******************************************************************************/
  11. #ifndef _DRV_SM3_H_
  12. #define _DRV_SM3_H_
  13. #include <stdint.h>
  14. #include "common.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define SM3_DATAIN_BLOCK_SIZE (64)
  19. #define SM3_DIGEST_OUT_SIZE (32)
  20. typedef struct {
  21. uint32_t total[2]; ///< Number of bytes processed
  22. uint32_t state[16]; ///< Intermediate digest state
  23. uint8_t buffer[SM3_DATAIN_BLOCK_SIZE]; ///< Data block beingprocessed
  24. uint8_t result[SM3_DIGEST_OUT_SIZE]; ///< Data block has processed
  25. } csi_sm3_context_t;
  26. /****** SM3 State ******/
  27. typedef struct {
  28. uint32_t busy : 1; ///< Calculate busy flag
  29. uint32_t error : 1; ///< Calculate error flag
  30. } csi_sm3_state_t;
  31. /****** SM3 Event ******/
  32. typedef enum {
  33. SM3_EVENT_COMPLETE = 0U, ///< Calculate completed
  34. SM3_EVENT_UPDATE,
  35. SM3_EVENT_START,
  36. SM3_EVENT_ERROR ///< Calculate error
  37. } csi_sm3_event_t;
  38. typedef struct csi_sm3_t csi_sm3_t;
  39. struct csi_sm3_t {
  40. csi_dev_t dev; ///< SM3 hw-device info
  41. void (*callback)(csi_sm3_t *sm3, csi_sm3_event_t event,
  42. void *arg); ///< SM3 event callback for user
  43. void * arg; ///< SM3 custom designed param passed to evt_cb
  44. csi_sm3_state_t state; ///< SM3 state
  45. void * priv;
  46. };
  47. // Function documentation
  48. /**
  49. \brief Initialize SM3 Interface. Initializes the resources needed for the SM3 interface
  50. \param[in] sm3 operate handle.
  51. \param[in] idx index of sm3
  52. \return error code \ref csi_error_t
  53. */
  54. csi_error_t csi_sm3_init(csi_sm3_t *sm3, uint32_t idx);
  55. /**
  56. \brief De-initialize SM3 Interface. stops operation and releases the software resources used by the interface
  57. \param[in] sm3 sm3 handle to operate.
  58. \return none
  59. */
  60. void csi_sm3_uninit(csi_sm3_t *sm3);
  61. /**
  62. \brief Attach the callback handler to SM3
  63. \param[in] sm3 Handle to operate
  64. \param[in] callback Callback function
  65. \param[in] arg Callback's param
  66. \return Error code \ref csi_error_t
  67. */
  68. csi_error_t csi_sm3_attach_callback(csi_sm3_t *sm3, void *callback, void *arg);
  69. /**
  70. \brief Detach the callback handler
  71. \param[in] sm3 Handle to operate
  72. \return None
  73. */
  74. void csi_sm3_detach_callback(csi_sm3_t *sm3);
  75. /**
  76. \brief start the engine
  77. \param[in] sm3 sm3 handle to .operate
  78. \param[in] context Pointer to the sm3 context \ref csi_sm3_context_t
  79. \return error code \ref csi_error_t
  80. */
  81. csi_error_t csi_sm3_start(csi_sm3_t *sm3, csi_sm3_context_t *context);
  82. /**
  83. \brief update the engine
  84. \param[in] sm3 sm3 handle to operate.
  85. \param[in] context Pointer to the sm3 context \ref csi_sm3_context_t
  86. \param[in] input Pointer to the Source data
  87. \param[in] size the data size
  88. \return error code \ref csi_error_t
  89. */
  90. csi_error_t csi_sm3_update(csi_sm3_t *sm3, csi_sm3_context_t *context, const uint8_t *input, uint32_t size);
  91. /**
  92. \brief Accumulate the engine (async mode)
  93. \param[in] sm3 Handle to operate
  94. \param[in] context Pointer to the SM3 context \ref csi_sm3_context_t
  95. \param[in] input Pointer to the Source data
  96. \param[in] size The data size
  97. \return Error code \ref csi_error_t
  98. */
  99. csi_error_t csi_sm3_update_async(csi_sm3_t *sm3, csi_sm3_context_t *context, const uint8_t *input, uint32_t size);
  100. /**
  101. \brief finish the engine
  102. \param[in] sm3 sm3 handle to operate.
  103. \param[in] context Pointer to the sm3 context \ref csi_sm3_context_t
  104. \param[out] output Pointer to the result data
  105. \param[out] out_size Pointer to the result data size(bytes)
  106. \return error code \ref csi_error_t
  107. */
  108. csi_error_t csi_sm3_finish(csi_sm3_t *sm3, csi_sm3_context_t *context, uint8_t *output, uint32_t *out_size);
  109. /**
  110. \brief Get SM3 state
  111. \param[in] sm3 Handle to operate
  112. \param[out] state SM3 state \ref csi_sm3_state_t
  113. \return Error code \ref csi_error_t
  114. */
  115. csi_error_t csi_sm3_get_state(csi_sm3_t *sm3, csi_sm3_state_t *state);
  116. /**
  117. \brief Enable SM3 power manage
  118. \param[in] sm3 Handle to operate
  119. \return Error code \ref csi_error_t
  120. */
  121. csi_error_t csi_sm3_enable_pm(csi_sm3_t *sm3);
  122. /**
  123. \brief Disable SM3 power manage
  124. \param[in] sm3 Handle to operate
  125. \return None
  126. */
  127. void csi_sm3_disable_pm(csi_sm3_t *sm3);
  128. #ifdef __cplusplus
  129. extern "C" {
  130. #endif
  131. #endif //_DRV_SM3_H