Harden ESP32 identity entropy handling

This commit is contained in:
mikecarper committed 2026-08-11 14:25:52 -07:00
1 parent ea3843e0b4
commit a65769c96a
15 files changed
+414 -51

No files matched your search

+19 -8
View File
@@ -2,9 +2,14 @@
#include <Arduino.h> // needed for PlatformIO
#include <Mesh.h>
#include <helpers/IdentityGeneration.h>
#include "helpers/radiolib/RXPowerSaving.h"
#include "helpers/radiolib/RxBoostedGainDefaults.h"
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#endif
#ifdef ENABLE_USB_INTERFACE
#include <helpers/CLICommandUtils.h>
#include <helpers/TracePathHelpers.h>
@@ -1382,15 +1387,21 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
void MyMesh::begin(bool has_display) {
BaseChatMesh::begin();
const bool is_new_install = !_store->loadMainIdentity(self_id);
const bool is_new_install = !_store->loadMainIdentity(self_id)
|| mesh::hasReservedIdentityPrefix(self_id);
bool identity_ready = true;
if (is_new_install) {
self_id = radio_new_identity(); // create new random identity
int count = 0;
while (count < 10 && (self_id.pub_key[0] == 0x00 || self_id.pub_key[0] == 0xFF)) { // reserved id hashes
self_id = radio_new_identity();
count++;
}
_store->saveMainIdentity(self_id);
identity_ready = mesh::generateUsableLocalIdentity(self_id, radio_new_identity);
if (identity_ready) _store->saveMainIdentity(self_id);
}
#if defined(ESP32_PLATFORM)
mesh::discardESP32TrueRandom();
#endif
if (!identity_ready) {
MESH_DEBUG_PRINTLN("Identity generation exhausted all attempts; rebooting");
board.reboot();
return;
}
// if name is provided as a build flag, use that as default node name instead
+19 -6
View File
@@ -1,9 +1,14 @@
#include <Arduino.h>
#include <target.h>
#include <helpers/ArduinoHelpers.h>
#include <helpers/IdentityGeneration.h>
#include <helpers/IdentityStore.h>
#include "KissModem.h"
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#endif
#if defined(NRF52_PLATFORM)
#include <InternalFileSystem.h>
#elif defined(RP2040_PLATFORM)
@@ -48,12 +53,20 @@ void loadOrCreateIdentity() {
#error "Filesystem not defined"
#endif
if (!store.load("_main", identity)) {
identity = radio_new_identity();
while (identity.pub_key[0] == 0x00 || identity.pub_key[0] == 0xFF) {
identity = radio_new_identity();
}
store.save("_main", identity);
const bool needs_identity = !store.load("_main", identity)
|| mesh::hasReservedIdentityPrefix(identity);
bool identity_ready = true;
if (needs_identity) {
identity_ready = mesh::generateUsableLocalIdentity(identity, radio_new_identity);
if (identity_ready) store.save("_main", identity);
}
#if defined(ESP32_PLATFORM)
mesh::discardESP32TrueRandom();
#endif
if (!identity_ready) {
MESH_DEBUG_PRINTLN("Identity generation exhausted all attempts; rebooting");
board.reboot();
}
}
+19 -7
View File
@@ -1,7 +1,11 @@
#include <Arduino.h> // needed for PlatformIO
#include <Mesh.h>
#include <helpers/IdentityGeneration.h>
#include "MyMesh.h"
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#endif
#if defined(ESP32) && MAX_RECENT_REPEATERS > 0
#include <new>
#endif
@@ -110,14 +114,22 @@ void setup() {
#else
#error "need to define filesystem"
#endif
if (!store.load("_main", the_mesh.self_id)) {
const bool needs_identity = !store.load("_main", the_mesh.self_id)
|| mesh::hasReservedIdentityPrefix(the_mesh.self_id);
bool identity_ready = true;
if (needs_identity) {
MESH_DEBUG_PRINTLN("Generating new keypair");
the_mesh.self_id = radio_new_identity(); // create new random identity
int count = 0;
while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
the_mesh.self_id = radio_new_identity(); count++;
}
store.save("_main", the_mesh.self_id);
identity_ready = mesh::generateUsableLocalIdentity(the_mesh.self_id, radio_new_identity);
if (identity_ready) store.save("_main", the_mesh.self_id);
}
#if defined(ESP32_PLATFORM)
mesh::discardESP32TrueRandom();
#endif
if (!identity_ready) {
MESH_DEBUG_PRINTLN("Identity generation exhausted all attempts; rebooting");
board.reboot();
return;
}
// Print the running firmware version at boot so it's visible after an OTA
+20 -7
View File
@@ -1,8 +1,13 @@
#include <Arduino.h> // needed for PlatformIO
#include <Mesh.h>
#include <helpers/IdentityGeneration.h>
#include "MyMesh.h"
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#endif
#ifdef ETHERNET_ENABLED
#define ETHERNET_CLI_BANNER "MeshCore Room Server CLI"
#include <helpers/nrf52/EthernetCLI.h>
@@ -78,13 +83,21 @@ void setup() {
#else
#error "need to define filesystem"
#endif
if (!store.load("_main", the_mesh.self_id)) {
the_mesh.self_id = radio_new_identity(); // create new random identity
int count = 0;
while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
the_mesh.self_id = radio_new_identity(); count++;
}
store.save("_main", the_mesh.self_id);
const bool needs_identity = !store.load("_main", the_mesh.self_id)
|| mesh::hasReservedIdentityPrefix(the_mesh.self_id);
bool identity_ready = true;
if (needs_identity) {
identity_ready = mesh::generateUsableLocalIdentity(the_mesh.self_id, radio_new_identity);
if (identity_ready) store.save("_main", the_mesh.self_id);
}
#if defined(ESP32_PLATFORM)
mesh::discardESP32TrueRandom();
#endif
if (!identity_ready) {
MESH_DEBUG_PRINTLN("Identity generation exhausted all attempts; rebooting");
board.reboot();
return;
}
Serial.print("Room ID: ");
+24 -7
View File
@@ -10,9 +10,13 @@
#endif
#include <helpers/ArduinoHelpers.h>
#include <helpers/IdentityGeneration.h>
#include <helpers/StaticPoolPacketManager.h>
#include <helpers/SimpleMeshTables.h>
#include <helpers/IdentityStore.h>
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#endif
#include <RTClib.h>
#include <target.h>
@@ -310,7 +314,10 @@ public:
#else
IdentityStore store(fs, "/identity");
#endif
if (!store.load("_main", self_id, _prefs.node_name, sizeof(_prefs.node_name))) { // legacy: node_name was from identity file
const bool needs_identity = !store.load("_main", self_id, _prefs.node_name, sizeof(_prefs.node_name))
|| mesh::hasReservedIdentityPrefix(self_id); // legacy: node_name was from identity file
bool identity_ready = true;
if (needs_identity) {
// Need way to get some entropy to seed RNG
Serial.println("Press ENTER to generate key:");
char c = 0;
@@ -319,12 +326,22 @@ public:
}
((StdRNG *)getRNG())->begin(millis());
self_id = mesh::LocalIdentity(getRNG()); // create new random identity
int count = 0;
while (count < 10 && (self_id.pub_key[0] == 0x00 || self_id.pub_key[0] == 0xFF)) { // reserved id hashes
self_id = mesh::LocalIdentity(getRNG()); count++;
}
store.save("_main", self_id);
#if defined(ESP32_PLATFORM)
identity_ready = mesh::generateUsableLocalIdentity(self_id, radio_new_identity);
#else
identity_ready = mesh::generateUsableLocalIdentity(
self_id, [this]() { return mesh::LocalIdentity(getRNG()); });
#endif
if (identity_ready) store.save("_main", self_id);
}
#if defined(ESP32_PLATFORM)
mesh::discardESP32TrueRandom();
#endif
if (!identity_ready) {
MESH_DEBUG_PRINTLN("Identity generation exhausted all attempts; rebooting");
board.reboot();
return;
}
// load persisted prefs
+20 -7
View File
@@ -1,4 +1,9 @@
#include "SensorMesh.h"
#include <helpers/IdentityGeneration.h>
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#endif
#ifdef DISPLAY_CLASS
#include "UITask.h"
@@ -100,14 +105,22 @@ void setup() {
#else
#error "need to define filesystem"
#endif
if (!store.load("_main", the_mesh.self_id)) {
const bool needs_identity = !store.load("_main", the_mesh.self_id)
|| mesh::hasReservedIdentityPrefix(the_mesh.self_id);
bool identity_ready = true;
if (needs_identity) {
MESH_DEBUG_PRINTLN("Generating new keypair");
the_mesh.self_id = radio_new_identity(); // create new random identity
int count = 0;
while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
the_mesh.self_id = radio_new_identity(); count++;
}
store.save("_main", the_mesh.self_id);
identity_ready = mesh::generateUsableLocalIdentity(the_mesh.self_id, radio_new_identity);
if (identity_ready) store.save("_main", the_mesh.self_id);
}
#if defined(ESP32_PLATFORM)
mesh::discardESP32TrueRandom();
#endif
if (!identity_ready) {
MESH_DEBUG_PRINTLN("Identity generation exhausted all attempts; rebooting");
board.reboot();
return;
}
Serial.print("Sensor ID: ");