luat_crypto_rtt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. #include "luat_base.h"
  2. #include "luat_crypto.h"
  3. #include "rtthread.h"
  4. #define LUAT_LOG_TAG "crypto"
  5. #include "luat_log.h"
  6. #ifdef RT_USING_HWCRYPTO
  7. #include "hwcrypto.h"
  8. #ifdef RT_HWCRYPTO_USING_MD5
  9. #define LUAT_CRYPTO_MD5
  10. int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr) {
  11. struct rt_hwcrypto_ctx *ctx;
  12. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
  13. if (ctx == NULL) {
  14. return -1; // 内存爆了??
  15. }
  16. rt_hwcrypto_hash_update(ctx, str, str_size);
  17. rt_hwcrypto_hash_finish(ctx, out_ptr, 32);
  18. rt_hwcrypto_hash_destroy(ctx);
  19. return 0;
  20. }
  21. int luat_crypto_hmac_md5_simple(const char* input, size_t ilen, const char* key, size_t keylen, void* output) {
  22. int i;
  23. struct rt_hwcrypto_ctx *ctx;
  24. unsigned char k_ipad[64] = {0};
  25. unsigned char k_opad[64] = {0};
  26. //unsigned char tempbuf[16];
  27. memset(k_ipad, 0x36, 64);
  28. memset(k_opad, 0x5C, 64);
  29. for(i=0; i<keylen; i++)
  30. {
  31. if(i>=64)
  32. {
  33. break;
  34. }
  35. k_ipad[i] ^=key[i];
  36. k_opad[i] ^=key[i];
  37. }
  38. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
  39. if (ctx == NULL) {
  40. return -1; // 内存爆了??
  41. }
  42. rt_hwcrypto_hash_update(ctx, k_ipad, 64);
  43. rt_hwcrypto_hash_update(ctx, input, ilen);
  44. rt_hwcrypto_hash_finish(ctx, output);
  45. //rt_hwcrypto_ctx_reset(ctx);
  46. rt_hwcrypto_hash_destroy(ctx);
  47. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
  48. rt_hwcrypto_hash_update(ctx, k_opad, 64);
  49. rt_hwcrypto_hash_update(ctx, output, 16);
  50. rt_hwcrypto_hash_finish(ctx, output);
  51. //rt_memcpy(output, tempbuf, 16);
  52. rt_hwcrypto_hash_destroy(ctx);
  53. return 0;
  54. }
  55. #endif
  56. #ifdef RT_HWCRYPTO_USING_SHA1
  57. #define LUAT_CRYPTO_SHA1
  58. int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr) {
  59. struct rt_hwcrypto_ctx *ctx;
  60. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
  61. if (ctx == NULL) {
  62. return -1; // 内存爆了??
  63. }
  64. rt_hwcrypto_hash_update(ctx, str, str_size);
  65. rt_hwcrypto_hash_finish(ctx, out_ptr, 40);
  66. rt_hwcrypto_hash_destroy(ctx);
  67. return 0;
  68. }
  69. int luat_crypto_hmac_sha1_simple(const char* input, size_t ilen, const char* key, size_t keylen, void* output) {
  70. int i;
  71. struct rt_hwcrypto_ctx *ctx;
  72. unsigned char k_ipad[64] = {0};
  73. unsigned char k_opad[64] = {0};
  74. //unsigned char tempbuf[20];
  75. memset(k_ipad, 0x36, 64);
  76. memset(k_opad, 0x5C, 64);
  77. for(i=0; i<keylen; i++)
  78. {
  79. if(i>=64)
  80. {
  81. break;
  82. }
  83. k_ipad[i] ^=key[i];
  84. k_opad[i] ^=key[i];
  85. }
  86. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
  87. if (ctx == NULL) {
  88. return -1; // 内存爆了??
  89. }
  90. rt_hwcrypto_hash_update(ctx, k_ipad, 64);
  91. rt_hwcrypto_hash_update(ctx, input, ilen);
  92. rt_hwcrypto_hash_finish(ctx, output);
  93. //rt_hwcrypto_ctx_reset(ctx);
  94. rt_hwcrypto_hash_destroy(ctx);
  95. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
  96. rt_hwcrypto_hash_update(ctx, k_opad, 64);
  97. rt_hwcrypto_hash_update(ctx, output, 20);
  98. rt_hwcrypto_hash_finish(ctx, output);
  99. //rt_memcpy(output, tempbuf, 20);
  100. rt_hwcrypto_hash_destroy(ctx);
  101. return 0;
  102. }
  103. #endif
  104. #endif
  105. #ifdef PKG_USING_MBEDTLS
  106. #include "mbedtls/cipher.h"
  107. #endif
  108. int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
  109. size_t cipher_size = 0;
  110. size_t pad_size = 0;
  111. size_t str_size = 0;
  112. size_t key_size = 0;
  113. size_t iv_size = 0;
  114. const char* cipher = luaL_optlstring(L, 1, "AES-128-ECB", &cipher_size);
  115. const char* pad = luaL_optlstring(L, 2, "PKCS7", &pad_size);
  116. const char* str = luaL_checklstring(L, 3, &str_size);
  117. const char* key = luaL_checklstring(L, 4, &key_size);
  118. const char* iv = luaL_optlstring(L, 5, "", &iv_size);
  119. int ret = 0;
  120. unsigned char output[32] = {0};
  121. size_t input_size = 0;
  122. size_t output_size = 0;
  123. size_t block_size = 0;
  124. luaL_Buffer buff;
  125. #ifdef PKG_USING_MBEDTLS
  126. mbedtls_cipher_context_t ctx;
  127. mbedtls_cipher_init(&ctx);
  128. const mbedtls_cipher_info_t * _cipher = mbedtls_cipher_info_from_string(cipher);
  129. if (_cipher == NULL) {
  130. lua_pushstring(L, "bad cipher name");
  131. lua_error(L);
  132. return 0;
  133. }
  134. ret = mbedtls_cipher_setup(&ctx, _cipher);
  135. if (ret) {
  136. LLOGE("mbedtls_cipher_setup fail %ld", ret);
  137. goto _exit;
  138. }
  139. ret = mbedtls_cipher_setkey(&ctx, key, key_size * 8, flags & 0x1);
  140. if (ret) {
  141. LLOGE("mbedtls_cipher_setkey fail %ld", ret);
  142. goto _exit;
  143. }
  144. // TODO 设置padding mode
  145. // mbedtls_cipher_set_padding_mode
  146. if (iv_size) {
  147. ret = mbedtls_cipher_set_iv(&ctx, iv, iv_size);
  148. if (ret) {
  149. LLOGE("mbedtls_cipher_set_iv fail %ld", ret);
  150. goto _exit;
  151. }
  152. }
  153. mbedtls_cipher_reset(&ctx);
  154. //mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_PKCS7);
  155. // 开始注入数据
  156. luaL_buffinit(L, &buff);
  157. block_size = mbedtls_cipher_get_block_size(&ctx);
  158. for (size_t i = 0; i < str_size; i+=block_size) {
  159. input_size = str_size - i;
  160. if (input_size > block_size)
  161. input_size = block_size;
  162. ret = mbedtls_cipher_update(&ctx, str+i, input_size, output, &output_size);
  163. if (ret) {
  164. LLOGE("mbedtls_cipher_update fail %ld", ret);
  165. goto _exit;
  166. }
  167. //else LLOGD("mbedtls_cipher_update, output size=%ld", output_size);
  168. if (output_size > 0)
  169. luaL_addlstring(&buff, output, output_size);
  170. output_size = 0;
  171. }
  172. ret = mbedtls_cipher_finish(&ctx, output, &output_size);
  173. if (ret) {
  174. LLOGE("mbedtls_cipher_finish fail %ld", ret);
  175. goto _exit;
  176. }
  177. //else LLOGD("mbedtls_cipher_finish, output size=%ld", output_size);
  178. if (output_size > 0)
  179. luaL_addlstring(&buff, output, output_size);
  180. _exit:
  181. mbedtls_cipher_free(&ctx);
  182. luaL_pushresult(&buff);
  183. return 1;
  184. #else
  185. //#ifdef RT_USING_HWCRYPTO
  186. // rtt的AES硬件加密, CBC输出的是等长数据, 不太对吧
  187. #if 0
  188. struct rt_hwcrypto_ctx *ctx = NULL;
  189. #ifdef RT_HWCRYPTO_USING_AES
  190. hwcrypto_mode mode = flags == 0 ? HWCRYPTO_MODE_DECRYPT : HWCRYPTO_MODE_ENCRYPT;
  191. ret = -1;
  192. luaL_buffinit(L, &buff);
  193. #ifdef RT_HWCRYPTO_USING_AES_ECB
  194. if (!strcmp("AES-128-ECB", cipher)) {
  195. LLOGD("AES-128-ECB HWCRYPTO");
  196. ctx = rt_hwcrypto_symmetric_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_AES_ECB);
  197. ret = rt_hwcrypto_symmetric_setkey(ctx, key, 128);
  198. ret = rt_hwcrypto_symmetric_crypt(ctx, mode, (str_size / 16) * 16, str, output);
  199. buff.n = str_size;
  200. }
  201. #endif
  202. #ifdef RT_HWCRYPTO_USING_AES_CBC
  203. if (!strcmp("AES-128-CBC", cipher)) {
  204. LLOGD("AES-128-CBC HWCRYPTO");
  205. ctx = rt_hwcrypto_symmetric_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_AES_CBC);
  206. ret = rt_hwcrypto_symmetric_setkey(ctx, key, 128);
  207. ret = rt_hwcrypto_symmetric_setiv(ctx, iv, iv_size);
  208. ret = rt_hwcrypto_symmetric_crypt(ctx, mode, (str_size / 16) * 16, str, output);
  209. buff.n = str_size + (flags == 0 ? -16 : 16);
  210. }
  211. #endif
  212. if (ctx)
  213. rt_hwcrypto_symmetric_destroy(ctx);
  214. if (ret) {
  215. LLOGW("AES FAIL %d", ret);
  216. return 0;
  217. }
  218. else {
  219. luaL_pushresult(&buff);
  220. return 1;
  221. }
  222. #endif
  223. #endif
  224. return 0;
  225. #endif
  226. }
  227. #ifdef PKG_USING_MBEDTLS
  228. #if !defined(MBEDTLS_CONFIG_FILE)
  229. #include "mbedtls/config.h"
  230. #else
  231. #include MBEDTLS_CONFIG_FILE
  232. #endif
  233. #include "mbedtls/sha1.h"
  234. #include "mbedtls/sha256.h"
  235. #ifdef MBEDTLS_SHA512_C
  236. #include "mbedtls/sha512.h"
  237. #endif
  238. #include "mbedtls/md5.h"
  239. #define LUAT_LOG_TAG "crypto"
  240. #include "luat_log.h"
  241. void luat_crypto_HmacSha1(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen);
  242. void luat_crypto_HmacSha256(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen);
  243. void luat_crypto_HmacSha512(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen);
  244. void luat_crypto_HmacMd5(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen);
  245. #ifndef LUAT_CRYPTO_MD5
  246. #define LUAT_CRYPTO_MD5
  247. int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr) {
  248. mbedtls_md5_context ctx;
  249. mbedtls_md5_init(&ctx);
  250. mbedtls_md5_starts(&ctx);
  251. mbedtls_md5_update(&ctx, (const unsigned char *)str, str_size);
  252. mbedtls_md5_finish(&ctx, (unsigned char *)out_ptr);
  253. mbedtls_md5_free(&ctx);
  254. return 0;
  255. }
  256. #endif
  257. #ifndef LUAT_CRYPTO_SHA1
  258. #define LUAT_CRYPTO_SHA1
  259. int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr) {
  260. mbedtls_sha1_context ctx;
  261. mbedtls_sha1_init(&ctx);
  262. mbedtls_sha1_starts(&ctx);
  263. mbedtls_sha1_update(&ctx, (const unsigned char *)str, str_size);
  264. mbedtls_sha1_finish(&ctx, (unsigned char *)out_ptr);
  265. mbedtls_sha1_free(&ctx);
  266. return 0;
  267. }
  268. #endif
  269. int luat_crypto_sha256_simple(const char* str, size_t str_size, void* out_ptr) {
  270. mbedtls_sha256_context ctx;
  271. mbedtls_sha256_init(&ctx);
  272. mbedtls_sha256_starts(&ctx, 0);
  273. mbedtls_sha256_update(&ctx, (const unsigned char *)str, str_size);
  274. mbedtls_sha256_finish(&ctx, (unsigned char *)out_ptr);
  275. mbedtls_sha256_free(&ctx);
  276. return 0;
  277. }
  278. #ifdef MBEDTLS_SHA512_C
  279. int luat_crypto_sha512_simple(const char* str, size_t str_size, void* out_ptr) {
  280. mbedtls_sha512_context ctx;
  281. mbedtls_sha512_init(&ctx);
  282. mbedtls_sha512_starts(&ctx, 0);
  283. mbedtls_sha512_update(&ctx, (const unsigned char *)str, str_size);
  284. mbedtls_sha512_finish(&ctx, (unsigned char *)out_ptr);
  285. mbedtls_sha512_free(&ctx);
  286. return 0;
  287. }
  288. #endif
  289. #ifndef LUAT_CRYPTO_MD5
  290. #define LUAT_CRYPTO_MD5
  291. int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  292. luat_crypto_HmacMd5((const unsigned char *)str, str_size, (unsigned char *)out_ptr, (const unsigned char *)mac, mac_size);
  293. return 0;
  294. }
  295. #endif
  296. #ifndef LUAT_CRYPTO_SHA1
  297. #define LUAT_CRYPTO_SHA1
  298. int luat_crypto_hmac_sha1_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  299. luat_crypto_HmacSha1((const unsigned char *)str, str_size, (unsigned char *)out_ptr, (const unsigned char *)mac, mac_size);
  300. return 0;
  301. }
  302. #endif
  303. int luat_crypto_hmac_sha256_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  304. luat_crypto_HmacSha256((const unsigned char *)str, str_size, (unsigned char *)out_ptr, (const unsigned char *)mac, mac_size);
  305. return 0;
  306. }
  307. #ifdef MBEDTLS_SHA512_C
  308. int luat_crypto_hmac_sha512_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
  309. luat_crypto_HmacSha512((const unsigned char *)str, str_size, (unsigned char *)out_ptr, (const unsigned char *)mac, mac_size);
  310. return 0;
  311. }
  312. #endif
  313. ///----------------------------
  314. #define ALI_SHA1_KEY_IOPAD_SIZE (64)
  315. #define ALI_SHA1_DIGEST_SIZE (20)
  316. #define ALI_SHA256_KEY_IOPAD_SIZE (64)
  317. #define ALI_SHA256_DIGEST_SIZE (32)
  318. #define ALI_SHA512_KEY_IOPAD_SIZE (128)
  319. #define ALI_SHA512_DIGEST_SIZE (64)
  320. #define ALI_MD5_KEY_IOPAD_SIZE (64)
  321. #define ALI_MD5_DIGEST_SIZE (16)
  322. // char atHb2Hex(unsigned char hb)
  323. // {
  324. // hb = hb&0xF;
  325. // return (char)(hb<10 ? '0'+hb : hb-10+'a');
  326. // }
  327. /*
  328. * output = SHA-1( input buffer )
  329. */
  330. void luat_crypto_HmacSha1(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen)
  331. {
  332. int i;
  333. mbedtls_sha1_context ctx;
  334. unsigned char k_ipad[ALI_SHA1_KEY_IOPAD_SIZE] = {0};
  335. unsigned char k_opad[ALI_SHA1_KEY_IOPAD_SIZE] = {0};
  336. unsigned char tempbuf[ALI_SHA1_DIGEST_SIZE];
  337. memset(k_ipad, 0x36, ALI_SHA1_KEY_IOPAD_SIZE);
  338. memset(k_opad, 0x5C, ALI_SHA1_KEY_IOPAD_SIZE);
  339. for(i=0; i<keylen; i++)
  340. {
  341. if(i>=ALI_SHA1_KEY_IOPAD_SIZE)
  342. {
  343. break;
  344. }
  345. k_ipad[i] ^=key[i];
  346. k_opad[i] ^=key[i];
  347. }
  348. mbedtls_sha1_init(&ctx);
  349. mbedtls_sha1_starts(&ctx);
  350. mbedtls_sha1_update(&ctx, k_ipad, ALI_SHA1_KEY_IOPAD_SIZE);
  351. mbedtls_sha1_update(&ctx, input, ilen);
  352. mbedtls_sha1_finish(&ctx, tempbuf);
  353. mbedtls_sha1_starts(&ctx);
  354. mbedtls_sha1_update(&ctx, k_opad, ALI_SHA1_KEY_IOPAD_SIZE);
  355. mbedtls_sha1_update(&ctx, tempbuf, ALI_SHA1_DIGEST_SIZE);
  356. mbedtls_sha1_finish(&ctx, tempbuf);
  357. // for(i=0; i<ALI_SHA1_DIGEST_SIZE; ++i)
  358. // {
  359. // output[i*2] = atHb2Hex(tempbuf[i]>>4);
  360. // output[i*2+1] = atHb2Hex(tempbuf[i]);
  361. // }
  362. memcpy(output, tempbuf, ALI_SHA1_DIGEST_SIZE);
  363. mbedtls_sha1_free(&ctx);
  364. }
  365. /*
  366. * output = SHA-256( input buffer )
  367. */
  368. void luat_crypto_HmacSha256(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen)
  369. {
  370. int i;
  371. mbedtls_sha256_context ctx;
  372. unsigned char k_ipad[ALI_SHA256_KEY_IOPAD_SIZE] = {0};
  373. unsigned char k_opad[ALI_SHA256_KEY_IOPAD_SIZE] = {0};
  374. memset(k_ipad, 0x36, 64);
  375. memset(k_opad, 0x5C, 64);
  376. if ((NULL == input) || (NULL == key) || (NULL == output)) {
  377. return;
  378. }
  379. if (keylen > ALI_SHA256_KEY_IOPAD_SIZE) {
  380. return;
  381. }
  382. for(i=0; i<keylen; i++)
  383. {
  384. if(i>=ALI_SHA256_KEY_IOPAD_SIZE)
  385. {
  386. break;
  387. }
  388. k_ipad[i] ^=key[i];
  389. k_opad[i] ^=key[i];
  390. }
  391. mbedtls_sha256_init(&ctx);
  392. mbedtls_sha256_starts(&ctx, 0);
  393. mbedtls_sha256_update(&ctx, k_ipad, ALI_SHA256_KEY_IOPAD_SIZE);
  394. mbedtls_sha256_update(&ctx, input, ilen);
  395. mbedtls_sha256_finish(&ctx, output);
  396. mbedtls_sha256_starts(&ctx, 0);
  397. mbedtls_sha256_update(&ctx, k_opad, ALI_SHA256_KEY_IOPAD_SIZE);
  398. mbedtls_sha256_update(&ctx, output, ALI_SHA256_DIGEST_SIZE);
  399. mbedtls_sha256_finish(&ctx, output);
  400. mbedtls_sha256_free(&ctx);
  401. }
  402. #ifdef MBEDTLS_SHA512_C
  403. /*
  404. * output = SHA-512( input buffer )
  405. */
  406. void luat_crypto_HmacSha512(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen)
  407. {
  408. int i;
  409. mbedtls_sha512_context ctx;
  410. unsigned char k_ipad[ALI_SHA512_KEY_IOPAD_SIZE] = {0};
  411. unsigned char k_opad[ALI_SHA512_KEY_IOPAD_SIZE] = {0};
  412. memset(k_ipad, 0x36, 64);
  413. memset(k_opad, 0x5C, 64);
  414. if ((NULL == input) || (NULL == key) || (NULL == output)) {
  415. return;
  416. }
  417. if (keylen > ALI_SHA512_KEY_IOPAD_SIZE) {
  418. return;
  419. }
  420. for(i=0; i<keylen; i++)
  421. {
  422. if(i>=ALI_SHA512_KEY_IOPAD_SIZE)
  423. {
  424. break;
  425. }
  426. k_ipad[i] ^=key[i];
  427. k_opad[i] ^=key[i];
  428. }
  429. mbedtls_sha512_init(&ctx);
  430. mbedtls_sha512_starts(&ctx, 0);
  431. mbedtls_sha512_update(&ctx, k_ipad, ALI_SHA512_KEY_IOPAD_SIZE);
  432. mbedtls_sha512_update(&ctx, input, ilen);
  433. mbedtls_sha512_finish(&ctx, output);
  434. mbedtls_sha512_starts(&ctx, 0);
  435. mbedtls_sha512_update(&ctx, k_opad, ALI_SHA512_KEY_IOPAD_SIZE);
  436. mbedtls_sha512_update(&ctx, output, ALI_SHA512_DIGEST_SIZE);
  437. mbedtls_sha512_finish(&ctx, output);
  438. mbedtls_sha512_free(&ctx);
  439. }
  440. #endif
  441. /*
  442. * output = MD-5( input buffer )
  443. */
  444. void luat_crypto_HmacMd5(const unsigned char *input, int ilen, unsigned char *output,const unsigned char *key, int keylen)
  445. {
  446. int i;
  447. mbedtls_md5_context ctx;
  448. unsigned char k_ipad[ALI_MD5_KEY_IOPAD_SIZE] = {0};
  449. unsigned char k_opad[ALI_MD5_KEY_IOPAD_SIZE] = {0};
  450. unsigned char tempbuf[ALI_MD5_DIGEST_SIZE];
  451. memset(k_ipad, 0x36, ALI_MD5_KEY_IOPAD_SIZE);
  452. memset(k_opad, 0x5C, ALI_MD5_KEY_IOPAD_SIZE);
  453. for(i=0; i<keylen; i++)
  454. {
  455. if(i>=ALI_MD5_KEY_IOPAD_SIZE)
  456. {
  457. break;
  458. }
  459. k_ipad[i] ^=key[i];
  460. k_opad[i] ^=key[i];
  461. }
  462. mbedtls_md5_init(&ctx);
  463. mbedtls_md5_starts(&ctx);
  464. mbedtls_md5_update(&ctx, k_ipad, ALI_MD5_KEY_IOPAD_SIZE);
  465. mbedtls_md5_update(&ctx, input, ilen);
  466. mbedtls_md5_finish(&ctx, tempbuf);
  467. mbedtls_md5_starts(&ctx);
  468. mbedtls_md5_update(&ctx, k_opad, ALI_MD5_KEY_IOPAD_SIZE);
  469. mbedtls_md5_update(&ctx, tempbuf, ALI_MD5_DIGEST_SIZE);
  470. mbedtls_md5_finish(&ctx, tempbuf);
  471. // for(i=0; i<ALI_MD5_DIGEST_SIZE; ++i)
  472. // {
  473. // output[i*2] = atHb2Hex(tempbuf[i]>>4);
  474. // output[i*2+1] = atHb2Hex(tempbuf[i]);
  475. // }
  476. memcpy(output, tempbuf, ALI_MD5_DIGEST_SIZE);
  477. mbedtls_md5_free(&ctx);
  478. }
  479. #endif