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