luat_crypto_air105.c 12 KB

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