Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/consti-dev' into HEAD
Browse files Browse the repository at this point in the history
# Conflicts:
#	executables/unit_test.cpp
#	executables/wfb_keygen.cpp
#	src/Encryption.hpp
#	src/WBTxRx.cpp
#	src/WBTxRx.h
  • Loading branch information
Consti10 committed Aug 11, 2023
2 parents 283f64d + 85051fe commit fcbd319
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 130 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
1 change: 1 addition & 0 deletions example_keys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Example air / ground keys, used by the unit test
2 changes: 2 additions & 0 deletions example_keys/drone.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
����;��d� =���go1�HF�%���4Ab׷��A� �"<�/ǰ]� �7��r+9(.�+/

1 change: 1 addition & 0 deletions example_keys/gs.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
R�[���ڭYmw��M�C�4~������Y�O�:��t����;;(Y� a�0):z&2���r
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
32 changes: 25 additions & 7 deletions executables/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,36 @@ 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";
std::optional<std::string> encKey = useGeneratedFiles ? std::optional<std::string>("../example_keys/gs.key") : std::nullopt;
std::optional<std::string> decKey = useGeneratedFiles ? std::optional<std::string>("../example_keys/drone.key") : std::nullopt;
static void test_encrypt_decrypt_validate(const bool use_key_from_file,bool message_signing_only) {
std::cout << "Using generated keypair (default seed otherwise):" << (use_key_from_file ? "y" : "n") << "\n";
//const std::string filename_gs="gs.key";
//const std::string filename_drone="drone.key";
const std::string filename_gs="../example_keys/gs.key";
const std::string filename_drone="../example_keys/drone.key";
wb::KeyPair encKey{};
wb::KeyPair decKey{};
if(use_key_from_file){
encKey=wb::read_keypair_from_file(filename_gs);
decKey=wb::read_keypair_from_file(filename_gs);
}else{
/*encKey=wb::generate_keypair_deterministic(false);
decKey=wb::generate_keypair_deterministic(false);*/
const auto before=std::chrono::steady_clock::now();
auto tmp=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;
encKey=tmp.drone;
decKey=tmp.drone;
}
if(message_signing_only){
std::cout<<"Testing message signing\n";
}else{
std::cout<<"Testing encryption & signing\n";
}

Encryptor encryptor{encKey,message_signing_only};
Decryptor decryptor{decKey,message_signing_only};
wb::Encryptor encryptor{encKey};
encryptor.set_encryption_enabled(!message_signing_only);
wb::Decryptor decryptor{decKey};
decryptor.set_encryption_enabled(!message_signing_only);
struct SessionStuff{
std::array<uint8_t, crypto_box_NONCEBYTES> sessionKeyNonce{}; // random data
std::array<uint8_t, crypto_aead_chacha20poly1305_KEYBYTES + crypto_box_MACBYTES> sessionKeyData{};
Expand All @@ -129,7 +147,7 @@ static void test_encrypt_decrypt_validate(const bool useGeneratedFiles,bool mess
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
5 changes: 3 additions & 2 deletions executables/wfb_keygen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Generates a new keypair and saves it to file for later use.
*/
int main(void) {
auto keypair=wbencryption::generate_keypair();
return wbencryption::write_to_file(keypair);
auto keypair=wb::generate_keypair();
//auto keypair=wb::generate_keypair_from_bind_phrase("openhd");
return wb::write_to_file(keypair);
}
Loading

0 comments on commit fcbd319

Please sign in to comment.