luat_lib_crypto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. @module crypto
  3. @summary 加解密和hash函数
  4. @version 1.0
  5. @date 2020.07.03
  6. */
  7. #include "luat_base.h"
  8. #include "luat_crypto.h"
  9. #define LUAT_LOG_TAG "luat.crypto"
  10. #include "luat_log.h"
  11. static unsigned char hexchars[] = "0123456789ABCDEF";
  12. static void fixhex(const char* source, char* dst, size_t len) {
  13. for (size_t i = 0; i < len; i++)
  14. {
  15. char ch = *(source+i);
  16. dst[i*2] = hexchars[(unsigned char)ch >> 4];
  17. dst[i*2+1] = hexchars[(unsigned char)ch & 0xF];
  18. }
  19. }
  20. /**
  21. 计算md5值
  22. @api crypto.md5(str)
  23. @string 需要计算的字符串
  24. @return string 计算得出的md5值的hex字符串
  25. @usage
  26. -- 计算字符串"abc"的md5
  27. log.info("md5", crypto.md5("abc"))
  28. */
  29. static int l_crypto_md5(lua_State *L) {
  30. size_t size = 0;
  31. const char* str = luaL_checklstring(L, 1, &size);
  32. char tmp[32] = {0};
  33. char dst[32] = {0};
  34. if (luat_crypto_md5_simple(str, size, tmp) == 0) {
  35. fixhex(tmp, dst, 16);
  36. lua_pushlstring(L, dst, 32);
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. /**
  42. 计算hmac_md5值
  43. @api crypto.hmac_md5(str, key)
  44. @string 需要计算的字符串
  45. @string 密钥
  46. @return string 计算得出的hmac_md5值的hex字符串
  47. @usage
  48. -- 计算字符串"abc"的hmac_md5
  49. log.info("hmac_md5", crypto.hmac_md5("abc", "1234567890"))
  50. */
  51. static int l_crypto_hmac_md5(lua_State *L) {
  52. size_t str_size = 0;
  53. size_t key_size = 0;
  54. const char* str = luaL_checklstring(L, 1, &str_size);
  55. const char* key = luaL_checklstring(L, 2, &key_size);
  56. char tmp[32] = {0};
  57. char dst[32] = {0};
  58. if (luat_crypto_hmac_md5_simple(str, str_size, key, key_size, tmp) == 0) {
  59. fixhex(tmp, dst, 16);
  60. lua_pushlstring(L, dst, 32);
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. /**
  66. 计算sha1值
  67. @api crypto.sha1(str)
  68. @string 需要计算的字符串
  69. @return string 计算得出的sha1值的hex字符串
  70. @usage
  71. -- 计算字符串"abc"的sha1
  72. log.info("sha1", crypto.sha1("abc"))
  73. */
  74. static int l_crypto_sha1(lua_State *L) {
  75. size_t size = 0;
  76. const char* str = luaL_checklstring(L, 1, &size);
  77. char tmp[40] = {0};
  78. char dst[40] = {0};
  79. if (luat_crypto_sha1_simple(str, size, tmp) == 0) {
  80. fixhex(tmp, dst, 20);
  81. lua_pushlstring(L, dst, 40);
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. /**
  87. 计算hmac_sha1值
  88. @api crypto.hmac_sha1(str, key)
  89. @string 需要计算的字符串
  90. @string 密钥
  91. @return string 计算得出的hmac_sha1值的hex字符串
  92. @usage
  93. -- 计算字符串"abc"的hmac_sha1
  94. log.info("hmac_sha1", crypto.hmac_sha1("abc", "1234567890"))
  95. */
  96. static int l_crypto_hmac_sha1(lua_State *L) {
  97. size_t str_size = 0;
  98. size_t key_size = 0;
  99. const char* str = luaL_checklstring(L, 1, &str_size);
  100. const char* key = luaL_checklstring(L, 2, &key_size);
  101. char tmp[40] = {0};
  102. char dst[40] = {0};
  103. if (luat_crypto_hmac_sha1_simple(str, str_size, key, key_size, tmp) == 0) {
  104. fixhex(tmp, dst, 20);
  105. lua_pushlstring(L, dst, 40);
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. /**
  111. 计算sha256值
  112. @api crypto.sha256(str)
  113. @string 需要计算的字符串
  114. @return string 计算得出的sha256值的hex字符串
  115. @usage
  116. -- 计算字符串"abc"的sha256
  117. log.info("sha256", crypto.sha256("abc"))
  118. */
  119. static int l_crypto_sha256(lua_State *L) {
  120. size_t size = 0;
  121. const char* str = luaL_checklstring(L, 1, &size);
  122. char tmp[64] = {0};
  123. char dst[64] = {0};
  124. if (luat_crypto_sha256_simple(str, size, tmp) == 0) {
  125. fixhex(tmp, dst, 32);
  126. lua_pushlstring(L, dst, 64);
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. /**
  132. 计算hmac_sha256值
  133. @api crypto.hmac_sha256(str, key)
  134. @string 需要计算的字符串
  135. @string 密钥
  136. @return string 计算得出的hmac_sha1值的hex字符串
  137. @usage
  138. -- 计算字符串"abc"的hmac_sha256
  139. log.info("hmac_sha256", crypto.hmac_sha256("abc", "1234567890"))
  140. */
  141. static int l_crypto_hmac_sha256(lua_State *L) {
  142. size_t str_size = 0;
  143. size_t key_size = 0;
  144. const char* str = luaL_checklstring(L, 1, &str_size);
  145. const char* key = luaL_checklstring(L, 2, &key_size);
  146. char tmp[64] = {0};
  147. char dst[64] = {0};
  148. if (luat_crypto_hmac_sha256_simple(str, str_size, key, key_size, tmp) == 0) {
  149. fixhex(tmp, dst, 32);
  150. lua_pushlstring(L, dst, 64);
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. //---
  156. /**
  157. 计算sha512值
  158. @api crypto.sha512(str)
  159. @string 需要计算的字符串
  160. @return string 计算得出的sha512值的hex字符串
  161. @usage
  162. -- 计算字符串"abc"的sha512
  163. log.info("sha512", crypto.sha512("abc"))
  164. */
  165. static int l_crypto_sha512(lua_State *L) {
  166. size_t size = 0;
  167. const char* str = luaL_checklstring(L, 1, &size);
  168. char tmp[128] = {0};
  169. char dst[128] = {0};
  170. if (luat_crypto_sha512_simple(str, size, tmp) == 0) {
  171. fixhex(tmp, dst, 64);
  172. lua_pushlstring(L, dst, 128);
  173. return 1;
  174. }
  175. return 0;
  176. }
  177. /**
  178. 计算hmac_sha512值
  179. @api crypto.hmac_sha512(str, key)
  180. @string 需要计算的字符串
  181. @string 密钥
  182. @return string 计算得出的hmac_sha1值的hex字符串
  183. @usage
  184. -- 计算字符串"abc"的hmac_sha512
  185. log.info("hmac_sha512", crypto.hmac_sha512("abc", "1234567890"))
  186. */
  187. static int l_crypto_hmac_sha512(lua_State *L) {
  188. size_t str_size = 0;
  189. size_t key_size = 0;
  190. const char* str = luaL_checklstring(L, 1, &str_size);
  191. const char* key = luaL_checklstring(L, 2, &key_size);
  192. char tmp[128] = {0};
  193. char dst[128] = {0};
  194. if (luat_crypto_hmac_sha512_simple(str, str_size, key, key_size, tmp) == 0) {
  195. fixhex(tmp, dst, 64);
  196. lua_pushlstring(L, dst, 128);
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. int l_crypto_cipher_xxx(lua_State *L, uint8_t flags);
  202. /**
  203. 对称加密
  204. @api crypto.cipher(type, padding, str, key, iv)
  205. @string 算法名称, 例如 AES-128-ECB/AES-128-CBC, 可查阅mbedtls的cipher_wrap.c
  206. @string 对齐方式, 当前仅支持PKCS7
  207. @string 需要加密的数据
  208. @string 密钥,需要对应算法的密钥长度
  209. @string IV值, 非ECB算法需要
  210. @return string 加密后的字符串
  211. @usage
  212. -- 计算AES
  213. local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
  214. local data2 = crypto.cipher_encrypt("AES-128-CBC", "PKCS7", "1234567890123456", "1234567890123456", "1234567890666666")
  215. */
  216. int l_crypto_cipher_encrypt(lua_State *L) {
  217. return l_crypto_cipher_xxx(L, 1);
  218. }
  219. /**
  220. 对称解密
  221. @api crypto.cipher(type, padding, str, key, iv)
  222. @string 算法名称, 例如 AES-128-ECB/AES-128-CBC, 可查阅mbedtls的cipher_wrap.c
  223. @string 对齐方式, 当前仅支持PKCS7
  224. @string 需要解密的数据
  225. @string 密钥,需要对应算法的密钥长度
  226. @string IV值, 非ECB算法需要
  227. @return string 解密后的字符串
  228. @usage
  229. -- 用AES加密,然后用AES解密
  230. local data = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "1234567890123456", "1234567890123456")
  231. local data2 = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", data, "1234567890123456")
  232. -- data的hex为 757CCD0CDC5C90EADBEEECF638DD0000
  233. -- data2的值为 1234567890123456
  234. */
  235. int l_crypto_cipher_decrypt(lua_State *L) {
  236. return l_crypto_cipher_xxx(L, 0);
  237. }
  238. #include "crc.h"
  239. /**
  240. 计算CRC16
  241. @api crypto.crc16(method, data, poly, initial, finally, inReversem outReverse)
  242. @string 输入模式
  243. @string 字符串
  244. @int poly值
  245. @int initial值
  246. @int finally值
  247. @int 输入反转,1反转,默认0不反转
  248. @int 输入反转,1反转,默认0不反转
  249. @return int 对应的CRC16值
  250. @usage
  251. -- 计算CRC16
  252. local crc = crypto.crc16("")
  253. */
  254. static int l_crypto_crc16(lua_State *L)
  255. {
  256. size_t inputLen;
  257. const char *inputmethod = luaL_checkstring(L, 1);
  258. const char *inputData = lua_tolstring(L,2,&inputLen);
  259. uint16_t poly = luaL_optnumber(L,3,0x0000);
  260. uint16_t initial = luaL_optnumber(L,4,0x0000);
  261. uint16_t finally = luaL_optnumber(L,5,0x0000);
  262. uint8_t inReverse = luaL_optnumber(L,6,0);
  263. uint8_t outReverse = luaL_optnumber(L,7,0);
  264. lua_pushinteger(L, calcCRC16(inputData, inputmethod,inputLen,poly,initial,finally,inReverse,outReverse));
  265. return 1;
  266. }
  267. /**
  268. 直接计算modbus的crc16值
  269. @api crypto.crc16_modbus(data)
  270. @string 数据
  271. @return int 对应的CRC16值
  272. @usage
  273. -- 计算CRC16 modbus
  274. local crc = crypto.crc16_modbus(data)
  275. */
  276. static int l_crypto_crc16_modbus(lua_State *L)
  277. {
  278. size_t len = 0;
  279. const char *inputData = luaL_checklstring(L, 1, &len);
  280. lua_pushinteger(L, calcCRC16_modbus(inputData, len));
  281. return 1;
  282. }
  283. /**
  284. 计算crc32值
  285. @api crypto.crc32(data)
  286. @string 数据
  287. @return int 对应的CRC32值
  288. @usage
  289. -- 计算CRC32
  290. local crc = crypto.crc32(data)
  291. */
  292. static int l_crypto_crc32(lua_State *L)
  293. {
  294. size_t len = 0;
  295. const char *inputData = luaL_checklstring(L, 1, &len);
  296. lua_pushinteger(L, calcCRC32(inputData, len));
  297. return 1;
  298. }
  299. /**
  300. 计算crc8值
  301. @api crypto.crc8(data)
  302. @string 数据
  303. @return int 对应的CRC8值
  304. @usage
  305. -- 计算CRC8
  306. local crc = crypto.crc8(data)
  307. */
  308. static int l_crypto_crc8(lua_State *L)
  309. {
  310. size_t len = 0;
  311. const char *inputData = luaL_checklstring(L, 1, &len);
  312. lua_pushinteger(L, calcCRC8(inputData, len));
  313. return 1;
  314. }
  315. #include "rotable.h"
  316. static const rotable_Reg reg_crypto[] =
  317. {
  318. { "md5" , l_crypto_md5 ,0},
  319. { "sha1" , l_crypto_sha1 ,0},
  320. { "sha256" , l_crypto_sha256 ,0},
  321. { "sha512" , l_crypto_sha512 ,0},
  322. { "hmac_md5" , l_crypto_hmac_md5 ,0},
  323. { "hmac_sha1" , l_crypto_hmac_sha1 ,0},
  324. { "hmac_sha256" , l_crypto_hmac_sha256 ,0},
  325. { "hmac_sha512" , l_crypto_hmac_sha512 ,0},
  326. { "cipher" , l_crypto_cipher_encrypt ,0},
  327. { "cipher_encrypt" ,l_crypto_cipher_encrypt ,0},
  328. { "cipher_decrypt" ,l_crypto_cipher_decrypt ,0},
  329. { "crc16", l_crypto_crc16 ,0},
  330. { "crc16_modbus", l_crypto_crc16_modbus ,0},
  331. { "crc32", l_crypto_crc32 ,0},
  332. { "crc8", l_crypto_crc8 ,0},
  333. { NULL, NULL ,0}
  334. };
  335. LUAMOD_API int luaopen_crypto( lua_State *L ) {
  336. luat_newlib(L, reg_crypto);
  337. return 1;
  338. }
  339. // 添加几个默认实现
  340. LUAT_WEAK int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr) {return -1;}
  341. LUAT_WEAK int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {return -1;}
  342. LUAT_WEAK int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr) {return -1;}
  343. LUAT_WEAK int luat_crypto_hmac_sha1_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {return -1;}
  344. LUAT_WEAK int luat_crypto_sha256_simple(const char* str, size_t str_size, void* out_ptr) {return -1;}
  345. LUAT_WEAK int luat_crypto_hmac_sha256_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {return -1;}
  346. LUAT_WEAK int luat_crypto_sha512_simple(const char* str, size_t str_size, void* out_ptr) {return -1;}
  347. LUAT_WEAK int luat_crypto_hmac_sha512_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {return -1;}