luat_crypto_sysp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include "luat_base.h"
  2. #include "luat_crypto.h"
  3. #if 1
  4. #define LUAT_LOG_TAG "crypto"
  5. #include "luat_log.h"
  6. #define PKG_USING_MBEDTLS
  7. #ifdef PKG_USING_MBEDTLS
  8. #include "mbedtls/cipher.h"
  9. #endif
  10. int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
  11. size_t cipher_size = 0;
  12. size_t pad_size = 0;
  13. size_t str_size = 0;
  14. size_t key_size = 0;
  15. size_t iv_size = 0;
  16. const char* cipher = luaL_optlstring(L, 1, "AES-128-ECB", &cipher_size);
  17. const char* pad = luaL_optlstring(L, 2, "PKCS7", &pad_size);
  18. const char* str = luaL_checklstring(L, 3, &str_size);
  19. const char* key = luaL_checklstring(L, 4, &key_size);
  20. const char* iv = luaL_optlstring(L, 5, "", &iv_size);
  21. int ret = 0;
  22. unsigned char output[32] = {0};
  23. size_t input_size = 0;
  24. size_t output_size = 0;
  25. size_t block_size = 0;
  26. luaL_Buffer buff;
  27. #ifdef PKG_USING_MBEDTLS
  28. mbedtls_cipher_context_t ctx;
  29. mbedtls_cipher_init(&ctx);
  30. const mbedtls_cipher_info_t * _cipher = mbedtls_cipher_info_from_string(cipher);
  31. if (_cipher == NULL) {
  32. lua_pushstring(L, "bad cipher name");
  33. lua_error(L);
  34. return 0;
  35. }
  36. ret = mbedtls_cipher_setup(&ctx, _cipher);
  37. if (ret) {
  38. LLOGE("mbedtls_cipher_setup fail %ld", ret);
  39. goto _exit;
  40. }
  41. ret = mbedtls_cipher_setkey(&ctx, key, key_size * 8, flags & 0x1);
  42. if (ret) {
  43. LLOGE("mbedtls_cipher_setkey fail %ld", ret);
  44. goto _exit;
  45. }
  46. // TODO 设置padding mode
  47. // mbedtls_cipher_set_padding_mode
  48. if (iv_size) {
  49. ret = mbedtls_cipher_set_iv(&ctx, iv, iv_size);
  50. if (ret) {
  51. LLOGE("mbedtls_cipher_set_iv fail %ld", ret);
  52. goto _exit;
  53. }
  54. }
  55. mbedtls_cipher_reset(&ctx);
  56. //mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_PKCS7);
  57. // 开始注入数据
  58. luaL_buffinit(L, &buff);
  59. block_size = mbedtls_cipher_get_block_size(&ctx);
  60. for (size_t i = 0; i < str_size; i+=block_size) {
  61. input_size = str_size - i;
  62. if (input_size > block_size)
  63. input_size = block_size;
  64. ret = mbedtls_cipher_update(&ctx, str+i, input_size, output, &output_size);
  65. if (ret) {
  66. LLOGE("mbedtls_cipher_update fail %ld", ret);
  67. goto _exit;
  68. }
  69. //else LLOGD("mbedtls_cipher_update, output size=%ld", output_size);
  70. if (output_size > 0)
  71. luaL_addlstring(&buff, output, output_size);
  72. output_size = 0;
  73. }
  74. ret = mbedtls_cipher_finish(&ctx, output, &output_size);
  75. if (ret) {
  76. LLOGE("mbedtls_cipher_finish fail %ld", ret);
  77. goto _exit;
  78. }
  79. //else LLOGD("mbedtls_cipher_finish, output size=%ld", output_size);
  80. if (output_size > 0)
  81. luaL_addlstring(&buff, output, output_size);
  82. _exit:
  83. mbedtls_cipher_free(&ctx);
  84. luaL_pushresult(&buff);
  85. return 1;
  86. #else
  87. return 0;
  88. #endif
  89. }
  90. #ifdef PKG_USING_MBEDTLS
  91. #if !defined(MBEDTLS_CONFIG_FILE)
  92. #include "mbedtls/config.h"
  93. #else
  94. #include MBEDTLS_CONFIG_FILE
  95. #endif
  96. #include "mbedtls/sha1.h"
  97. #include "mbedtls/sha256.h"
  98. #ifdef MBEDTLS_SHA512_C
  99. #include "mbedtls/sha512.h"
  100. #endif
  101. #include "mbedtls/md5.h"
  102. #define LUAT_LOG_TAG "crypto"
  103. #include "luat_log.h"
  104. void luat_crypto_HmacSha1(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen);
  105. void luat_crypto_HmacSha256(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen);
  106. void luat_crypto_HmacSha512(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen);
  107. void luat_crypto_HmacMd5(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen);
  108. #ifndef LUAT_CRYPTO_MD5
  109. #define LUAT_CRYPTO_MD5
  110. int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr) {
  111. mbedtls_md5_context ctx;
  112. mbedtls_md5_init(&ctx);
  113. mbedtls_md5_starts(&ctx);
  114. mbedtls_md5_update(&ctx, (const unsigned char *)str, str_size);
  115. mbedtls_md5_finish(&ctx, (unsigned char *)out_ptr);
  116. mbedtls_md5_free(&ctx);
  117. return 0;
  118. }
  119. #endif
  120. #ifndef LUAT_CRYPTO_SHA1
  121. #define LUAT_CRYPTO_SHA1
  122. int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr) {
  123. mbedtls_sha1_context ctx;
  124. mbedtls_sha1_init(&ctx);
  125. mbedtls_sha1_starts(&ctx);
  126. mbedtls_sha1_update(&ctx, (const unsigned char *)str, str_size);
  127. mbedtls_sha1_finish(&ctx, (unsigned char *)out_ptr);
  128. mbedtls_sha1_free(&ctx);
  129. return 0;
  130. }
  131. #endif
  132. int luat_crypto_sha256_simple(const char* str, size_t str_size, void* out_ptr) {
  133. mbedtls_sha256_context ctx;
  134. mbedtls_sha256_init(&ctx);
  135. mbedtls_sha256_starts(&ctx, 0);
  136. mbedtls_sha256_update(&ctx, (const unsigned char *)str, str_size);
  137. mbedtls_sha256_finish(&ctx, (unsigned char *)out_ptr);
  138. mbedtls_sha256_free(&ctx);
  139. return 0;
  140. }
  141. #ifdef MBEDTLS_SHA512_C
  142. int luat_crypto_sha512_simple(const char* str, size_t str_size, void* out_ptr) {
  143. mbedtls_sha512_context ctx;
  144. mbedtls_sha512_init(&ctx);
  145. mbedtls_sha512_starts(&ctx, 0);
  146. mbedtls_sha512_update(&ctx, (const unsigned char *)str, str_size);
  147. mbedtls_sha512_finish(&ctx, (unsigned char *)out_ptr);
  148. mbedtls_sha512_free(&ctx);
  149. return 0;
  150. }
  151. #endif
  152. #ifndef LUAT_CRYPTO_MD5
  153. #define LUAT_CRYPTO_MD5
  154. int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  155. luat_crypto_HmacMd5((const unsigned char *)str, str_size, (unsigned char *)out_ptr, (const unsigned char *)mac, mac_size);
  156. return 0;
  157. }
  158. #endif
  159. #ifndef LUAT_CRYPTO_SHA1
  160. #define LUAT_CRYPTO_SHA1
  161. int luat_crypto_hmac_sha1_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  162. luat_crypto_HmacSha1((const unsigned char *)str, str_size, (unsigned char *)out_ptr, (const unsigned char *)mac, mac_size);
  163. return 0;
  164. }
  165. #endif
  166. int luat_crypto_hmac_sha256_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  167. luat_crypto_HmacSha256((const unsigned char *)str, str_size, (unsigned char *)out_ptr, (const unsigned char *)mac, mac_size);
  168. return 0;
  169. }
  170. #ifdef MBEDTLS_SHA512_C
  171. int luat_crypto_hmac_sha512_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  172. luat_crypto_HmacSha512((const unsigned char *)str, str_size, (unsigned char *)out_ptr, (const unsigned char *)mac, mac_size);
  173. return 0;
  174. }
  175. #endif
  176. ///----------------------------
  177. #define ALI_SHA1_KEY_IOPAD_SIZE (64)
  178. #define ALI_SHA1_DIGEST_SIZE (20)
  179. #define ALI_SHA256_KEY_IOPAD_SIZE (64)
  180. #define ALI_SHA256_DIGEST_SIZE (32)
  181. #define ALI_SHA512_KEY_IOPAD_SIZE (128)
  182. #define ALI_SHA512_DIGEST_SIZE (64)
  183. #define ALI_MD5_KEY_IOPAD_SIZE (64)
  184. #define ALI_MD5_DIGEST_SIZE (16)
  185. // char atHb2Hex(unsigned char hb)
  186. // {
  187. // hb = hb&0xF;
  188. // return (char)(hb<10 ? '0'+hb : hb-10+'a');
  189. // }
  190. /*
  191. * output = SHA-1( input buffer )
  192. */
  193. void luat_crypto_HmacSha1(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen)
  194. {
  195. int i;
  196. mbedtls_sha1_context ctx;
  197. unsigned char k_ipad[ALI_SHA1_KEY_IOPAD_SIZE] = {0};
  198. unsigned char k_opad[ALI_SHA1_KEY_IOPAD_SIZE] = {0};
  199. unsigned char tempbuf[ALI_SHA1_DIGEST_SIZE];
  200. memset(k_ipad, 0x36, ALI_SHA1_KEY_IOPAD_SIZE);
  201. memset(k_opad, 0x5C, ALI_SHA1_KEY_IOPAD_SIZE);
  202. for(i=0; i<keylen; i++)
  203. {
  204. if(i>=ALI_SHA1_KEY_IOPAD_SIZE)
  205. {
  206. break;
  207. }
  208. k_ipad[i] ^=key[i];
  209. k_opad[i] ^=key[i];
  210. }
  211. mbedtls_sha1_init(&ctx);
  212. mbedtls_sha1_starts(&ctx);
  213. mbedtls_sha1_update(&ctx, k_ipad, ALI_SHA1_KEY_IOPAD_SIZE);
  214. mbedtls_sha1_update(&ctx, input, ilen);
  215. mbedtls_sha1_finish(&ctx, tempbuf);
  216. mbedtls_sha1_starts(&ctx);
  217. mbedtls_sha1_update(&ctx, k_opad, ALI_SHA1_KEY_IOPAD_SIZE);
  218. mbedtls_sha1_update(&ctx, tempbuf, ALI_SHA1_DIGEST_SIZE);
  219. mbedtls_sha1_finish(&ctx, tempbuf);
  220. // for(i=0; i<ALI_SHA1_DIGEST_SIZE; ++i)
  221. // {
  222. // output[i*2] = atHb2Hex(tempbuf[i]>>4);
  223. // output[i*2+1] = atHb2Hex(tempbuf[i]);
  224. // }
  225. memcpy(output, tempbuf, ALI_SHA1_DIGEST_SIZE);
  226. mbedtls_sha1_free(&ctx);
  227. }
  228. /*
  229. * output = SHA-256( input buffer )
  230. */
  231. void luat_crypto_HmacSha256(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen)
  232. {
  233. int i;
  234. mbedtls_sha256_context ctx;
  235. unsigned char k_ipad[ALI_SHA256_KEY_IOPAD_SIZE] = {0};
  236. unsigned char k_opad[ALI_SHA256_KEY_IOPAD_SIZE] = {0};
  237. memset(k_ipad, 0x36, 64);
  238. memset(k_opad, 0x5C, 64);
  239. if ((NULL == input) || (NULL == key) || (NULL == output)) {
  240. return;
  241. }
  242. if (keylen > ALI_SHA256_KEY_IOPAD_SIZE) {
  243. return;
  244. }
  245. for(i=0; i<keylen; i++)
  246. {
  247. if(i>=ALI_SHA256_KEY_IOPAD_SIZE)
  248. {
  249. break;
  250. }
  251. k_ipad[i] ^=key[i];
  252. k_opad[i] ^=key[i];
  253. }
  254. mbedtls_sha256_init(&ctx);
  255. mbedtls_sha256_starts(&ctx, 0);
  256. mbedtls_sha256_update(&ctx, k_ipad, ALI_SHA256_KEY_IOPAD_SIZE);
  257. mbedtls_sha256_update(&ctx, input, ilen);
  258. mbedtls_sha256_finish(&ctx, output);
  259. mbedtls_sha256_starts(&ctx, 0);
  260. mbedtls_sha256_update(&ctx, k_opad, ALI_SHA256_KEY_IOPAD_SIZE);
  261. mbedtls_sha256_update(&ctx, output, ALI_SHA256_DIGEST_SIZE);
  262. mbedtls_sha256_finish(&ctx, output);
  263. mbedtls_sha256_free(&ctx);
  264. }
  265. #ifdef MBEDTLS_SHA512_C
  266. /*
  267. * output = SHA-512( input buffer )
  268. */
  269. void luat_crypto_HmacSha512(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen)
  270. {
  271. int i;
  272. mbedtls_sha512_context ctx;
  273. unsigned char k_ipad[ALI_SHA512_KEY_IOPAD_SIZE] = {0};
  274. unsigned char k_opad[ALI_SHA512_KEY_IOPAD_SIZE] = {0};
  275. memset(k_ipad, 0x36, 64);
  276. memset(k_opad, 0x5C, 64);
  277. if ((NULL == input) || (NULL == key) || (NULL == output)) {
  278. return;
  279. }
  280. if (keylen > ALI_SHA512_KEY_IOPAD_SIZE) {
  281. return;
  282. }
  283. for(i=0; i<keylen; i++)
  284. {
  285. if(i>=ALI_SHA512_KEY_IOPAD_SIZE)
  286. {
  287. break;
  288. }
  289. k_ipad[i] ^=key[i];
  290. k_opad[i] ^=key[i];
  291. }
  292. mbedtls_sha512_init(&ctx);
  293. mbedtls_sha512_starts(&ctx, 0);
  294. mbedtls_sha512_update(&ctx, k_ipad, ALI_SHA512_KEY_IOPAD_SIZE);
  295. mbedtls_sha512_update(&ctx, input, ilen);
  296. mbedtls_sha512_finish(&ctx, output);
  297. mbedtls_sha512_starts(&ctx, 0);
  298. mbedtls_sha512_update(&ctx, k_opad, ALI_SHA512_KEY_IOPAD_SIZE);
  299. mbedtls_sha512_update(&ctx, output, ALI_SHA512_DIGEST_SIZE);
  300. mbedtls_sha512_finish(&ctx, output);
  301. mbedtls_sha512_free(&ctx);
  302. }
  303. #endif
  304. /*
  305. * output = MD-5( input buffer )
  306. */
  307. void luat_crypto_HmacMd5(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen)
  308. {
  309. int i;
  310. mbedtls_md5_context ctx;
  311. unsigned char k_ipad[ALI_MD5_KEY_IOPAD_SIZE] = {0};
  312. unsigned char k_opad[ALI_MD5_KEY_IOPAD_SIZE] = {0};
  313. unsigned char tempbuf[ALI_MD5_DIGEST_SIZE];
  314. memset(k_ipad, 0x36, ALI_MD5_KEY_IOPAD_SIZE);
  315. memset(k_opad, 0x5C, ALI_MD5_KEY_IOPAD_SIZE);
  316. for(i=0; i<keylen; i++)
  317. {
  318. if(i>=ALI_MD5_KEY_IOPAD_SIZE)
  319. {
  320. break;
  321. }
  322. k_ipad[i] ^=key[i];
  323. k_opad[i] ^=key[i];
  324. }
  325. mbedtls_md5_init(&ctx);
  326. mbedtls_md5_starts(&ctx);
  327. mbedtls_md5_update(&ctx, k_ipad, ALI_MD5_KEY_IOPAD_SIZE);
  328. mbedtls_md5_update(&ctx, input, ilen);
  329. mbedtls_md5_finish(&ctx, tempbuf);
  330. mbedtls_md5_starts(&ctx);
  331. mbedtls_md5_update(&ctx, k_opad, ALI_MD5_KEY_IOPAD_SIZE);
  332. mbedtls_md5_update(&ctx, tempbuf, ALI_MD5_DIGEST_SIZE);
  333. mbedtls_md5_finish(&ctx, tempbuf);
  334. // for(i=0; i<ALI_MD5_DIGEST_SIZE; ++i)
  335. // {
  336. // output[i*2] = atHb2Hex(tempbuf[i]>>4);
  337. // output[i*2+1] = atHb2Hex(tempbuf[i]);
  338. // }
  339. memcpy(output, tempbuf, ALI_MD5_DIGEST_SIZE);
  340. mbedtls_md5_free(&ctx);
  341. }
  342. #endif
  343. #endif