1 /******************************************************************************
2 *  Filename:       crypto.h
3 *  Revised:        2015-11-16 19:41:47 +0100 (Mon, 16 Nov 2015)
4 *  Revision:       45094
5 *
6 *  Description:    AES header file.
7 *
8 *  Copyright (c) 2015, 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 crypto_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #ifndef __CRYPTO_H__
49 #define __CRYPTO_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 <inc/hw_types.h>
65 #include <inc/hw_memmap.h>
66 #include <inc/hw_ints.h>
67 #include <inc/hw_crypto.h>
68 #include <driverlib/debug.h>
69 #include <driverlib/interrupt.h>
70 #include <driverlib/cpu.h>
71 
72 //*****************************************************************************
73 //
74 // Support for DriverLib in ROM:
75 // This section renames all functions that are not "static inline", so that
76 // calling these functions will default to implementation in flash. At the end
77 // of this file a second renaming will change the defaults to implementation in
78 // ROM for available functions.
79 //
80 // To force use of the implementation in flash, e.g. for debugging:
81 // - Globally: Define DRIVERLIB_NOROM at project level
82 // - Per function: Use prefix "NOROM_" when calling the function
83 //
84 //*****************************************************************************
85 #if !defined(DOXYGEN)
86     #define CRYPTOAesLoadKey                NOROM_CRYPTOAesLoadKey
87     #define CRYPTOAesEcb                    NOROM_CRYPTOAesEcb
88     #define CRYPTOAesEcbStatus              NOROM_CRYPTOAesEcbStatus
89     #define CRYPTOCcmAuthEncrypt            NOROM_CRYPTOCcmAuthEncrypt
90     #define CRYPTOCcmAuthEncryptStatus      NOROM_CRYPTOCcmAuthEncryptStatus
91     #define CRYPTOCcmAuthEncryptResultGet   NOROM_CRYPTOCcmAuthEncryptResultGet
92     #define CRYPTOCcmInvAuthDecrypt         NOROM_CRYPTOCcmInvAuthDecrypt
93     #define CRYPTOCcmInvAuthDecryptStatus   NOROM_CRYPTOCcmInvAuthDecryptStatus
94     #define CRYPTOCcmInvAuthDecryptResultGet NOROM_CRYPTOCcmInvAuthDecryptResultGet
95     #define CRYPTODmaEnable                 NOROM_CRYPTODmaEnable
96     #define CRYPTODmaDisable                NOROM_CRYPTODmaDisable
97 #endif
98 
99 //*****************************************************************************
100 //
101 // Length of AES Electronic Code Book (ECB) block in bytes
102 //
103 //*****************************************************************************
104 #define AES_ECB_LENGTH  16
105 
106 //*****************************************************************************
107 //
108 // Values that can be passed to CryptoIntEnable, CryptoIntDisable, and CryptoIntClear
109 // as the ui32IntFlags parameter, and returned from CryptoIntStatus.
110 //
111 //*****************************************************************************
112 #define CRYPTO_DMA_IN_DONE      0x00000002  // DMA done interrupt mask
113 #define CRYPTO_RESULT_RDY       0x00000001  // Result ready interrupt mask
114 #define CRYPTO_DMA_BUS_ERR      0x80000000  // DMA Bus error
115 #define CRYPTO_KEY_ST_WR_ERR    0x40000000  // Key Store Write failed
116 #define CRYPTO_KEY_ST_RD_ERR    0x20000000  // Key Store Read failed
117 
118 #define CRYPTO_INT_LEVEL        0x00000001  // Crypto Level interrupt enabled
119 #define CRYPTO_INT_PULSE        0x00000000  // Crypto pulse interrupt enabled
120 
121 #define CRYPTO_DMA_CHAN0        0x00000001  // Crypto DMA Channel 0
122 #define CRYPTO_DMA_CHAN1        0x00000002  // Crypto DMA Channel 1
123 
124 #define CRYPTO_AES128_ENCRYPT   0x0000000C  //
125 #define CRYPTO_AES128_DECRYPT   0x00000008  //
126 
127 #define CRYPTO_DMA_READY        0x00000000  // DMA ready
128 #define CRYPTO_DMA_BSY          0x00000003  // DMA busy
129 #define CRYPTO_DMA_BUS_ERROR    0x00020000  // DMA encountered bus error
130 
131 //*****************************************************************************
132 //
133 //  General constants
134 //
135 //*****************************************************************************
136 
137 // AES module return codes
138 #define AES_SUCCESS                      0
139 #define AES_KEYSTORE_READ_ERROR          1
140 #define AES_KEYSTORE_WRITE_ERROR         2
141 #define AES_DMA_BUS_ERROR                3
142 #define CCM_AUTHENTICATION_FAILED        4
143 #define AES_ECB_TEST_ERROR               8
144 #define AES_NULL_ERROR                   9
145 #define AES_CCM_TEST_ERROR              10
146 #define AES_DMA_BSY                     11
147 
148 // Key store module defines
149 #define STATE_BLENGTH                   16  // Number of bytes in State
150 #define KEY_BLENGTH                     16  // Number of bytes in Key
151 #define KEY_EXP_LENGTH                 176  // Nb * (Nr+1) * 4
152 
153 #define KEY_STORE_SIZE_128      0x00000001
154 #define KEY_STORE_SIZE_192      0x00000002
155 #define KEY_STORE_SIZE_256      0x00000003
156 #define KEY_STORE_SIZE_BITS     0x00000003
157 
158 //*****************************************************************************
159 //
160 // For 128 bit key all 8 Key Area locations from 0 to 8 are valid
161 // However for 192 bit and 256 bit keys, only even Key Areas 0, 2, 4, 6
162 // are valid. This is passes as a parameter to AesECBStart()
163 //
164 //*****************************************************************************
165 #define CRYPTO_KEY_AREA_0       0
166 #define CRYPTO_KEY_AREA_1       1
167 #define CRYPTO_KEY_AREA_2       2
168 #define CRYPTO_KEY_AREA_3       3
169 #define CRYPTO_KEY_AREA_4       4
170 #define CRYPTO_KEY_AREA_5       5
171 #define CRYPTO_KEY_AREA_6       6
172 #define CRYPTO_KEY_AREA_7       7
173 
174 //*****************************************************************************
175 //
176 // Defines for the current AES operation
177 //
178 //*****************************************************************************
179 #define CRYPTO_AES_NONE         0
180 #define CRYPTO_AES_KEYL0AD      1
181 #define CRYPTO_AES_ECB          2
182 #define CRYPTO_AES_CCM          3
183 #define CRYPTO_AES_RNG          4
184 
185 //*****************************************************************************
186 //
187 // Defines for the AES-CTR mode counter width
188 //
189 //*****************************************************************************
190 #define CRYPTO_AES_CTR_32       0x0
191 #define CRYPTO_AES_CTR_64       0x1
192 #define CRYPTO_AES_CTR_96       0x2
193 #define CRYPTO_AES_CTR_128      0x3
194 
195 //*****************************************************************************
196 //
197 // API Functions and prototypes
198 //
199 //*****************************************************************************
200 
201 //*****************************************************************************
202 //
203 //! \brief Write the key into the Key Ram.
204 //!
205 //! The \c ui32KeyLocation parameter is an enumerated type which specifies
206 //! the Key Ram location in which the key is stored.
207 //!
208 //! The pointer \c pui8AesKey has the address where the Key is stored.
209 //!
210 //! \param pui32AesKey is a pointer to an AES Key.
211 //! \param ui32KeyLocation is the location of the key in Key RAM.
212 //! This parameter can have any of the following values:
213 //! - \ref CRYPTO_KEY_AREA_0
214 //! - \ref CRYPTO_KEY_AREA_1
215 //! - \ref CRYPTO_KEY_AREA_2
216 //! - \ref CRYPTO_KEY_AREA_3
217 //! - \ref CRYPTO_KEY_AREA_4
218 //! - \ref CRYPTO_KEY_AREA_5
219 //! - \ref CRYPTO_KEY_AREA_6
220 //! - \ref CRYPTO_KEY_AREA_7
221 //!
222 //! \return Returns status of the function:
223 //! - \ref AES_SUCCESS
224 //! - \ref AES_KEYSTORE_READ_ERROR
225 //
226 //*****************************************************************************
227 extern uint32_t CRYPTOAesLoadKey(uint32_t *pui32AesKey,
228                                  uint32_t ui32KeyLocation);
229 
230 //*****************************************************************************
231 //
232 //! \brief Start an AES-ECB operation (encryption or decryption).
233 //!
234 //! The \c ui32KeyLocation parameter is an enumerated type which specifies
235 //! the Key Ram location in which the key is stored.
236 //!
237 //! \param pui32MsgIn is a pointer to the input data.
238 //! \param pui32MsgOut is a pointer to the output data.
239 //! \param ui32KeyLocation is the location of the key in Key RAM.
240 //! This parameter can have any of the following values:
241 //! - \ref CRYPTO_KEY_AREA_0
242 //! - \ref CRYPTO_KEY_AREA_1
243 //! - \ref CRYPTO_KEY_AREA_2
244 //! - \ref CRYPTO_KEY_AREA_3
245 //! - \ref CRYPTO_KEY_AREA_4
246 //! - \ref CRYPTO_KEY_AREA_5
247 //! - \ref CRYPTO_KEY_AREA_6
248 //! - \ref CRYPTO_KEY_AREA_7
249 //! \param bEncrypt is set \c true to encrypt or set \c false to decrypt.
250 //! \param bIntEnable is set \c true to enable Crypto interrupts or \c false to
251 //! disable Crypto interrupt.
252 //!
253 //! \return Returns status of the AES-ECB operation:
254 //! - \ref AES_SUCCESS
255 //! - \ref AES_KEYSTORE_READ_ERROR
256 //!
257 //! \sa \ref CRYPTOAesEcbStatus()
258 //
259 //*****************************************************************************
260 extern uint32_t CRYPTOAesEcb(uint32_t *pui32MsgIn, uint32_t *pui32MsgOut,
261                              uint32_t ui32KeyLocation, bool bEncrypt,
262                              bool bIntEnable);
263 
264 //*****************************************************************************
265 //
266 //! \brief Check the result of an AES ECB operation.
267 //!
268 //! This function should be called after \ref CRYPTOAesEcb() function to
269 //! check if the AES ECB operation was successful.
270 //!
271 //! \return Returns the status of the AES ECB operation:
272 //! - \ref AES_SUCCESS       : Successful.
273 //! - \ref AES_DMA_BUS_ERROR : Failed.
274 //! - \ref AES_DMA_BSY       : Operation is ongoing.
275 //!
276 //! \sa \ref CRYPTOAesEcb()
277 //
278 //*****************************************************************************
279 extern uint32_t CRYPTOAesEcbStatus(void);
280 
281 //*****************************************************************************
282 //
283 //! \brief Finish the encryption operation by resetting the operation mode.
284 //!
285 //! This function should be called after \ref CRYPTOAesEcbStatus() has reported
286 //! that the operation is finished successfully.
287 //!
288 //! \return None
289 //!
290 //! \sa \ref CRYPTOAesEcbStatus()
291 //
292 //*****************************************************************************
293 __STATIC_INLINE void
CRYPTOAesEcbFinish(void)294 CRYPTOAesEcbFinish(void)
295 {
296     //
297     // Result has already been copied to the output buffer by DMA.
298     // Disable master control/DMA clock and clear the operating mode.
299     //
300     HWREG(CRYPTO_BASE + CRYPTO_O_ALGSEL) = 0x00000000;
301     HWREG(CRYPTO_BASE + CRYPTO_O_AESCTL) = 0x00000000;
302 }
303 
304 //*****************************************************************************
305 //
306 //! \brief Start CCM operation.
307 //!
308 //! The \c ui32KeyLocation is an enumerated type which specifies the Key Ram
309 //! location in which the key is stored.
310 //!
311 //! \param bEncrypt determines whether to run encryption or not.
312 //! \param ui32AuthLength is the the length of the authentication field -
313 //! 0, 2, 4, 6, 8, 10, 12, 14 or 16 octets.
314 //! \param pui32Nonce is a pointer to 13-byte or 12-byte Nonce (Number used once).
315 //! \param pui32PlainText is a pointer to the octet string input message.
316 //! \param ui32PlainTextLength is the length of the message.
317 //! \param pui32Header is the length of the header (Additional Authentication
318 //! Data or AAD).
319 //! \param ui32HeaderLength is the length of the header in octets.
320 //! \param ui32KeyLocation is the location in Key RAM where the key is stored.
321 //! This parameter can have any of the following values:
322 //! - \ref CRYPTO_KEY_AREA_0
323 //! - \ref CRYPTO_KEY_AREA_1
324 //! - \ref CRYPTO_KEY_AREA_2
325 //! - \ref CRYPTO_KEY_AREA_3
326 //! - \ref CRYPTO_KEY_AREA_4
327 //! - \ref CRYPTO_KEY_AREA_5
328 //! - \ref CRYPTO_KEY_AREA_6
329 //! - \ref CRYPTO_KEY_AREA_7
330 //! \param ui32FieldLength is the size of the length field (2 or 3).
331 //! \param bIntEnable enables interrupts.
332 //!
333 //! \return Returns the status of the CCM operation
334 //! - \ref AES_SUCCESS
335 //! - \ref AES_KEYSTORE_READ_ERROR
336 //! - \ref AES_DMA_BUS_ERROR
337 //!
338 //! \sa \ref CRYPTOCcmAuthEncryptStatus()
339 //
340 //*****************************************************************************
341 extern uint32_t CRYPTOCcmAuthEncrypt(bool bEncrypt, uint32_t ui32AuthLength,
342                                      uint32_t *pui32Nonce,
343                                      uint32_t *pui32PlainText,
344                                      uint32_t ui32PlainTextLength,
345                                      uint32_t *pui32Header,
346                                      uint32_t ui32HeaderLength,
347                                      uint32_t ui32KeyLocation,
348                                      uint32_t ui32FieldLength,
349                                      bool bIntEnable);
350 
351 //*****************************************************************************
352 //
353 //! \brief Check the result of an AES CCM operation.
354 //!
355 //! This function should be called after \ref CRYPTOCcmAuthEncrypt() function to check
356 //! if the AES CCM operation was successful.
357 //!
358 //! \return Returns the status of the AES CCM operation:
359 //! - \ref AES_SUCCESS       : Successful.
360 //! - \ref AES_DMA_BUS_ERROR : Failed.
361 //! - \ref AES_DMA_BSY       : Operation is ongoing.
362 //!
363 //! \sa \ref CRYPTOCcmAuthEncrypt()
364 //
365 //*****************************************************************************
366 extern uint32_t CRYPTOCcmAuthEncryptStatus(void);
367 
368 //*****************************************************************************
369 //
370 //! \brief Get the result of an AES CCM operation.
371 //!
372 //! This function should be called after \ref CRYPTOCcmAuthEncryptStatus().
373 //!
374 //! \param ui32TagLength is length of the Tag.
375 //! \param pui32CcmTag is the location of the authentication Tag.
376 //!
377 //! \return Returns \ref AES_SUCCESS if successful.
378 //!
379 //! \sa \ref CRYPTOCcmAuthEncryptStatus()
380 //
381 //*****************************************************************************
382 extern uint32_t CRYPTOCcmAuthEncryptResultGet(uint32_t ui32TagLength,
383                                               uint32_t *pui32CcmTag);
384 
385 //*****************************************************************************
386 //
387 //! \brief Start a CCM Decryption and Inverse Authentication operation.
388 //!
389 //! The \c ui32KeyLocation is an enumerated type which specifies the Key Ram
390 //! location in which the key is stored.
391 //!
392 //! \param bDecrypt determines whether to run decryption or not.
393 //! \param ui32AuthLength is the the length of the authentication field -
394 //! 0, 2, 4, 6, 8, 10, 12, 14 or 16 octets.
395 //! \param pui32Nonce is a pointer to 13-byte or 12-byte Nonce (Number used once).
396 //! \param pui32CipherText is a pointer to the octet string encrypted message.
397 //! \param ui32CipherTextLength is the length of the encrypted message.
398 //! \param pui32Header is the length of the header (Additional Authentication
399 //! Data or AAD).
400 //! \param ui32HeaderLength is the length of the header in octets.
401 //! \param ui32KeyLocation is the location in Key RAM where the key is stored.
402 //! This parameter can have any of the following values:
403 //! - \ref CRYPTO_KEY_AREA_0
404 //! - \ref CRYPTO_KEY_AREA_1
405 //! - \ref CRYPTO_KEY_AREA_2
406 //! - \ref CRYPTO_KEY_AREA_3
407 //! - \ref CRYPTO_KEY_AREA_4
408 //! - \ref CRYPTO_KEY_AREA_5
409 //! - \ref CRYPTO_KEY_AREA_6
410 //! - \ref CRYPTO_KEY_AREA_7
411 //! \param ui32FieldLength is the size of the length field (2 or 3).
412 //! \param bIntEnable enables interrupts.
413 //!
414 //! \return Returns the status of the operation:
415 //! - \ref AES_SUCCESS
416 //! - \ref AES_KEYSTORE_READ_ERROR
417 //! - \ref AES_DMA_BUS_ERROR
418 //
419 //*****************************************************************************
420 extern uint32_t CRYPTOCcmInvAuthDecrypt(bool bDecrypt, uint32_t ui32AuthLength,
421                                         uint32_t *pui32Nonce,
422                                         uint32_t *pui32CipherText,
423                                         uint32_t ui32CipherTextLength,
424                                         uint32_t *pui32Header,
425                                         uint32_t ui32HeaderLength,
426                                         uint32_t ui32KeyLocation,
427                                         uint32_t ui32FieldLength,
428                                         bool bIntEnable);
429 
430 //*****************************************************************************
431 //
432 //! \brief Checks CCM decrypt and Inverse Authentication result.
433 //!
434 //! \return Returns status of operation:
435 //! - \ref AES_SUCCESS       : Operation was successful.
436 //! - \ref AES_DMA_BSY       : Operation is busy.
437 //! - \ref AES_DMA_BUS_ERROR : An error is encountered.
438 //
439 //*****************************************************************************
440 extern uint32_t CRYPTOCcmInvAuthDecryptStatus(void);
441 
442 //*****************************************************************************
443 //
444 //! \brief Get the result of the CCM operation.
445 //!
446 //! \param ui32AuthLength is the the length of the authentication field -
447 //! 0, 2, 4, 6, 8, 10, 12, 14 or 16 octets.
448 //! \param pui32CipherText is a pointer to the octet string encrypted message.
449 //! \param ui32CipherTextLength is the length of the encrypted message.
450 //! \param pui32CcmTag is the location of the authentication Tag.
451 //!
452 //! \return Returns AES_SUCCESS if successful.
453 //
454 //*****************************************************************************
455 extern uint32_t CRYPTOCcmInvAuthDecryptResultGet(uint32_t ui32AuthLength,
456                                                  uint32_t *pui32CipherText,
457                                                  uint32_t ui32CipherTextLength,
458                                                  uint32_t *pui32CcmTag);
459 
460 //*****************************************************************************
461 //
462 //! \brief Get the current status of the Crypto DMA controller.
463 //!
464 //! This function is used to poll the Crypto DMA controller to check if it is
465 //! ready for a new operation or if an error has occurred.
466 //!
467 //! The \ref CRYPTO_DMA_BUS_ERROR can also be caught using the crypto event
468 //! handler.
469 //!
470 //! \return Returns the current status of the DMA controller:
471 //! - \ref CRYPTO_DMA_READY     : DMA ready for a new operation
472 //! - \ref CRYPTO_DMA_BSY       : DMA is busy
473 //! - \ref CRYPTO_DMA_BUS_ERROR : DMA Bus error
474 //
475 //*****************************************************************************
476 __STATIC_INLINE uint32_t
CRYPTODmaStatus(void)477 CRYPTODmaStatus(void)
478 {
479     //
480     // Return the value of the status register.
481     //
482     return (HWREG(CRYPTO_BASE + CRYPTO_O_DMASTAT));
483 }
484 
485 //*****************************************************************************
486 //
487 //! \brief Enable Crypto DMA operation.
488 //!
489 //! The specified Crypto DMA channels are enabled.
490 //!
491 //! \param ui32Channels is a bitwise OR of the channels to enable.
492 //! - \ref CRYPTO_DMA_CHAN0
493 //! - \ref CRYPTO_DMA_CHAN1
494 //!
495 //! \return None
496 //
497 //*****************************************************************************
498 extern void CRYPTODmaEnable(uint32_t ui32Channels);
499 
500 //*****************************************************************************
501 //
502 //! \brief Disable Crypto DMA operation.
503 //!
504 //! The specified Crypto DMA channels are disabled.
505 //!
506 //! \param ui32Channels is a bitwise OR of the channels to disable.
507 //! - \ref CRYPTO_DMA_CHAN0
508 //! - \ref CRYPTO_DMA_CHAN1
509 //!
510 //! \return None
511 //
512 //*****************************************************************************
513 extern void CRYPTODmaDisable(uint32_t ui32Channels);
514 
515 //*****************************************************************************
516 //
517 //! \brief Enables individual Crypto interrupt sources.
518 //!
519 //! This function enables the indicated Crypto interrupt sources. Only the
520 //! sources that are enabled can be reflected to the processor interrupt.
521 //! Disabled sources have no effect on the processor.
522 //!
523 //! \param ui32IntFlags is the bitwise OR of the interrupt sources to be enabled.
524 //! - \ref CRYPTO_DMA_IN_DONE
525 //! - \ref CRYPTO_RESULT_RDY
526 //!
527 //! \return None
528 //
529 //*****************************************************************************
530 __STATIC_INLINE void
CRYPTOIntEnable(uint32_t ui32IntFlags)531 CRYPTOIntEnable(uint32_t ui32IntFlags)
532 {
533     //
534     // Check the arguments.
535     //
536     ASSERT((ui32IntFlags & CRYPTO_DMA_IN_DONE) |
537            (ui32IntFlags & CRYPTO_RESULT_RDY));
538 
539     //
540     // Using level interrupt.
541     //
542     HWREG(CRYPTO_BASE + CRYPTO_O_IRQTYPE) = CRYPTO_INT_LEVEL;
543 
544     //
545     // Enable the specified interrupts.
546     //
547     HWREG(CRYPTO_BASE + CRYPTO_O_IRQEN) |= ui32IntFlags;
548 }
549 
550 //*****************************************************************************
551 //
552 //! \brief Disables individual CRYPTO interrupt sources.
553 //!
554 //! This function disables the indicated Crypto interrupt sources. Only the
555 //! sources that are enabled can be reflected to the processor interrupt.
556 //! Disabled sources have no effect on the processor.
557 //!
558 //! \param ui32IntFlags is the bitwise OR of the interrupt sources to be enabled.
559 //! - \ref CRYPTO_DMA_IN_DONE
560 //! - \ref CRYPTO_RESULT_RDY
561 //!
562 //! \return None
563 //
564 //*****************************************************************************
565 __STATIC_INLINE void
CRYPTOIntDisable(uint32_t ui32IntFlags)566 CRYPTOIntDisable(uint32_t ui32IntFlags)
567 {
568     //
569     // Check the arguments.
570     //
571     ASSERT((ui32IntFlags & CRYPTO_DMA_IN_DONE) |
572            (ui32IntFlags & CRYPTO_RESULT_RDY));
573 
574     //
575     // Disable the specified interrupts.
576     //
577     HWREG(CRYPTO_BASE + CRYPTO_O_IRQEN) &= ~ui32IntFlags;
578 }
579 
580 //*****************************************************************************
581 //
582 //! \brief Gets the current interrupt status.
583 //!
584 //! This function returns the interrupt status for the specified Crypto. Either
585 //! the raw interrupt status or the status of interrupts that are allowed to
586 //! reflect to the processor can be returned.
587 //!
588 //! \param bMasked whether to use raw or masked interrupt status:
589 //! - \c false : Raw interrupt status is required.
590 //! - \c true  : Masked interrupt status is required.
591 //!
592 //! \return Returns the current interrupt status:
593 //! - \ref CRYPTO_DMA_IN_DONE
594 //! - \ref CRYPTO_RESULT_RDY
595 //
596 //*****************************************************************************
597 __STATIC_INLINE uint32_t
CRYPTOIntStatus(bool bMasked)598 CRYPTOIntStatus(bool bMasked)
599 {
600     uint32_t ui32Mask;
601 
602     //
603     // Return either the interrupt status or the raw interrupt status as
604     // requested.
605     //
606     if(bMasked)
607     {
608         ui32Mask = HWREG(CRYPTO_BASE + CRYPTO_O_IRQEN);
609         return(ui32Mask & HWREG(CRYPTO_BASE + CRYPTO_O_IRQSTAT));
610     }
611     else
612     {
613         return(HWREG(CRYPTO_BASE + CRYPTO_O_IRQSTAT) & 0x00000003);
614     }
615 }
616 
617 //*****************************************************************************
618 //
619 //! \brief Clears Crypto interrupt sources.
620 //!
621 //! The specified Crypto interrupt sources are cleared, so that they no longer
622 //! assert. This function must be called in the interrupt handler to keep the
623 //! interrupt from being recognized again immediately upon exit.
624 //!
625 //! \note Due to write buffers and synchronizers in the system it may take several
626 //! clock cycles from a register write clearing an event in a module and until the
627 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
628 //! clear the event source early in the interrupt service routine (ISR) to allow
629 //! the event clear to propagate to the NVIC before returning from the ISR.
630 //! At the same time, an early event clear allows new events of the same type to be
631 //! pended instead of ignored if the event is cleared later in the ISR.
632 //! It is the responsibility of the programmer to make sure that enough time has passed
633 //! before returning from the ISR to avoid false re-triggering of the cleared event.
634 //! A simple, although not necessarily optimal, way of clearing an event before
635 //! returning from the ISR is:
636 //! -# Write to clear event (interrupt source). (buffered write)
637 //! -# Dummy read from the event source module. (making sure the write has propagated)
638 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any synchronizers)
639 //!
640 //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
641 //! - \ref CRYPTO_DMA_IN_DONE
642 //! - \ref CRYPTO_RESULT_RDY
643 //!
644 //! \return None
645 //
646 //*****************************************************************************
647 __STATIC_INLINE void
CRYPTOIntClear(uint32_t ui32IntFlags)648 CRYPTOIntClear(uint32_t ui32IntFlags)
649 {
650     //
651     // Check the arguments.
652     //
653     ASSERT((ui32IntFlags & CRYPTO_DMA_IN_DONE) |
654            (ui32IntFlags & CRYPTO_RESULT_RDY));
655 
656     //
657     // Clear the requested interrupt sources,
658     //
659     HWREG(CRYPTO_BASE + CRYPTO_O_IRQCLR) = ui32IntFlags;
660 }
661 
662 //*****************************************************************************
663 //
664 //! \brief Registers an interrupt handler for a Crypto interrupt.
665 //!
666 //! This function does the actual registering of the interrupt handler.  This
667 //! function enables the global interrupt in the interrupt controller; specific
668 //! UART interrupts must be enabled via \ref CRYPTOIntEnable(). It is the interrupt
669 //! handler's responsibility to clear the interrupt source.
670 //!
671 //! \param pfnHandler is a pointer to the function to be called when the
672 //! UART interrupt occurs.
673 //!
674 //! \return None
675 //!
676 //! \sa \ref IntRegister() for important information about registering interrupt
677 //! handlers.
678 //
679 //*****************************************************************************
680 __STATIC_INLINE void
CRYPTOIntRegister(void (* pfnHandler)(void))681 CRYPTOIntRegister(void (*pfnHandler)(void))
682 {
683     //
684     // Register the interrupt handler.
685     //
686     IntRegister(INT_CRYPTO_RESULT_AVAIL_IRQ, pfnHandler);
687 
688     //
689     // Enable the UART interrupt.
690     //
691     IntEnable(INT_CRYPTO_RESULT_AVAIL_IRQ);
692 }
693 
694 //*****************************************************************************
695 //
696 //! \brief Unregisters an interrupt handler for a Crypto interrupt.
697 //!
698 //! This function does the actual unregistering of the interrupt handler. It
699 //! clears the handler to be called when a Crypto interrupt occurs. This
700 //! function also masks off the interrupt in the interrupt controller so that
701 //! the interrupt handler no longer is called.
702 //!
703 //! \return None
704 //!
705 //! \sa \ref IntRegister() for important information about registering interrupt
706 //! handlers.
707 //
708 //*****************************************************************************
709 __STATIC_INLINE void
CRYPTOIntUnregister(void)710 CRYPTOIntUnregister(void)
711 {
712     //
713     // Disable the interrupt.
714     //
715     IntDisable(INT_CRYPTO_RESULT_AVAIL_IRQ);
716 
717     //
718     // Unregister the interrupt handler.
719     //
720     IntUnregister(INT_CRYPTO_RESULT_AVAIL_IRQ);
721 }
722 
723 //*****************************************************************************
724 //
725 // Support for DriverLib in ROM:
726 // Redirect to implementation in ROM when available.
727 //
728 //*****************************************************************************
729 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
730     #include <driverlib/rom.h>
731     #ifdef ROM_CRYPTOAesLoadKey
732         #undef  CRYPTOAesLoadKey
733         #define CRYPTOAesLoadKey                ROM_CRYPTOAesLoadKey
734     #endif
735     #ifdef ROM_CRYPTOAesEcb
736         #undef  CRYPTOAesEcb
737         #define CRYPTOAesEcb                    ROM_CRYPTOAesEcb
738     #endif
739     #ifdef ROM_CRYPTOAesEcbStatus
740         #undef  CRYPTOAesEcbStatus
741         #define CRYPTOAesEcbStatus              ROM_CRYPTOAesEcbStatus
742     #endif
743     #ifdef ROM_CRYPTOCcmAuthEncrypt
744         #undef  CRYPTOCcmAuthEncrypt
745         #define CRYPTOCcmAuthEncrypt            ROM_CRYPTOCcmAuthEncrypt
746     #endif
747     #ifdef ROM_CRYPTOCcmAuthEncryptStatus
748         #undef  CRYPTOCcmAuthEncryptStatus
749         #define CRYPTOCcmAuthEncryptStatus      ROM_CRYPTOCcmAuthEncryptStatus
750     #endif
751     #ifdef ROM_CRYPTOCcmAuthEncryptResultGet
752         #undef  CRYPTOCcmAuthEncryptResultGet
753         #define CRYPTOCcmAuthEncryptResultGet   ROM_CRYPTOCcmAuthEncryptResultGet
754     #endif
755     #ifdef ROM_CRYPTOCcmInvAuthDecrypt
756         #undef  CRYPTOCcmInvAuthDecrypt
757         #define CRYPTOCcmInvAuthDecrypt         ROM_CRYPTOCcmInvAuthDecrypt
758     #endif
759     #ifdef ROM_CRYPTOCcmInvAuthDecryptStatus
760         #undef  CRYPTOCcmInvAuthDecryptStatus
761         #define CRYPTOCcmInvAuthDecryptStatus   ROM_CRYPTOCcmInvAuthDecryptStatus
762     #endif
763     #ifdef ROM_CRYPTOCcmInvAuthDecryptResultGet
764         #undef  CRYPTOCcmInvAuthDecryptResultGet
765         #define CRYPTOCcmInvAuthDecryptResultGet ROM_CRYPTOCcmInvAuthDecryptResultGet
766     #endif
767     #ifdef ROM_CRYPTODmaEnable
768         #undef  CRYPTODmaEnable
769         #define CRYPTODmaEnable                 ROM_CRYPTODmaEnable
770     #endif
771     #ifdef ROM_CRYPTODmaDisable
772         #undef  CRYPTODmaDisable
773         #define CRYPTODmaDisable                ROM_CRYPTODmaDisable
774     #endif
775 #endif
776 
777 //*****************************************************************************
778 //
779 // Mark the end of the C bindings section for C++ compilers.
780 //
781 //*****************************************************************************
782 #ifdef __cplusplus
783 }
784 #endif
785 
786 #endif  // __CRYPTO_H__
787 
788 //*****************************************************************************
789 //
790 //! Close the Doxygen group.
791 //! @}
792 //! @}
793 //
794 //*****************************************************************************
795