1 /**
2  * @file
3  * @brief   AES driver.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32680_AES_H_
27 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32680_AES_H_
28 
29 /***** Includes *****/
30 #include "aes_regs.h"
31 #include "aeskeys_regs.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /**
38  * @defgroup aes AES
39  * @ingroup periphlibs
40  * @{
41  */
42 /*@} end of group aes */
43 
44 /***** Definitions *****/
45 
46 typedef void (*mxc_aes_complete_t)(void *req, int result);
47 
48 /* ************************************************************************* */
49 /* Cipher Definitions                                                                          */
50 /* ************************************************************************* */
51 
52 /**
53   * @brief  Enumeration type to select AES key
54   *
55   */
56 typedef enum {
57     MXC_AES_128BITS = MXC_S_AES_CTRL_KEY_SIZE_AES128, ///< Select AES-128 bit key
58     MXC_AES_192BITS = MXC_S_AES_CTRL_KEY_SIZE_AES192, ///< Select AES-192 bit key
59     MXC_AES_256BITS = MXC_S_AES_CTRL_KEY_SIZE_AES256, ///< Select AES-256 bit key
60 } mxc_aes_keys_t;
61 
62 /**
63   * @brief  Enumeration type to select AES key source and encryption type
64   *
65   */
66 typedef enum {
67     MXC_AES_ENCRYPT_EXT_KEY = 0, ///< Encryption using External key
68     MXC_AES_DECRYPT_EXT_KEY = 1, ///< Encryption using internal key
69     MXC_AES_DECRYPT_INT_KEY = 2 ///< Decryption using internal key
70 } mxc_aes_enc_type_t;
71 
72 /**
73   * @brief  Structure used to set up AES request
74   *
75   */
76 typedef struct _mxc_aes_cipher_req_t {
77     uint32_t length; ///< Length of the data
78     uint32_t *inputData; ///< Pointer to input data
79     uint32_t *resultData; ///< Pointer to encrypted data
80     mxc_aes_keys_t keySize; ///< Size of AES key
81     mxc_aes_enc_type_t encryption; ///< Encrytion type or \ref mxc_aes_enc_type_t
82     mxc_aes_complete_t callback; ///< Callback function
83 } mxc_aes_req_t;
84 
85 /***** Function Prototypes *****/
86 
87 /* ************************************************************************* */
88 /* Global Control/Configuration functions                                    */
89 /* ************************************************************************* */
90 
91 /**
92  * @brief   Enable portions of the AES
93  *
94  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
95  */
96 int MXC_AES_Init(void);
97 
98 /**
99  * @brief   Enable AES Interrupts
100  *
101  * @param   interrupt interrupt to enable
102  */
103 void MXC_AES_EnableInt(uint32_t interrupt);
104 
105 /**
106  * @brief   Disable AES Interrupts
107  *
108  * @param   interrupt interrupt to disable
109  */
110 void MXC_AES_DisableInt(uint32_t interrupt);
111 
112 /**
113  * @brief   Checks the global AES Busy Status
114  *
115  * @return  E_BUSY if busy and E_NO_ERROR otherwise, see \ref MXC_Error_Codes for a list of return codes.
116  */
117 int MXC_AES_IsBusy(void);
118 
119 /**
120  * @brief   Disable and reset portions of the AES
121  *
122  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
123  */
124 int MXC_AES_Shutdown(void);
125 
126 /**
127  * @brief   This function should be called from the DMA Handler
128  *          when using Async functions
129  */
130 void MXC_AES_DMACallback(int ch, int error);
131 
132 /**
133  * @brief   This function should be called before encryption to genrate external key
134  */
135 void MXC_AES_GenerateKey(void);
136 
137 /**
138  * @brief   Set Key size for encryption or decryption
139  *
140  * @param   key Key size, see \ref mxc_aes_keys_t for a list of keys
141  */
142 void MXC_AES_SetKeySize(mxc_aes_keys_t key);
143 
144 /**
145  * @brief   Get the currently set key size
146  *
147  * @return  mxc_aes_keys_t see \ref mxc_aes_keys_t
148  */
149 mxc_aes_keys_t MXC_AES_GetKeySize(void);
150 
151 /**
152  * @brief   Flush Input Data FIFO
153  *
154  */
155 void MXC_AES_FlushInputFIFO(void);
156 
157 /**
158  * @brief   Flush Output Data FIFO
159  *
160  */
161 void MXC_AES_FlushOutputFIFO(void);
162 
163 /**
164  * @brief   Start AES Calculations
165  *
166  */
167 void MXC_AES_Start(void);
168 
169 /**
170  * @brief   Get Interrupt flags set
171  *
172  * @return  return the flags set in intfl register
173  */
174 uint32_t MXC_AES_GetFlags(void);
175 
176 /**
177  * @brief   Clear the interrupts
178  *
179  * @param   flags flags to be cleared
180  */
181 void MXC_AES_ClearFlags(uint32_t flags);
182 
183 /**
184  * @brief
185  * @note    The result will be stored in the req structure
186  *
187  * @param   req  Structure containing data for the encryption
188  *
189  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
190  */
191 int MXC_AES_Generic(mxc_aes_req_t *req);
192 
193 /**
194  * @brief   Perform an encryption
195  * @note    The result will be stored in the req structure
196  *
197  * @param   req  Structure containing data for the encryption
198  *
199  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
200  */
201 int MXC_AES_Encrypt(mxc_aes_req_t *req);
202 
203 /**
204  * @brief   Perform a decryption
205  * @note    The result will be stored in the req structure
206  *
207  * @param   req  Structure containing data for the decryption
208  *
209  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
210  */
211 int MXC_AES_Decrypt(mxc_aes_req_t *req);
212 
213 /**
214  * @brief   Perform AES TX using DMA. Configures DMA request and starts the transmission.
215  *
216  * @param   src_addr  source address
217  * @param   len       number of words of data
218  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
219  */
220 int MXC_AES_TXDMAConfig(void *src_addr, int len);
221 
222 /**
223  * @brief   Perform AES RX using DMA. Configures DMA request and receives data from AES FIFO.
224  *
225  * @param   dest_addr destination address
226  * @param   len       number of words of data
227  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
228  */
229 int MXC_AES_RXDMAConfig(void *dest_addr, int len);
230 
231 /**
232  * @brief   Perform encryption or decryption using DMA
233  *
234  * @param   req The result will be stored in the req structure. The user needs
235  *              to call MXC_AES_Handler() in the ISR
236  * @param   enc 0 for encryption and 1 for decryption
237  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
238  */
239 int MXC_AES_GenericAsync(mxc_aes_req_t *req, uint8_t enc);
240 
241 /**
242  * @brief   Perform an encryption using Interrupt
243  * @note    The result will be stored in the req structure. The user needs
244  *          to call MXC_AES_Handler() in the ISR
245  *
246  * @param   req  Structure containing data for the encryption
247  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
248  */
249 int MXC_AES_EncryptAsync(mxc_aes_req_t *req);
250 
251 /**
252  * @brief   Perform a decryption using Interrupt
253  * @note    The result will be stored in the req structure. The user needs
254  *          to call MXC_AES_Handler() in the ISR
255  *
256  * @param   req  Structure containing data for the decryption
257  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
258  */
259 int MXC_AES_DecryptAsync(mxc_aes_req_t *req);
260 
261 /**
262  * @brief   Set the external key
263  * @param   key  Buffer for the key.
264  * @param   len  Key size.
265  */
266 void MXC_AES_SetExtKey(const void *key, mxc_aes_keys_t len);
267 
268 #ifdef __cplusplus
269 }
270 #endif
271 /**@} end of group aes */
272 
273 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32680_AES_H_
274