From e5c061ff32afedcd72c78e8dfa4ffad2fa61cbc9 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 20 Apr 2022 14:37:21 +0200 Subject: [PATCH] rename... --- .../arduino/lib/Firewall/esp32Eeprom.cpp | 26 ------- .../arduino/lib/Firewall/esp32Storage.cpp | 70 +++++++++++++++++++ .../{esp32Eeprom.hpp => esp32Storage.hpp} | 15 ++-- SourceCode/arduino/src/main.cpp | 2 +- 4 files changed, 81 insertions(+), 32 deletions(-) delete mode 100644 SourceCode/arduino/lib/Firewall/esp32Eeprom.cpp create mode 100644 SourceCode/arduino/lib/Firewall/esp32Storage.cpp rename SourceCode/arduino/lib/Firewall/{esp32Eeprom.hpp => esp32Storage.hpp} (62%) diff --git a/SourceCode/arduino/lib/Firewall/esp32Eeprom.cpp b/SourceCode/arduino/lib/Firewall/esp32Eeprom.cpp deleted file mode 100644 index 5e6ae6f..0000000 --- a/SourceCode/arduino/lib/Firewall/esp32Eeprom.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "esp32Eeprom.hpp" - -namespace firewall -{ - Storage::Storage(const uint16_t eeprom_size) - { - this->eeprom_size = eeprom_size; - EEPROM.begin(this->eeprom_size); - set_amount_of_firewall_rules(EEPROM.read(this->settings_head)); - log_i("Amount of Rules: %i", get_amount_of_firewall_rules()); - } - - Storage::~Storage() - { - } - - uint8_t Storage::get_amount_of_firewall_rules() - { - return this->amount_of_rules; - } - - void Storage::set_amount_of_firewall_rules(const uint8_t new_amount) - { - this->amount_of_rules = new_amount; - } -} diff --git a/SourceCode/arduino/lib/Firewall/esp32Storage.cpp b/SourceCode/arduino/lib/Firewall/esp32Storage.cpp new file mode 100644 index 0000000..a4b718f --- /dev/null +++ b/SourceCode/arduino/lib/Firewall/esp32Storage.cpp @@ -0,0 +1,70 @@ +#include "esp32Storage.hpp" + +namespace firewall +{ + Storage::Storage(const uint16_t eeprom_size) + { + this->get_eeprom_hash(); + this->eeprom_size = eeprom_size; + EEPROM.begin(this->eeprom_size); + this->amount_of_rules = EEPROM.read(this->settings_head); + log_i("Amount of Rules: %i", this->amount_of_rules); + } + + Storage::~Storage() + { + } + + void Storage::clear_eeprom() + { + + for (int i = 0; i < this->eeprom_size; i++) + { + EEPROM.write(i, 0); + } + } + + void Storage::get_eeprom_hash() + { + char buffer[this->eeprom_size] = {0}; + for (int i = 0; i < this->eeprom_size; i++) + { + buffer[i] = EEPROM.readChar(i); + } + unsigned char *hashedPayload = get_hash(buffer); + for (int i = 0; i < sizeof(hashedPayload); i++) + { + char str[3]; + sprintf(str, "%02x", (int)hashedPayload[i]); + Serial.print(str); + } + } +} + +unsigned char *Storage::get_hash(const char *payload) +{ + unsigned char hashedPayload[32]; + mbedtls_md_context_t ctx; + mbedtls_md_type_t md_type = MBEDTLS_MD_MD5; + const size_t payloadLength = strlen(payload); + + mbedtls_md_init(&ctx); + mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0); + mbedtls_md_starts(&ctx); + mbedtls_md_update(&ctx, (const unsigned char *)payload, payloadLength); + mbedtls_md_finish(&ctx, hashedPayload); + mbedtls_md_free(&ctx); + + return hashedPayload; +} + +uint8_t Storage::get_amount_of_firewall_rules() +{ + return this->amount_of_rules; +} + +void Storage::set_amount_of_firewall_rules(const uint8_t new_amount) +{ + this->amount_of_rules = new_amount; +} +} diff --git a/SourceCode/arduino/lib/Firewall/esp32Eeprom.hpp b/SourceCode/arduino/lib/Firewall/esp32Storage.hpp similarity index 62% rename from SourceCode/arduino/lib/Firewall/esp32Eeprom.hpp rename to SourceCode/arduino/lib/Firewall/esp32Storage.hpp index e8d04f7..3a92d1f 100644 --- a/SourceCode/arduino/lib/Firewall/esp32Eeprom.hpp +++ b/SourceCode/arduino/lib/Firewall/esp32Storage.hpp @@ -1,8 +1,9 @@ -#ifndef ESP32_EEPROM_HPP -#define ESP32_EEPROM_HPP +#ifndef ESP32_STORAGE_HPP +#define ESP32_STORAGE_HPP #include "EEPROM.h" #include "FirewallTypes.h" +#include "mbedtls/md.h" namespace firewall { @@ -12,18 +13,22 @@ namespace firewall uint16_t eeprom_size; uint16_t settings_start = 0; uint16_t settings_head = settings_start; - uint16_t rules_start = 1000; + uint16_t rules_start = 100; uint16_t rules_head = rules_start; - uint16_t certificate_start = 3000; + uint16_t certificate_start = 800; uint16_t certificate_head = certificate_start; + void clear_eeprom(); + void get_eeprom_hash(); + unsigned char *Storage::get_hash(const char *); + protected: uint8_t amount_of_rules; uint8_t get_amount_of_firewall_rules(); void set_amount_of_firewall_rules(const uint8_t); public: - Storage(const uint16_t = 4000); + Storage(const uint16_t = 1000); ~Storage(); }; } diff --git a/SourceCode/arduino/src/main.cpp b/SourceCode/arduino/src/main.cpp index 77707cd..4b5dbf8 100644 --- a/SourceCode/arduino/src/main.cpp +++ b/SourceCode/arduino/src/main.cpp @@ -2,7 +2,7 @@ #include "WiFi.h" #include "esp32FirewallAPI.hpp" -#include "esp32Eeprom.hpp" +#include "esp32Storage.hpp" firewall::esp32FirewallApi *firewall_api; firewall::Storage *storage;