[sub1ghz-sensor-to-cloud/tida01476.git] / sensor_cc1310lp / Application / MAC / HAL / target / hal_aes.c
1 /******************************************************************************
3 @file hal_aes.c
5 @brief Support for Hardware AES encryption.
7 Group: WCS, LPC, BTS
8 Target Device: CC13xx
10 ******************************************************************************
12 Copyright (c) 2011-2017, Texas Instruments Incorporated
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 its contributors may be used to endorse or promote products derived
28 from this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
32 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 ******************************************************************************
43 Release Name: simplelink_cc13x0_sdk_1_30_00_06"
44 Release Date: 2017-03-08 14:43:25
45 *****************************************************************************/
47 /******************************************************************************
48 * INCLUDES
49 */
50 #include "osal.h"
51 #include "hal_aes.h"
52 #include "hal_mcu.h"
53 #include "hal_ccm.h"
54 #include <inc/hw_crypto.h>
55 #include <driverlib/crypto.h>
57 /******************************************************************************
58 * MACROS
59 */
60 /******************************************************************************
61 * CONSTANTS
62 */
64 /******************************************************************************
65 * TYPEDEFS
66 */
68 /******************************************************************************
69 * LOCAL VARIABLES
70 */
72 /******************************************************************************
73 * GLOBAL VARIABLES
74 */
75 void (*pSspAesEncrypt)( uint8 *, uint8 * ) = (void*)NULL;
77 /******************************************************************************
78 * FUNCTION PROTOTYPES
79 */
81 /******************************************************************************
82 * @fn HalAesInit
83 *
84 * @brief Initilize AES engine
85 *
86 * input parameters
87 *
88 * @param None
89 *
90 * @return None
91 */
92 void HalAesInit( void )
93 {
94 HWREG(CRYPTO_BASE + CRYPTO_O_IRQTYPE) |= 0x00000001;
95 HWREG(CRYPTO_BASE + CRYPTO_O_IRQEN) |= 0x00000003;
96 }
99 /******************************************************************************
100 * @fn ssp_HW_KeyInit
101 *
102 * @brief Writes the key into AES engine
103 *
104 * input parameters
105 *
106 * @param AesKey - Pointer to AES Key.
107 *
108 * @return None
109 */
110 void ssp_HW_KeyInit( uint8 *AesKey )
111 {
112 /* Load the AES key
113 * KeyStore has rentention after PM2
114 */
115 CRYPTOAesLoadKey( (uint32_t *)AesKey, 0);
116 }
119 /******************************************************************************
120 * @fn sspAesEncryptHW
121 *
122 * @brief Encrypts 16 byte block using AES encryption engine
123 *
124 * input parameters
125 *
126 * @param AesKey - Pointer to AES Key.
127 * @param Cstate - Pointer to input data.
128 *
129 * output parameters
130 *
131 * @param Cstate - Pointer to encrypted data.
132 *
133 * @return None
134 *
135 */
136 void sspAesEncryptHW( uint8 *AesKey, uint8 *Cstate )
137 {
138 /* please note that ssp_HW_KeyInit(AesKey); should have already
139 * been called prior to using this function
140 */
141 CRYPTOAesEcb( (uint32_t *)Cstate, (uint32_t *)Cstate, 0, true, false);
143 /* wait for completion of the operation */
144 do
145 {
146 ASM_NOP;
147 }while(CRYPTOAesEcbStatus());
149 CRYPTOAesEcbFinish();
150 }
153 /******************************************************************************
154 * @fn sspAesDecryptHW
155 *
156 * @brief Decrypts 16 byte block using AES encryption engine
157 *
158 * input parameters
159 *
160 * @param AesKey - Pointer to AES Key.
161 * @param Cstate - Pointer to input data.
162 *
163 * output parameters
164 *
165 * @param Cstate - Pointer to decrypted data.
166 *
167 * @return None
168 *
169 */
170 void sspAesDecryptHW( uint8 *AesKey, uint8 *Cstate )
171 {
172 /* please note that ssp_HW_KeyInit(AesKey); should have already
173 * been called prior to using this function
174 */
175 CRYPTOAesEcb( (uint32_t *)Cstate, (uint32_t *)Cstate, 0, false, false);
177 /* wait for completion of the operation */
178 do
179 {
180 ASM_NOP;
181 }while(!(CRYPTOAesEcbStatus()));
183 CRYPTOAesEcbFinish();
184 }
187 /******************************************************************************
188 * @fn sspAesEncryptHW_keylocation
189 *
190 * @brief Encrypts 16 byte block using AES encryption engine
191 *
192 * input parameters
193 *
194 * @param AesKey - Pointer to AES Key.
195 * @param Cstate - Pointer to input data.
196 *
197 * output parameters
198 *
199 * @param Cstate - Pointer to encrypted data.
200 *
201 * @return None
202 *
203 */
204 void sspAesEncryptHW_keylocation(uint8 *msg_in, uint8 *msg_out, uint8 key_location )
205 {
206 /* please note that ssp_HW_KeyInit(AesKey); should have already
207 * been called prior to using this function
208 */
209 CRYPTOAesEcb( (uint32_t *)msg_in, (uint32_t *)msg_out, key_location, true, false);
211 /* wait for completion of the operation */
212 do
213 {
214 ASM_NOP;
215 }while(!(CRYPTOAesEcbStatus()));
217 CRYPTOAesEcbFinish();
218 }
221 /******************************************************************************
222 * @fn sspAesDecryptHW_keylocation
223 *
224 * @brief Decrypts 16 byte block using AES decryption engine
225 *
226 * input parameters
227 *
228 * @param AesKey - Pointer to AES Key.
229 * @param Cstate - Pointer to input data.
230 *
231 * output parameters
232 *
233 * @param Cstate - Pointer to encrypted data.
234 *
235 * @return None
236 *
237 */
238 void sspAesDecryptHW_keylocation( uint8 *msg_in, uint8 *msg_out, uint8 key_location )
239 {
240 /* please note that ssp_HW_KeyInit(AesKey); should have already
241 * been called prior to using this function
242 */
243 CRYPTOAesEcb( (uint32_t *)msg_in, (uint32_t *)msg_out, key_location, false, false);
245 /* wait for completion of the operation */
246 do
247 {
248 ASM_NOP;
249 }while(CRYPTOAesEcbStatus());
251 CRYPTOAesEcbFinish();
252 }