diff --git a/src/Encryption.hpp b/src/Encryption.hpp index 19be4dc2..795817a7 100644 --- a/src/Encryption.hpp +++ b/src/Encryption.hpp @@ -167,11 +167,16 @@ class Decryptor { std::array tx_publickey{}; std::array session_key{}; public: + static constexpr auto SESSION_VALID_NEW=0; + static constexpr auto SESSION_VALID_NOT_NEW=1; + static constexpr auto SESSION_NOT_VALID=-1; /** - * Returns true if the session is a valid session in regards to the key-pairs AND the session is a new session - * (The same session key can be sent multiple times by the tx, since we are broadcast this is necessary) + * Returns 0 if the session is a valid session in regards to the key-pairs AND the session is a new session + * Returns 1 if the session is a valid session in regards to the key-pairs but it is not a new session + * Returns -1 if the session is not a valid session in regards to the key-pairs + * */ - bool onNewPacketSessionKeyData(const std::array &sessionKeyNonce, + int onNewPacketSessionKeyData(const std::array &sessionKeyNonce, const std::array &sessionKeyData) { std::array new_session_key{}; if (crypto_box_open_easy(new_session_key.data(), @@ -180,15 +185,15 @@ class Decryptor { tx_publickey.data(), rx_secretkey.data()) != 0) { // this basically should just never happen, and is an error wifibroadcast::log::get_default()->warn("unable to decrypt session key"); - return false; + return SESSION_NOT_VALID; } if (memcmp(session_key.data(), new_session_key.data(), sizeof(session_key)) != 0) { // this is NOT an error, the same session key is sent multiple times ! wifibroadcast::log::get_default()->info("Decryptor-New session detected"); session_key = new_session_key; - return true; + return SESSION_VALID_NEW; } - return false; + return SESSION_VALID_NOT_NEW; } /** * Decrypt (or validate only if encryption is disabled) the given message diff --git a/src/WBTxRx.cpp b/src/WBTxRx.cpp index 85a4943d..2e9377bf 100644 --- a/src/WBTxRx.cpp +++ b/src/WBTxRx.cpp @@ -357,11 +357,17 @@ void WBTxRx::on_new_packet(const uint8_t wlan_idx, const pcap_pkthdr &hdr, // card 2 on the ground likely picks up such a packet and if we were not to ignore it, we'd get the session key // TODO make it better - // for now, ignore session key packets not from card 0 - if(wlan_idx!=0){ + // Not needed anymore, due to unique air / ground id's + /*if(wlan_idx!=0){ return ; - } + }*/ SessionKeyPacket &sessionKeyPacket = *((SessionKeyPacket*) parsedPacket->payload); - if (m_decryptor->onNewPacketSessionKeyData(sessionKeyPacket.sessionKeyNonce, sessionKeyPacket.sessionKeyData)) { + const auto decrypt_res=m_decryptor->onNewPacketSessionKeyData(sessionKeyPacket.sessionKeyNonce, sessionKeyPacket.sessionKeyData); + if(wlan_idx==0 && (decrypt_res==Decryptor::SESSION_VALID_NEW || decrypt_res==Decryptor::SESSION_VALID_NOT_NEW)){ + m_pollution_openhd_rx_packets++; + recalculate_pollution_perc(); + } + if (decrypt_res==Decryptor::SESSION_VALID_NEW) { m_console->debug("Initializing new session."); m_rx_stats.n_received_valid_session_key_packets++; for(auto& handler:m_rx_handlers){ @@ -370,10 +376,6 @@ void WBTxRx::on_new_packet(const uint8_t wlan_idx, const pcap_pkthdr &hdr, opt_cb_session(); } } - if(wlan_idx==0){ - m_pollution_openhd_rx_packets++; - recalculate_pollution_perc(); - } } }else{ // the payload needs to include at least one byte of actual payload and the encryption suffix