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