1 /**
2   *********************************************************************************
3   *
4   * @file    ald_crypt.h
5   * @brief   Header file of CRYPT module driver.
6   *
7   * @version V1.0
8   * @date    19 Jun 2019
9   * @author  AE Team
10   * @note
11   *          Change Logs:
12   *          Date            Author          Notes
13   *          19 Jun 2019     AE Team         The first version
14   *
15   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
16   *
17   * SPDX-License-Identifier: Apache-2.0
18   *
19   * Licensed under the Apache License, Version 2.0 (the License); you may
20   * not use this file except in compliance with the License.
21   * You may obtain a copy of the License at
22   *
23   * www.apache.org/licenses/LICENSE-2.0
24   *
25   * Unless required by applicable law or agreed to in writing, software
26   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
27   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   * See the License for the specific language governing permissions and
29   * limitations under the License.
30   **********************************************************************************
31   */
32 
33 #ifndef __ALD_CRYPT_H__
34 #define __ALD_CRYPT_H__
35 
36 #ifdef __cplusplus
37  extern "C" {
38 #endif
39 
40 #include "utils.h"
41 
42 /** @addtogroup ES32FXXX_ALD
43   * @{
44   */
45 
46 /** @addtogroup CRYPT
47   * @{
48   */
49 
50 /** @defgroup CRYPT_Public_Types CRYPT Public Types
51   * @{
52   */
53 
54 /**
55   * @brief CRYPT encrypt or decrypt select
56   */
57 typedef enum {
58 	CRYPT_DECRYPT = 0U,	/**< Decrypt */
59 	CRYPT_ENCRYPT = 1U,	/**< Encrypt */
60 } crypt_encs_t;
61 
62 /**
63   * @brief CRYPT aes key select
64   */
65 typedef enum {
66 	CRYPT_BITS_128 = 0U,	/**< 128bit key for aes */
67 	CRYPT_BITS_192 = 1U,	/**< 192bit key for aes */
68 	CRYPT_BITS_256 = 2U,	/**< 256bit key for aes */
69 } crypt_aesks_t;
70 
71 /**
72   * @brief CRYPT mode select
73   */
74 typedef enum {
75 	CRYPT_MODE_ECB = 0U,	/**< ECB */
76 	CRYPT_MODE_CBC = 1U,	/**< CBC */
77 	CRYPT_MODE_CTR = 2U,	/**< CTR */
78 } crypt_mode_t;
79 
80 /**
81   * @brief CRYPT data type
82   */
83 typedef enum {
84 	CRYPT_DATA_CHANGE_NO = 0U,	/**< No exchange */
85 	CRYPT_DATA_CHANGE_16 = 1U,	/**< 16bit exchange */
86 	CRYPT_DATA_CHANGE_8  = 2U,	/**< 8bit exchange */
87 	CRYPT_DATA_CHANGE_1  = 3U,	/**< 1bit exchange */
88 } crypt_datatype_t;
89 
90 /**
91   * @brief CRYPT des key select
92   */
93 typedef enum {
94 	CRYPT_KEYS_2 = 0U,	/**< 2 key for des */
95 	CRYPT_KEYS_3 = 1U,	/**< 3 key for des */
96 	CRYPT_KEYS_1 = 2U,	/**< 1 key for des */
97 } crypt_desks_t;
98 
99 /**
100   * @brief CRYPT crypt select
101   */
102 typedef enum {
103 	CRYPT_CRYSEL_AES = 0U,	/**< AES */
104 	CRYPT_CRYSEL_DES = 1U,	/**< DES */
105 } crypt_crysel_t;
106 
107 /**
108   * @brief CRYPT interrupt
109   */
110 typedef enum {
111 	CRYPT_IT_IT = 0x80U,	/**< Interrupt */
112 } crypt_it_t;
113 
114 /**
115   * @brief CRYPT interrupt flag
116   */
117 typedef enum {
118 	CRYPT_FLAG_AESIF = 0x1U,  	/**< Aes flag */
119 	CRYPT_FLAG_DESIF = 0x2U,  	/**< Des flag */
120 	CRYPT_FLAG_DONE  = 0x100U,	/**< Complete flag */
121 } crypt_flag_t;
122 
123 /**
124   * @brief CRYPT key select
125   */
126 typedef enum {
127 	CRYPT_AES_BITS_128 = 0x4U,	/**< 128bit key of aes */
128 	CRYPT_AES_BITS_192 = 0x6U,	/**< 192bit key of aes */
129 	CRYPT_AES_BITS_256 = 0x8U,	/**< 192bit key of ase */
130 	CRYPT_DES_KEYS_1   = 0x12U,	/**< 1 key of des */
131 	CRYPT_DES_KEYS_2   = 0x14U,	/**< 2 key of des */
132 	CRYPT_DES_KEYS_3   = 0x16U,	/**< 3 key of des */
133 } crypt_ks_t;
134 
135 /**
136   * @brief CRYPT state structures definition
137   */
138 typedef enum {
139 	CRYPT_STATE_RESET = 0x0U,	/**< Peripheral is not initialized */
140 	CRYPT_STATE_READY = 0x1U,	/**< Peripheral Initialized and ready for use */
141 	CRYPT_STATE_BUSY  = 0x2U,	/**< An internal process is ongoing */
142 	CRYPT_STATE_ERROR = 0x4U,	/**< Error */
143 } crypt_state_t;
144 
145 /**
146   * @brief CRYPT key length
147   */
148 typedef enum {
149 	KEY_2_LEN = 2U,	/**< Key's lenth is 2 */
150 	KEY_4_LEN = 4U,	/**< Key's lenth is 4 */
151 	KEY_6_LEN = 6U,	/**< Key's lenth is 6 */
152 	KEY_8_LEN = 8U,	/**< Key's lenth is 8 */
153 } crypt_key_len_t;
154 
155 /**
156   * @brief CRYPT iv length
157   */
158 typedef enum {
159 	IV_2_LEN = 2U,	/**< iv's lenth is 2 */
160 	IV_4_LEN = 4U,	/**< iv's lenth is 4 */
161 } crypt_ivr_len_t;
162 
163 /**
164   * @brief CRYPT data type
165   */
166 typedef enum {
167 	DATA_32_BIT = 0U,	/**< 32 bit data,don't swap */
168 	DATA_16_BIT = 1U,	/**< 16 bit data,swap */
169 	DATA_8_BIT  = 2U,	/**< 8 bit data,swap */
170 	DATA_1_BIT  = 3U,	/**< 1 bit data, swap */
171 } crypt_data_t;
172 
173 /**
174   * @brief CRYPT init structure definition
175   */
176 typedef struct {
177 	crypt_mode_t mode;    	/**< Crypt mode */
178 	crypt_data_t type;    	/**< Data type select */
179 	crypt_ks_t key;		/**< Key select */
180 } crypt_init_t;
181 
182 /**
183   * @brief  CRYPT Handle Structure definition
184   */
185 typedef struct crypt_handle_s {
186 	CRYPT_TypeDef *perh;	/**< Register base address */
187 	crypt_init_t init;  	/**< CRYPT required parameters */
188 
189 	dma_handle_t hdma_m2p;	/**< CRYPT DMA handle parameters memory to crypt module */
190 	dma_handle_t hdma_p2m;	/**< CRYPT DMA handle parameters crypt module to memory */
191 
192 	uint8_t *plain_text; 	/**< Pointer to plain text */
193 	uint8_t *cipher_text;	/**< Pointer to cipher text */
194 	uint32_t size;       	/**< The size of crypt data buf */
195 	uint32_t count;      	/**< The count of crypt data buf */
196 	uint32_t step;       	/**< The step of once crypt 2(des) or 4(aes) */
197 	uint32_t dir;        	/**< ENCRYPT or DECRYPT */
198 	uint32_t iv[4];     	/**< The iv of crypt */
199 	uint32_t iv_size;	/**< The iv size */
200 	uint32_t key[8];     	/**< The key of crypt */
201 	uint32_t key_size;	/**< The key size */
202 	lock_state_t lock;   	/**< Locking object */
203 	crypt_state_t state; 	/**< CRYPT operation state */
204 
205 	void (*crypt_cplt_cbk)(struct crypt_handle_s *arg);	/**< Crypt completed callback */
206 	void (*err_cplt_cbk)(struct crypt_handle_s *arg);  	/**< Crypt error callback */
207 } crypt_handle_t;
208 /**
209   * @}
210   */
211 
212 /** @defgroup CRYPT_Public_Macros CRYPT Public Macros
213   * @{
214   */
215 #define CRYPT_GO(handle) 		(SET_BIT((handle)->perh->CON, CRYPT_CON_GO_MSK))
216 #define CRYPT_FIFOEN_ENABLE(handle) 	(SET_BIT((handle)->perh->CON, CRYPT_CON_FIFOEN_MSK))
217 #define CRYPT_FIFOEN_DISABLE(handle) 	(CLEAR_BIT((handle)->perh->CON, CRYPT_CON_FIFOEN_MSK))
218 #define CRYPT_IVEN_ENABLE(handle) 	(SET_BIT((handle)->perh->CON, CRYPT_CON_IVEN_MSK))
219 #define CRYPT_IVEN_DISABLE(handle) 	(CLEAR_BIT((handle)->perh->CON, CRYPT_CON_IVEN_MSK))
220 #define CRYPT_IE_ENABLE(handle)		(SET_BIT((handle)->perh->CON, CRYPT_CON_IE_MSK))
221 #define CRYPT_IE_DISABLE(handle)	(CLEAR_BIT((handle)->perh->CON, CRYPT_CON_IE_MSK))
222 #define CRYPT_DMA_ENABLE(handle)	(SET_BIT((handle)->perh->CON, CRYPT_CON_DMAEN_MSK))
223 #define CRYPT_DMA_DISABLE(handle)	(CLEAR_BIT((handle)->perh->CON, CRYPT_CON_DMAEN_MSK))
224 #define CRYPT_SETDIR(handle, dir)	do {(handle)->perh->CON &= ~(0x1 << CRYPT_CON_ENCS_POS);	\
225 					   (handle)->perh->CON |= (dir << CRYPT_CON_ENCS_POS);} while (0)
226 #define CRYPT_WRITE_FIFO(handle, data)  ((handle)->perh->FIFO = (data))
227 #define CRYPT_READ_FIFO(handle)		((handle)->perh->FIFO)
228 /**
229   * @}
230   */
231 
232 /** @defgroup CRYPT_Private_Macros   CRYPT Private Macros
233   * @{
234   */
235 #define IS_CRYPT(x)		((x) == CRYPT)
236 #define IS_CRYPT_MODE(x) 	(((x) == CRYPT_MODE_ECB) ||   \
237 				 ((x) == CRYPT_MODE_CBC) ||   \
238 				 ((x) == CRYPT_MODE_CTR))
239 #define IS_CRYPT_KS(x) 		(((x) == CRYPT_AES_BITS_128) || \
240 				 ((x) == CRYPT_AES_BITS_192) || \
241 				 ((x) == CRYPT_AES_BITS_256) || \
242 				 ((x) == CRYPT_DES_KEYS_1)   || \
243 				 ((x) == CRYPT_DES_KEYS_2)   || \
244 				 ((x) == CRYPT_DES_KEYS_3))
245 #define IS_CRYPT_IT(x)		((x) == CRYPT_IT_IT)
246 #define IS_CRYPT_FLAG(x) 	(((x) == CRYPT_FLAG_AESIF) || \
247 				 ((x) == CRYPT_FLAG_DESIF) || \
248 				 ((x) == CRYPT_FLAG_DONE))
249 #define IS_CRYPT_IV_LEN(x)	(((x) == IV_2_LEN) || \
250 				 ((x) == IV_4_LEN))
251 #define IS_CRYPT_KEY_LEN(x)	(((x) == KEY_2_LEN) || \
252 				 ((x) == KEY_4_LEN) || \
253 				 ((x) == KEY_6_LEN) || \
254 				 ((x) == KEY_8_LEN))
255 /**
256   * @}
257   */
258 
259 /** @addtogroup CRYPT_Public_Functions
260   * @{
261   */
262 
263 /** @addtogroup CRYPT_Public_Functions_Group1
264   * @{
265   */
266 extern ald_status_t ald_crypt_init(crypt_handle_t *hperh);
267 extern ald_status_t ald_crypt_write_key(crypt_handle_t *hperh, uint32_t * key, crypt_key_len_t len);
268 extern ald_status_t ald_crypt_read_key(crypt_handle_t *hperh, uint32_t * key, crypt_key_len_t len);
269 extern ald_status_t ald_crypt_write_ivr(crypt_handle_t *hperh, uint32_t * iv, crypt_ivr_len_t len);
270 extern ald_status_t ald_crypt_read_ivr(crypt_handle_t *hperh, uint32_t * iv, crypt_ivr_len_t len);
271 /**
272   * @}
273   */
274 
275 /** @addtogroup CRYPT_Public_Functions_Group2
276   * @{
277   */
278 extern ald_status_t ald_crypt_encrypt(crypt_handle_t *hperh, uint8_t * plain_text, uint8_t * cipher_text, uint32_t size);
279 extern ald_status_t ald_crypt_decrypt(crypt_handle_t *hperh, uint8_t * cipher_text, uint8_t * plain_text, uint32_t size);
280 extern ald_status_t ald_crypt_gcm_verify(crypt_handle_t *hperh, uint8_t *cipher_text, uint32_t size, uint8_t *aadata, uint32_t alen, uint8_t *tag);
281 extern ald_status_t ald_crypt_encrypt_by_it(crypt_handle_t *hperh, uint8_t *plain_text, uint8_t *cipher_text, uint32_t size);
282 extern ald_status_t ald_crypt_decrypt_by_it(crypt_handle_t *hperh, uint8_t *cipher_text, uint8_t *plain_text, uint32_t size);
283 
284 extern ald_status_t ald_crypt_encrypt_by_dma(crypt_handle_t *hperh, uint8_t * plain_text,
285              uint8_t *cipher_text, uint32_t size, uint8_t channel_m2p, uint8_t channel_p2m);
286 extern ald_status_t ald_crypt_decrypt_by_dma(crypt_handle_t *hperh, uint8_t * cipher_text,
287                uint8_t *plain_text, uint32_t size, uint8_t channel_m2p, uint8_t channel_p2m);
288 
289 /**
290   * @}
291   */
292 
293 /** @addtogroup CRYPT_Public_Functions_Group3
294   * @{
295   */
296 
297 extern ald_status_t ald_crypt_dma_pause(crypt_handle_t *hperh);
298 extern ald_status_t ald_crypt_dma_resume(crypt_handle_t *hperh);
299 extern ald_status_t ald_crypt_dma_stop(crypt_handle_t *hperh);
300 
301 extern void ald_crypt_irq_handler(crypt_handle_t *hperh);
302 /**
303   * @}
304   */
305 
306 /** @addtogroup CRYPT_Public_Functions_Group4
307   * @{
308   */
309 extern void ald_crypt_interrupt_config(crypt_handle_t *hperh, crypt_it_t it, type_func_t state);
310 extern flag_status_t ald_crypt_get_flag_status(crypt_handle_t *hperh, crypt_flag_t flag);
311 extern void ald_crypt_clear_flag_status(crypt_handle_t *hperh, crypt_flag_t flag);
312 extern it_status_t ald_crypt_get_it_status(crypt_handle_t *hperh, crypt_it_t it);
313 /**
314   * @}
315   */
316 
317 /** @addtogroup CRYPT_Public_Functions_Group5
318   * @{
319   */
320 extern crypt_state_t ald_crypt_get_state(crypt_handle_t *hperh);
321 /**
322   * @}
323   */
324 
325 /**
326   * @}
327   */
328 
329 /**
330   * @}
331   */
332 
333 /**
334   * @}
335   */
336 
337 #ifdef __cplusplus
338 }
339 #endif
340 
341 #endif
342