Просмотр исходного кода

update:废弃crc.h, luat_lib_crypto.c改用luat_crc.h里面的API

https://gitee.com/openLuat/LuatOS/issues/ID3U4R?from=project-issue
Jiangqian 3 месяцев назад
Родитель
Сommit
da5b8d4814

+ 4 - 2
components/crypto/luat_crc.c

@@ -62,10 +62,12 @@ uint8_t luat_crc8(const void *data, uint32_t len, uint8_t start, uint8_t poly, u
 /************************************************************************/
 /*  CRC16                                                                */
 /************************************************************************/
-uint16_t luat_crc16(const void *data, uint32_t len, uint16_t start, uint16_t poly, uint8_t is_reverse)
+
+uint16_t luat_crc16(const void *data, uint32_t len, uint16_t start,uint16_t final, uint16_t poly, uint8_t is_reverse)
 {
 	uint32_t i;
 	uint16_t CRC16 = start;
+	uint16_t CRC16_out = final;
 	uint16_t wTemp = poly;
 	uint8_t *Src = (uint8_t *)data;
 	if (is_reverse)
@@ -123,7 +125,7 @@ uint16_t luat_crc16(const void *data, uint32_t len, uint16_t start, uint16_t pol
 		}
 	}
 
-	return CRC16;
+	return (uint16_t)(CRC16^CRC16_out);
 }
 
 

+ 1 - 1
luat/include/luat_crypto.h

@@ -161,7 +161,7 @@ uint8_t luat_crc8(const void *data, uint32_t len, uint8_t start, uint8_t poly, u
  * @param is_reverse 是否逆序计算,顺序从MSB算到LSB,逆序从LSB算到MSB,不清楚的2个都试试
  * @return 输出值
  */
-uint16_t luat_crc16(const void *data, uint32_t len, uint16_t start, uint16_t poly, uint8_t is_reverse);
+uint16_t luat_crc16(const void *data, uint32_t len, uint16_t start, uint16_t final, uint16_t poly, uint8_t is_reverse);
 
 /**
  * @brief crc32常用算法

+ 49 - 11
luat/modules/luat_lib_crypto.c

@@ -283,7 +283,28 @@ int l_crypto_cipher_decrypt(lua_State *L) {
 }
 
 #include "crc.h"
-
+typedef struct{
+const char *name;   //参数模型
+uint16_t crc16_polynomial;   //多项式
+uint16_t initial_value;  //初始值
+uint16_t finally_data;  //结果异或值
+uint8_t input_reverse; //输入数据反转
+uint8_t output_reverse; //输出数据反转
+} crc16method;
+
+static const crc16method crc16method_table[] = 
+{
+    {(const char*)"IBM", 0x8005, 0x0000, 0x0000, 1, 1},
+    {(const char*)"MAXIM", 0x8005, 0x0000, 0xffff, 1, 1}, 
+    {(const char*)"USB", 0x8005, 0xffff, 0xffff, 1, 1}, 
+    {(const char*)"MODBUS", 0x8005, 0xffff, 0x0000, 1, 1}, 
+    {(const char*)"CCITT", 0x1021, 0x0000, 0x0000, 1, 1}, 
+    {(const char*)"CCITT-FALSE", 0x1021, 0xffff, 0x0000, 0, 0}, 
+    {(const char*)"X25", 0x1021, 0xffff, 0xffff, 1, 1}, 
+    {(const char*)"XMODEM", 0x1021, 0x0000,0x0000, 0, 0}, 
+    {(const char*)"DNP", 0x3D65, 0x0000, 0xffff, 1, 1},
+    {(const char*)"USER-DEFINED", 0x0000, 0x0000,0x0000, 0, 0},
+};
 /**
 计算CRC16
 @api crypto.crc16(method, data, poly, initial, finally, inReversem outReverse)
@@ -308,6 +329,11 @@ static int l_crypto_crc16(lua_State *L)
     size_t inputlen = 0;
     const unsigned char *inputData = NULL;
     const char  *inputmethod = (const char*)luaL_checkstring(L, 1);
+    uint16_t poly_default = 0x0000;
+    uint16_t initial_default = 0x0000;
+    uint16_t finally_default = 0x0000;
+    uint16_t in_reverse = 0;
+    uint16_t out_reverse = 0;
     if(lua_isuserdata(L, 2))
     {
         luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
@@ -315,13 +341,25 @@ static int l_crypto_crc16(lua_State *L)
         inputData = (const unsigned char *)(buff->addr + buff->cursor);
     }else{
         inputData = (const unsigned char*)lua_tolstring(L,2,&inputlen);
+    }    
+    for (int i = 0; i < sizeof(crc16method_table)/sizeof(crc16method_table[0]); i++)
+    {
+        if (strcmp(crc16method_table[i].name, inputmethod) == 0)
+        {
+            poly_default = crc16method_table[i].crc16_polynomial;
+            initial_default = crc16method_table[i].initial_value;
+            finally_default = crc16method_table[i].finally_data;
+            in_reverse = crc16method_table[i].input_reverse;
+            out_reverse = crc16method_table[i].output_reverse;
+        }
     }
-    uint16_t poly = (uint16_t)luaL_optnumber(L,3,0x0000);
-    uint16_t initial = (uint16_t)luaL_optnumber(L,4,0x0000);
-    uint16_t finally = (uint16_t)luaL_optnumber(L,5,0x0000);
-    uint8_t inReverse = (uint8_t)luaL_optnumber(L,6,0);
-    uint8_t outReverse = (uint8_t)luaL_optnumber(L,7,0);
-    lua_pushinteger(L, calcCRC16(inputData, inputmethod,inputlen,poly,initial,finally,inReverse,outReverse));
+    uint16_t poly = (uint16_t)luaL_optnumber(L,3,poly_default);
+    uint16_t initial = (uint16_t)luaL_optnumber(L,4,initial_default);
+    uint16_t finally = (uint16_t)luaL_optnumber(L,5,finally_default);
+    uint8_t inReverse = (uint8_t)luaL_optnumber(L,6,in_reverse);
+    uint8_t outReverse = (uint8_t)luaL_optnumber(L,7,out_reverse);
+    
+    lua_pushinteger(L, luat_crc16(inputData, inputlen, initial, finally, poly, inReverse));
     return 1;
 }
 
@@ -343,7 +381,7 @@ static int l_crypto_crc16_modbus(lua_State *L)
     const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
     uint16_t crc_init = (uint16_t)luaL_optinteger(L, 2, 0xFFFF);
 
-    lua_pushinteger(L, calcCRC16_modbus(inputData, len, crc_init));
+    lua_pushinteger(L, luat_crc16_modbus(inputData, len));
     return 1;
 }
 
@@ -387,10 +425,10 @@ local crc = crypto.crc8(data, 0x31, 0xff, false)
  */
 static int l_crypto_crc8(lua_State *L)
 {
-    size_t len = 0;
+    size_t len = 0; 
     const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
     if (!lua_isinteger(L, 2)) {
-        lua_pushinteger(L, calcCRC8(inputData, len));
+        lua_pushinteger(L, luat_crc8(inputData, len, 0x00, 0x07, 0));//poly默认值0x0000
     } else {
     	uint8_t poly = (uint8_t)lua_tointeger(L, 2);
     	uint8_t start = (uint8_t)luaL_optinteger(L, 3, 0);
@@ -400,7 +438,7 @@ static int l_crypto_crc8(lua_State *L)
     	}
 		lua_pushinteger(L, luat_crc8(inputData, len, start, poly, is_rev));
     }
-    return 1;
+    return 1; 
 }
 
 

+ 78 - 0
module/Air8000/demo/crypto/crc_crypto_app.lua

@@ -0,0 +1,78 @@
+local function crypto_task_func()
+    
+    -- 计算CRC16_IBM
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("IBM",originStr)
+    log.info("crc16_IBM", crc16)
+    -- 计算CRC16_MAXIM
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("MAXIM",originStr)
+    log.info("crc16_MAXIM", crc16)
+    -- 计算CRC16_modbus
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("USB",originStr)
+    log.info("crc16_USB", crc16)
+    -- 计算CRC16_modbus
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("MODBUS",originStr)
+    log.info("crc16_MODBUS", crc16)
+    -- 计算CRC16_modbus
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("CCITT",originStr)
+    log.info("crc16_CCITT", crc16)
+    -- 计算CRC16_modbus
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("CCITT-FALSE",originStr)
+    log.info("crc16_CCITT-FALSE", crc16)
+    -- 计算CRC16_modbus
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("X25",originStr)
+    log.info("crc16_X25", crc16)
+    -- 计算CRC16_modbus
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("XMODEM",originStr)
+    log.info("crc16_XMODEM", crc16)
+    -- 计算CRC16_modbus
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("DNP",originStr)
+    log.info("crc16_DNP", crc16)
+    -- 计算CRC16_modbus
+    local originStr = "123456sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc16 = crypto.crc16("USER-DEFINE",originStr)
+    log.info("crc16_USER-DEFINE", crc16)
+    
+    -- 计算CRC16 modbus
+    local crc16 = crypto.crc16_modbus("123456sdfdsfdsfdsffdsfdsfsdfs1234")
+    log.info("crc16", crc16)
+    crc16 = crypto.crc16_modbus("123456sdfdsfdsfdsffdsfdsfsdfs1234", 0xFFFF)
+    log.info("crc16", crc16)
+    
+    -- 计算CRC32
+    local data = "123456sdfdsfdsfdsffdsfdsfsdfs1234" 
+    local crc32 = crypto.crc32(data)
+    log.info("crc32", crc32) --21438764
+    -- start和poly可选, 是 2025.4.14 新增的参数
+    local crc32 = crypto.crc32(data, 0xFFFFFFFF, 0x04C11DB7, 0xFFFFFFFF) --等同于crypto.crc32(data)
+    log.info("crc32", crc32)
+
+    -- 计算CRC8
+    local data= "sdfdsfdsfdsffdsfdsfsdfs1234"
+    local crc8 = crypto.crc8(data)
+    log.info("crc8", crc8)
+    local crc8 = crypto.crc8(data, 0x31, 0xff, false)
+    log.info("crc8", crc8)  
+        
+    log.info("随机数测试")
+    for i=1, 10 do
+        sys.wait(100)
+        log.info("crypto", "真随机数",string.unpack("I",crypto.trng(4)))
+        -- log.info("crypto", "伪随机数",math.random()) -- 输出的是浮点数,不推荐
+        -- log.info("crypto", "伪随机数",math.random(1, 65525)) -- 不推荐
+    end
+
+    log.info("crypto", "ALL Done")
+    sys.wait(100000)
+end    
+
+--创建一个task,并且运行task的主函数crypto_task_func
+sys.taskInit(crypto_task_func)

+ 2 - 1
module/Air8000/demo/crypto/main.lua

@@ -63,7 +63,8 @@ end
 -- end, 3000)
 
 -- 加载crypto_app应用功能模块
-require "crypto_app"
+-- require "crypto_app"
+require "crc_crypto_app"
 
 -- 用户代码已结束---------------------------------------------
 -- 结尾总是这一句