diff --git a/.gitignore b/.gitignore index 02288c4..41cab91 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ cybergarden-sensor/.vscode +cybergarden-aggregator/.vscode diff --git a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp index 8c10285..5e6818f 100644 --- a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp @@ -8,7 +8,33 @@ #define ECC_LENGTH 4 #define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH) -bool TransmissionModule::receive(uint8_t &sensor_id, MeasureData &data) { +void TransmissionModule::decrypt(uint8_t *data, size_t data_size, uint8_t *output_data) +{ + size_t posn, len, inc = 5; + cipher->clear(); + cipher->setKey(encryptionKey, key_size); + cipher->setIV(iv, iv_size); + memset(buffer, 0xBA, sizeof(buffer)); + + for (posn = 0; posn < auth_size; posn += inc) { + len = auth_size - posn; + if (len > inc) + len = inc; + cipher->addAuthData(authdata + posn, len); + } + + for (posn = 0; posn < data_size; posn += inc) { + len = data_size - posn; + if (len > inc) + len = inc; + cipher->decrypt(buffer + posn, data + posn, len); + } + + memcpy(output_data, buffer, data_size); +} + +bool TransmissionModule::receive(uint8_t &sensor_id, MeasureData &data) +{ uint8_t buf[TOTAL_LENGTH]; if (!transmitterModule->receive(buf, TOTAL_LENGTH)) { return false; diff --git a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h index a782453..92efcf5 100644 --- a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h +++ b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h @@ -1,17 +1,39 @@ +#ifndef KEMPT_KINKAJOU_TRANSMISSIONMODULE_H +#define KEMPT_KINKAJOU_TRANSMISSIONMODULE_H + #include +#include +#include #include "MeasureData.h" #include "TransmitterModule.h" #include "ReedSolomonModule.h" -#ifndef KEMPT_KINKAJOU_TRANSMISSIONMODULE_H -#define KEMPT_KINKAJOU_TRANSMISSIONMODULE_H +#define MAX_PLAINTEXT_LEN 265 class TransmissionModule { private: TransmitterModule * transmitterModule; ReedSolomonModule * reedSolomonModule; + ChaChaPoly* cipher; + uint8_t encryptionKey[16] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f}; + uint8_t authdata[16] = {0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, + 0xc4, 0xc5, 0xc6, 0xc7}; + + uint8_t iv[16] = {0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, + 0x44, 0x45, 0x46, 0x47}; + uint8_t buffer[MAX_PLAINTEXT_LEN]; + size_t key_size = 16; + size_t auth_size = 12; + // size_t data_size = 4; + size_t iv_size = 12; public: - TransmissionModule(TransmitterModule &transmitterModule, ReedSolomonModule &reedSolomonModule) : transmitterModule(&transmitterModule), reedSolomonModule(&reedSolomonModule) {} + TransmissionModule(TransmitterModule &transmitterModule, ReedSolomonModule &reedSolomonModule) : + transmitterModule(&transmitterModule), + reedSolomonModule(&reedSolomonModule) { + cipher = new ChaChaPoly(); + } + void decrypt(uint8_t * data, size_t data_size, uint8_t* output_data); bool receive(uint8_t &sendor_id, MeasureData &data); }; diff --git a/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h b/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h index 6f61ef1..b57f4b6 100644 --- a/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h +++ b/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h @@ -1,12 +1,13 @@ // // Created by o.likhogub on 10/7/2023. // +#ifndef KEMPT_KINKAJOU_TRANSMITTERMODULE_H +#define KEMPT_KINKAJOU_TRANSMITTERMODULE_H #include "Arduino.h" #include #include -#ifndef KEMPT_KINKAJOU_TRANSMITTERMODULE_H -#define KEMPT_KINKAJOU_TRANSMITTERMODULE_H + class TransmitterModule { private: diff --git a/cybergarden-aggregator/lib/UuidModule/Uuid.cpp b/cybergarden-aggregator/lib/UuidModule/Uuid.cpp index 9dd81a9..f25836a 100644 --- a/cybergarden-aggregator/lib/UuidModule/Uuid.cpp +++ b/cybergarden-aggregator/lib/UuidModule/Uuid.cpp @@ -8,7 +8,7 @@ // https://en.wikipedia.org/wiki/Universally_unique_identifier -#include "UUID.h" +#include "Uuid.h" UUID::UUID() diff --git a/cybergarden-aggregator/platformio.ini b/cybergarden-aggregator/platformio.ini index 9f9afbf..9b58c2e 100644 --- a/cybergarden-aggregator/platformio.ini +++ b/cybergarden-aggregator/platformio.ini @@ -16,3 +16,4 @@ monitor_speed = 115200 lib_deps = mikem/RadioHead@^1.120 bblanchon/ArduinoJson@^6.21.3 + rweather/Crypto@^0.4.0