sm2.h 8.3 KB

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