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