diff --git a/src/mesh/CryptoEngine.cpp b/src/mesh/CryptoEngine.cpp index d32b73855..82d0a9f57 100644 --- a/src/mesh/CryptoEngine.cpp +++ b/src/mesh/CryptoEngine.cpp @@ -3,12 +3,17 @@ #include "architecture.h" #if !(MESHTASTIC_EXCLUDE_PKI) +#include "NodeDB.h" #include "aes-ccm.h" #include "meshUtils.h" #include #include +#include #include #if !(MESHTASTIC_EXCLUDE_PKI_KEYGEN) +#if !defined(ARCH_STM32WL) +#define CryptRNG RNG +#endif /** * Create a public/private key pair with Curve25519. @@ -18,6 +23,14 @@ */ void CryptoEngine::generateKeyPair(uint8_t *pubKey, uint8_t *privKey) { + // Mix in any randomness we can, to make key generation stronger. + CryptRNG.begin(optstr(APP_VERSION)); + if (myNodeInfo.device_id.size == 16) { + CryptRNG.stir(myNodeInfo.device_id.bytes, myNodeInfo.device_id.size); + } + auto noise = random(); + CryptRNG.stir((uint8_t *)&noise, sizeof(noise)); + LOG_DEBUG("Generate Curve25519 keypair"); Curve25519::dh1(public_key, private_key); memcpy(pubKey, public_key, sizeof(public_key)); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index cec39be4f..27c02f18b 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -261,7 +261,7 @@ NodeDB::NodeDB() #if !(MESHTASTIC_EXCLUDE_PKI_KEYGEN || MESHTASTIC_EXCLUDE_PKI) - if (!owner.is_licensed) { + if (!owner.is_licensed && config.lora.region != meshtastic_Config_LoRaConfig_RegionCode_UNSET) { bool keygenSuccess = false; if (config.security.private_key.size == 32) { if (crypto->regeneratePublicKey(config.security.public_key.bytes, config.security.private_key.bytes)) { diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 14fc9b3cd..385246000 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -671,6 +671,24 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c) config.lora = c.payload_variant.lora; // If we're setting region for the first time, init the region if (isRegionUnset && config.lora.region > meshtastic_Config_LoRaConfig_RegionCode_UNSET) { + if (!owner.is_licensed) { + bool keygenSuccess = false; + if (config.security.private_key.size == 32) { + if (crypto->regeneratePublicKey(config.security.public_key.bytes, config.security.private_key.bytes)) { + keygenSuccess = true; + } + } else { + LOG_INFO("Generate new PKI keys"); + crypto->generateKeyPair(config.security.public_key.bytes, config.security.private_key.bytes); + keygenSuccess = true; + } + if (keygenSuccess) { + config.security.public_key.size = 32; + config.security.private_key.size = 32; + owner.public_key.size = 32; + memcpy(owner.public_key.bytes, config.security.public_key.bytes, 32); + } + } config.lora.tx_enabled = true; initRegion(); if (myRegion->dutyCycle < 100) {