add CRC in sender

This commit is contained in:
King Kévin 2015-11-10 22:31:59 +01:00
parent 1107ed1c02
commit 416f67d4ae
1 changed files with 18 additions and 2 deletions

View File

@ -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];