aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2015-11-10 22:31:59 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2015-11-10 22:31:59 +0100
commit416f67d4aec97954cc16b4e31468b810aeef9a19 (patch)
tree3e042a6673c7572b559e85e1508e5b53e34f8b5c
parent1107ed1c026856e3ba388b574f5be93aa663d272 (diff)
add CRC in sender
-rw-r--r--arduino_nano/main.c20
1 files changed, 18 insertions, 2 deletions
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 <avr/wdt.h> // Watchdog timer handling
#include <avr/pgmspace.h> // Program Space Utilities
#include <avr/sleep.h> // Power Management and Sleep Modes
+#include <util/crc16.h> // 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<sizeof(nrf24_payload)-1 && i<sizeof(conf.iv); i++) {
nrf24_payload[i+1] ^= conf.iv[i];
@@ -216,6 +225,13 @@ int main(void)
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
+ // 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 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];