sec_crypto_sm4.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file sec_crypt_sm4.h
  6. * @brief Header File for SM4
  7. * @version V1.0
  8. * @date 20. Jul 2021
  9. * @model sm4
  10. ******************************************************************************/
  11. #ifndef _SC_SM4_H_
  12. #define _SC_SM4_H_
  13. #include "sec_include_config.h"
  14. #ifdef CONFIG_CSI_V2
  15. #ifdef SEC_LIB_VERSION
  16. #include "drv/sm4.h"
  17. #else
  18. #include "sm4.h"
  19. #endif
  20. #endif
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. \brief SM4 Ctrl Block
  26. */
  27. typedef struct {
  28. #ifdef CONFIG_CSI_V2
  29. csi_sm4_t sm4;
  30. #endif
  31. } sc_sm4_t;
  32. // Function documentation
  33. /**
  34. \brief Initialize sm4 Interface. Initializes the resources needed for
  35. the sm4 interface \param[in] sm4 operate handle \param[in] idx device
  36. id \return error code \ref uint32_t
  37. */
  38. uint32_t sc_sm4_init(sc_sm4_t *sm4, uint32_t idx);
  39. /**
  40. \brief De-initialize sm4 Interface. stops operation and releases the
  41. software resources used by the interface \param[in] sm4 handle to operate
  42. \return None
  43. */
  44. void sc_sm4_uninit(sc_sm4_t *sm4);
  45. /**
  46. \brief Set encrypt key
  47. \param[in] sm4 handle to operate
  48. \param[in] key Pointer to the key buf
  49. \return error code \ref uint32_t
  50. */
  51. uint32_t sc_sm4_set_encrypt_key(sc_sm4_t *sm4, uint8_t *key, csi_sm4_key_bits_t key_len);
  52. /**
  53. \brief Set decrypt key
  54. \param[in] sm4 handle to operate
  55. \param[in] key Pointer to the key buf
  56. \return error code \ref uint32_t
  57. */
  58. uint32_t sc_sm4_set_decrypt_key(sc_sm4_t *sm4, uint8_t *key, csi_sm4_key_bits_t key_len);
  59. /**
  60. \brief sm4 ecb encrypt
  61. \param[in] sm4 handle to operate
  62. \param[in] in Pointer to the Source data
  63. \param[out] out Pointer to the Result data
  64. \param[in] size the Source data size
  65. \return error code \ref uint32_t
  66. */
  67. uint32_t sc_sm4_ecb_encrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  68. uint32_t size);
  69. /**
  70. \brief sm4 ecb decrypt
  71. \param[in] sm4 handle to operate
  72. \param[in] in Pointer to the Source data
  73. \param[out] out Pointer to the Result data
  74. \param[in] size the Source data size
  75. \return error code \ref uint32_t
  76. */
  77. uint32_t sc_sm4_ecb_decrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  78. uint32_t size);
  79. /**
  80. \brief sm4 cbc encrypt
  81. \param[in] sm4 handle to operate
  82. \param[in] in Pointer to the Source data
  83. \param[out] out Pointer to the Result data
  84. \param[in] size the Source data size
  85. \param[in] iv init vector
  86. \return error code \ref uint32_t
  87. */
  88. uint32_t sc_sm4_cbc_encrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  89. uint32_t size, uint8_t *iv);
  90. /**
  91. \brief sm4 cbc decrypt
  92. \param[in] sm4 handle to operate
  93. \param[in] in Pointer to the Source data
  94. \param[out] out Pointer to the Result data
  95. \param[in] size the Source data size
  96. \param[in] iv init vector
  97. \return error code \ref uint32_t
  98. */
  99. uint32_t sc_sm4_cbc_decrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  100. uint32_t size, uint8_t *iv);
  101. /**
  102. \brief sm4 cfb1 encrypt
  103. \param[in] sm4 handle to operate
  104. \param[in] in Pointer to the Source data
  105. \param[out] out Pointer to the Result data
  106. \param[in] size the Source data size
  107. \param[in] iv init vector
  108. \return error code \ref uint32_t
  109. */
  110. uint32_t sc_sm4_cfb1_encrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  111. uint32_t size, uint8_t *iv);
  112. /**
  113. \brief sm4 cfb1 decrypt
  114. \param[in] sm4 handle to operate
  115. \param[in] in Pointer to the Source data
  116. \param[out] out Pointer to the Result data
  117. \param[in] size the Source data size
  118. \param[in] iv init vector
  119. \return error code \ref uint32_t
  120. */
  121. uint32_t sc_sm4_cfb1_decrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  122. uint32_t size, uint8_t *iv);
  123. /**
  124. \brief sm4 cfb8 encrypt
  125. \param[in] sm4 handle to operate
  126. \param[in] in Pointer to the Source data
  127. \param[out] out Pointer to the Result data
  128. \param[in] size the Source data size
  129. \param[in] iv init vector
  130. \return error code \ref uint32_t
  131. */
  132. uint32_t sc_sm4_cfb8_encrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  133. uint32_t size, uint8_t *iv);
  134. /**
  135. \brief sm4 cfb8 decrypt
  136. \param[in] sm4 handle to operate
  137. \param[in] in Pointer to the Source data
  138. \param[out] out Pointer to the Result data
  139. \param[in] size the Source data size
  140. \param[in] iv init vector
  141. \return error code \ref uint32_t
  142. */
  143. uint32_t sc_sm4_cfb8_decrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  144. uint32_t size, uint8_t *iv);
  145. /**
  146. \brief sm4 cfb128 decrypt
  147. \param[in] sm4 handle to operate
  148. \param[in] in Pointer to the Source data
  149. \param[out] out Pointer to the Result data
  150. \param[in] size the Source data size
  151. \param[in] iv init vector
  152. \param[out] num the number of the 128-bit block we have used
  153. \return error code \ref uint32_t
  154. */
  155. uint32_t sc_sm4_cfb128_decrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  156. uint32_t size, uint8_t *iv, uint32_t *num);
  157. /**
  158. \brief sm4 cfb128 encrypt
  159. \param[in] sm4 handle to operate
  160. \param[in] in Pointer to the Source data
  161. \param[out] out Pointer to the Result data
  162. \param[in] size the Source data size
  163. \param[in] iv init vector
  164. \param[out] num the number of the 128-bit block we have used
  165. \return error code \ref uint32_t
  166. */
  167. uint32_t sc_sm4_cfb128_encrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  168. uint32_t size, uint8_t *iv, uint32_t *num);
  169. /**
  170. \brief sm4 ofb encrypt
  171. \param[in] sm4 handle to operate
  172. \param[in] in Pointer to the Source data
  173. \param[out] out Pointer to the Result data
  174. \param[in] size the Source data size
  175. \param[in] iv init vector
  176. \param[out] num the number of the 128-bit block we have used
  177. \return error code \ref uint32_t
  178. */
  179. uint32_t sc_sm4_ofb_encrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  180. uint32_t size, uint8_t *iv, uint32_t *num);
  181. /**
  182. \brief sm4 ofb encrypt
  183. \param[in] sm4 handle to operate
  184. \param[in] in Pointer to the Source data
  185. \param[out] out Pointer to the Result data
  186. \param[in] size the Source data size
  187. \param[in] iv init vector
  188. \param[out] num the number of the 128-bit block we have used
  189. \return error code \ref uint32_t
  190. */
  191. uint32_t sc_sm4_ofb_decrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  192. uint32_t size, uint8_t *iv, uint32_t *num);
  193. /**
  194. \brief sm4 ctr encrypt
  195. \param[in] sm4 handle to operate
  196. \param[in] in Pointer to the Source data
  197. \param[out] out Pointer to the Result data
  198. \param[in] size the Source data size
  199. \param[in] nonce_counter counter
  200. \return error code \ref uint32_t
  201. */
  202. uint32_t sc_sm4_ctr_encrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  203. uint32_t size, uint8_t nonce_counter[16]);
  204. /**
  205. \brief sm4 ctr encrypt
  206. \param[in] sm4 handle to operate
  207. \param[in] in Pointer to the Source data
  208. \param[out] out Pointer to the Result data
  209. \param[in] size the Source data size
  210. \param[in] nonce_counter counter
  211. \return error code \ref uint32_t
  212. */
  213. uint32_t sc_sm4_ctr_decrypt(sc_sm4_t *sm4, uint8_t *in, uint8_t *out,
  214. uint32_t size, uint8_t nonce_counter[16]);
  215. #ifdef __cplusplus
  216. extern "C" {
  217. #endif
  218. #endif /* _SC_SM4_H_ */