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 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #include <stdint.h>
19 #include <drv/common.h>
20 
21 /*----- RSA Control Codes: Mode Parameters: Key Bits -----*/
22 typedef enum {
23     RSA_KEY_BITS_192             = 0,  ///< 192 Key bits
24     RSA_KEY_BITS_256,                  ///< 256 Key bits
25     RSA_KEY_BITS_512,                  ///< 512 Key bits
26     RSA_KEY_BITS_1024,                 ///< 1024 Key bits
27     RSA_KEY_BITS_2048,                 ///< 2048 Key bits
28     RSA_KEY_BITS_3072,                 ///< 3072 Key bits
29     RSA_KEY_BITS_4096                  ///< 4096 Key bits
30 } csi_rsa_key_bits_t;
31 
32 typedef enum {
33     RSA_PADDING_MODE_NO           = 0, ///< RSA NO Padding Mode
34     RSA_PADDING_MODE_PKCS1,            ///< RSA PKCS1 Padding Mode
35     RSA_PADDING_MODE_PKCS1_OAEP,       ///< RSA PKCS1 OAEP Padding Mode
36     RSA_PADDING_MODE_SSLV23,           ///< RSA SSLV23 Padding Mode
37     RSA_PADDING_MODE_X931,             ///< RSA X931 Padding Mode
38     RSA_PADDING_MODE_PSS               ///< RSA PSS Padding Mode
39 } csi_rsa_padding_type_t;
40 
41 typedef enum {
42     RSA_HASH_TYPE_MD5            = 0,
43     RSA_HASH_TYPE_SHA1,
44     RSA_HASH_TYPE_SHA224,
45     RSA_HASH_TYPE_SHA256,
46     RSA_HASH_TYPE_SHA384,
47     RSA_HASH_TYPE_SHA512
48 } csi_rsa_hash_type_t;
49 
50 typedef struct {
51     void *n;                                ///< Pointer to the public modulus
52     void *e;                                ///< Pointer to the public exponent
53     void *d;                                ///< Pointer to the private exponent
54     csi_rsa_key_bits_t  key_bits;           ///< RSA KEY BITS
55     csi_rsa_padding_type_t padding_type;    ///< RSA PADDING TYPE
56 } csi_rsa_context_t;
57 
58 /**
59 \brief RSA State
60 */
61 typedef struct {
62     uint8_t busy             : 1;        ///< Calculate busy flag
63     uint8_t error            : 1;        ///< Calculate error flag
64 } csi_rsa_state_t;
65 
66 typedef struct {
67     csi_dev_t           dev;
68     void                *cb;
69     void                *arg;
70     csi_rsa_state_t     state;
71     void                *prim;
72 } csi_rsa_t;
73 
74 typedef struct {
75   uint32_t pout[64];
76   uint8_t *pouts;
77   uint32_t *pout_size;
78   uint32_t u32keywords;
79   uint8_t *pdst;
80   uint32_t u32padding;
81   uint32_t u32dst_words;
82   uint32_t u32type;
83   uint32_t rsa_state;
84 }rsa_middle_t;
85 
86 /****** RSA Event *****/
87 typedef enum {
88     RSA_EVENT_COMPLETE    = 0,   ///< rsa event completed
89     RSA_EVENT_VERIFY_SUCCESS,
90     RSA_EVENT_VERIFY_FAILED,
91     RSA_EVENT_ERROR,             ///< error event
92 } csi_rsa_event_t;
93 
94 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.
95 
96 /**
97   \brief       Initialize RSA Interface. 1. Initializes the resources needed for the RSA interface 2.registers event callback function
98   \param[in]   rsa  RSA handle to operate.
99   \param[in]   idx  Device id
100   \return      Error code \ref csi_error_t
101 */
102 csi_error_t csi_rsa_init(csi_rsa_t *rsa, uint32_t idx);
103 
104 /**
105   \brief       De-initialize RSA Interface. stops operation and releases the software resources used by the interface
106   \param[in]   rsa  RSA handle to operate.
107   \return      none
108 */
109 void csi_rsa_uninit(csi_rsa_t *rsa);
110 
111 /**
112   \brief       Attach the callback handler to RSA
113   \param[in]   rsa  Operate handle.
114   \param[in]   cb    Callback function
115   \param[in]   arg   User can define it by himself as callback's param
116   \return      Error code \ref csi_error_t
117 */
118 csi_error_t csi_rsa_attach_callback(csi_rsa_t *rsa, csi_rsa_callback_t cb, void *arg);
119 
120 /**
121   \brief       Detach the callback handler
122   \param[in]   rsa  Operate handle.
123 */
124 void csi_rsa_detach_callback(csi_rsa_t *rsa);
125 
126 /**
127   \brief       Generate rsa key pair.
128   \param[in]   rsa       RSA handle to operate.
129   \param[out]  context   Pointer to the rsa context
130   \return      Error code \ref csi_error_t
131 */
132 csi_error_t csi_rsa_gen_key(csi_rsa_t *rsa, csi_rsa_context_t *context);
133 
134 /**
135   \brief       Encrypt
136   \param[in]   rsa       RSA handle to operate.
137   \param[in]   context   Pointer to the rsa context
138   \param[in]   src       Pointer to the source data.
139   \param[in]   src_size  The source data len
140   \param[out]  out       Pointer to the result buffer
141   \return      Error code \ref csi_error_t
142 */
143 csi_error_t csi_rsa_encrypt(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *out);
144 
145 /**
146   \brief       decrypt
147   \param[in]   rsa       RSA handle to operate.
148   \param[in]   context   Pointer to the rsa context
149   \param[in]   src       Pointer to the source data.
150   \param[in]   src_size  The source data len
151   \param[out]  out       Pointer to the result buffer
152   \param[out]  out_size  The result size
153   \return      Error code \ref csi_error_t
154 */
155 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);
156 
157 /**
158   \brief       RSA sign
159   \param[in]   rsa       RSA handle to operate.
160   \param[in]   context   Pointer to the rsa context
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[in]   hash_type The source data hash type
165   \return      Error code \ref csi_error_t
166 */
167 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);
168 
169 /**
170   \brief       RSA verify
171   \param[in]   rsa       RSA handle to operate.
172   \param[in]   context   Pointer to the rsa context
173   \param[in]   src       Pointer to the source data.
174   \param[in]   src_size  The source data len
175   \param[in]   signature Pointer to the signature
176   \param[in]   sig_size  The signature size
177   \param[in]   hash_type The source data hash type
178   \return      Verify result
179 */
180 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);
181 
182 /**
183   \brief       encrypt(async mode)
184   \param[in]   rsa       RSA handle to operate.
185   \param[in]   context   Pointer to the rsa context
186   \param[in]   src       Pointer to the source data.
187   \param[in]   src_size  The source data len
188   \param[out]  out       Pointer to the result buffer
189   \return      Error code \ref csi_error_t
190 */
191 csi_error_t csi_rsa_encrypt_async(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *out);
192 
193 /**
194   \brief       decrypt(async mode)
195   \param[in]   rsa       RSA handle to operate.
196   \param[in]   context   Pointer to the rsa context
197   \param[in]   src       Pointer to the source data.
198   \param[in]   src_size  The source data len
199   \param[out]  out       Pointer to the result buffer
200   \param[out]  out_size  The result size
201   \return      Error code \ref csi_error_t
202 */
203 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);
204 
205 /**
206   \brief       RSA sign(async mode)
207   \param[in]   rsa       RSA handle to operate.
208   \param[in]   context   Pointer to the rsa context
209   \param[in]   src       Pointer to the source data.
210   \param[in]   src_size  The source data len
211   \param[out]  signature Pointer to the signature
212   \param[in]   hash_type The source data hash type
213   \return      Error code \ref csi_error_t
214 */
215 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);
216 
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 /**
231   \brief       Get RSA state.
232   \param[in]   rsa      RSA handle to operate.
233   \param[out]  state    RSA state \ref csi_rsa_state_t.
234   \return      Error code \ref csi_error_t
235 */
236 csi_error_t csi_rsa_get_state(csi_rsa_t *rsa, csi_rsa_state_t *state);
237 
238 /**
239   \brief       Get big prime data
240   \param[in]   rsa          RSA handle to operate.
241   \param[in]   p            Pointer to the prime
242   \param[in]   bit_length   Pointer to the prime bit length
243   \return      Error code \ref csi_error_t
244 */
245 csi_error_t csi_rsa_get_prime(csi_rsa_t *rsa, void *p, uint32_t bit_length);
246 
247 /**
248   \brief       Enable rsa power manage
249   \param[in]   rsa  RSA handle to operate.
250   \return      Error code \ref csi_error_t
251 */
252 csi_error_t csi_rsa_enable_pm(csi_rsa_t *rsa);
253 
254 /**
255   \brief       Disable rsa power manage
256   \param[in]   rsa  RSA handle to operate.
257 */
258 void csi_rsa_disable_pm(csi_rsa_t *rsa);
259 
260 #ifdef __cplusplus
261 }
262 #endif
263 
264 #endif /* _DRV_RSA_H_ */
265