luat_lib_crypto.c 25 KB

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