|
|
@@ -2,11 +2,49 @@
|
|
2
|
2
|
|
|
3
|
3
|
#define LENGTH_OF_BLOCK 8
|
|
4
|
4
|
|
|
5
|
|
-void xxtea_Encrypt(uint32_t * data, uint8_t dataLength, const uint32_t key[4]);
|
|
6
|
|
-void xxtea_Decrypt(uint32_t * data, uint8_t dataLength, const uint32_t key[4]);
|
|
|
5
|
+
|
|
|
6
|
+#define NUMBER_OF_KEYS 8
|
|
|
7
|
+#define KEY_LENGTH 16
|
|
|
8
|
+
|
|
|
9
|
+const uint8_t encryptionKeys[NUMBER_OF_KEYS][KEY_LENGTH] = {
|
|
|
10
|
+ {
|
|
|
11
|
+ #include "key_0.h"
|
|
|
12
|
+ },
|
|
|
13
|
+ {
|
|
|
14
|
+ #include "key_1.h"
|
|
|
15
|
+ },
|
|
|
16
|
+ {
|
|
|
17
|
+ #include "key_2.h"
|
|
|
18
|
+ },
|
|
|
19
|
+ {
|
|
|
20
|
+ #include "key_3.h"
|
|
|
21
|
+ },
|
|
|
22
|
+ {
|
|
|
23
|
+ #include "key_4.h"
|
|
|
24
|
+ },
|
|
|
25
|
+ {
|
|
|
26
|
+ #include "key_5.h"
|
|
|
27
|
+ },
|
|
|
28
|
+ {
|
|
|
29
|
+ #include "key_6.h"
|
|
|
30
|
+ },
|
|
|
31
|
+ {
|
|
|
32
|
+ #include "key_7.h"
|
|
|
33
|
+ }
|
|
|
34
|
+};
|
|
|
35
|
+
|
|
|
36
|
+const uint8_t * key;
|
|
|
37
|
+
|
|
|
38
|
+void xxtea_Encrypt(uint32_t * data, uint8_t dataLength);
|
|
|
39
|
+void xxtea_Decrypt(uint32_t * data, uint8_t dataLength);
|
|
|
40
|
+
|
|
|
41
|
+void Set_Encryption_Key(uint8_t sensorId)
|
|
|
42
|
+{
|
|
|
43
|
+ key = encryptionKeys[sensorId & 0xF];
|
|
|
44
|
+}
|
|
7
|
45
|
|
|
8
|
46
|
/* The data packets are encrypted using the xxtea algorithm. */
|
|
9
|
|
-void Encrypt(uint32_t * data, uint8_t dataLength, uint64_t salt, const uint32_t key[4])
|
|
|
47
|
+void Encrypt(uint32_t * data, uint8_t dataLength, uint64_t salt)
|
|
10
|
48
|
{
|
|
11
|
49
|
/* This function assumes that the dataLength is a multiple of the length of the
|
|
12
|
50
|
* salt (8)
|
|
|
@@ -29,7 +67,7 @@ void Encrypt(uint32_t * data, uint8_t dataLength, uint64_t salt, const uint32_t
|
|
29
|
67
|
*((uint64_t*) currentPlaintextBlock) ^= *((uint64_t*) previousCipherBlock);
|
|
30
|
68
|
|
|
31
|
69
|
/* Encrypt the block */
|
|
32
|
|
- xxtea_Encrypt((uint32_t*) currentPlaintextBlock, LENGTH_OF_BLOCK, key);
|
|
|
70
|
+ xxtea_Encrypt((uint32_t*) currentPlaintextBlock, LENGTH_OF_BLOCK);
|
|
33
|
71
|
|
|
34
|
72
|
/* Setup for next block */
|
|
35
|
73
|
previousCipherBlock = currentPlaintextBlock;
|
|
|
@@ -39,7 +77,7 @@ void Encrypt(uint32_t * data, uint8_t dataLength, uint64_t salt, const uint32_t
|
|
39
|
77
|
}
|
|
40
|
78
|
|
|
41
|
79
|
|
|
42
|
|
-void Decrypt(uint32_t * data, uint8_t dataLength, uint64_t salt, const uint32_t key[4])
|
|
|
80
|
+void Decrypt(uint32_t * data, uint8_t dataLength, uint64_t salt)
|
|
43
|
81
|
{
|
|
44
|
82
|
/* This function assumes that the dataLength is a multiple of the length of the
|
|
45
|
83
|
* salt (8)
|
|
|
@@ -58,7 +96,7 @@ void Decrypt(uint32_t * data, uint8_t dataLength, uint64_t salt, const uint32_t
|
|
58
|
96
|
for (i = dataLength/LENGTH_OF_BLOCK; i > 0; i--)
|
|
59
|
97
|
{
|
|
60
|
98
|
/* Decrypt the block */
|
|
61
|
|
- xxtea_Decrypt((uint32_t*) currentCipherBlock, LENGTH_OF_BLOCK, key);
|
|
|
99
|
+ xxtea_Decrypt((uint32_t*) currentCipherBlock, LENGTH_OF_BLOCK);
|
|
62
|
100
|
|
|
63
|
101
|
/* XOR of the decrypted block with cipher block in front of it */
|
|
64
|
102
|
*((uint64_t*) currentCipherBlock) ^= *((uint64_t*) previousCipherBlock);
|
|
|
@@ -76,7 +114,7 @@ void Decrypt(uint32_t * data, uint8_t dataLength, uint64_t salt, const uint32_t
|
|
76
|
114
|
}
|
|
77
|
115
|
}
|
|
78
|
116
|
|
|
79
|
|
-void xxtea_Encrypt(uint32_t * data, uint8_t dataLength, const uint32_t key[4])
|
|
|
117
|
+void xxtea_Encrypt(uint32_t * data, uint8_t dataLength)
|
|
80
|
118
|
{
|
|
81
|
119
|
uint32_t sum = 0, z, y, e;
|
|
82
|
120
|
uint8_t i = 6 + 52/(dataLength/4), r;
|
|
|
@@ -90,13 +128,13 @@ void xxtea_Encrypt(uint32_t * data, uint8_t dataLength, const uint32_t key[4])
|
|
90
|
128
|
for (r = 0; r <= n; r++) {
|
|
91
|
129
|
// round
|
|
92
|
130
|
y = data[(r+1) % (n + 1)]; // right neighbour
|
|
93
|
|
- data[r] += ((z>>5 ^ y<<2) + (y>>3 ^ z<<4)) ^ ((sum^y) + (key[(r^e) & 3] ^ z));
|
|
|
131
|
+ data[r] += ((z>>5 ^ y<<2) + (y>>3 ^ z<<4)) ^ ((sum^y) + (((uint32_t*) key)[(r^e) & 3] ^ z));
|
|
94
|
132
|
z = data[r]; // left neighbour for the next round
|
|
95
|
133
|
}
|
|
96
|
134
|
} while (--i);
|
|
97
|
135
|
}
|
|
98
|
136
|
|
|
99
|
|
-void xxtea_Decrypt(uint32_t * data, uint8_t dataLength, const uint32_t key[4])
|
|
|
137
|
+void xxtea_Decrypt(uint32_t * data, uint8_t dataLength)
|
|
100
|
138
|
{
|
|
101
|
139
|
uint32_t sum, z, y, e;
|
|
102
|
140
|
int16_t i = 6 + 52/(dataLength/4), r;
|
|
|
@@ -110,7 +148,7 @@ void xxtea_Decrypt(uint32_t * data, uint8_t dataLength, const uint32_t key[4])
|
|
110
|
148
|
for (r = n-1; r >= 0; --r) {
|
|
111
|
149
|
// round
|
|
112
|
150
|
z = data[(r+n-1) % n];
|
|
113
|
|
- data[r] -= ((z>>5 ^ y<<2) + (y>>3 ^ z<<4)) ^ ((sum^y) + (key[(r^e) & 3] ^ z));
|
|
|
151
|
+ data[r] -= ((z>>5 ^ y<<2) + (y>>3 ^ z<<4)) ^ ((sum^y) + (((uint32_t*) key)[(r^e) & 3] ^ z));
|
|
114
|
152
|
y = data[r];
|
|
115
|
153
|
}
|
|
116
|
154
|
sum -= 0x9e3779b9;
|