Skip to content

Commit

Permalink
Adds bind phrase feature (#12)
Browse files Browse the repository at this point in the history
* rename encryption and move option out of constructor (just use the set_...)
Fix comp unit test

* rename encryption and move option out of constructor (just use the set_...)
Fix comp unit test

* encryption in WBTx - default to true

* add option to debug packets variance

* keys

* upload keys

* fix unit tests

* fix unit tests

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* encrypton - add bind phrase feature

* this option is obsolete

* encryption add bind phrase feature

* encryption add bind phrase feature

* encryption add bind phrase feature

* encryption add bind phrase feature

* encryption add bind phrase feature

* encryption add bind phrase feature

* encryption add bind phrase feature

* encryption add bind phrase feature

* encryption add bind phrase feature

* add likely wrong bind phrase feature
  • Loading branch information
Consti10 authored Aug 11, 2023
1 parent 5508043 commit 773e017
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 170 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build_and_unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ jobs:
./build/wfb_keygen
- name: Unit test
run: |
./build/unit_test
cd build
./unit_test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ wfb_tx
unit_test
benchmark
gs.key
drone.key
key_1.key
wfb_keygen
udp_generator_validator
socket_helper_test
Expand Down
1 change: 1 addition & 0 deletions example_key/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Example tx / rx key, generated from the default openhd bind phrase "openhd"
3 changes: 3 additions & 0 deletions example_key/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#bin/bash

./../cmake-build-debug/wfb_keygen -b openhd
2 changes: 2 additions & 0 deletions example_key/txrx.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
~%�Er>�������>����|��x�;r�F��KW��mn��hX��Y��--r{���C��m5�ʲ
�&��\� �T,�I�d����i1��}�/���E4V����)��p�0�5���L�P���
3 changes: 0 additions & 3 deletions example_keys/generate.sh

This file was deleted.

6 changes: 4 additions & 2 deletions executables/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ void benchmark_fec_encode(const Options &options, bool printBlockTime = false) {
void benchmark_crypt(const Options &options,const bool packet_validation_only) {
assert(options.benchmarkType == BENCHMARK_ENCRYPT || options.benchmarkType == BENCHMARK_DECRYPT);
const bool encrypt=options.benchmarkType==BENCHMARK_ENCRYPT;
Encryptor encryptor{std::nullopt,packet_validation_only};
Decryptor decryptor{std::nullopt,packet_validation_only};
wb::Encryptor encryptor{wb::generate_keypair_deterministic(true)};
encryptor.set_encryption_enabled(!packet_validation_only);
wb::Decryptor decryptor{wb::generate_keypair_deterministic(true)};
decryptor.set_encryption_enabled(!packet_validation_only);
std::array<uint8_t, crypto_box_NONCEBYTES> sessionKeyNonce{};
std::array<uint8_t, crypto_aead_chacha20poly1305_KEYBYTES + crypto_box_MACBYTES> sessionKeyData{};
encryptor.makeNewSessionKey(sessionKeyNonce, sessionKeyData);
Expand Down
3 changes: 1 addition & 2 deletions executables/example_hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ int main(int argc, char *const *argv) {
WBTxRx::WifiCard tmp_card{card,1};
cards.push_back(tmp_card);
WBTxRx::Options options_txrx{};
options_txrx.rtl8812au_rssi_fixup= true;
options_txrx.set_direction= true;
options_txrx.pcap_rx_set_direction = true;
options_txrx.use_gnd_identifier=!is_air;
if(advanced_debugging){
options_txrx.log_all_received_validated_packets= true;
Expand Down
3 changes: 1 addition & 2 deletions executables/example_pollute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ int main(int argc, char *const *argv) {
WBTxRx::WifiCard tmp_card{card,1};
cards.push_back(tmp_card);
WBTxRx::Options options_txrx{};
options_txrx.rtl8812au_rssi_fixup= true;
options_txrx.set_direction= true;
options_txrx.pcap_rx_set_direction = true;
options_txrx.enable_non_openhd_mode= true;

std::shared_ptr<WBTxRx> txrx=std::make_shared<WBTxRx>(cards,options_txrx);
Expand Down
5 changes: 2 additions & 3 deletions executables/example_udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ int main(int argc, char *const *argv) {
WBTxRx::WifiCard tmp_card{card,1};
cards.push_back(tmp_card);
WBTxRx::Options options_txrx{};
options_txrx.rtl8812au_rssi_fixup= true;
//options_txrx.set_direction= false;
options_txrx.set_direction= pcap_setdirection;
//options_txrx.pcap_rx_set_direction= false;
options_txrx.pcap_rx_set_direction = pcap_setdirection;
options_txrx.log_all_received_validated_packets= false;

std::shared_ptr<WBTxRx> txrx=std::make_shared<WBTxRx>(cards,options_txrx);
Expand Down
3 changes: 1 addition & 2 deletions executables/injection_rate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ int main(int argc, char *const *argv) {
WBTxRx::WifiCard tmp_card{card,1};
cards.push_back(tmp_card);
WBTxRx::Options options_txrx{};
options_txrx.rtl8812au_rssi_fixup= true;
//options_txrx.set_direction= false;
//options_txrx.pcap_rx_set_direction= false;
options_txrx.log_all_received_validated_packets= false;

std::shared_ptr<WBTxRx> txrx=std::make_shared<WBTxRx>(cards,options_txrx);
Expand Down
5 changes: 2 additions & 3 deletions executables/test_txrx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ int main(int argc, char *const *argv) {
WBTxRx::WifiCard tmp_card{card,1};
cards.push_back(tmp_card);
WBTxRx::Options options_txrx{};
options_txrx.rtl8812au_rssi_fixup= true;
//options_txrx.set_direction= false;
options_txrx.set_direction= pcap_setdirection;
//options_txrx.pcap_rx_set_direction= false;
options_txrx.pcap_rx_set_direction = pcap_setdirection;
options_txrx.log_all_received_validated_packets= true;

std::shared_ptr<WBTxRx> txrx=std::make_shared<WBTxRx>(cards,options_txrx);
Expand Down
32 changes: 18 additions & 14 deletions executables/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,34 @@ static void test_fec_stream_random_bs_fs_overhead_dropped(){
}

// Test encryption+packet validation and packet validation only
static void test_encrypt_decrypt_validate(const bool useGeneratedFiles,bool message_signing_only) {
std::cout << "Using generated keypair (default seed otherwise):" << (useGeneratedFiles ? "y" : "n") << "\n";
const std::string filename_gs="gs.key"; //"../example_keys/gs.key"
const std::string filename_drone="drone.key" //"../example_keys/drone.key"
std::optional<std::string> encKey = useGeneratedFiles ? std::optional<std::string>(filename_gs) : std::nullopt;
std::optional<std::string> decKey = useGeneratedFiles ? std::optional<std::string>(filename_drone) : std::nullopt;
if(message_signing_only){
std::cout<<"Testing message signing\n";
static void test_encrypt_decrypt_validate(const bool use_key_from_file,bool message_signing_only) {
const std::string TEST_TYPE=message_signing_only ? "Sign" : "Encrypt&Sign";
const std::string TEST_KEY_TYPE=use_key_from_file ? "key from file" : "default key";
fmt::print("Testing {} with {}\n",TEST_TYPE,TEST_KEY_TYPE);
const std::string KEY_FILENAME="../example_key/txrx.key";
wb::KeyPairTxRx keyPairTxRx{};
if(use_key_from_file){
keyPairTxRx=wb::read_keypair_from_file(KEY_FILENAME);
}else{
std::cout<<"Testing encryption & signing\n";
const auto before=std::chrono::steady_clock::now();
keyPairTxRx=wb::generate_keypair_from_bind_phrase("openhd");
std::cout<<"Generating keypair from bind phrase took:"<<MyTimeHelper::R(std::chrono::steady_clock::now()-before)<<std::endl;
}

Encryptor encryptor{encKey,message_signing_only};
Decryptor decryptor{decKey,message_signing_only};
wb::Encryptor encryptor{keyPairTxRx.get_tx_key(true)};// We send from air unit
encryptor.set_encryption_enabled(!message_signing_only);
wb::Decryptor decryptor{keyPairTxRx.get_rx_key(false)}; // To the ground unit
decryptor.set_encryption_enabled(!message_signing_only);
struct SessionStuff{
std::array<uint8_t, crypto_box_NONCEBYTES> sessionKeyNonce{}; // random data
std::array<uint8_t, crypto_box_NONCEBYTES> sessionKeyNonce{}; // filled with random data
std::array<uint8_t, crypto_aead_chacha20poly1305_KEYBYTES + crypto_box_MACBYTES> sessionKeyData{};
};
SessionStuff sessionKeyPacket;
// make session key (tx)
encryptor.makeNewSessionKey(sessionKeyPacket.sessionKeyNonce, sessionKeyPacket.sessionKeyData);
// and "receive" session key (rx)
assert(decryptor.onNewPacketSessionKeyData(sessionKeyPacket.sessionKeyNonce, sessionKeyPacket.sessionKeyData)
== Decryptor::SESSION_VALID_NEW);
== wb::Decryptor::SESSION_VALID_NEW);
// now encrypt a couple of packets and decrypt them again afterwards
for (uint64_t nonce = 0; nonce < 200; nonce++) {
const auto data = GenericHelper::createRandomDataBuffer(FEC_PACKET_MAX_PAYLOAD_SIZE);
Expand Down Expand Up @@ -170,7 +174,7 @@ static void test_encrypt_decrypt_validate(const bool useGeneratedFiles,bool mess
nonce, enrypted_wrong_sign->data(), enrypted_wrong_sign->size());
assert(decrypted== nullptr);
}
std::cout << "encryption test passed\n";
fmt::print("Test {} with {} passed\n",TEST_TYPE,TEST_KEY_TYPE);
}


Expand Down
32 changes: 28 additions & 4 deletions executables/wfb_keygen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,33 @@
#include "../src/Encryption.hpp"

/**
* Generates a new keypair and saves it to file for later use.
* Generates a new tx rx keypair and saves it to file for later use.
*/
int main(void) {
auto keypair=wbencryption::generate_keypair();
return wbencryption::write_to_file(keypair);
int main(int argc, char *const *argv) {
int opt;
std::optional<std::string> bind_phrase=std::nullopt;
while ((opt = getopt(argc, argv, "b:")) != -1) {
switch (opt) {
case 'b':{
bind_phrase=std::string(optarg);
}
break;
default: /* '?' */
show_usage:
fprintf(stderr,
"wfb-keygen [-b bind_phrase,deterministic], if no bind phrase is specified, random keys are generated (non-deterministic)\n",
argv[0]);
exit(1);
}
}
wb::KeyPairTxRx keyPairTxRx{};
if(bind_phrase.has_value()){
std::cout<<"Generating txrx keypair using bind phrase ["<<bind_phrase.value()<<"]"<<std::endl;
keyPairTxRx=wb::generate_keypair_from_bind_phrase(bind_phrase.value());
}else{
std::cout<<"Generating random txrx keypair"<<std::endl;
keyPairTxRx=wb::generate_keypair_random();
}
//auto keypair=wb::generate_keypair_from_bind_phrase("openhd");
return wb::write_keypair_to_file(keyPairTxRx,"txrx.key");
}
2 changes: 1 addition & 1 deletion latencyTesting/SimpleTestProgram/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env/
_trial_temp/
test
gs.key
drone.key
key_1.key
wfb_keygen
telemetry/conf/site.cfg
telemetry/conf/local.cfg
Expand Down
Loading

0 comments on commit 773e017

Please sign in to comment.