sec_crypto_rsa.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file seccrypt_rsa.h
  6. * @brief Header File for RSA
  7. * @version V1.0
  8. * @date 20. Jul 2020
  9. * @model rsa
  10. ******************************************************************************/
  11. #ifndef _SC_RSA_H_
  12. #define _SC_RSA_H_
  13. #include "sec_include_config.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #ifdef CONFIG_SYSTEM_SECURE
  18. #ifdef SEC_LIB_VERSION
  19. #include "drv/rsa.h"
  20. #else
  21. #include "rsa.h"
  22. #endif
  23. #endif
  24. #ifdef CONFIG_SEC_CRYPTO_RSA_SW
  25. #include "crypto_rsa.h"
  26. #endif
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #ifdef SEC_LIB_VERSION
  30. #include "drv/common.h"
  31. #else
  32. #include "common.h"
  33. #endif
  34. #include "sec_crypto_errcode.h"
  35. //TODO Del this file after updating to sc2.0
  36. /*----- RSA Control Codes: Mode Parameters: Key Bits -----*/
  37. typedef enum {
  38. SC_RSA_KEY_BITS_192 = 0, ///< 192 Key bits
  39. SC_RSA_KEY_BITS_256, ///< 256 Key bits
  40. SC_RSA_KEY_BITS_512, ///< 512 Key bits
  41. SC_RSA_KEY_BITS_1024, ///< 1024 Key bits
  42. SC_RSA_KEY_BITS_2048, ///< 2048 Key bits
  43. SC_RSA_KEY_BITS_3072, ///< 3072 Key bits
  44. SC_RSA_KEY_BITS_4096 ///< 4096 Key bits
  45. } sc_rsa_key_bits_t;
  46. typedef enum {
  47. SC_RSA_PADDING_MODE_NO = 0, ///< RSA NO Padding Mode
  48. SC_RSA_PADDING_MODE_PKCS1, ///< RSA PKCS1 Padding Mode
  49. SC_RSA_PADDING_MODE_PKCS1_OAEP, ///< RSA PKCS1 OAEP Padding Mode
  50. SC_RSA_PADDING_MODE_SSLV23, ///< RSA SSLV23 Padding Mode
  51. SC_RSA_PADDING_MODE_X931, ///< RSA X931 Padding Mode
  52. SC_RSA_PADDING_MODE_PSS ///< RSA PSS Padding Mode
  53. } sc_rsa_padding_type_t;
  54. typedef enum {
  55. SC_RSA_HASH_TYPE_MD5 = 0,
  56. SC_RSA_HASH_TYPE_SHA1,
  57. SC_RSA_HASH_TYPE_SHA224,
  58. SC_RSA_HASH_TYPE_SHA256,
  59. SC_RSA_HASH_TYPE_SHA384,
  60. SC_RSA_HASH_TYPE_SHA512
  61. } sc_rsa_hash_type_t;
  62. typedef struct {
  63. // #if (defined(CONFIG_SYSTEM_SECURE) && defined(CONFIG_CSI_V2))
  64. // csi_rsa_context_t rsa_ctx;
  65. // #else
  66. void * n; ///< Pointer to the public modulus
  67. void * e; ///< Pointer to the public exponent
  68. void * d; ///< Pointer to the private exponent
  69. sc_rsa_key_bits_t key_bits; ///< RSA KEY BITS
  70. sc_rsa_padding_type_t padding_type; ///< RSA PADDING TYPE
  71. sc_rsa_hash_type_t hash_type;
  72. // #endif
  73. } sc_rsa_context_t;
  74. /**
  75. \brief RSA State
  76. */
  77. typedef struct {
  78. uint8_t busy : 1; ///< Calculate busy flag
  79. uint8_t error : 1; ///< Calculate error flag
  80. } sc_rsa_state_t;
  81. typedef struct {
  82. #ifdef CONFIG_SYSTEM_SECURE
  83. #ifdef CONFIG_CSI_V1
  84. rsa_handle_t handle;
  85. #endif /* CONFIG_CSI_V1 */
  86. #ifdef CONFIG_CSI_V2
  87. csi_rsa_t csi_rsa;
  88. #endif
  89. #endif
  90. #if defined(CONFIG_SEC_CRYPTO_RSA_SW)
  91. sc_mbedtls_rsa_context rsa_ctx;
  92. #endif
  93. sc_rsa_key_bits_t bits;
  94. } sc_rsa_t;
  95. /****** RSA Event *****/
  96. typedef enum {
  97. SC_RSA_EVENT_COMPLETE = 0, ///< rsa event completed
  98. SC_RSA_EVENT_VERIFY_SUCCESS,
  99. SC_RSA_EVENT_VERIFY_FAILED,
  100. SC_RSA_EVENT_ERROR, ///< error event
  101. } sc_rsa_event_t;
  102. typedef void (*sc_rsa_callback_t)(
  103. sc_rsa_t *rsa, sc_rsa_event_t event,
  104. void *arg); ///< Pointer to \ref sc_rsa_callback_t : RSA Event call back.
  105. // Function documentation
  106. /**
  107. \brief Initialize RSA Interface. 1. Initializes the resources needed for the RSA interface 2.registers event callback function
  108. \param[in] rsa rsa handle to operate.
  109. \param[in] idx device id
  110. \param[in] data_bits rsa bit width
  111. \return \ref uint32_t
  112. */
  113. uint32_t sc_rsa_init(sc_rsa_t *rsa, uint32_t idx, sc_rsa_key_bits_t data_bits);
  114. /**
  115. \brief De-initialize RSA Interface. stops operation and releases the software resources used by the interface
  116. \param[in] rsa rsa handle to operate.
  117. \return none
  118. */
  119. void sc_rsa_uninit(sc_rsa_t *rsa);
  120. /**
  121. \brief attach the callback handler to RSA
  122. \param[in] rsa operate handle.
  123. \param[in] cb callback function
  124. \param[in] arg user can define it by himself as callback's param
  125. \return error code
  126. */
  127. uint32_t sc_rsa_attach_callback(sc_rsa_t *rsa, sc_rsa_callback_t cb, void *arg);
  128. /**
  129. \brief detach the callback handler
  130. \param[in] rsa operate handle.
  131. */
  132. void sc_rsa_detach_callback(sc_rsa_t *rsa);
  133. /**
  134. \brief generate rsa key pair.
  135. \param[in] rsa rsa handle to operate.
  136. \param[out] context Pointer to the rsa context
  137. \return \ref uint32_t
  138. */
  139. uint32_t sc_rsa_gen_key(sc_rsa_t *rsa, sc_rsa_context_t *context);
  140. /**
  141. \brief encrypt
  142. \param[in] rsa rsa handle to operate.
  143. \param[in] context Pointer to the rsa context
  144. \param[in] src Pointer to the source data.
  145. \param[in] src_size the source data len
  146. \param[out] out Pointer to the result buffer
  147. \return \ref uint32_t
  148. */
  149. uint32_t sc_rsa_encrypt(sc_rsa_t *rsa, sc_rsa_context_t *context, void *src,
  150. uint32_t src_size, void *out);
  151. /**
  152. \brief decrypt
  153. \param[in] rsa rsa handle to operate.
  154. \param[in] context Pointer to the rsa context
  155. \param[in] src Pointer to the source data.
  156. \param[in] src_size the source data len
  157. \param[out] out Pointer to the result buffer
  158. \param[out] out_size the result size
  159. \return \ref uint32_t
  160. */
  161. uint32_t sc_rsa_decrypt(sc_rsa_t *rsa, sc_rsa_context_t *context, void *src,
  162. uint32_t src_size, void *out, uint32_t *out_size);
  163. /**
  164. \brief rsa sign
  165. \param[in] rsa rsa handle to operate.
  166. \param[in] context Pointer to the rsa context
  167. \param[in] src Pointer to the source data.
  168. \param[in] src_size the source data len
  169. \param[out] signature Pointer to the signature
  170. \param[in] hash_type the source data hash type
  171. \return \ref uint32_t
  172. */
  173. uint32_t sc_rsa_sign(sc_rsa_t *rsa, sc_rsa_context_t *context, void *src, uint32_t src_size,
  174. void *signature, sc_rsa_hash_type_t hash_type);
  175. /**
  176. \brief rsa verify
  177. \param[in] rsa rsa handle to operate.
  178. \param[in] context Pointer to the rsa context
  179. \param[in] src Pointer to the source data.
  180. \param[in] src_size the source data len
  181. \param[in] signature Pointer to the signature
  182. \param[in] sig_size the signature size
  183. \param[in] hash_type the source data hash type
  184. \return verify result
  185. */
  186. bool sc_rsa_verify(sc_rsa_t *rsa, sc_rsa_context_t *context, void *src, uint32_t src_size,
  187. void *signature, uint32_t sig_size, sc_rsa_hash_type_t hash_type);
  188. /**
  189. \brief encrypt(async mode)
  190. \param[in] rsa rsa handle to operate.
  191. \param[in] context Pointer to the rsa context
  192. \param[in] src Pointer to the source data.
  193. \param[in] src_size the source data len
  194. \param[out] out Pointer to the result buffer
  195. \return \ref uint32_t
  196. */
  197. uint32_t sc_rsa_encrypt_async(sc_rsa_t *rsa, sc_rsa_context_t *context, void *src,
  198. uint32_t src_size, void *out);
  199. /**
  200. \brief decrypt(async mode)
  201. \param[in] rsa rsa handle to operate.
  202. \param[in] context Pointer to the rsa context
  203. \param[in] src Pointer to the source data.
  204. \param[in] src_size the source data len
  205. \param[out] out Pointer to the result buffer
  206. \param[out] out_size the result size
  207. \return \ref uint32_t
  208. */
  209. uint32_t sc_rsa_decrypt_async(sc_rsa_t *rsa, sc_rsa_context_t *context, void *src,
  210. uint32_t src_size, void *out, uint32_t *out_size);
  211. /**
  212. \brief rsa sign(async mode)
  213. \param[in] rsa rsa handle to operate.
  214. \param[in] context Pointer to the rsa context
  215. \param[in] src Pointer to the source data.
  216. \param[in] src_size the source data len
  217. \param[out] signature Pointer to the signature
  218. \param[in] hash_type the source data hash type
  219. \return \ref uint32_t
  220. */
  221. uint32_t sc_rsa_sign_async(sc_rsa_t *rsa, sc_rsa_context_t *context, void *src,
  222. uint32_t src_size, void *signature, sc_rsa_hash_type_t hash_type);
  223. /**
  224. \brief rsa verify(async mode)
  225. \param[in] rsa rsa handle to operate.
  226. \param[in] context Pointer to the rsa context
  227. \param[in] src Pointer to the source data.
  228. \param[in] src_size the source data len
  229. \param[in] signature Pointer to the signature
  230. \param[in] sig_size the signature size
  231. \param[in] hash_type the source data hash type
  232. \return verify result
  233. */
  234. uint32_t sc_rsa_verify_async(sc_rsa_t *rsa, sc_rsa_context_t *context, void *src,
  235. uint32_t src_size, void *signature, uint32_t sig_size,
  236. sc_rsa_hash_type_t hash_type);
  237. /**
  238. \brief Get RSA state.
  239. \param[in] rsa rsa handle to operate.
  240. \param[out] state rsa state \ref sc_rsa_state_t.
  241. \return \ref uint32_t
  242. */
  243. uint32_t sc_rsa_get_state(sc_rsa_t *rsa, sc_rsa_state_t *state);
  244. /**
  245. \brief Get big prime data
  246. \param[in] rsa rsa handle to operate.
  247. \param[in] p Pointer to the prime
  248. \param[in] bit_length Pointer to the prime bit length
  249. \return \ref uint32_t
  250. */
  251. uint32_t sc_rsa_get_prime(sc_rsa_t *rsa, void *p, uint32_t bit_length);
  252. /**
  253. \brief enable rsa power manage
  254. \param[in] rsa rsa handle to operate.
  255. \return error code
  256. */
  257. uint32_t sc_rsa_enable_pm(sc_rsa_t *rsa);
  258. /**
  259. \brief disable rsa power manage
  260. \param[in] rsa rsa handle to operate.
  261. */
  262. void sc_rsa_disable_pm(sc_rsa_t *rsa);
  263. /**
  264. \brief set if checked decrypt error.
  265. \param[in] checked if checked error.
  266. */
  267. void sc_rsa_set_ignore_decrypt_error(bool checked);
  268. /**
  269. \brief Get publickey by p q prime data
  270. \param[in] rsa rsa handle to operate.
  271. \param[in] context Pointer to the rsa context
  272. \param[in] p Pointer to the prime p
  273. \param[in] p_byte_len Pointer to the prime p byte length
  274. \param[in] q Pointer to the prime q
  275. \param[in] q_byte_len Pointer to the prime q byte length
  276. \param[in] out Pointer to the publickey
  277. */
  278. uint32_t sc_rsa_get_publickey(sc_rsa_t *rsa, sc_rsa_context_t *context, void *p, uint32_t p_byte_len,
  279. void *q, uint32_t q_byte_len, void *out);
  280. /**
  281. \brief Generation rsa keyparis
  282. \param[in] rsa rsa handle to operate.
  283. \param[in] context Pointer to the rsa context
  284. */
  285. uint32_t sc_rsa_gen_keypairs(sc_rsa_t *rsa, sc_rsa_context_t *context);
  286. #ifdef __cplusplus
  287. }
  288. #endif
  289. #endif /* _SC_RSA_H_ */