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:
+340
-79
@@ -1,4 +1,8 @@
|
||||
#include "OTA.h"
|
||||
#include "Can.h"
|
||||
#include "aes128.h"
|
||||
#include "config.h"
|
||||
#include "gd32c10x.h" // 主头文件,包含了gd32c10x_crc.h的内容
|
||||
#include "gd32c10x_fmc.h"
|
||||
#include "systick.h"
|
||||
#include <cstdio>
|
||||
@@ -12,17 +16,20 @@ OTAHandler ota;
|
||||
extern "C" {
|
||||
void canfd_rx_irq_handler(void);
|
||||
}
|
||||
#define DEBUG_OTA 1
|
||||
|
||||
static inline uint32_t extract_u32_from_4bytes(const uint8_t *array) {
|
||||
return ((uint32_t)array[3] << 24) | ((uint32_t)array[2] << 16) |
|
||||
((uint32_t)array[1] << 8) | (uint32_t)array[0];
|
||||
}
|
||||
void OTAHandler::init() {}
|
||||
|
||||
void OTAHandler::process() {
|
||||
if (!ota_active) {
|
||||
if (ota_state == OTA_STATE_IDLE) {
|
||||
ota_time = 0; // 重置计时器
|
||||
static u8 zero[1] = {ota_active};
|
||||
static u8 zero[1] = {0};
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_WAIT_ID, 0, zero);
|
||||
#ifdef DEBUG_OTA
|
||||
printf("wait OTA%d\r\n", ota_active);
|
||||
#if DEBUG_OTA
|
||||
printf("OTA status:%d\r\n", ota_state);
|
||||
#endif
|
||||
delay_1ms(10);
|
||||
return;
|
||||
@@ -32,9 +39,11 @@ void OTAHandler::process() {
|
||||
}
|
||||
|
||||
// 检查是否超时(例如:10秒无数据则退出OTA模式)
|
||||
if (ota_time > 10000) { // 10秒超时(10000 x 1ms)
|
||||
if (ota_time > OTA_TIMEOUT) { // 10秒超时(10000 x 1ms)
|
||||
ota_time = 0;
|
||||
ota_active = false;
|
||||
ota_state = OTA_STATE_IDLE;
|
||||
ota_error = OTA_ERROR_TIMEOUT;
|
||||
send_error_frame(OTA_ERROR_TIMEOUT); // 验证失败
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,39 +59,40 @@ bool OTAHandler::validateApplication() {
|
||||
}
|
||||
|
||||
void OTAHandler::jumpToApplication() {
|
||||
if (!validateApplication()) return;
|
||||
|
||||
// 1. 关闭所有中断
|
||||
__disable_irq();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
NVIC->ICER[i] = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
// 2. 重置外设(特别是CAN,防止冲突)
|
||||
can_deinit(CAN0);
|
||||
|
||||
// 3. 重置系统时钟到默认状态
|
||||
rcu_system_clock_source_config(RCU_CKSYSSRC_IRC8M);
|
||||
|
||||
// 4. 关闭一些关键外设时钟
|
||||
rcu_periph_clock_disable(RCU_GPIOA);
|
||||
rcu_periph_clock_disable(RCU_GPIOB);
|
||||
rcu_periph_clock_disable(RCU_GPIOC);
|
||||
rcu_periph_clock_disable(RCU_USART0);
|
||||
rcu_periph_clock_disable(RCU_CAN0);
|
||||
rcu_periph_clock_disable(RCU_TIMER0);
|
||||
rcu_periph_clock_disable(RCU_DMA0);
|
||||
|
||||
// 5. 设置MSP并跳转
|
||||
uint32_t msp_value = *(__IO uint32_t*)APP_START_ADDR;
|
||||
__set_MSP(msp_value);
|
||||
|
||||
uint32_t reset_handler = *(__IO uint32_t*)(APP_START_ADDR + 4);
|
||||
typedef void (*AppEntryFunc)(void);
|
||||
AppEntryFunc app_entry = (AppEntryFunc)reset_handler;
|
||||
|
||||
// 6. 跳转
|
||||
app_entry();
|
||||
if (!validateApplication())
|
||||
return;
|
||||
|
||||
// 1. 关闭所有中断
|
||||
__disable_irq();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
NVIC->ICER[i] = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
// 2. 重置外设(特别是CAN,防止冲突)
|
||||
can_deinit(CAN0);
|
||||
|
||||
// 3. 重置系统时钟到默认状态
|
||||
rcu_system_clock_source_config(RCU_CKSYSSRC_IRC8M);
|
||||
|
||||
// 4. 关闭一些关键外设时钟
|
||||
rcu_periph_clock_disable(RCU_GPIOA);
|
||||
rcu_periph_clock_disable(RCU_GPIOB);
|
||||
rcu_periph_clock_disable(RCU_GPIOC);
|
||||
rcu_periph_clock_disable(RCU_USART0);
|
||||
rcu_periph_clock_disable(RCU_CAN0);
|
||||
rcu_periph_clock_disable(RCU_TIMER0);
|
||||
rcu_periph_clock_disable(RCU_DMA0);
|
||||
|
||||
// 5. 设置MSP并跳转
|
||||
uint32_t msp_value = *(__IO uint32_t *)APP_START_ADDR;
|
||||
__set_MSP(msp_value);
|
||||
|
||||
uint32_t reset_handler = *(__IO uint32_t *)(APP_START_ADDR + 4);
|
||||
typedef void (*AppEntryFunc)(void);
|
||||
AppEntryFunc app_entry = (AppEntryFunc)reset_handler;
|
||||
|
||||
// 6. 跳转
|
||||
app_entry();
|
||||
}
|
||||
|
||||
void OTAHandler::eraseApplicationArea() {
|
||||
@@ -105,52 +115,303 @@ void OTAHandler::writeApplicationData(u32 offset, u8 *data, u32 size) {
|
||||
memcpy(&word, &data[i], 4);
|
||||
fmc_word_program(write_addr + i, word);
|
||||
}
|
||||
|
||||
fmc_lock();
|
||||
}
|
||||
|
||||
void OTAHandler::handleOTAMessage(u32 id, u8 *data, u8 len) {
|
||||
if (id == CANFD_OTA_REQ_ID) {
|
||||
#ifdef DEBUG_OTA
|
||||
printf("req OTA\r\n");
|
||||
if (id == CANFD_OTA_DATA_ID) {
|
||||
ota_time = 0;
|
||||
if (len == 64 and ota_state == OTA_STATE_RECEIVING_FIRMWARE) {
|
||||
#if DEBUG_OTA
|
||||
printf("stage8:OTA_RECEIVING_FIRMWARE\r\n");
|
||||
#endif
|
||||
// OTA请求包
|
||||
u32 new_size = *(u32 *)data;
|
||||
if (new_size <= APP_MAX_SIZE) {
|
||||
expected_size = new_size;
|
||||
received_size = 0;
|
||||
// eraseApplicationArea();
|
||||
ota_active = true;
|
||||
sendACK(new_size); // 确认开始
|
||||
#ifdef DEBUG_OTA
|
||||
printf("start OTA%d\r\n", ota_active);
|
||||
#endif
|
||||
}
|
||||
} else if (id == CANFD_OTA_DATA_ID && ota_active) {
|
||||
// 数据包格式:[4字节包序号][实际数据]
|
||||
if (len >= 4) {
|
||||
u32 packet_num = *(u32 *)data;
|
||||
u32 offset = packet_num * (len - 4);
|
||||
|
||||
if (offset < expected_size) {
|
||||
writeApplicationData(offset, &data[4], len - 4); // 写入数据
|
||||
received_size += (len - 4);
|
||||
sendACK(packet_num); // 确认ACK
|
||||
ota_time = 0; // 重置计时器
|
||||
|
||||
// 检查是否完成
|
||||
if (received_size >= expected_size) {
|
||||
ota_active = false;
|
||||
// 跳转前延时让ACK发出
|
||||
for (volatile int i = 0; i < 100000; i++)
|
||||
;
|
||||
jumpToApplication();
|
||||
}
|
||||
// 数据解包
|
||||
u32 receive_session_num;
|
||||
u32 receive_packet_num;
|
||||
u8 receive_encrypted_data[OTA_PACKET_SIZE];
|
||||
u32 receive_IFEND;
|
||||
u8 receive_dataCRC32[4];
|
||||
receive_session_num = extract_u32_from_4bytes(data);
|
||||
receive_packet_num = extract_u32_from_4bytes(data + 4);
|
||||
for (int i = 0; i < 48; i++) {
|
||||
receive_encrypted_data[i] = data[i + 8];
|
||||
}
|
||||
receive_IFEND = extract_u32_from_4bytes(data + 56);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
receive_dataCRC32[i] = data[i + 60];
|
||||
}
|
||||
|
||||
// 解密
|
||||
u8 decrypted_data[OTA_PACKET_SIZE];
|
||||
u8 calculated_dataCRC32[4];
|
||||
aes128_set_key(&aes_ctx, current_K_session);
|
||||
aes128_ecb_decrypt_48bytes(&aes_ctx, receive_encrypted_data,
|
||||
decrypted_data);
|
||||
|
||||
// 计算解密后CRC数据
|
||||
calculate_crc32_with_key(current_K_session, decrypted_data,
|
||||
calculated_dataCRC32);
|
||||
// CRC数据匹配
|
||||
if (memcmp(receive_dataCRC32, calculated_dataCRC32, SIZE_U32) !=
|
||||
0) {
|
||||
ota_error = OTA_ERROR_INVALID_CRC;
|
||||
send_error_frame(OTA_ERROR_INVALID_CRC);
|
||||
return;
|
||||
}
|
||||
if (receive_IFEND == 0xFFFFFFFF) { // 接收到尾帧,发送结束信号
|
||||
u8 pack_number[4] = {0xFF, 0xFF, 0xFF, 0xFF};
|
||||
CAN0_transmiter.sendDATA_ACK(CANFD_OTA_ACK_ID, pack_number);
|
||||
ota_state = OTA_STATE_FINISHED;
|
||||
return;
|
||||
}
|
||||
// 下一个请求
|
||||
u32 request_packet = receive_packet_num + 1;
|
||||
u8 pack_number[4];
|
||||
// 封包
|
||||
memcpy(&pack_number, &request_packet, 4);
|
||||
CAN0_transmiter.sendDATA_ACK(CANFD_OTA_ACK_ID, pack_number);
|
||||
} else if (len == 4 and ota_state == OTA_STATE_CHECKING_FIRMWARE) {
|
||||
#if DEBUG_OTA
|
||||
printf("stage7:OTA_STATE_CHECKING_FIRMWARE\r\n");
|
||||
#endif
|
||||
// OTA请求包
|
||||
u32 new_size = *(u32 *)data;
|
||||
if (new_size <= APP_MAX_SIZE) {
|
||||
expected_size = new_size;
|
||||
received_size = 0;
|
||||
} else {
|
||||
ota_error = OTA_ERROR_INVALID_SIZE;
|
||||
send_error_frame(OTA_ERROR_INVALID_SIZE);
|
||||
return;
|
||||
}
|
||||
// eraseApplicationArea();
|
||||
u8 send_data[6] = {0xBA, 0xC0, 0x00,
|
||||
0x00, 0x00, 0x00}; // 确认开始传输
|
||||
// 发送接收包0请求
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 6, send_data);
|
||||
ota_state = OTA_STATE_RECEIVING_FIRMWARE;
|
||||
} else if (len == 2 and ota_state == OTA_STATE_FINISHED) {
|
||||
#if DEBUG_OTA
|
||||
printf("stage8:OTA_STATE_FINISHED\r\n");
|
||||
#endif
|
||||
u8 send_data[2] = {0x0A, 0xAC}; // 确认开始传输
|
||||
// 4. 验证成功 - 发送最终ACK
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 2, send_data);
|
||||
|
||||
} else {
|
||||
ota_error = OTA_ERROR_INVALID_PACKET;
|
||||
send_error_frame(OTA_ERROR_INVALID_PACKET);
|
||||
}
|
||||
} else if (id == CANFD_OTA_REQ_ID) {
|
||||
ota_time = 0;
|
||||
#if DEBUG_OTA
|
||||
printf("stage2:OTA_STATE_WAITING_RANDOM\r\n");
|
||||
#endif
|
||||
if (data[0] == 0x00 and data[1] == 0xAC and len == 2 and
|
||||
ota_state == OTA_STATE_IDLE) { // 收到发起信号
|
||||
u8 send_data[2] = {0x00, 0xac}; // 确认握手开始,请求随机数
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 2,
|
||||
send_data); // 发送握手开始信号
|
||||
ota_state = OTA_STATE_WAITING_RANDOM;
|
||||
} else if (len == 16 and ota_state == OTA_STATE_WAITING_RANDOM) {
|
||||
#if DEBUG_OTA
|
||||
printf("stage4:OTA_STATE_WAITING_CONFIRM\r\n");
|
||||
#endif
|
||||
while (len-- > 0) {
|
||||
nonce_server[len] = data[len];
|
||||
}
|
||||
// 1. 获取设备唯一码
|
||||
getDeviceSerialNumber(device_uid);
|
||||
// 2. 初始化AES上下文(设置主密钥)
|
||||
aes128_set_key(&aes_ctx, K_master);
|
||||
// 3. 生成会话密钥 K_session (16字节)
|
||||
u8 K_session[16];
|
||||
aes128_encrypt_block(&aes_ctx, nonce_server,
|
||||
K_session); // 用主密钥加密随机数
|
||||
// 4. 用主密钥加密明文 (AES-128-ECB)
|
||||
u8 encrypted_Ksession[16];
|
||||
u8 temp_block[AES_BLOCK_SIZE];
|
||||
// 加密第一个16字节块
|
||||
aes128_encrypt_block(&aes_ctx, K_session, temp_block);
|
||||
memcpy(encrypted_Ksession, temp_block, 16);
|
||||
|
||||
// 5. 构造握手响应包 (36字节)
|
||||
u8 handshake_response[36];
|
||||
// 5.1 前12字节:设备UID
|
||||
memcpy(handshake_response, device_uid, 12);
|
||||
|
||||
// 5.2 接下来8字节:nonce后半部分
|
||||
memcpy(handshake_response + 12, nonce_server + 8, 8);
|
||||
|
||||
// 5.3 最后16字节:加密数据的K-session
|
||||
memcpy(handshake_response + 20, encrypted_Ksession, 16);
|
||||
|
||||
// 6. 通过CAN发送响应
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 36,
|
||||
handshake_response);
|
||||
|
||||
// 7. 保存会话密钥供后续使用
|
||||
memcpy(current_K_session, K_session, 16);
|
||||
|
||||
// 8. 更新状态机
|
||||
ota_state = OTA_STATE_WAITING_CONFIRM;
|
||||
} else if (len == 16 and ota_state == OTA_STATE_WAITING_CONFIRM) {
|
||||
#if DEBUG_OTA
|
||||
printf("stage6:OTA_STATE_CHECKING_SESSION\r\n");
|
||||
#endif
|
||||
u8 plaintext[16];
|
||||
u8 expected[16];
|
||||
// 1. 用K_session解密
|
||||
u8 encrypted_confirm[16];
|
||||
memcpy(encrypted_confirm, data, 16);
|
||||
aes128_set_key(&aes_ctx, current_K_session);
|
||||
aes128_decrypt_block(&aes_ctx, encrypted_confirm, plaintext);
|
||||
|
||||
// 2. 计算期望值 (nonce_server XOR 0x55)
|
||||
// 注意:需要保存原始的nonce_server
|
||||
for (int i = 0; i < 16; i++) {
|
||||
expected[i] = nonce_server[i] ^ 0x55;
|
||||
}
|
||||
|
||||
// 3. 验证解密结果
|
||||
if (memcmp(plaintext, expected, 16) != 0) {
|
||||
// 验证失败 - 可能密钥错误或攻击
|
||||
ota_error = OTA_ERROR_INVALID_KEY;
|
||||
send_error_frame(OTA_ERROR_INVALID_KEY); // 验证失败
|
||||
// reset_ota_state();
|
||||
return;
|
||||
}
|
||||
|
||||
u8 send_data[2] = {0xBA, 0xC0}; // 确认开始传输
|
||||
// 4. 验证成功 - 发送最终ACK
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 2, send_data);
|
||||
|
||||
// 5. 更新状态 - 进入固件接收阶段
|
||||
ota_state = OTA_STATE_CHECKING_FIRMWARE;
|
||||
} else {
|
||||
// 其他握手异常
|
||||
ota_error = OTA_ERROR_INVALID_HANDSHAKE;
|
||||
send_error_frame(OTA_ERROR_INVALID_HANDSHAKE);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OTAHandler::sendACK(u32 packet_num) {
|
||||
CAN0_transmiter.sendACK(CANFD_OTA_ACK_ID, packet_num);
|
||||
}
|
||||
void send_error_frame(u8 error) {
|
||||
u8 error_frame[1] = {error};
|
||||
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 1, error_frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取GD32设备唯一码
|
||||
* @param buffer 存储唯一码的缓冲区,至少需要12字节
|
||||
* @return void
|
||||
*/
|
||||
void getDeviceSerialNumber(u8 *buffer) {
|
||||
uint32_t serial_msb = DEVICE_SERIAL_ADDR_MSB;
|
||||
uint32_t serial_mid = DEVICE_SERIAL_ADDR_MID;
|
||||
uint32_t serial_lsb = DEVICE_SERIAL_ADDR_LSB;
|
||||
|
||||
// 将96位唯一ID复制到缓冲区 (以小端序存储)
|
||||
for (int i = 0; i < 4; i++) {
|
||||
buffer[i] = (u8)(serial_lsb >> (i * 8));
|
||||
buffer[i + 4] = (u8)(serial_mid >> (i * 8));
|
||||
buffer[i + 8] = (u8)(serial_msb >> (i * 8));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取GD32设备唯一码为字符串格式
|
||||
* @param str 存储唯一码字符串的缓冲区,至少需要25字节(包含结束符)
|
||||
* @return void
|
||||
*/
|
||||
void getDeviceSerialNumberString(char *str) {
|
||||
u8 serial[12];
|
||||
getDeviceSerialNumber(serial);
|
||||
|
||||
// 将字节序列转换为十六进制字符串表示
|
||||
for (int i = 0; i < 12; i++) {
|
||||
sprintf(&str[i * 2], "%02X", serial[i]);
|
||||
}
|
||||
str[24] = '\0'; // 确保字符串结束
|
||||
}
|
||||
|
||||
/**
|
||||
* 带密钥的CRC32校验函数
|
||||
*
|
||||
* @param key 16字节密钥
|
||||
* @param data 48字节待校验数据
|
||||
* @param output 4字节输出缓冲区,存储CRC32校验值
|
||||
*
|
||||
* 特点:
|
||||
* - 使用标准CRC32算法
|
||||
* - 16字节密钥用于初始化CRC32状态
|
||||
*/
|
||||
void calculate_crc32_with_key(const uint8_t *key, const uint8_t *data,
|
||||
u8 *output) {
|
||||
// CRC32查找表(标准多项式0xEDB88320)
|
||||
static const uint32_t crc_table[256] = {
|
||||
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
|
||||
0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
|
||||
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
|
||||
0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
|
||||
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
|
||||
0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
|
||||
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
|
||||
0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
|
||||
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
|
||||
0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
|
||||
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
|
||||
0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
|
||||
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
|
||||
0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
|
||||
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
|
||||
0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
|
||||
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
|
||||
0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
|
||||
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
|
||||
0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
|
||||
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
|
||||
0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
|
||||
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
|
||||
0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
|
||||
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
|
||||
0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
|
||||
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
|
||||
0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
|
||||
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
|
||||
0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
|
||||
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
|
||||
0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
|
||||
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
|
||||
0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
|
||||
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
|
||||
0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
|
||||
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
|
||||
0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
|
||||
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
|
||||
0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
|
||||
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
|
||||
0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
|
||||
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D};
|
||||
|
||||
// 用16字节密钥初始化CRC32状态
|
||||
uint32_t crc = 0xFFFFFFFF;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
crc = crc_table[(crc ^ key[i]) & 0xFF] ^ (crc >> 8);
|
||||
}
|
||||
|
||||
// 计算48字节数据的CRC32
|
||||
for (int i = 0; i < 48; i++) {
|
||||
crc = crc_table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8);
|
||||
}
|
||||
|
||||
// 取反得到最终结果
|
||||
crc = ~crc;
|
||||
|
||||
// 将CRC32结果拆分为4字节(小端格式)
|
||||
output[0] = (uint8_t)(crc >> 0);
|
||||
output[1] = (uint8_t)(crc >> 8);
|
||||
output[2] = (uint8_t)(crc >> 16);
|
||||
output[3] = (uint8_t)(crc >> 24);
|
||||
}
|
||||
|
||||
+37
-4
@@ -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
|
||||
@@ -0,0 +1,375 @@
|
||||
#include "aes128.h"
|
||||
|
||||
// S盒 (Substitution Box)
|
||||
static const u8 sbox[256] = {
|
||||
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
|
||||
0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
|
||||
0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
|
||||
0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
||||
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
|
||||
0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
|
||||
0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed,
|
||||
0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
|
||||
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
|
||||
0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
|
||||
0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec,
|
||||
0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
|
||||
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
|
||||
0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
|
||||
0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
|
||||
0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
|
||||
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
|
||||
0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
|
||||
0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
|
||||
0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
||||
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
|
||||
0xb0, 0x54, 0xbb, 0x16};
|
||||
|
||||
// 逆S盒 (InvSBox)
|
||||
const u8 inv_sbox[256] = {
|
||||
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
|
||||
0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
|
||||
0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32,
|
||||
0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
|
||||
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
|
||||
0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
|
||||
0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50,
|
||||
0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
|
||||
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
|
||||
0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
|
||||
0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41,
|
||||
0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
|
||||
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
|
||||
0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
|
||||
0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b,
|
||||
0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
|
||||
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
|
||||
0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
|
||||
0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d,
|
||||
0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
|
||||
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
|
||||
0x55, 0x21, 0x0c, 0x7d};
|
||||
|
||||
// 轮常量
|
||||
static const u8 rcon[10] = {0x01, 0x02, 0x04, 0x08, 0x10,
|
||||
0x20, 0x40, 0x80, 0x1b, 0x36};
|
||||
|
||||
// 伽罗瓦域乘法 (xtime) - 确保正确
|
||||
static u8 xtime(u8 x) { return (x << 1) ^ (((x >> 7) & 1) * 0x1b); }
|
||||
|
||||
// 关键修复:AES状态矩阵是列优先排列!
|
||||
// 内存布局应该是:s00, s10, s20, s30, s01, s11, s21, s31, ...
|
||||
// 而不是行优先
|
||||
|
||||
// 轮密钥加 - 无需修改
|
||||
void add_round_key(u8 *state, const u8 *round_key) {
|
||||
for (u8 i = 0; i < 16; i++) {
|
||||
state[i] ^= round_key[i];
|
||||
}
|
||||
}
|
||||
|
||||
// 字节替换 - 无需修改
|
||||
void sub_bytes(u8 *state) {
|
||||
for (u8 i = 0; i < 16; i++) {
|
||||
state[i] = sbox[state[i]];
|
||||
}
|
||||
}
|
||||
|
||||
// 行移位 - 修复为列优先排列
|
||||
void shift_rows(u8 *state) {
|
||||
u8 temp;
|
||||
|
||||
// AES状态矩阵(列优先):
|
||||
// 列0: state[0], state[1], state[2], state[3] -> 行0,1,2,3
|
||||
// 列1: state[4], state[5], state[6], state[7] -> 行0,1,2,3
|
||||
// 列2: state[8], state[9], state[10], state[11] -> 行0,1,2,3
|
||||
// 列3: state[12], state[13], state[14], state[15] -> 行0,1,2,3
|
||||
|
||||
// 第1行(索引0,4,8,12)不移位
|
||||
// 第2行(索引1,5,9,13)左移1位
|
||||
temp = state[1];
|
||||
state[1] = state[5];
|
||||
state[5] = state[9];
|
||||
state[9] = state[13];
|
||||
state[13] = temp;
|
||||
|
||||
// 第3行(索引2,6,10,14)左移2位
|
||||
temp = state[2];
|
||||
state[2] = state[10];
|
||||
state[10] = temp;
|
||||
temp = state[6];
|
||||
state[6] = state[14];
|
||||
state[14] = temp;
|
||||
|
||||
// 第4行(索引3,7,11,15)左移3位
|
||||
temp = state[3];
|
||||
state[3] = state[15];
|
||||
state[15] = state[11];
|
||||
state[11] = state[7];
|
||||
state[7] = temp;
|
||||
}
|
||||
|
||||
// 列混合 - 修复为正确的实现
|
||||
void mix_columns(u8 *state) {
|
||||
u8 a[4], b[4];
|
||||
|
||||
// 处理每一列
|
||||
for (u8 col = 0; col < 4; col++) {
|
||||
// 获取当前列的4个字节
|
||||
a[0] = state[col * 4 + 0];
|
||||
a[1] = state[col * 4 + 1];
|
||||
a[2] = state[col * 4 + 2];
|
||||
a[3] = state[col * 4 + 3];
|
||||
|
||||
// 计算混合后的值
|
||||
b[0] = xtime(a[0]) ^ xtime(a[1]) ^ a[1] ^ a[2] ^ a[3];
|
||||
b[1] = a[0] ^ xtime(a[1]) ^ xtime(a[2]) ^ a[2] ^ a[3];
|
||||
b[2] = a[0] ^ a[1] ^ xtime(a[2]) ^ xtime(a[3]) ^ a[3];
|
||||
b[3] = xtime(a[0]) ^ a[0] ^ a[1] ^ a[2] ^ xtime(a[3]);
|
||||
|
||||
// 存回结果
|
||||
state[col * 4 + 0] = b[0];
|
||||
state[col * 4 + 1] = b[1];
|
||||
state[col * 4 + 2] = b[2];
|
||||
state[col * 4 + 3] = b[3];
|
||||
}
|
||||
}
|
||||
|
||||
// 伽罗瓦域乘法 (用于逆列混合)
|
||||
static u8 gf_mul(u8 a, u8 b) {
|
||||
u8 p = 0;
|
||||
u8 high_bit;
|
||||
|
||||
while (b) {
|
||||
if (b & 1) {
|
||||
p ^= a;
|
||||
}
|
||||
high_bit = a & 0x80;
|
||||
a <<= 1;
|
||||
if (high_bit) {
|
||||
a ^= 0x1b; // x^8 + x^4 + x^3 + x + 1
|
||||
}
|
||||
b >>= 1;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
// 逆字节替换
|
||||
void inv_sub_bytes(u8 *state) {
|
||||
for (u8 i = 0; i < 16; i++) {
|
||||
state[i] = inv_sbox[state[i]];
|
||||
}
|
||||
}
|
||||
|
||||
// 逆行移位 - 与shift_rows对应
|
||||
void inv_shift_rows(u8 *state) {
|
||||
u8 temp;
|
||||
|
||||
// 第2行右移1位
|
||||
temp = state[13];
|
||||
state[13] = state[9];
|
||||
state[9] = state[5];
|
||||
state[5] = state[1];
|
||||
state[1] = temp;
|
||||
|
||||
// 第3行右移2位
|
||||
temp = state[2];
|
||||
state[2] = state[10];
|
||||
state[10] = temp;
|
||||
temp = state[6];
|
||||
state[6] = state[14];
|
||||
state[14] = temp;
|
||||
|
||||
// 第4行右移3位
|
||||
temp = state[7];
|
||||
state[7] = state[11];
|
||||
state[11] = state[15];
|
||||
state[15] = state[3];
|
||||
state[3] = temp;
|
||||
}
|
||||
|
||||
// 逆列混合 - 正确实现
|
||||
void inv_mix_columns(u8 *state) {
|
||||
u8 a[4], b[4];
|
||||
|
||||
// 处理每一列
|
||||
for (u8 col = 0; col < 4; col++) {
|
||||
// 获取当前列的4个字节
|
||||
a[0] = state[col * 4 + 0];
|
||||
a[1] = state[col * 4 + 1];
|
||||
a[2] = state[col * 4 + 2];
|
||||
a[3] = state[col * 4 + 3];
|
||||
|
||||
// 计算逆混合后的值
|
||||
b[0] = gf_mul(0x0e, a[0]) ^ gf_mul(0x0b, a[1]) ^ gf_mul(0x0d, a[2]) ^
|
||||
gf_mul(0x09, a[3]);
|
||||
b[1] = gf_mul(0x09, a[0]) ^ gf_mul(0x0e, a[1]) ^ gf_mul(0x0b, a[2]) ^
|
||||
gf_mul(0x0d, a[3]);
|
||||
b[2] = gf_mul(0x0d, a[0]) ^ gf_mul(0x09, a[1]) ^ gf_mul(0x0e, a[2]) ^
|
||||
gf_mul(0x0b, a[3]);
|
||||
b[3] = gf_mul(0x0b, a[0]) ^ gf_mul(0x0d, a[1]) ^ gf_mul(0x09, a[2]) ^
|
||||
gf_mul(0x0e, a[3]);
|
||||
|
||||
// 存回结果
|
||||
state[col * 4 + 0] = b[0];
|
||||
state[col * 4 + 1] = b[1];
|
||||
state[col * 4 + 2] = b[2];
|
||||
state[col * 4 + 3] = b[3];
|
||||
}
|
||||
}
|
||||
|
||||
// 密钥扩展 - 修复为标准实现
|
||||
static void key_expansion(aes128_context *ctx) {
|
||||
u8 temp[4];
|
||||
u8 i, rcon_iter = 0;
|
||||
|
||||
// 复制原始密钥到第一轮密钥
|
||||
memcpy(ctx->round_keys[0], ctx->key, 16);
|
||||
|
||||
// 生成10轮密钥
|
||||
for (i = 1; i <= 10; i++) {
|
||||
// 复制前一轮最后一列
|
||||
temp[0] = ctx->round_keys[i - 1][12];
|
||||
temp[1] = ctx->round_keys[i - 1][13];
|
||||
temp[2] = ctx->round_keys[i - 1][14];
|
||||
temp[3] = ctx->round_keys[i - 1][15];
|
||||
|
||||
// 字循环 (RotWord)
|
||||
u8 t = temp[0];
|
||||
temp[0] = temp[1];
|
||||
temp[1] = temp[2];
|
||||
temp[2] = temp[3];
|
||||
temp[3] = t;
|
||||
|
||||
// 字节替换 (SubWord)
|
||||
temp[0] = sbox[temp[0]];
|
||||
temp[1] = sbox[temp[1]];
|
||||
temp[2] = sbox[temp[2]];
|
||||
temp[3] = sbox[temp[3]];
|
||||
|
||||
// 轮常量异或
|
||||
temp[0] ^= rcon[i - 1];
|
||||
|
||||
// 生成新的第一列
|
||||
ctx->round_keys[i][0] = ctx->round_keys[i - 1][0] ^ temp[0];
|
||||
ctx->round_keys[i][1] = ctx->round_keys[i - 1][1] ^ temp[1];
|
||||
ctx->round_keys[i][2] = ctx->round_keys[i - 1][2] ^ temp[2];
|
||||
ctx->round_keys[i][3] = ctx->round_keys[i - 1][3] ^ temp[3];
|
||||
|
||||
// 生成其他列
|
||||
for (u8 j = 1; j < 4; j++) {
|
||||
ctx->round_keys[i][j * 4 + 0] =
|
||||
ctx->round_keys[i][(j - 1) * 4 + 0] ^
|
||||
ctx->round_keys[i - 1][j * 4 + 0];
|
||||
ctx->round_keys[i][j * 4 + 1] =
|
||||
ctx->round_keys[i][(j - 1) * 4 + 1] ^
|
||||
ctx->round_keys[i - 1][j * 4 + 1];
|
||||
ctx->round_keys[i][j * 4 + 2] =
|
||||
ctx->round_keys[i][(j - 1) * 4 + 2] ^
|
||||
ctx->round_keys[i - 1][j * 4 + 2];
|
||||
ctx->round_keys[i][j * 4 + 3] =
|
||||
ctx->round_keys[i][(j - 1) * 4 + 3] ^
|
||||
ctx->round_keys[i - 1][j * 4 + 3];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置密钥
|
||||
void aes128_set_key(aes128_context *ctx, const u8 *key) {
|
||||
memcpy(ctx->key, key, AES128_KEY_LEN);
|
||||
key_expansion(ctx);
|
||||
}
|
||||
|
||||
// 加密单个块
|
||||
void aes128_encrypt_block(aes128_context *ctx, const u8 *input,
|
||||
u8 *output) {
|
||||
// 复制输入到状态
|
||||
memcpy(ctx->state, input, AES_BLOCK_SIZE);
|
||||
|
||||
// 初始轮密钥加
|
||||
add_round_key(ctx->state, ctx->round_keys[0]);
|
||||
|
||||
// 9轮标准轮
|
||||
for (u8 round = 1; round <= 9; round++) {
|
||||
sub_bytes(ctx->state);
|
||||
shift_rows(ctx->state);
|
||||
mix_columns(ctx->state);
|
||||
add_round_key(ctx->state, ctx->round_keys[round]);
|
||||
}
|
||||
|
||||
// 最终轮 (没有mix_columns)
|
||||
sub_bytes(ctx->state);
|
||||
shift_rows(ctx->state);
|
||||
add_round_key(ctx->state, ctx->round_keys[10]);
|
||||
|
||||
// 复制结果到输出
|
||||
memcpy(output, ctx->state, AES_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
// ECB模式加密48字节数据
|
||||
void aes128_ecb_encrypt_48bytes(aes128_context *ctx, const u8 *input,
|
||||
u8 *output) {
|
||||
// 分3块加密 (48 = 16 * 3)
|
||||
aes128_encrypt_block(ctx, input, output);
|
||||
aes128_encrypt_block(ctx, input + 16, output + 16);
|
||||
aes128_encrypt_block(ctx, input + 32, output + 32);
|
||||
}
|
||||
|
||||
// 解密单个块
|
||||
void aes128_decrypt_block(aes128_context *ctx, const u8 *input,
|
||||
u8 *output) {
|
||||
// 复制输入到状态
|
||||
memcpy(ctx->state, input, AES_BLOCK_SIZE);
|
||||
|
||||
// 初始轮密钥加(使用最后一轮密钥)
|
||||
add_round_key(ctx->state, ctx->round_keys[10]);
|
||||
|
||||
// 9轮标准解密轮
|
||||
for (u8 round = 9; round >= 1; round--) {
|
||||
inv_shift_rows(ctx->state);
|
||||
inv_sub_bytes(ctx->state);
|
||||
add_round_key(ctx->state, ctx->round_keys[round]);
|
||||
inv_mix_columns(ctx->state);
|
||||
}
|
||||
|
||||
// 最终轮(没有inv_mix_columns)
|
||||
inv_shift_rows(ctx->state);
|
||||
inv_sub_bytes(ctx->state);
|
||||
add_round_key(ctx->state, ctx->round_keys[0]);
|
||||
|
||||
// 复制结果到输出
|
||||
memcpy(output, ctx->state, AES_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
// ECB模式解密48字节数据
|
||||
void aes128_ecb_decrypt_48bytes(aes128_context *ctx, const u8 *input,
|
||||
u8 *output) {
|
||||
// 分3块解密 (48 = 16 * 3)
|
||||
aes128_decrypt_block(ctx, input, output);
|
||||
aes128_decrypt_block(ctx, input + 16, output + 16);
|
||||
aes128_decrypt_block(ctx, input + 32, output + 32);
|
||||
}
|
||||
|
||||
// 验证函数:加密后解密,对比原始数据
|
||||
u8 aes128_verify_encryption(aes128_context *ctx,
|
||||
const u8 *original_data, size_t length) {
|
||||
u8 temp_ciphertext[48];
|
||||
u8 temp_plaintext[48];
|
||||
u8 result = 1; // 1表示验证成功,0表示失败
|
||||
|
||||
// 1. 加密原始数据
|
||||
aes128_ecb_encrypt_48bytes(ctx, original_data, temp_ciphertext);
|
||||
|
||||
// 2. 解密密文
|
||||
aes128_ecb_decrypt_48bytes(ctx, temp_ciphertext, temp_plaintext);
|
||||
|
||||
// 3. 对比原始数据和解密后的数据
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
if (original_data[i] != temp_plaintext[i]) {
|
||||
result = 0; // 验证失败
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef __AES_H
|
||||
#define __AES_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
// AES-128 密钥长度 (16 bytes)
|
||||
#define AES128_KEY_LEN 16
|
||||
// AES 块大小 (16 bytes)
|
||||
#define AES_BLOCK_SIZE 16
|
||||
|
||||
// AES 状态结构
|
||||
typedef struct {
|
||||
u8 key[AES128_KEY_LEN]; // 128位/16字节密钥
|
||||
u8 round_keys[11][16]; // 10轮 + 初始轮密钥
|
||||
u8 state[AES_BLOCK_SIZE]; // 状态矩阵
|
||||
} aes128_context;
|
||||
|
||||
// 内部函数声明(需要在头文件中声明,因为解密函数会使用)
|
||||
void add_round_key(u8 *state, const u8 *round_key);
|
||||
void sub_bytes(u8 *state);
|
||||
void shift_rows(u8 *state);
|
||||
void mix_columns(u8 *state);
|
||||
void inv_sub_bytes(u8 *state);
|
||||
void inv_shift_rows(u8 *state);
|
||||
void inv_mix_columns(u8 *state);
|
||||
|
||||
// 公共函数声明
|
||||
void aes128_set_key(aes128_context *ctx, const u8 *key);
|
||||
void aes128_encrypt_block(aes128_context *ctx, const u8 *input, u8 *output);
|
||||
void aes128_ecb_encrypt_48bytes(aes128_context *ctx, const u8 *input, u8 *output);
|
||||
void aes128_decrypt_block(aes128_context *ctx, const u8 *input, u8 *output);
|
||||
void aes128_ecb_decrypt_48bytes(aes128_context *ctx, const u8 *input, u8 *output);
|
||||
u8 aes128_verify_encryption(aes128_context *ctx, const u8 *original_data, size_t length);
|
||||
|
||||
// 逆S盒 (用于解密)
|
||||
extern const u8 inv_sbox[256];
|
||||
|
||||
#endif /* __AES_H */
|
||||
+28
-1
@@ -2,9 +2,12 @@
|
||||
#define __CONFIG_H__
|
||||
#include "stdint.h"
|
||||
|
||||
#define EEPDEBUG 0
|
||||
#define DEBUG_OTA 0
|
||||
|
||||
// Flash分区配置
|
||||
constexpr u32 BOOTLOADER_SIZE = 0x4000; // 16KB
|
||||
constexpr u32 APP_START_ADDR = 0x08004000; // APP起始地址
|
||||
constexpr u32 APP_START_ADDR = 0x08010000; // APP起始地址 0x08004000
|
||||
constexpr u32 APP2_START_ADDR = 0x08010000; // APP2起始地址
|
||||
constexpr u32 APP_MAX_SIZE = 0xC000; // 48KB
|
||||
constexpr u32 FMC_PAGE_SIZE = 0x400; // 1KB
|
||||
@@ -15,6 +18,11 @@ constexpr u32 CANFD_OTA_DATA_ID = 0x00000101;
|
||||
constexpr u32 CANFD_OTA_ACK_ID = 0x00000102;
|
||||
constexpr u32 CANFD_OTA_WAIT_ID = 0x00000103;
|
||||
|
||||
// 设备唯一ID寄存器地址 (GD32C10x系列)
|
||||
constexpr u32 DEVICE_SERIAL_ADDR_MSB = 0x1FFFF7E8; // 最高32位
|
||||
constexpr u32 DEVICE_SERIAL_ADDR_MID = 0x1FFFF7EC; // 中间32位
|
||||
constexpr u32 DEVICE_SERIAL_ADDR_LSB = 0x1FFFF7F0; // 最低32位
|
||||
|
||||
// CANFD数据段最大64字节,我们用1字节序号 + 48字节数据 + 1字节CRC校验
|
||||
constexpr u32 OTA_PACKET_SIZE = 48;
|
||||
typedef struct {
|
||||
@@ -23,4 +31,23 @@ typedef struct {
|
||||
u32 crc32; // 简单校验
|
||||
} canfd_ota_packet_t;
|
||||
|
||||
// 定义一个联合体类型用于float和byte数组之间的转换
|
||||
typedef union {
|
||||
u32 u;
|
||||
u8 b[4];
|
||||
} u32_union_t;
|
||||
|
||||
// 添加辅助函数用于从u8数组中解包float类型数据
|
||||
static inline u32_union_t unpack_bytes_to_float(u8 *buffer, u16 offset) {
|
||||
u32_union_t u32_union;
|
||||
u32_union.b[0] = buffer[offset];
|
||||
u32_union.b[1] = buffer[offset + 1];
|
||||
u32_union.b[2] = buffer[offset + 2];
|
||||
u32_union.b[3] = buffer[offset + 3];
|
||||
return u32_union;
|
||||
}
|
||||
// 其他
|
||||
constexpr u32 OTA_TIMEOUT = 10000;
|
||||
constexpr u8 SIZE_U32 = sizeof(uint32_t);
|
||||
constexpr u8 SIZE_FLOAT = sizeof(float);
|
||||
#endif
|
||||
@@ -10,9 +10,6 @@
|
||||
#include <stdint.h>
|
||||
|
||||
FlashEEPROM flashEEP = FlashEEPROM();
|
||||
constexpr u8 SIZE_U32 = sizeof(uint32_t);
|
||||
constexpr u8 SIZE_FLOAT = sizeof(float);
|
||||
#define EEPDEBUG 0
|
||||
|
||||
// FlashEEPROM类实现
|
||||
FlashEEPROM::FlashEEPROM() {
|
||||
|
||||
@@ -14,9 +14,10 @@ int fputc(int ch, FILE *f); // 声明fputc函数以重载标准库实现
|
||||
u8 USART0_init(void);
|
||||
u16 get_reset_cause(void);
|
||||
void unused_pin_init();
|
||||
void print_system_clock_info(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user