CC26xx Driver Library
crypto.h
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: crypto.h
3 * Revised: 2015-07-16 12:12:04 +0200 (Thu, 16 Jul 2015)
4 * Revision: 44151
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 //
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 //
225 //
226 //*****************************************************************************
227 extern uint32_t CRYPTOAesLoadKey(uint32_t *pui32AesKey,
228  uint32_t ui32KeyLocation);
229 
230 //*****************************************************************************
231 //
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 //
277 //
278 //*****************************************************************************
279 extern uint32_t CRYPTOAesEcbStatus(void);
280 
281 //*****************************************************************************
282 //
291 //
292 //*****************************************************************************
293 __STATIC_INLINE 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 //
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 //
364 //
365 //*****************************************************************************
366 extern uint32_t CRYPTOCcmAuthEncryptStatus(void);
367 
368 //*****************************************************************************
369 //
380 //
381 //*****************************************************************************
382 extern uint32_t CRYPTOCcmAuthEncryptResultGet(uint32_t ui32TagLength,
383  uint32_t *pui32CcmTag);
384 
385 //*****************************************************************************
386 //
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 //
438 //
439 //*****************************************************************************
440 extern uint32_t CRYPTOCcmInvAuthDecryptStatus(void);
441 
442 //*****************************************************************************
443 //
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 //
474 //
475 //*****************************************************************************
476 __STATIC_INLINE uint32_t
478 {
479  //
480  // Return the value of the status register.
481  //
482  return (HWREG(CRYPTO_BASE + CRYPTO_O_DMASTAT));
483 }
484 
485 //*****************************************************************************
486 //
496 //
497 //*****************************************************************************
498 extern void CRYPTODmaEnable(uint32_t ui32Channels);
499 
500 //*****************************************************************************
501 //
511 //
512 //*****************************************************************************
513 extern void CRYPTODmaDisable(uint32_t ui32Channels);
514 
515 //*****************************************************************************
516 //
528 //
529 //*****************************************************************************
530 __STATIC_INLINE void
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  //
543 
544  //
545  // Enable the specified interrupts.
546  //
547  HWREG(CRYPTO_BASE + CRYPTO_O_IRQEN) |= ui32IntFlags;
548 }
549 
550 //*****************************************************************************
551 //
563 //
564 //*****************************************************************************
565 __STATIC_INLINE void
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 //
595 //
596 //*****************************************************************************
597 __STATIC_INLINE uint32_t
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 //
639 //
640 //*****************************************************************************
641 __STATIC_INLINE void
642 CRYPTOIntClear(uint32_t ui32IntFlags)
643 {
644  //
645  // Check the arguments.
646  //
647  ASSERT((ui32IntFlags & CRYPTO_DMA_IN_DONE) |
648  (ui32IntFlags & CRYPTO_RESULT_RDY));
649 
650  //
651  // Clear the requested interrupt sources,
652  //
653  HWREG(CRYPTO_BASE + CRYPTO_O_IRQCLR) = ui32IntFlags;
654 }
655 
656 //*****************************************************************************
657 //
672 //
673 //*****************************************************************************
674 __STATIC_INLINE void
675 CRYPTOIntRegister(void (*pfnHandler)(void))
676 {
677  //
678  // Register the interrupt handler.
679  //
680  IntRegister(INT_CRYPTO, pfnHandler);
681 
682  //
683  // Enable the UART interrupt.
684  //
685  IntEnable(INT_CRYPTO);
686 }
687 
688 //*****************************************************************************
689 //
701 //
702 //*****************************************************************************
703 __STATIC_INLINE void
705 {
706  //
707  // Disable the interrupt.
708  //
709  IntDisable(INT_CRYPTO);
710 
711  //
712  // Unregister the interrupt handler.
713  //
714  IntUnregister(INT_CRYPTO);
715 }
716 
717 //*****************************************************************************
718 //
719 // Support for DriverLib in ROM:
720 // Redirect to implementation in ROM when available.
721 //
722 //*****************************************************************************
723 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
724  #include <driverlib/rom.h>
725  #ifdef ROM_CRYPTOAesLoadKey
726  #undef CRYPTOAesLoadKey
727  #define CRYPTOAesLoadKey ROM_CRYPTOAesLoadKey
728  #endif
729  #ifdef ROM_CRYPTOAesEcb
730  #undef CRYPTOAesEcb
731  #define CRYPTOAesEcb ROM_CRYPTOAesEcb
732  #endif
733  #ifdef ROM_CRYPTOAesEcbStatus
734  #undef CRYPTOAesEcbStatus
735  #define CRYPTOAesEcbStatus ROM_CRYPTOAesEcbStatus
736  #endif
737  #ifdef ROM_CRYPTOCcmAuthEncrypt
738  #undef CRYPTOCcmAuthEncrypt
739  #define CRYPTOCcmAuthEncrypt ROM_CRYPTOCcmAuthEncrypt
740  #endif
741  #ifdef ROM_CRYPTOCcmAuthEncryptStatus
742  #undef CRYPTOCcmAuthEncryptStatus
743  #define CRYPTOCcmAuthEncryptStatus ROM_CRYPTOCcmAuthEncryptStatus
744  #endif
745  #ifdef ROM_CRYPTOCcmAuthEncryptResultGet
746  #undef CRYPTOCcmAuthEncryptResultGet
747  #define CRYPTOCcmAuthEncryptResultGet ROM_CRYPTOCcmAuthEncryptResultGet
748  #endif
749  #ifdef ROM_CRYPTOCcmInvAuthDecrypt
750  #undef CRYPTOCcmInvAuthDecrypt
751  #define CRYPTOCcmInvAuthDecrypt ROM_CRYPTOCcmInvAuthDecrypt
752  #endif
753  #ifdef ROM_CRYPTOCcmInvAuthDecryptStatus
754  #undef CRYPTOCcmInvAuthDecryptStatus
755  #define CRYPTOCcmInvAuthDecryptStatus ROM_CRYPTOCcmInvAuthDecryptStatus
756  #endif
757  #ifdef ROM_CRYPTOCcmInvAuthDecryptResultGet
758  #undef CRYPTOCcmInvAuthDecryptResultGet
759  #define CRYPTOCcmInvAuthDecryptResultGet ROM_CRYPTOCcmInvAuthDecryptResultGet
760  #endif
761  #ifdef ROM_CRYPTODmaEnable
762  #undef CRYPTODmaEnable
763  #define CRYPTODmaEnable ROM_CRYPTODmaEnable
764  #endif
765  #ifdef ROM_CRYPTODmaDisable
766  #undef CRYPTODmaDisable
767  #define CRYPTODmaDisable ROM_CRYPTODmaDisable
768  #endif
769 #endif
770 
771 //*****************************************************************************
772 //
773 // Mark the end of the C bindings section for C++ compilers.
774 //
775 //*****************************************************************************
776 #ifdef __cplusplus
777 }
778 #endif
779 
780 #endif // __CRYPTO_H__
781 
782 //*****************************************************************************
783 //
787 //
788 //*****************************************************************************
static uint32_t CRYPTODmaStatus(void)
Get the current status of the Crypto DMA controller.
Definition: crypto.h:477
uint32_t CRYPTOCcmAuthEncryptStatus(void)
Check the result of an AES CCM operation.
Definition: crypto.c:590
#define CRYPTO_RESULT_RDY
Definition: crypto.h:113
static void CRYPTOIntClear(uint32_t ui32IntFlags)
Clears Crypto interrupt sources.
Definition: crypto.h:642
static void CRYPTOIntUnregister(void)
Unregisters an interrupt handler for a Crypto interrupt.
Definition: crypto.h:704
uint32_t CRYPTOCcmInvAuthDecrypt(bool bDecrypt, uint32_t ui32AuthLength, uint32_t *pui32Nonce, uint32_t *pui32CipherText, uint32_t ui32CipherTextLength, uint32_t *pui32Header, uint32_t ui32HeaderLength, uint32_t ui32KeyLocation, uint32_t ui32FieldLength, bool bIntEnable)
Start a CCM Decryption and Inverse Authentication operation.
Definition: crypto.c:678
static void CRYPTOIntEnable(uint32_t ui32IntFlags)
Enables individual Crypto interrupt sources.
Definition: crypto.h:531
uint32_t CRYPTOAesLoadKey(uint32_t *pui32AesKey, uint32_t ui32KeyLocation)
Write the key into the Key Ram.
Definition: crypto.c:85
uint32_t CRYPTOCcmAuthEncryptResultGet(uint32_t ui32TagLength, uint32_t *pui32CcmTag)
Get the result of an AES CCM operation.
Definition: crypto.c:629
#define CRYPTO_DMA_IN_DONE
Definition: crypto.h:112
uint32_t CRYPTOCcmInvAuthDecryptStatus(void)
Checks CCM decrypt and Inverse Authentication result.
Definition: crypto.c:913
#define ASSERT(expr)
Definition: debug.h:74
static void CRYPTOIntRegister(void(*pfnHandler)(void))
Registers an interrupt handler for a Crypto interrupt.
Definition: crypto.h:675
#define CRYPTO_INT_LEVEL
Definition: crypto.h:118
static void CRYPTOIntDisable(uint32_t ui32IntFlags)
Disables individual CRYPTO interrupt sources.
Definition: crypto.h:566
uint32_t CRYPTOCcmInvAuthDecryptResultGet(uint32_t ui32AuthLength, uint32_t *pui32CipherText, uint32_t ui32CipherTextLength, uint32_t *pui32CcmTag)
Get the result of the CCM operation.
Definition: crypto.c:952
uint32_t CRYPTOAesEcbStatus(void)
Check the result of an AES ECB operation.
Definition: crypto.c:317
static void CRYPTOAesEcbFinish(void)
Finish the encryption operation by resetting the operation mode.
Definition: crypto.h:294
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:203
static uint32_t CRYPTOIntStatus(bool bMasked)
Gets the current interrupt status.
Definition: crypto.h:598
void CRYPTODmaDisable(uint32_t ui32Channels)
Disable Crypto DMA operation.
Definition: crypto.c:1048
uint32_t CRYPTOAesEcb(uint32_t *pui32MsgIn, uint32_t *pui32MsgOut, uint32_t ui32KeyLocation, bool bEncrypt, bool bIntEnable)
Start an AES-ECB operation (encryption or decryption).
Definition: crypto.c:202
void CRYPTODmaEnable(uint32_t ui32Channels)
Enable Crypto DMA operation.
Definition: crypto.c:1021
void IntDisable(uint32_t ui32Interrupt)
Disables an interrupt.
Definition: interrupt.c:381
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Registers a function to be called when an interrupt occurs.
Definition: interrupt.c:155
void IntEnable(uint32_t ui32Interrupt)
Enables an interrupt.
Definition: interrupt.c:321
uint32_t CRYPTOCcmAuthEncrypt(bool bEncrypt, uint32_t ui32AuthLength, uint32_t *pui32Nonce, uint32_t *pui32PlainText, uint32_t ui32PlainTextLength, uint32_t *pui32Header, uint32_t ui32HeaderLength, uint32_t ui32KeyLocation, uint32_t ui32FieldLength, bool bIntEnable)
Start CCM operation.
Definition: crypto.c:357