luat_lib_gmssl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. @module gmssl
  3. @summary 国密算法
  4. @version 1.1
  5. @date 2023.03.02
  6. @author chenxudong1208
  7. @demo gmssl
  8. @tag LUAT_USE_GMSSL
  9. */
  10. #include "luat_base.h"
  11. #include "luat_malloc.h"
  12. #include "luat_str.h"
  13. #include <time.h>
  14. #include "luat_zbuff.h"
  15. #include "gmssl/sm2.h"
  16. #include "gmssl/sm3.h"
  17. #include "gmssl/sm4.h"
  18. // #include "mbedtls/hmac_drbg.h"
  19. #define LUAT_LOG_TAG "sm"
  20. #include "luat_log.h"
  21. #define SM3_DIGEST_LENGTH 32
  22. #define SM4_BLOCK_LEN 16
  23. #define SM2_STR_LEN 300
  24. #define HEX_CODE 16
  25. extern void luat_str_fromhex(const char* str, size_t len, char* buff);
  26. static void DeletePaddingBuf(luaL_Buffer *B, const char *pPadding, size_t nBufLen, uint8_t *pBuf, uint8_t pPaddLen)
  27. {
  28. uint8_t nPadLen;
  29. if((strcmp(pPadding, "PKCS5")==0) || (strcmp(pPadding, "PKCS7")==0))
  30. {
  31. nPadLen = *(pBuf+nBufLen-1);
  32. //printf("aes DeletePaddingBuf length=%d\n", nPadLen);
  33. if((pPaddLen-nPadLen) >= 0)
  34. {
  35. luaL_addlstring(B, (char*)pBuf, nBufLen-nPadLen);
  36. }
  37. }
  38. else if(strcmp(pPadding, "ZERO")==0)
  39. {
  40. uint8_t *pEnd = pBuf+nBufLen-1;
  41. nPadLen = 0;
  42. while(1)
  43. {
  44. if(*pEnd == 0)
  45. {
  46. nPadLen++;
  47. if(nPadLen == pPaddLen)
  48. {
  49. break;
  50. }
  51. pEnd--;
  52. }
  53. else
  54. {
  55. break;
  56. }
  57. }
  58. //printf("aes DeletePaddingBuf length=%d\n", nPadLen);
  59. if((pPaddLen-nPadLen) >= 0)
  60. {
  61. luaL_addlstring(B, (char*)pBuf, nBufLen-nPadLen);
  62. }
  63. }
  64. else
  65. {
  66. luaL_addlstring(B, (char*)pBuf, nBufLen);
  67. }
  68. }
  69. /*
  70. sm2算法加密
  71. @api sm.sm2encrypt(pkx,pky,data, mode, mode2)
  72. @string 公钥x,必选. HEX字符串
  73. @string 公钥y,必选. HEX字符串
  74. @string 待计算的数据,必选,最长32字节, 非HEX字符串
  75. @boolean 输出模式,默认false. false-GMSSL默认格式, true-网站兼容模式
  76. @boolean 标准版本,默认false. false-C1C3C2新国际, true-C1C2C3老国际
  77. @return string 加密后的字符串, 原样输出,未经HEX转换. 若加密失败会返回nil或空字符串
  78. @usage
  79. -- 提示 mode/mode2 参数是 2023.10.17 新增
  80. local originStr = "encryption standard"
  81. local pkx = "435B39CCA8F3B508C1488AFC67BE491A0F7BA07E581A0E4849A5CF70628A7E0A"
  82. local pky = "75DDBA78F15FEECB4C7895E2C1CDF5FE01DEBB2CDBADF45399CCF77BBA076A42"
  83. local private = "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0"
  84. local encodeStr = gmssl.sm2encrypt(pkx,pky,originStr)
  85. print(originStr,"encrypt",string.toHex(encodeStr))
  86. log.info("testsm.sm2decrypt",gmssl.sm2decrypt(private,encodeStr))
  87. */
  88. static int l_sm2_encrypt(lua_State *L)
  89. {
  90. // size_t randLen = 0;
  91. size_t pkxLen = 0;
  92. size_t pkyLen = 0;
  93. size_t pBufLen = 0;
  94. const char *pkx = lua_tolstring(L, 1,&pkxLen);
  95. const char *pky = lua_tolstring(L, 2,&pkyLen);
  96. const char *pBuf = lua_tolstring(L, 3,&pBufLen);
  97. int ret = 0;
  98. //检查参数合法性
  99. if((pkxLen!=64))
  100. {
  101. LLOGE("invalid pkx password length=%d", pkxLen);
  102. return 0;
  103. }
  104. if((pkyLen!=64))
  105. {
  106. LLOGE("invalid pky password length=%d", pkyLen);
  107. return 0;
  108. }
  109. if (pBufLen > SM2_MAX_PLAINTEXT_SIZE) {
  110. LLOGD("data too large max %d but %d", SM2_MAX_PLAINTEXT_SIZE, pBufLen);
  111. return 0;
  112. }
  113. int mode = 0;
  114. if (lua_isboolean(L, 4)) {
  115. mode = lua_toboolean(L, 4);
  116. }
  117. int mode2 = 0;
  118. if (lua_isboolean(L, 5)) {
  119. mode2 = lua_toboolean(L, 5);
  120. }
  121. SM2_KEY sm2 = {0};
  122. SM2_POINT point = {0};
  123. luat_str_fromhex(pkx, 64, (char*)point.x);
  124. luat_str_fromhex(pky, 64, (char*)point.y);
  125. ret = sm2_key_set_public_key(&sm2, (const SM2_POINT*)&point);
  126. if (ret != 1) {
  127. LLOGD("sm2_key_set_public_key %d", ret);
  128. return 0;
  129. }
  130. uint8_t out[SM2_MAX_CIPHERTEXT_SIZE] = {0};
  131. size_t olen = 0;
  132. if (mode) {
  133. SM2_CIPHERTEXT C = {0};
  134. ret = sm2_do_encrypt(&sm2, (const uint8_t *)pBuf, pBufLen, &C);
  135. if (ret == 1) {
  136. if (mode2 == 0) {
  137. memcpy(out, &C.point.x, 32);
  138. memcpy(out + 32, &C.point.y, 32);
  139. memcpy(out + 64, C.hash, 32);
  140. memcpy(out + 96, C.ciphertext, C.ciphertext_size);
  141. olen = 96 + C.ciphertext_size;
  142. }
  143. else {
  144. out[0] = 0x04;
  145. memcpy(out + 1, &C.point.x, 32);
  146. memcpy(out + 32 + 1, &C.point.y, 32);
  147. memcpy(out + 64 + 1, C.ciphertext, C.ciphertext_size);
  148. memcpy(out + 64 + C.ciphertext_size + 1, C.hash, 32);
  149. olen = 96 + C.ciphertext_size + 1;
  150. }
  151. }
  152. }
  153. else {
  154. ret = sm2_encrypt(&sm2, (const uint8_t *)pBuf, pBufLen, out, &olen);
  155. }
  156. if (ret != 1) {
  157. LLOGD("sm2_encrypt ret %d", ret);
  158. return 0;
  159. }
  160. lua_pushlstring(L, (char*)out, olen);
  161. return 1;
  162. }
  163. /*
  164. sm2算法解密
  165. @api sm.sm2decrypt(private,data,mode,mode2)
  166. @string 私钥,必选,HEX字符串
  167. @string 待计算的数据,必选,原始数据,非HEX字符串
  168. @boolean 输出模式,默认false. false-GMSSL默认格式, true-网站兼容模式
  169. @boolean 标准版本,默认false. false-C1C3C2新国际, true-C1C2C3老国际
  170. @return string 解密后的字符串,未经HEX转换.若解密失败会返回nil或空字符串
  171. @usage
  172. -- 提示 mode/mode2 参数是 2023.10.17 新增
  173. local originStr = "encryption standard"
  174. local pkx = "435B39CCA8F3B508C1488AFC67BE491A0F7BA07E581A0E4849A5CF70628A7E0A"
  175. local pky = "75DDBA78F15FEECB4C7895E2C1CDF5FE01DEBB2CDBADF45399CCF77BBA076A42"
  176. local private = "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0"
  177. local rand = "4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
  178. local encodeStr = gmssl.sm2encrypt(pkx,pky,rand,originStr)
  179. print(originStr,"encrypt",string.toHex(encodeStr))
  180. log.info("testsm.sm2decrypt",gmssl.sm2decrypt(private,encodeStr))
  181. */
  182. static int l_sm2_decrypt(lua_State *L)
  183. {
  184. size_t privateLen = 0;
  185. size_t pBufLen = 0;
  186. const char *private = lua_tolstring(L, 1,&privateLen);
  187. const char *pBuf = lua_tolstring(L, 2,&pBufLen);
  188. int ret = 0;
  189. int mode = 0;
  190. if (lua_isboolean(L, 3)) {
  191. mode = lua_toboolean(L, 3);
  192. }
  193. int mode2 = 0;
  194. if (lua_isboolean(L, 4)) {
  195. mode2 = lua_toboolean(L, 4);
  196. }
  197. //检查参数合法性
  198. if((privateLen!=64))
  199. {
  200. LLOGE("invalid private password length=%d", privateLen);
  201. return 0;
  202. }
  203. if (pBufLen < 97) {
  204. LLOGE("待数据太短,应该要97字节以上");
  205. return 0;
  206. }
  207. SM2_KEY sm2 = {0};
  208. char out[512] = {0};
  209. size_t olen = 0;
  210. luat_str_fromhex(private, 64, (char*)sm2.private_key);
  211. if (mode) {
  212. // LLOGD("网站兼容模式");
  213. SM2_CIPHERTEXT C = {0};
  214. if (mode2 == 0) {
  215. // LLOGD("C1C3C2");
  216. C.ciphertext_size = (uint8_t)(pBufLen - 96);
  217. // LLOGD("pBufLen %d ciphertext_size %d", pBufLen, C.ciphertext_size);
  218. memcpy(&C.point.x, pBuf, 32);
  219. memcpy(&C.point.y, pBuf + 32, 32);
  220. memcpy(C.hash, pBuf + 64, 32);
  221. memcpy(C.ciphertext, pBuf + 96, C.ciphertext_size);
  222. }
  223. else {
  224. // LLOGD("C1C2C3");
  225. pBuf ++;
  226. pBufLen --;
  227. C.ciphertext_size = (uint8_t)(pBufLen - 96);
  228. // LLOGD("pBufLen %d ciphertext_size %d", pBufLen, C.ciphertext_size);
  229. memcpy(&C.point.x, pBuf, 32);
  230. memcpy(&C.point.y, pBuf + 32, 32);
  231. memcpy(C.ciphertext, pBuf + 64, C.ciphertext_size);
  232. memcpy(C.hash, pBuf + 64 + C.ciphertext_size, 32);
  233. }
  234. ret = sm2_do_decrypt(&sm2, &C, (uint8_t *)out, &olen);
  235. }
  236. else {
  237. // LLOGD("GMSSL默认模式");
  238. ret = sm2_decrypt(&sm2, (uint8_t*)pBuf, pBufLen, (uint8_t*)out, &olen);
  239. }
  240. if (ret != 1) {
  241. LLOGD("sm2_decrypt ret %d", ret);
  242. return 0;
  243. }
  244. lua_pushlstring(L, (char*)out, olen);
  245. return 1;
  246. }
  247. /*
  248. sm3算法,算HASH值
  249. @api sm.sm3(data)
  250. @string 待计算的数据,必选
  251. @return string 对应的hash值
  252. @usage
  253. local encodeStr = gmssl.sm3("lqlq666lqlq946")
  254. log.info("testsm.sm3update",string.toHex(encodeStr))
  255. */
  256. static int l_sm3_update(lua_State *L)
  257. {
  258. size_t inputLen = 0;
  259. uint8_t dgst[SM3_DIGEST_LENGTH];
  260. const char *inputData = lua_tolstring(L,1,&inputLen);
  261. sm3_digest((uint8_t*)inputData, inputLen, dgst);
  262. lua_pushlstring(L, (char*)dgst, SM3_DIGEST_LENGTH);
  263. return 1;
  264. }
  265. /*
  266. sm3算法,算HASH值,但带HMAC
  267. @api sm.sm3hmac(data, key)
  268. @string 待计算的数据,必选
  269. @string 密钥
  270. @return string 对应的hash值
  271. @usage
  272. local encodeStr = gmssl.sm3hmac("lqlq666lqlq946", "123")
  273. log.info("testsm.sm3update",string.toHex(encodeStr))
  274. */
  275. static int l_sm3hmac_update(lua_State *L)
  276. {
  277. size_t inputLen = 0;
  278. size_t keyLen = 0;
  279. uint8_t dgst[SM3_DIGEST_LENGTH];
  280. const char *inputData = lua_tolstring(L, 1, &inputLen);
  281. const char *keyData = lua_tolstring(L, 2, &keyLen);
  282. sm3_hmac((uint8_t*)keyData, keyLen, (uint8_t*)inputData, inputLen, dgst);
  283. lua_pushlstring(L, (char*)dgst, SM3_DIGEST_LENGTH);
  284. return 1;
  285. }
  286. /*
  287. SM4加密算法
  288. @api gmssl.sm4encrypt(mode,padding,originStr,password)
  289. @string 加密模式, CBC或ECB
  290. @string 填充方式, NONE/ZERO/PKCS5/PKCS7
  291. @string 加密的字符串
  292. @string 密钥
  293. @return string 加密后的数据
  294. @usage
  295. local originStr = "SM4 ECB ZeroPadding test"
  296. --加密模式:ECB;填充方式:ZeroPadding;密钥:1234567890123456;密钥长度:128 bit
  297. local encodeStr = gmssl.sm4encrypt("ECB","ZERO",originStr,"1234567890123456")
  298. print(originStr,"encrypt",string.toHex(encodeStr))
  299. log.info("testsm.decrypt",gmssl.sm4decrypt("ECB","ZERO",encodeStr,"1234567890123456"))
  300. originStr = "SM4 ECB Pkcs5Padding test"
  301. --加密模式:ECB;填充方式:Pkcs5Padding;密钥:1234567890123456;密钥长度:128 bit
  302. encodeStr = gmssl.sm4encrypt("ECB","PKCS5",originStr,"1234567890123456")
  303. print(originStr,"encrypt",string.toHex(encodeStr))
  304. log.info("testsm.decrypt",gmssl.sm4decrypt("ECB","PKCS5",encodeStr,"1234567890123456"))
  305. originStr = "SM4 CBC Pkcs5Padding test"
  306. --加密模式:CBC;填充方式:Pkcs5Padding;密钥:1234567890123456;密钥长度:256 bit;偏移量:1234567890666666
  307. encodeStr = gmssl.sm4encrypt("CBC","PKCS5",originStr,"1234567890123456","1234567890666666")
  308. print(originStr,"encrypt",string.toHex(encodeStr))
  309. log.info("testsm.decrypt",gmssl.sm4decrypt("CBC","PKCS5",encodeStr,"1234567890123456","1234567890666666"))
  310. */
  311. static int l_sm4_encrypt(lua_State *L)
  312. {
  313. const char *pMode = luaL_checkstring(L, 1);
  314. const char *pPadding = luaL_checkstring(L, 2);
  315. size_t nBufLen = 0;
  316. const char *pBuf = lua_tolstring(L, 3, &nBufLen);
  317. size_t nPswdLen = 0;
  318. const char *pPassword = lua_tolstring(L, 4, &nPswdLen);
  319. size_t nIVLen = 0;
  320. const char *pIV = lua_tolstring(L, 5, &nIVLen);
  321. int nPadLen = SM4_BLOCK_LEN-(nBufLen%SM4_BLOCK_LEN);
  322. uint8_t pPadBuf[SM4_BLOCK_LEN] = {0};
  323. uint8_t *pInBuf = NULL;
  324. //检查参数合法性
  325. if((nPswdLen!=16))
  326. {
  327. return luaL_error(L, "invalid password length=%d, only support 128bit Password", nPswdLen);
  328. }
  329. if((strcmp(pMode, "ECB")!=0) && (strcmp(pMode, "CBC")!=0))
  330. {
  331. return luaL_error(L, "invalid mode=%s, only support ECB,CBC", pMode);
  332. }
  333. if((strcmp(pPadding, "NONE")!=0) && (strcmp(pPadding, "PKCS5")!=0) && (strcmp(pPadding, "PKCS7")!=0) && (strcmp((char*)pPadding, "ZERO")!=0))
  334. {
  335. return luaL_error(L, "invalid padding=%s, only support NONE,PKCS5,PKCS7,ZERO", pPadding);
  336. }
  337. if(((strcmp(pMode, "CBC")==0)) && (nIVLen!=16))
  338. {
  339. return luaL_error(L, "invalid iv length=%d, only support 128bit IV", nIVLen);
  340. }
  341. //构造填充数据
  342. if((strcmp(pPadding, "PKCS5")==0) || (strcmp(pPadding, "PKCS7")==0))
  343. {
  344. memset(pPadBuf, nPadLen, sizeof(pPadBuf));
  345. }
  346. else if(strcmp(pPadding, "ZERO")==0)
  347. {
  348. memset(pPadBuf, 0, sizeof(pPadBuf));
  349. }
  350. else if(strcmp(pPadding, "NONE")==0)
  351. {
  352. if((strcmp(pMode, "CBC")==0) || (strcmp(pMode, "ECB")==0)){
  353. if(nBufLen%SM4_BLOCK_LEN != 0)
  354. {
  355. return luaL_error(L, "buf len should be multiple of 16, len=%d", nBufLen);
  356. }
  357. }
  358. nPadLen = 0;
  359. }
  360. //加密
  361. {
  362. luaL_Buffer b;
  363. uint32_t nRmnLen;
  364. luaL_buffinit( L, &b );
  365. //原始数据和填充数据拼接在一起
  366. if (strcmp((char*)pPadding, "NONE")!=0)
  367. {
  368. pInBuf = luat_heap_malloc(nBufLen+nPadLen);
  369. if(pInBuf == NULL)
  370. {
  371. //LLOGD("aes_encrypt malloc error!!!\n");
  372. luaL_pushresult( &b );
  373. return 1;
  374. }
  375. memcpy(pInBuf, pBuf, nBufLen);
  376. memcpy(pInBuf+nBufLen, pPadBuf, nPadLen);
  377. nBufLen += nPadLen;
  378. nRmnLen = nBufLen;
  379. }
  380. else
  381. {
  382. pInBuf = luat_heap_malloc(nBufLen);
  383. nRmnLen = nBufLen;
  384. if(pInBuf == NULL)
  385. {
  386. //LLOGD("aes_encrypt malloc error!!!\n");
  387. luaL_pushresult( &b );
  388. return 1;
  389. }
  390. memcpy(pInBuf, pBuf, nBufLen);
  391. }
  392. SM4_KEY sm4_key;
  393. memset(&sm4_key,0,sizeof(SM4_KEY));
  394. sm4_set_encrypt_key(&sm4_key, (uint8_t*)pPassword);
  395. if(strcmp(pMode, "ECB") == 0)
  396. {
  397. //开始分组加密,每16字节一组
  398. char out[SM4_BLOCK_LEN];
  399. while(nRmnLen>0)
  400. {
  401. sm4_encrypt(&sm4_key, (uint8_t*)(pInBuf+nBufLen-nRmnLen), (uint8_t*)out);
  402. luaL_addlstring(&b, out, SM4_BLOCK_LEN);
  403. nRmnLen -= SM4_BLOCK_LEN;
  404. }
  405. }
  406. else if((strcmp(pMode, "CBC") == 0))
  407. {
  408. //待加密数据一次性传入
  409. // sm4_cbc_encrypt(pInBuf,pInBuf,nBufLen,&sm4_key,pIV,1);
  410. char *out = luat_heap_malloc(nBufLen);
  411. sm4_cbc_encrypt(&sm4_key, (uint8_t*)pIV, pInBuf, nBufLen / SM4_BLOCK_LEN, (uint8_t*)out);
  412. luaL_addlstring(&b, out, nBufLen);
  413. luat_heap_free(out);
  414. }
  415. if(pInBuf != NULL)
  416. {
  417. luat_heap_free(pInBuf);
  418. pInBuf = NULL;
  419. }
  420. luaL_pushresult( &b );
  421. return 1;
  422. }
  423. }
  424. /*
  425. SM4解密算法
  426. @api gmssl.sm4decrypt(mode,padding,encodeStr,password)
  427. @string 加密模式, CBC或ECB
  428. @string 填充方式, NONE/ZERO/PKCS5/PKCS7
  429. @string 已加密的字符串
  430. @string 密钥
  431. @return string 解密的字符串
  432. @usage
  433. -- 参考gmssl.sm4encrypt
  434. */
  435. static int l_sm4_decrypt(lua_State *L)
  436. {
  437. const char *pMode = luaL_checkstring(L, 1);
  438. const char *pPadding = luaL_checkstring(L, 2);
  439. size_t nBufLen = 0;
  440. const char *pBuf = lua_tolstring(L, 3, &nBufLen);
  441. size_t nPswdLen = 0;
  442. const char *pPassword = lua_tolstring(L, 4, &nPswdLen);
  443. size_t nIVLen = 0;
  444. const char *pIV = lua_tolstring(L, 5, &nIVLen);
  445. char out[SM4_BLOCK_LEN];
  446. //检查参数合法性
  447. int isCBC = strcmp((char*)pMode, "CBC") == 0;
  448. int isECB = strcmp((char*)pMode, "ECB") == 0;
  449. if(isCBC || isECB){
  450. if((nBufLen % 16) != 0){
  451. return luaL_error(L, "invalid BufLen length=%d, BufLen must be Integer multiples of 16", nBufLen);
  452. }
  453. }
  454. if((nPswdLen!=16))
  455. {
  456. return luaL_error(L, "invalid password length=%d, only support 128, 192, 256 bits", nPswdLen);
  457. }
  458. if(!isCBC && !isECB)
  459. {
  460. return luaL_error(L, "invalid mode=%s, only support ECB,CBC,CTR", pMode);
  461. }
  462. if((strcmp(pPadding, "NONE")!=0) && (strcmp(pPadding, "PKCS5")!=0) && (strcmp(pPadding, "PKCS7")!=0) && (strcmp((char*)pPadding, "ZERO")!=0))
  463. {
  464. return luaL_error(L, "invalid padding=%s, only support NONE,PKCS5,PKCS7,ZERO", pPadding);
  465. }
  466. if(isCBC && (nIVLen!=16))
  467. {
  468. return luaL_error(L, "invalid iv length=%d, only support 16", nIVLen);
  469. }
  470. //解密
  471. {
  472. luaL_Buffer b;
  473. uint32_t nRmnLen;
  474. luaL_buffinit( L, &b );
  475. nRmnLen = nBufLen;
  476. SM4_KEY sm4_key;
  477. memset(&sm4_key,0,sizeof(SM4_KEY));
  478. sm4_set_decrypt_key(&sm4_key,(uint8_t*)pPassword);
  479. if(isECB)
  480. {
  481. //开始分组解密,每16字节一组
  482. while(nRmnLen>0)
  483. {
  484. sm4_decrypt(&sm4_key,(uint8_t*)(pBuf+nBufLen-nRmnLen), (uint8_t*)out);
  485. //删除填充数据
  486. if(nRmnLen==SM4_BLOCK_LEN)
  487. {
  488. DeletePaddingBuf(&b, pPadding, SM4_BLOCK_LEN, (uint8_t*)out, SM4_BLOCK_LEN);
  489. }
  490. else
  491. {
  492. luaL_addlstring(&b, out, SM4_BLOCK_LEN);
  493. }
  494. nRmnLen -= SM4_BLOCK_LEN;
  495. }
  496. }
  497. else if (isCBC)
  498. {
  499. //待解密数据一次性传入
  500. if (nBufLen <= 1024) {
  501. char out[1024];
  502. sm4_cbc_decrypt(&sm4_key, (uint8_t*)pIV, (uint8_t*)pBuf, nBufLen/SM4_BLOCK_LEN, (uint8_t*)out);
  503. DeletePaddingBuf(&b, pPadding, nBufLen, (uint8_t*)out, SM4_BLOCK_LEN);
  504. }
  505. else {
  506. char *out = luat_heap_malloc(nBufLen);
  507. if (out == NULL) {
  508. LLOGE("out of memory when malloc SM4 decrypt buff");
  509. return 0;
  510. }
  511. sm4_cbc_decrypt(&sm4_key, (uint8_t*)pIV, (uint8_t*)pBuf, nBufLen/SM4_BLOCK_LEN, (uint8_t*)out);
  512. DeletePaddingBuf(&b, pPadding, nBufLen, (uint8_t*)out, SM4_BLOCK_LEN);
  513. luat_heap_free(out);
  514. }
  515. }
  516. luaL_pushresult( &b );
  517. return 1;
  518. }
  519. }
  520. #include "rotable2.h"
  521. static const rotable_Reg_t reg_gmssl[] =
  522. {
  523. { "sm2encrypt", ROREG_FUNC(l_sm2_encrypt)},
  524. { "sm2decrypt", ROREG_FUNC(l_sm2_decrypt)},
  525. { "sm3update", ROREG_FUNC(l_sm3_update)},
  526. { "sm3", ROREG_FUNC(l_sm3_update)},
  527. { "sm3hmac", ROREG_FUNC(l_sm3hmac_update)},
  528. { "sm4encrypt", ROREG_FUNC(l_sm4_encrypt)},
  529. { "sm4decrypt", ROREG_FUNC(l_sm4_decrypt)},
  530. { NULL, ROREG_INT(0) }
  531. };
  532. LUAMOD_API int luaopen_gmssl( lua_State *L ) {
  533. luat_newlib2(L, reg_gmssl);
  534. return 1;
  535. }