sec_crypto_sha.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file seccrypt_sha.h
  6. * @brief Header File for SHA
  7. * @version V1.0
  8. * @date 20. Jul 2020
  9. * @model sha
  10. ******************************************************************************/
  11. #ifndef _SC_SHA_H_
  12. #define _SC_SHA_H_
  13. #include "sec_include_config.h"
  14. #include <stdint.h>
  15. #ifdef CONFIG_SYSTEM_SECURE
  16. #ifdef SEC_LIB_VERSION
  17. #include "drv/sha.h"
  18. #else
  19. #include "sha.h"
  20. #endif
  21. #include "soc.h"
  22. #endif
  23. #ifdef CONFIG_SEC_CRYPTO_SM3
  24. #ifdef SEC_LIB_VERSION
  25. #include "drv/sm3.h"
  26. #else
  27. #include "sm3.h"
  28. #endif
  29. #endif
  30. #include "sec_crypto_errcode.h"
  31. #ifdef CONFIG_SEC_CRYPTO_SHA_SW
  32. #include "crypto_sha1.h"
  33. #include "crypto_sha256.h"
  34. #endif
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /*----- SHA Control Codes: Mode -----*/
  39. typedef enum {
  40. SC_SHA_MODE_SHA1 = 1U, ///< SHA_1 mode
  41. SC_SHA_MODE_256, ///< SHA_256 mode
  42. SC_SHA_MODE_224, ///< SHA_224 mode
  43. SC_SHA_MODE_512, ///< SHA_512 mode
  44. SC_SHA_MODE_384, ///< SHA_384 mode
  45. SC_SHA_MODE_512_256, ///< SHA_512_256 mode
  46. SC_SHA_MODE_512_224, ///< SHA_512_224 mode
  47. SC_SHA_MODE_MD5, ///< MD5 mode
  48. SC_SM3_MODE,
  49. } sc_sha_mode_t;
  50. /**
  51. \brief SHA State
  52. */
  53. typedef struct {
  54. uint32_t busy : 1; ///< calculate busy flag
  55. uint32_t error : 1; ///< calculate error flag
  56. } sc_sha_state_t;
  57. typedef struct {
  58. #ifdef CONFIG_SYSTEM_SECURE
  59. #ifdef CONFIG_CSI_V1
  60. uint8_t ctx[SHA_CONTEXT_SIZE];
  61. #endif /* CONFIG_CSI_V1 */
  62. #ifdef CONFIG_CSI_V2
  63. csi_sha_context_t ctx;
  64. csi_sm3_context_t sm3ctx;
  65. csi_sha_state_t state;
  66. csi_sm3_state_t sm3state;
  67. #endif
  68. #endif
  69. #if defined(CONFIG_TEE_CA)
  70. uint8_t ctx[224+8];
  71. #endif
  72. #if defined(CONFIG_SEC_CRYPTO_SHA_SW)
  73. sc_mbedtls_sha1_context sha1_ctx;
  74. sc_mbedtls_sha256_context sha2_ctx;
  75. #endif
  76. sc_sha_mode_t mode; ///< sha mode
  77. } sc_sha_context_t;
  78. /****** SHA Event *****/
  79. typedef enum {
  80. SC_SHA_EVENT_COMPLETE = 0U, ///< calculate completed
  81. SC_SHA_EVENT_ERROR ///< calculate error
  82. } sc_sha_event_t;
  83. typedef struct sc_sha {
  84. #ifdef CONFIG_SYSTEM_SECURE
  85. #ifdef CONFIG_CSI_V1
  86. sha_handle_t handle;
  87. sc_sha_context_t ctx;
  88. sc_sha_mode_t mode; ///< sha mode
  89. #endif /* CONFIG_CSI_V1 */
  90. #ifdef CONFIG_CSI_V2
  91. csi_sha_t csi_sha;
  92. csi_sm3_t csi_sm3;
  93. #endif
  94. #endif
  95. #if defined(CONFIG_TEE_CA)
  96. #endif
  97. } sc_sha_t;
  98. // Function documentation
  99. /**
  100. \brief Initialize SHA Interface. Initializes the resources needed for the SHA interface
  101. \param[in] sha operate handle.
  102. \param[in] idx index of sha
  103. \return error code \ref uint32_t
  104. */
  105. uint32_t sc_sha_init(sc_sha_t *sha, uint32_t idx);
  106. /**
  107. \brief De-initialize SHA Interface. stops operation and releases the software resources used by the interface
  108. \param[in] sha sha handle to operate.
  109. \return none
  110. */
  111. void sc_sha_uninit(sc_sha_t *sha);
  112. /**
  113. \brief attach the callback handler to SHA
  114. \param[in] sha operate handle.
  115. \param[in] callback callback function
  116. \param[in] arg callback's param
  117. \return error code
  118. */
  119. uint32_t sc_sha_attach_callback(sc_sha_t *sha, void *callback, void *arg);
  120. /**
  121. \brief detach the callback handler
  122. \param[in] sha operate handle.
  123. */
  124. void sc_sha_detach_callback(sc_sha_t *sha);
  125. /**
  126. \brief start the engine
  127. \param[in] sha sha handle to operate.
  128. \param[in] context Pointer to the sha context \ref sc_sha_context_t
  129. \param[in] mode sha mode \ref sc_sha_mode_t
  130. \return error code \ref uint32_t
  131. */
  132. uint32_t sc_sha_start(sc_sha_t *sha, sc_sha_context_t *context, sc_sha_mode_t mode);
  133. /**
  134. \brief update the engine
  135. \param[in] sha sha handle to operate.
  136. \param[in] context Pointer to the sha context \ref sc_sha_context_t
  137. \param[in] input Pointer to the Source data
  138. \param[in] size the data size
  139. \return error code \ref uint32_t
  140. */
  141. uint32_t sc_sha_update(sc_sha_t *sha, sc_sha_context_t *context, const void *input, uint32_t size);
  142. /**
  143. \brief accumulate the engine (async mode)
  144. \param[in] sha sha handle to operate.
  145. \param[in] context Pointer to the sha context \ref sc_sha_context_t
  146. \param[in] input Pointer to the Source data
  147. \param[in] size the data size
  148. \return error code \ref uint32_t
  149. */
  150. uint32_t sc_sha_update_async(sc_sha_t *sha, sc_sha_context_t *context, const void *input, uint32_t size);
  151. /**
  152. \brief finish the engine
  153. \param[in] sha sha handle to operate.
  154. \param[in] context Pointer to the sha context \ref sc_sha_context_t
  155. \param[out] output Pointer to the result data
  156. \param[out] out_size Pointer to the result data size(bytes)
  157. \return error code \ref uint32_t
  158. */
  159. uint32_t sc_sha_finish(sc_sha_t *sha, sc_sha_context_t *context, void *output, uint32_t *out_size);
  160. /**
  161. \brief calculate the digest
  162. \param[in] sha sha handle to operate.
  163. \param[in] idx index of sha
  164. \param[in] context Pointer to the sha context \ref sc_sha_context_t
  165. \param[in] mode sha mode \ref sc_sha_mode_t
  166. \param[in] input Pointer to the Source data
  167. \param[in] size the data size
  168. \param[out] output Pointer to the result data
  169. \param[out] out_size Pointer to the result data size(bytes)
  170. \return error code \ref uint32_t
  171. */
  172. uint32_t sc_sha_digest(sc_sha_t *sha, uint32_t idx, sc_sha_context_t *context, sc_sha_mode_t mode,
  173. const void *input, uint32_t size, void *output, uint32_t out_size);
  174. /**
  175. \brief finish the engine
  176. \param[in] sha sha handle to operate.
  177. \param[in] context Pointer to the sha context \ref sc_sha_context_t
  178. \return error code \ref uint32_t
  179. */
  180. uint32_t sc_sha_get_state(sc_sha_t *sha,sc_sha_context_t *context);
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. #endif /* _sc_SHA_H_ */