Skip to content

Commit

Permalink
rename encryption and move option out of constructor (just use the se…
Browse files Browse the repository at this point in the history
…t_...)

Fix comp unit test
  • Loading branch information
Consti10 committed Aug 10, 2023
1 parent 5508043 commit 2b7bf64
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
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};
Encryptor encryptor{std::nullopt};
encryptor.set_encryption_enabled(!packet_validation_only);
Decryptor decryptor{std::nullopt};
encryptor.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
8 changes: 5 additions & 3 deletions executables/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static void test_fec_stream_random_bs_fs_overhead_dropped(){
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"
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){
Expand All @@ -120,8 +120,10 @@ static void test_encrypt_decrypt_validate(const bool useGeneratedFiles,bool mess
std::cout<<"Testing encryption & signing\n";
}

Encryptor encryptor{encKey,message_signing_only};
Decryptor decryptor{decKey,message_signing_only};
Encryptor encryptor{encKey};
encryptor.set_encryption_enabled(!message_signing_only);
Decryptor decryptor{decKey};
encryptor.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 Down
21 changes: 9 additions & 12 deletions src/Encryption.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class Encryptor {
* @param keypair encryption key, otherwise enable a default deterministic encryption key by using std::nullopt
* @param DISABLE_ENCRYPTION_FOR_PERFORMANCE only validate, do not encrypt (less CPU usage)
*/
explicit Encryptor(std::optional<std::string> keypair, const bool DISABLE_ENCRYPTION_FOR_PERFORMANCE = false)
: DISABLE_ENCRYPTION_FOR_PERFORMANCE(DISABLE_ENCRYPTION_FOR_PERFORMANCE) {
explicit Encryptor(std::optional<std::string> keypair){
if (keypair == std::nullopt) {
// use default encryption keys
crypto_box_seed_keypair(rx_publickey.data(), tx_secretkey.data(), DEFAULT_ENCRYPTION_SEED.data());
Expand Down Expand Up @@ -93,7 +92,7 @@ class Encryptor {
* Returns written data size (msg payload plus sign data)
*/
int authenticate_and_encrypt(const uint64_t nonce,const uint8_t *src,std::size_t src_len,uint8_t* dest){
if(DISABLE_ENCRYPTION_FOR_PERFORMANCE){
if(!m_encrypt_data){
memcpy(dest,src, src_len);
uint8_t* sign=dest+src_len;
const auto sub_key=create_onetimeauth_subkey(nonce,session_key);
Expand All @@ -120,24 +119,22 @@ class Encryptor {
* @param encryption_enabled
*/
void set_encryption_enabled(bool encryption_enabled){
DISABLE_ENCRYPTION_FOR_PERFORMANCE=!encryption_enabled;
m_encrypt_data =encryption_enabled;
}
private:
// tx->rx keypair
std::array<uint8_t, crypto_box_SECRETKEYBYTES> tx_secretkey{};
std::array<uint8_t, crypto_box_PUBLICKEYBYTES> rx_publickey{};
std::array<uint8_t, crypto_aead_chacha20poly1305_KEYBYTES> session_key{};
// use this one if you are worried about CPU usage when using encryption
bool DISABLE_ENCRYPTION_FOR_PERFORMANCE;
//static_assert(crypto_onetimeauth_BYTES);
bool m_encrypt_data= true;
};

class Decryptor {
public:
// enable a default deterministic encryption key by using std::nullopt
// else, pass path to file with encryption keys
explicit Decryptor(std::optional<std::string> keypair, const bool DISABLE_ENCRYPTION_FOR_PERFORMANCE = false)
: DISABLE_ENCRYPTION_FOR_PERFORMANCE(DISABLE_ENCRYPTION_FOR_PERFORMANCE) {
explicit Decryptor(std::optional<std::string> keypair){
if (keypair == std::nullopt) {
crypto_box_seed_keypair(tx_publickey.data(), rx_secretkey.data(), DEFAULT_ENCRYPTION_SEED.data());
wifibroadcast::log::get_default()->debug("Using default keys");
Expand All @@ -160,7 +157,7 @@ class Decryptor {
}
private:
// use this one if you are worried about CPU usage when using encryption
bool DISABLE_ENCRYPTION_FOR_PERFORMANCE;
bool m_encrypt_data= true;
public:
std::array<uint8_t, crypto_box_SECRETKEYBYTES> rx_secretkey{};
public:
Expand Down Expand Up @@ -202,7 +199,7 @@ class Decryptor {
* @param dest needs to be at least @param encrypted - 16 bytes big.
*/
bool authenticate_and_decrypt(const uint64_t& nonce,const uint8_t* encrypted,int encrypted_size,uint8_t* dest){
if(DISABLE_ENCRYPTION_FOR_PERFORMANCE){
if(!m_encrypt_data){
const auto payload_size=encrypted_size-crypto_onetimeauth_BYTES;
assert(payload_size>0);
const uint8_t* sign=encrypted+payload_size;
Expand Down Expand Up @@ -233,7 +230,7 @@ class Decryptor {
return nullptr;
}
int get_additional_payload_size() const{
if(DISABLE_ENCRYPTION_FOR_PERFORMANCE){
if(m_encrypt_data){
return crypto_onetimeauth_BYTES;
}
return crypto_aead_chacha20poly1305_ABYTES;
Expand All @@ -243,7 +240,7 @@ class Decryptor {
* @param encryption_enabled
*/
void set_encryption_enabled(bool encryption_enabled){
DISABLE_ENCRYPTION_FOR_PERFORMANCE=!encryption_enabled;
m_encrypt_data =encryption_enabled;
}
};

Expand Down

0 comments on commit 2b7bf64

Please sign in to comment.