Replace compile-time HMAC key with USB provisioning

ESP firmware:
- Store HMAC key in NVS via Preferences library instead of compile-time
  hmac_key.h
- KEY? responds with HMAC_KEY:<hex> (if provisioned) or NO_KEY
- New SET_KEY:<64-hex> command stores key in NVS and responds OK
- Boot messages updated to reflect provisioning state

Android app:
- UsbKeyReceiver detects NO_KEY response and auto-provisions:
  generates random 32-byte key via SecureRandom, sends SET_KEY,
  waits for OK, saves key to SharedPreferences
- Read loop now breaks on both HMAC_KEY: and NO_KEY responses

Cleanup:
- Removed tools/generate_hmac_key.py and src/hmac_key.h
- Removed hmac_key.h from .gitignore
- README updated for new provisioning flow
This commit is contained in:
eddy 2026-07-26 09:30:21 +02:00
commit 9a84b05b8a
5 changed files with 155 additions and 116 deletions

View file

@ -1,53 +0,0 @@
#!/usr/bin/env python3
"""Generate src/hmac_key.h with a random 32-byte HMAC-SHA256 key.
Usage:
python3 tools/generate_hmac_key.py
This creates src/hmac_key.h (gitignored) and prints the hex key to stdout.
The hex key should be entered in the Android app's Settings screen.
"""
import os
import sys
import binascii
KEY_LENGTH = 32
OUTPUT_FILE = os.path.join("src", "hmac_key.h")
def generate():
key = os.urandom(KEY_LENGTH)
hex_key = binascii.hexlify(key).decode().upper()
header = f"""#ifndef HMAC_KEY_H
#define HMAC_KEY_H
// AUTO-GENERATED by tools/generate_hmac_key.py
// Do NOT commit this file. Do NOT edit by hand.
// To regenerate: python3 tools/generate_hmac_key.py
const uint8_t HMAC_KEY[{KEY_LENGTH}] = {{
{', '.join(f'0x{b:02x}' for b in key)}
}};
#endif // HMAC_KEY_H
"""
os.makedirs("src", exist_ok=True)
with open(OUTPUT_FILE, "w") as f:
f.write(header)
print(f"HMAC key written to {OUTPUT_FILE}")
print(f"Enter this key in the Android app Settings:")
print(f" {hex_key}")
print()
print(f"ESP serial output after boot will show the same key.")
if __name__ == "__main__":
if os.path.exists(OUTPUT_FILE):
overwrite = input(f"{OUTPUT_FILE} already exists. Overwrite? [y/N] ")
if overwrite.lower() != 'y':
print("Aborted.")
sys.exit(0)
generate()