2 * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 /******************************************************************************
18 * @brief header file for rsa driver
21 ******************************************************************************/
30 #include <drv_common.h>
33 /// definition for rsa handle.
34 typedef void *rsa_handle_t;
36 /****** RSA specific error codes *****/
38 RSA_ERROR_DATA_BITS , ///< Specified number of Data bits not supported
39 RSA_ERROR_ENDIAN ///< Specified endian not supported
42 /*----- RSA Control Codes: Mode Parameters: Data Bits -----*/
44 RSA_DATA_BITS_192 = 0, ///< 192 Data bits
45 RSA_DATA_BITS_256 , ///< 256 Data bits
46 RSA_DATA_BITS_512 , ///< 512 Data bits
47 RSA_DATA_BITS_1024 , ///< 1024 Data bits (default)
48 RSA_DATA_BITS_2048 ///< 2048 Data bits
51 /*----- RSA Control Codes: Mode Parameters: Endian -----*/
53 RSA_ENDIAN_MODE_LITTLE = 0, ///< RSA Little Endian Mode
54 RSA_ENDIAN_MODE_BIG ///< RSA Big Endian Mode
58 RSA_PADDING_MODE_PKCS1 = 1, ///< RSA PKCS1 Padding Mode
59 RSA_PADDING_MODE_NO , ///< RSA NO Padding Mode
60 RSA_PADDING_MODE_SSLV23 , ///< RSA SSLV23 Padding Mode
61 RSA_PADDING_MODE_PKCS1_OAEP , ///< RSA PKCS1 OAEP Padding Mode
62 RSA_PADDING_MODE_X931 , ///< RSA X931 Padding Mode
63 RSA_PADDING_MODE_PSS ///< RSA PSS Padding Mode
67 RSA_HASH_TYPE_MD5 = 0,
69 RSA_HASH_TYPE_SHA224 ,
70 RSA_HASH_TYPE_SHA256 ,
71 RSA_HASH_TYPE_SHA384 ,
75 /*----- RSA Control Codes: Mode Parameters: Padding mode -----*/
77 rsa_padding_type_e padding_type;
78 rsa_hash_type_e hash_type;
85 uint32_t busy : 1; ///< Calculate busy flag
88 /****** RSA Event *****/
90 RSA_EVENT_ENCRYPT_COMPLETE = 0, ///< Encrypt completed
91 RSA_EVENT_DECRYPT_COMPLETE , ///< Decrypt completed
92 RSA_EVENT_SIGN_COMPLETE , ///< Sign completed
93 RSA_EVENT_VERIFY_COMPLETE , ///< Verify completed
96 typedef void (*rsa_event_cb_t)(rsa_event_e event); ///< Pointer to \ref rsa_event_cb_t : RSA Event call back.
100 \brief RSA Device Driver Capabilities.
103 uint32_t bits_192 : 1; ///< supports 192bits modular length
104 uint32_t bits_256 : 1; ///< supports 256bits modular length
105 uint32_t bits_512 : 1; ///< supports 512bits modular length
106 uint32_t bits_1024 : 1; ///< supports 1024bits modular length
107 uint32_t bits_2048 : 1; ///< supports 2048bits modular length
108 } rsa_capabilities_t;
111 // Function documentation
114 \brief get rsa handle count.
115 \return rsa handle count
117 int32_t csi_rsa_get_instance_count(void);
120 \brief Initialize RSA Interface. 1. Initializes the resources needed for the RSA interface 2.registers event callback function
121 \param[in] idx must not exceed return value of csi_rsa_get_instance_count()
122 \param[in] cb_event Pointer to \ref rsa_event_cb_t
123 \return pointer to rsa handle
125 rsa_handle_t csi_rsa_initialize(int32_t idx, rsa_event_cb_t cb_event);
128 \brief De-initialize RSA Interface. stops operation and releases the software resources used by the interface
129 \param[in] handle rsa handle to operate.
132 int32_t csi_rsa_uninitialize(rsa_handle_t handle);
135 \brief Get driver capabilities.
136 \param[in] handle rsa handle to operate.
137 \return \ref rsa_capabilities_t
139 rsa_capabilities_t csi_rsa_get_capabilities(rsa_handle_t handle);
142 \brief config rsa mode.
143 \param[in] handle rsa handle to operate.
144 \param[in] data_bits \ref rsa_data_bits_e
145 \param[in] endian \ref rsa_endian_mode_e
148 int32_t csi_rsa_config(rsa_handle_t handle,
149 rsa_data_bits_e data_bits,
150 rsa_endian_mode_e endian,
156 \param[in] handle rsa handle to operate.
157 \param[in] n Pointer to the public modulus
158 \param[in] e Pointer to the public exponent
159 \param[in] src Pointer to the source data.
160 \param[in] src_size the source data len
161 \param[out] out Pointer to the result buffer
162 \param[out] out_size the result size
163 \param[in] padding \ref rsa_padding_t
166 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);
171 \param[in] handle rsa handle to operate.
172 \param[in] n Pointer to the public modulus
173 \param[in] d Pointer to the privte exponent
174 \param[in] src Pointer to the source data.
175 \param[in] src_size the source data len
176 \param[out] out Pointer to the result buffer
177 \param[out] out_size the result size
178 \param[in] padding \ref rsa_padding_t
181 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);
185 \param[in] handle rsa handle to operate.
186 \param[in] n Pointer to the public modulus
187 \param[in] d Pointer to the privte exponent
188 \param[in] src Pointer to the source data.
189 \param[in] src_size the source data len
190 \param[out] signature Pointer to the signature
191 \param[out] sig_size the signature size
192 \param[in] padding \ref rsa_padding_t
195 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);
199 \param[in] handle rsa handle to operate.
200 \param[in] n Pointer to the public modulus
201 \param[in] e Pointer to the public exponent
202 \param[in] src Pointer to the source data.
203 \param[in] src_size the source data len
204 \param[in] signature Pointer to the signature
205 \param[in] sig_size the signature size
206 \param[out] result Pointer to the result
207 \param[in] padding \ref rsa_padding_t
210 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);
212 \brief Get RSA status.
213 \param[in] handle rsa handle to operate.
214 \return RSA status \ref rsa_status_t
216 rsa_status_t csi_rsa_get_status(rsa_handle_t handle);
223 #endif /* _CSI_RSA_H_ */