aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2015-11-10 12:28:36 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2015-11-10 12:28:36 +0100
commit3ac49be7fd8856ee7b3ef79db88706ff127b3d24 (patch)
tree3349547748ac5110fe13708e57eeceb522dbe17b
parentd0522117831fbb5a5d7440c06ada19e83a583e67 (diff)
add key and use AVR crypto lib. payload format changed
-rw-r--r--arduino_nano/main.c53
-rw-r--r--arduino_nano/main.h8
2 files changed, 37 insertions, 24 deletions
diff --git a/arduino_nano/main.c b/arduino_nano/main.c
index c8b14fa..36c3ca0 100644
--- a/arduino_nano/main.c
+++ b/arduino_nano/main.c
@@ -35,6 +35,11 @@
#include "usart.h" // basic USART functions
#include "nrf24.h" // nRF24L01 functions
+// AES encryption functions
+#include "aes_types.h"
+#include "aes128_enc.h"
+#include "aes_keyschedule.h"
+
/* variables */
bool query_pzem004 = false; // flag to query the PZEM-004 power meter
volatile uint8_t timer_seconds = 0; // how many seconds have passed
@@ -86,6 +91,8 @@ void io_init(void)
int main(void)
{
io_init(); // initialize IOs
+ uint8_t nrf24_payload[1+4*4] = {0}; // measurement values to be sent: ID byte + encrypted 4x4 float values (voltage, current, power, energy)
+ nrf24_payload[0] = conf.id; // set ID
//printf(PSTR("welcome to the spar counter power meter monitor\n"));
@@ -99,7 +106,18 @@ int main(void)
for (uint8_t i=0; i<sizeof(cmd); i++) {
usart_putchar_nonblocking(cmd[i],NULL);
}
-
+
+ /* start encryption */
+ aes128_ctx_t ctx; // the context where the round keys are stored
+ aes128_init(conf.key, &ctx); // generating the round keys from the 128 bit key
+ // XOR data with IV for AES CBC mode
+ for (uint8_t i=0; i<sizeof(nrf24_payload)-1 && i<sizeof(conf.iv); i++) {
+ nrf24_payload[i+1] ^= conf.iv[i];
+ }
+ aes128_enc(&nrf24_payload[1], &ctx); // encrypt data
+ memcpy(conf.iv,&nrf24_payload[1],sizeof(conf.iv)); // save IV back to continue CBC mode
+ nrf24_transmit(nrf24_payload,sizeof(nrf24_payload)); // transmit empty packet to sync CBC
+
/* start timer to periodically query the power meter */
timer_seconds = 0; // restart seconds counter
TCCR1B |= (1<<CS12)|(0<<CS11)|(0<<CS10); // set prescale to 256 (starting the timer)
@@ -194,28 +212,17 @@ int main(void)
if (send_values) { // send the stored values from the power meter using the nRF24L01
action = true; // an action is performed
send_values = false; // clear flag
- uint8_t message[3+6+6+6+6]; // create TLV based message
- uint8_t i = 0;
- message[i++] = 0; // type: message source
- message[i++] = 1; // length
- message[i++] = 1; // value: home 1
- message[i++] = 1; // type: voltage
- message[i++] = 4; // length
- memcpy(&message[i],&voltage,4); // value
- i += 4; // value
- message[i++] = 2; // type: current
- message[i++] = 4; // length
- memcpy(&message[i],&current,4); // value
- i += 4; // value
- message[i++] = 3; // type: power
- message[i++] = 4; // length
- memcpy(&message[i],&power,4); // value
- i += 4; // value
- message[i++] = 4; // type: energy
- message[i++] = 4; // length
- memcpy(&message[i],&energy,4); // value
- i += 4; // value
- nrf24_transmit(message,i);
+ memcpy(&nrf24_payload[1+0],&voltage,4); // populate voltage
+ memcpy(&nrf24_payload[1+4],&current,4); // populate current
+ memcpy(&nrf24_payload[1+8],&power,4); // populate power
+ memcpy(&nrf24_payload[1+12],&energy,4); // populate energy
+ // XOR data with last data for AES CBC mode
+ for (uint8_t i=0; i<sizeof(nrf24_payload)-1 && i<sizeof(conf.iv); i++) {
+ nrf24_payload[i+1] ^= conf.iv[i];
+ }
+ aes128_enc(&nrf24_payload[1], &ctx); // encrypt data
+ memcpy(conf.iv,&nrf24_payload[1],sizeof(conf.iv)); // save data back to continue CBC mode
+ nrf24_transmit(nrf24_payload,sizeof(nrf24_payload)); // transmit empty packet to sync CBC
}
if (nrf24_flag) {
action = true; // an action was performed
diff --git a/arduino_nano/main.h b/arduino_nano/main.h
index 0b91aa5..1c832f9 100644
--- a/arduino_nano/main.h
+++ b/arduino_nano/main.h
@@ -20,9 +20,15 @@
/* spark counter configuration */
struct configuration {
+ uint8_t id; // the identity of the node
uint8_t rx_addr[5]; // nRF24 receiving address
uint8_t tx_addr[5]; // nRF24 transmit address
uint8_t channel; // nRF24 channel
uint8_t request_period; // how many seconds between power meter requests
+ uint8_t key[16]; // the AES 128 bits key
+ uint8_t iv[16]; // the initialisation vector for CBC encryption
+};
+struct configuration conf = {1,{1,'h','o','m','e'},{0,'h','o','m','e'},42,5,
+ {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff},
+ {0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00}
};
-struct configuration conf = {{1,'h','o','m','e'},{0,'h','o','m','e'},42,5};