1 /*
2  *  Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *
8  *    Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  *    Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the
14  *    distribution.
15  *
16  *    Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 //*****************************************************************************
34 //
35 //  aes.c
36 //
37 //  Driver for the AES module.
38 //
39 //*****************************************************************************
40 
41 //*****************************************************************************
42 //
43 //! \addtogroup AES_Advanced_Encryption_Standard_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #include <stdbool.h>
49 #include <stdint.h>
50 #include "inc/hw_aes.h"
51 #include "inc/hw_dthe.h"
52 #include "inc/hw_ints.h"
53 #include "inc/hw_memmap.h"
54 #include "inc/hw_nvic.h"
55 #include "inc/hw_types.h"
56 #include "aes.h"
57 #include "debug.h"
58 #include "interrupt.h"
59 
60 #define AES_BLOCK_SIZE_IN_BYTES 16
61 
62 //*****************************************************************************
63 //
64 //! Configures the AES module.
65 //!
66 //! \param ui32Base is the base address of the AES module.
67 //! \param ui32Config is the configuration of the AES module.
68 //!
69 //! This function configures the AES module based on the specified parameters.
70 //! It does not change any DMA- or interrupt-related parameters.
71 //!
72 //! The ui32Config parameter is a bit-wise OR of a number of configuration
73 //! flags. The valid flags are grouped based on their function.
74 //!
75 //! The direction of the operation is specified with only of following flags:
76 //!
77 //! - \b AES_CFG_DIR_ENCRYPT - Encryption mode
78 //! - \b AES_CFG_DIR_DECRYPT - Decryption mode
79 //!
80 //! The key size is specified with only one of the following flags:
81 //!
82 //! - \b AES_CFG_KEY_SIZE_128BIT - Key size of 128 bits
83 //! - \b AES_CFG_KEY_SIZE_192BIT - Key size of 192 bits
84 //! - \b AES_CFG_KEY_SIZE_256BIT - Key size of 256 bits
85 //!
86 //! The mode of operation is specified with only one of the following flags.
87 //!
88 //! - \b AES_CFG_MODE_ECB - Electronic codebook mode
89 //! - \b AES_CFG_MODE_CBC - Cipher-block chaining mode
90 //! - \b AES_CFG_MODE_CFB - Cipher feedback mode
91 //! - \b AES_CFG_MODE_CTR - Counter mode
92 //! - \b AES_CFG_MODE_ICM - Integer counter mode
93 //! - \b AES_CFG_MODE_XTS - Ciphertext stealing mode
94 //! - \b AES_CFG_MODE_XTS_TWEAKJL - XEX-based tweaked-codebook mode with
95 //! ciphertext stealing with previous/intermediate tweak value and j loaded
96 //! - \b AES_CFG_MODE_XTS_K2IJL - XEX-based tweaked-codebook mode with
97 //! ciphertext stealing with key2, i and j loaded
98 //! - \b AES_CFG_MODE_XTS_K2ILJ0 - XEX-based tweaked-codebook mode with
99 //! ciphertext stealing with key2 and i loaded, j = 0
100 //! - \b AES_CFG_MODE_F8 - F8 mode
101 //! - \b AES_CFG_MODE_F9 - F9 mode
102 //! - \b AES_CFG_MODE_CBCMAC - Cipher block chaining message authentication
103 //! code mode
104 //! - \b AES_CFG_MODE_GCM - Galois/counter mode
105 //! - \b AES_CFG_MODE_GCM_HLY0ZERO - Galois/counter mode with GHASH with H
106 //! loaded and Y0-encrypted forced to zero
107 //! - \b AES_CFG_MODE_GCM_HLY0CALC - Galois/counter mode with GHASH with H
108 //! loaded and Y0-encrypted calculated internally
109 //! - \b AES_CFG_MODE_GCM_HY0CALC - Galois/Counter mode with autonomous GHASH
110 //! (both H and Y0-encrypted calculated internally)
111 //! - \b AES_CFG_MODE_CCM - Counter with CBC-MAC mode
112 //!
113 //! The following defines are used to specify the counter width.  It is only
114 //! required to be defined when using CTR, CCM, or GCM modes, only one of the
115 //! following defines must be used to specify the counter width length:
116 //!
117 //! - \b AES_CFG_CTR_WIDTH_32 - Counter is 32 bits
118 //! - \b AES_CFG_CTR_WIDTH_64 - Counter is 64 bits
119 //! - \b AES_CFG_CTR_WIDTH_96 - Counter is 96 bits
120 //! - \b AES_CFG_CTR_WIDTH_128 - Counter is 128 bits
121 //!
122 //! Only one of the following defines must be used to specify the length field
123 //! for CCM operations (L):
124 //!
125 //! - \b AES_CFG_CCM_L_2 - 2 bytes
126 //! - \b AES_CFG_CCM_L_4 - 4 bytes
127 //! - \b AES_CFG_CCM_L_8 - 8 bytes
128 //!
129 //! Only one of the following defines must be used to specify the length of the
130 //! authentication field for CCM operations (M) through the \e ui32Config
131 //! argument in the AESConfigSet() function:
132 //!
133 //! - \b AES_CFG_CCM_M_4 - 4 bytes
134 //! - \b AES_CFG_CCM_M_6 - 6 bytes
135 //! - \b AES_CFG_CCM_M_8 - 8 bytes
136 //! - \b AES_CFG_CCM_M_10 - 10 bytes
137 //! - \b AES_CFG_CCM_M_12 - 12 bytes
138 //! - \b AES_CFG_CCM_M_14 - 14 bytes
139 //! - \b AES_CFG_CCM_M_16 - 16 bytes
140 //!
141 //! \return None.
142 //
143 //*****************************************************************************
144 void
AESConfigSet(uint32_t ui32Base,uint32_t ui32Config)145 AESConfigSet(uint32_t ui32Base, uint32_t ui32Config)
146 {
147     //
148     // Check the arguments.
149     //
150     ASSERT(ui32Base == AES_BASE);
151     ASSERT((ui32Config & AES_CFG_DIR_ENCRYPT) ||
152            (ui32Config & AES_CFG_DIR_DECRYPT));
153     ASSERT((ui32Config & AES_CFG_KEY_SIZE_128BIT) ||
154            (ui32Config & AES_CFG_KEY_SIZE_192BIT) ||
155            (ui32Config & AES_CFG_KEY_SIZE_256BIT));
156     ASSERT((ui32Config & AES_CFG_MODE_ECB) ||
157            (ui32Config & AES_CFG_MODE_CBC) ||
158            (ui32Config & AES_CFG_MODE_CTR) ||
159            (ui32Config & AES_CFG_MODE_ICM) ||
160            (ui32Config & AES_CFG_MODE_CFB) ||
161            (ui32Config & AES_CFG_MODE_XTS_TWEAKJL) ||
162            (ui32Config & AES_CFG_MODE_XTS_K2IJL) ||
163            (ui32Config & AES_CFG_MODE_XTS_K2ILJ0) ||
164            (ui32Config & AES_CFG_MODE_F8) ||
165            (ui32Config & AES_CFG_MODE_F9) ||
166            (ui32Config & AES_CFG_MODE_CTR) ||
167            (ui32Config & AES_CFG_MODE_CBCMAC) ||
168            (ui32Config & AES_CFG_MODE_GCM_HLY0ZERO) ||
169            (ui32Config & AES_CFG_MODE_GCM_HLY0CALC) ||
170            (ui32Config & AES_CFG_MODE_GCM_HY0CALC) ||
171            (ui32Config & AES_CFG_MODE_CCM));
172     ASSERT(((ui32Config & AES_CFG_MODE_CTR) ||
173             (ui32Config & AES_CFG_MODE_GCM_HLY0ZERO) ||
174             (ui32Config & AES_CFG_MODE_GCM_HLY0CALC) ||
175             (ui32Config & AES_CFG_MODE_GCM_HY0CALC) ||
176             (ui32Config & AES_CFG_MODE_CCM)) &&
177            ((ui32Config & AES_CFG_CTR_WIDTH_32) ||
178             (ui32Config & AES_CFG_CTR_WIDTH_64) ||
179             (ui32Config & AES_CFG_CTR_WIDTH_96) ||
180             (ui32Config & AES_CFG_CTR_WIDTH_128)));
181     ASSERT((ui32Config & AES_CFG_MODE_CCM) &&
182            ((ui32Config & AES_CFG_CCM_L_2) ||
183             (ui32Config & AES_CFG_CCM_L_4) ||
184             (ui32Config & AES_CFG_CCM_L_8)) &&
185            ((ui32Config & AES_CFG_CCM_M_4) ||
186             (ui32Config & AES_CFG_CCM_M_6) ||
187             (ui32Config & AES_CFG_CCM_M_8) ||
188             (ui32Config & AES_CFG_CCM_M_10) ||
189             (ui32Config & AES_CFG_CCM_M_12) ||
190             (ui32Config & AES_CFG_CCM_M_14) ||
191             (ui32Config & AES_CFG_CCM_M_16)));
192 
193     //
194     // Backup the save context field before updating the register.
195     //
196     if(HWREG(ui32Base + AES_O_CTRL) & AES_CTRL_SAVE_CONTEXT)
197     {
198         ui32Config |= AES_CTRL_SAVE_CONTEXT;
199     }
200 
201     //
202     // Write the CTRL register with the new value
203     //
204     HWREG(ui32Base + AES_O_CTRL) = ui32Config;
205 }
206 
207 //*****************************************************************************
208 //
209 //! Writes the key 1 configuration registers, which are used for encryption or
210 //! decryption.
211 //!
212 //! \param ui32Base is the base address for the AES module.
213 //! \param pui8Key is an array of bytes, containing the key to be
214 //! configured.  The least significant word in the 0th index.
215 //! \param ui32Keysize is the size of the key, which must be one of the
216 //! following values:  \b AES_CFG_KEY_SIZE_128, \b AES_CFG_KEY_SIZE_192, or
217 //! \b AES_CFG_KEY_SIZE_256.
218 //!
219 //! This function writes key 1 configuration registers based on the key
220 //! size.  This function is used in all modes.
221 //!
222 //! \return None.
223 //
224 //*****************************************************************************
225 void
AESKey1Set(uint32_t ui32Base,uint8_t * pui8Key,uint32_t ui32Keysize)226 AESKey1Set(uint32_t ui32Base, uint8_t *pui8Key, uint32_t ui32Keysize)
227 {
228     //
229     // Check the arguments.
230     //
231     ASSERT(ui32Base == AES_BASE);
232     ASSERT((ui32Keysize == AES_CFG_KEY_SIZE_128BIT) ||
233            (ui32Keysize == AES_CFG_KEY_SIZE_192BIT) ||
234            (ui32Keysize == AES_CFG_KEY_SIZE_256BIT));
235 
236     //
237     // With all key sizes, the first 4 words are written.
238     //
239     HWREG(ui32Base + AES_O_KEY1_0) = * ((uint32_t *)(pui8Key + 0));
240     HWREG(ui32Base + AES_O_KEY1_1) = * ((uint32_t *)(pui8Key + 4));
241     HWREG(ui32Base + AES_O_KEY1_2) = * ((uint32_t *)(pui8Key + 8));
242     HWREG(ui32Base + AES_O_KEY1_3) = * ((uint32_t *)(pui8Key + 12));
243 
244     //
245     // The key is 192 or 256 bits.  Write the next 2 words.
246     //
247     if(ui32Keysize != AES_CFG_KEY_SIZE_128BIT)
248     {
249         HWREG(ui32Base + AES_O_KEY1_4) = * ((uint32_t *)(pui8Key + 16));
250         HWREG(ui32Base + AES_O_KEY1_5) = * ((uint32_t *)(pui8Key + 20));
251     }
252 
253     //
254     // The key is 256 bits.  Write the last 2 words.
255     //
256     if(ui32Keysize == AES_CFG_KEY_SIZE_256BIT)
257     {
258         HWREG(ui32Base + AES_O_KEY1_6) = * ((uint32_t *)(pui8Key + 24));
259         HWREG(ui32Base + AES_O_KEY1_7) = * ((uint32_t *)(pui8Key + 28));
260     }
261 }
262 
263 //*****************************************************************************
264 //
265 //! Writes the key 2 configuration registers, which are used for encryption or
266 //! decryption.
267 //!
268 //! \param ui32Base is the base address for the AES module.
269 //! \param pui8Key is an array of bytes, containing the key to be
270 //! configured.  The least significant word in the 0th index.
271 //! \param ui32Keysize is the size of the key, which must be one of the
272 //! following values:  \b AES_CFG_KEY_SIZE_128, \b AES_CFG_KEY_SIZE_192, or
273 //! \b AES_CFG_KEY_SIZE_256.
274 //!
275 //! This function writes the key 2 configuration registers based on the key
276 //! size.  This function is used in the F8, F9, XTS, CCM, and CBC-MAC modes.
277 //!
278 //! \return None.
279 //
280 //*****************************************************************************
281 void
AESKey2Set(uint32_t ui32Base,uint8_t * pui8Key,uint32_t ui32Keysize)282 AESKey2Set(uint32_t ui32Base, uint8_t *pui8Key, uint32_t ui32Keysize)
283 {
284     //
285     // Check the arguments.
286     //
287     ASSERT(ui32Base == AES_BASE);
288     ASSERT((ui32Keysize == AES_CFG_KEY_SIZE_128BIT) ||
289            (ui32Keysize == AES_CFG_KEY_SIZE_192BIT) ||
290            (ui32Keysize == AES_CFG_KEY_SIZE_256BIT));
291 
292     //
293     // With all key sizes, the first 4 words are written.
294     //
295     HWREG(ui32Base + AES_O_KEY2_0) = * ((uint32_t *)(pui8Key + 0));
296     HWREG(ui32Base + AES_O_KEY2_1) = * ((uint32_t *)(pui8Key + 4));
297     HWREG(ui32Base + AES_O_KEY2_2) = * ((uint32_t *)(pui8Key + 8));
298     HWREG(ui32Base + AES_O_KEY2_3) = * ((uint32_t *)(pui8Key + 12));
299 
300     //
301     // The key is 192 or 256 bits.  Write the next 2 words.
302     //
303     if(ui32Keysize != AES_CFG_KEY_SIZE_128BIT)
304     {
305         HWREG(ui32Base + AES_O_KEY2_4) = * ((uint32_t *)(pui8Key + 16));
306         HWREG(ui32Base + AES_O_KEY2_5) = * ((uint32_t *)(pui8Key + 20));
307     }
308 
309     //
310     // The key is 256 bits.  Write the last 2 words.
311     //
312     if(ui32Keysize == AES_CFG_KEY_SIZE_256BIT)
313     {
314         HWREG(ui32Base + AES_O_KEY2_6) = * ((uint32_t *)(pui8Key + 24));
315         HWREG(ui32Base + AES_O_KEY2_7) = * ((uint32_t *)(pui8Key + 28));
316     }
317 }
318 
319 //*****************************************************************************
320 //
321 //! Writes key 3 configuration registers, which are used for encryption or
322 //! decryption.
323 //!
324 //! \param ui32Base is the base address for the AES module.
325 //! \param pui8Key is a pointer to an array bytes, containing
326 //! the key to be configured. The least significant word is in the 0th index.
327 //!
328 //! This function writes the key 2 configuration registers with key 3 data
329 //! used in CBC-MAC and F8 modes.  This key is always 128 bits.
330 //!
331 //! \return None.
332 //
333 //*****************************************************************************
334 void
AESKey3Set(uint32_t ui32Base,uint8_t * pui8Key)335 AESKey3Set(uint32_t ui32Base, uint8_t *pui8Key)
336 {
337     //
338     // Check the arguments.
339     //
340     ASSERT(ui32Base == AES_BASE);
341 
342     //
343     // Write the key into the upper 4 key registers
344     //
345     HWREG(ui32Base + AES_O_KEY2_4) = * ((uint32_t *)(pui8Key + 0));
346     HWREG(ui32Base + AES_O_KEY2_5) = * ((uint32_t *)(pui8Key + 4));
347     HWREG(ui32Base + AES_O_KEY2_6) = * ((uint32_t *)(pui8Key + 8));
348     HWREG(ui32Base + AES_O_KEY2_7) = * ((uint32_t *)(pui8Key + 12));
349 }
350 
351 //*****************************************************************************
352 //
353 //! Writes the Initial Vector (IV) register, needed in some of the AES Modes.
354 //!
355 //! \param ui32Base is the base address of the AES module.
356 //! \param pui8IVdata is an array of 16 bytes (128 bits), containing the IV
357 //! value to be configured. The least significant word is in the 0th index.
358 //!
359 //! This functions writes the initial vector registers in the AES module.
360 //!
361 //! \return None.
362 //
363 //*****************************************************************************
364 void
AESIVSet(uint32_t ui32Base,uint8_t * pui8IVdata)365 AESIVSet(uint32_t ui32Base, uint8_t *pui8IVdata)
366 {
367     //
368     // Check the arguments.
369     //
370     ASSERT(ui32Base == AES_BASE);
371 
372     //
373     // Write the initial vector registers.
374     //
375     HWREG(ui32Base + AES_O_IV_IN_0) = *((uint32_t *)(pui8IVdata+0));
376     HWREG(ui32Base + AES_O_IV_IN_1) = *((uint32_t *)(pui8IVdata+4));
377     HWREG(ui32Base + AES_O_IV_IN_2) = *((uint32_t *)(pui8IVdata+8));
378     HWREG(ui32Base + AES_O_IV_IN_3) = *((uint32_t *)(pui8IVdata+12));
379 }
380 
381 
382 //*****************************************************************************
383 //
384 //! Reads the Initial Vector (IV) register, needed in some of the AES Modes.
385 //!
386 //! \param ui32Base is the base address of the AES module.
387 //! \param pui8IVdata is pointer to an array of 16 bytes.
388 //!
389 //! This functions reads the initial vector registers in the AES module.
390 //!
391 //! \return None.
392 //
393 //*****************************************************************************
394 void
AESIVGet(uint32_t ui32Base,uint8_t * pui8IVdata)395 AESIVGet(uint32_t ui32Base, uint8_t *pui8IVdata)
396 {
397     //
398     // Check the arguments.
399     //
400     ASSERT(ui32Base == AES_BASE);
401 
402     //
403     // Write the initial vector registers.
404     //
405     *((uint32_t *)(pui8IVdata+ 0)) = HWREG(ui32Base + AES_O_IV_IN_0);
406     *((uint32_t *)(pui8IVdata+ 4)) = HWREG(ui32Base + AES_O_IV_IN_1);
407     *((uint32_t *)(pui8IVdata+ 8)) = HWREG(ui32Base + AES_O_IV_IN_2);
408     *((uint32_t *)(pui8IVdata+12)) = HWREG(ui32Base + AES_O_IV_IN_3);
409 }
410 
411 //*****************************************************************************
412 //
413 //! Saves the tag registers to a user-defined location.
414 //!
415 //! \param ui32Base is the base address of the AES module.
416 //! \param pui8TagData is pointer to the location that stores the tag data.
417 //!
418 //! This function stores the tag data for use authenticated encryption and
419 //! decryption operations.
420 //!
421 //! \return None.
422 //
423 //*****************************************************************************
424 void
AESTagRead(uint32_t ui32Base,uint8_t * pui8TagData)425 AESTagRead(uint32_t ui32Base, uint8_t *pui8TagData)
426 {
427     //
428     // Check the arguments.
429     //
430     ASSERT(ui32Base == AES_BASE);
431 
432     //
433     // Read the tag data.
434     //
435     *((uint32_t *)(pui8TagData+0)) = HWREG((ui32Base + AES_O_TAG_OUT_0));
436     *((uint32_t *)(pui8TagData+4)) = HWREG((ui32Base + AES_O_TAG_OUT_1));
437     *((uint32_t *)(pui8TagData+8)) = HWREG((ui32Base + AES_O_TAG_OUT_2));
438     *((uint32_t *)(pui8TagData+12)) = HWREG((ui32Base + AES_O_TAG_OUT_3));
439 }
440 
441 //*****************************************************************************
442 //
443 //! Used to set the write crypto data length in the AES module.
444 //!
445 //! \param ui32Base is the base address of the AES module.
446 //! \param ui64Length is the crypto data length in bytes.
447 //!
448 //! This function stores the cryptographic data length in blocks for all modes.
449 //! Data lengths up to (2^61 - 1) bytes are allowed.  For GCM, any value up
450 //! to (2^36 - 2) bytes are allowed because a 32-bit block counter is used.  For
451 //! basic modes (ECB/CBC/CTR/ICM/CFB128), zero can be programmed into the
452 //! length field, indicating that the length is infinite.
453 //!
454 //! When this function is called, the engine is triggered to start using
455 //! this context.
456 //!
457 //! \note This length does not include the authentication-only data used in
458 //! some modes.  Use the AESAuthLengthSet() function to specify the
459 //! authentication data length.
460 //!
461 //! \return None
462 //
463 //*****************************************************************************
464 void
AESDataLengthSet(uint32_t ui32Base,uint64_t ui64Length)465 AESDataLengthSet(uint32_t ui32Base, uint64_t ui64Length)
466 {
467     //
468     // Check the arguments.
469     //
470     ASSERT(ui32Base == AES_BASE);
471 
472     //
473     // Write the length register by shifting the 64-bit ui64Length.
474     //
475     HWREG(ui32Base + AES_O_C_LENGTH_0) = (uint32_t)(ui64Length);
476     HWREG(ui32Base + AES_O_C_LENGTH_1) = (uint32_t)(ui64Length >> 32);
477 }
478 
479 //*****************************************************************************
480 //
481 //! Sets the optional additional authentication data (AAD) length.
482 //!
483 //! \param ui32Base is the base address of the AES module.
484 //! \param ui32Length is the length in bytes.
485 //!
486 //! This function is only used to write the authentication data length in the
487 //! combined modes (GCM or CCM) and XTS mode.  Supported AAD lengths for CCM
488 //! are from 0 to (2^16 - 28) bytes.  For GCM, any value up to (2^32 - 1) can
489 //! be used.  For XTS mode, this register is used to load j.  Loading of j is
490 //! only required if j != 0.  j represents the sequential number of the 128-bit
491 //! blocks inside the data unit.  Consequently, j must be multiplied by 16
492 //! when passed to this function, thereby placing the block number in
493 //! bits [31:4] of the register.
494 //!
495 //! When this function is called, the engine is triggered to start using
496 //! this context for GCM and CCM.
497 //!
498 //! \return None
499 //
500 //*****************************************************************************
501 void
AESAuthDataLengthSet(uint32_t ui32Base,uint32_t ui32Length)502 AESAuthDataLengthSet(uint32_t ui32Base, uint32_t ui32Length)
503 {
504     //
505     // Check the arguments.
506     //
507     ASSERT(ui32Base == AES_BASE);
508 
509     //
510     // Write the length into the register.
511     //
512     HWREG(ui32Base + AES_O_AUTH_LENGTH) = ui32Length;
513 }
514 
515 //*****************************************************************************
516 //
517 //! Reads plaintext/ciphertext from data registers without blocking.
518 //! This api writes data in blocks
519 //!
520 //! \param ui32Base is the base address of the AES module.
521 //! \param pui8Dest is a pointer to an array of words of data.
522 //! \param ui8Length the length can be from 1 to 16
523 //!
524 //! This function reads a block of either plaintext or ciphertext out of the
525 //! AES module.  If the output data is not ready, the function returns
526 //! false.  If the read completed successfully, the function returns true.
527 //! A block is 16 bytes or 4 words.
528 //!
529 //! \return true or false.
530 //
531 //*****************************************************************************
532 bool
AESDataReadNonBlocking(uint32_t ui32Base,uint8_t * pui8Dest,uint8_t ui8Length)533 AESDataReadNonBlocking(uint32_t ui32Base, uint8_t *pui8Dest, uint8_t ui8Length)
534 {
535 	volatile uint32_t pui32Dest[4];
536 	uint8_t ui8BytCnt;
537 	uint8_t *pui8DestTemp;
538     //
539     // Check the arguments.
540     //
541     ASSERT(ui32Base == AES_BASE);
542     if((ui8Length == 0)||(ui8Length>16))
543     {
544        	return(false);
545     }
546 
547     //
548     // Check if the output is ready before reading the data.  If it not ready,
549     // return false.
550     //
551     if((AES_CTRL_OUTPUT_READY & (HWREG(ui32Base + AES_O_CTRL))) == 0)
552     {
553         return(false);
554     }
555 
556     //
557     // Read a block of data from the data registers
558     //
559     pui32Dest[0] = HWREG(ui32Base + AES_O_DATA_IN_3);
560     pui32Dest[1] = HWREG(ui32Base + AES_O_DATA_IN_2);
561     pui32Dest[2] = HWREG(ui32Base + AES_O_DATA_IN_1);
562     pui32Dest[3] = HWREG(ui32Base + AES_O_DATA_IN_0);
563 
564     //
565     //Copy the data to a block memory
566     //
567     pui8DestTemp = (uint8_t *)pui32Dest;
568     for(ui8BytCnt = 0; ui8BytCnt < ui8Length ; ui8BytCnt++)
569     {
570      	*(pui8Dest+ui8BytCnt) = *(pui8DestTemp+ui8BytCnt);
571     }
572     //
573     // Read successful, return true.
574     //
575     return(true);
576 }
577 
578 
579 //*****************************************************************************
580 //
581 //! Reads plaintext/ciphertext from data registers with blocking.
582 //! This api writes data in blocks
583 //!
584 //! \param ui32Base is the base address of the AES module.
585 //! \param pui8Dest is a pointer to an array of words.
586 //! \param ui8Length is the length of data in bytes to be read.
587 //!        ui8Length can be from 1 to 16
588 //!
589 //! This function reads a block of either plaintext or ciphertext out of the
590 //! AES module.  If the output is not ready, the function waits until it
591 //! is ready. A block is 16 bytes or 4 words.
592 //!
593 //! \return None.
594 //
595 //*****************************************************************************
596 
597 void
AESDataRead(uint32_t ui32Base,uint8_t * pui8Dest,uint8_t ui8Length)598 AESDataRead(uint32_t ui32Base, uint8_t *pui8Dest, uint8_t ui8Length)
599 {
600 	volatile uint32_t pui32Dest[4];
601 	uint8_t ui8BytCnt;
602 	uint8_t *pui8DestTemp;
603 
604     //
605     // Check the arguments.
606     //
607     ASSERT(ui32Base == AES_BASE);
608     if((ui8Length == 0)||(ui8Length>16))
609    	{
610        	return;
611     }
612 
613 
614     //
615     // Wait for the output to be ready before reading the data.
616     //
617     while((AES_CTRL_OUTPUT_READY & (HWREG(ui32Base + AES_O_CTRL))) == 0)
618     {
619     }
620 
621     //
622     // Read a block of data from the data registers
623     //
624     pui32Dest[0] = HWREG(ui32Base + AES_O_DATA_IN_3);
625     pui32Dest[1] = HWREG(ui32Base + AES_O_DATA_IN_2);
626     pui32Dest[2] = HWREG(ui32Base + AES_O_DATA_IN_1);
627     pui32Dest[3] = HWREG(ui32Base + AES_O_DATA_IN_0);
628     //
629     //Copy the data to a block memory
630     //
631     pui8DestTemp = (uint8_t *)pui32Dest;
632     for(ui8BytCnt = 0; ui8BytCnt < ui8Length ; ui8BytCnt++)
633    	{
634     	*(pui8Dest+ui8BytCnt) = *(pui8DestTemp+ui8BytCnt);
635    	}
636 
637     return;
638 }
639 
640 //*****************************************************************************
641 //
642 //! Writes plaintext/ciphertext to data registers without blocking.
643 //!
644 //! \param ui32Base is the base address of the AES module.
645 //! \param pui8Src is a pointer to an array of words of data.
646 //! \param ui8Length the length can be from 1 to 16
647 //!
648 //! This function writes a block of either plaintext or ciphertext into the
649 //! AES module.  If the input is not ready, the function returns false
650 //! If the write completed successfully, the function returns true.
651 //!
652 //! \return True or false.
653 //
654 //*****************************************************************************
655 bool
AESDataWriteNonBlocking(uint32_t ui32Base,uint8_t * pui8Src,uint8_t ui8Length)656 AESDataWriteNonBlocking(uint32_t ui32Base, uint8_t *pui8Src, uint8_t ui8Length)
657 {
658 	volatile uint32_t pui32Src[4]={0,0,0,0};
659 	uint8_t ui8BytCnt;
660 	uint8_t *pui8SrcTemp;
661 
662 	//
663     // Check the arguments.
664     //
665     ASSERT(ui32Base == AES_BASE);
666     if((ui8Length == 0)||(ui8Length>16))
667    	{
668       	return(false);
669     }
670 
671     //
672     // Check if the input is ready.  If not, then return false.
673     //
674     if(!(AES_CTRL_INPUT_READY & (HWREG(ui32Base + AES_O_CTRL))))
675     {
676         return(false);
677     }
678 
679 
680     //
681     //Copy the data to a block memory
682     //
683     pui8SrcTemp = (uint8_t *)pui32Src;
684     for(ui8BytCnt = 0; ui8BytCnt < ui8Length ; ui8BytCnt++)
685 	{
686 		*(pui8SrcTemp+ui8BytCnt) = *(pui8Src+ui8BytCnt);
687 	}
688     //
689     // Write a block of data into the data registers.
690     //
691     HWREG(ui32Base + AES_O_DATA_IN_3) = pui32Src[0];
692     HWREG(ui32Base + AES_O_DATA_IN_2) = pui32Src[1];
693     HWREG(ui32Base + AES_O_DATA_IN_1) = pui32Src[2];
694     HWREG(ui32Base + AES_O_DATA_IN_0) = pui32Src[3];
695 
696     //
697     // Write successful, return true.
698     //
699     return(true);
700 }
701 
702 
703 //*****************************************************************************
704 //
705 //! Writes plaintext/ciphertext to data registers with blocking.
706 //!
707 //! \param ui32Base is the base address of the AES module.
708 //! \param pui8Src is a pointer to an array of bytes.
709 //! \param ui8Length the length can be from 1 to 16
710 //!
711 //! This function writes a block of either plaintext or ciphertext into the
712 //! AES module.  If the input is not ready, the function waits until it is
713 //! ready before performing the write.
714 //!
715 //! \return None.
716 //
717 //*****************************************************************************
718 
719 void
AESDataWrite(uint32_t ui32Base,uint8_t * pui8Src,uint8_t ui8Length)720 AESDataWrite(uint32_t ui32Base, uint8_t *pui8Src, uint8_t ui8Length)
721 {
722 	volatile uint32_t pui32Src[4]={0,0,0,0};
723 	uint8_t ui8BytCnt;
724 	uint8_t *pui8SrcTemp;
725     //
726     // Check the arguments.
727     //
728     ASSERT(ui32Base == AES_BASE);
729     if((ui8Length == 0)||(ui8Length>16))
730 	{
731     	return;
732 	}
733     //
734     // Wait for input ready.
735     //
736     while((AES_CTRL_INPUT_READY & (HWREG(ui32Base + AES_O_CTRL))) == 0)
737     {
738     }
739 
740     //
741     //Copy the data to a block memory
742     //
743     pui8SrcTemp = (uint8_t *)pui32Src;
744     for(ui8BytCnt = 0; ui8BytCnt < ui8Length ; ui8BytCnt++)
745    	{
746    		*(pui8SrcTemp+ui8BytCnt) = *(pui8Src+ui8BytCnt);
747    	}
748 
749     //
750     // Write a block of data into the data registers.
751     //
752     HWREG(ui32Base + AES_O_DATA_IN_3) = pui32Src[0];
753     HWREG(ui32Base + AES_O_DATA_IN_2) = pui32Src[1];
754     HWREG(ui32Base + AES_O_DATA_IN_1) = pui32Src[2];
755     HWREG(ui32Base + AES_O_DATA_IN_0) = pui32Src[3];
756 }
757 
758 
759 //*****************************************************************************
760 //
761 //! Used to process(transform) blocks of data, either encrypt or decrypt it.
762 //!
763 //! \param ui32Base is the base address of the AES module.
764 //! \param pui8Src is a pointer to the memory location where the input data
765 //! is stored.
766 //! \param pui8Dest is a pointer to the memory location output is written.
767 //! \param ui32Length is the length of the cryptographic data in bytes.
768 //!
769 //! This function iterates the encryption or decryption mechanism number over
770 //! the data length.  Before calling this function, ensure that the AES
771 //! module is properly configured the key, data size, mode, etc.  Only ECB,
772 //! CBC, CTR, ICM, CFB, XTS and F8 operating modes should be used.  The data
773 //! is processed in 4-word (16-byte) blocks.
774 //!
775 //! \note This function only supports values of \e ui32Length less than 2^32,
776 //! because the memory size is restricted to between 0 to 2^32 bytes.
777 //!
778 //! \return Returns true if data was processed successfully.  Returns false
779 //! if data processing failed.
780 //
781 //*****************************************************************************
782 bool
AESDataProcess(uint32_t ui32Base,uint8_t * pui8Src,uint8_t * pui8Dest,uint32_t ui32Length)783 AESDataProcess(uint32_t ui32Base, uint8_t *pui8Src, uint8_t *pui8Dest,
784                uint32_t ui32Length)
785 {
786     uint32_t ui32Count, ui32BlkCount, ui32ByteCount;
787 
788     //
789     // Check the arguments.
790     //
791     ASSERT(ui32Base == AES_BASE);
792 
793     //
794     // Write the length register first, which triggers the engine to start
795     // using this context.
796     //
797     AESDataLengthSet(AES_BASE, (uint64_t) ui32Length);
798 
799     //
800     // Now loop until the blocks are written.
801     //
802     ui32BlkCount = ui32Length/16;
803     for(ui32Count = 0; ui32Count < ui32BlkCount; ui32Count += 1)
804     {
805         //
806         // Write the data registers.
807         //
808         AESDataWrite(ui32Base, pui8Src + (ui32Count*16) ,16);
809 
810         //
811         // Read the data registers.
812         //
813         AESDataRead(ui32Base, pui8Dest + (ui32Count*16) ,16);
814 
815     }
816 
817     //
818     //Now handle the residue bytes
819     //
820     ui32ByteCount = ui32Length%16;
821     if(ui32ByteCount)
822     {
823     	//
824     	// Write the data registers.
825     	//
826     	AESDataWrite(ui32Base, pui8Src + (16*ui32BlkCount) ,ui32ByteCount);
827 
828     	//
829     	// Read the data registers.
830     	//
831         AESDataRead(ui32Base, pui8Dest + (16*ui32BlkCount) ,ui32ByteCount);
832     }
833 
834 
835 
836     //
837     // Return true to indicate successful completion of the function.
838     //
839     return(true);
840 }
841 //*****************************************************************************
842 //
843 //! Used to generate message authentication code (MAC) using CBC-MAC and F9 mode.
844 //!
845 //! \param ui32Base is the base address of the AES module.
846 //! \param pui8Src is a pointer to the memory location where the input data
847 //! is stored.
848 //! \param ui32Length  is the length of the cryptographic data in bytes.
849 //! \param pui8Tag is a pointer to a 4-word array where the hash tag is
850 //! written.
851 //!
852 //! This function processes data to produce a hash tag that can be used tor
853 //! authentication.   Before calling this function, ensure that the AES
854 //! module is properly configured the key, data size, mode, etc.  Only
855 //! CBC-MAC and F9 modes should be used.
856 //!
857 //! \return Returns true if data was processed successfully.  Returns false
858 //! if data processing failed.
859 //
860 //*****************************************************************************
861 bool
AESDataMAC(uint32_t ui32Base,uint8_t * pui8Src,uint32_t ui32Length,uint8_t * pui8Tag)862 AESDataMAC(uint32_t ui32Base, uint8_t *pui8Src, uint32_t ui32Length,
863             uint8_t *pui8Tag)
864 {
865     uint32_t ui32Count, ui32BlkCount, ui32ByteCount;
866 	//
867     // Check the arguments.
868     //
869     ASSERT(ui32Base == AES_BASE);
870 
871     //
872     // Write the length register first, which triggers the engine to start
873     // using this context.
874     //
875     AESDataLengthSet(AES_BASE, (uint64_t) ui32Length);
876 
877     //
878     // Write the data registers.
879     //
880 
881     //
882     // Now loop until the blocks are written.
883     //
884     ui32BlkCount = ui32Length/16;
885     for(ui32Count = 0; ui32Count < ui32BlkCount; ui32Count += 1)
886     {
887 	   //
888 	   // Write the data registers.
889 	   //
890 	   AESDataWrite(ui32Base, pui8Src + ui32Count*16 ,16);
891     }
892 
893     //
894     //Now handle the residue bytes
895     //
896     ui32ByteCount = ui32Length%16;
897     if(ui32ByteCount)
898     {
899 	   //
900 	   // Write the data registers.
901 	   //
902 	   AESDataWrite(ui32Base, pui8Src + (ui32Count*ui32BlkCount) ,ui32ByteCount);
903     }
904 
905     //
906     // Wait for the context data regsiters to be ready.
907     //
908     while((AES_CTRL_SVCTXTRDY & (HWREG(AES_BASE + AES_O_CTRL))) == 0)
909     {
910     }
911 
912     //
913     // Read the hash tag value.
914     //
915     AESTagRead(AES_BASE, pui8Tag);
916 
917     //
918     // Return true to indicate successful completion of the function.
919     //
920     return(true);
921 }
922 
923 //*****************************************************************************
924 //
925 //! Used for Authenticated encryption (AE) of the data. Processes and authenticates blocks of data,
926 //! either encrypt the data or decrypt the data.
927 //!
928 //! \param ui32Base  is the base address of the AES module.
929 //! \param pui8Src is a pointer to the memory location where the input data
930 //! is stored. The data must be padded to the 16-byte boundary.
931 //! \param pui8Dest is a pointer to the memory location output is written.
932 //! The space for written data must be rounded up to the 16-byte boundary.
933 //! \param ui32Length is the length of the cryptographic data in bytes.
934 //! \param pui8AuthSrc is a pointer to the memory location where the
935 //! additional authentication data is stored.  The data must be padded to the
936 //! 16-byte boundary.
937 //! \param ui32AuthLength is the length of the additional authentication
938 //! data in bytes.
939 //! \param pui8Tag is a pointer to a 4-word array where the hash tag is
940 //! written.
941 //!
942 //! This function encrypts or decrypts blocks of data in addition to
943 //! authentication data.  A hash tag is also produced.  Before calling this
944 //! function, ensure that the AES module is properly configured the key,
945 //! data size, mode, etc.  Only CCM and GCM modes should be used.
946 //!
947 //! \return Returns true if data was processed successfully.  Returns false
948 //! if data processing failed.
949 //
950 //*****************************************************************************
951 bool
AESDataProcessAE(uint32_t ui32Base,uint8_t * pui8Src,uint8_t * pui8Dest,uint32_t ui32Length,uint8_t * pui8AuthSrc,uint32_t ui32AuthLength,uint8_t * pui8Tag)952 AESDataProcessAE(uint32_t ui32Base, uint8_t *pui8Src, uint8_t *pui8Dest,
953                    uint32_t ui32Length, uint8_t *pui8AuthSrc,
954                    uint32_t ui32AuthLength, uint8_t *pui8Tag)
955 {
956     uint32_t ui32Count;
957 
958     //
959     // Check the arguments.
960     //
961     ASSERT(ui32Base == AES_BASE);
962 
963     //
964     // Set the data length.
965     //
966     AESDataLengthSet(AES_BASE, (uint64_t) ui32Length);
967 
968     //
969     // Set the additional authentication data length.
970     //
971     AESAuthDataLengthSet(AES_BASE, ui32AuthLength);
972 
973     //
974     // Now loop until the authentication data blocks are written.
975     //
976     for(ui32Count = 0; ui32Count < ui32AuthLength; ui32Count += 16)
977     {
978         //
979         // Write the data registers.
980         //
981         AESDataWrite(ui32Base, pui8AuthSrc + (ui32Count),16);
982     }
983 
984     //
985     // Now loop until the data blocks are written.
986     //
987     for(ui32Count = 0; ui32Count < ui32Length; ui32Count += 16)
988     {
989         //
990         // Write the data registers.
991         //
992         AESDataWrite(ui32Base, pui8Src + (ui32Count),16);
993 
994         //
995         //
996         // Read the data registers.
997         //
998         AESDataRead(ui32Base, pui8Dest + (ui32Count),16);
999     }
1000 
1001     //
1002     // Wait for the context data regsiters to be ready.
1003     //
1004     while((AES_CTRL_SVCTXTRDY & (HWREG(AES_BASE + AES_O_CTRL))) == 0)
1005     {
1006     }
1007 
1008     //
1009     // Read the hash tag value.
1010     //
1011     AESTagRead(AES_BASE, pui8Tag);
1012 
1013     //
1014     // Return true to indicate successful completion of the function.
1015     //
1016     return(true);
1017 }
1018 
1019 //*****************************************************************************
1020 //
1021 //! Returns the current AES module interrupt status.
1022 //!
1023 //! \param ui32Base is the base address of the AES module.
1024 //! \param bMasked is \b false if the raw interrupt status is required and
1025 //! \b true if the masked interrupt status is required.
1026 //!
1027 //! \return Returns a bit mask of the interrupt sources, which is a logical OR
1028 //! of any of the following:
1029 //!
1030 //! - \b AES_INT_CONTEXT_IN - Context interrupt
1031 //! - \b AES_INT_CONTEXT_OUT - Authentication tag (and IV) interrupt.
1032 //! - \b AES_INT_DATA_IN - Data input interrupt
1033 //! - \b AES_INT_DATA_OUT - Data output interrupt
1034 //! - \b AES_INT_DMA_CONTEXT_IN - Context DMA done interrupt
1035 //! - \b AES_INT_DMA_CONTEXT_OUT - Authentication tag (and IV) DMA done
1036 //! interrupt
1037 //! - \b AES_INT_DMA_DATA_IN - Data input DMA done interrupt
1038 //! - \b AES_INT_DMA_DATA_OUT - Data output DMA done interrupt
1039 //
1040 //*****************************************************************************
1041 uint32_t
AESIntStatus(uint32_t ui32Base,bool bMasked)1042 AESIntStatus(uint32_t ui32Base, bool bMasked)
1043 {
1044     uint32_t ui32Temp;
1045     uint32_t ui32IrqEnable;
1046 
1047     //
1048     // Check the arguments.
1049     //
1050     ASSERT(ui32Base == AES_BASE);
1051 
1052     //
1053     // Read the IRQ status register and return the value.
1054     //
1055     if(bMasked)
1056     {
1057         ui32Temp = HWREG(DTHE_BASE + DTHE_O_AES_MIS);
1058         ui32IrqEnable = HWREG(ui32Base + AES_O_IRQENABLE);
1059         return((HWREG(ui32Base + AES_O_IRQSTATUS) &
1060                 ui32IrqEnable) | ((ui32Temp & 0x0000000F) << 16));
1061     }
1062     else
1063     {
1064         ui32Temp = HWREG(DTHE_BASE + DTHE_O_AES_RIS);
1065         return(HWREG(ui32Base + AES_O_IRQSTATUS) |
1066                ((ui32Temp & 0x0000000F) << 16));
1067     }
1068 }
1069 
1070 //*****************************************************************************
1071 //
1072 //! Enables AES module interrupts.
1073 //!
1074 //! \param ui32Base is the base address of the AES module.
1075 //! \param ui32IntFlags is a bit mask of the interrupt sources to enable.
1076 //!
1077 //! This function enables the interrupts in the AES module. The \e ui32IntFlags
1078 //! parameter is the logical OR of any of the following:
1079 //!
1080 //! - \b AES_INT_CONTEXT_IN - Context interrupt
1081 //! - \b AES_INT_CONTEXT_OUT - Authentication tag (and IV) interrupt
1082 //! - \b AES_INT_DATA_IN - Data input interrupt
1083 //! - \b AES_INT_DATA_OUT - Data output interrupt
1084 //! - \b AES_INT_DMA_CONTEXT_IN - Context DMA done interrupt
1085 //! - \b AES_INT_DMA_CONTEXT_OUT - Authentication tag (and IV) DMA done
1086 //! interrupt
1087 //! - \b AES_INT_DMA_DATA_IN - Data input DMA done interrupt
1088 //! - \b AES_INT_DMA_DATA_OUT - Data output DMA done interrupt
1089 //!
1090 //! \note Interrupts that have been previously been enabled are not disabled
1091 //! when this function is called.
1092 //!
1093 //! \return None.
1094 //
1095 //*****************************************************************************
1096 void
AESIntEnable(uint32_t ui32Base,uint32_t ui32IntFlags)1097 AESIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
1098 {
1099     //
1100     // Check the arguments.
1101     //
1102     ASSERT(ui32Base == AES_BASE);
1103     ASSERT((ui32IntFlags == AES_INT_CONTEXT_IN) ||
1104            (ui32IntFlags == AES_INT_CONTEXT_OUT) ||
1105            (ui32IntFlags == AES_INT_DATA_IN) ||
1106            (ui32IntFlags == AES_INT_DATA_OUT) ||
1107            (ui32IntFlags == AES_INT_DMA_CONTEXT_IN) ||
1108            (ui32IntFlags == AES_INT_DMA_CONTEXT_OUT) ||
1109            (ui32IntFlags == AES_INT_DMA_DATA_IN) ||
1110            (ui32IntFlags == AES_INT_DMA_DATA_OUT));
1111 
1112     //
1113     // Set the flags.
1114     //
1115     HWREG(DTHE_BASE + DTHE_O_AES_IM) &= ~((ui32IntFlags & 0x000F0000) >> 16);
1116     HWREG(ui32Base + AES_O_IRQENABLE) |= ui32IntFlags & 0x0000ffff;
1117 }
1118 
1119 //*****************************************************************************
1120 //
1121 //! Disables AES module interrupts.
1122 //!
1123 //! \param ui32Base is the base address of the AES module.
1124 //! \param ui32IntFlags is a bit mask of the interrupt sources to disable.
1125 //!
1126 //! This function disables the interrupt sources in the AES module. The
1127 //! \e ui32IntFlags parameter is the logical OR of any of the following:
1128 //!
1129 //! - \b AES_INT_CONTEXT_IN - Context interrupt
1130 //! - \b AES_INT_CONTEXT_OUT - Authentication tag (and IV) interrupt
1131 //! - \b AES_INT_DATA_IN - Data input interrupt
1132 //! - \b AES_INT_DATA_OUT - Data output interrupt
1133 //! - \b AES_INT_DMA_CONTEXT_IN - Context DMA done interrupt
1134 //! - \b AES_INT_DMA_CONTEXT_OUT - Authentication tag (and IV) DMA done
1135 //! interrupt
1136 //! - \b AES_INT_DMA_DATA_IN - Data input DMA done interrupt
1137 //! - \b AES_INT_DMA_DATA_OUT - Data output DMA done interrupt
1138 //!
1139 //! \note The DMA done interrupts are the only interrupts that can be cleared.
1140 //! The remaining interrupts can be disabled instead using AESIntDisable().
1141 //!
1142 //! \return None.
1143 //
1144 //*****************************************************************************
1145 void
AESIntDisable(uint32_t ui32Base,uint32_t ui32IntFlags)1146 AESIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
1147 {
1148     //
1149     // Check the arguments.
1150     //
1151     ASSERT(ui32Base == AES_BASE);
1152     ASSERT((ui32IntFlags == AES_INT_CONTEXT_IN) ||
1153            (ui32IntFlags == AES_INT_CONTEXT_OUT) ||
1154            (ui32IntFlags == AES_INT_DATA_IN) ||
1155            (ui32IntFlags == AES_INT_DATA_OUT) ||
1156            (ui32IntFlags == AES_INT_DMA_CONTEXT_IN) ||
1157            (ui32IntFlags == AES_INT_DMA_CONTEXT_OUT) ||
1158            (ui32IntFlags == AES_INT_DMA_DATA_IN) ||
1159            (ui32IntFlags == AES_INT_DMA_DATA_OUT));
1160 
1161     //
1162     // Clear the flags.
1163     //
1164     HWREG(DTHE_BASE + DTHE_O_AES_IM) |= ((ui32IntFlags & 0x000F0000) >> 16);
1165     HWREG(ui32Base + AES_O_IRQENABLE) &= ~(ui32IntFlags & 0x0000ffff);
1166 }
1167 
1168 //*****************************************************************************
1169 //
1170 //! Clears AES module interrupts.
1171 //!
1172 //! \param ui32Base is the base address of the AES module.
1173 //! \param ui32IntFlags is a bit mask of the interrupt sources to disable.
1174 //!
1175 //! This function clears the interrupt sources in the AES module.  The
1176 //! \e ui32IntFlags parameter is the logical OR of any of the following:
1177 //!
1178 //! - \b AES_INT_DMA_CONTEXT_IN - Context DMA done interrupt
1179 //! - \b AES_INT_DMA_CONTEXT_OUT - Authentication tag (and IV) DMA done
1180 //! interrupt
1181 //! - \b AES_INT_DMA_DATA_IN - Data input DMA done interrupt
1182 //! - \b AES_INT_DMA_DATA_OUT - Data output DMA done interrupt
1183 //!
1184 //! \note Only the DMA done interrupts can be cleared.  The remaining
1185 //! interrupts should be disabled with AESIntDisable().
1186 //!
1187 //! \return None.
1188 //
1189 //*****************************************************************************
1190 void
AESIntClear(uint32_t ui32Base,uint32_t ui32IntFlags)1191 AESIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
1192 {
1193     //
1194     // Check the arguments.
1195     //
1196     ASSERT(ui32Base == AES_BASE);
1197     ASSERT((ui32IntFlags == AES_INT_DMA_CONTEXT_IN) ||
1198            (ui32IntFlags == AES_INT_DMA_CONTEXT_OUT) ||
1199            (ui32IntFlags == AES_INT_DMA_DATA_IN) ||
1200            (ui32IntFlags == AES_INT_DMA_DATA_OUT));
1201 
1202     HWREG(DTHE_BASE + DTHE_O_AES_IC) = ((ui32IntFlags >> 16) & 0x0000000F);
1203 }
1204 
1205 //*****************************************************************************
1206 //
1207 //! Registers an interrupt handler for the AES module.
1208 //!
1209 //! \param ui32Base is the base address of the AES module.
1210 //! \param pfnHandler is a pointer to the function to be called when the
1211 //! enabled AES interrupts occur.
1212 //!
1213 //! This function registers the interrupt handler in the interrupt vector
1214 //! table, and enables AES interrupts on the interrupt controller; specific AES
1215 //! interrupt sources must be enabled using AESIntEnable().  The interrupt
1216 //! handler being registered must clear the source of the interrupt using
1217 //! AESIntClear().
1218 //!
1219 //! If the application is using a static interrupt vector table stored in
1220 //! flash, then it is not necessary to register the interrupt handler this way.
1221 //! Instead, IntEnable() is used to enable AES interrupts on the
1222 //! interrupt controller.
1223 //!
1224 //! \sa IntRegister() for important information about registering interrupt
1225 //! handlers.
1226 //!
1227 //! \return None.
1228 //
1229 //*****************************************************************************
1230 void
AESIntRegister(uint32_t ui32Base,void (* pfnHandler)(void))1231 AESIntRegister(uint32_t ui32Base, void(*pfnHandler)(void))
1232 {
1233     //
1234     // Check the arguments.
1235     //
1236     ASSERT(ui32Base == AES_BASE);
1237 
1238     //
1239     // Register the interrupt handler.
1240     //
1241     IntRegister(INT_AES, pfnHandler);
1242 
1243     //
1244     // Enable the interrupt
1245     //
1246     IntEnable(INT_AES);
1247 }
1248 
1249 //*****************************************************************************
1250 //
1251 //! Unregisters an interrupt handler for the AES module.
1252 //!
1253 //! \param ui32Base is the base address of the AES module.
1254 //!
1255 //! This function unregisters the previously registered interrupt handler and
1256 //! disables the interrupt in the interrupt controller.
1257 //!
1258 //! \sa IntRegister() for important information about registering interrupt
1259 //! handlers.
1260 //!
1261 //! \return None.
1262 //
1263 //*****************************************************************************
1264 void
AESIntUnregister(uint32_t ui32Base)1265 AESIntUnregister(uint32_t ui32Base)
1266 {
1267     //
1268     // Check the arguments.
1269     //
1270     ASSERT(ui32Base == AES_BASE);
1271 
1272     //
1273     // Disable the interrupt.
1274     //
1275     IntDisable(INT_AES);
1276 
1277     //
1278     // Unregister the interrupt handler.
1279     //
1280     IntUnregister(INT_AES);
1281 }
1282 
1283 //*****************************************************************************
1284 //
1285 //! Enables uDMA requests for the AES module.
1286 //!
1287 //! \param ui32Base is the base address of the AES module.
1288 //! \param ui32Flags is a bit mask of the uDMA requests to be enabled.
1289 //!
1290 //! This function enables the uDMA request sources in the AES module.
1291 //! The \e ui32Flags parameter is the logical OR of any of the following:
1292 //!
1293 //! - \b AES_DMA_DATA_IN
1294 //! - \b AES_DMA_DATA_OUT
1295 //! - \b AES_DMA_CONTEXT_IN
1296 //! - \b AES_DMA_CONTEXT_OUT
1297 //!
1298 //! \return None.
1299 //
1300 //*****************************************************************************
1301 void
AESDMAEnable(uint32_t ui32Base,uint32_t ui32Flags)1302 AESDMAEnable(uint32_t ui32Base, uint32_t ui32Flags)
1303 {
1304     //
1305     // Check the arguments.
1306     //
1307     ASSERT(ui32Base == AES_BASE);
1308     ASSERT((ui32Flags == AES_DMA_DATA_IN) ||
1309            (ui32Flags == AES_DMA_DATA_OUT) ||
1310            (ui32Flags == AES_DMA_CONTEXT_IN) ||
1311            (ui32Flags == AES_DMA_CONTEXT_OUT));
1312 
1313     //
1314     // Set the flags in the current register value.
1315     //
1316     HWREG(ui32Base + AES_O_SYSCONFIG) |= ui32Flags;
1317 }
1318 
1319 //*****************************************************************************
1320 //
1321 //! Disables uDMA requests for the AES module.
1322 //!
1323 //! \param ui32Base is the base address of the AES module.
1324 //! \param ui32Flags is a bit mask of the uDMA requests to be disabled.
1325 //!
1326 //! This function disables the uDMA request sources in the AES module.
1327 //! The \e ui32Flags parameter is the logical OR of any of the
1328 //! following:
1329 //!
1330 //! - \b AES_DMA_DATA_IN
1331 //! - \b AES_DMA_DATA_OUT
1332 //! - \b AES_DMA_CONTEXT_IN
1333 //! - \b AES_DMA_CONTEXT_OUT
1334 //!
1335 //! \return None.
1336 //
1337 //*****************************************************************************
1338 void
AESDMADisable(uint32_t ui32Base,uint32_t ui32Flags)1339 AESDMADisable(uint32_t ui32Base, uint32_t ui32Flags)
1340 {
1341     //
1342     // Check the arguments.
1343     //
1344     ASSERT(ui32Base == AES_BASE);
1345     ASSERT((ui32Flags == AES_DMA_DATA_IN) ||
1346            (ui32Flags == AES_DMA_DATA_OUT) ||
1347            (ui32Flags == AES_DMA_CONTEXT_IN) ||
1348            (ui32Flags == AES_DMA_CONTEXT_OUT));
1349 
1350     //
1351     // Clear the flags in the current register value.
1352     //
1353     HWREG(ui32Base + AES_O_SYSCONFIG) &= ~ui32Flags;
1354 }
1355 
1356 //*****************************************************************************
1357 //
1358 // Close the Doxygen group.
1359 //! @}
1360 //
1361 //*****************************************************************************
1362