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