rsa.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file drv/rsa.h
  6. * @brief Header File for RSA Driver
  7. * @version V1.0
  8. * @date 02. June 2020
  9. * @model rsa
  10. ******************************************************************************/
  11. #ifndef _DRV_RSA_H_
  12. #define _DRV_RSA_H_
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #include <stdint.h>
  17. #include "common.h"
  18. #define RSA_PRIME_256_BIT_LEN (128)
  19. #define RSA_PRIME_512_BIT_LEN (256)
  20. #define RSA_PRIME_1024_BIT_LEN (512)
  21. #define RSA_PRIME_2048_BIT_LEN (1024)
  22. #define RSA_PRIME_4096_BIT_LEN (2048)
  23. #define RSA_256_BYTE_LEN (32)
  24. #define RSA_512_BYTE_LEN (64)
  25. #define RSA_1024_BYTE_LEN (128)
  26. #define RSA_2048_BYTE_LEN (256)
  27. #define RSA_4096_BYTE_LEN (512)
  28. #define RSA_EM_BYTE_LEN RSA_4096_BYTE_LEN
  29. #define SHA256_DIGEST_BYTE_LEN 32
  30. #define RSA_PKCS1_PADDING_SIZE 11
  31. #define RSA_MD5_OID_LEN (6 + 8 + 4)
  32. #define RSA_SHA1_OID_LEN (6 + 5 + 4)
  33. #define RSA_SHA224_OID_LEN (6 + 9 + 4)
  34. #define RSA_SHA256_OID_LEN (6 + 9 + 4)
  35. #define RSA_SHA384_OID_LEN (6 + 9 + 4)
  36. #define RSA_SHA512_OID_LEN (6 + 9 + 4)
  37. /*----- RSA Control Codes: Mode Parameters: Key Bits -----*/
  38. typedef enum {
  39. RSA_KEY_BITS_192 = 0, ///< 192 Key bits
  40. RSA_KEY_BITS_256, ///< 256 Key bits
  41. RSA_KEY_BITS_512, ///< 512 Key bits
  42. RSA_KEY_BITS_1024, ///< 1024 Key bits
  43. RSA_KEY_BITS_2048, ///< 2048 Key bits
  44. RSA_KEY_BITS_3072, ///< 3072 Key bits
  45. RSA_KEY_BITS_4096 ///< 4096 Key bits
  46. } csi_rsa_key_bits_t;
  47. typedef enum {
  48. RSA_PADDING_MODE_NO = 0, ///< RSA NO Padding Mode
  49. RSA_PADDING_MODE_PKCS1, ///< RSA PKCS1 Padding Mode
  50. RSA_PADDING_MODE_PKCS1_OAEP, ///< RSA PKCS1 OAEP Padding Mode
  51. RSA_PADDING_MODE_SSLV23, ///< RSA SSLV23 Padding Mode
  52. RSA_PADDING_MODE_X931, ///< RSA X931 Padding Mode
  53. RSA_PADDING_MODE_PSS ///< RSA PSS Padding Mode
  54. } csi_rsa_padding_type_t;
  55. typedef enum {
  56. RSA_HASH_TYPE_MD5 = 0,
  57. RSA_HASH_TYPE_SHA1,
  58. RSA_HASH_TYPE_SHA224,
  59. RSA_HASH_TYPE_SHA256,
  60. RSA_HASH_TYPE_SHA384,
  61. RSA_HASH_TYPE_SHA512
  62. } csi_rsa_hash_type_t;
  63. typedef struct {
  64. csi_rsa_hash_type_t hash_type;
  65. uint32_t oid_len;
  66. uint8_t *oid;
  67. }RSA_OID;
  68. typedef struct {
  69. void *n; ///< Pointer to the public modulus
  70. void *e; ///< Pointer to the public exponent
  71. void *d; ///< Pointer to the private exponent
  72. csi_rsa_key_bits_t key_bits; ///< RSA KEY BITS
  73. csi_rsa_padding_type_t padding_type; ///< RSA PADDING TYPE
  74. } csi_rsa_context_t;
  75. /**
  76. \brief RSA State
  77. */
  78. typedef struct {
  79. uint8_t busy : 1; ///< Calculate busy flag
  80. uint8_t error : 1; ///< Calculate error flag
  81. } csi_rsa_state_t;
  82. typedef struct {
  83. csi_dev_t dev;
  84. void *cb;
  85. void *arg;
  86. csi_rsa_state_t state;
  87. void *prim;
  88. } csi_rsa_t;
  89. typedef struct {
  90. uint32_t pout[64];
  91. uint8_t *pouts;
  92. uint32_t *pout_size;
  93. uint32_t u32keywords;
  94. uint8_t *pdst;
  95. uint32_t u32padding;
  96. uint32_t u32dst_words;
  97. uint32_t u32type;
  98. uint32_t rsa_state;
  99. }rsa_middle_t;
  100. /****** RSA Event *****/
  101. typedef enum {
  102. RSA_EVENT_COMPLETE = 0, ///< rsa event completed
  103. RSA_EVENT_VERIFY_SUCCESS,
  104. RSA_EVENT_VERIFY_FAILED,
  105. RSA_EVENT_ERROR, ///< error event
  106. } csi_rsa_event_t;
  107. typedef void (*csi_rsa_callback_t)(csi_rsa_t *rsa, csi_rsa_event_t event, void *arg); ///< Pointer to \ref csi_rsa_callback_t : RSA Event call back.
  108. /**
  109. \brief Initialize RSA Interface. 1. Initializes the resources needed for the RSA interface 2.registers event callback function
  110. \param[in] rsa RSA handle to operate.
  111. \param[in] idx Device id
  112. \return Error code \ref csi_error_t
  113. */
  114. csi_error_t csi_rsa_init(csi_rsa_t *rsa, uint32_t idx);
  115. /**
  116. \brief De-initialize RSA Interface. stops operation and releases the software resources used by the interface
  117. \param[in] rsa RSA handle to operate.
  118. \return none
  119. */
  120. void csi_rsa_uninit(csi_rsa_t *rsa);
  121. /**
  122. \brief Attach the callback handler to RSA
  123. \param[in] rsa Operate handle.
  124. \param[in] cb Callback function
  125. \param[in] arg User can define it by himself as callback's param
  126. \return Error code \ref csi_error_t
  127. */
  128. csi_error_t csi_rsa_attach_callback(csi_rsa_t *rsa, csi_rsa_callback_t cb, void *arg);
  129. /**
  130. \brief Detach the callback handler
  131. \param[in] rsa Operate handle.
  132. */
  133. void csi_rsa_detach_callback(csi_rsa_t *rsa);
  134. /**
  135. \brief Generate rsa key pair.
  136. \param[in] rsa RSA handle to operate.
  137. \param[out] context Pointer to the rsa context
  138. \return Error code \ref csi_error_t
  139. */
  140. csi_error_t csi_rsa_gen_key(csi_rsa_t *rsa, csi_rsa_context_t *context);
  141. /**
  142. \brief Encrypt
  143. \param[in] rsa RSA handle to operate.
  144. \param[in] context Pointer to the rsa context
  145. \param[in] src Pointer to the source data.
  146. \param[in] src_size The source data len
  147. \param[out] out Pointer to the result buffer
  148. \return Error code \ref csi_error_t
  149. */
  150. csi_error_t csi_rsa_encrypt(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, 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 Error code \ref csi_error_t
  160. */
  161. csi_error_t csi_rsa_decrypt(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *out, uint32_t *out_size);
  162. /**
  163. \brief RSA sign
  164. \param[in] rsa RSA handle to operate.
  165. \param[in] context Pointer to the rsa context
  166. \param[in] src Pointer to the source data.
  167. \param[in] src_size The source data len
  168. \param[out] signature Pointer to the signature
  169. \param[in] hash_type The source data hash type
  170. \return Error code \ref csi_error_t
  171. */
  172. csi_error_t csi_rsa_sign(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *signature, csi_rsa_hash_type_t hash_type);
  173. /**
  174. \brief RSA verify
  175. \param[in] rsa RSA handle to operate.
  176. \param[in] context Pointer to the rsa context
  177. \param[in] src Pointer to the source data.
  178. \param[in] src_size The source data len
  179. \param[in] signature Pointer to the signature
  180. \param[in] sig_size The signature size
  181. \param[in] hash_type The source data hash type
  182. \return Verify result
  183. */
  184. bool csi_rsa_verify(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *signature, uint32_t sig_size, csi_rsa_hash_type_t hash_type);
  185. /**
  186. \brief encrypt(async mode)
  187. \param[in] rsa RSA handle to operate.
  188. \param[in] context Pointer to the rsa context
  189. \param[in] src Pointer to the source data.
  190. \param[in] src_size The source data len
  191. \param[out] out Pointer to the result buffer
  192. \return Error code \ref csi_error_t
  193. */
  194. csi_error_t csi_rsa_encrypt_async(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *out);
  195. /**
  196. \brief decrypt(async mode)
  197. \param[in] rsa RSA handle to operate.
  198. \param[in] context Pointer to the rsa context
  199. \param[in] src Pointer to the source data.
  200. \param[in] src_size The source data len
  201. \param[out] out Pointer to the result buffer
  202. \param[out] out_size The result size
  203. \return Error code \ref csi_error_t
  204. */
  205. csi_error_t csi_rsa_decrypt_async(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *out, uint32_t *out_size);
  206. /**
  207. \brief RSA sign(async mode)
  208. \param[in] rsa RSA handle to operate.
  209. \param[in] context Pointer to the rsa context
  210. \param[in] src Pointer to the source data.
  211. \param[in] src_size The source data len
  212. \param[out] signature Pointer to the signature
  213. \param[in] hash_type The source data hash type
  214. \return Error code \ref csi_error_t
  215. */
  216. csi_error_t csi_rsa_sign_async(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *signature, csi_rsa_hash_type_t hash_type);
  217. /**
  218. \brief RSA verify(async mode)
  219. \param[in] rsa RSA handle to operate.
  220. \param[in] context Pointer to the rsa context
  221. \param[in] src Pointer to the source data.
  222. \param[in] src_size The source data len
  223. \param[in] signature Pointer to the signature
  224. \param[in] sig_size The signature size
  225. \param[in] hash_type The source data hash type
  226. \return Verify result
  227. */
  228. csi_error_t csi_rsa_verify_async(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *signature, uint32_t sig_size, csi_rsa_hash_type_t hash_type);
  229. /**
  230. \brief Get RSA state.
  231. \param[in] rsa RSA handle to operate.
  232. \param[out] state RSA state \ref csi_rsa_state_t.
  233. \return Error code \ref csi_error_t
  234. */
  235. csi_error_t csi_rsa_get_state(csi_rsa_t *rsa, csi_rsa_state_t *state);
  236. /**
  237. \brief Get big prime data
  238. \param[in] rsa RSA handle to operate.
  239. \param[in] p Pointer to the prime
  240. \param[in] bit_length Pointer to the prime bit length
  241. \return Error code \ref csi_error_t
  242. */
  243. csi_error_t csi_rsa_get_prime(csi_rsa_t *rsa, void *p, uint32_t bit_length);
  244. /**
  245. \brief Enable rsa power manage
  246. \param[in] rsa RSA handle to operate.
  247. \return Error code \ref csi_error_t
  248. */
  249. csi_error_t csi_rsa_enable_pm(csi_rsa_t *rsa);
  250. /**
  251. \brief Disable rsa power manage
  252. \param[in] rsa RSA handle to operate.
  253. */
  254. void csi_rsa_disable_pm(csi_rsa_t *rsa);
  255. /**
  256. \brief Get publickey by p q prime data
  257. \param[in] rsa rsa handle to operate.
  258. \param[in] p Pointer to the prime p
  259. \param[in] p_byte_len Pointer to the prime p byte length
  260. \param[in] q Pointer to the prime q
  261. \param[in] q_byte_len Pointer to the prime q byte length
  262. \param[in] out Pointer to the publickey
  263. \param[in] keybits_len Pointer to the publickey bits length
  264. \return \ref csi_error_t
  265. */
  266. csi_error_t csi_rsa_get_publickey(csi_rsa_t *rsa, void *p, uint32_t p_byte_len, void *q, uint32_t q_byte_len, void *out, csi_rsa_key_bits_t keybits_len);
  267. /**
  268. \brief Generation rsa keyparis
  269. \param[in] rsa rsa handle to operate.
  270. \param[in] context Pointer to the rsa context
  271. \param[in] keybits_len Pointer to the publickey bits length
  272. \return \ref csi_error_t
  273. */
  274. csi_error_t csi_rsa_gen_keypairs(csi_rsa_t *rsa, csi_rsa_context_t *context, csi_rsa_key_bits_t keybits_len);
  275. void csi_rsa_set_ignore_decrypt_error(bool checked);
  276. #ifdef __cplusplus
  277. }
  278. #endif
  279. #endif /* _DRV_RSA_H_ */