]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/aes256.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / aes256.c
1 /*
2  * -------------------------------------------
3  *    MSP432 DriverLib - v01_04_00_18 
4  * -------------------------------------------
5  *
6  * --COPYRIGHT--,BSD,BSD
7  * Copyright (c) 2015, Texas Instruments Incorporated
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * *  Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * *  Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * *  Neither the name of Texas Instruments Incorporated nor the names of
22  *    its contributors may be used to endorse or promote products derived
23  *    from this software without 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,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  * --/COPYRIGHT--*/
37 #include <aes256.h>
38 #include <interrupt.h>
39 #include <debug.h>
40
41 bool AES256_setCipherKey(uint32_t moduleInstance, const uint8_t * cipherKey,
42         uint_fast16_t keyLength)
43 {
44     uint8_t i;
45     uint16_t sCipherKey;
46
47     AES256_CMSIS(moduleInstance)->rCTL0.r |= 0;
48
49     switch (keyLength)
50     {
51     case AES256_KEYLENGTH_128BIT:
52         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__128BIT;
53         break;
54
55     case AES256_KEYLENGTH_192BIT:
56         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__192BIT;
57         break;
58
59     case AES256_KEYLENGTH_256BIT:
60         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__256BIT;
61         break;
62     default:
63         return false;
64     }
65
66     keyLength = keyLength / 8;
67
68     for (i = 0; i < keyLength; i = i + 2)
69     {
70         sCipherKey = (uint16_t) (cipherKey[i]);
71         sCipherKey = sCipherKey | ((uint16_t) (cipherKey[i + 1]) << 8);
72         AES256_CMSIS(moduleInstance)->rKEY.r = sCipherKey;
73     }
74
75     // Wait until key is written
76     while (!BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESKEYWR_OFS))
77         ;
78
79     return true;
80 }
81
82 void AES256_encryptData(uint32_t moduleInstance, const uint8_t * data,
83         uint8_t * encryptedData)
84 {
85     uint8_t i;
86     uint16_t tempData = 0;
87     uint16_t tempVariable = 0;
88
89     // Set module to encrypt mode
90     AES256_CMSIS(moduleInstance)->rCTL0.r &= ~AESOP_M;
91
92     // Write data to encrypt to module
93     for (i = 0; i < 16; i = i + 2)
94     {
95         tempVariable = (uint16_t) (data[i]);
96         tempVariable = tempVariable | ((uint16_t) (data[i + 1]) << 8);
97         AES256_CMSIS(moduleInstance)->rDIN.r = tempVariable;
98     }
99
100     // Key that is already written shall be used
101     // Encryption is initialized by setting AESKEYWR to 1
102     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESKEYWR_OFS) = 1;
103
104     // Wait unit finished ~167 MCLK
105     while (BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESBUSY_OFS))
106         ;
107
108     // Write encrypted data back to variable
109     for (i = 0; i < 16; i = i + 2)
110     {
111         tempData = AES256_CMSIS(moduleInstance)->rDOUT.r;
112         *(encryptedData + i) = (uint8_t) tempData;
113         *(encryptedData + i + 1) = (uint8_t) (tempData >> 8);
114     }
115 }
116
117 void AES256_decryptData(uint32_t moduleInstance, const uint8_t * data,
118         uint8_t * decryptedData)
119 {
120     uint8_t i;
121     uint16_t tempData = 0;
122     uint16_t tempVariable = 0;
123
124     // Set module to decrypt mode
125     AES256_CMSIS(moduleInstance)->rCTL0.r |= (AESOP_3);
126
127     // Write data to decrypt to module
128     for (i = 0; i < 16; i = i + 2)
129     {
130         tempVariable = (uint16_t) (data[i + 1] << 8);
131         tempVariable = tempVariable | ((uint16_t) (data[i]));
132         AES256_CMSIS(moduleInstance)->rDIN.r = tempVariable;
133     }
134
135     // Key that is already written shall be used
136     // Now decryption starts
137     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESKEYWR_OFS) = 1;
138
139     // Wait unit finished ~167 MCLK
140     while (BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESBUSY_OFS))
141         ;
142
143     // Write encrypted data back to variable
144     for (i = 0; i < 16; i = i + 2)
145     {
146         tempData = AES256_CMSIS(moduleInstance)->rDOUT.r;
147         *(decryptedData + i) = (uint8_t) tempData;
148         *(decryptedData + i + 1) = (uint8_t) (tempData >> 8);
149     }
150 }
151
152 bool AES256_setDecipherKey(uint32_t moduleInstance, const uint8_t * cipherKey,
153         uint_fast16_t keyLength)
154 {
155     uint8_t i;
156     uint16_t tempVariable = 0;
157
158     // Set module to decrypt mode
159     AES256_CMSIS(moduleInstance)->rCTL0.r =
160             (AES256_CMSIS(moduleInstance)->rCTL0.r & ~AESOP_M) | AESOP1;
161
162     switch (keyLength)
163     {
164     case AES256_KEYLENGTH_128BIT:
165         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__128BIT;
166         break;
167
168     case AES256_KEYLENGTH_192BIT:
169         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__192BIT;
170         break;
171
172     case AES256_KEYLENGTH_256BIT:
173         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__256BIT;
174         break;
175
176     default:
177         return false;
178     }
179
180     keyLength = keyLength / 8;
181
182     // Write cipher key to key register
183     for (i = 0; i < keyLength; i = i + 2)
184     {
185         tempVariable = (uint16_t) (cipherKey[i]);
186         tempVariable = tempVariable | ((uint16_t) (cipherKey[i + 1]) << 8);
187         AES256_CMSIS(moduleInstance)->rKEY.r = tempVariable;
188     }
189
190     // Wait until key is processed ~52 MCLK
191     while (BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESBUSY_OFS))
192         ;
193
194     return true;
195 }
196
197 void AES256_clearInterruptFlag(uint32_t moduleInstance)
198 {
199     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rCTL0.r,AESRDYIFG_OFS) = 0;
200 }
201
202 uint32_t AES256_getInterruptFlagStatus(uint32_t moduleInstance)
203 {
204     return BITBAND_PERI(AES256_CMSIS(moduleInstance)->rCTL0.r, AESRDYIFG_OFS);
205 }
206
207 void AES256_enableInterrupt(uint32_t moduleInstance)
208 {
209     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rCTL0.r,AESRDYIE_OFS) = 1;
210 }
211
212 void AES256_disableInterrupt(uint32_t moduleInstance)
213 {
214     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rCTL0.r,AESRDYIE_OFS) = 0;
215 }
216
217 void AES256_reset(uint32_t moduleInstance)
218 {
219     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rCTL0.r,AESSWRST_OFS) = 1;
220 }
221
222 void AES256_startEncryptData(uint32_t moduleInstance, const uint8_t * data)
223 {
224     uint8_t i;
225     uint16_t tempVariable = 0;
226
227     // Set module to encrypt mode
228     AES256_CMSIS(moduleInstance)->rCTL0.r &= ~AESOP_M;
229
230     // Write data to encrypt to module
231     for (i = 0; i < 16; i = i + 2)
232     {
233         tempVariable = (uint16_t) (data[i]);
234         tempVariable = tempVariable | ((uint16_t) (data[i + 1]) << 8);
235         AES256_CMSIS(moduleInstance)->rDIN.r = tempVariable;
236     }
237
238     // Key that is already written shall be used
239     // Encryption is initialized by setting AESKEYWR to 1
240     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESKEYWR_OFS) = 1;
241 }
242
243 void AES256_startDecryptData(uint32_t moduleInstance, const uint8_t * data)
244 {
245     uint8_t i;
246     uint16_t tempVariable = 0;
247
248     // Set module to decrypt mode
249     AES256_CMSIS(moduleInstance)->rCTL0.r |= (AESOP_3);
250
251     // Write data to decrypt to module
252     for (i = 0; i < 16; i = i + 2)
253     {
254         tempVariable = (uint16_t) (data[i + 1] << 8);
255         tempVariable = tempVariable | ((uint16_t) (data[i]));
256         AES256_CMSIS(moduleInstance)->rDIN.r = tempVariable;
257     }
258
259     // Key that is already written shall be used
260     // Now decryption starts
261     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESKEYWR_OFS) = 1;
262 }
263
264 bool AES256_startSetDecipherKey(uint32_t moduleInstance,
265         const uint8_t * cipherKey, uint_fast16_t keyLength)
266 {
267     uint8_t i;
268     uint16_t tempVariable = 0;
269
270     AES256_CMSIS(moduleInstance)->rCTL0.r =
271             (AES256_CMSIS(moduleInstance)->rCTL0.r & ~AESOP_M) | AESOP1;
272
273     switch (keyLength)
274     {
275     case AES256_KEYLENGTH_128BIT:
276         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__128BIT;
277         break;
278
279     case AES256_KEYLENGTH_192BIT:
280         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__192BIT;
281         break;
282
283     case AES256_KEYLENGTH_256BIT:
284         AES256_CMSIS(moduleInstance)->rCTL0.r |= AESKL__256BIT;
285         break;
286
287     default:
288         return false;
289     }
290
291     keyLength = keyLength / 8;
292
293     // Write cipher key to key register
294     for (i = 0; i < keyLength; i = i + 2)
295     {
296         tempVariable = (uint16_t) (cipherKey[i]);
297         tempVariable = tempVariable | ((uint16_t) (cipherKey[i + 1]) << 8);
298         AES256_CMSIS(moduleInstance)->rKEY.r = tempVariable;
299     }
300
301     return true;
302 }
303
304 bool AES256_getDataOut(uint32_t moduleInstance, uint8_t *outputData)
305 {
306     uint8_t i;
307     uint16_t tempData = 0;
308
309     // If module is busy, exit and return failure
310     if (BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESBUSY_OFS))
311         return false;
312
313     // Write encrypted data back to variable
314     for (i = 0; i < 16; i = i + 2)
315     {
316         tempData = AES256_CMSIS(moduleInstance)->rDOUT.r;
317         *(outputData + i) = (uint8_t) tempData;
318         *(outputData + i + 1) = (uint8_t) (tempData >> 8);
319     }
320
321     return true;
322 }
323
324 bool AES256_isBusy(uint32_t moduleInstance)
325 {
326     return BITBAND_PERI(AES256_CMSIS(moduleInstance)->rSTAT.r, AESBUSY_OFS);
327 }
328
329 void AES256_clearErrorFlag(uint32_t moduleInstance)
330 {
331     BITBAND_PERI(AES256_CMSIS(moduleInstance)->rCTL0.r, AESERRFG_OFS) = 0;
332 }
333
334 uint32_t AES256_getErrorFlagStatus(uint32_t moduleInstance)
335 {
336     return BITBAND_PERI(AES256_CMSIS(moduleInstance)->rCTL0.r, AESERRFG_OFS);
337 }
338
339 void AES256_registerInterrupt(uint32_t moduleInstance, void (*intHandler)(void))
340 {
341     Interrupt_registerInterrupt(INT_AES256, intHandler);
342     Interrupt_enableInterrupt(INT_AES256);
343 }
344
345 void AES256_unregisterInterrupt(uint32_t moduleInstance)
346 {
347     Interrupt_disableInterrupt(INT_AES256);
348     Interrupt_unregisterInterrupt(INT_AES256);
349 }
350
351 uint32_t AES256_getInterruptStatus(uint32_t moduleInstance)
352 {
353     return AES256_getInterruptFlagStatus(moduleInstance);
354 }
355