diff --git a/arduino_nano/main.c b/arduino_nano/main.c index 280834f..19a471c 100644 --- a/arduino_nano/main.c +++ b/arduino_nano/main.c @@ -30,6 +30,7 @@ #include // Watchdog timer handling #include // Program Space Utilities #include // Power Management and Sleep Modes +#include // CRC Computations #include "main.h" // main definitions #include "usart.h" // basic USART functions @@ -91,8 +92,7 @@ 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 + uint8_t nrf24_payload[1+4*4+1] = {0}; // measurement values to be sent: ID byte + encrypted 4x4 float values (voltage, current, power, energy) + 1 CRC8 Mixim/Dallas/iButton/1Wire of the plain text (to know if the key/IV is correct) //printf(PSTR("welcome to the spar counter power meter monitor\n")); @@ -110,6 +110,15 @@ int main(void) /* 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 + // set data to invalid + memset(&nrf24_payload[1],0xff,sizeof(conf.iv)); + // set ID + nrf24_payload[0] = conf.id; + // calculate CRC over values + nrf24_payload[17] = 0; + for (uint8_t i = 0; i < sizeof(conf.iv); i++) { + nrf24_payload[17] = _crc_ibutton_update(nrf24_payload[17], nrf24_payload[1+i]); + } // XOR data with IV for AES CBC mode for (uint8_t i=0; i