sec_crypto_sm2.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file sec_crypt_sm2.h
  6. * @brief Header File for SM2
  7. * @version V1.0
  8. * @date 20. Jul 2021
  9. * @model sm2
  10. ******************************************************************************/
  11. #ifndef _SC_SM2_H_
  12. #define _SC_SM2_H_
  13. #include "sec_include_config.h"
  14. #ifdef CONFIG_SEC_CRYPTO_SM2
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #ifdef SEC_LIB_VERSION
  19. #include "drv/sm2.h"
  20. #else
  21. #include "sm2.h"
  22. #endif
  23. typedef struct {
  24. uint32_t sm2_curve : 1; ///< supports 256bits curve
  25. } sc_sm2_capabilities_t;
  26. /**
  27. \brief SM2 ciphertext order
  28. */
  29. typedef enum {
  30. SC_SM2_C1C3C2 = 0,
  31. SC_SM2_C1C2C3,
  32. } sc_sm2_cipher_order_e;
  33. typedef enum {
  34. SC_SM2_ENDIAN_LITTLE = 0, ///< Little Endian
  35. SC_SM2_ENDIAN_BIG ///< Big Endian
  36. } sc_sm2_endian_mode_e;
  37. /**
  38. \brief SM2 status
  39. */
  40. typedef struct {
  41. uint32_t busy : 1; ///< Calculate busy flag
  42. } sc_sm2_state_t;
  43. /**
  44. \brief SM2 key exchange role
  45. */
  46. typedef enum { SC_SM2_Role_Sponsor = 0, SC_SM2_Role_Responsor } sc_sm2_exchange_role_e;
  47. /****** SM2 Event *****/
  48. typedef enum {
  49. SC_SM2_EVENT_MAKE_KEY_COMPLETE = 0, ///< Make key completed
  50. SC_SM2_EVENT_ENCRYPT_COMPLETE, ///< Encrypt completed
  51. SC_SM2_EVENT_DECRYPT_COMPLETE, ///< Decrypt completed
  52. SC_SM2_EVENT_SIGN_COMPLETE, ///< Sign completed
  53. SC_SM2_EVENT_VERIFY_COMPLETE, ///< Verify completed
  54. SC_SM2_EVENT_EXCHANGE_KEY_COMPLETE, ///< Exchange key completed
  55. } sc_sm2_event_e;
  56. typedef struct {
  57. #ifdef CONFIG_CSI_V2
  58. csi_sm2_t sm2;
  59. #endif
  60. } sc_sm2_t;
  61. ///< Pointer to \ref sc_sm2_callback_t : SM2 Event call back.
  62. typedef void (*sc_sm2_callback_t)(sc_sm2_event_e event);
  63. /**
  64. \brief Initialize SM2.
  65. \param[in] sm2 sm2 handle to operate.
  66. \param[in] idx device id
  67. \return Error code \ref uint32_t
  68. */
  69. uint32_t sc_sm2_init(sc_sm2_t *sm2, uint32_t idx);
  70. /**
  71. \brief De-initialize SM2 Interface. stops operation and releases the
  72. software resources used by the interface \param[in] sm2 sm2 handle to
  73. operate. \return none
  74. */
  75. void sc_sm2_uninit(sc_sm2_t *sm2);
  76. /**
  77. \brief sm2 get capability.
  78. \param[in] sm2 Operate handle.
  79. \return Error code \ref uint32_t
  80. */
  81. uint32_t sc_sm2_config(sc_sm2_t *sm2, sc_sm2_cipher_order_e co, sc_sm2_endian_mode_e endian);
  82. /**
  83. \brief Attach the callback handler to SM2
  84. \param[in] sm2 Operate handle.
  85. \param[in] cb Callback function
  86. \param[in] arg User can define it by himself as callback's param
  87. \return Error code \ref uint32_t
  88. */
  89. uint32_t sc_sm2_attach_callback(sc_sm2_t *sm2, sc_sm2_callback_t cb, void *arg);
  90. /**
  91. \brief Detach the callback handler
  92. \param[in] sm2 Operate handle.
  93. */
  94. void sc_sm2_detach_callback(sc_sm2_t *sm2);
  95. /**
  96. \brief sm2 get capability.
  97. \param[in] sm2 Operate handle.
  98. \param[out] cap Pointer of sc_sm2_capabilities_t.
  99. \return Error code \ref uint32_t
  100. */
  101. uint32_t sc_sm2_get_capabilities(sc_sm2_t *sm2, sc_sm2_capabilities_t *cap);
  102. /**
  103. \brief check whether the public key and private key are a pair.
  104. \param[in] sm2 sm2 handle to operate.
  105. \param[in] private Pointer to the sm2 private key, alloc by caller.
  106. \param[in] public Pointer to the sm2 public key, alloc by caller.
  107. \return Error code \ref uint32_t
  108. */
  109. uint32_t sc_sm2_check_keypair(sc_sm2_t *sm2, uint8_t pubkey[65], uint8_t prikey[32]);
  110. /**
  111. \brief generate sm2 key.
  112. \param[in] sm2 sm2 handle to operate.
  113. \param[out] private Pointer to the sm2 private key, alloc by caller.
  114. \param[out] public Pointer to the sm2 public key, alloc by caller.
  115. \return Error code \ref uint32_t
  116. */
  117. uint32_t sc_sm2_gen_key(sc_sm2_t *sm2, uint8_t pubkey[65], uint8_t prikey[32]);
  118. /**
  119. \brief sm2 sign
  120. \param[in] sm2 sm2 handle to operate.
  121. \param[in] d Pointer to the digest.
  122. \param[out] privkey Pointer to the private key
  123. \param[out] s Pointer to the signature
  124. \return Error code \ref uint32_t
  125. */
  126. uint32_t sc_sm2_sign(sc_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]);
  127. /**
  128. \brief sm2 sign
  129. \param[in] sm2 sm2 handle to operate.
  130. \param[in] d Pointer to the digest.
  131. \param[out] privkey Pointer to the private key
  132. \param[out] s Pointer to the signature
  133. \return Error code \ref uint32_t
  134. */
  135. uint32_t sc_sm2_sign_async(sc_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]);
  136. /* TODO */
  137. /**
  138. \brief sm2 verify
  139. \param[in] sm2 sm2 handle to operate.
  140. \param[in] d Pointer to the digest.
  141. \param[out] privkey Pointer to the private key
  142. \param[out] s Pointer to the signature
  143. \return verify result
  144. */
  145. bool sc_sm2_verify(sc_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]);
  146. /**
  147. \brief sm2 verify
  148. \param[in] sm2 sm2 handle to operate.
  149. \param[in] d Pointer to the digest.
  150. \param[out] privkey Pointer to the private key
  151. \param[out] s Pointer to the signature
  152. \return verify result
  153. */
  154. bool sc_sm2_verify_async(sc_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]);
  155. /**
  156. \brief sm2 encrypto
  157. \param[in] sm2 sm2 handle to operate.
  158. \param[in] plain Pointer to the plaintext.
  159. \param[in] PlainByteLen plaintext len
  160. \param[in] pubKey public key.
  161. \param[out] cipher Pointer to the chipher
  162. \param[out] cipher_byte_len Pointer to the chipher len.
  163. \return Error code \ref uint32_t
  164. */
  165. uint32_t sc_sm2_encrypt(sc_sm2_t *sm2, uint8_t *plain, uint32_t plain_len,
  166. uint8_t pubKey[65], uint8_t *cipher, uint32_t *cipher_len);
  167. /**
  168. \brief sm2 encrypto
  169. \param[in] sm2 sm2 handle to operate.
  170. \param[in] cipher Pointer to the chipher
  171. \param[in] CipherByteLen chipher len.
  172. \param[in] prikey private key.
  173. \param[out] plain Pointer to the plaintext.
  174. \param[out] PlainByteLen plaintext len
  175. \return Error code \ref uint32_t
  176. */
  177. uint32_t sc_sm2_decrypt(sc_sm2_t *sm2, uint8_t *cipher, uint32_t cipher_len,
  178. uint8_t prikey[32], uint8_t *plain,
  179. uint32_t *plain_len);
  180. /**
  181. \brief sm2 key exchange
  182. \param[in] sm2 sm2 handle to operate.
  183. \return Error code \ref uint32_t
  184. */
  185. uint32_t sc_sm2_exchangekey(sc_sm2_t *sm2, sc_sm2_exchange_role_e role,
  186. uint8_t *da, uint8_t *pb, uint8_t *ra1, uint8_t *ra,
  187. uint8_t *rb, uint8_t *za, uint8_t *zb,
  188. uint32_t k_len, uint8_t *ka, uint8_t *s1,
  189. uint8_t *sa);
  190. /**
  191. \brief sm2 key exchange get Z.
  192. \param[in] sm2 sm2 handle to operate.
  193. \return Error code \ref uint32_t
  194. */
  195. uint32_t sc_sm2_getZ(sc_sm2_t *sm2, uint8_t *id, uint32_t id_len,
  196. uint8_t pubkey[65], uint8_t z[32]);
  197. /**
  198. \brief sm2 key exchange get E
  199. \param[in] sm2 sm2 handle to operate.
  200. \return Error code \ref uint32_t
  201. */
  202. uint32_t sc_sm2_getE(sc_sm2_t *sm2, uint8_t *m, uint32_t len, uint8_t z[32], uint8_t e[32]);
  203. /**
  204. \brief Get SM2 state.
  205. \param[in] sm2 SM2 handle to operate.
  206. \param[out] state SM2 state \ref sc_sm2_state_t.
  207. \return Error code \ref uint32_t
  208. */
  209. uint32_t sc_sm2_get_state(sc_sm2_t *sm2, sc_sm2_state_t *state);
  210. #ifdef __cplusplus
  211. extern "C" {
  212. #endif
  213. #endif
  214. #endif /* _SC_SM2_H_ */