ecc.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (C) 2017-2022 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file drv/ecc.h
  6. * @brief Header File for ECC Driver
  7. * @version V3.3
  8. * @date 30. May 2022
  9. * @model ECC
  10. ******************************************************************************/
  11. #ifndef _DRV_ECC_H_
  12. #define _DRV_ECC_H_
  13. #include <stdint.h>
  14. #include "common.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define CSI_ECC_PUBKEY_LEN (65-1)
  19. #define CSI_ECC_PRIVKEY_LEN (32)
  20. #define CSI_ECC_PUBKEYTMP_LEN (65)
  21. #define CSI_ECC_RK_LEN (24) //random
  22. #define CSI_ECC_SIGNATURE_LEN (64)
  23. #define CSI_ECC_DIGEST_LEN (32)
  24. #define ECC_PRIME_CURVE_G_BYTES (64)
  25. #define ECC_PRIME_CURVE_P_BYTES (70)
  26. typedef struct {
  27. uint32_t ecc_curve : 1; ///< supports 256bits curve
  28. } ecc_capabilities_t;
  29. /**
  30. \brief ECC ciphertext order
  31. */
  32. typedef enum {
  33. ECC_C1C3C2 = 0,
  34. ECC_C1C2C3,
  35. } ecc_cipher_order_e;
  36. /**
  37. \brief ECC endian mode
  38. */
  39. typedef enum {
  40. ECC_ENDIAN_LITTLE = 0, ///< Little Endian
  41. ECC_ENDIAN_BIG ///< Big Endian
  42. } ecc_endian_mode_e;
  43. /**
  44. \brief ECC prime curve type
  45. */
  46. typedef enum {
  47. ECC_PRIME256V1 = 0,
  48. } ecc_prime_curve_type;
  49. /**
  50. \brief ECC key exchange role
  51. */
  52. typedef enum { ECC_Role_Sponsor = 0, ECC_Role_Responsor } ecc_exchange_role_e;
  53. /****** ECC Event *****/
  54. typedef enum {
  55. ECC_EVENT_MAKE_KEY_COMPLETE = 0, ///< Make key completed
  56. ECC_EVENT_ENCRYPT_COMPLETE, ///< Encrypt completed
  57. ECC_EVENT_DECRYPT_COMPLETE, ///< Decrypt completed
  58. ECC_EVENT_SIGN_COMPLETE, ///< Sign completed
  59. ECC_EVENT_VERIFY_COMPLETE, ///< Verify completed
  60. ECC_EVENT_EXCHANGE_KEY_COMPLETE, ///< Exchange key completed
  61. } ecc_event_e;
  62. /**
  63. \brief ECC prime curve param
  64. */
  65. typedef struct {
  66. ecc_prime_curve_type type;
  67. uint32_t *p;
  68. } csi_ecc_prime_curve_t;
  69. /**
  70. \brief ECC curve type g param
  71. */
  72. typedef struct {
  73. ecc_prime_curve_type type;
  74. uint8_t *G;
  75. uint8_t *n;
  76. } csi_ecc_curve_g_t;
  77. /**
  78. \brief ECC status
  79. */
  80. typedef struct {
  81. uint32_t busy : 1; ///< Calculate busy flag
  82. } csi_ecc_state_t;
  83. /**
  84. \brief ECC handle
  85. */
  86. typedef struct {
  87. csi_dev_t dev;
  88. void * cb;
  89. void * arg;
  90. csi_ecc_state_t state;
  91. ecc_prime_curve_type type;
  92. } csi_ecc_t;
  93. ///< Pointer to \ref csi_ecc_callback_t : ECC Event call back.
  94. typedef void (*csi_ecc_callback_t)(ecc_event_e event);
  95. /**
  96. \brief Initialize ECC.
  97. \param[in] idx device id
  98. \return Error code \ref csi_error_t
  99. */
  100. csi_error_t csi_ecc_init(csi_ecc_t *ecc, uint32_t idx);
  101. /**
  102. \brief De-initialize ECC Interface. stops operation and releases the software resources used by the interface
  103. \param[in] ecc ecc handle to operate.
  104. \return none
  105. */
  106. void csi_ecc_uninit(csi_ecc_t *ecc);
  107. /**
  108. \brief ecc get capability.
  109. \param[in] ecc Operate handle.
  110. \return Error code \ref csi_error_t
  111. */
  112. csi_error_t csi_ecc_config(csi_ecc_t *ecc, ecc_cipher_order_e co, ecc_endian_mode_e endian);
  113. /**
  114. \brief Attach the callback handler to ECC
  115. \param[in] ecc Operate handle.
  116. \param[in] cb Callback function
  117. \param[in] arg User can define it by himself as callback's param
  118. \return Error code Error code \ref csi_error_t
  119. */
  120. csi_error_t csi_ecc_attach_callback(csi_ecc_t *ecc, csi_ecc_callback_t cb, void *arg);
  121. /**
  122. \brief Detach the callback handler
  123. \param[in] ecc Operate handle.
  124. \return Error code Error code \ref csi_error_t
  125. */
  126. csi_error_t csi_ecc_detach_callback(csi_ecc_t *ecc);
  127. /**
  128. \brief ecc get capability.
  129. \param[in] ecc Operate handle.
  130. \param[out] cap Pointer of ecc_capabilities_t.
  131. \return Error code Error code \ref csi_error_t
  132. */
  133. csi_error_t csi_ecc_get_capabilities(csi_ecc_t *ecc, ecc_capabilities_t *cap);
  134. /**
  135. \brief check whether the public key and private key are a pair.
  136. \param[in] ecc ecc handle to operate.
  137. \param[in] private Pointer to the ecc private key, alloc by caller.
  138. \param[in] public Pointer to the ecc public key, alloc by caller.
  139. \return Error code \ref csi_error_t
  140. */
  141. csi_error_t csi_ecc_check_keypair(csi_ecc_t *ecc, uint8_t pubkey[65], uint8_t prikey[32]);
  142. /**
  143. \brief generate ecc key.
  144. \param[in] ecc ecc handle to operate.
  145. \param[out] private Pointer to the ecc private key, alloc by caller.
  146. \param[out] public Pointer to the ecc public key, alloc by caller.
  147. \return Error code \ref csi_error_t
  148. */
  149. csi_error_t csi_ecc_gen_key(csi_ecc_t *ecc, uint8_t pubkey[65], uint8_t prikey[32]);
  150. /**
  151. \brief generate ecc public key by private key.
  152. \param[in] ecc ecc handle to operate.
  153. \param[out] private Pointer to the ecc private key, alloc by caller.
  154. \param[out] public Pointer to the ecc public key, alloc by caller.
  155. \return Error code \ref csi_error_t
  156. */
  157. csi_error_t csi_ecc_gen_pubkey(csi_ecc_t *ecc, uint8_t pubkey[65], uint8_t prikey[32]);
  158. /**
  159. \brief ecc sign
  160. \param[in] ecc ecc handle to operate.
  161. \param[in] d Pointer to the digest.
  162. \param[out] privkey Pointer to the private key
  163. \param[out] s Pointer to the signature
  164. \return Error code \ref csi_error_t
  165. */
  166. csi_error_t csi_ecc_sign(csi_ecc_t *ecc, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]);
  167. /**
  168. \brief ecc sign asybnc
  169. \param[in] ecc ecc handle to operate.
  170. \param[in] d Pointer to the digest.
  171. \param[out] privkey Pointer to the private key
  172. \param[out] s Pointer to the signature
  173. \return Error code \ref csi_error_t
  174. */
  175. csi_error_t csi_ecc_sign_async(csi_ecc_t *ecc, uint8_t d[32], uint8_t prikey[32], uint8_t s[64]);
  176. /* TODO */
  177. /**
  178. \brief ecc verify
  179. \param[in] ecc ecc handle to operate.
  180. \param[in] d Pointer to the digest.
  181. \param[out] privkey Pointer to the private key
  182. \param[out] s Pointer to the signature
  183. \return verify result
  184. */
  185. bool csi_ecc_verify(csi_ecc_t *ecc, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]);
  186. /**
  187. \brief ecc verify
  188. \param[in] ecc ecc handle to operate.
  189. \param[in] d Pointer to the digest.
  190. \param[out] privkey Pointer to the private key
  191. \param[out] s Pointer to the signature
  192. \return verify result
  193. */
  194. bool csi_ecc_verify_async(csi_ecc_t *ecc, uint8_t d[32], uint8_t pubkey[65], uint8_t s[64]);
  195. /**
  196. \brief ecc encrypto
  197. \param[in] ecc ecc handle to operate.
  198. \param[in] Plain Pointer to the plaintext.
  199. \param[in] PlainByteLen plaintext len
  200. \param[in] pubKey public key.
  201. \param[out] Cipher Pointer to the chipher
  202. \param[out] CipherByteLen Pointer to the chipher len.
  203. \return Error code \ref csi_error_t
  204. */
  205. csi_error_t csi_ecc_encrypt(csi_ecc_t *ecc, uint8_t *Plain,
  206. uint32_t PlainByteLen, uint8_t pubKey[65],
  207. uint8_t *Cipher, uint32_t *CipherByteLen);
  208. /**
  209. \brief ecc encrypto
  210. \param[in] ecc ecc handle to operate.
  211. \param[in] Cipher Pointer to the chipher
  212. \param[in] CipherByteLen chipher len.
  213. \param[in] prikey private key.
  214. \param[out] Plain Pointer to the plaintext.
  215. \param[out] PlainByteLen plaintext len
  216. \return Error code \ref csi_error_t
  217. */
  218. csi_error_t csi_ecc_decrypt(csi_ecc_t *ecc, uint8_t *Cipher,
  219. uint32_t CipherByteLen, uint8_t prikey[32],
  220. uint8_t *Plain, uint32_t *PlainByteLen);
  221. /**
  222. \brief ecc key exchange
  223. \param[in] ecc ecc handle to operate.
  224. \return Error code \ref csi_error_t
  225. */
  226. csi_error_t csi_ecc_exchangekey(csi_ecc_t *ecc, ecc_exchange_role_e role,
  227. uint8_t *dA, uint8_t *PB, uint8_t *rA,
  228. uint8_t *RA, uint8_t *RB, uint8_t *ZA,
  229. uint8_t *ZB, uint32_t kByteLen, uint8_t *KA,
  230. uint8_t *S1, uint8_t *SA);
  231. /**
  232. \brief ecc key exchange get Z.
  233. \param[in] ecc ecc handle to operate.
  234. \return Error code Error code \ref csi_error_t
  235. */
  236. csi_error_t csi_ecc_getZ(csi_ecc_t *ecc, uint8_t *ID, uint32_t byteLenofID,
  237. uint8_t pubKey[65], uint8_t Z[32]);
  238. /**
  239. \brief ecc key exchange get E
  240. \param[in] ecc ecc handle to operate.
  241. \return Error code Error code \ref csi_error_t
  242. */
  243. csi_error_t csi_ecc_getE(csi_ecc_t *ecc, uint8_t *M, uint32_t byteLen,
  244. uint8_t Z[32], uint8_t E[32]);
  245. /**
  246. \brief Get ECC state.
  247. \param[in] ecc ECC handle to operate.
  248. \param[out] state ECC state \ref csi_ecc_state_t.
  249. \return Error code Error code \ref csi_error_t
  250. */
  251. csi_error_t csi_ecc_get_state(csi_ecc_t *ecc, csi_ecc_state_t *state);
  252. /**
  253. \brief Enable ecc power manage
  254. \param[in] ecc ECC handle to operate.
  255. \return Error code Error code \ref csi_error_t
  256. */
  257. csi_error_t csi_ecc_enable_pm(csi_ecc_t *ecc);
  258. /**
  259. \brief Disable ecc power manage
  260. \param[in] ecc ECC handle to operate.
  261. */
  262. void csi_ecc_disable_pm(csi_ecc_t *ecc);
  263. #ifdef __cplusplus
  264. extern "C" {
  265. #endif
  266. #endif