luat_crypto_rtt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "luat_base.h"
  2. #include "luat_crypto.h"
  3. #include "rtthread.h"
  4. #define LUAT_LOG_TAG "luat.crypto"
  5. #include "luat_log.h"
  6. #ifdef RT_USING_HWCRYPTO
  7. #include "hwcrypto.h"
  8. #ifdef RT_HWCRYPTO_USING_MD5
  9. int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr) {
  10. struct rt_hwcrypto_ctx *ctx;
  11. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
  12. if (ctx == NULL) {
  13. return -1; // 内存爆了??
  14. }
  15. rt_hwcrypto_hash_update(ctx, str, str_size);
  16. rt_hwcrypto_hash_finish(ctx, out_ptr, 32);
  17. rt_hwcrypto_hash_destroy(ctx);
  18. return 0;
  19. }
  20. int luat_crypto_hmac_md5_simple(const char* input, size_t ilen, const char* key, size_t keylen, void* output) {
  21. int i;
  22. struct rt_hwcrypto_ctx *ctx;
  23. unsigned char k_ipad[64] = {0};
  24. unsigned char k_opad[64] = {0};
  25. //unsigned char tempbuf[16];
  26. memset(k_ipad, 0x36, 64);
  27. memset(k_opad, 0x5C, 64);
  28. for(i=0; i<keylen; i++)
  29. {
  30. if(i>=64)
  31. {
  32. break;
  33. }
  34. k_ipad[i] ^=key[i];
  35. k_opad[i] ^=key[i];
  36. }
  37. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
  38. if (ctx == NULL) {
  39. return -1; // 内存爆了??
  40. }
  41. rt_hwcrypto_hash_update(ctx, k_ipad, 64);
  42. rt_hwcrypto_hash_update(ctx, input, ilen);
  43. rt_hwcrypto_hash_finish(ctx, output);
  44. //rt_hwcrypto_ctx_reset(ctx);
  45. rt_hwcrypto_hash_destroy(ctx);
  46. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
  47. rt_hwcrypto_hash_update(ctx, k_opad, 64);
  48. rt_hwcrypto_hash_update(ctx, output, 16);
  49. rt_hwcrypto_hash_finish(ctx, output);
  50. //rt_memcpy(output, tempbuf, 16);
  51. rt_hwcrypto_hash_destroy(ctx);
  52. return 0;
  53. }
  54. #endif
  55. #ifdef RT_HWCRYPTO_USING_SHA1
  56. int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr) {
  57. struct rt_hwcrypto_ctx *ctx;
  58. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
  59. if (ctx == NULL) {
  60. return -1; // 内存爆了??
  61. }
  62. rt_hwcrypto_hash_update(ctx, str, str_size);
  63. rt_hwcrypto_hash_finish(ctx, out_ptr, 40);
  64. rt_hwcrypto_hash_destroy(ctx);
  65. return 0;
  66. }
  67. int luat_crypto_hmac_sha1_simple(const char* input, size_t ilen, const char* key, size_t keylen, void* output) {
  68. int i;
  69. struct rt_hwcrypto_ctx *ctx;
  70. unsigned char k_ipad[64] = {0};
  71. unsigned char k_opad[64] = {0};
  72. //unsigned char tempbuf[20];
  73. memset(k_ipad, 0x36, 64);
  74. memset(k_opad, 0x5C, 64);
  75. for(i=0; i<keylen; i++)
  76. {
  77. if(i>=64)
  78. {
  79. break;
  80. }
  81. k_ipad[i] ^=key[i];
  82. k_opad[i] ^=key[i];
  83. }
  84. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
  85. if (ctx == NULL) {
  86. return -1; // 内存爆了??
  87. }
  88. rt_hwcrypto_hash_update(ctx, k_ipad, 64);
  89. rt_hwcrypto_hash_update(ctx, input, ilen);
  90. rt_hwcrypto_hash_finish(ctx, output);
  91. //rt_hwcrypto_ctx_reset(ctx);
  92. rt_hwcrypto_hash_destroy(ctx);
  93. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
  94. rt_hwcrypto_hash_update(ctx, k_opad, 64);
  95. rt_hwcrypto_hash_update(ctx, output, 20);
  96. rt_hwcrypto_hash_finish(ctx, output);
  97. //rt_memcpy(output, tempbuf, 20);
  98. rt_hwcrypto_hash_destroy(ctx);
  99. return 0;
  100. }
  101. #endif
  102. #endif
  103. #ifdef PKG_USING_MBEDTLS
  104. #include "mbedtls/cipher.h"
  105. #endif
  106. int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
  107. size_t cipher_size = 0;
  108. size_t pad_size = 0;
  109. size_t str_size = 0;
  110. size_t key_size = 0;
  111. size_t iv_size = 0;
  112. const char* cipher = luaL_optlstring(L, 1, "AES-128-ECB", &cipher_size);
  113. const char* pad = luaL_optlstring(L, 2, "PKCS7", &pad_size);
  114. const char* str = luaL_checklstring(L, 3, &str_size);
  115. const char* key = luaL_checklstring(L, 4, &key_size);
  116. const char* iv = luaL_optlstring(L, 5, "", &iv_size);
  117. int ret = 0;
  118. unsigned char output[32] = {0};
  119. size_t input_size = 0;
  120. size_t output_size = 0;
  121. size_t block_size = 0;
  122. luaL_Buffer buff;
  123. #ifdef PKG_USING_MBEDTLS
  124. mbedtls_cipher_context_t ctx;
  125. mbedtls_cipher_init(&ctx);
  126. const mbedtls_cipher_info_t * _cipher = mbedtls_cipher_info_from_string(cipher);
  127. if (_cipher == NULL) {
  128. lua_pushstring(L, "bad cipher name");
  129. lua_error(L);
  130. return 0;
  131. }
  132. ret = mbedtls_cipher_setup(&ctx, _cipher);
  133. if (ret) {
  134. LLOGE("mbedtls_cipher_setup fail %ld", ret);
  135. goto _exit;
  136. }
  137. ret = mbedtls_cipher_setkey(&ctx, key, key_size * 8, flags & 0x1);
  138. if (ret) {
  139. LLOGE("mbedtls_cipher_setkey fail %ld", ret);
  140. goto _exit;
  141. }
  142. // TODO 设置padding mode
  143. // mbedtls_cipher_set_padding_mode
  144. if (iv_size) {
  145. ret = mbedtls_cipher_set_iv(&ctx, iv, iv_size);
  146. if (ret) {
  147. LLOGE("mbedtls_cipher_set_iv fail %ld", ret);
  148. goto _exit;
  149. }
  150. }
  151. mbedtls_cipher_reset(&ctx);
  152. //mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_PKCS7);
  153. // 开始注入数据
  154. luaL_buffinit(L, &buff);
  155. block_size = mbedtls_cipher_get_block_size(&ctx);
  156. for (size_t i = 0; i < str_size; i+=block_size) {
  157. input_size = str_size - i;
  158. if (input_size > block_size)
  159. input_size = block_size;
  160. ret = mbedtls_cipher_update(&ctx, str+i, input_size, output, &output_size);
  161. if (ret) {
  162. LLOGE("mbedtls_cipher_update fail %ld", ret);
  163. goto _exit;
  164. }
  165. //else LLOGD("mbedtls_cipher_update, output size=%ld", output_size);
  166. if (output_size > 0)
  167. luaL_addlstring(&buff, output, output_size);
  168. output_size = 0;
  169. }
  170. ret = mbedtls_cipher_finish(&ctx, output, &output_size);
  171. if (ret) {
  172. LLOGE("mbedtls_cipher_finish fail %ld", ret);
  173. goto _exit;
  174. }
  175. //else LLOGD("mbedtls_cipher_finish, output size=%ld", output_size);
  176. if (output_size > 0)
  177. luaL_addlstring(&buff, output, output_size);
  178. _exit:
  179. mbedtls_cipher_free(&ctx);
  180. luaL_pushresult(&buff);
  181. return 1;
  182. #else
  183. //#ifdef RT_USING_HWCRYPTO
  184. // rtt的AES硬件加密, CBC输出的是等长数据, 不太对吧
  185. #if 0
  186. struct rt_hwcrypto_ctx *ctx = NULL;
  187. #ifdef RT_HWCRYPTO_USING_AES
  188. hwcrypto_mode mode = flags == 0 ? HWCRYPTO_MODE_DECRYPT : HWCRYPTO_MODE_ENCRYPT;
  189. ret = -1;
  190. luaL_buffinit(L, &buff);
  191. #ifdef RT_HWCRYPTO_USING_AES_ECB
  192. if (!strcmp("AES-128-ECB", cipher)) {
  193. LLOGD("AES-128-ECB HWCRYPTO");
  194. ctx = rt_hwcrypto_symmetric_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_AES_ECB);
  195. ret = rt_hwcrypto_symmetric_setkey(ctx, key, 128);
  196. ret = rt_hwcrypto_symmetric_crypt(ctx, mode, (str_size / 16) * 16, str, output);
  197. buff.n = str_size;
  198. }
  199. #endif
  200. #ifdef RT_HWCRYPTO_USING_AES_CBC
  201. if (!strcmp("AES-128-CBC", cipher)) {
  202. LLOGD("AES-128-CBC HWCRYPTO");
  203. ctx = rt_hwcrypto_symmetric_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_AES_CBC);
  204. ret = rt_hwcrypto_symmetric_setkey(ctx, key, 128);
  205. ret = rt_hwcrypto_symmetric_setiv(ctx, iv, iv_size);
  206. ret = rt_hwcrypto_symmetric_crypt(ctx, mode, (str_size / 16) * 16, str, output);
  207. buff.n = str_size + (flags == 0 ? -16 : 16);
  208. }
  209. #endif
  210. if (ctx)
  211. rt_hwcrypto_symmetric_destroy(ctx);
  212. if (ret) {
  213. LLOGW("AES FAIL %d", ret);
  214. return 0;
  215. }
  216. else {
  217. luaL_pushresult(&buff);
  218. return 1;
  219. }
  220. #endif
  221. #endif
  222. return 0;
  223. #endif
  224. }