From 825b335932ba1808cab2356a1be64759901f1ae5 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sun, 21 Jul 2024 16:11:00 -0500 Subject: [PATCH 1/3] Make sure to call randomSeed() on esp32 --- src/platform/esp32/main-esp32.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/platform/esp32/main-esp32.cpp b/src/platform/esp32/main-esp32.cpp index aa51e810a8..bc5c1a8f0b 100644 --- a/src/platform/esp32/main-esp32.cpp +++ b/src/platform/esp32/main-esp32.cpp @@ -93,6 +93,7 @@ void esp32Setup() { uint32_t seed = esp_random(); LOG_DEBUG("Setting random seed %u\n", seed); + randomSeed(seed); LOG_DEBUG("Total heap: %d\n", ESP.getHeapSize()); LOG_DEBUG("Free heap: %d\n", ESP.getFreeHeap()); From 6bc45e267057623116e5e354cd0f06aa51c8c0fb Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sun, 21 Jul 2024 16:11:27 -0500 Subject: [PATCH 2/3] Randomize the top 22 bits of the Message ID --- src/mesh/Router.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index c8c18ae6d5..35536e7149 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -92,22 +92,23 @@ void Router::enqueueReceivedMessage(meshtastic_MeshPacket *p) // FIXME, move this someplace better PacketId generatePacketId() { - static uint32_t i; // Note: trying to keep this in noinit didn't help for working across reboots + static uint32_t rollingPacketId; // Note: trying to keep this in noinit didn't help for working across reboots static bool didInit = false; - uint32_t numPacketId = UINT32_MAX; - if (!didInit) { didInit = true; // pick a random initial sequence number at boot (to prevent repeated reboots always starting at 0) // Note: we mask the high order bit to ensure that we never pass a 'negative' number to random - i = random(numPacketId & 0x7fffffff); - LOG_DEBUG("Initial packet id %u, numPacketId %u\n", i, numPacketId); + rollingPacketId = random(UINT32_MAX & 0x7fffffff); + LOG_DEBUG("Initial packet id %u\n", rollingPacketId); } - i++; - PacketId id = (i % numPacketId) + 1; // return number between 1 and numPacketId (ie - never zero) + rollingPacketId++; + + rollingPacketId &= UINT32_MAX >> 22; // Mask out the top 22 bits + PacketId id = rollingPacketId | random(UINT32_MAX & 0x7fffffff) << 10; // top 22 bits + LOG_DEBUG("Partially randomized packet id %u\n", id); return id; } From b65edc7c0c23e380917beed62e05f3abfbc4fbae Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Tue, 23 Jul 2024 01:05:16 -0500 Subject: [PATCH 3/3] Make it clear that we are not calling randomSeed() on purpose --- src/platform/esp32/main-esp32.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/platform/esp32/main-esp32.cpp b/src/platform/esp32/main-esp32.cpp index bc5c1a8f0b..3910f718f1 100644 --- a/src/platform/esp32/main-esp32.cpp +++ b/src/platform/esp32/main-esp32.cpp @@ -91,9 +91,12 @@ void enableSlowCLK() void esp32Setup() { + /* We explicitly don't want to do call randomSeed, + // as that triggers the esp32 core to use a less secure pseudorandom function. uint32_t seed = esp_random(); LOG_DEBUG("Setting random seed %u\n", seed); randomSeed(seed); + */ LOG_DEBUG("Total heap: %d\n", ESP.getHeapSize()); LOG_DEBUG("Free heap: %d\n", ESP.getFreeHeap());