Przeglądaj źródła

Add handling for different encryption keys for the stations

There are eight key files in the common folder. The files are converted
to header files on compile time and the array of the keys is filled with
them. After determining the device id via the pin programming the
corresponding key is chosen.

Each of the keys is
mapped to one station id.
Bernd Gottschlag 5 lat temu
rodzic
commit
59c6b14d5b

+ 1
- 1
.gitignore Wyświetl plik

@@ -30,4 +30,4 @@ common/rust-protocol/target
30 30
 
31 31
 # Keys should never be in the repository
32 32
 common/display_key.txt
33
-common/weather_station_0_key.txt
33
+common/weather_station_*_key.txt

+ 3
- 0
weather-sensor/firmware/.gitignore Wyświetl plik

@@ -4,3 +4,6 @@ main
4 4
 
5 5
 
6 6
 *.swp
7
+
8
+# The generated header files for the keys stored in ../../common should never be in the repository
9
+#key_*.h

+ 48
- 10
weather-sensor/firmware/encryption.c Wyświetl plik

@@ -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;

+ 3
- 2
weather-sensor/firmware/encryption.h Wyświetl plik

@@ -2,7 +2,8 @@
2 2
 #define ENCRYPTION_H
3 3
 
4 4
 
5
-void Encrypt(uint32_t * data, uint8_t dataLength , uint64_t salt, const uint32_t key[4]);
6
-void Decrypt(uint32_t * data, uint8_t dataLength, uint64_t salt, const uint32_t key[4]);
5
+void Set_Encryption_Key(uint8_t sensorId);
6
+void Encrypt(uint32_t * data, uint8_t dataLength , uint64_t salt);
7
+void Decrypt(uint32_t * data, uint8_t dataLength, uint64_t salt);
7 8
 
8 9
 #endif

+ 3
- 0
weather-sensor/firmware/main.c Wyświetl plik

@@ -69,6 +69,9 @@ int main (void)
69 69
 	Configure_Pin_Programming_Pins();
70 70
 	ownId = Get_Own_Identifier();
71 71
 
72
+	/* Set the encryption key */
73
+	Set_Encryption_Key(ownId);
74
+
72 75
 	/* Initialize the SPI */
73 76
 	Initialize_SPI();
74 77
 

+ 4
- 1
weather-sensor/firmware/makefile Wyświetl plik

@@ -11,7 +11,7 @@ clean:
11 11
 flash: main.hex
12 12
 	sudo avrdude -c buspirate -b 115200 -P /dev/ttyUSB0 -p m88p -v -U flash:w:main.hex
13 13
 
14
-obj/%.o: %.c $(DEPS)
14
+obj/%.o: %.c $(DEPS) key_0.h key_1.h key_2.h key_3.h key_4.h key_5.h key_6.h key_7.h
15 15
 	avr-gcc -c $< -o $@ $(CFLAGS)
16 16
 
17 17
 obj/%.o: BME280_driver/%.c $(DEPS)
@@ -20,5 +20,8 @@ obj/%.o: BME280_driver/%.c $(DEPS)
20 20
 main: obj/main.o obj/spi.o obj/nrf24l01.o obj/bme280_interface.o obj/bme280.o obj/pin_programming.o obj/crc.o obj/encryption.o obj/radio.o
21 21
 	avr-gcc $^ -o $@ $(CFLAGS)
22 22
 
23
+key_%.h: ../../common/weather_station_%_key.txt
24
+	@cat $< | sed "s/^\[\(.*\)\]$$/\1/g" > $@
25
+
23 26
 main.hex: main
24 27
 	avr-objcopy -O ihex -R .eeprom main main.hex

+ 2
- 6
weather-sensor/firmware/radio.c Wyświetl plik

@@ -3,8 +3,6 @@
3 3
 #include "encryption.h"
4 4
 #include "crc.h"
5 5
 
6
-const uint8_t encryptionKey[16] = {0x9e, 0x37, 0x79, 0xb9, 0x9b, 0x97, 0x73, 0xe9, 0xb9, 0x79, 0x37, 0x9e, 0x6b, 0x69, 0x51, 0x56}; /* TODO: use exernal file with the keys */
7
-
8 6
 bool Send_Message(PACKET * packet, uint64_t * salt);
9 7
 
10 8
 bool Send_Get_Salt_Message(PACKET * packet, uint64_t * salt) //TODO: put into own file
@@ -48,8 +46,7 @@ bool Send_Message(PACKET * packet, uint64_t * salt)
48 46
 
49 47
 	Encrypt((uint32_t*) packet->payload.buffer,
50 48
 	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
51
-	        *salt,
52
-	        (uint32_t*) encryptionKey);
49
+	        *salt);
53 50
 
54 51
 	success = NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
55 52
 
@@ -70,8 +67,7 @@ bool Read_Salt_Message(PACKET * packet, uint64_t * salt)
70 67
 	baseStationSalt = packet->salt;
71 68
 	Decrypt((uint32_t*)packet->payload.buffer,
72 69
 	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc),
73
-	        baseStationSalt,
74
-	        (uint32_t*) encryptionKey);
70
+	        baseStationSalt);
75 71
 
76 72
 	crcRemainder = Calculate_Crc(packet->payload.buffer,
77 73
 	                             PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc));

Ładowanie…
Anuluj
Zapisz