forked from novag/SlimLoRa
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncryption.cpp
More file actions
executable file
·498 lines (412 loc) · 11.5 KB
/
Encryption.cpp
File metadata and controls
executable file
·498 lines (412 loc) · 11.5 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <Arduino.h>
#include <string.h>
#include "SlimLoRa.h"
/**
* Encrypts/Decrypts the payload of a LoRaWAN data message.
*
* Decryption is performed the same way as encryption as the network server
* conveniently uses AES decryption for encrypting download payloads.
*
* @param payload Pointer to the data to encrypt or decrypt Address of the register to be written.
* @param payload_length Number of bytes to process.
* @param frame_counter Frame counter for upstream frames.
* @param direction Direction of message.
*/
void SlimLoRa::EncryptPayload(uint8_t *payload, uint8_t payload_length, uint32_t frame_counter, uint8_t direction) {
uint8_t block_count = 0;
uint8_t incomplete_block_size = 0;
uint8_t block_a[16];
#if LORAWAN_OTAA_ENABLED
uint8_t dev_addr[4], app_s_key[16]; //, nwk_s_key[16];
GetDevAddr(dev_addr);
// special case. We have to use NwkSKey for downlink in port 0.
if ( downPort == 0 && direction == LORAWAN_DIRECTION_DOWN ) {
GetNwkSEncKey(app_s_key);
} else {
GetAppSKey(app_s_key);
}
#endif // LORAWAN_OTAA_ENABLED
// Calculate number of blocks
block_count = payload_length / 16;
incomplete_block_size = payload_length % 16;
if (incomplete_block_size != 0) {
block_count++;
}
for (uint8_t i = 1; i <= block_count; i++) {
block_a[0] = 0x01;
block_a[1] = 0x00;
block_a[2] = 0x00;
block_a[3] = 0x00;
block_a[4] = 0x00;
block_a[5] = direction;
#if LORAWAN_OTAA_ENABLED
block_a[6] = dev_addr[3];
block_a[7] = dev_addr[2];
block_a[8] = dev_addr[1];
block_a[9] = dev_addr[0];
#else
block_a[6] = DevAddr[3];
block_a[7] = DevAddr[2];
block_a[8] = DevAddr[1];
block_a[9] = DevAddr[0];
#endif // LORAWAN_OTAA_ENABLED
block_a[10] = frame_counter & 0xFF;
block_a[11] = frame_counter >> 8;
block_a[12] = frame_counter >> 16; // Frame counter upper bytes
block_a[13] = frame_counter >> 24;
block_a[14] = 0x00;
block_a[15] = i;
// Calculate S
#if LORAWAN_OTAA_ENABLED
AesEncrypt(app_s_key, block_a);
#else
AesEncrypt(AppSKey, block_a);
#endif // LORAWAN_OTAA_ENABLED
// Check for last block
if (i != block_count) {
for (uint8_t j = 0; j < 16; j++) {
*payload ^= block_a[j];
payload++;
}
} else {
if (incomplete_block_size == 0) {
incomplete_block_size = 16;
}
for (uint8_t j = 0; j < incomplete_block_size; j++) {
*payload ^= block_a[j];
payload++;
}
}
}
}
/**
* Calculates an AES MIC of given data using a key.
*
* @param key 16-byte long key.
* @param data Pointer to the data to process.
* @param initial_block Pointer to an initial 16-byte block.
* @param final_mic 4-byte array for final MIC output.
* @param data_length Number of bytes to process.
*/
void SlimLoRa::CalculateMic(const uint8_t *key, uint8_t *data, uint8_t *initial_block, uint8_t *final_mic, uint8_t data_length) {
uint8_t key1[16] = {0};
uint8_t key2[16] = {0};
uint8_t old_data[16] = {0};
uint8_t new_data[16] = {0};
uint8_t block_count = 0;
uint8_t incomplete_block_size = 0;
uint8_t block_counter = 1;
// Calculate number of blocks and blocksize of last block
block_count = data_length / 16;
incomplete_block_size = data_length % 16;
if (incomplete_block_size != 0) {
block_count++;
}
GenerateKeys(key, key1, key2);
// Copy initial block to old_data if present
if (initial_block != NULL) {
for (uint8_t i = 0; i < 16; i++) {
old_data[i] = *initial_block;
initial_block++;
}
AesEncrypt(key, old_data);
}
// Calculate first block_count - 1 blocks
while (block_counter < block_count) {
for (uint8_t i = 0; i < 16; i++) {
new_data[i] = *data;
data++;
}
XorData(new_data, old_data);
AesEncrypt(key, new_data);
for (uint8_t i = 0; i < 16; i++) {
old_data[i] = new_data[i];
}
block_counter++;
}
// Pad and calculate last block
if (incomplete_block_size == 0) {
for (uint8_t i = 0; i < 16; i++) {
new_data[i] = *data;
data++;
}
XorData(new_data, key1);
XorData(new_data, old_data);
AesEncrypt(key, new_data);
} else {
for (uint8_t i = 0; i < 16; i++) {
if (i < incomplete_block_size) {
new_data[i] = *data;
data++;
}
if (i == incomplete_block_size) {
new_data[i] = 0x80;
}
if (i > incomplete_block_size) {
new_data[i] = 0x00;
}
}
XorData(new_data, key2);
XorData(new_data, old_data);
AesEncrypt(key, new_data);
}
final_mic[0] = new_data[0];
final_mic[1] = new_data[1];
final_mic[2] = new_data[2];
final_mic[3] = new_data[3];
// Original
//pseudo_byte_ = final_mic[3];
// clv
//pseudo_byte_ = micros(); pseudo_byte_ = pseudo_byte_ >> 5; // 0 - 7
pseudo_byte_ = micros() >> 8 & ( LORAWAN_UPLINK_CHANNEL_COUNT - 1 ) ; // [7] for 8 channels [15] for 16 channels;
}
/**
* Calculates an AES MIC of a LoRaWAN message.
*
* @param data Pointer to the data to process.
* @param final_mic 4-byte array for final MIC output.
* @param data_length Number of bytes to process.
* @param frame_counter Frame counter for uplink frames.
* @param direction Number of message.
*/
void SlimLoRa::CalculateMessageMic(uint8_t *data, uint8_t *final_mic, uint8_t data_length, uint32_t frame_counter, uint8_t direction) {
uint8_t block_b[16];
#if LORAWAN_OTAA_ENABLED
uint8_t dev_addr[4], nwk_s_key[16];
GetDevAddr(dev_addr);
GetNwkSEncKey(nwk_s_key);
#endif // LORAWAN_OTAA_ENABLED
block_b[0] = 0x49;
block_b[1] = 0x00;
block_b[2] = 0x00;
block_b[3] = 0x00;
block_b[4] = 0x00;
block_b[5] = direction;
#if LORAWAN_OTAA_ENABLED
block_b[6] = dev_addr[3];
block_b[7] = dev_addr[2];
block_b[8] = dev_addr[1];
block_b[9] = dev_addr[0];
#else
block_b[6] = DevAddr[3];
block_b[7] = DevAddr[2];
block_b[8] = DevAddr[1];
block_b[9] = DevAddr[0];
#endif // LORAWAN_OTAA_ENABLED
block_b[10] = frame_counter & 0xFF;
block_b[11] = (frame_counter >> 8) & 0xFF;
block_b[12] = (frame_counter >> 16) & 0xFF; // Frame counter upper bytes
block_b[13] = (frame_counter >> 24) & 0xFF;
block_b[14] = 0x00;
block_b[15] = data_length;
#if LORAWAN_OTAA_ENABLED
CalculateMic(nwk_s_key, data, block_b, final_mic, data_length);
#else
CalculateMic(NwkSKey, data, block_b, final_mic, data_length);
#endif // LORAWAN_OTAA_ENABLED
}
/**
* Generate keys for MIC calculation.
*
* @param key .
* @param key1 Pointer to key 1.
* @param key2 Pointer to key 2.
*/
void SlimLoRa::GenerateKeys(const uint8_t *key, uint8_t *key1, uint8_t *key2) {
uint8_t msb_key;
// Encrypt the zeros in key1 with the NwkSkey
AesEncrypt(key, key1);
// Create key1
// Check if MSB is 1
if ((key1[0] & 0x80) == 0x80) {
msb_key = 1;
} else {
msb_key = 0;
}
// Shift key1 one bit left
ShiftLeftData(key1);
// if MSB was 1
if (msb_key == 1) {
key1[15] = key1[15] ^ 0x87;
}
// Copy key1 to key2
for (uint8_t i = 0; i < 16; i++) {
key2[i] = key1[i];
}
// Check if MSB is 1
if ((key2[0] & 0x80) == 0x80) {
msb_key = 1;
} else {
msb_key = 0;
}
// Shift key2 one bit left
ShiftLeftData(key2);
// Check if MSB was 1
if (msb_key == 1) {
key2[15] = key2[15] ^ 0x87;
}
}
void SlimLoRa::ShiftLeftData(uint8_t *data) {
uint8_t overflow = 0;
for (uint8_t 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 SlimLoRa::XorData(uint8_t *new_data, uint8_t *old_data) {
for (uint8_t i = 0; i < 16; i++) {
new_data[i] = new_data[i] ^ old_data[i];
}
}
/**
* AES encrypts data with 128 bit key.
*
* @param key 128 bit key.
* @param data Plaintext to encrypt.
*/
void SlimLoRa::AesEncrypt(const uint8_t *key, uint8_t *data) {
uint8_t round;
uint8_t round_key[16];
uint8_t state[4][4];
// Copy input to state arry
for (uint8_t column = 0; column < 4; column++) {
for (uint8_t row = 0; row < 4; row++) {
state[row][column] = data[row + (column << 2)];
}
}
// Copy key to round key
memcpy(&round_key[0], &key[0], 16);
// Add round key
AesAddRoundKey(round_key, state);
// Preform 9 full rounds with mixed collums
for (round = 1; round < 10; round++) {
// Perform Byte substitution with S table
for (uint8_t column = 0; column < 4; column++) {
for (uint8_t row = 0; row < 4; row++) {
state[row][column] = AesSubByte(state[row][column]);
}
}
// Perform row Shift
AesShiftRows(state);
// Mix Collums
AesMixCollums(state);
// Calculate new round key
AesCalculateRoundKey(round, round_key);
// Add the round key to the round_key
AesAddRoundKey(round_key, state);
}
// Perform Byte substitution with S table whitout mix collums
for (uint8_t column = 0; column < 4; column++) {
for (uint8_t row = 0; row < 4; row++) {
state[row][column] = AesSubByte(state[row][column]);
}
}
// Shift rows
AesShiftRows(state);
// Calculate new round key
AesCalculateRoundKey(round, round_key);
// Add round key
AesAddRoundKey(round_key, state);
// Copy the state into the data array
for (uint8_t column = 0; column < 4; column++) {
for (uint8_t row = 0; row < 4; row++) {
data[row + (column << 2)] = state[row][column];
}
}
}
void SlimLoRa::AesAddRoundKey(uint8_t *round_key, uint8_t (*state)[4]) {
for (uint8_t column = 0; column < 4; column++) {
for (uint8_t row = 0; row < 4; row++) {
state[row][column] ^= round_key[row + (column << 2)];
}
}
}
uint8_t SlimLoRa::AesSubByte(uint8_t byte) {
// uint8_t S_Row, S_Collum;
// uint8_t S_Byte;
// S_Row = ((byte >> 4) & 0x0F);
// S_Collum = ((byte >> 0) & 0x0F);
// S_Byte = kSTable[S_Row][S_Collum];
// return kSTable[((byte >> 4) & 0x0F)][((byte >> 0) & 0x0F)]; // original
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
return pgm_read_byte(&(kSTable[((byte >> 4) & 0x0F)][((byte >> 0) & 0x0F)]));
#else
return kSTable[((byte >> 4) & 0x0F)][((byte >> 0) & 0x0F)];
#endif
}
void SlimLoRa::AesShiftRows(uint8_t (*state)[4]) {
uint8_t buffer;
// Store firt byte in buffer
buffer = state[1][0];
// Shift all bytes
state[1][0] = state[1][1];
state[1][1] = state[1][2];
state[1][2] = state[1][3];
state[1][3] = buffer;
buffer = state[2][0];
state[2][0] = state[2][2];
state[2][2] = buffer;
buffer = state[2][1];
state[2][1] = state[2][3];
state[2][3] = buffer;
buffer = state[3][3];
state[3][3] = state[3][2];
state[3][2] = state[3][1];
state[3][1] = state[3][0];
state[3][0] = buffer;
}
void SlimLoRa::AesMixCollums(uint8_t (*state)[4]) {
uint8_t a[4], b[4];
for (uint8_t column = 0; column < 4; column++) {
for (uint8_t row = 0; row < 4; row++) {
a[row] = state[row][column];
b[row] = (state[row][column] << 1);
if ((state[row][column] & 0x80) == 0x80) {
b[row] ^= 0x1B;
}
}
state[0][column] = b[0] ^ a[1] ^ b[1] ^ a[2] ^ a[3];
state[1][column] = a[0] ^ b[1] ^ a[2] ^ b[2] ^ a[3];
state[2][column] = a[0] ^ a[1] ^ b[2] ^ a[3] ^ b[3];
state[3][column] = a[0] ^ b[0] ^ a[1] ^ a[2] ^ b[3];
}
}
void SlimLoRa::AesCalculateRoundKey(uint8_t round, uint8_t *round_key) {
uint8_t tmp[4];
// Calculate rcon
uint8_t rcon = 0x01;
while (round != 1) {
uint8_t b = rcon & 0x80;
rcon = rcon << 1;
if (b == 0x80) {
rcon ^= 0x1b;
}
round--;
}
// Calculate first tmp
// Copy laste byte from previous key and subsitute the byte, but shift the array contents around by 1.
tmp[0] = AesSubByte(round_key[12 + 1]);
tmp[1] = AesSubByte(round_key[12 + 2]);
tmp[2] = AesSubByte(round_key[12 + 3]);
tmp[3] = AesSubByte(round_key[12 + 0]);
// XOR with rcon
tmp[0] ^= rcon;
// Calculate new key
for (uint8_t i = 0; i < 4; i++) {
for (uint8_t j = 0; j < 4; j++) {
round_key[j + (i << 2)] ^= tmp[j];
tmp[j] = round_key[j + (i << 2)];
}
}
}