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