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