添加单条数据加密功能,简化上位机判别逻辑

更新单条数据最大长度
crypt.c 更新解密函数
This commit is contained in:
CSSC-WORK\murmur 2023-08-19 14:41:13 +08:00
parent 696b675e6b
commit 14c09d5134
10 changed files with 91 additions and 47 deletions

View File

@ -65,10 +65,12 @@ MSH_CMD_EXPORT(alarm_sample,alarm sample);
void poTT_callback(rt_alarm_t alarm, time_t timestamp)
{
LOG_D("power UP TT.");
initTT();
}
void pdTT_callback(rt_alarm_t alarm, time_t timestamp)
{
LOG_D("power DOWN TT.");
deInitTT();
}
//每个开窗设置对应两个rtc闹钟一个上电一个下电
//更新开窗设置前需要先清空闹钟设置。可改为调用rt_alarm_control()修改。

View File

@ -190,7 +190,7 @@ static void upSendFile_thread_entry(void *parameter)
LOG_D("ready to send '%s---%d'",f->fname,f->index);
rt_uint8_t len = pack_File(f->fname, 0, d, s);
for (size_t i = 0; i < len; i++) {
LOG_HEX("rst",16,d[i],s[i]);
LOG_HEX("sendFile",27,d[i],s[i]);
}
// list_thread();
if (len)//部分demo数据体积>>1k
@ -203,7 +203,7 @@ static void upSendFile_thread_entry(void *parameter)
if (sendMsg(d[var], s[var]) == RT_EOK) {
LOG_D("send pack[%d] done.",var+1);
};
rt_thread_mdelay(1000);
rt_thread_mdelay(3000);//发送间隔目前服务器未处理暂设为3s避免粘包
}
}
LOG_I("upSendFile done.");

View File

@ -87,9 +87,19 @@ size_t aes_128_cbc_pkcs7_de(rt_uint8_t *data, size_t len, rt_uint8_t *dout)
mbedtls_aes_free(&ctx); //释放结构体
// LOG_HEX("de",16,edata,len);
size_t rst = len-edata[len-1];
uint8_t v= edata[len-1];
if (v==0 || !(v<len)) {//判断解密是否成功依据PKCS7填充模式解密数据最后按
return 0;
}
uint8_t t[v];
size_t rst = len-v;
memset(t,v,v);
if (memcmp(edata+rst,t,v) != 0) {//依据PKCS7填充模式正确解密的数据
return 0;
}
edata[rst]='\0';
// LOG_HEX("de",16,edata,len);
rt_memcpy(dout, edata, rst+1);
return rst;

View File

@ -16,7 +16,7 @@
#include <ulog.h>
extern SYS_CFG scfg;
extern int cryptSingleMsg(uint8_t *din, size_t len, uint8_t *dout);
/**
* ASCII字符形式存入log
* @param din
@ -151,7 +151,7 @@ void upSend_thread_entry(void* parameter)
uint8_t sta = 1;
while (1 && !isTTjh()) //判断TT状态
while (0 && !isTTjh()) //判断TT状态
{
static uint8_t trycnt = 0;
rt_thread_mdelay(4000);
@ -180,7 +180,13 @@ void upSend_thread_entry(void* parameter)
packInit(&cfg, fin, 0); //写入配置
cfg.fcurpiece[0] = 1;
cfg.fallpiece[0] = 1;
size_t rst = packMsg(&cfg, msg->data, msg->len, dout);//packMsgs
//crypt before pack
uint8_t tmp[200];
uint8_t len = cryptSingleMsg(msg->data, msg->len, tmp);
size_t rst = packMsg(&cfg, tmp, len, dout);//packMsgs
// size_t rst = packMsg(&cfg, tmpmsg->data, msg->len, dout);//packMsgs
LOG_HEX("upSend", 27, dout, rst);
if (rst)
{
@ -426,7 +432,7 @@ static int getAndCheckLoc(uint8_t *dout, size_t pairCnt)
memcpy(dout,loc,cnt);
return cnt;
}
static int cryptLocMsg(uint8_t *din, size_t len, uint8_t *dout);
/**
* c回应深度数据
* @param din
@ -445,7 +451,7 @@ int reportDepth(uint8_t *din,size_t len)
rt_memcpy(dout+10, din, len);//5aa5从第11字节开始共len个字节
LOG_HEX("depth",16,dout,len+10);
//加密。因加密后数据长度会变化,故不能只加密位置数据。
nlen = cryptLocMsg(din, len+10, dout);
nlen = cryptSingleMsg(din, len+10, dout);
LOG_HEX("crypt",16,dout,nlen);
// LOG_D("位置数据加密完成");
upSend(dout, nlen);
@ -461,6 +467,7 @@ void updateAllSysCfg(uint8_t *cfg, size_t len)
LOG_I("FUNC = update SYSCFG");
SYS_CFG ts;
memcpy(&scfg,cfg,len);
//详见SYS_CFG的定义
/**
*
uint8_t sendInterval;
@ -468,15 +475,13 @@ void updateAllSysCfg(uint8_t *cfg, size_t len)
uint8_t maxTTRetryCnt;
uint8_t minTTPeriCnt;
uint8_t minTTsinal;
uint8_t minActiveTime;
uint8_t maxActiveTime;
uint8_t timeout;
uint8_t openWindowTime[8];
uint16_t maxSizePerFile;
uint8_t commMode;
uint8_t selfDesSW;
uint8_t locAlert;
uint8_t locReportInterval;
uint8_t locRepInterval;
*/
set_cfg("sendInterval",scfg.sendInterval);
@ -494,7 +499,7 @@ void updateAllSysCfg(uint8_t *cfg, size_t len)
set_cfg("commMode",scfg.commMode);
set_cfg("selfDesSW",scfg.selfDesSW);
set_cfg("locAlert",scfg.locAlert);
set_cfg("locReportInterval",scfg.locReportInterval);
set_cfg("locRepInterval",scfg.locRepInterval);
}
/**
*
@ -538,6 +543,7 @@ void reportSysCfg()
* 15[]+[]+[15]
*
* 15MOB(most oldest bit)
* 1610
* @param dout
* @return
*/
@ -549,7 +555,7 @@ static int packLocMsg(uint8_t *dout)
alertMsg[8] = 0; //首字节00为定时发送未检测围栏其它为在围栏外
int len = time2Byte(alertMsg+9);//添加时间戳
len += getAndCheckLoc(alertMsg + 9 + len, 15) + 1; //p指向第2个数据
len += getAndCheckLoc(alertMsg + 9 + len, 10) + 1; //p指向第2个数据
alertMsg[7] = len; //update len of raw data
alertMsg[8 + len] = bccCRC(alertMsg + 2, 8 + len - 2); //update CRC
@ -564,33 +570,33 @@ static void d_packLocMsg(void)
{
uint8_t tmp[200];
size_t len = packLocMsg(tmp);
len = cryptLocMsg(tmp, len, tmp);
LOG_HEX("c",16,tmp,len);
// len = cryptSingleMsg(tmp, len, tmp);
// LOG_HEX("c",16,tmp,len);
upSend(tmp, len);
}
/**
*
* @param din
* @param len
* @param dout
* @return
*/
static int cryptLocMsg(uint8_t *din, size_t len, uint8_t *dout)
{ //加密。因加密后数据长度会变化,故不能只加密位置数据。
uint8_t cd[200]={0xAB,0xBA};
size_t nlen = 0;
//#define FULL_DATA_CRYPT
#ifdef FULL_DATA_CRYPT
nlen = cryp_data(din, len, cd);
#else
//单独加密时在加密后数据头部添加0xABBA便于识别、解析
nlen = cryp_data(din, len, cd + 2) + 2;
#endif
memcpy(dout, cd, nlen);
return nlen;
}
///**
// * 加密位置信息。对位置信息的更改需在加密前操作
// * @param din 存储待加密位置信息的数组
// * @param len 带加密长度
// * @param dout 存储加密结果的数组
// * @return 加密后的长度
// */
//int cryptSingleMsg(uint8_t *din, size_t len, uint8_t *dout)
//{ //加密。因加密后数据长度会变化,故不能只加密位置数据。
// uint8_t cd[200]={0xAB,0xBA};
// size_t nlen = 0;
//
////#define FULL_DATA_CRYPT
//#ifdef FULL_DATA_CRYPT
// nlen = cryp_data(din, len, cd);
//#else
// //单独加密时在加密后数据头部添加0xABBA便于识别、解析
// nlen = cryp_data(din, len, cd + 2) + 2;
//#endif
// memcpy(dout, cd, nlen);
// return nlen;
//}
//static uint8_t locMsg[200];
//static rt_timer_t repLoc;
@ -611,7 +617,7 @@ static void packAndSendLoc_thread_entry(void *parameter)
int len = packLocMsg(rst);
// LOG_D("len=%d", len);
len = cryptLocMsg(rst, len, rst);
len = cryptSingleMsg(rst, len, rst);
// LOG_D("len=%d", len);
LOG_HEX("crypt", 16, rst, len);
@ -717,7 +723,7 @@ int depthAlert(uint8_t *din, int len)
// rt_memcpy(dout+10, din, len);//5aa5从第11字节开始共len个字节
// LOG_HEX("depth",16,dout,len+10);
// //加密。因加密后数据长度会变化,故不能只加密位置数据。
// nlen = cryptLocMsg(din, len+10, dout);
// nlen = cryptSingleMsg(din, len+10, dout);
// LOG_HEX("crypt",16,dout,nlen);
// LOG_D("位置数据加密完成");
// upSend(dout, nlen);

View File

@ -303,7 +303,7 @@ static int webclient_get_data(const char *url)
if (!isTCPok() || webclient_request(url, RT_NULL, RT_NULL, 0, (void **) &buffer, &length) < 0)
{
LOG_E("TT server is not ready.");
LOG_E("TT server or webclient is not ready.");
if (buffer)
{
web_free(buffer);

View File

@ -40,8 +40,7 @@ int main(void)
rt_pin_mode(TT_EN, PIN_MODE_OUTPUT);
rt_pin_write(TT_EN, PIN_LOW);
// rt_pin_attach_irq(TT_EN, PIN_IRQ_MODE_FALLING, pwTT_irq_callback, RT_NULL); // 下降沿触发
// rt_pin_irq_enable(TT_EN, PIN_IRQ_ENABLE); // 使能中断
rt_pin_mode(TR485_RE, PIN_MODE_OUTPUT);
rt_pin_write(TR485_RE, PIN_LOW);

View File

@ -452,7 +452,28 @@ int isDataASCII(uint8_t *din, size_t len)
/**
* 便
* @param din
* @param len
* @param dout
* @return
*/
int cryptSingleMsg(uint8_t *din, size_t len, uint8_t *dout)
{ //加密。因加密后数据长度会变化,故不能只加密位置数据。
uint8_t cd[200]={0xAB,0xBA};
size_t nlen = 0;
//#define FULL_DATA_CRYPT
#ifdef FULL_DATA_CRYPT
nlen = cryp_data(din, len, cd);
#else
//单独加密时在加密后数据头部添加0xABBA便于识别、解析
nlen = cryp_data(din, len, cd + 2) + 2;
#endif
memcpy(dout, cd, nlen);
return nlen;
}

View File

@ -323,7 +323,13 @@ rt_uint8_t pack_File(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[
// LOG_HEX("hex_file",27,buffer,block_size);
uint8_t index = (i / flen);
cfg.fcurpiece[0] = index + 1;
uint8_t col = packMsg(&cfg, buffer, block_size, dout[index]);
//crypt before pack
uint8_t tmp[200];
uint8_t len = cryptSingleMsg(buffer, block_size, tmp);
uint8_t col = packMsg(&cfg, tmp, len, dout[index]);
// uint8_t col = packMsg(&cfg, buffer, block_size, dout[index]);
arrLen[index] = col;
// LOG_D("index/row bef-aft");
// LOG_D("%5d/%3d %3d-%3d",index+1,row,block_size,col);

View File

@ -19,7 +19,7 @@
//#include "posix/string.h"
#include <dfs_file.h>
#define FRAME_DATA_LEN_MAX 180
#define FRAME_DATA_LEN_MAX (180-16)
#define STR_LEN_MAX 30
/*

View File

@ -41,7 +41,7 @@ typedef struct
uint8_t commMode;
uint8_t selfDesSW;
uint8_t locAlert;
uint8_t locReportInterval;
uint8_t locRepInterval;
} SYS_CFG;