]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/T-HEAD_CB2201_CDK/csi/csi_driver/include/drv_rsa.h
Introduce a port for T-HEAD CK802. A simple demo for T-HEAD CB2201 is also included.
[freertos] / FreeRTOS / Demo / T-HEAD_CB2201_CDK / csi / csi_driver / include / drv_rsa.h
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                        void *arg
152                       );
153
154 /**
155   \brief       encrypt
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
164   \return      error code
165 */
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);
167
168
169 /**
170   \brief       decrypt
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
179   \return      error code
180 */
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);
182
183 /**
184   \brief       rsa sign
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
193   \return      error code
194 */
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);
196
197 /**
198   \brief       rsa verify
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
208   \return      error code
209 */
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);
211 /**
212   \brief       Get RSA status.
213   \param[in]   handle  rsa handle to operate.
214   \return      RSA status \ref rsa_status_t
215 */
216 rsa_status_t csi_rsa_get_status(rsa_handle_t handle);
217
218
219 #ifdef __cplusplus
220 }
221 #endif
222
223 #endif /* _CSI_RSA_H_ */