1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#include "protocol_sacp.h"
#include <string.h>
#include "mbedtls/chachapoly.h"
// 全局序列号和滑动窗口
static uint16_t sequence = 0;
static uint16_t sequence_window[3][SEQUENCE_WINDOW_SIZE];
static uint8_t window_index[3] = {0, 0, 0};
// CRC8 查表(CRC-8/MAXIM 多项式 x^8 + x^5 + x^4 + 1)
static const uint8_t crc8_table[256] = {
0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83, 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E, 0x5F, 0x01, 0xE3, 0xBD, 0x3E, 0x60, 0x82, 0xDC,
....
}
static uint8_t sacp_calc_crc8(uint8_t *buffer, uint16_t len) {
uint8_t crc = 0;
for (uint16_t i = 0; i < len; i++) {
crc = crc8_table[crc ^ buffer[i]];
}
return crc;
}
static uint16_t calc_checksum(uint8_t *buffer, uint16_t length) {
uint32_t checksum = 0;
if (!length || !buffer) return 0;
for (uint16_t j = 0; j < (length - 1); j += 2)
checksum += ((uint32_t)buffer[j] << 8) | buffer[j + 1];
if (length % 2)
checksum += buffer[length - 1];
while (checksum > 0xffff)
checksum = (checksum >> 16) + (checksum & 0xffff);
return (uint16_t)(~checksum);
}
static int validate_timestamp(uint32_t timestamp) {
uint32_t current_time = HAL_GetTick();
int32_t diff = (int32_t)(current_time - timestamp);
return (diff >= -TIME_WINDOW && diff <= TIME_WINDOW);
}
static int validate_sequence(uint8_t sender_id, uint16_t sequence) {
if (sender_id > 2) return 0;
uint8_t idx = window_index[sender_id];
for (uint8_t i = 0; i < SEQUENCE_WINDOW_SIZE; i++) {
if (sequence_window[sender_id][i] == sequence) {
return 0;
}
}
sequence_window[sender_id][idx] = sequence;
window_index[sender_id] = (idx + 1) % SEQUENCE_WINDOW_SIZE;
return 1;
}
uint16_t sacp_sequence_pop(void) {
return sequence++;
}
ErrCode sacp_parse(uint8_t *data, uint16_t len, SACP_param_t *out, const uint8_t *key) {
uint8_t *parse_buff = out->buff;
if (len == 0 || parse_buff[0] != SACP_PDU_SOF_H) {
out->length = 0;
}
for (uint16_t i = 0; i < len; i++) {
parse_buff[out->length++] = data[i];
if (out->length < 7) {
continue;
} else if (out->length == 7) {
if (sacp_calc_crc8(parse_buff, 6) != parse_buff[6]) {
out->length = 0;
return E_FAIL;
}
} else {
uint16_t data_len = (parse_buff[3] << 8) | parse_buff[2];
uint16_t total_len = data_len + SACP_HEADER_LEN;
if (out->length == total_len) {
uint16_t checksum = calc_checksum(&parse_buff[SACP_HEADER_LEN], data_len - 2);
uint16_t checksum1 = (parse_buff[total_len - 1] << 8) | parse_buff[total_len - 2];
if (checksum != checksum1) {
out->length = 0;
return E_FAIL;
}
uint32_t timestamp = (parse_buff[13] << 24) | (parse_buff[12] << 16) |
(parse_buff[11] << 8) | parse_buff[10];
uint16_t seq = (parse_buff[9] << 8) | parse_buff[8];
if (!validate_timestamp(timestamp) || !validate_sequence(parse_buff[7], seq)) {
out->length = 0;
return E_FAIL;
}
// 解密
mbedtls_chachapoly_context chacha;
mbedtls_chachapoly_init(&chacha);
uint8_t nonce[CHACHA_NONCE_SIZE];
memcpy(nonce, &parse_buff[14], CHACHA_NONCE_SIZE);
uint8_t *ciphertext = &parse_buff[SACP_HEADER_LEN];
uint16_t ciphertext_len = data_len - CHACHA_NONCE_SIZE - 2 - CHACHA_TAG_SIZE;
uint8_t tag[CHACHA_TAG_SIZE];
memcpy(tag, &parse_buff[total_len - CHACHA_TAG_SIZE - 2], CHACHA_TAG_SIZE);
if (mbedtls_chachapoly_setkey(&chacha, key) != 0 ||
mbedtls_chachapoly_auth_decrypt(&chacha, ciphertext_len, nonce,
NULL, 0, tag, ciphertext, ciphertext) != 0) {
mbedtls_chachapoly_free(&chacha);
out->length = 0;
return E_FAIL;
}
mbedtls_chachapoly_free(&chacha);
out->length = SACP_HEADER_LEN + ciphertext_len;
return E_SUCCESS;
} else if (out->length > total_len) {
out->length = 0;
return E_FAIL;
}
}
}
return E_IN_PROGRESS;
}
uint16_t sacp_package(SACP_head_base_t *head, uint8_t *in_data, uint16_t length, uint8_t *out_data, const uint8_t *key) {
if (length > (PACK_PACKET_MAX_SIZE - SACP_HEADER_LEN - CHACHA_NONCE_SIZE - CHACHA_TAG_SIZE - 2)) {
return 0;
}
// 生成 nonce
uint8_t nonce[CHACHA_NONCE_SIZE];
memcpy(nonce, &head->sequence, 2);
memcpy(nonce + 2, &head->timestamp, 4);
memset(nonce + 6, head->recever_id, 1);
memset(nonce + 7, SACP_ID_CONTROLLER, 1);
memset(nonce + 8, 0, 4);
// 加密
uint8_t tag[CHACHA_TAG_SIZE];
mbedtls_chachapoly_context chacha;
mbedtls_chachapoly_init(&chacha);
uint8_t *ciphertext = out_data + SACP_HEADER_LEN + CHACHA_NONCE_SIZE;
if (mbedtls_chachapoly_setkey(&chacha, key) != 0 ||
mbedtls_chachapoly_encrypt_and_tag(&chacha, length, nonce, NULL, 0,
in_data, ciphertext, tag) != 0) {
mbedtls_chachapoly_free(&chacha);
return 0;
}
mbedtls_chachapoly_free(&chacha);
// 封装头部
SACP_struct_t *out = (SACP_struct_t *)out_data;
out->sof_h = SACP_PDU_SOF_H;
out->sof_l = SACP_PDU_SOF_L;
out->length = SACP_HEADER_LEN + CHACHA_NONCE_SIZE + length + CHACHA_TAG_SIZE + 2;
out->version = SACP_VERSION;
out->recever_id = head->recever_id;
out->crc8 = sacp_calc_crc8(out_data, 6);
out->sender_id = SACP_ID_CONTROLLER;
out->attr = head->attribute;
out->sequence = head->sequence;
out->command_set = head->command_set;
out->command_id = head->command_id;
out->timestamp = head->timestamp;
memcpy(out->nonce, nonce, CHACHA_NONCE_SIZE);
memcpy(out->data, nonce, CHACHA_NONCE_SIZE);
memcpy(out->data + CHACHA_NONCE_SIZE + length, tag, CHACHA_TAG_SIZE);
// 校验和
uint16_t checksum = calc_checksum(&out_data[SACP_HEADER_LEN], out->length - SACP_HEADER_LEN - 2);
out_data[out->length - 2] = (uint8_t)(checksum & 0xFF);
out_data[out->length - 1] = (uint8_t)(checksum >> 8);
return out->length;
}
#endif
|