1 /******************************************************************************
2 *  Filename:       aes.h
3 *  Revised:        2019-01-25 14:45:16 +0100 (Fri, 25 Jan 2019)
4 *  Revision:       54287
5 *
6 *  Description:    AES header file.
7 *
8 *  Copyright (c) 2015 - 2020, Texas Instruments Incorporated
9 *  All rights reserved.
10 *
11 *  Redistribution and use in source and binary forms, with or without
12 *  modification, are permitted provided that the following conditions are met:
13 *
14 *  1) Redistributions of source code must retain the above copyright notice,
15 *     this list of conditions and the following disclaimer.
16 *
17 *  2) Redistributions in binary form must reproduce the above copyright notice,
18 *     this list of conditions and the following disclaimer in the documentation
19 *     and/or other materials provided with the distribution.
20 *
21 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 *     be used to endorse or promote products derived from this software without
23 *     specific prior written permission.
24 *
25 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 *  POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 //*****************************************************************************
40 //
41 //! \addtogroup peripheral_group
42 //! @{
43 //! \addtogroup aes_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #ifndef __AES_H__
49 #define __AES_H__
50 
51 //*****************************************************************************
52 //
53 // If building with a C++ compiler, make all of the definitions in this header
54 // have a C binding.
55 //
56 //*****************************************************************************
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 #include <string.h>
65 #include "../inc/hw_types.h"
66 #include "../inc/hw_memmap.h"
67 #include "../inc/hw_ints.h"
68 #include "../inc/hw_crypto.h"
69 #include "debug.h"
70 #include "interrupt.h"
71 #include "cpu.h"
72 
73 //*****************************************************************************
74 //
75 // Support for DriverLib in ROM:
76 // This section renames all functions that are not "static inline", so that
77 // calling these functions will default to implementation in flash. At the end
78 // of this file a second renaming will change the defaults to implementation in
79 // ROM for available functions.
80 //
81 // To force use of the implementation in flash, e.g. for debugging:
82 // - Globally: Define DRIVERLIB_NOROM at project level
83 // - Per function: Use prefix "NOROM_" when calling the function
84 //
85 //*****************************************************************************
86 #if !defined(DOXYGEN)
87     #define AESStartDMAOperation            NOROM_AESStartDMAOperation
88     #define AESSetInitializationVector      NOROM_AESSetInitializationVector
89     #define AESWriteCCMInitializationVector NOROM_AESWriteCCMInitializationVector
90     #define AESReadTag                      NOROM_AESReadTag
91     #define AESVerifyTag                    NOROM_AESVerifyTag
92     #define AESWriteToKeyStore              NOROM_AESWriteToKeyStore
93     #define AESReadFromKeyStore             NOROM_AESReadFromKeyStore
94     #define AESWaitForIRQFlags              NOROM_AESWaitForIRQFlags
95     #define AESConfigureCCMCtrl             NOROM_AESConfigureCCMCtrl
96 #endif
97 
98 
99 //*****************************************************************************
100 //
101 // Values that can be passed to AESIntEnable, AESIntDisable, and AESIntClear
102 // as the intFlags parameter, and returned from AESIntStatus.
103 // Only AES_DMA_IN_DONE and AES_RESULT_RDY are routed to the NVIC. Check each
104 // function to see if it supports other interrupt status flags.
105 //
106 //*****************************************************************************
107 #define AES_DMA_IN_DONE                 CRYPTO_IRQEN_DMA_IN_DONE_M
108 #define AES_RESULT_RDY                  CRYPTO_IRQEN_RESULT_AVAIL_M
109 #define AES_DMA_BUS_ERR                 CRYPTO_IRQCLR_DMA_BUS_ERR_M
110 #define AES_KEY_ST_WR_ERR               CRYPTO_IRQCLR_KEY_ST_WR_ERR_M
111 #define AES_KEY_ST_RD_ERR               CRYPTO_IRQCLR_KEY_ST_RD_ERR_M
112 
113 
114 //*****************************************************************************
115 //
116 //  General constants
117 //
118 //*****************************************************************************
119 
120 // AES module return codes
121 #define AES_SUCCESS                     0
122 #define AES_KEYSTORE_ERROR              1
123 #define AES_KEYSTORE_AREA_INVALID       2
124 #define AES_DMA_BUSY                    3
125 #define AES_DMA_ERROR                   4
126 #define AES_TAG_NOT_READY               5
127 #define AES_TAG_VERIFICATION_FAILED     6
128 
129 // Key store module defines
130 #define AES_IV_LENGTH_BYTES             16
131 #define AES_TAG_LENGTH_BYTES            16
132 #define AES_128_KEY_LENGTH_BYTES        (128 / 8)
133 #define AES_192_KEY_LENGTH_BYTES        (192 / 8)
134 #define AES_256_KEY_LENGTH_BYTES        (256 / 8)
135 
136 #define AES_BLOCK_SIZE                  16
137 
138 // DMA status codes
139 #define AES_DMA_CHANNEL0_ACTIVE         CRYPTO_DMASTAT_CH0_ACT_M
140 #define AES_DMA_CHANNEL1_ACTIVE         CRYPTO_DMASTAT_CH1_ACT_M
141 #define AES_DMA_PORT_ERROR              CRYPTO_DMASTAT_PORT_ERR_M
142 
143 // Crypto module operation types
144 #define AES_ALGSEL_AES                  CRYPTO_ALGSEL_AES_M
145 #define AES_ALGSEL_KEY_STORE            CRYPTO_ALGSEL_KEY_STORE_M
146 #define AES_ALGSEL_TAG                  CRYPTO_ALGSEL_TAG_M
147 
148 
149 //*****************************************************************************
150 //
151 // For 128-bit keys, all 8 key area locations from 0 to 7 are valid.
152 // A 256-bit key requires two consecutive Key Area locations. The base key area
153 // may be odd. Do not attempt to write a 256-bit key to AES_KEY_AREA_7.
154 //
155 //*****************************************************************************
156 #define AES_KEY_AREA_0          0
157 #define AES_KEY_AREA_1          1
158 #define AES_KEY_AREA_2          2
159 #define AES_KEY_AREA_3          3
160 #define AES_KEY_AREA_4          4
161 #define AES_KEY_AREA_5          5
162 #define AES_KEY_AREA_6          6
163 #define AES_KEY_AREA_7          7
164 
165 //*****************************************************************************
166 //
167 // Defines for the AES-CTR mode counter width
168 //
169 //*****************************************************************************
170 #define AES_CTR_WIDTH_32        0x0
171 #define AES_CTR_WIDTH_64        0x1
172 #define AES_CTR_WIDTH_96        0x2
173 #define AES_CTR_WIDTH_128       0x3
174 
175 //*****************************************************************************
176 //
177 // API Functions and prototypes
178 //
179 //*****************************************************************************
180 
181 //*****************************************************************************
182 //
183 //! \brief Start a crypto DMA operation
184 //!
185 //!        Enable the crypto DMA channels, configure the channel addresses,
186 //!        and set the length of the data transfer.
187 //!        Setting the length of the data transfer automatically starts the
188 //!        transfer. It is also used by the hardware module as a signal to
189 //!        begin the encryption, decryption, or MAC operation.
190 //!
191 //! \param [in] channel0Addr A pointer to the address channel 0 shall use.
192 //!
193 //! \param [in] channel0Length Length of the data in bytes to be read from or
194 //!                            written to at channel0Addr. Set to 0 to not set up
195 //!                            this channel. Permitted ranges are mode dependent
196 //!                            and displayed below.
197 //!                            - ECB:        [16]
198 //!                            - CBC:        [1, sizeof(RAM)]
199 //!                            - CBC-MAC:    [1, sizeof(RAM)]
200 //!                            - CCM:        [1, sizeof(RAM)]
201 //!
202 //! \param [out] channel1Addr A pointer to the address channel 1 shall use.
203 //!
204 //! \param [in] channel1Length Length of the data in bytes to be read from or
205 //!                            written to at channel1Addr. Set to 0 to not set up
206 //!                            this channel.Permitted ranges are mode dependent
207 //!                            and displayed below.
208 //!                            - ECB:        [16]
209 //!                            - CBC:        [1, sizeof(RAM)]
210 //!                            - CBC-MAC:    [1, sizeof(RAM)]
211 //!                            - CCM:        [1, sizeof(RAM)]
212 //!
213 //! \return None
214 //
215 //*****************************************************************************
216 extern void AESStartDMAOperation(const uint8_t *channel0Addr, uint32_t channel0Length,  uint8_t *channel1Addr, uint32_t channel1Length);
217 
218 //*****************************************************************************
219 //
220 //! \brief Write the initialization vector (IV) to the crypto module.
221 //!
222 //!         Depending on the mode of operation, the tag must be constructed
223 //!         differently:
224 //!             - CBC:      No special care must be taken. Any 128-bit IV
225 //!                         (initialization vector) will suffice.
226 //!             - CBC-MAC:  IV's must be all 0's.
227 //!             - CCM:      Only 12 and 13 byte IV's are permitted. See code
228 //!                         below for formatting.
229 //! \code
230 //!                         uint8_t initVectorLength = 12;  // Could also be 13
231 //!
232 //!                         union {
233 //!                             uint32_t word[4];
234 //!                             uint8_t byte[16];
235 //!                         } initVector;
236 //!
237 //!                         uint8_t initVectorUnformatted[initVectorLength];
238 //!
239 //!                         // This is the same field length value that is written to the ctrl register
240 //!                         initVector.byte[0] = L - 1;
241 //!
242 //!                         memcpy(&initVector.byte[1], initVectorUnformatted, initVectorLength);
243 //!
244 //!                         // Fill the remaining bytes with zeros
245 //!                         for (initVectorLength++; initVectorLength < sizeof(initVector.byte); initVectorLength++) {
246 //!                             initVector.byte[initVectorLength] = 0;
247 //!                         }
248 //! \endcode
249 //!
250 //! \param [in] initializationVector Pointer to an array with four 32-bit elements
251 //!                                  to be used as initialization vector.
252 //!                                  Elements of array must be word aligned in memory.
253 //!
254 //! \return None
255 //
256 //*****************************************************************************
257 extern void AESSetInitializationVector(const uint32_t *initializationVector);
258 
259 //*****************************************************************************
260 //
261 //! \brief Generate and load the initialization vector for a CCM operation.
262 //!
263 //!
264 //! \param [in] nonce Pointer to a nonce of length \c nonceLength.
265 //!
266 //! \param [in] nonceLength Number of bytes to copy from \c nonce when creating
267 //!                         the CCM IV. The L-value is also derived from it.
268 //!
269 //! \return None
270 //
271 //*****************************************************************************
272 extern void AESWriteCCMInitializationVector(const uint8_t *nonce, uint32_t nonceLength);
273 
274 //*****************************************************************************
275 //
276 //! \brief Read the tag out from the crypto module.
277 //!
278 //! This function copies the \c tagLength bytes from the tag calculated by the
279 //! crypto module in CCM, GCM, or CBC-MAC mode to \c tag.
280 //!
281 //! \param [out] tag Pointer to an array of \c tagLength bytes.
282 //!
283 //! \param [in] tagLength Number of bytes to copy to \c tag.
284 //!
285 //! \return Returns a status code depending on the result of the transfer.
286 //! - \ref AES_TAG_NOT_READY if the tag is not ready yet
287 //! - \ref AES_SUCCESS otherwise
288 //
289 //*****************************************************************************
290 extern uint32_t AESReadTag(uint8_t *tag, uint32_t tagLength);
291 
292 //*****************************************************************************
293 //
294 //! \brief Verifies the provided \c tag against calculated one
295 //!
296 //! This function compares the provided tag against the tag calculated by the
297 //! crypto module during the last CCM, GCM, or CBC-MAC
298 //!
299 //! This function copies the \c tagLength bytes from the tag calculated by the
300 //! crypto module in CCM, GCM, or CBC-MAC mode to \c tag.
301 //!
302 //! \param [in] tag Pointer to an array of \c tagLength bytes.
303 //!
304 //! \param [in] tagLength Number of bytes to compare.
305 //!
306 //! \return Returns a status code depending on the result of the transfer.
307 //! - \ref AES_TAG_VERIFICATION_FAILED if the verification failed
308 //! - \ref AES_SUCCESS otherwise
309 //
310 //*****************************************************************************
311 extern uint32_t AESVerifyTag(const uint8_t *tag, uint32_t tagLength);
312 
313 //*****************************************************************************
314 //
315 //! \brief Transfer a key from main memory to a key area within the key store.
316 //!
317 //!     The crypto DMA transfers the key and function does not return until
318 //!     the operation completes.
319 //!     The keyStore can only contain valid keys of one \c aesKeyLength at
320 //!     any one point in time. The keyStore cannot contain both 128-bit and
321 //!     256-bit keys simultaneously. When a key of a different \c aesKeyLength
322 //!     from the previous \c aesKeyLength is loaded, all previous keys are
323 //!     invalidated.
324 //!
325 //! \param [in] aesKey Pointer to key. Does not need to be word-aligned.
326 //!
327 //! \param [in] aesKeyLength The key size in bytes. Currently, 128-bit, 192-bit,
328 //!                          and 256-bit keys are supported.
329 //! - \ref AES_128_KEY_LENGTH_BYTES
330 //! - \ref AES_192_KEY_LENGTH_BYTES
331 //! - \ref AES_256_KEY_LENGTH_BYTES
332 //!
333 //! \param [in] keyStoreArea The key store area to transfer the key to.
334 //!                          When using 128-bit keys, only the specified key store
335 //!                          area will be occupied.
336 //!                          When using 256-bit or 192-bit keys, two consecutive key areas
337 //!                          are used to store the key.
338 //! - \ref AES_KEY_AREA_0
339 //! - \ref AES_KEY_AREA_1
340 //! - \ref AES_KEY_AREA_2
341 //! - \ref AES_KEY_AREA_3
342 //! - \ref AES_KEY_AREA_4
343 //! - \ref AES_KEY_AREA_5
344 //! - \ref AES_KEY_AREA_6
345 //! - \ref AES_KEY_AREA_7
346 //!
347 //!     When using 256-bit or 192-bit keys, the 8 \c keyStoreArea's are
348 //!     split into four sets of two. Selecting any \c keyStoreArea automatically
349 //!     occupies the second \c keyStoreArea of the tuples below:
350 //!
351 //! - (\ref AES_KEY_AREA_0, \ref AES_KEY_AREA_1)
352 //! - (\ref AES_KEY_AREA_2, \ref AES_KEY_AREA_3)
353 //! - (\ref AES_KEY_AREA_4, \ref AES_KEY_AREA_5)
354 //! - (\ref AES_KEY_AREA_6, \ref AES_KEY_AREA_7)
355 //!
356 //!     For example: if \c keyStoreArea == \ref AES_KEY_AREA_2,
357 //!     both \ref AES_KEY_AREA_2 and \ref AES_KEY_AREA_3 are occupied.
358 //!     If \c keyStoreArea == \ref AES_KEY_AREA_5, both \ref AES_KEY_AREA_4 and \ref AES_KEY_AREA_5 are occupied.
359 //!
360 //! \return Returns a status code depending on the result of the transfer.
361 //!         If there was an error in the read process itself, an error is
362 //!         returned.
363 //!         Otherwise, a success code is returned.
364 //! - \ref AES_KEYSTORE_ERROR
365 //! - \ref AES_SUCCESS
366 //!
367 //! \sa AESReadFromKeyStore
368 //
369 //*****************************************************************************
370 extern uint32_t AESWriteToKeyStore(const uint8_t *aesKey, uint32_t aesKeyLength, uint32_t keyStoreArea);
371 
372 //*****************************************************************************
373 //
374 //! \brief Transfer a key from key store area to the internal buffers within
375 //!        the hardware module.
376 //!
377 //!     The function polls until the transfer is complete.
378 //!
379 //! \param [in] keyStoreArea The key store area to transfer the key from. When using
380 //!                          256-bit keys, either of the occupied key areas may be
381 //!                          specified to load the key. There is no need to specify
382 //!                          the length of the key here as the key store keeps track
383 //!                          of how long a key associated with any valid key area is
384 //!                          and where is starts.
385 //! - \ref AES_KEY_AREA_0
386 //! - \ref AES_KEY_AREA_1
387 //! - \ref AES_KEY_AREA_2
388 //! - \ref AES_KEY_AREA_3
389 //! - \ref AES_KEY_AREA_4
390 //! - \ref AES_KEY_AREA_5
391 //! - \ref AES_KEY_AREA_6
392 //! - \ref AES_KEY_AREA_7
393 //!
394 //! \return Returns a status code depending on the result of the transfer.
395 //!         When specifying a \c keyStoreArea value without a valid key in it an
396 //!         error is returned.
397 //!         If there was an error in the read process itself, an error is
398 //!         returned.
399 //!         Otherwise, a success code is returned.
400 //! - \ref AES_KEYSTORE_AREA_INVALID
401 //! - \ref AES_KEYSTORE_ERROR
402 //! - \ref AES_SUCCESS
403 //!
404 //! \sa AESWriteToKeyStore
405 //
406 //*****************************************************************************
407 extern uint32_t AESReadFromKeyStore(uint32_t keyStoreArea);
408 
409 
410 //*****************************************************************************
411 //
412 //! \brief Poll the interrupt status register and clear when done.
413 //!
414 //!        This function polls until one of the bits in the \c irqFlags is
415 //!        asserted. Only \ref AES_DMA_IN_DONE and \ref AES_RESULT_RDY can actually
416 //!        trigger the interrupt line. That means that one of those should
417 //!        always be included in \c irqFlags and will always be returned together
418 //!        with any error codes.
419 //!
420 //! \param [in] irqFlags IRQ flags to poll and mask that the status register will be
421 //!                      masked with. May consist of any bitwise OR of the flags
422 //!                      below that includes at least one of
423 //!                      \ref AES_DMA_IN_DONE or \ref AES_RESULT_RDY :
424 //! - \ref AES_DMA_IN_DONE
425 //! - \ref AES_RESULT_RDY
426 //! - \ref AES_DMA_BUS_ERR
427 //! - \ref AES_KEY_ST_WR_ERR
428 //! - \ref AES_KEY_ST_RD_ERR
429 //!
430 //! \return Returns the IRQ status register masked with \c irqFlags. May be any
431 //!         bitwise OR of the following masks:
432 //! - \ref AES_DMA_IN_DONE
433 //! - \ref AES_RESULT_RDY
434 //! - \ref AES_DMA_BUS_ERR
435 //! - \ref AES_KEY_ST_WR_ERR
436 //! - \ref AES_KEY_ST_RD_ERR
437 //
438 //*****************************************************************************
439 extern uint32_t AESWaitForIRQFlags(uint32_t irqFlags);
440 
441 //*****************************************************************************
442 //
443 //! \brief Configure AES engine for CCM operation.
444 //!
445 //! \param [in] nonceLength Length of the nonce. Must be <= 14.
446 //!
447 //! \param [in] macLength Length of the MAC. Must be <= 16.
448 //!
449 //! \param [in] encrypt Whether to set up an encrypt or decrypt operation.
450 //! - true: encrypt
451 //! - false: decrypt
452 //!
453 //! \return None
454 //
455 //*****************************************************************************
456 extern void AESConfigureCCMCtrl(uint32_t nonceLength, uint32_t macLength, bool encrypt);
457 
458 //*****************************************************************************
459 //
460 //! \brief Invalidate a key in the key store
461 //!
462 //! \param [in] keyStoreArea is the entry in the key store to invalidate. This
463 //!                          permanently deletes the key from the key store.
464 //! - \ref AES_KEY_AREA_0
465 //! - \ref AES_KEY_AREA_1
466 //! - \ref AES_KEY_AREA_2
467 //! - \ref AES_KEY_AREA_3
468 //! - \ref AES_KEY_AREA_4
469 //! - \ref AES_KEY_AREA_5
470 //! - \ref AES_KEY_AREA_6
471 //! - \ref AES_KEY_AREA_7
472 //!
473 //! \return None
474 //
475 //*****************************************************************************
AESInvalidateKey(uint32_t keyStoreArea)476 __STATIC_INLINE void AESInvalidateKey(uint32_t keyStoreArea)
477 {
478     ASSERT((keyStoreArea == AES_KEY_AREA_0) ||
479            (keyStoreArea == AES_KEY_AREA_1) ||
480            (keyStoreArea == AES_KEY_AREA_2) ||
481            (keyStoreArea == AES_KEY_AREA_3) ||
482            (keyStoreArea == AES_KEY_AREA_4) ||
483            (keyStoreArea == AES_KEY_AREA_5) ||
484            (keyStoreArea == AES_KEY_AREA_6) ||
485            (keyStoreArea == AES_KEY_AREA_7));
486 
487     // Clear any previously written key at the key location
488     HWREG(CRYPTO_BASE + CRYPTO_O_KEYWRITTENAREA) = (0x00000001 << keyStoreArea);
489 }
490 
491 //*****************************************************************************
492 //
493 //! \brief Select type of operation
494 //!
495 //! \param [in] algorithm Flags that specify which type of operation the crypto
496 //!                       module shall perform. The flags are mutually exclusive.
497 //! - 0 : Reset the module
498 //! - \ref AES_ALGSEL_AES
499 //! - \ref AES_ALGSEL_TAG
500 //! - \ref AES_ALGSEL_KEY_STORE
501 //!
502 //! \return None
503 //
504 //*****************************************************************************
AESSelectAlgorithm(uint32_t algorithm)505 __STATIC_INLINE void AESSelectAlgorithm(uint32_t algorithm)
506 {
507     ASSERT((algorithm == AES_ALGSEL_AES) ||
508            (algorithm == AES_ALGSEL_AES | AES_ALGSEL_TAG) ||
509            (algorithm == AES_ALGSEL_KEY_STORE));
510 
511     HWREG(CRYPTO_BASE + CRYPTO_O_ALGSEL) = algorithm;
512 }
513 
514 //*****************************************************************************
515 //
516 //! \brief Set up the next crypto module operation.
517 //!
518 //! The function uses a bitwise OR of the fields within the CRYPTO_O_AESCTL
519 //! register. The relevant field names have the format:
520 //! - CRYPTO_AESCTL_[field name]
521 //!
522 //! \param [in] ctrlMask Specifies which register fields shall be set.
523 //!
524 //! \return None
525 //
526 //*****************************************************************************
AESSetCtrl(uint32_t ctrlMask)527 __STATIC_INLINE void AESSetCtrl(uint32_t ctrlMask)
528 {
529     HWREG(CRYPTO_BASE + CRYPTO_O_AESCTL) = ctrlMask;
530 }
531 
532 //*****************************************************************************
533 //
534 //! \brief Specify length of the crypto operation.
535 //!
536 //!        Despite specifying it here, the crypto DMA must still be
537 //!        set up with the correct data length.
538 //!
539 //! \param [in] length Data length in bytes. If this
540 //!                    value is set to 0, only authentication of the AAD is
541 //!                    performed in CCM-mode and AESWriteAuthLength() must be set to
542 //!                    >0.
543 //!                    Range depends on the mode:
544 //!                      - ECB:        [16]
545 //!                      - CBC:        [1, sizeof(RAM)]
546 //!                      - CBC-MAC:    [1, sizeof(RAM)]
547 //!                      - CCM:        [0, sizeof(RAM)]
548 //!
549 //! \return None
550 //!
551 //! \sa AESWriteAuthLength
552 //
553 //*****************************************************************************
AESSetDataLength(uint32_t length)554 __STATIC_INLINE void AESSetDataLength(uint32_t length)
555 {
556     HWREG(CRYPTO_BASE + CRYPTO_O_AESDATALEN0) = length;
557     HWREG(CRYPTO_BASE + CRYPTO_O_AESDATALEN1) = 0;
558 }
559 
560 //*****************************************************************************
561 //
562 //! \brief Specify the length of the additional authentication data (AAD).
563 //!
564 //!        Despite specifying it here, the crypto DMA must still be set up with
565 //!        the correct AAD length.
566 //!
567 //! \param [in] length Specifies how long the AAD is in a CCM operation. In CCM mode,
568 //!                    set this to 0 if no AAD is required. If set to 0,
569 //!                    AESWriteDataLength() must be set to >0.
570 //!                    Range depends on the mode:
571 //!                      - ECB:        Do not call.
572 //!                      - CBC:        [0]
573 //!                      - CBC-MAC:    [0]
574 //!                      - CCM:        [0, sizeof(RAM)]
575 //!
576 //! \return None
577 //!
578 //! \sa AESWriteDataLength
579 //
580 //*****************************************************************************
AESSetAuthLength(uint32_t length)581 __STATIC_INLINE void AESSetAuthLength(uint32_t length)
582 {
583     HWREG(CRYPTO_BASE + CRYPTO_O_AESAUTHLEN) = length;
584 }
585 
586 //*****************************************************************************
587 //
588 //! \brief Reset the accelerator and cancel ongoing operations
589 //!
590 //!
591 //! \return None
592 //
593 //*****************************************************************************
AESReset(void)594 __STATIC_INLINE void AESReset(void)
595 {
596     HWREG(CRYPTO_BASE + CRYPTO_O_SWRESET) = 0x00000001;
597 }
598 
599 //*****************************************************************************
600 //
601 //! \brief Enable individual crypto interrupt sources.
602 //!
603 //! This function enables the indicated crypto interrupt sources. Only the
604 //! sources that are enabled can be reflected to the processor interrupt.
605 //! Disabled sources have no effect on the processor.
606 //!
607 //! \param [in] intFlags is the bitwise OR of the interrupt sources to be enabled.
608 //! - \ref AES_DMA_IN_DONE
609 //! - \ref AES_RESULT_RDY
610 //!
611 //! \return None
612 //
613 //*****************************************************************************
AESIntEnable(uint32_t intFlags)614 __STATIC_INLINE void AESIntEnable(uint32_t intFlags)
615 {
616     // Check the arguments.
617     ASSERT((intFlags & AES_DMA_IN_DONE) ||
618            (intFlags & AES_RESULT_RDY));
619 
620     // Using level interrupt.
621     HWREG(CRYPTO_BASE + CRYPTO_O_IRQTYPE) = CRYPTO_IRQTYPE_LEVEL_M;
622 
623     // Enable the specified interrupts.
624     HWREG(CRYPTO_BASE + CRYPTO_O_IRQEN) |= intFlags;
625 }
626 
627 //*****************************************************************************
628 //
629 //! \brief Disable individual crypto interrupt sources.
630 //!
631 //! This function disables the indicated crypto interrupt sources. Only the
632 //! sources that are enabled can be reflected to the processor interrupt.
633 //! Disabled sources have no effect on the processor.
634 //!
635 //! \param [in] intFlags is the bitwise OR of the interrupt sources to be enabled.
636 //! - \ref AES_DMA_IN_DONE
637 //! - \ref AES_RESULT_RDY
638 //!
639 //! \return None
640 //
641 //*****************************************************************************
AESIntDisable(uint32_t intFlags)642 __STATIC_INLINE void AESIntDisable(uint32_t intFlags)
643 {
644     // Check the arguments.
645     ASSERT((intFlags & AES_DMA_IN_DONE) ||
646            (intFlags & AES_RESULT_RDY));
647 
648     // Disable the specified interrupts.
649     HWREG(CRYPTO_BASE + CRYPTO_O_IRQEN) &= ~intFlags;
650 }
651 
652 //*****************************************************************************
653 //
654 //! \brief Get the current masked interrupt status.
655 //!
656 //! This function returns the masked interrupt status of the crypto module.
657 //!
658 //! \return Returns the status of the masked lines when enabled:
659 //! - \ref AES_DMA_IN_DONE
660 //! - \ref AES_RESULT_RDY
661 //
662 //*****************************************************************************
AESIntStatusMasked(void)663 __STATIC_INLINE uint32_t AESIntStatusMasked(void)
664 {
665     uint32_t mask;
666 
667     // Return the masked interrupt status
668     mask = HWREG(CRYPTO_BASE + CRYPTO_O_IRQEN);
669     return(mask & HWREG(CRYPTO_BASE + CRYPTO_O_IRQSTAT));
670 }
671 
672 //*****************************************************************************
673 //
674 //! \brief Get the current raw interrupt status.
675 //!
676 //! This function returns the raw interrupt status of the crypto module.
677 //! It returns both the status of the lines routed to the NVIC as well as the
678 //! error flags.
679 //!
680 //! \return Returns the raw interrupt status:
681 //! - \ref AES_DMA_IN_DONE
682 //! - \ref AES_RESULT_RDY
683 //! - \ref AES_DMA_BUS_ERR
684 //! - \ref AES_KEY_ST_WR_ERR
685 //! - \ref AES_KEY_ST_RD_ERR
686 //
687 //*****************************************************************************
AESIntStatusRaw(void)688 __STATIC_INLINE uint32_t AESIntStatusRaw(void)
689 {
690     // Return either the raw interrupt status
691     return(HWREG(CRYPTO_BASE + CRYPTO_O_IRQSTAT));
692 }
693 
694 //*****************************************************************************
695 //
696 //! \brief Clear crypto interrupt sources.
697 //!
698 //! The specified crypto interrupt sources are cleared, so that they no longer
699 //! assert. This function must be called in the interrupt handler to keep the
700 //! interrupt from being recognized again immediately upon exit.
701 //!
702 //! \note Due to write buffers and synchronizers in the system it may take several
703 //! clock cycles from a register write clearing an event in the module until the
704 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
705 //! clear the event source early in the interrupt service routine (ISR) to allow
706 //! the event clear to propagate to the NVIC before returning from the ISR.
707 //!
708 //! \param [in] intFlags is a bit mask of the interrupt sources to be cleared.
709 //! - \ref AES_DMA_IN_DONE
710 //! - \ref AES_RESULT_RDY
711 //!
712 //! \return None
713 //
714 //*****************************************************************************
AESIntClear(uint32_t intFlags)715 __STATIC_INLINE void AESIntClear(uint32_t intFlags)
716 {
717     // Check the arguments.
718     ASSERT((intFlags & AES_DMA_IN_DONE) ||
719            (intFlags & AES_RESULT_RDY));
720 
721     // Clear the requested interrupt sources,
722     HWREG(CRYPTO_BASE + CRYPTO_O_IRQCLR) = intFlags;
723 }
724 
725 //*****************************************************************************
726 //
727 //! \brief Register an interrupt handler for a crypto interrupt.
728 //!
729 //! This function does the actual registering of the interrupt handler.  This
730 //! function enables the global interrupt in the interrupt controller; specific
731 //! crypto interrupts must be enabled via \ref AESIntEnable(). It is the interrupt
732 //! handler's responsibility to clear the interrupt source.
733 //!
734 //! \param handlerFxn is a pointer to the function to be called when the
735 //! crypto interrupt occurs.
736 //!
737 //! \return None
738 //!
739 //! \sa \ref IntRegister() for important information about registering interrupt
740 //! handlers.
741 //
742 //*****************************************************************************
AESIntRegister(void (* handlerFxn)(void))743 __STATIC_INLINE void AESIntRegister(void (*handlerFxn)(void))
744 {
745     // Register the interrupt handler.
746     IntRegister(INT_CRYPTO_RESULT_AVAIL_IRQ, handlerFxn);
747 
748     // Enable the crypto interrupt.
749     IntEnable(INT_CRYPTO_RESULT_AVAIL_IRQ);
750 }
751 
752 //*****************************************************************************
753 //
754 //! \brief Unregister an interrupt handler for a crypto interrupt.
755 //!
756 //! This function does the actual unregistering of the interrupt handler. It
757 //! clears the handler called when a crypto interrupt occurs. This
758 //! function also masks off the interrupt in the interrupt controller so that
759 //! the interrupt handler no longer is called.
760 //!
761 //! \return None
762 //!
763 //! \sa \ref IntRegister() for important information about registering interrupt
764 //! handlers.
765 //
766 //*****************************************************************************
AESIntUnregister(void)767 __STATIC_INLINE void AESIntUnregister(void)
768 {
769     //
770     // Disable the interrupt.
771     //
772     IntDisable(INT_CRYPTO_RESULT_AVAIL_IRQ);
773 
774     //
775     // Unregister the interrupt handler.
776     //
777     IntUnregister(INT_CRYPTO_RESULT_AVAIL_IRQ);
778 }
779 
780 //*****************************************************************************
781 //
782 // Support for DriverLib in ROM:
783 // Redirect to implementation in ROM when available.
784 //
785 //*****************************************************************************
786 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
787     #include "../driverlib/rom.h"
788     #ifdef ROM_AESStartDMAOperation
789         #undef  AESStartDMAOperation
790         #define AESStartDMAOperation            ROM_AESStartDMAOperation
791     #endif
792     #ifdef ROM_AESSetInitializationVector
793         #undef  AESSetInitializationVector
794         #define AESSetInitializationVector      ROM_AESSetInitializationVector
795     #endif
796     #ifdef ROM_AESWriteCCMInitializationVector
797         #undef  AESWriteCCMInitializationVector
798         #define AESWriteCCMInitializationVector ROM_AESWriteCCMInitializationVector
799     #endif
800     #ifdef ROM_AESReadTag
801         #undef  AESReadTag
802         #define AESReadTag                      ROM_AESReadTag
803     #endif
804     #ifdef ROM_AESVerifyTag
805         #undef  AESVerifyTag
806         #define AESVerifyTag                    ROM_AESVerifyTag
807     #endif
808     #ifdef ROM_AESWriteToKeyStore
809         #undef  AESWriteToKeyStore
810         #define AESWriteToKeyStore              ROM_AESWriteToKeyStore
811     #endif
812     #ifdef ROM_AESReadFromKeyStore
813         #undef  AESReadFromKeyStore
814         #define AESReadFromKeyStore             ROM_AESReadFromKeyStore
815     #endif
816     #ifdef ROM_AESWaitForIRQFlags
817         #undef  AESWaitForIRQFlags
818         #define AESWaitForIRQFlags              ROM_AESWaitForIRQFlags
819     #endif
820     #ifdef ROM_AESConfigureCCMCtrl
821         #undef  AESConfigureCCMCtrl
822         #define AESConfigureCCMCtrl             ROM_AESConfigureCCMCtrl
823     #endif
824 #endif
825 
826 //*****************************************************************************
827 //
828 // Mark the end of the C bindings section for C++ compilers.
829 //
830 //*****************************************************************************
831 #ifdef __cplusplus
832 }
833 #endif
834 
835 #endif  // __AES_H__
836 
837 //*****************************************************************************
838 //
839 //! Close the Doxygen group.
840 //! @}
841 //! @}
842 //
843 //*****************************************************************************
844