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 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #include <stdint.h>
30 #include <drv_common.h>
31 
32 
33 /// definition for rsa handle.
34 typedef void *rsa_handle_t;
35 
36 /****** RSA specific error codes *****/
37 typedef enum {
38     RSA_ERROR_DATA_BITS                     ,     ///< Specified number of Data bits not supported
39     RSA_ERROR_ENDIAN                              ///< Specified endian not supported
40 } drv_rsa_error_e;
41 
42 /*----- RSA Control Codes: Mode Parameters: Data Bits -----*/
43 typedef enum {
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
49 } rsa_data_bits_e;
50 
51 /*----- RSA Control Codes: Mode Parameters: Endian -----*/
52 typedef enum {
53     RSA_ENDIAN_MODE_LITTLE        = 0,  ///< RSA Little Endian Mode
54     RSA_ENDIAN_MODE_BIG                 ///< RSA Big Endian Mode
55 } rsa_endian_mode_e;
56 
57 typedef enum {
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
64 } rsa_padding_type_e;
65 
66 typedef enum {
67     RSA_HASH_TYPE_MD5            = 0,
68     RSA_HASH_TYPE_SHA1               ,
69     RSA_HASH_TYPE_SHA224             ,
70     RSA_HASH_TYPE_SHA256             ,
71     RSA_HASH_TYPE_SHA384             ,
72     RSA_HASH_TYPE_SHA512
73 } rsa_hash_type_e;
74 
75 /*----- RSA Control Codes: Mode Parameters: Padding mode -----*/
76 typedef struct {
77     rsa_padding_type_e padding_type;
78     rsa_hash_type_e    hash_type;
79 } rsa_padding_t;
80 
81 /**
82 \brief RSA Status
83 */
84 typedef struct {
85     uint32_t busy             : 1;        ///< Calculate busy flag
86 } rsa_status_t;
87 
88 /****** RSA Event *****/
89 typedef enum {
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
94 } rsa_event_e;
95 
96 typedef void (*rsa_event_cb_t)(rsa_event_e event);   ///< Pointer to \ref rsa_event_cb_t : RSA Event call back.
97 
98 
99 /**
100 \brief RSA Device Driver Capabilities.
101 */
102 typedef struct {
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;
109 
110 
111 // Function documentation
112 
113 /**
114   \brief       get rsa handle count.
115   \return      rsa handle count
116 */
117 int32_t csi_rsa_get_instance_count(void);
118 
119 /**
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
124 */
125 rsa_handle_t csi_rsa_initialize(int32_t idx, rsa_event_cb_t cb_event);
126 
127 /**
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.
130   \return      error code
131 */
132 int32_t csi_rsa_uninitialize(rsa_handle_t handle);
133 
134 /**
135   \brief       Get driver capabilities.
136   \param[in]   handle rsa handle to operate.
137   \return      \ref rsa_capabilities_t
138 */
139 rsa_capabilities_t csi_rsa_get_capabilities(rsa_handle_t handle);
140 
141 /**
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
146   \return      error code
147 */
148 int32_t csi_rsa_config(rsa_handle_t handle,
149                        rsa_data_bits_e data_bits,
150                        rsa_endian_mode_e endian
151                       );
152 
153 /**
154   \brief       encrypt
155   \param[in]   handle  rsa handle to operate.
156   \param[in]   n         Pointer to the public modulus
157   \param[in]   e         Pointer to the public exponent
158   \param[in]   src       Pointer to the source data.
159   \param[in]   src_size  the source data len
160   \param[out]  out       Pointer to the result buffer
161   \param[out]  out_size  the result size
162   \param[in]   padding   \ref  rsa_padding_t
163   \return      error code
164 */
165 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);
166 
167 
168 /**
169   \brief       decrypt
170   \param[in]   handle  rsa handle to operate.
171   \param[in]   n         Pointer to the public modulus
172   \param[in]   d         Pointer to the privte exponent
173   \param[in]   src       Pointer to the source data.
174   \param[in]   src_size  the source data len
175   \param[out]  out       Pointer to the result buffer
176   \param[out]  out_size  the result size
177   \param[in]   padding   \ref rsa_padding_t
178   \return      error code
179 */
180 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);
181 
182 /**
183   \brief       rsa sign
184   \param[in]   handle  rsa handle to operate.
185   \param[in]   n         Pointer to the public modulus
186   \param[in]   d         Pointer to the privte exponent
187   \param[in]   src       Pointer to the source data.
188   \param[in]   src_size  the source data len
189   \param[out]  signature Pointer to the signature
190   \param[out]  sig_size  the signature size
191   \param[in]   padding   \ref rsa_padding_t
192   \return      error code
193 */
194 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);
195 
196 /**
197   \brief       rsa verify
198   \param[in]   handle  rsa handle to operate.
199   \param[in]   n         Pointer to the public modulus
200   \param[in]   e         Pointer to the public exponent
201   \param[in]   src       Pointer to the source data.
202   \param[in]   src_size  the source data len
203   \param[in]   signature Pointer to the signature
204   \param[in]   sig_size  the signature size
205   \param[out]  result    Pointer to the result
206   \param[in]   padding   \ref rsa_padding_t
207   \return      error code
208 */
209 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);
210 /**
211   \brief       Get RSA status.
212   \param[in]   handle  rsa handle to operate.
213   \return      RSA status \ref rsa_status_t
214 */
215 rsa_status_t csi_rsa_get_status(rsa_handle_t handle);
216 
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 #endif /* _CSI_RSA_H_ */
223