From e9b781c3902a7ec7c58356e682484604d6ea1a3c Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 01:54:35 +0300 Subject: [PATCH 1/3] builds --- .../TransmissionModule/TransmissionModule.cpp | 47 ++++++++++++++++++- .../TransmissionModule/TransmissionModule.h | 28 +++++++++-- cybergarden-sensor/platformio.ini | 8 ++-- cybergarden-sensor/src/main.cpp | 13 ++--- 4 files changed, 82 insertions(+), 14 deletions(-) diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp index 2403d97..2d9b959 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp @@ -8,7 +8,52 @@ #define ECC_LENGTH 4 #define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH) -void TransmissionModule::transmit(MeasureData &data) { +void TransmissionModule::encrypt(uint8_t * input_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->encrypt(buffer + posn, input_data + posn, len); + } + + byte buffer2[MAX_PLAINTEXT_LEN]; + memset(buffer2, 0xBA, sizeof(buffer)); + + cipher->setKey(encryptionKey, 16); + cipher->setIV(iv, iv_size); + + for (posn = 0; posn < data_size; posn += inc) { + len = data_size - posn; + if (len > inc) + len = inc; + cipher->decrypt(buffer2 + posn, buffer + posn, len); + } + + if (memcmp(buffer2, input_data, data_size) != 0) { + // Serial.println("doesn't work"); + } else{ + // Serial.println("works"); + } + + memcpy(output_data, buffer, data_size); +} + +void TransmissionModule::transmit(MeasureData &data) +{ uint8_t msg[MSG_LENGTH]; msg[0] = data.sensor_id; memcpy(msg + 1, (void *) &data, 4); diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h index d3a81fa..247b77d 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h @@ -1,23 +1,43 @@ +#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: - unsigned char * encryptionKey = 0; + TransmissionModule( TransmitterModule &transmitterModule, ReedSolomonModule &reedSolomonModule ) : transmitterModule(&transmitterModule), - reedSolomonModule(&reedSolomonModule) {} + reedSolomonModule(&reedSolomonModule) { + cipher = new ChaChaPoly(); + } + void encrypt(uint8_t * data, size_t data_size, uint8_t* output_data); void transmit(MeasureData &data); }; diff --git a/cybergarden-sensor/platformio.ini b/cybergarden-sensor/platformio.ini index 9f204c0..203b30b 100644 --- a/cybergarden-sensor/platformio.ini +++ b/cybergarden-sensor/platformio.ini @@ -9,8 +9,10 @@ ; https://docs.platformio.org/page/projectconf.html [env:esp32doit-devkit-v1] -platform = atmelavr -board = megaatmega2560 +platform = espressif32 +board = esp32dev framework = arduino +monitor_speed = 115200 lib_deps = - mikem/RadioHead@^1.120 \ No newline at end of file + mikem/RadioHead@^1.120 + rweather/Crypto@^0.4.0 diff --git a/cybergarden-sensor/src/main.cpp b/cybergarden-sensor/src/main.cpp index 8eb0ba6..63c7234 100644 --- a/cybergarden-sensor/src/main.cpp +++ b/cybergarden-sensor/src/main.cpp @@ -8,9 +8,9 @@ ReedSolomonModule reedSolomonModule; TransmissionModule transmissionModule(transmitterModule, reedSolomonModule); void setup() { - Serial.begin(9600); + Serial.begin(115200); transmitterModule.init(); - pinMode(LED_BUILTIN, OUTPUT); + // pinMode(LED_BUILTIN, OUTPUT); } uint16_t getMockSensorData() { @@ -22,10 +22,11 @@ void loop() { measureData.sensor_id = 0xA5; measureData.sensor_type = 0x01; measureData.payload = getMockSensorData(); + // transmissionModule.encrypt((uint8_t*)&measureData+1, 4, (uint8_t*)&measureData+1); transmissionModule.transmit(measureData); - analogWrite(LED_BUILTIN, 255); - delay(200); - analogWrite(LED_BUILTIN, 0); - delay(10000); + // analogWrite(LED_BUILTIN, 255); + // delay(200); + // analogWrite(LED_BUILTIN, 0); + // delay(10000); } \ No newline at end of file From 214617355993b8330fd61e83505150dac2ac3c6d Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 02:18:20 +0300 Subject: [PATCH 2/3] fixed spi-flash-reboot on esp32, encryption kinda works --- .gitignore | 1 + .../TransmissionModule/TransmissionModule.cpp | 36 ++++++++++--------- .../lib/TransmitterModule/TransmitterModule.h | 6 ++-- cybergarden-sensor/src/main.cpp | 9 ++--- 4 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..02288c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cybergarden-sensor/.vscode diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp index 2d9b959..2e4e09f 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp @@ -30,24 +30,28 @@ void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t cipher->encrypt(buffer + posn, input_data + posn, len); } - byte buffer2[MAX_PLAINTEXT_LEN]; - memset(buffer2, 0xBA, sizeof(buffer)); + //************************* + //Encryption Testing + //************************* - cipher->setKey(encryptionKey, 16); - cipher->setIV(iv, iv_size); + // byte buffer2[MAX_PLAINTEXT_LEN]; + // memset(buffer2, 0xBA, sizeof(buffer)); - for (posn = 0; posn < data_size; posn += inc) { - len = data_size - posn; - if (len > inc) - len = inc; - cipher->decrypt(buffer2 + posn, buffer + posn, len); - } + // cipher->setKey(encryptionKey, 16); + // cipher->setIV(iv, iv_size); - if (memcmp(buffer2, input_data, data_size) != 0) { - // Serial.println("doesn't work"); - } else{ - // Serial.println("works"); - } + // for (posn = 0; posn < data_size; posn += inc) { + // len = data_size - posn; + // if (len > inc) + // len = inc; + // cipher->decrypt(buffer2 + posn, buffer + posn, len); + // } + + // if (memcmp(buffer2, input_data, data_size) != 0) { + // Serial.println("doesn't work"); + // } else{ + // Serial.println("works"); + // } memcpy(output_data, buffer, data_size); } @@ -57,8 +61,6 @@ void TransmissionModule::transmit(MeasureData &data) uint8_t msg[MSG_LENGTH]; msg[0] = data.sensor_id; memcpy(msg + 1, (void *) &data, 4); - - uint8_t encoded[MSG_LENGTH + ECC_LENGTH]; reedSolomonModule->encode(msg, encoded); transmitterModule->send(encoded, MSG_LENGTH + ECC_LENGTH); diff --git a/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h b/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h index b8f6964..94bdae9 100644 --- a/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h +++ b/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h @@ -8,15 +8,15 @@ #ifndef KEMPT_KINKAJOU_TRANSMITTERMODULE_H #define KEMPT_KINKAJOU_TRANSMITTERMODULE_H -#define RH_TRANSMIT_PIN 12 -#define RH_RECEIVE_PIN 11 +#define RH_TRANSMIT_PIN 5 +#define RH_RECEIVE_PIN 4 class TransmitterModule { private: RH_ASK *rhAsk; public: TransmitterModule() { - rhAsk = new RH_ASK(); + rhAsk = new RH_ASK(2000, RH_RECEIVE_PIN, RH_TRANSMIT_PIN); } bool init() { return rhAsk->init(); diff --git a/cybergarden-sensor/src/main.cpp b/cybergarden-sensor/src/main.cpp index 63c7234..508530b 100644 --- a/cybergarden-sensor/src/main.cpp +++ b/cybergarden-sensor/src/main.cpp @@ -10,7 +10,6 @@ TransmissionModule transmissionModule(transmitterModule, reedSolomonModule); void setup() { Serial.begin(115200); transmitterModule.init(); - // pinMode(LED_BUILTIN, OUTPUT); } uint16_t getMockSensorData() { @@ -22,11 +21,7 @@ void loop() { measureData.sensor_id = 0xA5; measureData.sensor_type = 0x01; measureData.payload = getMockSensorData(); - // transmissionModule.encrypt((uint8_t*)&measureData+1, 4, (uint8_t*)&measureData+1); + transmissionModule.encrypt((uint8_t*)&measureData+1, 4, (uint8_t*)&measureData+1); transmissionModule.transmit(measureData); - - // analogWrite(LED_BUILTIN, 255); - // delay(200); - // analogWrite(LED_BUILTIN, 0); - // delay(10000); + delay(1000); } \ No newline at end of file From f10ac55b925f14d948dbc111767b698a430d5be9 Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 02:19:32 +0300 Subject: [PATCH 3/3] cleaned a little --- cybergarden-sensor/.vscode/extensions.json | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 cybergarden-sensor/.vscode/extensions.json diff --git a/cybergarden-sensor/.vscode/extensions.json b/cybergarden-sensor/.vscode/extensions.json deleted file mode 100644 index 080e70d..0000000 --- a/cybergarden-sensor/.vscode/extensions.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ], - "unwantedRecommendations": [ - "ms-vscode.cpptools-extension-pack" - ] -}