2 * -------------------------------------------
3 * MSP432 DriverLib - v3_10_00_09
4 * -------------------------------------------
6 * --COPYRIGHT--,BSD,BSD
7 * Copyright (c) 2014, Texas Instruments Incorporated
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
38 #include <interrupt.h>
41 bool AES256_setCipherKey(uint32_t moduleInstance, const uint8_t * cipherKey,
42 uint_fast16_t keyLength)
47 AES256_CMSIS(moduleInstance)->CTL0 |= 0;
51 case AES256_KEYLENGTH_128BIT:
52 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__128BIT;
55 case AES256_KEYLENGTH_192BIT:
56 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__192BIT;
59 case AES256_KEYLENGTH_256BIT:
60 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__256BIT;
66 keyLength = keyLength / 8;
68 for (i = 0; i < keyLength; i = i + 2)
70 sCipherKey = (uint16_t) (cipherKey[i]);
71 sCipherKey = sCipherKey | ((uint16_t) (cipherKey[i + 1]) << 8);
72 AES256_CMSIS(moduleInstance)->KEY = sCipherKey;
75 // Wait until key is written
76 while (!BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_KEYWR_OFS))
82 void AES256_encryptData(uint32_t moduleInstance, const uint8_t * data,
83 uint8_t * encryptedData)
86 uint16_t tempData = 0;
87 uint16_t tempVariable = 0;
89 // Set module to encrypt mode
90 AES256_CMSIS(moduleInstance)->CTL0 &= ~AES256_CTL0_OP_MASK;
92 // Write data to encrypt to module
93 for (i = 0; i < 16; i = i + 2)
95 tempVariable = (uint16_t) (data[i]);
96 tempVariable = tempVariable | ((uint16_t) (data[i + 1]) << 8);
97 AES256_CMSIS(moduleInstance)->DIN = tempVariable;
100 // Key that is already written shall be used
101 // Encryption is initialized by setting AES256_STAT_KEYWR to 1
102 BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_KEYWR_OFS) = 1;
104 // Wait unit finished ~167 MCLK
105 while (BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_BUSY_OFS))
108 // Write encrypted data back to variable
109 for (i = 0; i < 16; i = i + 2)
111 tempData = AES256_CMSIS(moduleInstance)->DOUT;
112 *(encryptedData + i) = (uint8_t) tempData;
113 *(encryptedData + i + 1) = (uint8_t) (tempData >> 8);
117 void AES256_decryptData(uint32_t moduleInstance, const uint8_t * data,
118 uint8_t * decryptedData)
121 uint16_t tempData = 0;
122 uint16_t tempVariable = 0;
124 // Set module to decrypt mode
125 AES256_CMSIS(moduleInstance)->CTL0 |= (AES256_CTL0_OP_3);
127 // Write data to decrypt to module
128 for (i = 0; i < 16; i = i + 2)
130 tempVariable = (uint16_t) (data[i + 1] << 8);
131 tempVariable = tempVariable | ((uint16_t) (data[i]));
132 AES256_CMSIS(moduleInstance)->DIN = tempVariable;
135 // Key that is already written shall be used
136 // Now decryption starts
137 BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_KEYWR_OFS) = 1;
139 // Wait unit finished ~167 MCLK
140 while (BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_BUSY_OFS))
143 // Write encrypted data back to variable
144 for (i = 0; i < 16; i = i + 2)
146 tempData = AES256_CMSIS(moduleInstance)->DOUT;
147 *(decryptedData + i) = (uint8_t) tempData;
148 *(decryptedData + i + 1) = (uint8_t) (tempData >> 8);
152 bool AES256_setDecipherKey(uint32_t moduleInstance, const uint8_t * cipherKey,
153 uint_fast16_t keyLength)
156 uint16_t tempVariable = 0;
158 // Set module to decrypt mode
159 AES256_CMSIS(moduleInstance)->CTL0 =
160 (AES256_CMSIS(moduleInstance)->CTL0 & ~AES256_CTL0_OP_MASK) | AES256_CTL0_OP1;
164 case AES256_KEYLENGTH_128BIT:
165 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__128BIT;
168 case AES256_KEYLENGTH_192BIT:
169 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__192BIT;
172 case AES256_KEYLENGTH_256BIT:
173 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__256BIT;
180 keyLength = keyLength / 8;
182 // Write cipher key to key register
183 for (i = 0; i < keyLength; i = i + 2)
185 tempVariable = (uint16_t) (cipherKey[i]);
186 tempVariable = tempVariable | ((uint16_t) (cipherKey[i + 1]) << 8);
187 AES256_CMSIS(moduleInstance)->KEY = tempVariable;
190 // Wait until key is processed ~52 MCLK
191 while (BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_BUSY_OFS))
197 void AES256_clearInterruptFlag(uint32_t moduleInstance)
199 BITBAND_PERI(AES256_CMSIS(moduleInstance)->CTL0,AES256_CTL0_RDYIFG_OFS) = 0;
202 uint32_t AES256_getInterruptFlagStatus(uint32_t moduleInstance)
204 return BITBAND_PERI(AES256_CMSIS(moduleInstance)->CTL0, AES256_CTL0_RDYIFG_OFS);
207 void AES256_enableInterrupt(uint32_t moduleInstance)
209 BITBAND_PERI(AES256_CMSIS(moduleInstance)->CTL0,AES256_CTL0_RDYIE_OFS) = 1;
212 void AES256_disableInterrupt(uint32_t moduleInstance)
214 BITBAND_PERI(AES256_CMSIS(moduleInstance)->CTL0,AES256_CTL0_RDYIE_OFS) = 0;
217 void AES256_reset(uint32_t moduleInstance)
219 BITBAND_PERI(AES256_CMSIS(moduleInstance)->CTL0,AES256_CTL0_SWRST_OFS) = 1;
222 void AES256_startEncryptData(uint32_t moduleInstance, const uint8_t * data)
225 uint16_t tempVariable = 0;
227 // Set module to encrypt mode
228 AES256_CMSIS(moduleInstance)->CTL0 &= ~AES256_CTL0_OP_MASK;
230 // Write data to encrypt to module
231 for (i = 0; i < 16; i = i + 2)
233 tempVariable = (uint16_t) (data[i]);
234 tempVariable = tempVariable | ((uint16_t) (data[i + 1]) << 8);
235 AES256_CMSIS(moduleInstance)->DIN = tempVariable;
238 // Key that is already written shall be used
239 // Encryption is initialized by setting AES256_STAT_KEYWR to 1
240 BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_KEYWR_OFS) = 1;
243 void AES256_startDecryptData(uint32_t moduleInstance, const uint8_t * data)
246 uint16_t tempVariable = 0;
248 // Set module to decrypt mode
249 AES256_CMSIS(moduleInstance)->CTL0 |= (AES256_CTL0_OP_3);
251 // Write data to decrypt to module
252 for (i = 0; i < 16; i = i + 2)
254 tempVariable = (uint16_t) (data[i + 1] << 8);
255 tempVariable = tempVariable | ((uint16_t) (data[i]));
256 AES256_CMSIS(moduleInstance)->DIN = tempVariable;
259 // Key that is already written shall be used
260 // Now decryption starts
261 BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_KEYWR_OFS) = 1;
264 bool AES256_startSetDecipherKey(uint32_t moduleInstance,
265 const uint8_t * cipherKey, uint_fast16_t keyLength)
268 uint16_t tempVariable = 0;
270 AES256_CMSIS(moduleInstance)->CTL0 =
271 (AES256_CMSIS(moduleInstance)->CTL0 & ~AES256_CTL0_OP_MASK) | AES256_CTL0_OP1;
275 case AES256_KEYLENGTH_128BIT:
276 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__128BIT;
279 case AES256_KEYLENGTH_192BIT:
280 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__192BIT;
283 case AES256_KEYLENGTH_256BIT:
284 AES256_CMSIS(moduleInstance)->CTL0 |= AES256_CTL0_KL__256BIT;
291 keyLength = keyLength / 8;
293 // Write cipher key to key register
294 for (i = 0; i < keyLength; i = i + 2)
296 tempVariable = (uint16_t) (cipherKey[i]);
297 tempVariable = tempVariable | ((uint16_t) (cipherKey[i + 1]) << 8);
298 AES256_CMSIS(moduleInstance)->KEY = tempVariable;
304 bool AES256_getDataOut(uint32_t moduleInstance, uint8_t *outputData)
307 uint16_t tempData = 0;
309 // If module is busy, exit and return failure
310 if (BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_BUSY_OFS))
313 // Write encrypted data back to variable
314 for (i = 0; i < 16; i = i + 2)
316 tempData = AES256_CMSIS(moduleInstance)->DOUT;
317 *(outputData + i) = (uint8_t) tempData;
318 *(outputData + i + 1) = (uint8_t) (tempData >> 8);
324 bool AES256_isBusy(uint32_t moduleInstance)
326 return BITBAND_PERI(AES256_CMSIS(moduleInstance)->STAT, AES256_STAT_BUSY_OFS);
329 void AES256_clearErrorFlag(uint32_t moduleInstance)
331 BITBAND_PERI(AES256_CMSIS(moduleInstance)->CTL0, AES256_CTL0_ERRFG_OFS) = 0;
334 uint32_t AES256_getErrorFlagStatus(uint32_t moduleInstance)
336 return BITBAND_PERI(AES256_CMSIS(moduleInstance)->CTL0, AES256_CTL0_ERRFG_OFS);
339 void AES256_registerInterrupt(uint32_t moduleInstance, void (*intHandler)(void))
341 Interrupt_registerInterrupt(INT_AES256, intHandler);
342 Interrupt_enableInterrupt(INT_AES256);
345 void AES256_unregisterInterrupt(uint32_t moduleInstance)
347 Interrupt_disableInterrupt(INT_AES256);
348 Interrupt_unregisterInterrupt(INT_AES256);
351 uint32_t AES256_getInterruptStatus(uint32_t moduleInstance)
353 return AES256_getInterruptFlagStatus(moduleInstance);