Controller B OTA: link_peer (STATUS to A, PAIR from the wire, AES-GCM SECURE frames), updater ported from A, console, CI workflow, shared header v2 maintenance frames; wiki + docs
Firmware / build (push) Successful in 2m57s
Firmware / build (push) Successful in 2m57s
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* ota_config.h - where Controller B looks for firmware updates, and what it trusts.
|
||||
*
|
||||
* Same scheme as Controller A (ESP32-S3-Touch-LCD-5B/include/config/ota_config.h):
|
||||
* releases of THIS repo on Gitea, manifest signed with the shared project key,
|
||||
* target string "patrol-b" so an A image is never accepted here.
|
||||
* See wiki/pages/controller-b-ota.md.
|
||||
*
|
||||
* Nothing in here is secret. The signing private key lives only in the Gitea
|
||||
* secret BMW_NISSAN_FIRMWARE_SIGNING_KEY.
|
||||
*/
|
||||
|
||||
#ifndef OTA_CONFIG_H
|
||||
#define OTA_CONFIG_H
|
||||
|
||||
// Version string baked in by CI (-DFW_VERSION="v1.2.3"); local builds say "dev".
|
||||
#ifndef FW_VERSION
|
||||
#define FW_VERSION "dev"
|
||||
#endif
|
||||
|
||||
#define OTA_TARGET "patrol-b"
|
||||
|
||||
#define OTA_SERVER "https://git.infra.msai.al"
|
||||
#define OTA_REPO "art/ESP32-S3-LCD-1.3"
|
||||
#define OTA_LATEST_API_URL OTA_SERVER "/api/v1/repos/" OTA_REPO "/releases/latest"
|
||||
#define OTA_DOWNLOAD_BASE OTA_SERVER "/" OTA_REPO "/releases/download/"
|
||||
|
||||
// A fresh image must run this long before it is confirmed; a reset before that
|
||||
// makes the bootloader fall back to the previous slot.
|
||||
#define OTA_CONFIRM_MS 30000
|
||||
|
||||
// TLS trust: Let's Encrypt roots (ISRG Root X1 + ISRG Root YR), copied from A.
|
||||
static const char OTA_CA_PEM[] =
|
||||
// ISRG Root X1
|
||||
"-----BEGIN CERTIFICATE-----\n"
|
||||
"MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n"
|
||||
"T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3WnJEIt+oQvlYZbY8tpEroKrFB6PFGzrJIDRyts4HqvuH52RFj2zv/B\n"
|
||||
"AgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1Ud\n"
|
||||
"DgQWBBTe51tg0CJtQCh9Pw0B/qS1UrRRlDANBgkqhkiG9w0BAQsFAAOCAgEAWHnf\n"
|
||||
"713Bdkq7t5yN2dNIgQakUb94X9WuyhMEHHkgx4oDpSUlnG0w4g94MoqaEUE31ZjR\n"
|
||||
"LU7L5LD1g9ujFHTQu8AD215AHMVQFbm6j8hQxdXHAzDajFNQnOlDJrLjzIx176oy\n"
|
||||
"AjvUtejZx2NNmdb5fd0WGVGsCdoAJ3N8ozo7ajE8t6vfxStZb4BQ9WYJGHUDrv2N\n"
|
||||
"i5tJF6CNiPnlzs3BUfECRbE4JSk+jvy8+VoGiFE8qsH/j78x2fjgQhAQFV7P7Zxy\n"
|
||||
"dBTZ1wEkNpZNW2qnaK1SKBLa+xf6E06YRIq5uaI+HWH8SY1y5VbRgzq40EKg3yxP\n"
|
||||
"06fz+uYAUIFJoLNfhwRCc3Q6pQVuMX3yAjHAes4gk4moGcLQ5p7HAh39yeylZc1J\n"
|
||||
"41sx/jKwLIkPE6Rr1Nf4pxdsxf9SA4yOEiAkDgq04DVxn8hgYFdUtBCuiuVC2heA\n"
|
||||
"EiqVEa+8QZjuw8Gj0EbHXcRd1nInvGqRS1o9Is7YBdQN57X1AYveGBNNqjICSb7c\n"
|
||||
"awuw1EawTDrs13VUlJVEsbQ0/O/1aaV73mCdOQ8azqL2KTv1Ewu1xbquE2S+kdQU\n"
|
||||
"To9TUwat3wUA6cwXh1EfpS/3fJ0aGah5hdpRyoCLDlsSn8tkrjMfFFX0viC+GxHc\n"
|
||||
"sI1ANRYvqSFC2X1VRZfDg+wD6E21BccmifG4yWc=\n"
|
||||
"-----END CERTIFICATE-----\n";
|
||||
|
||||
// Firmware signing public key (ECDSA P-256), same key as Controller A.
|
||||
static const char OTA_SIGNING_PUBKEY_PEM[] =
|
||||
"-----BEGIN PUBLIC KEY-----\n"
|
||||
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE496tys+NeIrdDLNaGHLzr6L4oGmF\n"
|
||||
"Y9brIfDTgn8IOWBm8J4nmLUef7zU4ydPM5BKXqUrTshJEpLd74GJIIOtkg==\n"
|
||||
"-----END PUBLIC KEY-----\n";
|
||||
|
||||
#endif // OTA_CONFIG_H
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Serial console (Controller B) - line reader + command chain.
|
||||
*
|
||||
* Commands live in their modules (LinkPeer, Ota); this only splits lines and
|
||||
* asks each handler in turn. `help` lists them.
|
||||
*/
|
||||
|
||||
#ifndef CONSOLE_H
|
||||
#define CONSOLE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace Console {
|
||||
|
||||
typedef bool (*handler_t)(const char* line);
|
||||
|
||||
/** Register up to 8 handlers; each returns true if it consumed the line. */
|
||||
void addHandler(handler_t h);
|
||||
|
||||
/** Non-blocking: read whatever is pending on Serial, dispatch complete lines. */
|
||||
void tick();
|
||||
|
||||
} // namespace Console
|
||||
|
||||
#endif // CONSOLE_H
|
||||
@@ -7,6 +7,10 @@
|
||||
* Channel discovery: A transmits on ESPNOW_CHANNEL unless it is associated to
|
||||
* an access point. B listens on ESPNOW_CHANNEL first and, while it hears
|
||||
* nothing, hops through channels 1..13 until frames arrive, then locks.
|
||||
*
|
||||
* Frames other than DATA/CONFIG (the SECURE envelope from A) are routed to
|
||||
* LinkPeer, and the sender MAC of A's frames is remembered so LinkPeer can
|
||||
* unicast STATUS frames back (sendTo).
|
||||
*/
|
||||
|
||||
#ifndef ESPNOW_RECEIVER_H
|
||||
@@ -31,6 +35,12 @@ bool isReady();
|
||||
uint8_t currentChannel();
|
||||
uint32_t framesReceived();
|
||||
|
||||
/** A's MAC as seen on its last DATA/CONFIG frame. False until A was heard by radio. */
|
||||
bool peerMac(uint8_t out[6]);
|
||||
|
||||
/** Unicast one complete frame to a MAC (peer added on first use, unencrypted). */
|
||||
bool sendTo(const uint8_t mac[6], const uint8_t* frame, size_t len);
|
||||
|
||||
} // namespace EspNowReceiver
|
||||
|
||||
#endif // ESPNOW_RECEIVER_H
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Link Peer Module (Controller B) - the maintenance side of the link to A.
|
||||
*
|
||||
* The data path (DATA/CONFIG frames) is UARTReceiver's. This module owns
|
||||
* everything else on the link, see wiki/pages/controller-b-ota.md:
|
||||
*
|
||||
* - STATUS frames B -> A every LINK_STATUS_PERIOD_MS (ESP-NOW unicast to A's
|
||||
* MAC, learned from the frames A broadcasts): our version, OTA phase, uptime.
|
||||
* - PAIR frames from A, accepted from the UART wire ONLY: the shared key that
|
||||
* protects everything below. Kept in NVS, survives reflashes.
|
||||
* - SECURE frames from A (ESP-NOW unicast, AES-256-GCM under the pairing key):
|
||||
* WIFI_CREDS -> stored in NVS for the OTA module to join with
|
||||
* OTA_CMD -> handed to the OTA module (check / install / rollback)
|
||||
*
|
||||
* Console (`link ...`, `wifi ...`) via Console::handle chain.
|
||||
*/
|
||||
|
||||
#ifndef LINK_PEER_H
|
||||
#define LINK_PEER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "protocol/automotive_data.h"
|
||||
|
||||
namespace LinkPeer {
|
||||
|
||||
/** Load pairing key + Wi-Fi credentials from NVS. Call before the receivers start. */
|
||||
void init();
|
||||
|
||||
/** STATUS transmit + housekeeping. Call from loop(). */
|
||||
void tick();
|
||||
|
||||
/** ESP-NOW frames that are not DATA/CONFIG (EspNowReceiver hands them here). */
|
||||
void onRadioFrame(const uint8_t* mac, const uint8_t* frame, size_t len);
|
||||
|
||||
/** A PAIR payload that arrived on the UART wire (UARTReceiver hands it here). */
|
||||
void onPairFrame(const link_pair_t* p);
|
||||
|
||||
bool isPaired();
|
||||
uint16_t keyFingerprint(); // 0 when unpaired
|
||||
|
||||
/** Stored Wi-Fi credentials (from A). False if none. Never printed. */
|
||||
bool getWifiCredentials(char* ssid, size_t ssid_n, char* pass, size_t pass_n);
|
||||
bool hasWifiCredentials();
|
||||
void forgetWifi();
|
||||
void unpair();
|
||||
|
||||
// ---- OTA module integration ------------------------------------------------
|
||||
|
||||
typedef void (*ota_cmd_handler_t)(uint8_t cmd, uint8_t flags);
|
||||
void setOtaCommandHandler(ota_cmd_handler_t handler);
|
||||
|
||||
/** What the STATUS frame reports about the updater. */
|
||||
void setOtaStatus(uint8_t phase, uint8_t progress, const char* latest, const char* message,
|
||||
bool wifi_joined, bool pending_verify);
|
||||
|
||||
/** Console: `link status | unpair`, `wifi status | forget`. */
|
||||
bool handleCommand(const char* line);
|
||||
|
||||
} // namespace LinkPeer
|
||||
|
||||
#endif // LINK_PEER_H
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* ota_update.h - Controller B pulls its own firmware from this repo's Gitea
|
||||
* releases. Port of Controller A's module (ESP32-S3-Touch-LCD-5B/src/modules/
|
||||
* ota_update.cpp), see wiki/pages/controller-b-ota.md.
|
||||
*
|
||||
* Triggered by A (OTA_CMD inside a SECURE frame, behind A's parking gate and
|
||||
* its Updates screen) or by this console:
|
||||
*
|
||||
* ota status running version, slot, rollback state
|
||||
* ota check join Wi-Fi, ask Gitea for the latest release, leave
|
||||
* ota install [force] join, download, verify, flash idle slot, reboot
|
||||
* ota rollback boot the previous slot
|
||||
*
|
||||
* Safety, same as A:
|
||||
* - refused unless parked: link alive with engine rpm 0 and no shaft speed,
|
||||
* or link silent (ignition off / bench). A has already applied its gate.
|
||||
* - image must match the signed manifest (sha256 + ECDSA P-256 over
|
||||
* "patrol-b|version|sha256"); the idle slot is never activated otherwise.
|
||||
* - a new image must run OTA_CONFIRM_MS before it is marked valid, else the
|
||||
* bootloader falls back to the old slot.
|
||||
*
|
||||
* Wi-Fi: B lives in bare STA mode for ESP-NOW. For a job it joins the network
|
||||
* whose credentials A handed over (LinkPeer), then disconnects again so the
|
||||
* receiver can hop back to A's channel.
|
||||
*/
|
||||
|
||||
#ifndef OTA_UPDATE_H
|
||||
#define OTA_UPDATE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace Ota {
|
||||
|
||||
/** Log version/slot; flag an unconfirmed new image. Call early in setup(). */
|
||||
void init();
|
||||
|
||||
/** Runs queued jobs, confirms a fresh image after healthy uptime. Call from loop(). */
|
||||
void tick();
|
||||
|
||||
/** LinkPeer hands A's OTA commands here (registered by init()). */
|
||||
void onLinkCommand(uint8_t cmd, uint8_t flags);
|
||||
|
||||
/** Console entry point (`ota ...`). */
|
||||
bool handleCommand(const char* line);
|
||||
|
||||
const char* version();
|
||||
|
||||
} // namespace Ota
|
||||
|
||||
#endif // OTA_UPDATE_H
|
||||
@@ -16,6 +16,13 @@
|
||||
* - link_config_t: Patrol gauge calibration + alert thresholds, edited on A,
|
||||
* executed on B (tach + coolant PWM outputs)
|
||||
* - the same frames go out on UART1 and ESP-NOW; B dedupes by packet_id
|
||||
*
|
||||
* v2 additions (2026-09-27, PACKET_VERSION unchanged - DATA/CONFIG are the same
|
||||
* bytes, an older B counts the new types as FRAME_ERR_TYPE and carries on):
|
||||
* - STATUS B -> A (ESP-NOW unicast): B's version, OTA phase, uptime
|
||||
* - PAIR A -> B (UART wire ONLY): the shared secret for SECURE frames
|
||||
* - SECURE A -> B (ESP-NOW unicast): AES-256-GCM envelope carrying an
|
||||
* OTA_CMD or WIFI_CREDS payload. See ESP32-S3-LCD-1.3/wiki/pages/controller-b-ota.md
|
||||
*/
|
||||
|
||||
#ifndef _AUTOMOTIVE_DATA_H
|
||||
@@ -53,12 +60,19 @@
|
||||
#define PACKET_END_BYTE 0x55
|
||||
#define PACKET_VERSION 0x02
|
||||
|
||||
#define PACKET_TYPE_DATA 0x01 // payload = automotive_data_t
|
||||
#define PACKET_TYPE_CONFIG 0x02 // payload = link_config_t
|
||||
#define PACKET_TYPE_DATA 0x01 // A -> B payload = automotive_data_t
|
||||
#define PACKET_TYPE_CONFIG 0x02 // A -> B payload = link_config_t
|
||||
#define PACKET_TYPE_STATUS 0x03 // B -> A payload = link_status_t (ESP-NOW unicast)
|
||||
#define PACKET_TYPE_PAIR 0x04 // A -> B payload = link_pair_t (UART wire only, never radio)
|
||||
#define PACKET_TYPE_SECURE 0x05 // A -> B payload = secure envelope, see LINK MAINTENANCE below
|
||||
// Inner types carried inside a SECURE envelope (never on the wire in the clear):
|
||||
#define LINK_INNER_OTA_CMD 0x11 // link_ota_cmd_t
|
||||
#define LINK_INNER_WIFI_CREDS 0x12 // link_wifi_creds_t
|
||||
|
||||
#define PACKET_HEADER_SIZE 4 // start, version, type, length
|
||||
#define PACKET_TRAILER_SIZE 2 // checksum, end
|
||||
#define PACKET_MAX_PAYLOAD 64
|
||||
#define PACKET_MAX_PAYLOAD 160 // was 64 until 2026-09-27; SECURE(WIFI_CREDS) needs 127.
|
||||
// Whole frame 166 B < ESP-NOW's 250 B limit.
|
||||
#define PACKET_MAX_SIZE (PACKET_HEADER_SIZE + PACKET_MAX_PAYLOAD + PACKET_TRAILER_SIZE)
|
||||
|
||||
// ============================================================================
|
||||
@@ -201,6 +215,88 @@ static inline void link_config_defaults(link_config_t* c) {
|
||||
c->battery_low_x100 = 1100;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// LINK MAINTENANCE FRAMES (2026-09-27) - Controller B firmware updates
|
||||
// ============================================================================
|
||||
//
|
||||
// B pulls its own signed release over Wi-Fi; A only hands it the credentials
|
||||
// and the trigger. Both repos are public, so nothing secret is compiled in:
|
||||
// the shared key is generated on A at pairing time and travels to B over the
|
||||
// UART wire only. Everything A later sends B by radio that matters (Wi-Fi
|
||||
// credentials, OTA commands) goes inside a SECURE envelope under that key.
|
||||
// STATUS frames carry nothing secret and go in the clear.
|
||||
|
||||
#define LINK_KEY_LEN 32 // AES-256
|
||||
#define LINK_NONCE_LEN 12 // GCM IV, random per frame
|
||||
#define LINK_TAG_LEN 16 // GCM tag
|
||||
#define LINK_STATUS_PERIOD_MS 5000 // B -> A STATUS cadence
|
||||
#define LINK_STATUS_TIMEOUT_MS 15000 // A: B "not heard" after this
|
||||
|
||||
// --- PAIR (A -> B, wire only) -----------------------------------------------
|
||||
#define LINK_PAIR_MAGIC 0x52494150UL // "PAIR"
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint32_t magic; // LINK_PAIR_MAGIC - guards against a torn frame
|
||||
uint8_t key[LINK_KEY_LEN]; // the shared secret; both sides keep it in NVS
|
||||
uint8_t a_mac[6]; // A's STA MAC: the only sender B accepts SECURE frames from
|
||||
} link_pair_t; // 42 bytes
|
||||
|
||||
// --- STATUS (B -> A, ESP-NOW unicast, every LINK_STATUS_PERIOD_MS) ----------
|
||||
// ota_phase mirrors Controller A's Ota::phase_t so one UI renders both.
|
||||
#define LINK_OTA_IDLE 0
|
||||
#define LINK_OTA_CHECKING 1
|
||||
#define LINK_OTA_UP_TO_DATE 2
|
||||
#define LINK_OTA_AVAILABLE 3
|
||||
#define LINK_OTA_INSTALLING 4
|
||||
#define LINK_OTA_FAILED 5
|
||||
|
||||
#define LINK_STATUS_PAIRED (1 << 0) // B holds a pairing key
|
||||
#define LINK_STATUS_HAVE_WIFI (1 << 1) // B holds Wi-Fi credentials
|
||||
#define LINK_STATUS_WIFI_JOINED (1 << 2) // B is currently on the network (during a job)
|
||||
#define LINK_STATUS_PENDING_VERIFY (1 << 3) // B runs a fresh image not yet confirmed
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
char version[16]; // B's firmware version ("dev" for local builds)
|
||||
uint8_t ota_phase; // LINK_OTA_*
|
||||
uint8_t ota_progress; // 0..100 while installing
|
||||
uint8_t flags; // LINK_STATUS_*
|
||||
uint8_t reserved;
|
||||
uint16_t key_fp; // first two bytes of SHA-256(pairing key); 0 = unpaired.
|
||||
// Lets A see "paired with MY key" vs "paired with an old one".
|
||||
uint32_t uptime_s;
|
||||
uint32_t free_heap; // internal heap free, bytes
|
||||
char latest[16]; // latest release tag B knows of ("" = never checked)
|
||||
char message[32]; // failure reason when ota_phase == LINK_OTA_FAILED
|
||||
} link_status_t; // 78 bytes
|
||||
|
||||
// --- OTA_CMD (inner, A -> B) ------------------------------------------------
|
||||
#define LINK_OTA_CMD_CHECK 1
|
||||
#define LINK_OTA_CMD_INSTALL 2
|
||||
#define LINK_OTA_CMD_ROLLBACK 3
|
||||
#define LINK_OTA_FORCE (1 << 0) // reinstall even if versions match
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t cmd; // LINK_OTA_CMD_*
|
||||
uint8_t flags; // LINK_OTA_FORCE
|
||||
uint32_t nonce; // random; B drops a repeat of a recently seen one
|
||||
} link_ota_cmd_t; // 6 bytes
|
||||
|
||||
// --- WIFI_CREDS (inner, A -> B) ---------------------------------------------
|
||||
#define LINK_WIFI_SET 1
|
||||
#define LINK_WIFI_FORGET 2
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t action; // LINK_WIFI_*
|
||||
char ssid[33];
|
||||
char pass[64];
|
||||
} link_wifi_creds_t; // 98 bytes
|
||||
|
||||
// --- SECURE envelope ---------------------------------------------------------
|
||||
// payload = nonce[12] | tag[16] | ciphertext
|
||||
// ciphertext = AES-256-GCM(key, nonce, plaintext = inner_type | inner_payload)
|
||||
// Helpers that do the encryption live in protocol/link_secure.h (mbedtls).
|
||||
#define LINK_SECURE_OVERHEAD (LINK_NONCE_LEN + LINK_TAG_LEN)
|
||||
#define LINK_SECURE_MAX_INNER (PACKET_MAX_PAYLOAD - LINK_SECURE_OVERHEAD) // 132: 1 + 98 fits
|
||||
|
||||
// ============================================================================
|
||||
// CAN BUS ID MAPPINGS (BMW-Specific)
|
||||
// ============================================================================
|
||||
@@ -413,6 +509,12 @@ static inline frame_status_t frame_parse(const uint8_t* f, size_t n,
|
||||
if (l != sizeof(automotive_data_t)) return FRAME_ERR_TYPE;
|
||||
} else if (t == PACKET_TYPE_CONFIG) {
|
||||
if (l != sizeof(link_config_t)) return FRAME_ERR_TYPE;
|
||||
} else if (t == PACKET_TYPE_STATUS) {
|
||||
if (l != sizeof(link_status_t)) return FRAME_ERR_TYPE;
|
||||
} else if (t == PACKET_TYPE_PAIR) {
|
||||
if (l != sizeof(link_pair_t)) return FRAME_ERR_TYPE;
|
||||
} else if (t == PACKET_TYPE_SECURE) {
|
||||
if (l < LINK_SECURE_OVERHEAD + 1) return FRAME_ERR_TYPE; // at least the inner type byte
|
||||
} else {
|
||||
return FRAME_ERR_TYPE;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* link_secure.h - the SECURE envelope of the A <-> B link (see automotive_data.h).
|
||||
*
|
||||
* THIS FILE MUST BE BYTE-IDENTICAL IN BOTH REPOS, like automotive_data.h.
|
||||
*
|
||||
* payload = nonce[12] | tag[16] | AES-256-GCM(key, nonce, inner_type | inner)
|
||||
*
|
||||
* Both cores ship mbedtls (2.28 on Controller B's IDF 4.4, 3.x on Controller
|
||||
* A's IDF 5.5); the GCM calls are the same, only SHA-256's name differs.
|
||||
*/
|
||||
|
||||
#ifndef LINK_SECURE_H
|
||||
#define LINK_SECURE_H
|
||||
|
||||
#include "automotive_data.h"
|
||||
#include <mbedtls/gcm.h>
|
||||
#include <mbedtls/sha256.h>
|
||||
#include <mbedtls/version.h>
|
||||
#if __has_include(<esp_random.h>)
|
||||
#include <esp_random.h>
|
||||
#else
|
||||
#include <esp_system.h> // IDF 4.4: esp_random / esp_fill_random live here
|
||||
#endif
|
||||
|
||||
/** First two bytes of SHA-256(key), never 0 (0 means "unpaired" on the wire). */
|
||||
static inline uint16_t link_key_fingerprint(const uint8_t key[LINK_KEY_LEN]) {
|
||||
uint8_t h[32];
|
||||
#if MBEDTLS_VERSION_MAJOR >= 3
|
||||
mbedtls_sha256(key, LINK_KEY_LEN, h, 0);
|
||||
#else
|
||||
mbedtls_sha256_ret(key, LINK_KEY_LEN, h, 0);
|
||||
#endif
|
||||
uint16_t fp = (uint16_t)((h[0] << 8) | h[1]);
|
||||
return fp ? fp : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seal inner_type + inner into a SECURE payload.
|
||||
* @param out buffer of PACKET_MAX_PAYLOAD bytes
|
||||
* @return payload length, or 0 on error / oversize
|
||||
*/
|
||||
static inline size_t link_secure_seal(const uint8_t key[LINK_KEY_LEN], uint8_t inner_type,
|
||||
const void* inner, size_t inner_len, uint8_t* out) {
|
||||
if (inner_len + 1 > LINK_SECURE_MAX_INNER) return 0;
|
||||
uint8_t plain[LINK_SECURE_MAX_INNER];
|
||||
plain[0] = inner_type;
|
||||
memcpy(&plain[1], inner, inner_len);
|
||||
size_t n = inner_len + 1;
|
||||
|
||||
uint8_t* nonce = out;
|
||||
uint8_t* tag = out + LINK_NONCE_LEN;
|
||||
uint8_t* ct = out + LINK_SECURE_OVERHEAD;
|
||||
esp_fill_random(nonce, LINK_NONCE_LEN);
|
||||
|
||||
mbedtls_gcm_context g;
|
||||
mbedtls_gcm_init(&g);
|
||||
int rc = mbedtls_gcm_setkey(&g, MBEDTLS_CIPHER_ID_AES, key, LINK_KEY_LEN * 8);
|
||||
if (rc == 0) {
|
||||
rc = mbedtls_gcm_crypt_and_tag(&g, MBEDTLS_GCM_ENCRYPT, n, nonce, LINK_NONCE_LEN,
|
||||
NULL, 0, plain, ct, LINK_TAG_LEN, tag);
|
||||
}
|
||||
mbedtls_gcm_free(&g);
|
||||
return rc == 0 ? LINK_SECURE_OVERHEAD + n : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a SECURE payload. On success *inner_type and inner (LINK_SECURE_MAX_INNER
|
||||
* bytes) are filled; *inner_len excludes the type byte.
|
||||
*/
|
||||
static inline bool link_secure_open(const uint8_t key[LINK_KEY_LEN], const uint8_t* payload, size_t len,
|
||||
uint8_t* inner_type, uint8_t* inner, size_t* inner_len) {
|
||||
if (len < LINK_SECURE_OVERHEAD + 1 || len > PACKET_MAX_PAYLOAD) return false;
|
||||
size_t n = len - LINK_SECURE_OVERHEAD;
|
||||
uint8_t plain[LINK_SECURE_MAX_INNER];
|
||||
|
||||
mbedtls_gcm_context g;
|
||||
mbedtls_gcm_init(&g);
|
||||
int rc = mbedtls_gcm_setkey(&g, MBEDTLS_CIPHER_ID_AES, key, LINK_KEY_LEN * 8);
|
||||
if (rc == 0) {
|
||||
rc = mbedtls_gcm_auth_decrypt(&g, n, payload, LINK_NONCE_LEN, NULL, 0,
|
||||
payload + LINK_NONCE_LEN, LINK_TAG_LEN,
|
||||
payload + LINK_SECURE_OVERHEAD, plain);
|
||||
}
|
||||
mbedtls_gcm_free(&g);
|
||||
if (rc != 0) return false;
|
||||
|
||||
*inner_type = plain[0];
|
||||
*inner_len = n - 1;
|
||||
memcpy(inner, &plain[1], n - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // LINK_SECURE_H
|
||||
Reference in New Issue
Block a user