feat(ota): 实现安全OTA更新协议支持AES加密和CRC校验

- 添加AES128加密解密功能支持安全固件传输
- 实现基于随机数握手的安全会话密钥协商机制
- 集成CRC32带密钥校验确保数据完整性
- 重构OTA状态机支持握手、数据传输和验证流程
- 添加CANFD数据长度规范处理和安全跳转逻辑
- 移除调试打印语句优化性能

refactor(can): 优化CANFD发送和OTA确认处理逻辑

- 重构CANFD_AsyncSend函数支持规范数据长度
- 重命名sendACK为sendDATA_ACK并添加重载函数
- 修复数据长度处理和缓冲区填充逻辑
- 优化CAN消息发送的数据复制和验证流程
- 移除未使用的联合体类型定义

chore(project): 添加AES加密模块和系统初始化配置

- 在项目配置中添加AES128加密解密文件
- 在main.cpp中初始化CRC外设模块
- 添加安全十六进制打印调试函数
- 集成AES加密库到OTA处理流程中
- 优化系统时钟初始化配置
This commit is contained in:
cbb
2026-01-01 01:39:19 +08:00
parent a25ec5f029
commit 9c7145ef99
28 changed files with 2470 additions and 1040 deletions
+37 -4
View File
@@ -2,27 +2,60 @@
#define __OTA_H
#include "Can.h"
#include "aes128.h"
#include "config.h"
#include "gd32c10x.h"
#include <stdint.h>
class OTAHandler {
public:
bool ota_active;
u32 ota_time;
u32 ota_stateota_time;
uint32_t received_size;
uint32_t expected_size;
void init();
void process();
bool isActive() const { return ota_active; }
bool isActive() const { return ota_state; }
void handleOTAMessage(uint32_t id, uint8_t *data, uint8_t len);
void jumpToApplication();
private:
u16 ota_time;
bool validateApplication();
void eraseApplicationArea();
void writeApplicationData(uint32_t offset, uint8_t *data, uint32_t size);
void sendACK(uint32_t packet_num);
typedef enum {
OTA_STATE_IDLE = 0,
OTA_STATE_WAITING_RANDOM, // 第二阶段等待随机数
OTA_STATE_WAITING_CONFIRM, // 等待上位机确认
OTA_STATE_CHECKING_FIRMWARE,
OTA_STATE_RECEIVING_FIRMWARE,
OTA_STATE_FINISHED
} ota_state_t;
typedef enum {
OTA_ERROR_NONE = 0,
OTA_ERROR_TIMEOUT,
OTA_ERROR_INVALID_KEY,
OTA_ERROR_INVALID_PACKET,
OTA_ERROR_INVALID_HANDSHAKE,
OTA_ERROR_INVALID_CRC,
OTA_ERROR_INVALID_SIZE
} ota_error_t;
// 全局变量(需在安全区域存储)
uint8_t K_master[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98,
0x76, 0x54, 0x32, 0x10}; // 预置主密钥
uint8_t device_uid[12]; // 从GD32 UID寄存器读取
uint8_t current_K_session[16];
uint8_t nonce_server[16];
aes128_context aes_ctx; // AES上下文
ota_state_t ota_state = OTA_STATE_IDLE;
ota_error_t ota_error = OTA_ERROR_NONE;
};
void getDeviceSerialNumber(uint8_t *buffer);
void calculate_crc32_with_key(const uint8_t *key, const uint8_t *data,
u8 *output);
void send_error_frame(uint8_t error);
#endif