drv_rsa.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file drv_rsa.h
  18. * @brief header file for rsa driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #ifndef _CSI_RSA_H_
  23. #define _CSI_RSA_H_
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stdint.h>
  28. #include <drv_common.h>
  29. /// definition for rsa handle.
  30. typedef void *rsa_handle_t;
  31. /****** RSA specific error codes *****/
  32. typedef enum {
  33. RSA_ERROR_DATA_BITS , ///< Specified number of Data bits not supported
  34. RSA_ERROR_ENDIAN ///< Specified endian not supported
  35. } drv_rsa_error_e;
  36. /*----- RSA Control Codes: Mode Parameters: Data Bits -----*/
  37. typedef enum {
  38. RSA_DATA_BITS_192 = 0, ///< 192 Data bits
  39. RSA_DATA_BITS_256 , ///< 256 Data bits
  40. RSA_DATA_BITS_512 , ///< 512 Data bits
  41. RSA_DATA_BITS_1024 , ///< 1024 Data bits (default)
  42. RSA_DATA_BITS_2048 ///< 2048 Data bits
  43. } rsa_data_bits_e;
  44. /*----- RSA Control Codes: Mode Parameters: Endian -----*/
  45. typedef enum {
  46. RSA_ENDIAN_MODE_LITTLE = 0, ///< RSA Little Endian Mode
  47. RSA_ENDIAN_MODE_BIG ///< RSA Big Endian Mode
  48. } rsa_endian_mode_e;
  49. typedef enum {
  50. RSA_PADDING_MODE_PKCS1 = 1, ///< RSA PKCS1 Padding Mode
  51. RSA_PADDING_MODE_NO , ///< RSA NO Padding Mode
  52. RSA_PADDING_MODE_SSLV23 , ///< RSA SSLV23 Padding Mode
  53. RSA_PADDING_MODE_PKCS1_OAEP , ///< RSA PKCS1 OAEP Padding Mode
  54. RSA_PADDING_MODE_X931 , ///< RSA X931 Padding Mode
  55. RSA_PADDING_MODE_PSS ///< RSA PSS Padding Mode
  56. } rsa_padding_type_e;
  57. typedef enum {
  58. RSA_HASH_TYPE_MD5 = 0,
  59. RSA_HASH_TYPE_SHA1 ,
  60. RSA_HASH_TYPE_SHA224 ,
  61. RSA_HASH_TYPE_SHA256 ,
  62. RSA_HASH_TYPE_SHA384 ,
  63. RSA_HASH_TYPE_SHA512
  64. } rsa_hash_type_e;
  65. /*----- RSA Control Codes: Mode Parameters: Padding mode -----*/
  66. typedef struct {
  67. rsa_padding_type_e padding_type;
  68. rsa_hash_type_e hash_type;
  69. } rsa_padding_t;
  70. /**
  71. \brief RSA Status
  72. */
  73. typedef struct {
  74. uint32_t busy : 1; ///< Calculate busy flag
  75. } rsa_status_t;
  76. /****** RSA Event *****/
  77. typedef enum {
  78. RSA_EVENT_ENCRYPT_COMPLETE = 0, ///< Encrypt completed
  79. RSA_EVENT_DECRYPT_COMPLETE , ///< Decrypt completed
  80. RSA_EVENT_SIGN_COMPLETE , ///< Sign completed
  81. RSA_EVENT_VERIFY_COMPLETE , ///< Verify completed
  82. } rsa_event_e;
  83. typedef void (*rsa_event_cb_t)(rsa_event_e event); ///< Pointer to \ref rsa_event_cb_t : RSA Event call back.
  84. /**
  85. \brief RSA Device Driver Capabilities.
  86. */
  87. typedef struct {
  88. uint32_t bits_192 : 1; ///< supports 192bits modular length
  89. uint32_t bits_256 : 1; ///< supports 256bits modular length
  90. uint32_t bits_512 : 1; ///< supports 512bits modular length
  91. uint32_t bits_1024 : 1; ///< supports 1024bits modular length
  92. uint32_t bits_2048 : 1; ///< supports 2048bits modular length
  93. } rsa_capabilities_t;
  94. // Function documentation
  95. /**
  96. \brief get rsa handle count.
  97. \return rsa handle count
  98. */
  99. int32_t csi_rsa_get_instance_count(void);
  100. /**
  101. \brief Initialize RSA Interface. 1. Initializes the resources needed for the RSA interface 2.registers event callback function
  102. \param[in] idx must not exceed return value of csi_rsa_get_instance_count()
  103. \param[in] cb_event Pointer to \ref rsa_event_cb_t
  104. \return pointer to rsa handle
  105. */
  106. rsa_handle_t csi_rsa_initialize(int32_t idx, rsa_event_cb_t cb_event);
  107. /**
  108. \brief De-initialize RSA Interface. stops operation and releases the software resources used by the interface
  109. \param[in] handle rsa handle to operate.
  110. \return error code
  111. */
  112. int32_t csi_rsa_uninitialize(rsa_handle_t handle);
  113. /**
  114. \brief Get driver capabilities.
  115. \param[in] handle rsa handle to operate.
  116. \return \ref rsa_capabilities_t
  117. */
  118. rsa_capabilities_t csi_rsa_get_capabilities(rsa_handle_t handle);
  119. /**
  120. \brief config rsa mode.
  121. \param[in] handle rsa handle to operate.
  122. \param[in] data_bits \ref rsa_data_bits_e
  123. \param[in] endian \ref rsa_endian_mode_e
  124. \return error code
  125. */
  126. int32_t csi_rsa_config(rsa_handle_t handle,
  127. rsa_data_bits_e data_bits,
  128. rsa_endian_mode_e endian
  129. );
  130. /**
  131. \brief encrypt
  132. \param[in] handle rsa handle to operate.
  133. \param[in] n Pointer to the public modulus
  134. \param[in] e Pointer to the public exponent
  135. \param[in] src Pointer to the source data.
  136. \param[in] src_size the source data len
  137. \param[out] out Pointer to the result buffer
  138. \param[out] out_size the result size
  139. \param[in] padding \ref rsa_padding_t
  140. \return error code
  141. */
  142. int32_t csi_rsa_encrypt(rsa_handle_t handle, void *n, void *e, void *src, int32_t src_size, void *out, uint32_t *out_size, rsa_padding_t padding);
  143. /**
  144. \brief decrypt
  145. \param[in] handle rsa handle to operate.
  146. \param[in] n Pointer to the public modulus
  147. \param[in] d Pointer to the privte exponent
  148. \param[in] src Pointer to the source data.
  149. \param[in] src_size the source data len
  150. \param[out] out Pointer to the result buffer
  151. \param[out] out_size the result size
  152. \param[in] padding \ref rsa_padding_t
  153. \return error code
  154. */
  155. int32_t csi_rsa_decrypt(rsa_handle_t handle, void *n, void *d, void *src, uint32_t src_size, void *out, uint32_t *out_size, rsa_padding_t padding);
  156. /**
  157. \brief rsa sign
  158. \param[in] handle rsa handle to operate.
  159. \param[in] n Pointer to the public modulus
  160. \param[in] d Pointer to the privte exponent
  161. \param[in] src Pointer to the source data.
  162. \param[in] src_size the source data len
  163. \param[out] signature Pointer to the signature
  164. \param[out] sig_size the signature size
  165. \param[in] padding \ref rsa_padding_t
  166. \return error code
  167. */
  168. int32_t csi_rsa_sign(rsa_handle_t handle, void *n, void *d, void *src, uint32_t src_size, void *signature, void *sig_size, rsa_padding_t padding);
  169. /**
  170. \brief rsa verify
  171. \param[in] handle rsa handle to operate.
  172. \param[in] n Pointer to the public modulus
  173. \param[in] e Pointer to the public exponent
  174. \param[in] src Pointer to the source data.
  175. \param[in] src_size the source data len
  176. \param[in] signature Pointer to the signature
  177. \param[in] sig_size the signature size
  178. \param[out] result Pointer to the result
  179. \param[in] padding \ref rsa_padding_t
  180. \return error code
  181. */
  182. int32_t csi_rsa_verify(rsa_handle_t handle, void *n, void *e, void *src, uint32_t src_size, void *signature, uint32_t sig_size, void *result, rsa_padding_t padding);
  183. /**
  184. \brief Get RSA status.
  185. \param[in] handle rsa handle to operate.
  186. \return RSA status \ref rsa_status_t
  187. */
  188. rsa_status_t csi_rsa_get_status(rsa_handle_t handle);
  189. #ifdef __cplusplus
  190. }
  191. #endif
  192. #endif /* _CSI_RSA_H_ */