diff --git a/applications/cryp/cryp.c b/applications/cryp/cryp.c index 536627f..b28f649 100644 --- a/applications/cryp/cryp.c +++ b/applications/cryp/cryp.c @@ -166,13 +166,6 @@ void aes_file(const char *fin, const char *fout) goto _exit; } - fd_out = open(fout, O_WRONLY | O_CREAT | O_TRUNC, 0); - if (fd_out < 0) - { - LOG_E("open the output file : %s error!\n", fout); - goto _exit; - } - rt_uint16_t file_size = lseek(fd_in, 0, SEEK_END); lseek(fd_in, 0, SEEK_SET); @@ -185,8 +178,15 @@ void aes_file(const char *fin, const char *fout) } read(fd_in, buffer, file_size); + close(fd_in);//读取完毕后及时关闭,使支持fin=fout覆盖写入 size_t len = aes_256_cbc_pkcs7(buffer, file_size, outbuffer); + fd_out = open(fout, O_WRONLY | O_CREAT | O_TRUNC, 0); + if (fd_out < 0) + { + LOG_E("open the output file : %s error!\n", fout); + goto _exit; + } write(fd_out, outbuffer, len); LOG_I("AESed to %s done. File size from %d bytes to %d.\n", fout,file_size, len); @@ -214,7 +214,7 @@ void aes_file(const char *fin, const char *fout) // return RT_EOK; } -void aes_file_test(int argc, char **argv) +void crypt_file_test(int argc, char **argv) { if (argc == 3) @@ -230,7 +230,88 @@ void aes_file_test(int argc, char **argv) } +/** + * 将文件内容采用AES-256方式加密 + * @param fin 待加密的文件名称字符串 + * @param fout 加密后文件名称字符串 + */ +void deaes_file(const char *fin, const char *fout) +{ + rt_uint8_t *buffer = RT_NULL, *outbuffer = RT_NULL; + int fd_in = -1, fd_out = -1; + + fd_in = open(fin, O_RDONLY, 0); + if (fd_in < 0) + { + LOG_E("open the input file : %s error!\n", fin); + goto _exit; + } + + rt_uint16_t file_size = lseek(fd_in, 0, SEEK_END); + lseek(fd_in, 0, SEEK_SET); + + buffer = (rt_uint8_t *) malloc(file_size); + outbuffer = (rt_uint8_t *) malloc(file_size + 16); //加密后最大大16字节 + if (!buffer || !outbuffer) + { + LOG_E("No memory for AES!\n"); + goto _exit; + } + + read(fd_in, buffer, file_size); + close(fd_in);//读取完毕后及时关闭,使支持fin=fout覆盖写入 + + size_t len = aes_256_cbc_pkcs7_de(buffer, file_size, outbuffer); + fd_out = open(fout, O_WRONLY | O_CREAT | O_TRUNC, 0); + if (fd_out < 0) + { + LOG_E("open the output file : %s error!\n", fout); + goto _exit; + } + write(fd_out, outbuffer, len); + + LOG_I("deAESed to %s done. File size from %d bytes to %d.\n", fout,file_size, len); + +// goto _exit; + + _exit: if (buffer) + { + rt_free(buffer); + } + if (outbuffer) + { + rt_free(outbuffer); + } + if (fd_in >= 0) + { + close(fd_in); + } + + if (fd_out >= 0) + { + close(fd_out); + } + +// return RT_EOK; +} +void decrypt_file_test(int argc, char **argv) +{ + + if (argc == 3) + { + deaes_file(argv[1], argv[2]); + } + else + { + rt_kprintf("Usage:\n"); + rt_kprintf("deaes_file_test [input_file] [output_file] \"input_file\" to \"output_file\" \n"); + + } + +} + #include /* 导出到自动初始化 */ MSH_CMD_EXPORT(aes_string_test, 使用AES-256加密字符串。crypt string using AES-256.); -MSH_CMD_EXPORT(aes_file_test, 使用AES-256加密文件。crypt file using AES-256.); +MSH_CMD_EXPORT(crypt_file_test, 使用AES-256加密文件。crypt file using AES-256.); +MSH_CMD_EXPORT(decrypt_file_test, 使用AES-256解密文件。decrypt file using AES-256.); diff --git a/applications/func/func.c b/applications/func/func.c index c78edde..b13a58b 100644 --- a/applications/func/func.c +++ b/applications/func/func.c @@ -154,8 +154,8 @@ return 0x63; typedef struct { - rt_uint8_t len; - rt_uint8_t data[200]; + uint16_t len; + rt_uint8_t data[300]; } SMSG;//single messgae @@ -168,6 +168,8 @@ void upSend_thread_entry(void* parameter) // LOG_HEX("--",16,msg->data,msg->len); //check status +#define CHK_STA +#ifdef CHK_STA uint8_t trycnt = 0; while (!isTTjh()) //判断TT状态 { @@ -184,7 +186,7 @@ void upSend_thread_entry(void* parameter) cacheDataToFile(msg->data, msg->len); return ; } - +#endif //打包数据 uint8_t dout[300]; @@ -200,17 +202,19 @@ void upSend_thread_entry(void* parameter) #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; - uint8_t tmp[200]; - uint8_t len = cryptSingleMsg(msg->data+var, (msg->len-var)>PACK_SIZE?PACK_SIZE:(msg->len-var), tmp); - size_t rst = packMsg(&cfg, tmp, len, dout);//packMsgs + static uint8_t tmp[200]; + uint8_t tlen = (msg->len-var)>PACK_SIZE?PACK_SIZE:(msg->len-var); + uint8_t len = cryptSingleMsg(msg->data+var, tlen, tmp); +// LOG_HEX("2",16,msg->data+var,tlen); + + size_t rst = packMsg(&cfg, tmp, len, dout);//引起数据异常 #else size_t rst = packMsg(&cfg, tmpmsg->data, msg->len, dout);//packMsgs #endif -// LOG_HEX("upSend", 27, dout, rst); + LOG_HEX("upSend", 27, dout, rst); if (rst) { if (sendMsg(dout, rst) == RT_EOK) @@ -394,10 +398,33 @@ void setCommWindow(uint8_t *t, size_t len) } -void d_sw(void) +void d_demo(int argc, char **argv) { - uint8_t cfg[]={0x03, 0x1F, 0x03, 0x20, 0x07, 0x1F, 0x09, 0x1E}; - setCommWindow(cfg, 8); + if (argc == 2) + { + int fd = open(argv[1], O_RDONLY, 0); + if (fd < 0) + { + LOG_E("open the input file : %s error!\n", argv[1]); + return; + } + rt_uint16_t file_size = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + + rt_uint8_t *buffer = RT_NULL; + buffer = (rt_uint8_t *) malloc(file_size); + if (!buffer) + { + LOG_E("No memory for reading file!\n"); + close(fd); + return; + } + + read(fd, buffer, file_size); + close(fd); + upSend(buffer, file_size); + rt_free(buffer); + } } @@ -1851,6 +1878,44 @@ int isNeedRestore(void) return rst; } +/** + * 将密文解密至内存 + * @param fin 密文文件 + * @param buffer 解密后数据 + * @return 数据长度 + */ +size_t decryptCdata(const char *fin, uint8_t *dout) +{ + int fd = -1; + fd = open(fin, O_RDONLY, 0); + if (fd < 0) + { + LOG_E("[hex] open the input file : %s error.", fin); + return RT_ERROR; + } + size_t file_size = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + + rt_uint8_t *buffer = RT_NULL; + buffer = rt_malloc(file_size); + read(fd, buffer, file_size); + close(fd); + + size_t rst = decryp_data(buffer, file_size, dout); + rt_free(buffer); + + return rst; +} + +/** + * 加密待发的缓存文件,覆盖源文件 + * @param f 缓存文件 + */ +void cryptCdata(const char *f) +{ + aes_file(f, f); +} + #define FUNC_DEMO #ifdef FUNC_DEMO //测试时导出命令到控制台 @@ -1863,7 +1928,7 @@ MSH_CMD_EXPORT(d_getFileSize,d_getFileSize); MSH_CMD_EXPORT(isEthUP,isEthUP); MSH_CMD_EXPORT(reportSysCfg,reportSysCfg); MSH_CMD_EXPORT(d_isInFence,d_isInFence); -MSH_CMD_EXPORT(d_sw,msw); +MSH_CMD_EXPORT(d_demo,demo for debug function); MSH_CMD_EXPORT(set3SRTC,发送指令设置RTC); #endif diff --git a/applications/main.c b/applications/main.c index 1fd8708..c5ada8a 100644 --- a/applications/main.c +++ b/applications/main.c @@ -86,7 +86,7 @@ void show_version(void) char str[30]; uint8_t t[10]; size_t len=time2Byte(t); - rt_kprintf("SW Version: %s, build-%s\n","2.61d",bytes2str(t, 3, 10, "", str)); + rt_kprintf("SW Version: %s, build-%s\n","2.7d",bytes2str(t, 3, 10, "", str)); } MSH_CMD_EXPORT(show_version,显示版本号);