luat_crypto_air105.c 14 KB

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