aboutsummaryrefslogtreecommitdiff
path: root/arduino_nano/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'arduino_nano/main.c')
-rw-r--r--arduino_nano/main.c53
1 files changed, 30 insertions, 23 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