luat_lib_crypto.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. @module crypto
  3. @summary 加解密和hash函数
  4. @version 1.0
  5. @date 2020.07.03
  6. @demo crypto
  7. @tag LUAT_USE_CRYPTO
  8. */
  9. #include "luat_base.h"
  10. #include "luat_crypto.h"
  11. #include "luat_mem.h"
  12. #include "luat_str.h"
  13. #include <time.h>
  14. #include "luat_zbuff.h"
  15. // #include "mbedtls/md.h"
  16. #define LUAT_LOG_TAG "crypto"
  17. #define LUAT_CRYPTO_TYPE "crypto"
  18. #include "luat_log.h"
  19. static const unsigned char hexchars[] = "0123456789ABCDEF";
  20. static void fixhex(const char* source, char* dst, size_t len) {
  21. for (size_t i = 0; i < len; i++)
  22. {
  23. char ch = *(source+i);
  24. dst[i*2] = hexchars[(unsigned char)ch >> 4];
  25. dst[i*2+1] = hexchars[(unsigned char)ch & 0xF];
  26. }
  27. }
  28. /**
  29. 计算md5值
  30. @api crypto.md5(str)
  31. @string 需要计算的字符串
  32. @return string 计算得出的md5值的hex字符串
  33. @usage
  34. -- 计算字符串"abc"的md5
  35. log.info("md5", crypto.md5("abc"))
  36. */
  37. static int l_crypto_md5(lua_State *L) {
  38. size_t size = 0;
  39. const char* str = luaL_checklstring(L, 1, &size);
  40. char tmp[32] = {0};
  41. char dst[32] = {0};
  42. if (luat_crypto_md5_simple(str, size, tmp) == 0) {
  43. fixhex(tmp, dst, 16);
  44. lua_pushlstring(L, dst, 32);
  45. return 1;
  46. }
  47. return 0;
  48. }
  49. /**
  50. 计算hmac_md5值
  51. @api crypto.hmac_md5(str, key)
  52. @string 需要计算的字符串
  53. @string 密钥
  54. @return string 计算得出的hmac_md5值的hex字符串
  55. @usage
  56. -- 计算字符串"abc"的hmac_md5
  57. log.info("hmac_md5", crypto.hmac_md5("abc", "1234567890"))
  58. */
  59. static int l_crypto_hmac_md5(lua_State *L) {
  60. size_t str_size = 0;
  61. size_t key_size = 0;
  62. const char* str = luaL_checklstring(L, 1, &str_size);
  63. const char* key = luaL_checklstring(L, 2, &key_size);
  64. char tmp[32] = {0};
  65. char dst[32] = {0};
  66. if (luat_crypto_hmac_md5_simple(str, str_size, key, key_size, tmp) == 0) {
  67. fixhex(tmp, dst, 16);
  68. lua_pushlstring(L, dst, 32);
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. /**
  74. 计算sha1值
  75. @api crypto.sha1(str)
  76. @string 需要计算的字符串
  77. @return string 计算得出的sha1值的hex字符串
  78. @usage
  79. -- 计算字符串"abc"的sha1
  80. log.info("sha1", crypto.sha1("abc"))
  81. */
  82. static int l_crypto_sha1(lua_State *L) {
  83. size_t size = 0;
  84. const char* str = luaL_checklstring(L, 1, &size);
  85. char tmp[40] = {0};
  86. char dst[40] = {0};
  87. if (luat_crypto_sha1_simple(str, size, tmp) == 0) {
  88. fixhex(tmp, dst, 20);
  89. lua_pushlstring(L, dst, 40);
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. /**
  95. 计算hmac_sha1值
  96. @api crypto.hmac_sha1(str, key)
  97. @string 需要计算的字符串
  98. @string 密钥
  99. @return string 计算得出的hmac_sha1值的hex字符串
  100. @usage
  101. -- 计算字符串"abc"的hmac_sha1
  102. log.info("hmac_sha1", crypto.hmac_sha1("abc", "1234567890"))
  103. */
  104. static int l_crypto_hmac_sha1(lua_State *L) {
  105. size_t str_size = 0;
  106. size_t key_size = 0;
  107. const char* str = luaL_checklstring(L, 1, &str_size);
  108. const char* key = luaL_checklstring(L, 2, &key_size);
  109. char tmp[40] = {0};
  110. char dst[40] = {0};
  111. if (luat_crypto_hmac_sha1_simple(str, str_size, key, key_size, tmp) == 0) {
  112. fixhex(tmp, dst, 20);
  113. lua_pushlstring(L, dst, 40);
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. /**
  119. 计算sha256值
  120. @api crypto.sha256(str)
  121. @string 需要计算的字符串
  122. @return string 计算得出的sha256值的hex字符串
  123. @usage
  124. -- 计算字符串"abc"的sha256
  125. log.info("sha256", crypto.sha256("abc"))
  126. */
  127. static int l_crypto_sha256(lua_State *L) {
  128. size_t size = 0;
  129. const char* str = luaL_checklstring(L, 1, &size);
  130. char tmp[64] = {0};
  131. char dst[64] = {0};
  132. if (luat_crypto_sha256_simple(str, size, tmp) == 0) {
  133. fixhex(tmp, dst, 32);
  134. lua_pushlstring(L, dst, 64);
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. /**
  140. 计算hmac_sha256值
  141. @api crypto.hmac_sha256(str, key)
  142. @string 需要计算的字符串
  143. @string 密钥
  144. @return string 计算得出的hmac_sha256值的hex字符串
  145. @usage
  146. -- 计算字符串"abc"的hmac_sha256
  147. log.info("hmac_sha256", crypto.hmac_sha256("abc", "1234567890"))
  148. */
  149. static int l_crypto_hmac_sha256(lua_State *L) {
  150. size_t str_size = 0;
  151. size_t key_size = 0;
  152. const char* str = luaL_checklstring(L, 1, &str_size);
  153. const char* key = luaL_checklstring(L, 2, &key_size);
  154. char tmp[64] = {0};
  155. char dst[64] = {0};
  156. if (key_size > 64) {
  157. luat_crypto_sha256_simple(key, key_size, dst);
  158. key = (const char*)dst;
  159. key_size = 64;
  160. }
  161. if (luat_crypto_hmac_sha256_simple(str, str_size, key, key_size, tmp) == 0) {
  162. fixhex(tmp, dst, 32);
  163. lua_pushlstring(L, dst, 64);
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. //---
  169. /**
  170. 计算sha512值
  171. @api crypto.sha512(str)
  172. @string 需要计算的字符串
  173. @return string 计算得出的sha512值的hex字符串
  174. @usage
  175. -- 计算字符串"abc"的sha512
  176. log.info("sha512", crypto.sha512("abc"))
  177. */
  178. static int l_crypto_sha512(lua_State *L) {
  179. size_t size = 0;
  180. const char* str = luaL_checklstring(L, 1, &size);
  181. char tmp[128] = {0};
  182. char dst[128] = {0};
  183. if (luat_crypto_sha512_simple(str, size, tmp) == 0) {
  184. fixhex(tmp, dst, 64);
  185. lua_pushlstring(L, dst, 128);
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. /**
  191. 计算hmac_sha512值
  192. @api crypto.hmac_sha512(str, key)
  193. @string 需要计算的字符串
  194. @string 密钥
  195. @return string 计算得出的hmac_sha512值的hex字符串
  196. @usage
  197. -- 计算字符串"abc"的hmac_sha512
  198. log.info("hmac_sha512", crypto.hmac_sha512("abc", "1234567890"))
  199. */
  200. static int l_crypto_hmac_sha512(lua_State *L) {
  201. size_t str_size = 0;
  202. size_t key_size = 0;
  203. const char* str = luaL_checklstring(L, 1, &str_size);
  204. const char* key = luaL_checklstring(L, 2, &key_size);
  205. char tmp[128] = {0};
  206. char dst[128] = {0};
  207. if (key_size > 128) {
  208. luat_crypto_sha512_simple(key, key_size, dst);
  209. key = (const char*)dst;
  210. key_size = 128;
  211. }
  212. if (luat_crypto_hmac_sha512_simple(str, str_size, key, key_size, tmp) == 0) {
  213. fixhex(tmp, dst, 64);
  214. lua_pushlstring(L, dst, 128);
  215. return 1;
  216. }
  217. return 0;
  218. }
  219. int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
  220. luat_crypto_cipher_ctx_t cctx = {0};
  221. cctx.cipher = luaL_optlstring(L, 1, "AES-128-ECB", &cctx.cipher_size);
  222. cctx.pad = luaL_optlstring(L, 2, "PKCS7", &cctx.pad_size);
  223. cctx.str = luaL_checklstring(L, 3, &cctx.str_size);
  224. cctx.key = luaL_checklstring(L, 4, &cctx.key_size);
  225. cctx.iv = luaL_optlstring(L, 5, "", &cctx.iv_size);
  226. cctx.flags = flags;
  227. luaL_Buffer buff;
  228. luaL_buffinitsize(L, &buff, cctx.str_size + 16);
  229. cctx.outbuff = buff.b;
  230. int ret = luat_crypto_cipher_xxx(&cctx);
  231. if (ret) {
  232. return 0;
  233. }
  234. luaL_pushresultsize(&buff, cctx.outlen);
  235. return 1;
  236. }
  237. /**
  238. 对称加密
  239. @api crypto.cipher_encrypt(type, padding, str, key, iv)
  240. @string 算法名称, 例如 AES-128-ECB/AES-128-CBC, 可查阅crypto.cipher_list()
  241. @string 对齐方式, 支持PKCS7/ZERO/ONE_AND_ZEROS/ZEROS_AND_LEN/NONE
  242. @string 需要加密的数据
  243. @string 密钥,需要对应算法的密钥长度
  244. @string IV值, 非ECB算法需要
  245. @return string 加密后的字符串
  246. @usage
  247. -- 计算AES
  248. local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
  249. local data2 = crypto.cipher_encrypt("AES-128-CBC", "PKCS7", "1234567890123456", "1234567890123456", "1234567890666666")
  250. */
  251. int l_crypto_cipher_encrypt(lua_State *L) {
  252. return l_crypto_cipher_xxx(L, 1);
  253. }
  254. /**
  255. 对称解密
  256. @api crypto.cipher_decrypt(type, padding, str, key, iv)
  257. @string 算法名称, 例如 AES-128-ECB/AES-128-CBC, 可查阅crypto.cipher_list()
  258. @string 对齐方式, 支持PKCS7/ZERO/ONE_AND_ZEROS/ZEROS_AND_LEN/NONE
  259. @string 需要解密的数据
  260. @string 密钥,需要对应算法的密钥长度
  261. @string IV值, 非ECB算法需要
  262. @return string 解密后的字符串
  263. @usage
  264. -- 用AES加密,然后用AES解密
  265. local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
  266. local data2 = crypto.cipher_decrypt("AES-128-ECB", "PKCS7", data, "1234567890123456")
  267. -- data的hex为 757CCD0CDC5C90EADBEEECF638DD0000
  268. -- data2的值为 1234567890123456
  269. */
  270. int l_crypto_cipher_decrypt(lua_State *L) {
  271. return l_crypto_cipher_xxx(L, 0);
  272. }
  273. #include "crc.h"
  274. /**
  275. 计算CRC16
  276. @api crypto.crc16(method, data, poly, initial, finally, inReversem outReverse)
  277. @string CRC16模式("IBM","MAXIM","USB","MODBUS","CCITT","CCITT-FALSE","X25","XMODEM","DNP","USER-DEFINED")
  278. @string 字符串
  279. @int poly值
  280. @int initial值
  281. @int finally值
  282. @int 输入反转,1反转,默认0不反转
  283. @int 输入反转,1反转,默认0不反转
  284. @return int 对应的CRC16值
  285. @usage
  286. -- 计算CRC16
  287. local crc = crypto.crc16("")
  288. */
  289. static int l_crypto_crc16(lua_State *L)
  290. {
  291. size_t inputlen;
  292. const unsigned char *inputData;
  293. const char *inputmethod = (const char*)luaL_checkstring(L, 1);
  294. if(lua_isuserdata(L, 2))
  295. {
  296. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  297. inputlen = buff->len - buff->cursor;
  298. inputData = (const unsigned char *)(buff->addr + buff->cursor);
  299. }else{
  300. inputData = (const unsigned char*)lua_tolstring(L,2,&inputlen);
  301. }
  302. uint16_t poly = luaL_optnumber(L,3,0x0000);
  303. uint16_t initial = luaL_optnumber(L,4,0x0000);
  304. uint16_t finally = luaL_optnumber(L,5,0x0000);
  305. uint8_t inReverse = luaL_optnumber(L,6,0);
  306. uint8_t outReverse = luaL_optnumber(L,7,0);
  307. lua_pushinteger(L, calcCRC16(inputData, inputmethod,inputlen,poly,initial,finally,inReverse,outReverse));
  308. return 1;
  309. }
  310. /**
  311. 直接计算modbus的crc16值
  312. @api crypto.crc16_modbus(data, start)
  313. @string 数据
  314. @int 初始化值,默认0xFFFF
  315. @return int 对应的CRC16值
  316. @usage
  317. -- 计算CRC16 modbus
  318. local crc = crypto.crc16_modbus(data)
  319. -- 2023.11.06 新增初始值设置
  320. crc = crypto.crc16_modbus(data, 0xFFFF)
  321. */
  322. static int l_crypto_crc16_modbus(lua_State *L)
  323. {
  324. size_t len = 0;
  325. const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
  326. uint16_t crc_init = luaL_optinteger(L, 2, 0xFFFF);
  327. lua_pushinteger(L, calcCRC16_modbus(inputData, len, crc_init));
  328. return 1;
  329. }
  330. /**
  331. 计算crc32值
  332. @api crypto.crc32(data, start, poly, endv)
  333. @string 数据
  334. @int 初始化值,默认0xFFFFFFFF
  335. @int crc多项式,可选,默认0x04C11DB7
  336. @int 结束值,可选,默认0xFFFFFFFF,计算结果异或结束值才是最终输出值
  337. @return int 对应的CRC32值
  338. @usage
  339. -- 计算CRC32
  340. local crc = crypto.crc32(data)
  341. -- start和poly可选, 是 2025.4.14 新增的参数
  342. local crc = crypto.crc32(data, 0xFFFFFFFF, 0x04C11DB7, 0xFFFFFFFF) --等同于crypto.crc32(data)
  343. */
  344. static int l_crypto_crc32(lua_State *L)
  345. {
  346. size_t len = 0;
  347. const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
  348. uint32_t start = luaL_optinteger(L, 2, 0xffffffff);
  349. uint32_t poly = luaL_optinteger(L, 3, 0x04C11DB7);
  350. uint32_t end = luaL_optinteger(L, 4, 0xffffffff);
  351. lua_pushinteger(L, luat_crc32(inputData, len, start, poly) ^ end);
  352. return 1;
  353. }
  354. /**
  355. 计算crc8值
  356. @api crypto.crc8(data, poly, start, revert)
  357. @string 数据
  358. @int crc多项式,可选,如果不写,将忽略除了数据外所有参数
  359. @int crc初始值,可选,默认0
  360. @boolean 是否需要逆序处理,默认否
  361. @return int 对应的CRC8值
  362. @usage
  363. -- 计算CRC8
  364. local crc = crypto.crc8(data)
  365. local crc = crypto.crc8(data, 0x31, 0xff, false)
  366. */
  367. static int l_crypto_crc8(lua_State *L)
  368. {
  369. size_t len = 0;
  370. const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
  371. if (!lua_isinteger(L, 2)) {
  372. lua_pushinteger(L, calcCRC8(inputData, len));
  373. } else {
  374. uint8_t poly = lua_tointeger(L, 2);
  375. uint8_t start = luaL_optinteger(L, 3, 0);
  376. uint8_t is_rev = 0;
  377. if (lua_isboolean(L, 4)) {
  378. is_rev = lua_toboolean(L, 4);
  379. }
  380. lua_pushinteger(L, luat_crc8(inputData, len, start, poly, is_rev));
  381. }
  382. return 1;
  383. }
  384. static inline unsigned char crc7(const unsigned char* message, int length, unsigned char CRCPoly, unsigned char CRC)
  385. {
  386. // unsigned char CRCPoly = 0xe5;
  387. unsigned char CRCTable[256];
  388. // unsigned char CRC = 0x00;
  389. for (int i = 0; i < 256; i++){
  390. CRCTable[i] = (i & 0x80) ? i ^ CRCPoly : i;
  391. for (int j = 1; j < 8; j++){
  392. CRCTable[i] <<= 1;
  393. if (CRCTable[i] & 0x80)
  394. CRCTable[i] ^= CRCPoly;
  395. }
  396. }
  397. for (int i = 0; i < length; i++)
  398. CRC = CRCTable[(CRC << 1) ^ message[i]];
  399. return CRC<< 1;
  400. }
  401. /**
  402. 计算crc7值
  403. @api crypto.crc7(data, poly, start)
  404. @string 数据
  405. @int crc多项式,可选,默认0xE5
  406. @int crc初始值,可选,默认0x00
  407. @return int 对应的CRC7值
  408. @usage
  409. -- 计算CRC7, 本API于2023.10.07新增
  410. local crc = crypto.crc7(data)
  411. local crc = crypto.crc7(data, 0x31, 0xff)
  412. */
  413. static int l_crypto_crc7(lua_State* L) {
  414. size_t len = 0;
  415. const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
  416. unsigned char poly = luaL_optinteger(L, 2, 0xe5);
  417. unsigned char start = luaL_optinteger(L, 3, 0);
  418. unsigned char result = crc7(inputData, len, poly, start);
  419. lua_pushinteger(L, result);
  420. return 1;
  421. }
  422. /**
  423. 生成真随机数
  424. @api crypto.trng(len)
  425. @int 数据长度
  426. @return string 指定随机数字符串
  427. @usage
  428. -- 生成32位随机数ir
  429. local r = crypto.trng(4)
  430. local _, ir = pack.unpack(r, "I")
  431. */
  432. static int l_crypto_trng(lua_State *L) {
  433. int ret = 0;
  434. size_t len = luaL_checkinteger(L, 1);
  435. if (len < 1) {
  436. return 0;
  437. }
  438. if (len > 128)
  439. len = 128;
  440. char buff[128];
  441. ret = luat_crypto_trng(buff, len);
  442. if(ret ==0){
  443. lua_pushlstring(L, buff, len);
  444. return 1;
  445. }
  446. return 0;
  447. }
  448. /**
  449. 计算TOTP动态密码的结果
  450. @api crypto.totp(secret,time)
  451. @string 网站提供的密钥(就是BASE32编码后的结果)
  452. @int 可选,时间戳,默认当前时间
  453. @return int 计算得出的六位数结果 计算失败返回nil
  454. @usage
  455. --使用当前系统时间计算
  456. local otp = crypto.totp("asdfassdfasdfass")
  457. */
  458. static int l_crypto_totp(lua_State *L) {
  459. size_t len = 0;
  460. const char* secret_base32 = luaL_checklstring(L,1,&len);
  461. char * secret = (char *)luat_heap_malloc(len+1);
  462. len = (size_t)luat_str_base32_decode((const uint8_t * )secret_base32,(uint8_t*)secret,len+1);
  463. uint64_t t = 0;
  464. if (lua_isinteger(L, 2)) {
  465. t = (uint64_t)(luaL_checkinteger(L, 2))/30;
  466. }
  467. else {
  468. t = (uint64_t)(time(NULL)/30);
  469. }
  470. uint8_t data[sizeof(uint64_t)] = {0};
  471. for(size_t i=0;i<sizeof(uint64_t);i++)
  472. data[sizeof(uint64_t)-1-i] = *(((uint8_t*)&t)+i);
  473. uint8_t hmac[20] = {0};
  474. int ret = luat_crypto_hmac_sha1_simple((const char *)data, sizeof(data), (const char *)secret, len, hmac);
  475. luat_heap_free(secret);
  476. if(ret == 0)
  477. {
  478. uint8_t offset = hmac[19] & 0x0f;
  479. uint32_t r = (
  480. ((uint32_t)((hmac[offset + 0] & 0x7f)) << 24) |
  481. ((uint32_t)((hmac[offset + 1] & 0xff)) << 16) |
  482. ((uint32_t)((hmac[offset + 2] & 0xff)) << 8) |
  483. ((uint32_t)(hmac[offset + 3] & 0xff))
  484. ) % 1000000;
  485. lua_pushinteger(L,r);
  486. return 1;
  487. }
  488. return 0;
  489. }
  490. /**
  491. 将数据进行base64编码
  492. @api crypto.base64_encode(data)
  493. @string 待编码的数据
  494. @return string 编码后的数据
  495. @usage
  496. -- 本函数与 string.toBase64 是同一个
  497. local data = "123"
  498. local bdata = crypto.base64_encode(data)
  499. log.info("base64", "encode", data, bdata)
  500. data = crypto.base64_decode(data)
  501. log.info("base64", "decode", data, bdata)
  502. */
  503. int l_str_toBase64(lua_State *L);
  504. /**
  505. 将数据进行base64解码
  506. @api crypto.base64_decode(data)
  507. @string 待解码的数据
  508. @return string 解码后的数据
  509. @usage
  510. -- 本函数与 string.fromBase64 是同一个
  511. local data = "123"
  512. local bdata = crypto.base64_encode(data)
  513. log.info("base64", "encode", data, bdata)
  514. data = crypto.base64_decode(data)
  515. log.info("base64", "decode", data, bdata)
  516. */
  517. int l_str_fromBase64(lua_State *L);
  518. /**
  519. 获取当前固件支持的cipher列表
  520. @api crypto.cipher_list()
  521. @return table 本固件支持的cipher列表,字符串数组
  522. @usage
  523. -- 本API于2022.07.27添加
  524. local ciphers = crypto.cipher_list()
  525. if ciphers then
  526. log.info("crypto", "ciphers list", json.encode(ciphers))
  527. end
  528. */
  529. int l_crypto_cipher_list(lua_State *L) {
  530. const char* list[64] = {0};
  531. size_t len = 64;
  532. lua_newtable(L);
  533. int ret = luat_crypto_cipher_list(list, &len);
  534. if (ret == 0) {
  535. for (size_t i = 0; i < len; i++){
  536. lua_pushstring(L, list[i]);
  537. lua_seti(L, -2, i + 1);
  538. }
  539. }
  540. else {
  541. LLOGD("bsp not support cipher_list");
  542. }
  543. return 1;
  544. }
  545. /**
  546. 获取当前固件支持的cipher suites列表
  547. @api crypto.cipher_suites()
  548. @return table 本固件支持的cipher suites列表,字符串数组
  549. @usage
  550. -- 本API于2022.11.16添加
  551. local suites = crypto.cipher_suites()
  552. if suites then
  553. log.info("crypto", "ciphers suites", json.encode(suites))
  554. end
  555. */
  556. int l_crypto_cipher_suites(lua_State *L) {
  557. const char* list[128] = {0};
  558. size_t len = 128;
  559. lua_newtable(L);
  560. int ret = luat_crypto_cipher_suites(list, &len);
  561. if (ret == 0) {
  562. for (size_t i = 0; i < len; i++){
  563. lua_pushstring(L, list[i]);
  564. lua_seti(L, -2, i + 1);
  565. }
  566. }
  567. else {
  568. LLOGD("bsp not support cipher_suites");
  569. }
  570. return 1;
  571. }
  572. /**
  573. 计算文件的hash值(md5/sha1/sha256及hmac形式)
  574. @api crypto.md_file(tp, path, hmac)
  575. @string hash类型, 大小字母, 例如 "MD5" "SHA1" "SHA256"
  576. @string 文件路径, 例如 /luadb/logo.jpg
  577. @string hmac值,可选
  578. @return string HEX过的hash值,若失败会无返回值
  579. @usage
  580. -- 无hmac的hash值
  581. log.info("md5", crypto.md_file("MD5", "/luadb/logo.jpg"))
  582. log.info("sha1", crypto.md_file("SHA1", "/luadb/logo.jpg"))
  583. log.info("sha256", crypto.md_file("SHA256", "/luadb/logo.jpg"))
  584. -- 带hmac的hash值
  585. log.info("hmac_md5", crypto.md_file("MD5", "/luadb/logo.jpg", "123456"))
  586. log.info("hmac_sha1", crypto.md_file("SHA1", "/luadb/logo.jpg", "123456"))
  587. log.info("hmac_sha256", crypto.md_file("SHA256", "/luadb/logo.jpg", "123456"))
  588. */
  589. static int l_crypto_md_file(lua_State *L) {
  590. size_t key_len = 0;
  591. size_t path_size = 0;
  592. const char* key = NULL;
  593. const char *md = luaL_checkstring(L, 1);
  594. const char* path = luaL_checklstring(L, 2, &path_size);
  595. if (path_size < 2)
  596. return 0;
  597. if (lua_type(L, 3) == LUA_TSTRING) {
  598. key = luaL_checklstring(L, 3, &key_len);
  599. }
  600. char buff[128] = {0};
  601. char output[64];
  602. int ret = luat_crypto_md_file(md, output, key, key_len, path);
  603. if (ret < 1) {
  604. return 0;
  605. }
  606. fixhex(output, buff, ret);
  607. lua_pushlstring(L, buff, ret *2);
  608. return 1;
  609. }
  610. /**
  611. 计算数据的hash值(md5/sha1/sha256及hmac形式)
  612. @api crypto.md(tp, data, hmac)
  613. @string hash类型, 大小字母, 例如 "MD5" "SHA1" "SHA256"
  614. @string 待处理的数据
  615. @string hmac值,可选
  616. @return string HEX过的hash值,若失败会无返回值
  617. @usage
  618. -- 无hmac的hash值
  619. log.info("md5", crypto.md("MD5", "1234567890"))
  620. log.info("sha1", crypto.md("SHA1", "1234567890"))
  621. log.info("sha256", crypto.md("SHA256", "1234567890"))
  622. -- 带hmac的hash值
  623. log.info("hmac_md5", crypto.md("MD5", "1234567890", "123456"))
  624. log.info("hmac_sha1", crypto.md("SHA1", "1234567890", "123456"))
  625. log.info("hmac_sha256", crypto.md("SHA256", "1234567890", "123456"))
  626. */
  627. static int l_crypto_md(lua_State *L) {
  628. size_t key_len = 0;
  629. size_t data_size = 0;
  630. const char* key = NULL;
  631. const char *md = luaL_checkstring(L, 1);
  632. const char* data = luaL_checklstring(L, 2, &data_size);
  633. if (lua_type(L, 3) == LUA_TSTRING) {
  634. key = luaL_checklstring(L, 3, &key_len);
  635. }
  636. char buff[128] = {0};
  637. char output[64];
  638. int ret = luat_crypto_md(md, data, data_size, output, key, key_len);
  639. if (ret < 1) {
  640. return 0;
  641. }
  642. fixhex(output, buff, ret);
  643. lua_pushlstring(L, buff, ret *2);
  644. return 1;
  645. }
  646. /*
  647. 创建流式hash用的stream
  648. @api crypto.hash_init(tp)
  649. @string hash类型, 大写字母, 例如 "MD5" "SHA1" "SHA256"
  650. @string hmac值,可选
  651. @return userdata 成功返回一个数据结构,否则返回nil
  652. @usage
  653. -- 无hmac的hash stream
  654. local md5_stream = crypto.hash_init("MD5")
  655. local sha1_stream = crypto.hash_init("SHA1")
  656. local sha256_stream = crypto.hash_init("SHA256")
  657. -- 带hmac的hash stream
  658. local md5_stream = crypto.hash_init("MD5", "123456")
  659. local sha1_stream = crypto.hash_init("SHA1", "123456")
  660. local sha256_stream = crypto.hash_init("SHA256", "123456")
  661. */
  662. static int l_crypt_hash_init(lua_State *L) {
  663. luat_crypt_stream_t *stream = (luat_crypt_stream_t *)lua_newuserdata(L, sizeof(luat_crypt_stream_t));
  664. if(stream == NULL) {
  665. return 0;
  666. } else {
  667. memset(stream, 0x00, sizeof(luat_crypt_stream_t));
  668. const char* key = NULL;
  669. const char* md = luaL_checkstring(L, 1);
  670. if(lua_type(L, 2) == LUA_TSTRING) {
  671. key = luaL_checklstring(L, 2, &(stream->key_len));
  672. }
  673. int ret = luat_crypto_md_init(md, key, stream);
  674. if (ret < 0) {
  675. return 0;
  676. } else {
  677. luaL_setmetatable(L, LUAT_CRYPTO_TYPE);
  678. }
  679. }
  680. return 1;
  681. }
  682. /*
  683. 流式hash更新数据
  684. @api crypto.hash_update(stream, data)
  685. @userdata crypto.hash_init()创建的stream, 必选
  686. @string 待计算的数据,必选
  687. @return 无
  688. @usage
  689. crypto.hash_update(stream, "OK")
  690. */
  691. static int l_crypt_hash_update(lua_State *L) {
  692. luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
  693. size_t data_len = 0;
  694. const char *data = luaL_checklstring(L, 2, &data_len);
  695. luat_crypto_md_update(data, data_len ,stream);
  696. return 0;
  697. }
  698. /*
  699. 获取流式hash校验值并释放创建的stream
  700. @api crypto.hash_finish(stream)
  701. @userdata crypto.hash_init()创建的stream,必选
  702. @return string 成功返回计算得出的流式hash值的hex字符串,失败无返回
  703. @usage
  704. local hashResult = crypto.hash_finish(stream)
  705. */
  706. static int l_crypt_hash_finish(lua_State *L) {
  707. luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
  708. char buff[128] = {0};
  709. char output[64];
  710. int ret = luat_crypto_md_finish(output, stream);
  711. //LLOGD("finish result %d", ret);
  712. if (ret < 1) {
  713. return 0;
  714. }
  715. fixhex(output, buff, ret);
  716. lua_pushlstring(L, buff, ret * 2);
  717. return 1;
  718. }
  719. /*
  720. 计算checksum校验和
  721. @api crypto.checksum(data, mode)
  722. @string 待计算的数据,必选
  723. @int 模式,累加模式, 0 - 异或, 1 - 累加, 默认为0
  724. @return int checksum值,校验和
  725. @usage
  726. -- 本函数在 2022.12.28 添加
  727. -- 单纯计算checksum值
  728. local ck = crypto.checksum("OK")
  729. log.info("checksum", "ok", string.format("%02X", ck))
  730. -- 第二个参数mode在2023.5.23日添加
  731. */
  732. static int l_crypt_checksum(lua_State *L) {
  733. size_t len = 0;
  734. uint8_t checksum = 0x00;
  735. uint8_t tmp = 0;
  736. const char* sentence = luaL_checklstring(L, 1, &len);
  737. int mode = luaL_optinteger(L, 2, 0);
  738. // LLOGD("mode %d", mode);
  739. for (size_t i = 0; i < len; i++)
  740. {
  741. tmp = *sentence;
  742. if (mode == 1) {
  743. checksum += tmp;
  744. }
  745. else {
  746. checksum ^= tmp;
  747. }
  748. // LLOGD("> %02X > %02X", checksum, tmp);
  749. sentence ++;
  750. }
  751. lua_pushinteger(L, checksum);
  752. return 1;
  753. }
  754. #include "rotable2.h"
  755. static const rotable_Reg_t reg_crypto[] =
  756. {
  757. { "md5" , ROREG_FUNC(l_crypto_md5 )},
  758. { "sha1" , ROREG_FUNC(l_crypto_sha1 )},
  759. { "sha256" , ROREG_FUNC(l_crypto_sha256 )},
  760. { "sha512" , ROREG_FUNC(l_crypto_sha512 )},
  761. { "hmac_md5" , ROREG_FUNC(l_crypto_hmac_md5 )},
  762. { "hmac_sha1" , ROREG_FUNC(l_crypto_hmac_sha1 )},
  763. { "hmac_sha256" , ROREG_FUNC(l_crypto_hmac_sha256 )},
  764. { "hmac_sha512" , ROREG_FUNC(l_crypto_hmac_sha512 )},
  765. { "cipher" , ROREG_FUNC(l_crypto_cipher_encrypt )},
  766. { "cipher_encrypt" ,ROREG_FUNC(l_crypto_cipher_encrypt )},
  767. { "cipher_decrypt" ,ROREG_FUNC(l_crypto_cipher_decrypt )},
  768. { "cipher_list" , ROREG_FUNC(l_crypto_cipher_list )},
  769. { "cipher_suites", ROREG_FUNC(l_crypto_cipher_suites)},
  770. { "crc16", ROREG_FUNC(l_crypto_crc16 )},
  771. { "crc16_modbus", ROREG_FUNC(l_crypto_crc16_modbus )},
  772. { "crc32", ROREG_FUNC(l_crypto_crc32 )},
  773. { "crc8", ROREG_FUNC(l_crypto_crc8 )},
  774. { "crc7", ROREG_FUNC(l_crypto_crc7 )},
  775. { "trng", ROREG_FUNC(l_crypto_trng )},
  776. { "totp", ROREG_FUNC(l_crypto_totp )},
  777. { "base64_encode", ROREG_FUNC(l_str_toBase64)},
  778. { "base64_decode", ROREG_FUNC(l_str_fromBase64)},
  779. { "md_file", ROREG_FUNC(l_crypto_md_file)},
  780. { "md", ROREG_FUNC(l_crypto_md)},
  781. { "checksum", ROREG_FUNC(l_crypt_checksum)},
  782. { "hash_init", ROREG_FUNC(l_crypt_hash_init)},
  783. { "hash_update", ROREG_FUNC(l_crypt_hash_update)},
  784. { "hash_finish", ROREG_FUNC(l_crypt_hash_finish)},
  785. { NULL, ROREG_INT(0) }
  786. };
  787. LUAMOD_API int luaopen_crypto( lua_State *L ) {
  788. luat_newlib2(L, reg_crypto);
  789. luaL_newmetatable(L, LUAT_CRYPTO_TYPE);
  790. lua_pop(L, 1);
  791. return 1;
  792. }
  793. // 添加几个默认实现
  794. #ifndef LUAT_COMPILER_NOWEAK
  795. LUAT_WEAK int luat_crypto_trng(char* buff, size_t len) {
  796. memset(buff, 0, len);
  797. return 0;
  798. }
  799. #endif