sm2.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file drv/sm2.h
  6. * @brief Header File for SM2 Driver
  7. * @version V2.0
  8. * @date 9. DEC 2020
  9. * @model SM2
  10. ******************************************************************************/
  11. #ifndef _DRV_SM2_H_
  12. #define _DRV_SM2_H_
  13. #include <stdint.h>
  14. #include "common.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. typedef struct {
  19. uint32_t sm2_curve : 1; ///< supports 256bits curve
  20. } sm2_capabilities_t;
  21. /**
  22. \brief SM2 ciphertext order
  23. */
  24. typedef enum {
  25. SM2_C1C3C2 = 0,
  26. SM2_C1C2C3,
  27. } sm2_cipher_order_e;
  28. typedef enum {
  29. SM2_ENDIAN_LITTLE = 0, ///< Little Endian
  30. SM2_ENDIAN_BIG ///< Big Endian
  31. } sm2_endian_mode_e;
  32. /**
  33. \brief SM2 status
  34. */
  35. typedef struct {
  36. uint32_t busy : 1; ///< Calculate busy flag
  37. } csi_sm2_state_t;
  38. /**
  39. \brief SM2 key exchange role
  40. */
  41. typedef enum { SM2_Role_Sponsor = 0, SM2_Role_Responsor } sm2_exchange_role_e;
  42. /****** SM2 Event *****/
  43. typedef enum {
  44. SM2_EVENT_MAKE_KEY_COMPLETE = 0, ///< Make key completed
  45. SM2_EVENT_ENCRYPT_COMPLETE, ///< Encrypt completed
  46. SM2_EVENT_DECRYPT_COMPLETE, ///< Decrypt completed
  47. SM2_EVENT_SIGN_COMPLETE, ///< Sign completed
  48. SM2_EVENT_VERIFY_COMPLETE, ///< Verify completed
  49. SM2_EVENT_EXCHANGE_KEY_COMPLETE, ///< Exchange key completed
  50. } sm2_event_e;
  51. typedef struct {
  52. csi_dev_t dev;
  53. void * cb;
  54. void * arg;
  55. csi_sm2_state_t state;
  56. void * prim;
  57. } csi_sm2_t;
  58. ///< Pointer to \ref csi_sm2_callback_t : SM2 Event call back.
  59. typedef void (*csi_sm2_callback_t)(sm2_event_e event);
  60. /**
  61. \brief Initialize SM2.
  62. \param[in] sm2 sm2 handle to operate.
  63. \param[in] idx device id
  64. \return \ref uint32_t
  65. */
  66. csi_error_t csi_sm2_init(csi_sm2_t *sm2, uint32_t idx);
  67. /**
  68. \brief De-initialize SM2 Interface. stops operation and releases the software resources used by the interface
  69. \param[in] sm2 sm2 handle to operate.
  70. \return none
  71. */
  72. void csi_sm2_uninit(csi_sm2_t *sm2);
  73. /**
  74. \brief sm2 get capability.
  75. \param[in] sm2 Operate handle.
  76. \return \ref uint32_t
  77. */
  78. csi_error_t csi_sm2_config(csi_sm2_t *sm2, sm2_cipher_order_e co,
  79. sm2_endian_mode_e endian);
  80. /**
  81. \brief Attach the callback handler to SM2
  82. \param[in] sm2 Operate handle.
  83. \param[in] cb Callback function
  84. \param[in] arg User can define it by himself as callback's param
  85. \return Error code \ref csi_error_t
  86. */
  87. csi_error_t csi_sm2_attach_callback(csi_sm2_t *sm2, csi_sm2_callback_t cb,
  88. void *arg);
  89. /**
  90. \brief Detach the callback handler
  91. \param[in] sm2 Operate handle.
  92. */
  93. void csi_sm2_detach_callback(csi_sm2_t *sm2);
  94. /**
  95. \brief sm2 get capability.
  96. \param[in] sm2 Operate handle.
  97. \param[out] cap Pointer of sm2_capabilities_t.
  98. \return \ref uint32_t
  99. */
  100. csi_error_t csi_sm2_get_capabilities(csi_sm2_t *sm2, sm2_capabilities_t *cap);
  101. csi_error_t csi_sm2_check_keypair(csi_sm2_t *sm2, uint8_t pubkey[65],
  102. uint8_t prikey[32]);
  103. /**
  104. \brief generate sm2 key.
  105. \param[in] sm2 sm2 handle to operate.
  106. \param[out] private Pointer to the sm2 private key, alloc by caller.
  107. \param[out] public Pointer to the sm2 public key, alloc by caller.
  108. \return \ref uint32_t
  109. */
  110. csi_error_t csi_sm2_gen_key(csi_sm2_t *sm2, uint8_t pubkey[65],
  111. uint8_t prikey[32]);
  112. /**
  113. \brief sm2 sign
  114. \param[in] sm2 sm2 handle to operate.
  115. \param[in] d Pointer to the digest.
  116. \param[out] privkey Pointer to the private key
  117. \param[out] s Pointer to the signature
  118. \return \ref uint32_t
  119. */
  120. csi_error_t csi_sm2_sign(csi_sm2_t *sm2, uint8_t d[32], uint8_t prikey[32],
  121. uint8_t s[64]);
  122. /**
  123. \brief sm2 sign
  124. \param[in] sm2 sm2 handle to operate.
  125. \param[in] d Pointer to the digest.
  126. \param[out] privkey Pointer to the private key
  127. \param[out] s Pointer to the signature
  128. \return \ref uint32_t
  129. */
  130. csi_error_t csi_sm2_sign_async(csi_sm2_t *sm2, uint8_t d[32],
  131. uint8_t prikey[32], uint8_t s[64]);
  132. /* TODO */
  133. /**
  134. \brief sm2 verify
  135. \param[in] sm2 sm2 handle to operate.
  136. \param[in] d Pointer to the digest.
  137. \param[out] privkey Pointer to the private key
  138. \param[out] s Pointer to the signature
  139. \return verify result
  140. */
  141. bool csi_sm2_verify(csi_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65],
  142. uint8_t s[64]);
  143. /**
  144. \brief sm2 verify
  145. \param[in] sm2 sm2 handle to operate.
  146. \param[in] d Pointer to the digest.
  147. \param[out] privkey Pointer to the private key
  148. \param[out] s Pointer to the signature
  149. \return verify result
  150. */
  151. bool csi_sm2_verify_async(csi_sm2_t *sm2, uint8_t d[32], uint8_t pubkey[65],
  152. uint8_t s[64]);
  153. /**
  154. \brief sm2 encrypto
  155. \param[in] sm2 sm2 handle to operate.
  156. \param[in] Plain Pointer to the plaintext.
  157. \param[in] PlainByteLen plaintext len
  158. \param[in] pubKey public key.
  159. \param[out] Cipher Pointer to the chipher
  160. \param[out] CipherByteLen Pointer to the chipher len.
  161. \return uint32_t
  162. */
  163. csi_error_t csi_sm2_encrypt(csi_sm2_t *sm2, uint8_t *Plain,
  164. uint32_t PlainByteLen, uint8_t pubKey[65],
  165. uint8_t *Cipher, uint32_t *CipherByteLen);
  166. /**
  167. \brief sm2 encrypto
  168. \param[in] sm2 sm2 handle to operate.
  169. \param[in] Cipher Pointer to the chipher
  170. \param[in] CipherByteLen chipher len.
  171. \param[in] prikey private key.
  172. \param[out] Plain Pointer to the plaintext.
  173. \param[out] PlainByteLen plaintext len
  174. \return uint32_t
  175. */
  176. csi_error_t csi_sm2_decrypt(csi_sm2_t *sm2, uint8_t *Cipher,
  177. uint32_t CipherByteLen, uint8_t prikey[32],
  178. uint8_t *Plain, uint32_t *PlainByteLen);
  179. /**
  180. \brief sm2 key exchange
  181. \param[in] sm2 sm2 handle to operate.
  182. \return uint32_t
  183. */
  184. csi_error_t csi_sm2_exchangekey(csi_sm2_t *sm2, sm2_exchange_role_e role,
  185. uint8_t *dA, uint8_t *PB, uint8_t *rA,
  186. uint8_t *RA, uint8_t *RB, uint8_t *ZA,
  187. uint8_t *ZB, uint32_t kByteLen, uint8_t *KA,
  188. uint8_t *S1, uint8_t *SA);
  189. /**
  190. \brief sm2 key exchange get Z.
  191. \param[in] sm2 sm2 handle to operate.
  192. \return uint32_t
  193. */
  194. csi_error_t csi_sm2_getZ(csi_sm2_t *sm2, uint8_t *ID, uint32_t byteLenofID,
  195. uint8_t pubKey[65], uint8_t Z[32]);
  196. /**
  197. \brief sm2 key exchange get E
  198. \param[in] sm2 sm2 handle to operate.
  199. \return uint32_t
  200. */
  201. csi_error_t csi_sm2_getE(csi_sm2_t *sm2, uint8_t *M, uint32_t byteLen,
  202. 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 csi_sm2_state_t.
  207. \return Error code \ref csi_error_t
  208. */
  209. csi_error_t csi_sm2_get_state(csi_sm2_t *sm2, csi_sm2_state_t *state);
  210. /**
  211. \brief Enable sm2 power manage
  212. \param[in] sm2 SM2 handle to operate.
  213. \return Error code \ref csi_error_t
  214. */
  215. csi_error_t csi_sm2_enable_pm(csi_sm2_t *sm2);
  216. /**
  217. \brief Disable sm2 power manage
  218. \param[in] sm2 SM2 handle to operate.
  219. */
  220. void csi_sm2_disable_pm(csi_sm2_t *sm2);
  221. #ifdef __cplusplus
  222. extern "C" {
  223. #endif
  224. #endif