2020-2021 Sunseeker Telemetry and Lighting System
aes.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // aes.c - Driver for the aes Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_AES__
17 #include "aes.h"
18 
19 #include <assert.h>
20 
21 uint8_t AES_setCipherKey (uint16_t baseAddress,
22  const uint8_t * CipherKey
23  )
24 {
25  uint8_t i = 0;
26  uint16_t tempVariable = 0;
27 
28  // Wait until AES accelerator is busy
29  while(AESBUSY == (HWREG16(baseAddress + OFS_AESASTAT) & AESBUSY) );
30 
31  for (i = 0; i < 16; i = i + 2)
32  {
33  tempVariable = (uint16_t)(CipherKey[i]);
34  tempVariable = tempVariable | ((uint16_t)(CipherKey[i + 1]) << 8);
35  HWREG16(baseAddress + OFS_AESAKEY) = tempVariable;
36  }
37 
38  // Wait until key is written
39  while(0x00 == (HWREG16(baseAddress + OFS_AESASTAT) & AESKEYWR ));
40 
41  return STATUS_SUCCESS;
42 }
43 
44 uint8_t AES_encryptData (uint16_t baseAddress,
45  const uint8_t * Data,
46  uint8_t * encryptedData)
47 {
48  uint8_t i;
49  uint16_t tempData = 0;
50  uint16_t tempVariable = 0;
51 
52  // Set module to encrypt mode
53  HWREG16(baseAddress + OFS_AESACTL0) &= ~AESOP_3;
54 
55  // Write data to encrypt to module
56  for (i = 0; i < 16; i = i + 2)
57  {
58 
59  tempVariable = (uint16_t)(Data[i]);
60  tempVariable = tempVariable | ((uint16_t)(Data[i+1]) << 8);
61  HWREG16(baseAddress + OFS_AESADIN) = tempVariable;
62  }
63 
64  // Key that is already written shall be used
65  // Encryption is initialized by setting AESKEYWR to 1
66  HWREG16(baseAddress + OFS_AESASTAT) |= AESKEYWR;
67 
68  // Wait unit finished ~167 MCLK
69  while(AESBUSY == (HWREG16(baseAddress + OFS_AESASTAT) & AESBUSY) );
70 
71  // Write encrypted data back to variable
72  for (i = 0; i < 16; i = i + 2)
73  {
74  tempData = HWREG16(baseAddress + OFS_AESADOUT);
75  *(encryptedData + i) = (uint8_t)tempData;
76  *(encryptedData +i + 1) = (uint8_t)(tempData >> 8);
77 
78  }
79 
80  return STATUS_SUCCESS;
81 }
82 
83 uint8_t AES_decryptData (uint16_t baseAddress,
84  const uint8_t * Data,
85  uint8_t * decryptedData)
86 {
87  uint8_t i;
88  uint16_t tempData = 0;
89  uint16_t tempVariable = 0;
90 
91  // Set module to decrypt mode
92  HWREG16(baseAddress + OFS_AESACTL0) |= (AESOP_3);
93 
94  // Write data to decrypt to module
95  for (i = 0; i < 16; i = i + 2)
96  {
97  tempVariable = (uint16_t)(Data[i+1] << 8);
98  tempVariable = tempVariable | ((uint16_t)(Data[i]));
99  HWREG16(baseAddress + OFS_AESADIN) = tempVariable;
100  }
101 
102  // Key that is already written shall be used
103  // Now decryption starts
104  HWREG16(baseAddress + OFS_AESASTAT) |= AESKEYWR;
105 
106  // Wait unit finished ~167 MCLK
107  while(AESBUSY == (HWREG16(baseAddress + OFS_AESASTAT) & AESBUSY ));
108 
109  // Write encrypted data back to variable
110  for (i = 0; i < 16; i = i + 2)
111  {
112  tempData = HWREG16(baseAddress + OFS_AESADOUT);
113  *(decryptedData + i ) = (uint8_t)tempData;
114  *(decryptedData +i + 1) = (uint8_t)(tempData >> 8);
115  }
116 
117  return STATUS_SUCCESS;
118 }
119 
120 uint8_t AES_setDecipherKey (uint16_t baseAddress,
121  const uint8_t * CipherKey)
122 {
123  uint8_t i;
124  uint16_t tempVariable = 0;
125 
126  // Set module to decrypt mode
127  HWREG16(baseAddress + OFS_AESACTL0) &= ~(AESOP0);
128  HWREG16(baseAddress + OFS_AESACTL0) |= AESOP1;
129 
130  // Write cipher key to key register
131  for (i = 0; i < 16; i = i + 2)
132  {
133  tempVariable = (uint16_t)(CipherKey[i]);
134  tempVariable = tempVariable | ((uint16_t)(CipherKey[i + 1]) << 8);
135  HWREG16(baseAddress + OFS_AESAKEY) = tempVariable;
136  }
137 
138  // Wait until key is processed ~52 MCLK
139  while((HWREG16(baseAddress + OFS_AESASTAT) & AESBUSY) == AESBUSY);
140 
141  return STATUS_SUCCESS;
142 }
143 
144 void AES_clearInterrupt (uint16_t baseAddress )
145 {
146  HWREG8(baseAddress + OFS_AESACTL0) &= ~AESRDYIFG;
147 }
148 
149 uint32_t AES_getInterruptStatus (uint16_t baseAddress)
150 {
151  return ((HWREG8(baseAddress + OFS_AESACTL0) & AESRDYIFG) << 0x04);
152 }
153 
154 void AES_enableInterrupt (uint16_t baseAddress)
155 {
156  HWREG8(baseAddress + OFS_AESACTL0) |= AESRDYIE;
157 }
158 
159 void AES_disableInterrupt (uint16_t baseAddress)
160 {
161  HWREG8(baseAddress + OFS_AESACTL0) &= ~AESRDYIE;
162 }
163 
164 void AES_reset (uint16_t baseAddress)
165 {
166  HWREG8(baseAddress + OFS_AESACTL0) |= AESSWRST;
167 }
168 
169 uint8_t AES_startEncryptData (uint16_t baseAddress,
170  const uint8_t * Data,
171  uint8_t * encryptedData)
172 {
173  uint8_t i;
174  uint16_t tempVariable = 0;
175 
176  // Set module to encrypt mode
177  HWREG16(baseAddress + OFS_AESACTL0) &= ~AESOP_3;
178 
179  // Write data to encrypt to module
180  for (i = 0; i < 16; i = i + 2)
181  {
182  tempVariable = (uint16_t)(Data[i]);
183  tempVariable = tempVariable | ((uint16_t)(Data[i+1]) << 8);
184  HWREG16(baseAddress + OFS_AESADIN) = tempVariable;
185  }
186 
187  // Key that is already written shall be used
188  // Encryption is initialized by setting AESKEYWR to 1
189  HWREG16(baseAddress + OFS_AESASTAT) |= AESKEYWR;
190 
191  return STATUS_SUCCESS;
192 }
193 
194 uint8_t AES_startDecryptData (uint16_t baseAddress,
195  const uint8_t * Data)
196 {
197  uint8_t i;
198  uint16_t tempVariable = 0;
199 
200  // Set module to decrypt mode
201  HWREG16(baseAddress + OFS_AESACTL0) |= (AESOP_3);
202 
203  // Write data to decrypt to module
204  for (i = 0; i < 16; i = i + 2)
205  {
206  tempVariable = (uint16_t)(Data[i+1] << 8);
207  tempVariable = tempVariable | ((uint16_t)(Data[i]));
208  HWREG16(baseAddress + OFS_AESADIN) = tempVariable;
209  }
210 
211  // Key that is already written shall be used
212  // Now decryption starts
213  HWREG16(baseAddress + OFS_AESASTAT) |= AESKEYWR;
214 
215  return STATUS_SUCCESS;
216 }
217 
218 uint8_t AES_startSetDecipherKey (uint16_t baseAddress,
219  const uint8_t * CipherKey)
220 {
221  uint8_t i;
222  uint16_t tempVariable = 0;
223 
224  HWREG16(baseAddress + OFS_AESACTL0) &= ~(AESOP0);
225  HWREG16(baseAddress + OFS_AESACTL0) |= AESOP1;
226 
227  // Write cipher key to key register
228  for (i = 0; i < 16; i = i + 2)
229  {
230  tempVariable = (uint16_t)(CipherKey[i]);
231  tempVariable = tempVariable | ((uint16_t)(CipherKey[i+1]) << 8);
232  HWREG16(baseAddress + OFS_AESAKEY) = tempVariable;
233  }
234 
235  return STATUS_SUCCESS;
236 }
237 
238 uint8_t AES_getDataOut(uint16_t baseAddress,
239  uint8_t *OutputData
240  )
241 {
242  uint8_t i;
243  uint16_t tempData = 0;
244 
245  // If module is busy, exit and return failure
246  if( AESBUSY == (HWREG16(baseAddress + OFS_AESASTAT) & AESBUSY))
247  return STATUS_FAIL;
248 
249  // Write encrypted data back to variable
250  for (i = 0; i < 16; i = i + 2)
251  {
252  tempData = HWREG16(baseAddress + OFS_AESADOUT);
253  *(OutputData + i) = (uint8_t)tempData;
254  *(OutputData +i + 1) = (uint8_t)(tempData >> 8);
255  }
256 
257  return STATUS_SUCCESS;
258 }
259 
260 uint8_t AES_isBusy (uint16_t baseAddress)
261 {
262  return (HWREG16(baseAddress + OFS_AESASTAT) & AESBUSY);
263 }
264 
265 void AES_clearErrorFlag (uint16_t baseAddress )
266 {
267  HWREG8(baseAddress + OFS_AESACTL0) &= ~AESERRFG;
268 }
269 
270 uint32_t AES_getErrorFlagStatus (uint16_t baseAddress)
271 {
272  return (HWREG8(baseAddress + OFS_AESACTL0) & AESERRFG);
273 }
274 
275 uint8_t AES_startDecryptDataUsingEncryptionKey (
276  uint16_t baseAddress,
277  const uint8_t * Data)
278 {
279  uint8_t i;
280  uint16_t tempVariable = 0;
281 
282  // Set module to decrypt mode
283  HWREG16(baseAddress + OFS_AESACTL0) &= ~(AESOP1);
284  HWREG16(baseAddress + OFS_AESACTL0) |= AESOP0;
285 
286  // Write data to decrypt to module
287  for (i = 0; i < 16; i = i + 2)
288  {
289  tempVariable = (uint16_t)(Data[i+1] << 8);
290  tempVariable = tempVariable | ((uint16_t)(Data[i]));
291  HWREG16(baseAddress + OFS_AESADIN) = tempVariable;
292  }
293 
294  // Key that is already written shall be used
295  // Now decryption starts
296  HWREG16(baseAddress + OFS_AESASTAT) |= AESKEYWR;
297 
298  return STATUS_SUCCESS;
299 }
300 
301 uint8_t AES_decryptDataUsingEncryptionKey (uint16_t baseAddress,
302  const uint8_t * Data,
303  uint8_t * decryptedData)
304 {
305  uint8_t i;
306  uint16_t tempData = 0;
307  uint16_t tempVariable = 0;
308 
309  // Set module to decrypt mode
310  HWREG16(baseAddress + OFS_AESACTL0) &= ~(AESOP1);
311  HWREG16(baseAddress + OFS_AESACTL0) |= AESOP0;
312 
313  // Write data to decrypt to module
314  for (i = 0; i < 16; i = i + 2)
315  {
316  tempVariable = (uint16_t)(Data[i+1] << 8);
317  tempVariable = tempVariable | ((uint16_t)(Data[i]));
318  HWREG16(baseAddress + OFS_AESADIN) = tempVariable;
319  }
320 
321  // Key that is already written shall be used
322  // Now decryption starts
323  HWREG16(baseAddress + OFS_AESASTAT) |= AESKEYWR;
324 
325  // Wait unit finished ~214 MCLK
326  while(AESBUSY == (HWREG16(baseAddress + OFS_AESASTAT) & AESBUSY) );
327 
328  // Write encrypted data back to variable
329  for (i = 0; i < 16; i = i + 2)
330  {
331  tempData = HWREG16(baseAddress + OFS_AESADOUT);
332  *(decryptedData + i ) = (uint8_t)tempData;
333  *(decryptedData +i + 1) = (uint8_t)(tempData >> 8);
334  }
335 
336  return STATUS_SUCCESS;
337 }
338 
339 #endif
340 //*****************************************************************************
341 //
344 //
345 //*****************************************************************************
uint16_t Data
uint8_t CipherKey[32]
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_FAIL
Definition: hw_memmap.h:23
#define STATUS_SUCCESS
Definition: hw_memmap.h:22