2026-06-28 04:32:18 +08:00
|
|
|
|
/*
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* protocol.h - OTA 握手辅助(nonce 扰码 + 会话密钥生命周期)
|
2026-05-31 03:20:26 +08:00
|
|
|
|
*
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* 本模块刻意保持轻量(原「帧格式 / CRC16 / AES-CBC」中间件已迁出):
|
|
|
|
|
|
* - 不负责 SPI Link v2 帧 / CRC32
|
|
|
|
|
|
* - 不负责 AES 加解密(OTA 数据包 AES-ECB 在 ota_relay 内)
|
|
|
|
|
|
* - 仅提供:RNG 扰码常数、server nonce 生成、session key 存清
|
|
|
|
|
|
*
|
|
|
|
|
|
* AES 主密钥 MASTER_KEY[] 见 app_config.h。
|
2026-04-27 00:42:26 +08:00
|
|
|
|
*/
|
|
|
|
|
|
#ifndef PROTOCOL_H
|
|
|
|
|
|
#define PROTOCOL_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2026-07-16 23:00:15 +08:00
|
|
|
|
#include <stddef.h>
|
2026-04-27 00:42:26 +08:00
|
|
|
|
#include "app_config.h"
|
|
|
|
|
|
|
2026-07-16 23:00:15 +08:00
|
|
|
|
/** @brief AES-128 / 会话密钥长度(字节)。 */
|
|
|
|
|
|
inline constexpr size_t SESSION_KEY_LEN = 16;
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-07-16 23:00:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief OTA 会话密钥上下文。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 仅缓存协商得到的 K_session;真正加解密在 ota_relay 中用该 key 做 AES-ECB。
|
|
|
|
|
|
*/
|
2026-04-27 00:42:26 +08:00
|
|
|
|
typedef struct {
|
2026-07-16 23:00:15 +08:00
|
|
|
|
uint8_t key[SESSION_KEY_LEN]; ///< 会话密钥 K_session(16 字节)
|
2026-04-27 00:42:26 +08:00
|
|
|
|
} session_ctx_t;
|
|
|
|
|
|
|
2026-05-05 04:48:31 +08:00
|
|
|
|
/**
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* @brief OTA 握手辅助类(单例)
|
2026-05-31 03:20:26 +08:00
|
|
|
|
*
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* 持有 per-boot 扰码常数 m_scrambleConst;
|
|
|
|
|
|
* sessionKeyInit / sessionKeyClear 为无状态静态工具,不依赖单例成员。
|
2026-05-05 04:48:31 +08:00
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
class Protocol {
|
|
|
|
|
|
public:
|
2026-06-15 01:14:29 +08:00
|
|
|
|
/**
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* @brief 获取 Protocol 单例实例。
|
2026-06-15 01:14:29 +08:00
|
|
|
|
* @return Protocol 单例引用。
|
|
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
static Protocol& instance();
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-06-15 01:14:29 +08:00
|
|
|
|
/**
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* @brief 初始化协议层(幂等)。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 使用硬件 RNG 填充 m_scrambleConst,供 scrambleGenerate 混合使用。
|
|
|
|
|
|
* 可重复调用;已初始化则直接返回。
|
2026-06-15 01:14:29 +08:00
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
void init();
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-06-15 01:14:29 +08:00
|
|
|
|
/**
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* @brief 生成基于时间戳、地址和随机种子的 16 字节 server nonce。
|
|
|
|
|
|
* @param timestamp 时间戳(如 FreeRTOS tick)。
|
|
|
|
|
|
* @param mem_addr 内存地址(增加熵,通常取栈/缓冲区地址)。
|
2026-06-15 01:14:29 +08:00
|
|
|
|
* @param rand_seed 随机种子。
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* @param nonce_out 输出缓冲区,至少 16 字节;为 nullptr 时直接返回。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @note 非标准 KDF,仅作 OTA 握手 nonce,勿当通用密钥派生。
|
|
|
|
|
|
* @note 若尚未 init(),内部会自动调用 init()。
|
2026-06-15 01:14:29 +08:00
|
|
|
|
*/
|
2026-06-16 23:17:06 +08:00
|
|
|
|
void scrambleGenerate(uint32_t timestamp, uint32_t mem_addr,
|
2026-07-16 23:00:15 +08:00
|
|
|
|
uint32_t rand_seed, uint8_t *nonce_out);
|
|
|
|
|
|
|
2026-06-15 01:14:29 +08:00
|
|
|
|
/**
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* @brief 会话密钥初始化,将密钥拷贝到会话上下文。
|
2026-06-15 01:14:29 +08:00
|
|
|
|
* @param sess 会话上下文指针。
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* @param k_session 会话密钥数据(至少 SESSION_KEY_LEN 字节)。
|
2026-06-15 01:14:29 +08:00
|
|
|
|
* @return 成功返回 0,参数无效返回 -1。
|
|
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
static int sessionKeyInit(session_ctx_t *sess, const uint8_t *k_session);
|
2026-07-16 23:00:15 +08:00
|
|
|
|
|
2026-06-15 01:14:29 +08:00
|
|
|
|
/**
|
2026-07-16 23:00:15 +08:00
|
|
|
|
* @brief 清除会话上下文中的密钥数据(抗优化擦除)。
|
|
|
|
|
|
* @param sess 会话上下文指针;为 nullptr 时忽略。
|
2026-06-15 01:14:29 +08:00
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
static void sessionKeyClear(session_ctx_t *sess);
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
private:
|
2026-07-16 23:00:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 私有默认构造函数。
|
|
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
Protocol() = default;
|
2026-07-16 23:00:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 私有析构函数。
|
|
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
~Protocol() = default;
|
2026-07-16 23:00:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 禁止复制构造。
|
|
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
Protocol(const Protocol&) = delete;
|
2026-07-16 23:00:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 禁止复制赋值。
|
|
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
Protocol& operator=(const Protocol&) = delete;
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-07-16 23:00:15 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 抗编译器优化的内存擦除(volatile 逐字节写 0)。
|
|
|
|
|
|
* @param p 起始地址;nullptr 忽略。
|
|
|
|
|
|
* @param n 字节数。
|
|
|
|
|
|
*/
|
|
|
|
|
|
static void secureWipe(void *p, size_t n);
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t m_scrambleConst[SCRAMBLE_CONST_LEN] = {0}; ///< per-boot 扰码常数(init 时 RNG 填充)
|
|
|
|
|
|
bool m_initialized = false; ///< 是否已 init
|
2026-05-31 03:20:26 +08:00
|
|
|
|
};
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-07-16 23:00:15 +08:00
|
|
|
|
#endif // PROTOCOL_H
|