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
+121 -3
View File
@@ -1,16 +1,21 @@
#include "main.h"
#include "Can.h"
#include "aes128.h"
#include "initial.h"
#include "sys/systick.h"
#include <cstdio>
extern CAN_transmiter CAN0_transmiter; // CAN初始化
BootloaderSystem sys;
extern OTAHandler ota;
void BootloaderSystem::initHardware() {
systick_config(); // 系统时钟初始化
SystemCoreClockUpdate(); // 更新系统时钟变量
USART0_init(); // 调试串口初始化
systick_config(); // 系统时钟初始化
USART0_init(); // 调试串口初始化
rcu_periph_clock_enable(RCU_CRC); // 使能CRC外设时钟
crc_deinit(); // 初始化CRC模块
CAN0_transmiter.init();
}
void BootloaderSystem::run() {
@@ -26,8 +31,121 @@ void BootloaderSystem::run() {
ota.jumpToApplication();
}
}
// 发送单个字节到串口 (用于调试)
void uart_send_byte(uint8_t data) {
// 实际的串口发送代码
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, data);
}
// 以十六进制格式打印数据
// 安全的十六进制打印函数,不使用sprintf,不使用大缓冲区
void safe_print_hex(const char *prefix, const uint8_t *data, size_t length) {
// 1. 先打印前缀
const char *p = prefix;
while (*p) {
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, *p++);
}
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, ':');
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, ' ');
// 2. 逐字节打印数据,每16字节换行
for (size_t i = 0; i < length; i++) {
uint8_t byte = data[i];
// 打印高4位
uint8_t high = (byte >> 4) & 0x0F;
char high_char = (high < 10) ? ('0' + high) : ('A' + high - 10);
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, high_char);
// 打印低4位
uint8_t low = byte & 0x0F;
char low_char = (low < 10) ? ('0' + low) : ('A' + low - 10);
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, low_char);
// 打印空格
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, ' ');
// 每16字节换行
if ((i + 1) % 16 == 0 && i < length - 1) {
// 换行
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, '\r');
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, '\n');
// 缩进(4个空格)
for (int j = 0; j < 4; j++) {
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, ' ');
}
}
}
// 3. 打印换行
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, '\r');
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, '\n');
}
void test_decrypt() {
printf("AES Encryption Verification Starting...\r\n");
// 定义48字节测试数据
uint8_t plaintext[48] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
// 定义128位密钥 (16字节)
uint8_t key[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
// 创建AES上下文
aes128_context ctx;
// 设置密钥
aes128_set_key(&ctx, key);
// 打印原始数据
safe_print_hex("Original Data", plaintext, 48);
uint8_t ciphertext[48];
uint8_t decrypted[48];
// 加密
aes128_ecb_encrypt_48bytes(&ctx, plaintext, ciphertext);
safe_print_hex("Encrypted Data", ciphertext, 48);
// 解密
aes128_ecb_decrypt_48bytes(&ctx, ciphertext, decrypted);
safe_print_hex("Decrypted Data", decrypted, 48);
}
int main(void) {
sys.initHardware();
// test_decrypt();
sys.run();
while (1)