hash&salt code simplified

This commit is contained in:
cyberman54 2020-12-28 16:12:00 +01:00
parent 70150fa606
commit 3cbaf240d9
7 changed files with 23 additions and 22 deletions

View File

@ -10,6 +10,7 @@
#include "display.h" #include "display.h"
#include "sds011read.h" #include "sds011read.h"
#include "sdcard.h" #include "sdcard.h"
#include "macsniff.h"
extern Ticker cyclicTimer; extern Ticker cyclicTimer;

View File

@ -14,7 +14,7 @@
#include "corona.h" #include "corona.h"
#endif #endif
uint16_t get_salt(void); uint32_t renew_salt(void);
uint64_t macConvert(uint8_t *paddr); uint64_t macConvert(uint8_t *paddr);
esp_err_t macQueueInit(void); esp_err_t macQueueInit(void);
void mac_process(void *pvParameters); void mac_process(void *pvParameters);

View File

@ -97,7 +97,6 @@ void doHousekeeping() {
"free heap = %d bytes)", "free heap = %d bytes)",
ESP.getMinFreeHeap(), ESP.getFreeHeap()); ESP.getMinFreeHeap(), ESP.getFreeHeap());
reset_counters(); // clear macs container and reset all counters reset_counters(); // clear macs container and reset all counters
get_salt(); // get new salt for salting hashes
if (ESP.getMinFreeHeap() <= MEM_LOW) // check again if (ESP.getMinFreeHeap() <= MEM_LOW) // check again
do_reset(true); // memory leak, reset device do_reset(true); // memory leak, reset device
@ -108,7 +107,6 @@ void doHousekeeping() {
if (ESP.getMinFreePsram() <= MEM_LOW) { if (ESP.getMinFreePsram() <= MEM_LOW) {
ESP_LOGI(TAG, "PSRAM full, counter cleared"); ESP_LOGI(TAG, "PSRAM full, counter cleared");
reset_counters(); // clear macs container and reset all counters reset_counters(); // clear macs container and reset all counters
get_salt(); // get new salt for salting hashes
if (ESP.getMinFreePsram() <= MEM_LOW) // check again if (ESP.getMinFreePsram() <= MEM_LOW) // check again
do_reset(true); // memory leak, reset device do_reset(true); // memory leak, reset device
@ -140,6 +138,7 @@ void reset_counters() {
macs.clear(); // clear all macs container macs.clear(); // clear all macs container
macs_wifi = 0; macs_wifi = 0;
macs_ble = 0; macs_ble = 0;
renew_salt(); // get new salt
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY
dp_plotCurve(0, true); dp_plotCurve(0, true);
#endif #endif

View File

@ -9,10 +9,11 @@ static const char TAG[] = __FILE__;
QueueHandle_t MacQueue; QueueHandle_t MacQueue;
TaskHandle_t macProcessTask; TaskHandle_t macProcessTask;
uint16_t salt = 0; static uint32_t salt = renew_salt();
uint16_t get_salt(void) { uint32_t renew_salt(void) {
salt = (uint16_t)random(65536); // get new 16bit random for salting hashes salt = esp_random();
ESP_LOGV(TAG, "new salt = %04X", salt);
return salt; return salt;
} }
@ -101,9 +102,6 @@ void IRAM_ATTR mac_add(uint8_t *paddr, int8_t rssi, snifftype_t sniff_type) {
uint16_t mac_analyze(MacBuffer_t MacBuffer) { uint16_t mac_analyze(MacBuffer_t MacBuffer) {
if (salt == 0) // ensure we have salt (appears after radio is turned on)
return 0;
if ((cfg.rssilimit) && if ((cfg.rssilimit) &&
(MacBuffer.rssi < cfg.rssilimit)) { // rssi is negative value (MacBuffer.rssi < cfg.rssilimit)) { // rssi is negative value
ESP_LOGI(TAG, "%s RSSI %d -> ignoring (limit: %d)", ESP_LOGI(TAG, "%s RSSI %d -> ignoring (limit: %d)",
@ -126,8 +124,7 @@ uint16_t mac_analyze(MacBuffer_t MacBuffer) {
} }
}; };
char buff[10]; // temporary buffer for printf uint32_t *mac; // pointer to shortened 4 byte MAC
uint32_t *mac; // temporary buffer for shortened MAC
// only last 3 MAC Address bytes are used for MAC address anonymization // only last 3 MAC Address bytes are used for MAC address anonymization
// but since it's uint32 we take 4 bytes to avoid 1st value to be 0. // but since it's uint32 we take 4 bytes to avoid 1st value to be 0.
@ -138,11 +135,15 @@ uint16_t mac_analyze(MacBuffer_t MacBuffer) {
// and increment counter on display // and increment counter on display
// https://en.wikipedia.org/wiki/MAC_Address_Anonymization // https://en.wikipedia.org/wiki/MAC_Address_Anonymization
snprintf(buff, sizeof(buff), "%08X", // reversed 4 byte MAC added to current salt
*mac + (uint32_t)salt); // convert unsigned 32-bit salted MAC const uint32_t saltedmac = *mac + salt;
// to 8 digit hex string
uint16_t hashedmac = hash(&buff[3], 5); // hash MAC 8 digit -> 5 digit // hashed 4 byte MAC
auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique // to save RAM, we use only lower 2 bytes of hash, since collisions don't
// matter in our use case
const uint16_t hashedmac = hash((const char *)&saltedmac, 4);
auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique
bool added = bool added =
newmac.second ? true : false; // true if hashed MAC is unique in container newmac.second ? true : false; // true if hashed MAC is unique in container
@ -183,7 +184,8 @@ uint16_t mac_analyze(MacBuffer_t MacBuffer) {
// Log scan result // Log scan result
ESP_LOGV(TAG, ESP_LOGV(TAG,
"%s %s RSSI %ddBi -> salted MAC %s -> Hash %04X -> WiFi:%d " "%s %s RSSI %ddBi -> MAC %0x:%0x:%0x:%0x:%0x:%0x -> salted %04X"
" -> hashed %04X -> WiFi:%d "
"BLTH:%d " "BLTH:%d "
#if (COUNT_ENS) #if (COUNT_ENS)
"(CWA:%d)" "(CWA:%d)"
@ -191,7 +193,9 @@ uint16_t mac_analyze(MacBuffer_t MacBuffer) {
"-> %d Bytes left", "-> %d Bytes left",
added ? "new " : "known", added ? "new " : "known",
MacBuffer.sniff_type == MAC_SNIFF_WIFI ? "WiFi" : "BLTH", MacBuffer.sniff_type == MAC_SNIFF_WIFI ? "WiFi" : "BLTH",
MacBuffer.rssi, buff, hashedmac, macs_wifi, macs_ble, MacBuffer.rssi, MacBuffer.mac[0], MacBuffer.mac[1], MacBuffer.mac[2],
MacBuffer.mac[3], MacBuffer.mac[4], MacBuffer.mac[5], saltedmac,
hashedmac, macs_wifi, macs_ble,
#if (COUNT_ENS) #if (COUNT_ENS)
cwa_report(), cwa_report(),
#endif #endif

View File

@ -428,7 +428,7 @@ void setup() {
// initialize salt value using esp_random() called by random() in // initialize salt value using esp_random() called by random() in
// arduino-esp32 core. Note: do this *after* wifi has started, since // arduino-esp32 core. Note: do this *after* wifi has started, since
// function gets it's seed from RF noise // function gets it's seed from RF noise
get_salt(); // get new 16bit for salting hashes reset_counters();
// start state machine // start state machine
ESP_LOGI(TAG, "Starting Interrupt Handler..."); ESP_LOGI(TAG, "Starting Interrupt Handler...");

View File

@ -18,7 +18,6 @@ void set_reset(uint8_t val[]) {
case 1: // reset MAC counter case 1: // reset MAC counter
ESP_LOGI(TAG, "Remote command: reset MAC counter"); ESP_LOGI(TAG, "Remote command: reset MAC counter");
reset_counters(); // clear macs reset_counters(); // clear macs
get_salt(); // get new salt
break; break;
case 2: // reset device to factory settings case 2: // reset device to factory settings
ESP_LOGI(TAG, "Remote command: reset device to factory settings"); ESP_LOGI(TAG, "Remote command: reset device to factory settings");
@ -119,7 +118,6 @@ void set_countmode(uint8_t val[]) {
return; return;
} }
reset_counters(); // clear macs reset_counters(); // clear macs
get_salt(); // get new salt
} }
void set_screensaver(uint8_t val[]) { void set_screensaver(uint8_t val[]) {

View File

@ -115,7 +115,6 @@ void sendData() {
// clear counter if not in cumulative counter mode // clear counter if not in cumulative counter mode
if (cfg.countermode != 1) { if (cfg.countermode != 1) {
reset_counters(); // clear macs container and reset all counters reset_counters(); // clear macs container and reset all counters
get_salt(); // get new salt for salting hashes
ESP_LOGI(TAG, "Counter cleared"); ESP_LOGI(TAG, "Counter cleared");
} }
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY