Skip to content

Commit

Permalink
improve pollution stat
Browse files Browse the repository at this point in the history
  • Loading branch information
Consti10 committed Aug 10, 2023
1 parent 3802bb7 commit 220c377
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
17 changes: 11 additions & 6 deletions src/Encryption.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,16 @@ class Decryptor {
std::array<uint8_t, crypto_box_PUBLICKEYBYTES> tx_publickey{};
std::array<uint8_t, crypto_aead_chacha20poly1305_KEYBYTES> 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<uint8_t, crypto_box_NONCEBYTES> &sessionKeyNonce,
int onNewPacketSessionKeyData(const std::array<uint8_t, crypto_box_NONCEBYTES> &sessionKeyNonce,
const std::array<uint8_t,crypto_aead_chacha20poly1305_KEYBYTES+ crypto_box_MACBYTES> &sessionKeyData) {
std::array<uint8_t, sizeof(session_key)> new_session_key{};
if (crypto_box_open_easy(new_session_key.data(),
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions src/WBTxRx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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
Expand Down

0 comments on commit 220c377

Please sign in to comment.