luat_crypto_air105.c 13 KB

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