1 /*
2  * Copyright (C) 2017-2020 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file       drv/aes.h
7  * @brief      Header File for AES Driver
8  * @version    V1.0
9  * @date       9. Oct 2020
10  * @model      aes
11  ******************************************************************************/
12 
13 #ifndef _DRV_AES_H_
14 #define _DRV_AES_H_
15 
16 #include <stdint.h>
17 #include <drv/common.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /*----- Encrypt & Decrypt: Config key length -----*/
24 typedef enum {
25     AES_KEY_LEN_BITS_128        = 0,       ///< 128 Data bits
26     AES_KEY_LEN_BITS_192,                  ///< 192 Data bits
27     AES_KEY_LEN_BITS_256                   ///< 256 Data bits
28 } csi_aes_key_bits_t;
29 
30 /**
31 \brief AES Ctrl Block
32 */
33 typedef struct {
34     csi_dev_t               dev;
35     void                    *priv;
36 } csi_aes_t;
37 
38 /**
39   \brief       Initialize AES interface. Initializes the resources needed for the AES interface
40   \param[in]   aes    Handle to operate
41   \param[in]   idx    Device id
42   \return      Error code \ref csi_error_t
43 */
44 csi_error_t csi_aes_init(csi_aes_t *aes, uint32_t idx);
45 
46 /**
47   \brief       De-initialize AES interface. Stops operation and releases the software resources used by the interface
48   \param[in]   aes    Dandle to operate
49   \return      None
50 */
51 void csi_aes_uninit(csi_aes_t *aes);
52 
53 /**
54   \brief       Set encrypt key
55   \param[in]   aes        Handle to operate
56   \param[in]   key        Pointer to the key buf
57   \param[in]   key_len    Pointer to \ref csi_aes_key_bits_t
58   \return      Error code \ref Csi_error_t
59 */
60 csi_error_t csi_aes_set_encrypt_key(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len);
61 
62 /**
63   \brief       Set decrypt key
64   \param[in]   aes        Handle to operate
65   \param[in]   key        Pointer to the key buf
66   \param[in]   key_len    Pointer to \ref csi_aes_key_bits_t
67   \return      Error code \ref Csi_error_t
68 */
69 csi_error_t csi_aes_set_decrypt_key(csi_aes_t *aes, void *key, csi_aes_key_bits_t key_len);
70 
71 /**
72   \brief       AES ecb encrypt
73   \param[in]   aes     Handle to operate
74   \param[in]   in      Pointer to the source data
75   \param[out]  out     Pointer to the result data
76   \param[in]   size    The source data size
77   \return      Error code \ref Csi_error_t
78 */
79 csi_error_t csi_aes_ecb_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size);
80 
81 /**
82   \brief       AES ecb decrypt
83   \param[in]   aes     Handle to operate
84   \param[in]   in      Pointer to the source data
85   \param[out]  out     Pointer to the result data
86   \param[in]   size    The source data size
87   \return      Error code \ref Csi_error_t
88 */
89 csi_error_t csi_aes_ecb_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size);
90 
91 /**
92   \brief       AES cbc encrypt
93   \param[in]   aes     Handle to operate
94   \param[in]   in      Pointer to the source data
95   \param[out]  out     Pointer to the result data
96   \param[in]   size    The source data size
97   \param[in]   iv      Init vector
98   \return      Error code \ref Csi_error_t
99 */
100 csi_error_t csi_aes_cbc_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
101 
102 /**
103   \brief       AES cbc decrypt
104   \param[in]   aes     Handle to operate
105   \param[in]   in      Pointer to the source data
106   \param[out]  out     Pointer to the result data
107   \param[in]   size    The source data size
108   \param[in]   iv      Init vector
109   \return      Error code \ref Csi_error_t
110 */
111 csi_error_t csi_aes_cbc_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
112 
113 /**
114   \brief       AES cfb1 encrypt
115   \param[in]   aes     Handle to operate
116   \param[in]   in      Pointer to the source data
117   \param[out]  out     Pointer to the result data
118   \param[in]   size    The source data size
119   \param[in]   iv      Init vector
120   \return      Error code \ref Csi_error_t
121 */
122 csi_error_t csi_aes_cfb1_encrypt(csi_aes_t *aes, void *in, void *out,  uint32_t size, void *iv);
123 
124 /**
125   \brief       AES cfb1 decrypt
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_cfb1_decrypt(csi_aes_t *aes, void *in, void *out,  uint32_t size, void *iv);
134 
135 /**
136   \brief       AES cfb8 encrypt
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_cfb8_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
145 
146 /**
147   \brief       AES cfb8 decrypt
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_cfb8_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv);
156 
157 /**
158   \brief       AES cfb128 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   \param[out]  num     The number of the 128-bit block we have used
165   \return      Error code \ref csi_error_t
166 */
167 csi_error_t csi_aes_cfb128_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv, uint32_t *num);
168 
169 /**
170   \brief       AES cfb128 encrypt
171   \param[in]   aes     Handle to operate
172   \param[in]   in      Pointer to the source data
173   \param[out]  out     Pointer to the result data
174   \param[in]   size    The source data size
175   \param[in]   iv      Init vector
176   \param[out]  num     The number of the 128-bit block we have used
177   \return      Error code \ref csi_error_t
178 */
179 csi_error_t csi_aes_cfb128_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv, uint32_t *num);
180 
181 /**
182   \brief       AES ofb encrypt
183   \param[in]   aes     Handle to operate
184   \param[in]   in      Pointer to the source data
185   \param[out]  out     Pointer to the result data
186   \param[in]   size    The source data size
187   \param[in]   iv      Init vector
188   \param[out]  num     The number of the 128-bit block we have used
189   \return      Error code \ref csi_error_t
190 */
191 csi_error_t csi_aes_ofb_encrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv, uint32_t *num);
192 
193 /**
194   \brief       AES ofb decrypt
195   \param[in]   aes     Handle to operate
196   \param[in]   in      Pointer to the source data
197   \param[out]  out     Pointer to the result data
198   \param[in]   size    The source data size
199   \param[in]   iv      Init vector
200   \param[out]  num     The number of the 128-bit block we have used
201   \return      Error code \ref csi_error_t
202 */
203 csi_error_t csi_aes_ofb_decrypt(csi_aes_t *aes, void *in, void *out, uint32_t size, void *iv, uint32_t *num);
204 
205 /**
206   \brief       AES ctr encrypt
207   \param[in]   aes              Handle to operate
208   \param[in]   in               Pointer to the source data
209   \param[out]  out              Pointer to the result data
210   \param[in]   size             The source data size
211   \param[in]   nonce_counter    Pointer to the 128-bit nonce and counter
212   \param[in]   stream_block     Pointer to the saved stream-block for resuming
213   \param[in]   iv               Init vector
214   \param[out]  num              The number of the 128-bit block we have used
215   \return      Error code \ref csi_error_t
216 */
217 csi_error_t csi_aes_ctr_encrypt(csi_aes_t *aes,
218                                 void *in,
219                                 void *out,
220                                 uint32_t size,
221                                 uint8_t nonce_counter[16],
222                                 uint8_t stream_block[16],
223                                 void *iv,
224                                 uint32_t *num);
225 
226 /**
227   \brief       AES ctr 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]   nonce_counter    Pointer to the 128-bit nonce and counter
233   \param[in]   stream_block     Pointer to the saved stream-block for resuming
234   \param[in]   iv               Init vecotr
235   \param[out]  num              The number of the 128-bit block we have used
236   \return      Error code \ref csi_error_t
237 */
238 csi_error_t csi_aes_ctr_decrypt(csi_aes_t *aes,
239                                 void *in,
240                                 void *out,
241                                 uint32_t size,
242                                 uint8_t nonce_counter[16],
243                                 uint8_t stream_block[16],
244                                 void *iv,
245                                 uint32_t *num);
246 
247 /**
248   \brief       Enable AES power manage
249   \param[in]   aes    Handle to operate
250   \return      Error code \ref csi_error_t
251 */
252 csi_error_t csi_aes_enable_pm(csi_aes_t *aes);
253 
254 /**
255   \brief       Disable AES power manage
256   \param[in]   aes    Handle to operate
257   \return      None
258 */
259 void csi_aes_disable_pm(csi_aes_t *aes);
260 
261 #ifdef __cplusplus
262 }
263 #endif
264 
265 #endif /* _DRV_AES_H_ */
266