Skip to content

Commit

Permalink
Filesystem functions moved into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
buldo committed Nov 17, 2023
1 parent 15f9abe commit 865fa39
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 42 deletions.
1 change: 1 addition & 0 deletions WBLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ target_sources(wifibroadcast PRIVATE
${CMAKE_CURRENT_LIST_DIR}/src/encription/KeyPairTxRx.hpp
${CMAKE_CURRENT_LIST_DIR}/src/encription/Encryptor.cpp
${CMAKE_CURRENT_LIST_DIR}/src/encription/Encryption.cpp
${CMAKE_CURRENT_LIST_DIR}/src/encription/EncryptionFsUtils.cpp
${CMAKE_CURRENT_LIST_DIR}/src/encription/Decryptor.cpp

${CMAKE_CURRENT_LIST_DIR}/src/fec/FEC.cpp
Expand Down
1 change: 1 addition & 0 deletions executables/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "../src/fec/FEC.hpp"

#include "../src/encription/Encryption.hpp"
#include "../src/encription/EncryptionFsUtils.hpp"
#include "../src/HelperSources/Helper.hpp"
#include "../src/Ieee80211Header.hpp"
#include "../src/wifibroadcast_spdlog.hpp"
Expand Down
1 change: 1 addition & 0 deletions executables/wfb_keygen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <optional>

#include "../src/encription/Encryption.hpp"
#include "../src/encription/EncryptionFsUtils.hpp"
#include "../src/encription/KeyPairTxRx.hpp"

/**
Expand Down
31 changes: 0 additions & 31 deletions src/encription/Encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,6 @@ wb::KeyPairTxRx wb::generate_keypair_from_bind_phrase(
return ret;
}

int wb::write_keypair_to_file(const wb::KeyPairTxRx& keypair_txrx,
const std::string& filename) {
FILE *fp;
if ((fp = fopen(filename.c_str(), "w")) == nullptr) {
std::cerr<<"Unable to save "<<filename<<std::endl;
assert(false);
return 1;
}
assert(fwrite(keypair_txrx.key_1.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1);
assert(fwrite(keypair_txrx.key_1.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1);
assert(fwrite(keypair_txrx.key_2.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1);
assert(fwrite(keypair_txrx.key_2.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1);
fclose(fp);
return 0;
}

wb::KeyPairTxRx wb::read_keypair_from_file(const std::string& filename) {
KeyPairTxRx ret{};
FILE *fp;
if ((fp = fopen(filename.c_str(), "r")) == nullptr) {
std::cerr<<fmt::format("Unable to open {}: {}", filename.c_str(), strerror(errno))<<std::endl;
assert(false);
}
assert(fread(ret.key_1.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1);
assert(fread(ret.key_1.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1);
assert(fread(ret.key_2.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1);
assert(fread(ret.key_2.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1);
fclose(fp);
return ret;
}

std::array<uint8_t, crypto_aead_chacha20poly1305_KEYBYTES> wb::create_onetimeauth_subkey(
const uint64_t& nonce, const std::array<uint8_t, 32U>& session_key) {
// sub-key for this packet
Expand Down
11 changes: 0 additions & 11 deletions src/encription/Encryption.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ static constexpr auto DEFAULT_BIND_PHRASE="openhd";
*/
KeyPairTxRx generate_keypair_from_bind_phrase(const std::string& bind_phrase=DEFAULT_BIND_PHRASE);

/**
* Saves the KeyPairTxRx as a raw file
*/
int write_keypair_to_file(const KeyPairTxRx& keypair_txrx,const std::string& filename);

/**
* Reads a raw KeyPairTxRx from a raw file previusly generated.
*/
KeyPairTxRx read_keypair_from_file(const std::string& filename);


/**
* https://libsodium.gitbook.io/doc/key_derivation
* UINT16SeqNrHelper since we both support encryption and one time validation to save cpu performance
Expand Down
45 changes: 45 additions & 0 deletions src/encription/EncryptionFsUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Created by consti10 on 13.08.23.
//

#include "EncryptionFsUtils.hpp"

#include <sodium/crypto_box.h>

#include <cassert>
#include <cstring>
#include <iostream>

#include <spdlog/spdlog.h>
#include "../wifibroadcast_spdlog.hpp"

int wb::write_keypair_to_file(const wb::KeyPairTxRx& keypair_txrx,
const std::string& filename) {
FILE *fp;
if ((fp = fopen(filename.c_str(), "w")) == nullptr) {
std::cerr<<"Unable to save "<<filename<<std::endl;
assert(false);
return 1;
}
assert(fwrite(keypair_txrx.key_1.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1);
assert(fwrite(keypair_txrx.key_1.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1);
assert(fwrite(keypair_txrx.key_2.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1);
assert(fwrite(keypair_txrx.key_2.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1);
fclose(fp);
return 0;
}

wb::KeyPairTxRx wb::read_keypair_from_file(const std::string& filename) {
KeyPairTxRx ret{};
FILE *fp;
if ((fp = fopen(filename.c_str(), "r")) == nullptr) {
std::cerr<<fmt::format("Unable to open {}: {}", filename.c_str(), strerror(errno))<<std::endl;
assert(false);
}
assert(fread(ret.key_1.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1);
assert(fread(ret.key_1.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1);
assert(fread(ret.key_2.secret_key.data(), crypto_box_SECRETKEYBYTES, 1, fp)==1);
assert(fread(ret.key_2.public_key.data(), crypto_box_PUBLICKEYBYTES, 1, fp)==1);
fclose(fp);
return ret;
}
22 changes: 22 additions & 0 deletions src/encription/EncryptionFsUtils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef ENCRYPTION_FS_UTILS_HPP
#define ENCRYPTION_FS_UTILS_HPP

#include "KeyPairTxRx.hpp"
#include <string>

namespace wb {

/**
* Saves the KeyPairTxRx as a raw file
*/
int write_keypair_to_file(const KeyPairTxRx& keypair_txrx,
const std::string& filename);

/**
* Reads a raw KeyPairTxRx from a raw file previusly generated.
*/
KeyPairTxRx read_keypair_from_file(const std::string& filename);

} // namespace wb end

#endif // ENCRYPTION_FS_UTILS_HPP
5 changes: 5 additions & 0 deletions src/encription/Key.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#ifndef KEY_HPP
#define KEY_HPP
#include <sodium/crypto_box.h>

#include <cstdint>
#include <array>

namespace wb {


// A wb key consists of a public and secret key
struct Key {
std::array<uint8_t, crypto_box_PUBLICKEYBYTES> public_key;
Expand Down

0 comments on commit 865fa39

Please sign in to comment.