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