ecc.h 8.3 KB

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