Merge pull request #1 from K-Lab-Students/sensor-encryption

Sensor encryption
aggregator-decription
Oleg Likhogub 1 year ago committed by GitHub
commit 94d9716cdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

1
.gitignore vendored

@ -0,0 +1 @@
cybergarden-sensor/.vscode

@ -8,12 +8,59 @@
#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);
}
//*************************
//Encryption Testing
//*************************
// 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);
uint8_t encoded[MSG_LENGTH + ECC_LENGTH];
reedSolomonModule->encode(msg, encoded);
transmitterModule->send(encoded, MSG_LENGTH + ECC_LENGTH);

@ -1,23 +1,43 @@
#ifndef KEMPT_KINKAJOU_TRANSMISSIONMODULE_H
#define KEMPT_KINKAJOU_TRANSMISSIONMODULE_H
#include <Arduino.h>
#include <Crypto.h>
#include <ChaChaPoly.h>
#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);
};

@ -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();

@ -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
mikem/RadioHead@^1.120
rweather/Crypto@^0.4.0

@ -8,9 +8,8 @@ ReedSolomonModule reedSolomonModule;
TransmissionModule transmissionModule(transmitterModule, reedSolomonModule);
void setup() {
Serial.begin(9600);
Serial.begin(115200);
transmitterModule.init();
pinMode(LED_BUILTIN, OUTPUT);
}
uint16_t getMockSensorData() {
@ -22,10 +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.transmit(measureData);
analogWrite(LED_BUILTIN, 255);
delay(200);
analogWrite(LED_BUILTIN, 0);
delay(1000);
}
Loading…
Cancel
Save