添加解密函数

This commit is contained in:
CSSC-WORK\murmur 2023-08-18 15:36:41 +08:00
parent c165e15b80
commit a8877e415b
4 changed files with 75 additions and 28 deletions

View File

@ -21,7 +21,7 @@
#define KEY_LEN 32//密钥长度 #define KEY_LEN 32//密钥长度
#endif #endif
int aes_128_cbc_pkcs7(rt_uint8_t *data, rt_uint16_t len, rt_uint8_t *dout) size_t aes_128_cbc_pkcs7(rt_uint8_t *data, size_t len, rt_uint8_t *dout)
{ {
// AES-256 // AES-256
// 工作模式 CBC ,填充模式 PKCS7不同语言要保持一致。 // 工作模式 CBC ,填充模式 PKCS7不同语言要保持一致。
@ -42,38 +42,73 @@ int aes_128_cbc_pkcs7(rt_uint8_t *data, rt_uint16_t len, rt_uint8_t *dout)
rt_uint16_t cnt = (m + 1) * 16; rt_uint16_t cnt = (m + 1) * 16;
rt_uint8_t *pbuff = rt_malloc(cnt); //分配内存 rt_uint8_t *pbuff = rt_malloc(cnt); //分配内存
// rt_kprintf("%d -- %d %d new len is %d.\n", len,m,n,cnt);
rt_memcpy(pbuff, data, len); //data -> buff rt_memcpy(pbuff, data, len); //data -> buff
rt_memset(pbuff + len, (16 - n), 16 - n); //按PKCS7规则填充 rt_memset(pbuff + len, (16 - n), 16 - n); //按PKCS7规则填充
// for (size_t i = 0; i < (m + 1) * 16; i++)
// {
// rt_kprintf("0x%02X ", p_key[i]);
// }
unsigned char edata[cnt];//输出buff unsigned char edata[cnt];//输出buff
mbedtls_aes_context ctx; //创建结构体 mbedtls_aes_context ctx; //创建结构体
mbedtls_aes_init(&ctx); //初始化 mbedtls_aes_init(&ctx); //初始化
mbedtls_aes_setkey_enc(&ctx, key, KEY_LEN * 8); //设置密钥 mbedtls_aes_setkey_enc(&ctx, key, KEY_LEN * 8); //设置密钥
// rt_kprintf("--%d\n", rst); // rt_kprintf("--%d\n", rst);
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, (m + 1) * 16, iv, pbuff, edata); //加密 mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, cnt, iv, pbuff, edata); //加密
// rt_kprintf("--%d\n", rst); // rt_kprintf("--%d\n", rst);
mbedtls_aes_free(&ctx); //释放结构体 mbedtls_aes_free(&ctx); //释放结构体
rt_memcpy(dout, edata, cnt); rt_memcpy(dout, edata, cnt);
rt_free(pbuff); //释放内存 rt_free(pbuff); //释放内存
// for (rt_uint16_t i = 0; i < cnt; i++)
// {
// rt_kprintf("0x%02X ", edata[i]);
// }
// rt_kprintf("\n---DONE.---\n");
return cnt; return cnt;
} }
size_t aes_128_cbc_pkcs7_de(rt_uint8_t *data, size_t len, rt_uint8_t *dout)
{
// AES-256
// 工作模式 CBC ,填充模式 PKCS7不同语言要保持一致。
// PKCS7规则是长度不够时缺几位补几个几长度够时也要补
// key长度由KEY_LEN定义默认32位
// iv取密钥前16位
// 已多平台验证
unsigned char key[KEY_LEN];
rt_memset(key, 0x00, KEY_LEN); //初始化密钥填充0x00
rt_strcpy(key, "Cssc722.Cssc722.Cssc722.."); //写入密钥
unsigned char iv[16];
rt_memcpy(iv, key, 16); //取密钥前16为偏移量
unsigned char edata[len];//输出buff
memset(edata,0xff,len);
mbedtls_aes_context ctx; //创建结构体
mbedtls_aes_init(&ctx); //初始化
mbedtls_aes_setkey_dec(&ctx, key, KEY_LEN * 8); //设置密钥
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, len, iv, data, edata); //加密
mbedtls_aes_free(&ctx); //释放结构体
// LOG_HEX("de",16,edata,len);
size_t rst = len-edata[len-1];
edata[rst]='\0';
// LOG_HEX("de",16,edata,len);
rt_memcpy(dout, edata, rst+1);
return rst;
}
/**
*
* @param din
* @param len
* @param dout
* @return
*/
size_t decryp_data(const uint8_t *din, size_t len, uint8_t *dout)
{
size_t nlen = aes_128_cbc_pkcs7_de(din, len, dout);
return nlen;
}
/** /**
* *
* @param din * @param din
@ -88,20 +123,22 @@ size_t cryp_data(const uint8_t *din, int len, uint8_t *dout)
} }
void aes_string_test(int argc, char **argv) void aes_string_test(int argc, char **argv)
{ {
rt_uint8_t data[128]; rt_uint8_t data[128];
size_t len; size_t len;
rt_uint8_t str[100];
if (argc == 1) if (argc == 1)
{ {
len=aes_128_cbc_pkcs7("Test String", strlen("Test String"), data); len=cryp_data("Test String", strlen("Test String"), data);
} }
else if (argc == 2) else if (argc == 2)
{ {
len=aes_128_cbc_pkcs7(argv[1], strlen(argv[1]), data); len=cryp_data(argv[1], strlen(argv[1]), data);
} }
for (size_t var = 0; var < len; ++var) { LOG_HEX("cry",16,data,len);
rt_kprintf("0x%02X ", data[var]); len = decryp_data(data, len, str);
} rt_kprintf("decrypt str is '%s'\n", str,len);
rt_kprintf("\n---DONE.---\n"); // rt_kprintf("\n---DONE.---\n");
} }
/** /**

View File

@ -11,5 +11,6 @@
#define CRYP_H_ #define CRYP_H_
void aes_file(const char *fin, const char *fout); void aes_file(const char *fin, const char *fout);
size_t cryp_data(const uint8_t *din, int len, uint8_t *dout); size_t cryp_data(const uint8_t *din, size_t len, uint8_t *dout);
size_t decryp_data(const uint8_t *din, size_t len, uint8_t *dout);
#endif /* CRYP_H_ */ #endif /* CRYP_H_ */

View File

@ -402,6 +402,14 @@ static int getAndCheckLoc(uint8_t *dout, size_t pairCnt)
{ {
uint8_t loc[200]; uint8_t loc[200];
size_t cnt = getLoc(loc,pairCnt); size_t cnt = getLoc(loc,pairCnt);
if (!cnt) {
LOG_W("位置信息还未准备好。");
}
else {
memcpy(dout,loc,cnt);
}
return cnt;
while (!cnt) while (!cnt)
{ {
static uint8_t i=0; static uint8_t i=0;
@ -590,7 +598,7 @@ int isInFence(uint8_t *loc);
* 10s更新一次 * 10s更新一次
* @return * @return
*/ */
static void packAndChkLoc_thread_entry(void *parameter) static void packAndSendLoc_thread_entry(void *parameter)
{ {
while (isTCPok()) while (isTCPok())
{ {
@ -653,7 +661,7 @@ static void packAndChkLoc_thread_entry(void *parameter)
// } // }
//} //}
void initUpSendLoc() void upSendLoc()
{ {
// isReadyToSendLoc = rt_sem_create("SendLoc", 0, RT_IPC_FLAG_PRIO); // isReadyToSendLoc = rt_sem_create("SendLoc", 0, RT_IPC_FLAG_PRIO);
// repLoc = rt_timer_create("repLoc", cb_upSendLoc, // repLoc = rt_timer_create("repLoc", cb_upSendLoc,
@ -673,7 +681,7 @@ void initUpSendLoc()
// return; // return;
// } // }
rt_thread_t thread = rt_thread_create("PCLoc", packAndChkLoc_thread_entry, RT_NULL, 1024 * 1, 27, 10); rt_thread_t thread = rt_thread_create("PCLoc", packAndSendLoc_thread_entry, RT_NULL, 1024 * 1, 27, 10);
/* 创建成功则启动线程 */ /* 创建成功则启动线程 */
if (thread != RT_NULL) if (thread != RT_NULL)
{ {
@ -1265,7 +1273,7 @@ int isEthUP()
MSH_CMD_EXPORT(d_getFreeSpace,getFreeSpace); MSH_CMD_EXPORT(d_getFreeSpace,getFreeSpace);
MSH_CMD_EXPORT(selfTest,sysSelfTest); MSH_CMD_EXPORT(selfTest,sysSelfTest);
MSH_CMD_EXPORT(d_packLocMsg,dpackLocMsg); MSH_CMD_EXPORT(d_packLocMsg,dpackLocMsg);
MSH_CMD_EXPORT(initUpSendLoc,initUpSendLoc); MSH_CMD_EXPORT(upSendLoc,);
MSH_CMD_EXPORT(d_cacheData,d_cacheData); MSH_CMD_EXPORT(d_cacheData,d_cacheData);
MSH_CMD_EXPORT(d_getFileSize,d_getFileSize); MSH_CMD_EXPORT(d_getFileSize,d_getFileSize);
MSH_CMD_EXPORT(isEthUP,isEthUP); MSH_CMD_EXPORT(isEthUP,isEthUP);

View File

@ -150,9 +150,10 @@ extern SYS_CFG scfg;
void initcfg() void initcfg()
{ {
// updatecfg(); memset(rstInfo,0x3f,RST_LEN);//初始化
bootstamp = rt_tick_get_millisecond(); bootstamp = rt_tick_get_millisecond();
} }
INIT_APP_EXPORT(initcfg);
/** /**
* *