-
Notifications
You must be signed in to change notification settings - Fork 5
/
Encrypt.cpp
406 lines (344 loc) · 9.95 KB
/
Encrypt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/******************************************************************************************
* Copyright 2017 Ideetron B.V.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************************/
/****************************************************************************************
* File: Encrypt.cpp
* Author: Gerben den Hartog
* Compagny: Ideetron B.V.
* Website: http://www.ideetron.nl/LoRa
* E-mail: [email protected]
****************************************************************************************/
/****************************************************************************************
* Created on: 09-02-2017
* Supported Hardware: ID150119-02 Nexus board with RFM95
****************************************************************************************/
/*
*****************************************************************************************
* INCLUDE FILES
*****************************************************************************************
*/
#include "Encrypt.h"
#include "AES-128.h"
#include "Struct.h"
/*
*****************************************************************************************
* INCLUDE GLOBAL VARIABLES
*****************************************************************************************
*/
/*
*****************************************************************************************
* Description : Function used to encrypt and decrypt the data in a LoRaWAN data message
*
* Arguments : *Buffer pointer to the buffer cointaining the data to de/encrypt
* *Session_Data pointer to sLoRa_Session sturct
* *Message pointer to sLoRa_Message struct containing the message specific variables
*****************************************************************************************
*/
void Encrypt_Payload(sBuffer *Buffer, unsigned char *Key, sLoRa_Message *Message)
{
unsigned char i = 0x00;
unsigned char j;
unsigned char Number_of_Blocks = 0x00;
unsigned char Incomplete_Block_Size = 0x00;
unsigned char Block_A[16];
//Calculate number of blocks
Number_of_Blocks = Buffer->Counter / 16;
Incomplete_Block_Size = Buffer->Counter % 16;
if(Incomplete_Block_Size != 0)
{
Number_of_Blocks++;
}
for(i = 0x00; i < Number_of_Blocks; i++)
{
Block_A[0] = 0x01;
Block_A[1] = 0x00;
Block_A[2] = 0x00;
Block_A[3] = 0x00;
Block_A[4] = 0x00;
Block_A[5] = Message->Direction;
Block_A[6] = Message->DevAddr[3];
Block_A[7] = Message->DevAddr[2];
Block_A[8] = Message->DevAddr[1];
Block_A[9] = Message->DevAddr[0];
Block_A[10] = (Message->Frame_Counter & 0x00FF);
Block_A[11] = ((Message->Frame_Counter >> 8) & 0x00FF);
Block_A[12] = 0x00; //Frame counter upper Bytes
Block_A[13] = 0x00;
Block_A[14] = 0x00;
Block_A[15] = i + 1;
//Calculate S
AES_Encrypt(Block_A,Key);
//Check for last block
if(i != (Number_of_Blocks - 1))
{
for(j = 0; j < 16; j++)
{
Buffer->Data[(i*16)+j] ^= Block_A[j];
}
}
else
{
if(Incomplete_Block_Size == 0)
{
Incomplete_Block_Size = 16;
}
for(j = 0; j < Incomplete_Block_Size; j++)
{
Buffer->Data[(i*16)+j] ^= Block_A[j];
}
}
}
}
/*
*****************************************************************************************
* Description : Function used to build a the data that is used for calculating the MIC of a data message
*
* Arguments : *Buffer pointer to the buffer cointaining the data
* *Session_Data pointer to sLoRa_Session sturct
* *Message pointer to sLoRa_Message struct containing the message specific variables
*****************************************************************************************
*/
void Construct_Data_MIC(sBuffer *Buffer, sLoRa_Session *Session_Data, sLoRa_Message *Message)
{
unsigned char i;
unsigned char MIC_Data[80];
sBuffer MIC_Buffer = { &MIC_Data[0], 0x00 };
unsigned char Block_B[16];
//Construct Block B
Block_B[0] = 0x49;
Block_B[1] = 0x00;
Block_B[2] = 0x00;
Block_B[3] = 0x00;
Block_B[4] = 0x00;
Block_B[5] = Message->Direction;
Block_B[6] = Message->DevAddr[3];
Block_B[7] = Message->DevAddr[2];
Block_B[8] = Message->DevAddr[1];
Block_B[9] = Message->DevAddr[0];
Block_B[10] = (Message->Frame_Counter & 0x00FF);
Block_B[11] = ((Message->Frame_Counter >> 8) & 0x00FF);
Block_B[12] = 0x00; //Frame counter upper bytes
Block_B[13] = 0x00;
Block_B[14] = 0x00;
Block_B[15] = Buffer->Counter;
//Copy Block B into MIC data
for(i = 0x00; i < 16; i++)
{
MIC_Data[i] = Block_B[i];
}
//Add data to it
for(i = 0x00; i < Buffer->Counter; i++)
{
MIC_Data[i + 16] = Buffer->Data[i];
}
//Calculate the correct buffer length
MIC_Buffer.Counter = 16 + Buffer->Counter;
//Calculate the MIC
Calculate_MIC(&MIC_Buffer, Session_Data->NwkSKey, Message);
}
/*
*****************************************************************************************
* Description : Function used to calculate the MIC of data
*
* Arguments : *Buffer pointer to the buffer cointaining the data the MIC should be calculated from
* *Key pointer to key used for the MIC calculation
* *Message pointer to sLoRa_Message struct containing the message specific variables
*****************************************************************************************
*/
void Calculate_MIC(sBuffer *Buffer, unsigned char *Key, sLoRa_Message *Message)
{
unsigned char i, j;
unsigned char Key_K1[16] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned char Key_K2[16] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned char Old_Data[16] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned char New_Data[16] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned char Number_of_Blocks = 0x00;
unsigned char Incomplete_Block_Size = 0x00;
//Calculate number of Blocks and blocksize of last block
Number_of_Blocks = Buffer->Counter / 16;
Incomplete_Block_Size = Buffer->Counter % 16;
//if there is an incomplete block at the end add 1 to the number of blocks
if(Incomplete_Block_Size != 0)
{
Number_of_Blocks++;
}
Generate_Keys(Key, Key_K1, Key_K2);
//Preform full calculating until n-1 messsage blocks
for(j = 0x0; j < (Number_of_Blocks - 1); j++)
{
//Copy data into array
for(i = 0; i < 16; i++)
{
New_Data[i] = Buffer->Data[(j*16)+i];
}
//Preform XOR with old data
XOR(New_Data,Old_Data);
//Preform AES encryption
AES_Encrypt(New_Data,Key);
//Copy New_Data to Old_Data
for(i = 0; i < 16; i++)
{
Old_Data[i] = New_Data[i];
}
}
//Perform calculation on last block
//Check if Datalength is a multiple of 16
if(Incomplete_Block_Size == 0)
{
//Copy last data into array
for(i = 0; i < 16; i++)
{
New_Data[i] = Buffer->Data[((Number_of_Blocks -1)*16)+i];
}
//Preform XOR with Key 1
XOR(New_Data,Key_K1);
//Preform XOR with old data
XOR(New_Data,Old_Data);
//Preform last AES routine
AES_Encrypt(New_Data,Key);
}
else
{
//Copy the remaining data and fill the rest
for(i = 0; i < 16; i++)
{
if(i < Incomplete_Block_Size)
{
New_Data[i] = Buffer->Data[((Number_of_Blocks -1)*16)+i];
}
if(i == Incomplete_Block_Size)
{
New_Data[i] = 0x80;
}
if(i > Incomplete_Block_Size)
{
New_Data[i] = 0x00;
}
}
//Preform XOR with Key 2
XOR(New_Data,Key_K2);
//Preform XOR with Old data
XOR(New_Data,Old_Data);
//Preform last AES routine
AES_Encrypt(New_Data,Key);
}
Message->MIC[0] = New_Data[0];
Message->MIC[1] = New_Data[1];
Message->MIC[2] = New_Data[2];
Message->MIC[3] = New_Data[3];
}
/*
*****************************************************************************************
* Description : Function used to generate keys for the MIC calculation
*
* Arguments : *Key pointer to key used for the MIC calculation
* *K1 pointer to Key1
* *K2 pointer ot Key2
*****************************************************************************************
*/
void Generate_Keys(unsigned char *Key, unsigned char *K1, unsigned char *K2)
{
unsigned char i;
unsigned char MSB_Key;
//Encrypt the zeros in K1 with the NwkSkey
AES_Encrypt(K1,Key);
//Create K1
//Check if MSB is 1
if((K1[0] & 0x80) == 0x80)
{
MSB_Key = 1;
}
else
{
MSB_Key = 0;
}
//Shift K1 one bit left
Shift_Left(K1);
//if MSB was 1
if(MSB_Key == 1)
{
K1[15] = K1[15] ^ 0x87;
}
//Copy K1 to K2
for( i = 0; i < 16; i++)
{
K2[i] = K1[i];
}
//Check if MSB is 1
if((K2[0] & 0x80) == 0x80)
{
MSB_Key = 1;
}
else
{
MSB_Key = 0;
}
//Shift K2 one bit left
Shift_Left(K2);
//Check if MSB was 1
if(MSB_Key == 1)
{
K2[15] = K2[15] ^ 0x87;
}
}
void Shift_Left(unsigned char *Data)
{
unsigned char i;
unsigned char Overflow = 0;
//unsigned char High_Byte, Low_Byte;
for(i = 0; i < 16; i++)
{
//Check for overflow on next byte except for the last byte
if(i < 15)
{
//Check if upper bit is one
if((Data[i+1] & 0x80) == 0x80)
{
Overflow = 1;
}
else
{
Overflow = 0;
}
}
else
{
Overflow = 0;
}
//Shift one left
Data[i] = (Data[i] << 1) + Overflow;
}
}
void XOR(unsigned char *New_Data,unsigned char *Old_Data)
{
unsigned char i;
for(i = 0; i < 16; i++)
{
New_Data[i] = New_Data[i] ^ Old_Data[i];
}
}