sec_crypto_ecc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2017-2022 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file sec_crypt_ecc.h
  6. * @brief Header File for ECC
  7. * @version V3.3
  8. * @date 30. May 2022
  9. * @model ecc
  10. ******************************************************************************/
  11. #ifndef _SC_ECC_H_
  12. #define _SC_ECC_H_
  13. #include "sec_include_config.h"
  14. #define CONFIG_SEC_CRYPTO_ECC
  15. #ifdef CONFIG_SEC_CRYPTO_ECC
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #ifdef SEC_LIB_VERSION
  20. #include "drv/ecc.h"
  21. #else
  22. #include "ecc.h"
  23. #endif
  24. typedef enum {
  25. SC_ECC_PRIME256V1 = 0,
  26. } sc_ecc_curve_type;
  27. /**
  28. \brief ECC ciphertext order
  29. */
  30. typedef enum {
  31. SC_ECC_C1C3C2 = 0,
  32. SC_ECC_C1C2C3,
  33. } sc_ecc_cipher_order_e;
  34. typedef enum {
  35. SC_ECC_ENDIAN_LITTLE = 0, ///< Little Endian
  36. SC_ECC_ENDIAN_BIG ///< Big Endian
  37. } sc_ecc_endian_mode_e;
  38. /**
  39. \brief ECC key exchange role
  40. */
  41. typedef enum { SC_ECC_Role_Sponsor = 0, SC_ECC_Role_Responsor } sc_ecc_exchange_role_e;
  42. /****** ECC Event *****/
  43. typedef enum {
  44. SC_ECC_EVENT_MAKE_KEY_COMPLETE = 0, ///< Make key completed
  45. SC_ECC_EVENT_ENCRYPT_COMPLETE, ///< Encrypt completed
  46. SC_ECC_EVENT_DECRYPT_COMPLETE, ///< Decrypt completed
  47. SC_ECC_EVENT_SIGN_COMPLETE, ///< Sign completed
  48. SC_ECC_EVENT_VERIFY_COMPLETE, ///< Verify completed
  49. SC_ECC_EVENT_EXCHANGE_KEY_COMPLETE, ///< Exchange key completed
  50. } sc_ecc_event_e;
  51. typedef struct {
  52. uint32_t ecc_curve : 1; ///< supports 256bits curve
  53. } sc_ecc_capabilities_t;
  54. /**
  55. \brief ECC status
  56. */
  57. typedef struct {
  58. uint32_t busy : 1; ///< Calculate busy flag
  59. } sc_ecc_state_t;
  60. typedef struct {
  61. #ifdef CONFIG_CSI_V2
  62. csi_ecc_t ecc;
  63. #endif
  64. } sc_ecc_t;
  65. ///< Pointer to \ref sc_ecc_callback_t : ECC Event call back.
  66. typedef void (*sc_ecc_callback_t)(sc_ecc_event_e event);
  67. /**
  68. \brief Initialize ECC.
  69. \param[in] ecc ecc handle to operate.
  70. \param[in] idx device id
  71. \return \ref uint32_t
  72. */
  73. uint32_t sc_ecc_init(sc_ecc_t *ecc, uint32_t idx);
  74. /**
  75. \brief De-initialize ECC Interface. stops operation and releases the
  76. software resources used by the interface \param[in] ecc ecc handle to
  77. operate. \return none
  78. */
  79. void sc_ecc_uninit(sc_ecc_t *ecc);
  80. /**
  81. \brief ecc get capability.
  82. \param[in] ecc Operate handle.
  83. \return \ref uint32_t
  84. */
  85. uint32_t sc_ecc_config(sc_ecc_t *ecc, sc_ecc_cipher_order_e co,
  86. sc_ecc_endian_mode_e endian);
  87. /**
  88. \brief Attach the callback handler to ECC
  89. \param[in] ecc Operate handle.
  90. \param[in] cb Callback function
  91. \param[in] arg User can define it by himself as callback's param
  92. \return Error code \ref uint32_t
  93. */
  94. uint32_t sc_ecc_attach_callback(sc_ecc_t *ecc, sc_ecc_callback_t cb, void *arg);
  95. /**
  96. \brief Detach the callback handler
  97. \param[in] ecc Operate handle.
  98. */
  99. uint32_t sc_ecc_detach_callback(sc_ecc_t *ecc);
  100. /**
  101. \brief ecc get capability.
  102. \param[in] ecc Operate handle.
  103. \param[out] cap Pointer of sc_ecc_capabilities_t.
  104. \return \ref uint32_t
  105. */
  106. uint32_t sc_ecc_get_capabilities(sc_ecc_t *ecc, sc_ecc_capabilities_t *cap);
  107. uint32_t sc_ecc_check_keypair(sc_ecc_t *ecc, uint8_t pubkey[65],
  108. uint8_t prikey[32]);
  109. /**
  110. \brief generate ecc key.
  111. \param[in] ecc ecc handle to operate.
  112. \param[out] private Pointer to the ecc private key, alloc by caller.
  113. \param[out] public Pointer to the ecc public key, alloc by caller.
  114. \return \ref uint32_t
  115. */
  116. uint32_t sc_ecc_gen_key(sc_ecc_t *ecc, uint8_t pubkey[65], uint8_t prikey[32]);
  117. /**
  118. \brief generate ecc pubkey.
  119. \param[in] ecc ecc handle to operate.
  120. \param[in] prikey Pointer to the ecc private key, alloc by caller.
  121. \param[out] pubkey Pointer to the ecc public key, alloc by caller.
  122. \return \ref uint32_t
  123. */
  124. uint32_t sc_ecc_gen_pubkey(sc_ecc_t *ecc, uint8_t pubkey[65],
  125. uint8_t prikey[32], sc_ecc_curve_type type);
  126. /**
  127. \brief ecc sign
  128. \param[in] ecc ecc handle to operate.
  129. \param[in] d Pointer to the digest.
  130. \param[out] privkey Pointer to the private key
  131. \param[out] s Pointer to the signature
  132. \return \ref uint32_t
  133. */
  134. uint32_t sc_ecc_sign(sc_ecc_t *ecc, uint8_t d[32], uint8_t prikey[32],
  135. uint8_t s[64], sc_ecc_curve_type type);
  136. /**
  137. \brief ecc sign
  138. \param[in] ecc ecc handle to operate.
  139. \param[in] d Pointer to the digest.
  140. \param[out] privkey Pointer to the private key
  141. \param[out] s Pointer to the signature
  142. \return \ref uint32_t
  143. */
  144. uint32_t sc_ecc_sign_async(sc_ecc_t *ecc, uint8_t d[32], uint8_t prikey[32],
  145. uint8_t s[64], sc_ecc_curve_type type);
  146. /* TODO */
  147. /**
  148. \brief ecc verify
  149. \param[in] ecc ecc handle to operate.
  150. \param[in] d Pointer to the digest.
  151. \param[out] privkey Pointer to the private key
  152. \param[out] s Pointer to the signature
  153. \return verify result
  154. */
  155. bool sc_ecc_verify(sc_ecc_t *ecc, uint8_t d[32], uint8_t pubkey[65],
  156. uint8_t s[64], sc_ecc_curve_type type);
  157. /**
  158. \brief ecc verify
  159. \param[in] ecc ecc handle to operate.
  160. \param[in] d Pointer to the digest.
  161. \param[out] privkey Pointer to the private key
  162. \param[out] s Pointer to the signature
  163. \return verify result
  164. */
  165. bool sc_ecc_verify_async(sc_ecc_t *ecc, uint8_t d[32], uint8_t pubkey[65],
  166. uint8_t s[64], sc_ecc_curve_type type);
  167. /**
  168. \brief ecc encrypto
  169. \param[in] ecc ecc handle to operate.
  170. \param[in] plain Pointer to the plaintext.
  171. \param[in] PlainByteLen plaintext len
  172. \param[in] pubKey public key.
  173. \param[out] cipher Pointer to the chipher
  174. \param[out] cipher_byte_len Pointer to the chipher len.
  175. \return uint32_t
  176. */
  177. uint32_t sc_ecc_encrypt(sc_ecc_t *ecc, uint8_t *plain, uint32_t plain_len,
  178. uint8_t pubKey[65], uint8_t *cipher,
  179. uint32_t *cipher_len);
  180. /**
  181. \brief ecc encrypto
  182. \param[in] ecc ecc handle to operate.
  183. \param[in] cipher Pointer to the chipher
  184. \param[in] CipherByteLen chipher len.
  185. \param[in] prikey private key.
  186. \param[out] plain Pointer to the plaintext.
  187. \param[out] PlainByteLen plaintext len
  188. \return uint32_t
  189. */
  190. uint32_t sc_ecc_decrypt(sc_ecc_t *ecc, uint8_t *cipher, uint32_t cipher_len,
  191. uint8_t prikey[32], uint8_t *plain,
  192. uint32_t *plain_len);
  193. /**
  194. \brief ecc key exchange
  195. \param[in] ecc ecc handle to operate.
  196. \return uint32_t
  197. */
  198. uint32_t sc_ecc_exchangekey(sc_ecc_t *ecc, sc_ecc_exchange_role_e role,
  199. uint8_t *da, uint8_t *pb, uint8_t *ra1, uint8_t *ra,
  200. uint8_t *rb, uint8_t *za, uint8_t *zb,
  201. uint32_t k_len, uint8_t *ka, uint8_t *s1,
  202. uint8_t *sa);
  203. /**
  204. \brief ecc key exchange get Z.
  205. \param[in] ecc ecc handle to operate.
  206. \return uint32_t
  207. */
  208. uint32_t sc_ecc_getZ(sc_ecc_t *ecc, uint8_t *id, uint32_t id_len,
  209. uint8_t pubkey[65], uint8_t z[32]);
  210. /**
  211. \brief ecc key exchange get E
  212. \param[in] ecc ecc handle to operate.
  213. \return uint32_t
  214. */
  215. uint32_t sc_ecc_getE(sc_ecc_t *ecc, uint8_t *m, uint32_t len, uint8_t z[32],
  216. uint8_t e[32]);
  217. /**
  218. \brief Get ECC state.
  219. \param[in] ecc ECC handle to operate.
  220. \param[out] state ECC state \ref sc_ecc_state_t.
  221. \return Error code \ref uint32_t
  222. */
  223. uint32_t sc_ecc_get_state(sc_ecc_t *ecc, sc_ecc_state_t *state);
  224. #ifdef __cplusplus
  225. extern "C" {
  226. #endif
  227. #endif
  228. #endif /* _SC_ECC_H_ */