From 178ebf7282ce0c3722419d2c67496768c81f394c Mon Sep 17 00:00:00 2001 From: Matthias Wientapper Date: Sat, 25 Oct 2025 13:20:04 +0200 Subject: [PATCH 1/2] * Add cli command to regenerate key pair --- examples/simple_repeater/MyMesh.cpp | 13 +++++++++++++ examples/simple_repeater/MyMesh.h | 1 + src/helpers/CommonCLI.cpp | 5 +++++ src/helpers/CommonCLI.h | 1 + 4 files changed, 20 insertions(+) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index f328c752e..2617b2cfd 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -807,6 +807,19 @@ void MyMesh::clearStats() { ((SimpleMeshTables *)getTables())->resetStats(); } +void MyMesh::regenerateKeys() { + MESH_DEBUG_PRINTLN("Generating new keypair"); + mesh::LocalIdentity new_id = radio_new_identity(); + + int count = 0; + while (count < 10 && (new_id.pub_key[0] == 0x00 || new_id.pub_key[0] == 0xFF)) { + new_id = radio_new_identity(); + count++; + } + + saveIdentity(new_id); +} + void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) { while (*command == ' ') command++; // skip leading spaces diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index a9ab251ee..60629dc65 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -188,6 +188,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { void saveIdentity(const mesh::LocalIdentity& new_id) override; void clearStats() override; + void regenerateKeys() override; void handleCommand(uint32_t sender_timestamp, char* command, char* reply); void loop(); diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index b8bb698a5..365eb148d 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -245,6 +245,11 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else if (memcmp(command, "clear stats", 11) == 0) { _callbacks->clearStats(); strcpy(reply, "(OK - stats reset)"); + } else if (memcmp(command, "regeneratekeys", 14) == 0) { + // regenerate key pair + MESH_DEBUG_PRINTLN("Generating new keypair"); + _callbacks->regenerateKeys(); + _board->reboot(); // doesn't return /* * GET commands */ diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index ea59aa92d..e2b6b63ee 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -69,6 +69,7 @@ class CommonCLICallbacks { virtual mesh::LocalIdentity& getSelfId() = 0; virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0; virtual void clearStats() = 0; + virtual void regenerateKeys() = 0; virtual void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) = 0; virtual void setBridgeState(bool enable) { From 82242052f978606456633cb9fea62ee12afd08ae Mon Sep 17 00:00:00 2001 From: Matthias Wientapper Date: Sun, 26 Oct 2025 20:11:30 +0100 Subject: [PATCH 2/2] * Regenerate keys with public key matching user defined first byte --- examples/simple_repeater/MyMesh.cpp | 21 ++++++++++----------- examples/simple_repeater/MyMesh.h | 2 +- src/helpers/CommonCLI.cpp | 24 +++++++++++++++++++++--- src/helpers/CommonCLI.h | 2 +- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 2617b2cfd..4879ec8d8 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -807,17 +807,16 @@ void MyMesh::clearStats() { ((SimpleMeshTables *)getTables())->resetStats(); } -void MyMesh::regenerateKeys() { - MESH_DEBUG_PRINTLN("Generating new keypair"); - mesh::LocalIdentity new_id = radio_new_identity(); - - int count = 0; - while (count < 10 && (new_id.pub_key[0] == 0x00 || new_id.pub_key[0] == 0xFF)) { - new_id = radio_new_identity(); - count++; - } - - saveIdentity(new_id); +void MyMesh::regenerateKeys(uint8_t byte) { + if (byte >0 && byte < 0xff){ + MESH_DEBUG_PRINTLN("Generating new keypair"); + mesh::LocalIdentity new_id = radio_new_identity(); + + while (new_id.pub_key[0] != byte) { + new_id = radio_new_identity(); + } + saveIdentity(new_id); + } } void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) { diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 60629dc65..a1291b914 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -188,7 +188,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { void saveIdentity(const mesh::LocalIdentity& new_id) override; void clearStats() override; - void regenerateKeys() override; + void regenerateKeys(uint8_t byte); void handleCommand(uint32_t sender_timestamp, char* command, char* reply); void loop(); diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 365eb148d..6ee29b196 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -245,11 +245,29 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch } else if (memcmp(command, "clear stats", 11) == 0) { _callbacks->clearStats(); strcpy(reply, "(OK - stats reset)"); - } else if (memcmp(command, "regeneratekeys", 14) == 0) { + } else if (memcmp(command, "regeneratekeys ", 15) == 0) { + // Parse first hex digit + int value = 0; + if (command[15] >= '0' && command[15] <= '9') + value = (command[15] - '0') << 4; + else if (command[15] >= 'a' && command[15] <= 'f') + value = (command[15] - 'a' + 10) << 4; + else if (command[15] >= 'A' && command[15] <= 'F') + value = (command[15] - 'A' + 10) << 4; + // Parse second hex digit + if (command[16] >= '0' && command[16] <= '9') + value |= (command[16] - '0'); + else if (command[16] >= 'a' && command[16] <= 'f') + value |= (command[16] - 'a' + 10); + else if (command[16] >= 'A' && command[16] <= 'F') + value |= (command[16] - 'A' + 10); // regenerate key pair MESH_DEBUG_PRINTLN("Generating new keypair"); - _callbacks->regenerateKeys(); - _board->reboot(); // doesn't return + if ((value > 0) && (value < 0xff)){ + _callbacks->regenerateKeys(value); + _board->reboot(); // doesn't return + } + sprintf(reply, "> ERROR"); /* * GET commands */ diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index e2b6b63ee..be1438206 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -69,7 +69,7 @@ class CommonCLICallbacks { virtual mesh::LocalIdentity& getSelfId() = 0; virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0; virtual void clearStats() = 0; - virtual void regenerateKeys() = 0; + virtual void regenerateKeys(uint8_t byte) = 0; virtual void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) = 0; virtual void setBridgeState(bool enable) {