From b65fb0a66a0e716b9820e5c8c92c4e5874098fb7 Mon Sep 17 00:00:00 2001 From: "CSSC-WORK\\murmur" Date: Thu, 9 Nov 2023 17:12:57 +0800 Subject: [PATCH] =?UTF-8?q?trDataTolog=20=E6=95=B0=E6=8D=AE=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=B8=BA=E5=AF=86=E6=96=87=20=E6=B7=BB=E5=8A=A0pack?= =?UTF-8?q?=5Fdata=E5=87=BD=E6=95=B0=EF=BC=8C=E5=AF=86=E6=96=87=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E6=98=8E=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/core.c | 11 +++-- applications/cryp/cryp.c | 4 +- applications/func/func.c | 19 ++++++-- applications/ttmsg/ttmsg.c | 94 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 11 deletions(-) diff --git a/applications/core.c b/applications/core.c index 3fc0eab..8755bb9 100644 --- a/applications/core.c +++ b/applications/core.c @@ -453,7 +453,7 @@ static void upSendFile_thread_entry(void *parameter) rt_sem_release(okTosend); return; } - if (getFileSize(f->fname) > scfg.maxSizePerFile+200) {//部分demo数据体积>>1k + if (getFileSize(f->fname) > scfg.maxSizePerFile+500) {//部分demo数据体积>>1k LOG_W("file '%s' is too large to send.",f->fname); clearFileToSend(f->fname); rt_sem_release(okTosend); @@ -461,10 +461,12 @@ static void upSendFile_thread_entry(void *parameter) } LOG_D("ready to send '%s---%d'",f->fname,f->index); - uint8_t len = pack_File(f->fname, 0, d, s); - for (size_t i = 0; i < len; i++) { +// uint8_t len = pack_File(f->fname, 0, d, s); + uint8_t len = pack_data(f->fname, 0, d, s); + +// for (size_t i = 0; i < len; i++) { // LOG_HEX("msg",27,d[i],s[i]); - } +// } // list_thread(); if (len) { @@ -544,6 +546,7 @@ void getAndSendFile() getLstCacheFileName(fnow); manualAaddlst(fnow); updateCacheFileName(); + cryptCdata(fnow);//密文覆盖明文 rt_thread_mdelay(1000); diff --git a/applications/cryp/cryp.c b/applications/cryp/cryp.c index b28f649..cb3ce2e 100644 --- a/applications/cryp/cryp.c +++ b/applications/cryp/cryp.c @@ -189,7 +189,7 @@ void aes_file(const char *fin, const char *fout) } write(fd_out, outbuffer, len); - LOG_I("AESed to %s done. File size from %d bytes to %d.\n", fout,file_size, len); +// LOG_I("AESed to %s done. File size from %d bytes to %d.\n", fout,file_size, len); // goto _exit; @@ -251,7 +251,7 @@ void deaes_file(const char *fin, const char *fout) lseek(fd_in, 0, SEEK_SET); buffer = (rt_uint8_t *) malloc(file_size); - outbuffer = (rt_uint8_t *) malloc(file_size + 16); //加密后最大大16字节 + outbuffer = (rt_uint8_t *) malloc(file_size); //加密后最大大16字节 if (!buffer || !outbuffer) { LOG_E("No memory for AES!\n"); diff --git a/applications/func/func.c b/applications/func/func.c index b13a58b..7603c5d 100644 --- a/applications/func/func.c +++ b/applications/func/func.c @@ -70,6 +70,9 @@ int trDataTolog(uint8_t *din, size_t len, uint8_t isTx) strcat(log,isTx?" [T]: ":" [R]: "); write(fd, log, strlen(log)); + len = cryp_data(din, len, din);//加密数据 + bytes2str(din, len, 16, " ", din); + len = strlen(din); int rst = write(fd, din, len); if (rst != len) { @@ -196,17 +199,17 @@ void upSend_thread_entry(void* parameter) time2Str(fin); strcat(fin,".bin"); packInit(&cfg, fin, 0); //写入配置 -#define PACK_SIZE 130 + cfg.fcurpiece[0] = 1; - cfg.fallpiece[0] = msg->len/PACK_SIZE+1; + cfg.fallpiece[0] = msg->len/FRAME_DATA_LEN_MAX+1; #ifdef CRYPT_BEFRE_PACK //crypt before pack //更改后3S单帧数据升为200+,超出TT最大单包数据容量,须分包 - for (size_t var = 0; var < msg->len; var=var+PACK_SIZE) { - cfg.fcurpiece[0] = var/PACK_SIZE+1; + for (size_t var = 0; var < msg->len; var=var+FRAME_DATA_LEN_MAX) { + cfg.fcurpiece[0] = var/FRAME_DATA_LEN_MAX+1; static uint8_t tmp[200]; - uint8_t tlen = (msg->len-var)>PACK_SIZE?PACK_SIZE:(msg->len-var); + uint8_t tlen = (msg->len-var)>FRAME_DATA_LEN_MAX?FRAME_DATA_LEN_MAX:(msg->len-var); uint8_t len = cryptSingleMsg(msg->data+var, tlen, tmp); // LOG_HEX("2",16,msg->data+var,tlen); @@ -1523,6 +1526,7 @@ int cacheDataToFile(uint8_t *din, size_t len) if (size > scfg.maxSizePerFile) { updateCacheFileName();//更新文件名,置于线程前避免冲突 + cryptCdata(f);//密文覆盖明文 postFileInfo(f,0);//加入待发列表 } rst=RT_EOK; @@ -1898,6 +1902,11 @@ size_t decryptCdata(const char *fin, uint8_t *dout) rt_uint8_t *buffer = RT_NULL; buffer = rt_malloc(file_size); + if (!buffer) + { + LOG_E("No memory for read cdata!\n"); + return RT_ERROR; + } read(fd, buffer, file_size); close(fd); diff --git a/applications/ttmsg/ttmsg.c b/applications/ttmsg/ttmsg.c index a6df7cf..7796398 100644 --- a/applications/ttmsg/ttmsg.c +++ b/applications/ttmsg/ttmsg.c @@ -353,6 +353,100 @@ rt_uint8_t pack_File(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[ } +/** + * 2023.11.08追加功能,缓存为密文,此函数将缓存解密后发送 + * @param fin + * @param flag + * @param dout + * @param arrLen + * @return + */ +rt_uint8_t pack_data(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[200], rt_uint8_t *arrLen) +{ + static MSG cfg; + rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间 + rt_uint8_t rst = packInit(&cfg, fin, flag); + if (rst != RT_EOK) + { + return 0; + } + + uint8_t decryptData[2048]; + size_t file_size = decryptCdata(fin, decryptData); + if (!file_size) { + LOG_E("failed to decrypt"); + return 0; + } + size_t flen = 150; // 每包数据初始长度,最大不超过 FRAME_DATA_LEN_MAX + + // 粗略调整每包大小 + if (file_size > (size_t) FRAME_DATA_LEN_MAX) + { + while ((file_size % flen) < (flen / 1.2) && flen < FRAME_DATA_LEN_MAX) // 阈值约83% + { + flen += 1; + } + if ((file_size % flen) < (flen / 1.2)) + { // 到达限制仍不符合要求 + flen = 150; + while ((file_size % flen) < (flen / 2.5) && flen < FRAME_DATA_LEN_MAX) // 调整阈值为40% + { + flen += 1; + } + } + } + else + { + flen = file_size; + } + LOG_E("p=%d",flen); + //延用算法,用特定大小依次读取数据,弃用一次性全部读入 + + uint8_t row = (file_size / flen) + (file_size % flen > 0 ? 1 : 0); + cfg.fallpiece[0] = row; + LOG_E("--------row=%d,%d",row,cfg.fallpiece[0]); + uint8_t block_size = 0; + rt_uint8_t *buffer = RT_NULL; + buffer = (rt_uint8_t *) rt_malloc(flen); + if (buffer == RT_NULL) + { + LOG_E("No memory for read %s.", fin); + return 0; + } + + for (size_t i = 0; i < file_size; i += flen) + { + if ((file_size - i) < flen) + { + block_size = file_size - i; + } + else + { + block_size = flen; + } + + rt_memset(buffer, 0x00, flen); + rt_memcpy(buffer, decryptData, block_size); + uint8_t index = (i / flen); + cfg.fcurpiece[0] = index + 1; + + //crypt before pack + static uint8_t tmp[200]; + uint8_t len = cryptSingleMsg(buffer, block_size, tmp); + uint8_t col = packMsg(&cfg, tmp, len, dout[index]); + + arrLen[index] = col; + } + + if (buffer != RT_NULL) + { + rt_free(buffer); + } + + LOG_E("row=%d,%d",row,cfg.fallpiece[0]); + return row;//cfg.fallpiece[0]; +} + /** * 天通上电 * @param parameter "0"断电,其它上电