fix: remove hardcoded keys and load them from NVS + script + key reset

This commit is contained in:
Alexis Fourmaux 2026-05-13 20:34:54 +02:00
parent 7a07bb2522
commit 20fa0bc034
7 changed files with 31 additions and 36 deletions

3
.gitignore vendored
View file

@ -3,3 +3,6 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
secrets.csv
nvs_secrets.bin

4
flash_secrets.sh Executable file
View file

@ -0,0 +1,4 @@
# Si nécessaire, activer le venv de platformio : source ~/.platformio/penv/bin/activate
pio pkg exec -p "tool-esptoolpy" -- python -m esp_idf_nvs_partition_gen generate secrets.csv nvs_secrets.bin 0x6000
pio pkg exec -p "tool-esptoolpy" -- esptool.py write_flash 0x9000 nvs_secrets.bin

View file

@ -2,19 +2,19 @@
RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE];
LoRaTransmitter::LoRaTransmitter(
uint64_t joinEUI,
uint64_t devEUI,
uint8_t* nwkKey,
uint8_t* appKey
)
: _joinEUI(joinEUI),
_devEUI(devEUI),
_radio(new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1)),
LoRaTransmitter::LoRaTransmitter()
: _radio(new Module(LORA_NSS, LORA_DIO0, LORA_RST, LORA_DIO1)),
_node(&_radio, &EU868)
{
memcpy(_nwkKey, nwkKey, 16);
memcpy(_appKey, appKey, 16);
_prefs.begin("lorawan", true);
_joinEUI = _prefs.getULong64("join_eui", 0);
_devEUI = _prefs.getULong64("dev_eui", 0);
_prefs.getBytes("nwk_key", _nwkKey, 16);
_prefs.getBytes("app_key", _appKey, 16);
_prefs.end();
memset(_noncesBuffer, 0, RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
}

View file

@ -15,12 +15,7 @@
class LoRaTransmitter : public ITransmitter {
public:
LoRaTransmitter(
uint64_t joinEUI,
uint64_t devEUI,
uint8_t* nwkKey,
uint8_t* appKey
);
LoRaTransmitter();
TransmitError init() override;
TransmitError join() override;

6
secrets.csv.example Normal file
View file

@ -0,0 +1,6 @@
key,type,encoding,value
lorawan,namespace,,
join_eui,data,hex2bin,0011223344556677
dev_eui,data,hex2bin,0011223344556677
app_key,data,hex2bin,AABBCCDDEEFF00112233445566778899
nwk_key,data,hex2bin,AABBCCDDEEFF00112233445566778899

View file

@ -1,13 +0,0 @@
#pragma once
#define MAX_RESTART 1
#define MAX_FATAL_RESTART 3
uint64_t joinEUI = 0x911b5c62b4dc2079;
uint64_t devEUI = 0x0586fe41112d83d9;
uint8_t appKey[] = {
0x81, 0x24, 0xbe, 0x86, 0x59, 0x06, 0xa4, 0x4f,
0xe7, 0x39, 0xd6, 0x0f, 0xd6, 0x88, 0x91, 0xcc};
uint8_t nwkKey[] = {
0xb8, 0x30, 0x20, 0xf2, 0x52, 0x3b, 0xd2, 0xb0,
0x1d, 0x12, 0xe1, 0xb9, 0xe7, 0xe1, 0xb0, 0xde};

View file

@ -4,15 +4,13 @@
#include <GazDisplay.h>
#include "config.h"
#define MAX_RESTART 1
#define MAX_FATAL_RESTART 3
RTC_DATA_ATTR uint8_t fatalRestartCount = 0;
RTC_DATA_ATTR uint8_t restartCount = 0;
ITransmitter* transmitter = new LoRaTransmitter(
joinEUI,
devEUI,
nwkKey,
appKey
);
ITransmitter* transmitter;
void fatalError(const char* msg){
Serial.printf("[FATAL] %s\n", msg);
@ -57,6 +55,8 @@ void setup() {
Serial.begin(115200);
delay(2000);
transmitter = new LoRaTransmitter();
if (displayInit() != DisplayError::OK){
nonCriticalError("Initialisation du périphérique d'affichage échouée");
}