luat_crypto_win32.c 12 KB

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