1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /******************************************************************************
20  * @file       drv/aes.h
21  * @brief      Header File for AES Driver
22  * @version    V1.0
23  * @date       9. Oct 2020
24  * @model      aes
25  ******************************************************************************/
26 
27 #ifndef _DRV_AES_H_
28 #define _DRV_AES_H_
29 
30 #include <stdint.h>
31 #include <drv/common.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /*----- Encrypt & Decrypt: Config key length -----*/
38 typedef enum {
39     AES_KEY_LEN_BITS_128        = 0,       /* 128 Data bits */
40     AES_KEY_LEN_BITS_192,                  /* 192 Data bits */
41     AES_KEY_LEN_BITS_256                   /* 256 Data bits */
42 } csi_aes_key_bits_t;
43 
44 /**
45 \brief AES Ctrl Block
46 */
47 typedef struct {
48     csi_dev_t               dev;
49     void                    *priv;
50 } csi_aes_t;
51 
52 /**
53   \brief       Initialize AES interface. Initializes the resources needed for the AES interface
54   \param[in]   aes    Handle to operate
55   \param[in]   idx    Device id
56   \return      Error code \ref csi_error_t
57 */
58 csi_error_t csi_aes_init(csi_aes_t *aes, uint32_t idx);
59 
60 /**
61   \brief       De-initialize AES interface. Stops operation and releases the software resources used by the interface
62   \param[in]   aes    Dandle to operate
63   \return      None
64 */
65 void csi_aes_uninit(csi_aes_t *aes);
66 
67 /**
68   \brief       Set encrypt key
69   \param[in]   aes        Handle to operate
70   \param[in]   key        Pointer to the key buf
71   \param[in]   key_len    Pointer to \ref csi_aes_key_bits_t
72   \return      Error code \ref Csi_error_t
73 */
74 csi_error_t csi_aes_set_encrypt_key(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len);
75 
76 /**
77   \brief       Set decrypt key
78   \param[in]   aes        Handle to operate
79   \param[in]   key        Pointer to the key buf
80   \param[in]   key_len    Pointer to \ref csi_aes_key_bits_t
81   \return      Error code \ref Csi_error_t
82 */
83 csi_error_t csi_aes_set_decrypt_key(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len);
84 /**
85   \brief       Set encrypt key2. This API is used for the algorithm which has two keys,
86                 such as xts, used for the key of tweak
87   \param[in]   aes        Handle to operate
88   \param[in]   key        Pointer to the key buf
89   \param[in]   key_len    Pointer to \ref csi_aes_key_bits_t
90   \return      Error code \ref Csi_error_t
91 */
92 csi_error_t csi_aes_set_encrypt_key2(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len);
93 
94 /**
95   \brief       Set decrypt key2. This API is used for the algorithm which has two keys,
96                 such as xts, used for the key of tweak
97   \param[in]   aes        Handle to operate
98   \param[in]   key        Pointer to the key buf
99   \param[in]   key_len    Pointer to \ref csi_aes_key_bits_t
100   \return      Error code \ref Csi_error_t
101 */
102 csi_error_t csi_aes_set_decrypt_key2(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len);
103 
104 /**
105   \brief       AES ecb encrypt
106   \param[in]   aes     Handle to operate
107   \param[in]   in      Pointer to the source data
108   \param[out]  out     Pointer to the result data
109   \param[in]   size    The source data size
110   \return      Error code \ref Csi_error_t
111 */
112 csi_error_t csi_aes_ecb_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size);
113 
114 /**
115   \brief       AES ecb decrypt
116   \param[in]   aes     Handle to operate
117   \param[in]   in      Pointer to the source data
118   \param[out]  out     Pointer to the result data
119   \param[in]   size    The source data size
120   \return      Error code \ref Csi_error_t
121 */
122 csi_error_t csi_aes_ecb_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size);
123 
124 /**
125   \brief       AES cbc encrypt
126   \param[in]   aes     Handle to operate
127   \param[in]   in      Pointer to the source data
128   \param[out]  out     Pointer to the result data
129   \param[in]   size    The source data size
130   \param[in]   iv      Init vector
131   \return      Error code \ref Csi_error_t
132 */
133 csi_error_t csi_aes_cbc_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
134 
135 /**
136   \brief       AES cbc decrypt
137   \param[in]   aes     Handle to operate
138   \param[in]   in      Pointer to the source data
139   \param[out]  out     Pointer to the result data
140   \param[in]   size    The source data size
141   \param[in]   iv      Init vector
142   \return      Error code \ref Csi_error_t
143 */
144 csi_error_t csi_aes_cbc_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
145 
146 /**
147   \brief       AES cfb1 encrypt
148   \param[in]   aes     Handle to operate
149   \param[in]   in      Pointer to the source data
150   \param[out]  out     Pointer to the result data
151   \param[in]   size    The source data size
152   \param[in]   iv      Init vector
153   \return      Error code \ref Csi_error_t
154 */
155 csi_error_t csi_aes_cfb1_encrypt(csi_aes_t *aes, void *in, void *out,  uint32_t size, void *iv);
156 
157 /**
158   \brief       AES cfb1 decrypt
159   \param[in]   aes     Handle to operate
160   \param[in]   in      Pointer to the source data
161   \param[out]  out     Pointer to the result data
162   \param[in]   size    The source data size
163   \param[in]   iv      Init vector
164   \return      Error code \ref Csi_error_t
165 */
166 csi_error_t csi_aes_cfb1_decrypt(csi_aes_t *aes, void *in, void *out,  uint32_t size, void *iv);
167 
168 /**
169   \brief       AES cfb8 encrypt
170   \param[in]   aes     Handle to operate
171   \param[in]   in      Pointer to the source data
172   \param[out]  out     Pointer to the result data
173   \param[in]   size    The source data size
174   \param[in]   iv      Init vector
175   \return      Error code \ref Csi_error_t
176 */
177 csi_error_t csi_aes_cfb8_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
178 
179 /**
180   \brief       AES cfb8 decrypt
181   \param[in]   aes     Handle to operate
182   \param[in]   in      Pointer to the source data
183   \param[out]  out     Pointer to the result data
184   \param[in]   size    The source data size
185   \param[in]   iv      Init vector
186   \return      Error code \ref Csi_error_t
187 */
188 csi_error_t csi_aes_cfb8_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
189 
190 /**
191   \brief       AES cfb128 decrypt
192   \param[in]   aes     Handle to operate
193   \param[in]   in      Pointer to the source data
194   \param[out]  out     Pointer to the result data
195   \param[in]   size    The source data size
196   \param[in]   iv      Init vector
197   \param[out]  num     The number of the 128-bit block we have used
198   \return      Error code \ref csi_error_t
199 */
200 csi_error_t csi_aes_cfb128_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv, uint32_t *num);
201 
202 /**
203   \brief       AES cfb128 encrypt
204   \param[in]   aes     Handle to operate
205   \param[in]   in      Pointer to the source data
206   \param[out]  out     Pointer to the result data
207   \param[in]   size    The source data size
208   \param[in]   iv      Init vector
209   \param[out]  num     The number of the 128-bit block we have used
210   \return      Error code \ref csi_error_t
211 */
212 csi_error_t csi_aes_cfb128_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv, uint32_t *num);
213 
214 /**
215   \brief       AES ofb encrypt
216   \param[in]   aes     Handle to operate
217   \param[in]   in      Pointer to the source data
218   \param[out]  out     Pointer to the result data
219   \param[in]   size    The source data size
220   \param[in]   iv      Init vector
221   \param[out]  num     The number of the 128-bit block we have used
222   \return      Error code \ref csi_error_t
223 */
224 csi_error_t csi_aes_ofb_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv, uint32_t *num);
225 
226 /**
227   \brief       AES ofb decrypt
228   \param[in]   aes     Handle to operate
229   \param[in]   in      Pointer to the source data
230   \param[out]  out     Pointer to the result data
231   \param[in]   size    The source data size
232   \param[in]   iv      Init vector
233   \param[out]  num     The number of the 128-bit block we have used
234   \return      Error code \ref csi_error_t
235 */
236 csi_error_t csi_aes_ofb_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv, uint32_t *num);
237 
238 /**
239   \brief       AES ctr encrypt
240   \param[in]   aes              Handle to operate
241   \param[in]   in               Pointer to the source data
242   \param[out]  out              Pointer to the result data
243   \param[in]   size             The source data size
244   \param[in]   iv               Init vector
245   \return      Error code \ref csi_error_t
246 */
247 csi_error_t csi_aes_ctr_encrypt(csi_aes_t *aes, void *in,void *out, uint32_t size, void *iv);
248 
249 /**
250   \brief       AES ctr decrypt
251   \param[in]   aes              Handle to operate
252   \param[in]   in               Pointer to the source data
253   \param[out]  out              Pointer to the result data
254   \param[in]   size             The source data size
255   \param[in]   iv               Init vecotr
256   \return      Error code \ref csi_error_t
257 */
258 csi_error_t csi_aes_ctr_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
259 
260 /**
261   \brief       AES cts encrypt
262   \param[in]   aes              Handle to operate
263   \param[in]   in               Pointer to the source data
264   \param[out]  out              Pointer to the result data
265   \param[in]   size             The source data size
266   \param[in]   iv               Init vector
267   \return      Error code \ref csi_error_t
268 */
269 csi_error_t csi_aes_cts_encrypt(csi_aes_t *aes, void *in,void *out, uint32_t size, void *iv);
270 
271 /**
272   \brief       AES cts decrypt
273   \param[in]   aes              Handle to operate
274   \param[in]   in               Pointer to the source data
275   \param[out]  out              Pointer to the result data
276   \param[in]   size             The source data size
277   \param[in]   iv               Init vecotr
278   \return      Error code \ref csi_error_t
279 */
280 csi_error_t csi_aes_cts_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
281 
282 
283 /**
284   \brief       AES xts encrypt
285   \param[in]   aes              Handle to operate
286   \param[in]   in               Pointer to the source data
287   \param[out]  out              Pointer to the result data
288   \param[in]   size             The source data size
289   \param[in]   iv               Init vector
290   \return      Error code \ref csi_error_t
291 */
292 csi_error_t csi_aes_xts_encrypt(csi_aes_t *aes, void *in,void *out, uint32_t size, void *iv);
293 
294 /**
295   \brief       AES xts decrypt
296   \param[in]   aes              Handle to operate
297   \param[in]   in               Pointer to the source data
298   \param[out]  out              Pointer to the result data
299   \param[in]   size             The source data size
300   \param[in]   iv               Init vecotr
301   \return      Error code \ref csi_error_t
302 */
303 csi_error_t csi_aes_xts_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
304 
305 #ifdef __cplusplus
306 }
307 #endif
308 
309 #endif /* _DRV_AES_H_ */
310