luat_lib_crypto.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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_malloc.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. /**
  221. 对称加密
  222. @api crypto.cipher_encrypt(type, padding, str, key, iv)
  223. @string 算法名称, 例如 AES-128-ECB/AES-128-CBC, 可查阅crypto.cipher_list()
  224. @string 对齐方式, 支持PKCS7/ZERO/ONE_AND_ZEROS/ZEROS_AND_LEN/NONE
  225. @string 需要加密的数据
  226. @string 密钥,需要对应算法的密钥长度
  227. @string IV值, 非ECB算法需要
  228. @return string 加密后的字符串
  229. @usage
  230. -- 计算AES
  231. local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
  232. local data2 = crypto.cipher_encrypt("AES-128-CBC", "PKCS7", "1234567890123456", "1234567890123456", "1234567890666666")
  233. */
  234. int l_crypto_cipher_encrypt(lua_State *L) {
  235. return l_crypto_cipher_xxx(L, 1);
  236. }
  237. /**
  238. 对称解密
  239. @api crypto.cipher_decrypt(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加密,然后用AES解密
  248. local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
  249. local data2 = crypto.cipher_decrypt("AES-128-ECB", "PKCS7", data, "1234567890123456")
  250. -- data的hex为 757CCD0CDC5C90EADBEEECF638DD0000
  251. -- data2的值为 1234567890123456
  252. */
  253. int l_crypto_cipher_decrypt(lua_State *L) {
  254. return l_crypto_cipher_xxx(L, 0);
  255. }
  256. #include "crc.h"
  257. /**
  258. 计算CRC16
  259. @api crypto.crc16(method, data, poly, initial, finally, inReversem outReverse)
  260. @string CRC16模式("IBM","MAXIM","USB","MODBUS","CCITT","CCITT-FALSE","X25","XMODEM","DNP","USER-DEFINED")
  261. @string 字符串
  262. @int poly值
  263. @int initial值
  264. @int finally值
  265. @int 输入反转,1反转,默认0不反转
  266. @int 输入反转,1反转,默认0不反转
  267. @return int 对应的CRC16值
  268. @usage
  269. -- 计算CRC16
  270. local crc = crypto.crc16("")
  271. */
  272. static int l_crypto_crc16(lua_State *L)
  273. {
  274. size_t inputlen;
  275. const unsigned char *inputData;
  276. const char *inputmethod = (const char*)luaL_checkstring(L, 1);
  277. if(lua_isuserdata(L, 2))
  278. {
  279. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  280. inputlen = buff->len - buff->cursor;
  281. inputData = (const unsigned char *)(buff->addr + buff->cursor);
  282. }else{
  283. inputData = (const unsigned char*)lua_tolstring(L,2,&inputlen);
  284. }
  285. uint16_t poly = luaL_optnumber(L,3,0x0000);
  286. uint16_t initial = luaL_optnumber(L,4,0x0000);
  287. uint16_t finally = luaL_optnumber(L,5,0x0000);
  288. uint8_t inReverse = luaL_optnumber(L,6,0);
  289. uint8_t outReverse = luaL_optnumber(L,7,0);
  290. lua_pushinteger(L, calcCRC16(inputData, inputmethod,inputlen,poly,initial,finally,inReverse,outReverse));
  291. return 1;
  292. }
  293. /**
  294. 直接计算modbus的crc16值
  295. @api crypto.crc16_modbus(data, start)
  296. @string 数据
  297. @int 初始化值,默认0xFFFF
  298. @return int 对应的CRC16值
  299. @usage
  300. -- 计算CRC16 modbus
  301. local crc = crypto.crc16_modbus(data)
  302. -- 2023.11.06 新增初始值设置
  303. crc = crypto.crc16_modbus(data, 0xFFFF)
  304. */
  305. static int l_crypto_crc16_modbus(lua_State *L)
  306. {
  307. size_t len = 0;
  308. const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
  309. uint16_t crc_init = luaL_optinteger(L, 2, 0xFFFF);
  310. lua_pushinteger(L, calcCRC16_modbus(inputData, len, crc_init));
  311. return 1;
  312. }
  313. /**
  314. 计算crc32值
  315. @api crypto.crc32(data)
  316. @string 数据
  317. @return int 对应的CRC32值
  318. @usage
  319. -- 计算CRC32
  320. local crc = crypto.crc32(data)
  321. */
  322. static int l_crypto_crc32(lua_State *L)
  323. {
  324. size_t len = 0;
  325. const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
  326. lua_pushinteger(L, calcCRC32(inputData, len));
  327. return 1;
  328. }
  329. /**
  330. 计算crc8值
  331. @api crypto.crc8(data, poly, start, revert)
  332. @string 数据
  333. @int crc多项式,可选,如果不写,将忽略除了数据外所有参数
  334. @int crc初始值,可选,默认0
  335. @boolean 是否需要逆序处理,默认否
  336. @return int 对应的CRC8值
  337. @usage
  338. -- 计算CRC8
  339. local crc = crypto.crc8(data)
  340. local crc = crypto.crc8(data, 0x31, 0xff, false)
  341. */
  342. static int l_crypto_crc8(lua_State *L)
  343. {
  344. size_t len = 0;
  345. const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
  346. if (!lua_isinteger(L, 2)) {
  347. lua_pushinteger(L, calcCRC8(inputData, len));
  348. } else {
  349. uint8_t poly = lua_tointeger(L, 2);
  350. uint8_t start = luaL_optinteger(L, 3, 0);
  351. uint8_t is_rev = 0;
  352. if (lua_isboolean(L, 4)) {
  353. is_rev = lua_toboolean(L, 4);
  354. }
  355. uint8_t i;
  356. uint8_t CRC8 = start;
  357. uint8_t *Src = (uint8_t *)inputData;
  358. if (is_rev)
  359. {
  360. poly = 0;
  361. for (i = 0; i < 8; i++)
  362. {
  363. if (start & (1 << (7 - i)))
  364. {
  365. poly |= 1 << i;
  366. }
  367. }
  368. while (len--)
  369. {
  370. CRC8 ^= *Src++;
  371. for (i = 0; i < 8; i++)
  372. {
  373. if ((CRC8 & 0x01))
  374. {
  375. CRC8 >>= 1;
  376. CRC8 ^= poly;
  377. }
  378. else
  379. {
  380. CRC8 >>= 1;
  381. }
  382. }
  383. }
  384. }
  385. else
  386. {
  387. while (len--)
  388. {
  389. CRC8 ^= *Src++;
  390. for (i = 8; i > 0; --i)
  391. {
  392. if ((CRC8 & 0x80))
  393. {
  394. CRC8 <<= 1;
  395. CRC8 ^= poly;
  396. }
  397. else
  398. {
  399. CRC8 <<= 1;
  400. }
  401. }
  402. }
  403. }
  404. lua_pushinteger(L, CRC8);
  405. }
  406. return 1;
  407. }
  408. static inline unsigned char crc7(const unsigned char* message, int length, unsigned char CRCPoly, unsigned char CRC)
  409. {
  410. // unsigned char CRCPoly = 0xe5;
  411. unsigned char CRCTable[256];
  412. // unsigned char CRC = 0x00;
  413. for (int i = 0; i < 256; i++){
  414. CRCTable[i] = (i & 0x80) ? i ^ CRCPoly : i;
  415. for (int j = 1; j < 8; j++){
  416. CRCTable[i] <<= 1;
  417. if (CRCTable[i] & 0x80)
  418. CRCTable[i] ^= CRCPoly;
  419. }
  420. }
  421. for (int i = 0; i < length; i++)
  422. CRC = CRCTable[(CRC << 1) ^ message[i]];
  423. return CRC<< 1;
  424. }
  425. /**
  426. 计算crc7值
  427. @api crypto.crc7(data, poly, start)
  428. @string 数据
  429. @int crc多项式,可选,默认0xE5
  430. @int crc初始值,可选,默认0x00
  431. @return int 对应的CRC7值
  432. @usage
  433. -- 计算CRC7, 本API于2023.10.07新增
  434. local crc = crypto.crc7(data)
  435. local crc = crypto.crc7(data, 0x31, 0xff)
  436. */
  437. static int l_crypto_crc7(lua_State* L) {
  438. size_t len = 0;
  439. const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
  440. unsigned char poly = luaL_optinteger(L, 2, 0xe5);
  441. unsigned char start = luaL_optinteger(L, 3, 0);
  442. unsigned char result = crc7(inputData, len, poly, start);
  443. lua_pushinteger(L, result);
  444. return 1;
  445. }
  446. /**
  447. 生成真随机数
  448. @api crypto.trng(len)
  449. @int 数据长度
  450. @return string 指定随机数字符串
  451. @usage
  452. -- 生成32位随机数ir
  453. local r = crypto.trng(4)
  454. local _, ir = pack.unpack(r, "I")
  455. */
  456. static int l_crypto_trng(lua_State *L) {
  457. int ret = 0;
  458. size_t len = luaL_checkinteger(L, 1);
  459. if (len < 1) {
  460. return 0;
  461. }
  462. if (len > 128)
  463. len = 128;
  464. char buff[128];
  465. ret = luat_crypto_trng(buff, len);
  466. if(ret ==0){
  467. lua_pushlstring(L, buff, len);
  468. return 1;
  469. }
  470. return 0;
  471. }
  472. /**
  473. 计算TOTP动态密码的结果
  474. @api crypto.totp(secret,time)
  475. @string 网站提供的密钥(就是BASE32编码后的结果)
  476. @int 可选,时间戳,默认当前时间
  477. @return int 计算得出的六位数结果 计算失败返回nil
  478. @usage
  479. --使用当前系统时间计算
  480. local otp = crypto.totp("asdfassdfasdfass")
  481. */
  482. static int l_crypto_totp(lua_State *L) {
  483. size_t len = 0;
  484. const char* secret_base32 = luaL_checklstring(L,1,&len);
  485. char * secret = (char *)luat_heap_malloc(len+1);
  486. len = (size_t)luat_str_base32_decode((const uint8_t * )secret_base32,(uint8_t*)secret,len+1);
  487. uint64_t t = 0;
  488. if (lua_isinteger(L, 2)) {
  489. t = (uint64_t)(luaL_checkinteger(L, 2))/30;
  490. }
  491. else {
  492. t = (uint64_t)(time(NULL)/30);
  493. }
  494. uint8_t data[sizeof(uint64_t)] = {0};
  495. for(size_t i=0;i<sizeof(uint64_t);i++)
  496. data[sizeof(uint64_t)-1-i] = *(((uint8_t*)&t)+i);
  497. uint8_t hmac[20] = {0};
  498. int ret = luat_crypto_hmac_sha1_simple((const char *)data, sizeof(data), (const char *)secret, len, hmac);
  499. luat_heap_free(secret);
  500. if(ret == 0)
  501. {
  502. uint8_t offset = hmac[19] & 0x0f;
  503. uint32_t r = (
  504. ((uint32_t)((hmac[offset + 0] & 0x7f)) << 24) |
  505. ((uint32_t)((hmac[offset + 1] & 0xff)) << 16) |
  506. ((uint32_t)((hmac[offset + 2] & 0xff)) << 8) |
  507. ((uint32_t)(hmac[offset + 3] & 0xff))
  508. ) % 1000000;
  509. lua_pushinteger(L,r);
  510. return 1;
  511. }
  512. return 0;
  513. }
  514. /**
  515. 将数据进行base64编码
  516. @api crypto.base64_encode(data)
  517. @string 待编码的数据
  518. @return string 编码后的数据
  519. @usage
  520. -- 本函数与 string.toBase64 是同一个
  521. local data = "123"
  522. local bdata = crypto.base64_encode(data)
  523. log.info("base64", "encode", data, bdata)
  524. data = crypto.base64_decode(data)
  525. log.info("base64", "decode", data, bdata)
  526. */
  527. int l_str_toBase64(lua_State *L);
  528. /**
  529. 将数据进行base64解码
  530. @api crypto.base64_decode(data)
  531. @string 待解码的数据
  532. @return string 解码后的数据
  533. @usage
  534. -- 本函数与 string.fromBase64 是同一个
  535. local data = "123"
  536. local bdata = crypto.base64_encode(data)
  537. log.info("base64", "encode", data, bdata)
  538. data = crypto.base64_decode(data)
  539. log.info("base64", "decode", data, bdata)
  540. */
  541. int l_str_fromBase64(lua_State *L);
  542. /**
  543. 获取当前固件支持的cipher列表
  544. @api crypto.cipher_list()
  545. @return table 本固件支持的cipher列表,字符串数组
  546. @usage
  547. -- 本API于2022.07.27添加
  548. local ciphers = crypto.cipher_list()
  549. if ciphers then
  550. log.info("crypto", "ciphers list", json.encode(ciphers))
  551. end
  552. */
  553. int l_crypto_cipher_list(lua_State *L) {
  554. const char* list[64] = {0};
  555. size_t len = 64;
  556. lua_newtable(L);
  557. int ret = luat_crypto_cipher_list(list, &len);
  558. if (ret == 0) {
  559. for (size_t i = 0; i < len; i++){
  560. lua_pushstring(L, list[i]);
  561. lua_seti(L, -2, i + 1);
  562. }
  563. }
  564. else {
  565. LLOGD("bsp not support cipher_list");
  566. }
  567. return 1;
  568. }
  569. /**
  570. 获取当前固件支持的cipher suites列表
  571. @api crypto.cipher_suites()
  572. @return table 本固件支持的cipher suites列表,字符串数组
  573. @usage
  574. -- 本API于2022.11.16添加
  575. local suites = crypto.cipher_suites()
  576. if suites then
  577. log.info("crypto", "ciphers suites", json.encode(suites))
  578. end
  579. */
  580. int l_crypto_cipher_suites(lua_State *L) {
  581. const char* list[128] = {0};
  582. size_t len = 128;
  583. lua_newtable(L);
  584. int ret = luat_crypto_cipher_suites(list, &len);
  585. if (ret == 0) {
  586. for (size_t i = 0; i < len; i++){
  587. lua_pushstring(L, list[i]);
  588. lua_seti(L, -2, i + 1);
  589. }
  590. }
  591. else {
  592. LLOGD("bsp not support cipher_suites");
  593. }
  594. return 1;
  595. }
  596. /**
  597. 计算文件的hash值(md5/sha1/sha256及hmac形式)
  598. @api crypto.md_file(tp, path, hmac)
  599. @string hash类型, 大小字母, 例如 "MD5" "SHA1" "SHA256"
  600. @string 文件路径, 例如 /luadb/logo.jpg
  601. @string hmac值,可选
  602. @return string HEX过的hash值,若失败会无返回值
  603. @usage
  604. -- 无hmac的hash值
  605. log.info("md5", crypto.md_file("MD5", "/luadb/logo.jpg"))
  606. log.info("sha1", crypto.md_file("SHA1", "/luadb/logo.jpg"))
  607. log.info("sha256", crypto.md_file("SHA256", "/luadb/logo.jpg"))
  608. -- 带hmac的hash值
  609. log.info("hmac_md5", crypto.md_file("MD5", "/luadb/logo.jpg", "123456"))
  610. log.info("hmac_sha1", crypto.md_file("SHA1", "/luadb/logo.jpg", "123456"))
  611. log.info("hmac_sha256", crypto.md_file("SHA256", "/luadb/logo.jpg", "123456"))
  612. */
  613. static int l_crypto_md_file(lua_State *L) {
  614. size_t key_len = 0;
  615. size_t path_size = 0;
  616. const char* key = NULL;
  617. const char *md = luaL_checkstring(L, 1);
  618. const char* path = luaL_checklstring(L, 2, &path_size);
  619. if (path_size < 2)
  620. return 0;
  621. if (lua_type(L, 3) == LUA_TSTRING) {
  622. key = luaL_checklstring(L, 3, &key_len);
  623. }
  624. char buff[128] = {0};
  625. char output[64];
  626. int ret = luat_crypto_md_file(md, output, key, key_len, path);
  627. if (ret < 1) {
  628. return 0;
  629. }
  630. fixhex(output, buff, ret);
  631. lua_pushlstring(L, buff, ret *2);
  632. return 1;
  633. }
  634. /**
  635. 计算数据的hash值(md5/sha1/sha256及hmac形式)
  636. @api crypto.md(tp, data, hmac)
  637. @string hash类型, 大小字母, 例如 "MD5" "SHA1" "SHA256"
  638. @string 待处理的数据
  639. @string hmac值,可选
  640. @return string HEX过的hash值,若失败会无返回值
  641. @usage
  642. -- 无hmac的hash值
  643. log.info("md5", crypto.md("MD5", "1234567890"))
  644. log.info("sha1", crypto.md("SHA1", "1234567890"))
  645. log.info("sha256", crypto.md("SHA256", "1234567890"))
  646. -- 带hmac的hash值
  647. log.info("hmac_md5", crypto.md("MD5", "1234567890", "123456"))
  648. log.info("hmac_sha1", crypto.md("SHA1", "1234567890", "123456"))
  649. log.info("hmac_sha256", crypto.md("SHA256", "1234567890", "123456"))
  650. */
  651. static int l_crypto_md(lua_State *L) {
  652. size_t key_len = 0;
  653. size_t data_size = 0;
  654. const char* key = NULL;
  655. const char *md = luaL_checkstring(L, 1);
  656. const char* data = luaL_checklstring(L, 2, &data_size);
  657. if (lua_type(L, 3) == LUA_TSTRING) {
  658. key = luaL_checklstring(L, 3, &key_len);
  659. }
  660. char buff[128] = {0};
  661. char output[64];
  662. int ret = luat_crypto_md(md, data, data_size, output, key, key_len);
  663. if (ret < 1) {
  664. return 0;
  665. }
  666. fixhex(output, buff, ret);
  667. lua_pushlstring(L, buff, ret *2);
  668. return 1;
  669. }
  670. /*
  671. 创建流式hash用的stream
  672. @api crypto.hash_init(tp)
  673. @string hash类型, 大写字母, 例如 "MD5" "SHA1" "SHA256"
  674. @string hmac值,可选
  675. @return userdata 成功返回一个数据结构,否则返回nil
  676. @usage
  677. -- 无hmac的hash stream
  678. local md5_stream = crypto.hash_init("MD5")
  679. local sha1_stream = crypto.hash_init("SHA1")
  680. local sha256_stream = crypto.hash_init("SHA256")
  681. -- 带hmac的hash stream
  682. local md5_stream = crypto.hash_init("MD5", "123456")
  683. local sha1_stream = crypto.hash_init("SHA1", "123456")
  684. local sha256_stream = crypto.hash_init("SHA256", "123456")
  685. */
  686. static int l_crypt_hash_init(lua_State *L) {
  687. luat_crypt_stream_t *stream = (luat_crypt_stream_t *)lua_newuserdata(L, sizeof(luat_crypt_stream_t));
  688. if(stream == NULL) {
  689. lua_pushnil(L);
  690. } else {
  691. memset(stream, 0x00, sizeof(luat_crypt_stream_t));
  692. const char* key = NULL;
  693. const char* md = luaL_checkstring(L, 1);
  694. memcpy(stream->tp, md, strlen(md)+1);
  695. if(lua_type(L, 2) == LUA_TSTRING) {
  696. key = luaL_checklstring(L, 2, &(stream->key_len));
  697. }
  698. int ret = luat_crypto_md_init(md, key, stream);
  699. if (ret < 0) {
  700. lua_pushnil(L);
  701. } else {
  702. luaL_setmetatable(L, LUAT_CRYPTO_TYPE);
  703. }
  704. }
  705. return 1;
  706. }
  707. /*
  708. 流式hash更新数据
  709. @api crypto.hash_update(stream, data)
  710. @userdata crypto.hash_init()创建的stream, 必选
  711. @string 待计算的数据,必选
  712. @return 无
  713. @usage
  714. crypto.hash_update(stream, "OK")
  715. */
  716. static int l_crypt_hash_update(lua_State *L) {
  717. luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
  718. size_t data_len = 0;
  719. const char *data = luaL_checklstring(L, 2, &data_len);
  720. luat_crypto_md_update(stream->tp, data, data_len ,stream);
  721. return 0;
  722. }
  723. /*
  724. 获取流式hash校验值并释放创建的stream
  725. @api crypto.hash_finish(stream)
  726. @userdata crypto.hash_init()创建的stream,必选
  727. @return string 成功返回计算得出的流式hash值的hex字符串,失败无返回
  728. @usage
  729. local hashResult = crypto.hash_finish(stream)
  730. */
  731. static int l_crypt_hash_finish(lua_State *L) {
  732. luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
  733. char buff[128] = {0};
  734. char output[64];
  735. int ret = luat_crypto_md_finish(stream->tp, output, stream);
  736. LLOGD("finish result %d", ret);
  737. if (ret < 1) {
  738. return 0;
  739. }
  740. fixhex(output, buff, ret);
  741. lua_pushlstring(L, buff, ret * 2);
  742. return 1;
  743. }
  744. /*
  745. 计算checksum校验和
  746. @api crypto.checksum(data, mode)
  747. @string 待计算的数据,必选
  748. @int 模式,累加模式, 0 - 异或, 1 - 累加, 默认为0
  749. @return int checksum值,校验和
  750. @usage
  751. -- 本函数在 2022.12.28 添加
  752. -- 单纯计算checksum值
  753. local ck = crypto.checksum("OK")
  754. log.info("checksum", "ok", string.format("%02X", ck))
  755. -- 第二个参数mode在2023.5.23日添加
  756. */
  757. static int l_crypt_checksum(lua_State *L) {
  758. size_t len = 0;
  759. uint8_t checksum = 0x00;
  760. uint8_t tmp = 0;
  761. const char* sentence = luaL_checklstring(L, 1, &len);
  762. int mode = luaL_optinteger(L, 2, 0);
  763. // LLOGD("mode %d", mode);
  764. for (size_t i = 0; i < len; i++)
  765. {
  766. tmp = *sentence;
  767. if (mode == 1) {
  768. checksum += tmp;
  769. }
  770. else {
  771. checksum ^= tmp;
  772. }
  773. // LLOGD("> %02X > %02X", checksum, tmp);
  774. sentence ++;
  775. }
  776. lua_pushinteger(L, checksum);
  777. return 1;
  778. }
  779. #include "rotable2.h"
  780. static const rotable_Reg_t reg_crypto[] =
  781. {
  782. { "md5" , ROREG_FUNC(l_crypto_md5 )},
  783. { "sha1" , ROREG_FUNC(l_crypto_sha1 )},
  784. { "sha256" , ROREG_FUNC(l_crypto_sha256 )},
  785. { "sha512" , ROREG_FUNC(l_crypto_sha512 )},
  786. { "hmac_md5" , ROREG_FUNC(l_crypto_hmac_md5 )},
  787. { "hmac_sha1" , ROREG_FUNC(l_crypto_hmac_sha1 )},
  788. { "hmac_sha256" , ROREG_FUNC(l_crypto_hmac_sha256 )},
  789. { "hmac_sha512" , ROREG_FUNC(l_crypto_hmac_sha512 )},
  790. { "cipher" , ROREG_FUNC(l_crypto_cipher_encrypt )},
  791. { "cipher_encrypt" ,ROREG_FUNC(l_crypto_cipher_encrypt )},
  792. { "cipher_decrypt" ,ROREG_FUNC(l_crypto_cipher_decrypt )},
  793. { "cipher_list" , ROREG_FUNC(l_crypto_cipher_list )},
  794. { "cipher_suites", ROREG_FUNC(l_crypto_cipher_suites)},
  795. { "crc16", ROREG_FUNC(l_crypto_crc16 )},
  796. { "crc16_modbus", ROREG_FUNC(l_crypto_crc16_modbus )},
  797. { "crc32", ROREG_FUNC(l_crypto_crc32 )},
  798. { "crc8", ROREG_FUNC(l_crypto_crc8 )},
  799. { "crc7", ROREG_FUNC(l_crypto_crc7 )},
  800. { "trng", ROREG_FUNC(l_crypto_trng )},
  801. { "totp", ROREG_FUNC(l_crypto_totp )},
  802. { "base64_encode", ROREG_FUNC(l_str_toBase64)},
  803. { "base64_decode", ROREG_FUNC(l_str_fromBase64)},
  804. { "md_file", ROREG_FUNC(l_crypto_md_file)},
  805. { "md", ROREG_FUNC(l_crypto_md)},
  806. { "checksum", ROREG_FUNC(l_crypt_checksum)},
  807. { "hash_init", ROREG_FUNC(l_crypt_hash_init)},
  808. { "hash_update", ROREG_FUNC(l_crypt_hash_update)},
  809. { "hash_finish", ROREG_FUNC(l_crypt_hash_finish)},
  810. { NULL, ROREG_INT(0) }
  811. };
  812. LUAMOD_API int luaopen_crypto( lua_State *L ) {
  813. luat_newlib2(L, reg_crypto);
  814. luaL_newmetatable(L, LUAT_CRYPTO_TYPE);
  815. lua_pop(L, 1);
  816. return 1;
  817. }
  818. // 添加几个默认实现
  819. #ifndef LUAT_COMPILER_NOWEAK
  820. LUAT_WEAK int luat_crypto_trng(char* buff, size_t len) {
  821. memset(buff, 0, len);
  822. return 0;
  823. }
  824. #endif