1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 
6 /******************************************************************************
7  * @file     drv_aes.h
8  * @brief    Header File for AES Driver
9  * @version  V1.0
10  * @date     02. June 2017
11  * @model    aes
12  ******************************************************************************/
13 #ifndef _CSI_AES_H_
14 #define _CSI_AES_H_
15 
16 
17 #include <stdint.h>
18 #include <drv/common.h>
19 #include <drv/errno.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /// definition for aes handle.
26 typedef void *aes_handle_t;
27 
28 /****** AES specific error codes *****/
29 typedef enum {
30     AES_ERROR_MODE  = (DRV_ERROR_SPECIFIC + 1),        ///< Specified Mode not supported
31     AES_ERROR_DATA_BITS,                               ///< Specified number of Data bits not supported
32     AES_ERROR_ENDIAN                                   ///< Specified endian not supported
33 } aes_error_e;
34 
35 /*----- AES Control Codes: Mode -----*/
36 typedef enum {
37     AES_MODE_ECB                  = 0,   ///< ECB Mode
38     AES_MODE_CBC,                        ///< CBC Mode
39     AES_MODE_CFB1,                       ///< CFB1 Mode
40     AES_MODE_CFB8,                       ///< CFB8 Mode
41     AES_MODE_CFB128,                     ///< CFB128 Mode
42     AES_MODE_OFB,                        ///< OFB Mode
43     AES_MODE_CTR                         ///< CTR Mode
44 } aes_mode_e;
45 
46 /*----- AES Control Codes: Crypto Mode -----*/
47 typedef enum {
48     AES_CRYPTO_MODE_ENCRYPT                  = 0,   ///< encrypt Mode
49     AES_CRYPTO_MODE_DECRYPT,                        ///< decrypt Mode
50 } aes_crypto_mode_e;
51 
52 /*----- AES Control Codes: Mode Parameters: Key length -----*/
53 typedef enum {
54     AES_KEY_LEN_BITS_128        = 0,      ///< 128 Data bits
55     AES_KEY_LEN_BITS_192,                 ///< 192 Data bits
56     AES_KEY_LEN_BITS_256                  ///< 256 Data bits
57 } aes_key_len_bits_e;
58 
59 /*----- AES Control Codes: Mode Parameters: Endian -----*/
60 typedef enum {
61     AES_ENDIAN_LITTLE          = 0,       ///< Little Endian
62     AES_ENDIAN_BIG                        ///< Big Endian
63 } aes_endian_mode_e;
64 
65 /**
66 \brief AES Status
67 */
68 typedef struct {
69     uint32_t busy             : 1;        ///< busy flag
70 } aes_status_t;
71 
72 /****** AES Event *****/
73 typedef enum {
74     AES_EVENT_CRYPTO_COMPLETE    = 0   ///< Encrypt completed
75 } aes_event_e;
76 typedef void (*aes_event_cb_t)(int32_t idx, aes_event_e event);   ///< Pointer to \ref aes_event_cb_t : AES Event call back.
77 
78 
79 /**
80 \brief AES Device Driver Capabilities.
81 */
82 typedef struct {
83     uint32_t ecb_mode           : 1;      ///< supports ECB mode
84     uint32_t cbc_mode           : 1;      ///< supports CBC mode
85     uint32_t cfb1_mode          : 1;      ///< supports CFB1 mode
86     uint32_t cfb8_mode          : 1;      ///< supports CFB8 mode
87     uint32_t cfb128_mode        : 1;      ///< supports CFB128 mode
88     uint32_t ofb_mode           : 1;      ///< supports OFB mode
89     uint32_t ctr_mode           : 1;      ///< supports CTR mode
90     uint32_t bits_128           : 1;      ///< supports 128bits key length
91     uint32_t bits_192           : 1;      ///< supports 192bits key length
92     uint32_t bits_256           : 1;      ///< supports 256bits key length
93 } aes_capabilities_t;
94 
95 
96 // Function documentation
97 
98 /**
99   \brief       Initialize AES Interface. 1. Initializes the resources needed for the AES interface 2.registers event callback function
100   \param[in]   idx   device id
101   \param[in]   cb_event  event callback function \ref aes_event_cb_t
102   \return      if success return aes handle else return NULL
103 */
104 aes_handle_t csi_aes_initialize(int32_t idx, aes_event_cb_t cb_event);
105 
106 /**
107   \brief       De-initialize AES Interface. stops operation and releases the software resources used by the interface
108   \param[in]   handle  aes handle to operate.
109   \return      error code
110 */
111 int32_t csi_aes_uninitialize(aes_handle_t handle);
112 
113 /**
114   \brief       control aes power.
115   \param[in]   handle  aes handle to operate.
116   \param[in]   state   power state.\ref csi_power_stat_e.
117   \return      error code
118 */
119 int32_t csi_aes_power_control(aes_handle_t handle, csi_power_stat_e state);
120 
121 /**
122   \brief       Get driver capabilities.
123   \param[in]   idx device id
124   \return      \ref aes_capabilities_t
125 */
126 aes_capabilities_t csi_aes_get_capabilities(int32_t idx);
127 
128 /**
129   \brief       config aes mode.
130   \param[in]   handle  aes handle to operate.
131   \param[in]   mode      \ref aes_mode_e
132   \param[in]   keylen_bits \ref aes_key_len_bits_e
133   \param[in]   endian    \ref aes_endian_mode_e
134   \return      error code
135 */
136 int32_t csi_aes_config(aes_handle_t handle,
137                        aes_mode_e mode,
138                        aes_key_len_bits_e keylen_bits,
139                        aes_endian_mode_e endian
140                       );
141 
142 /**
143   \brief       set crypto key.
144   \param[in]   handle    aes handle to operate.
145   \param[in]   context   aes information context
146   \param[in]   key       Pointer to the key buf
147   \param[in]   key_len   Pointer to \ref aes_key_len_bits_e
148   \param[in]   enc       \ref aes_crypto_mode_e
149   \return      error code
150 */
151 int32_t csi_aes_set_key(aes_handle_t handle, void *context, void *key, aes_key_len_bits_e key_len, aes_crypto_mode_e enc);
152 
153 /**
154   \brief       aes ecb encrypt or decrypt
155   \param[in]   handle  aes handle to operate.
156   \param[in]   context aes information context
157   \param[in]   in   Pointer to the Source data
158   \param[out]  out  Pointer to the Result data.
159   \param[in]   len  the Source data len.
160   \return      error code
161 */
162 int32_t csi_aes_ecb_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len);
163 
164 /**
165   \brief       aes cbc encrypt or decrypt
166   \param[in]   handle  aes handle to operate.
167   \param[in]   context aes information context
168   \param[in]   in   Pointer to the Source data
169   \param[out]  out  Pointer to the Result data.
170   \param[in]   len  the Source data len.
171   \param[in]   iv   Pointer to initialization vector
172   \return      error code
173 */
174 int32_t csi_aes_cbc_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len, uint8_t iv[16]);
175 
176 /**
177   \brief       aes cfb1 encrypt or decrypt
178   \param[in]   handle  aes handle to operate.
179   \param[in]   context aes information context
180   \param[in]   in   Pointer to the Source data
181   \param[out]  out  Pointer to the Result data.
182   \param[in]   len  the Source data len.
183   \param[in]   iv   Pointer to initialization vector
184   \return      error code
185 */
186 int32_t csi_aes_cfb1_crypto(aes_handle_t handle, void *context, void *in, void *out,  uint32_t len, uint8_t iv[16]);
187 
188 /**
189   \brief       aes cfb8 encrypt or decrypt
190   \param[in]   handle  aes handle to operate.
191   \param[in]   context aes information context
192   \param[in]   in   Pointer to the Source data
193   \param[out]  out  Pointer to the Result data.
194   \param[in]   len  the Source data len.
195   \param[in]   iv   Pointer to initialization vector
196   \return      error code
197 */
198 int32_t csi_aes_cfb8_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len, uint8_t iv[16]);
199 
200 /**
201   \brief       aes cfb128 encrypt or decrypt
202   \param[in]   handle  aes handle to operate.
203   \param[in]   context aes information context
204   \param[in]   in   Pointer to the Source data
205   \param[out]  out  Pointer to the Result data.
206   \param[in]   len  the Source data len.
207   \param[in]   iv   Pointer to initialization vector
208   \param[in]   num  the number of the 128-bit block we have used
209   \return      error code
210 */
211 int32_t csi_aes_cfb128_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len, uint8_t iv[16], uint32_t *num);
212 
213 /**
214   \brief       aes ofb encrypt or decrypt
215   \param[in]   handle  aes handle to operate.
216   \param[in]   context aes information context
217   \param[in]   in   Pointer to the Source data
218   \param[out]  out  Pointer to the Result data.
219   \param[in]   len  the Source data len.
220   \param[in]   iv   Pointer to initialization vector
221   \param[in]   num  the number of the 128-bit block we have used
222   \return      error code
223 */
224 int32_t csi_aes_ofb_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len, uint8_t iv[16], uint32_t *num);
225 
226 /**
227   \brief       aes ctr encrypt or decrypt
228   \param[in]   handle  aes handle to operate.
229   \param[in]   context aes information context
230   \param[in]   in   Pointer to the Source data
231   \param[out]  out  Pointer to the Result data.
232   \param[in]   len  the Source data len.
233   \param[in]   nonce_counter   Pointer to the 128-bit nonce and counter
234   \param[in]   stream_block  Pointer to the saved stream-block for resuming
235   \param[in]   num  the number of the 128-bit block we have used
236   \return      error code
237 */
238 int32_t csi_aes_ctr_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len, uint8_t nonce_counter[16], uint8_t stream_block[16], uint32_t *num);
239 
240 /**
241   \brief       Get AES status.
242   \param[in]   handle  aes handle to operate.
243   \return      AES status \ref aes_status_t
244 */
245 aes_status_t csi_aes_get_status(aes_handle_t handle);
246 
247 #ifdef __cplusplus
248 }
249 #endif
250 
251 #endif /* _CSI_AES_H_ */
252