Browse Source

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 years ago
parent
commit
59c6b14d5b

+ 1
- 1
.gitignore View File

30
 
30
 
31
 # Keys should never be in the repository
31
 # Keys should never be in the repository
32
 common/display_key.txt
32
 common/display_key.txt
33
-common/weather_station_0_key.txt
33
+common/weather_station_*_key.txt

+ 3
- 0
weather-sensor/firmware/.gitignore View File

4
 
4
 
5
 
5
 
6
 *.swp
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 View File

2
 
2
 
3
 #define LENGTH_OF_BLOCK 8
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
 /* The data packets are encrypted using the xxtea algorithm. */
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
 	/* This function assumes that the dataLength is a multiple of the length of the
49
 	/* This function assumes that the dataLength is a multiple of the length of the
12
 	 * salt (8)
50
 	 * salt (8)
29
 		*((uint64_t*) currentPlaintextBlock) ^= *((uint64_t*) previousCipherBlock);
67
 		*((uint64_t*) currentPlaintextBlock) ^= *((uint64_t*) previousCipherBlock);
30
 
68
 
31
 		/* Encrypt the block */
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
 		/* Setup for next block */
72
 		/* Setup for next block */
35
 		previousCipherBlock = currentPlaintextBlock;
73
 		previousCipherBlock = currentPlaintextBlock;
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
 	/* This function assumes that the dataLength is a multiple of the length of the
82
 	/* This function assumes that the dataLength is a multiple of the length of the
45
 	 * salt (8)
83
 	 * salt (8)
58
 	for (i = dataLength/LENGTH_OF_BLOCK; i > 0; i--)
96
 	for (i = dataLength/LENGTH_OF_BLOCK; i > 0; i--)
59
 	{
97
 	{
60
 		/* Decrypt the block */
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
 		/* XOR of the decrypted block with cipher block in front of it */
101
 		/* XOR of the decrypted block with cipher block in front of it */
64
 		*((uint64_t*) currentCipherBlock) ^= *((uint64_t*) previousCipherBlock);
102
 		*((uint64_t*) currentCipherBlock) ^= *((uint64_t*) previousCipherBlock);
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
     uint32_t sum = 0, z, y, e;
119
     uint32_t sum = 0, z, y, e;
82
     uint8_t i = 6 + 52/(dataLength/4), r;
120
     uint8_t i = 6 + 52/(dataLength/4), r;
90
         for (r = 0; r <= n; r++) {
128
         for (r = 0; r <= n; r++) {
91
             // round
129
             // round
92
             y = data[(r+1) % (n + 1)]; // right neighbour
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
             z = data[r]; // left neighbour for the next round
132
             z = data[r]; // left neighbour for the next round
95
         }
133
         }
96
     } while (--i);
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
     uint32_t sum, z, y, e;
139
     uint32_t sum, z, y, e;
102
     int16_t i = 6 + 52/(dataLength/4), r;
140
     int16_t i = 6 + 52/(dataLength/4), r;
110
         for (r = n-1; r >= 0; --r) {
148
         for (r = n-1; r >= 0; --r) {
111
             // round
149
             // round
112
             z = data[(r+n-1) % n];
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
             y = data[r];
152
             y = data[r];
115
         }
153
         }
116
         sum -= 0x9e3779b9;
154
         sum -= 0x9e3779b9;

+ 3
- 2
weather-sensor/firmware/encryption.h View File

2
 #define ENCRYPTION_H
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
 #endif
9
 #endif

+ 3
- 0
weather-sensor/firmware/main.c View File

69
 	Configure_Pin_Programming_Pins();
69
 	Configure_Pin_Programming_Pins();
70
 	ownId = Get_Own_Identifier();
70
 	ownId = Get_Own_Identifier();
71
 
71
 
72
+	/* Set the encryption key */
73
+	Set_Encryption_Key(ownId);
74
+
72
 	/* Initialize the SPI */
75
 	/* Initialize the SPI */
73
 	Initialize_SPI();
76
 	Initialize_SPI();
74
 
77
 

+ 4
- 1
weather-sensor/firmware/makefile View File

11
 flash: main.hex
11
 flash: main.hex
12
 	sudo avrdude -c buspirate -b 115200 -P /dev/ttyUSB0 -p m88p -v -U flash:w:main.hex
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
 	avr-gcc -c $< -o $@ $(CFLAGS)
15
 	avr-gcc -c $< -o $@ $(CFLAGS)
16
 
16
 
17
 obj/%.o: BME280_driver/%.c $(DEPS)
17
 obj/%.o: BME280_driver/%.c $(DEPS)
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
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
 	avr-gcc $^ -o $@ $(CFLAGS)
21
 	avr-gcc $^ -o $@ $(CFLAGS)
22
 
22
 
23
+key_%.h: ../../common/weather_station_%_key.txt
24
+	@cat $< | sed "s/^\[\(.*\)\]$$/\1/g" > $@
25
+
23
 main.hex: main
26
 main.hex: main
24
 	avr-objcopy -O ihex -R .eeprom main main.hex
27
 	avr-objcopy -O ihex -R .eeprom main main.hex

+ 2
- 6
weather-sensor/firmware/radio.c View File

3
 #include "encryption.h"
3
 #include "encryption.h"
4
 #include "crc.h"
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
 bool Send_Message(PACKET * packet, uint64_t * salt);
6
 bool Send_Message(PACKET * packet, uint64_t * salt);
9
 
7
 
10
 bool Send_Get_Salt_Message(PACKET * packet, uint64_t * salt) //TODO: put into own file
8
 bool Send_Get_Salt_Message(PACKET * packet, uint64_t * salt) //TODO: put into own file
48
 
46
 
49
 	Encrypt((uint32_t*) packet->payload.buffer,
47
 	Encrypt((uint32_t*) packet->payload.buffer,
50
 	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
48
 	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
51
-	        *salt,
52
-	        (uint32_t*) encryptionKey);
49
+	        *salt);
53
 
50
 
54
 	success = NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
51
 	success = NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
55
 
52
 
70
 	baseStationSalt = packet->salt;
67
 	baseStationSalt = packet->salt;
71
 	Decrypt((uint32_t*)packet->payload.buffer,
68
 	Decrypt((uint32_t*)packet->payload.buffer,
72
 	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc),
69
 	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc),
73
-	        baseStationSalt,
74
-	        (uint32_t*) encryptionKey);
70
+	        baseStationSalt);
75
 
71
 
76
 	crcRemainder = Calculate_Crc(packet->payload.buffer,
72
 	crcRemainder = Calculate_Crc(packet->payload.buffer,
77
 	                             PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc));
73
 	                             PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(packet->crc));

Loading…
Cancel
Save