md5.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /* md5.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #if !defined(NO_MD5)
  26. #if defined(WOLFSSL_TI_HASH)
  27. /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
  28. #else
  29. #include <wolfssl/wolfcrypt/md5.h>
  30. #include <wolfssl/wolfcrypt/error-crypt.h>
  31. #include <wolfssl/wolfcrypt/logging.h>
  32. #include <wolfssl/wolfcrypt/hash.h>
  33. #ifdef NO_INLINE
  34. #include <wolfssl/wolfcrypt/misc.h>
  35. #else
  36. #define WOLFSSL_MISC_INCLUDED
  37. #include <wolfcrypt/src/misc.c>
  38. #endif
  39. /* Hardware Acceleration */
  40. #if defined(STM32_HASH)
  41. /* Supports CubeMX HAL or Standard Peripheral Library */
  42. #define HAVE_MD5_CUST_API
  43. int wc_InitMd5_ex(wc_Md5* md5, void* heap, int devId)
  44. {
  45. if (md5 == NULL) {
  46. return BAD_FUNC_ARG;
  47. }
  48. (void)devId;
  49. (void)heap;
  50. wc_Stm32_Hash_Init(&md5->stmCtx);
  51. return 0;
  52. }
  53. int wc_Md5Update(wc_Md5* md5, const byte* data, word32 len)
  54. {
  55. int ret;
  56. if (md5 == NULL || (data == NULL && len > 0)) {
  57. return BAD_FUNC_ARG;
  58. }
  59. ret = wolfSSL_CryptHwMutexLock();
  60. if (ret == 0) {
  61. ret = wc_Stm32_Hash_Update(&md5->stmCtx, HASH_AlgoSelection_MD5,
  62. data, len, WC_MD5_BLOCK_SIZE);
  63. wolfSSL_CryptHwMutexUnLock();
  64. }
  65. return ret;
  66. }
  67. int wc_Md5Final(wc_Md5* md5, byte* hash)
  68. {
  69. int ret;
  70. if (md5 == NULL || hash == NULL) {
  71. return BAD_FUNC_ARG;
  72. }
  73. ret = wolfSSL_CryptHwMutexLock();
  74. if (ret == 0) {
  75. ret = wc_Stm32_Hash_Final(&md5->stmCtx, HASH_AlgoSelection_MD5,
  76. hash, WC_MD5_DIGEST_SIZE);
  77. wolfSSL_CryptHwMutexUnLock();
  78. }
  79. (void)wc_InitMd5(md5); /* reset state */
  80. return ret;
  81. }
  82. #elif defined(FREESCALE_MMCAU_SHA)
  83. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  84. #include "cau_api.h"
  85. #else
  86. #include "fsl_mmcau.h"
  87. #endif
  88. #define XTRANSFORM(S,B) Transform((S), (B))
  89. #define XTRANSFORM_LEN(S,B,L) Transform_Len((S), (B), (L))
  90. #ifndef WC_HASH_DATA_ALIGNMENT
  91. /* these hardware API's require 4 byte (word32) alignment */
  92. #define WC_HASH_DATA_ALIGNMENT 4
  93. #endif
  94. static int Transform(wc_Md5* md5, const byte* data)
  95. {
  96. int ret = wolfSSL_CryptHwMutexLock();
  97. if (ret == 0) {
  98. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  99. cau_md5_hash_n((byte*)data, 1, (unsigned char*)md5->digest);
  100. #else
  101. MMCAU_MD5_HashN((byte*)data, 1, (uint32_t*)md5->digest);
  102. #endif
  103. wolfSSL_CryptHwMutexUnLock();
  104. }
  105. return ret;
  106. }
  107. static int Transform_Len(wc_Md5* md5, const byte* data, word32 len)
  108. {
  109. int ret = wolfSSL_CryptHwMutexLock();
  110. if (ret == 0) {
  111. #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
  112. if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) {
  113. /* data pointer is NOT aligned,
  114. * so copy and perform one block at a time */
  115. byte* local = (byte*)md5->buffer;
  116. while (len >= WC_MD5_BLOCK_SIZE) {
  117. XMEMCPY(local, data, WC_MD5_BLOCK_SIZE);
  118. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  119. cau_md5_hash_n(local, 1, (unsigned char*)md5->digest);
  120. #else
  121. MMCAU_MD5_HashN(local, 1, (uint32_t*)md5->digest);
  122. #endif
  123. data += WC_MD5_BLOCK_SIZE;
  124. len -= WC_MD5_BLOCK_SIZE;
  125. }
  126. }
  127. else
  128. #endif
  129. {
  130. #ifdef FREESCALE_MMCAU_CLASSIC_SHA
  131. cau_md5_hash_n((byte*)data, len / WC_MD5_BLOCK_SIZE,
  132. (unsigned char*)md5->digest);
  133. #else
  134. MMCAU_MD5_HashN((byte*)data, len / WC_MD5_BLOCK_SIZE,
  135. (uint32_t*)md5->digest);
  136. #endif
  137. }
  138. wolfSSL_CryptHwMutexUnLock();
  139. }
  140. return ret;
  141. }
  142. #elif defined(WOLFSSL_PIC32MZ_HASH)
  143. #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
  144. #define HAVE_MD5_CUST_API
  145. #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \
  146. !defined(WOLFSSL_QNX_CAAM)
  147. /* functions implemented in wolfcrypt/src/port/caam/caam_sha.c */
  148. #define HAVE_MD5_CUST_API
  149. #else
  150. #define NEED_SOFT_MD5
  151. #endif /* End Hardware Acceleration */
  152. #ifdef NEED_SOFT_MD5
  153. #define XTRANSFORM(S,B) Transform((S),(B))
  154. #define F1(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  155. #define F2(x, y, z) F1(z, x, y)
  156. #define F3(x, y, z) ((x) ^ (y) ^ (z))
  157. #define F4(x, y, z) ((y) ^ ((x) | ~(z)))
  158. #define MD5STEP(f, w, x, y, z, data, s) \
  159. (w) = (rotlFixed((w) + f(x, y, z) + (data), s) + (x))
  160. static int Transform(wc_Md5* md5, const byte* data)
  161. {
  162. const word32* buffer = (const word32*)data;
  163. /* Copy context->state[] to working vars */
  164. word32 a = md5->digest[0];
  165. word32 b = md5->digest[1];
  166. word32 c = md5->digest[2];
  167. word32 d = md5->digest[3];
  168. MD5STEP(F1, a, b, c, d, buffer[0] + 0xd76aa478, 7);
  169. MD5STEP(F1, d, a, b, c, buffer[1] + 0xe8c7b756, 12);
  170. MD5STEP(F1, c, d, a, b, buffer[2] + 0x242070db, 17);
  171. MD5STEP(F1, b, c, d, a, buffer[3] + 0xc1bdceee, 22);
  172. MD5STEP(F1, a, b, c, d, buffer[4] + 0xf57c0faf, 7);
  173. MD5STEP(F1, d, a, b, c, buffer[5] + 0x4787c62a, 12);
  174. MD5STEP(F1, c, d, a, b, buffer[6] + 0xa8304613, 17);
  175. MD5STEP(F1, b, c, d, a, buffer[7] + 0xfd469501, 22);
  176. MD5STEP(F1, a, b, c, d, buffer[8] + 0x698098d8, 7);
  177. MD5STEP(F1, d, a, b, c, buffer[9] + 0x8b44f7af, 12);
  178. MD5STEP(F1, c, d, a, b, buffer[10] + 0xffff5bb1, 17);
  179. MD5STEP(F1, b, c, d, a, buffer[11] + 0x895cd7be, 22);
  180. MD5STEP(F1, a, b, c, d, buffer[12] + 0x6b901122, 7);
  181. MD5STEP(F1, d, a, b, c, buffer[13] + 0xfd987193, 12);
  182. MD5STEP(F1, c, d, a, b, buffer[14] + 0xa679438e, 17);
  183. MD5STEP(F1, b, c, d, a, buffer[15] + 0x49b40821, 22);
  184. MD5STEP(F2, a, b, c, d, buffer[1] + 0xf61e2562, 5);
  185. MD5STEP(F2, d, a, b, c, buffer[6] + 0xc040b340, 9);
  186. MD5STEP(F2, c, d, a, b, buffer[11] + 0x265e5a51, 14);
  187. MD5STEP(F2, b, c, d, a, buffer[0] + 0xe9b6c7aa, 20);
  188. MD5STEP(F2, a, b, c, d, buffer[5] + 0xd62f105d, 5);
  189. MD5STEP(F2, d, a, b, c, buffer[10] + 0x02441453, 9);
  190. MD5STEP(F2, c, d, a, b, buffer[15] + 0xd8a1e681, 14);
  191. MD5STEP(F2, b, c, d, a, buffer[4] + 0xe7d3fbc8, 20);
  192. MD5STEP(F2, a, b, c, d, buffer[9] + 0x21e1cde6, 5);
  193. MD5STEP(F2, d, a, b, c, buffer[14] + 0xc33707d6, 9);
  194. MD5STEP(F2, c, d, a, b, buffer[3] + 0xf4d50d87, 14);
  195. MD5STEP(F2, b, c, d, a, buffer[8] + 0x455a14ed, 20);
  196. MD5STEP(F2, a, b, c, d, buffer[13] + 0xa9e3e905, 5);
  197. MD5STEP(F2, d, a, b, c, buffer[2] + 0xfcefa3f8, 9);
  198. MD5STEP(F2, c, d, a, b, buffer[7] + 0x676f02d9, 14);
  199. MD5STEP(F2, b, c, d, a, buffer[12] + 0x8d2a4c8a, 20);
  200. MD5STEP(F3, a, b, c, d, buffer[5] + 0xfffa3942, 4);
  201. MD5STEP(F3, d, a, b, c, buffer[8] + 0x8771f681, 11);
  202. MD5STEP(F3, c, d, a, b, buffer[11] + 0x6d9d6122, 16);
  203. MD5STEP(F3, b, c, d, a, buffer[14] + 0xfde5380c, 23);
  204. MD5STEP(F3, a, b, c, d, buffer[1] + 0xa4beea44, 4);
  205. MD5STEP(F3, d, a, b, c, buffer[4] + 0x4bdecfa9, 11);
  206. MD5STEP(F3, c, d, a, b, buffer[7] + 0xf6bb4b60, 16);
  207. MD5STEP(F3, b, c, d, a, buffer[10] + 0xbebfbc70, 23);
  208. MD5STEP(F3, a, b, c, d, buffer[13] + 0x289b7ec6, 4);
  209. MD5STEP(F3, d, a, b, c, buffer[0] + 0xeaa127fa, 11);
  210. MD5STEP(F3, c, d, a, b, buffer[3] + 0xd4ef3085, 16);
  211. MD5STEP(F3, b, c, d, a, buffer[6] + 0x04881d05, 23);
  212. MD5STEP(F3, a, b, c, d, buffer[9] + 0xd9d4d039, 4);
  213. MD5STEP(F3, d, a, b, c, buffer[12] + 0xe6db99e5, 11);
  214. MD5STEP(F3, c, d, a, b, buffer[15] + 0x1fa27cf8, 16);
  215. MD5STEP(F3, b, c, d, a, buffer[2] + 0xc4ac5665, 23);
  216. MD5STEP(F4, a, b, c, d, buffer[0] + 0xf4292244, 6);
  217. MD5STEP(F4, d, a, b, c, buffer[7] + 0x432aff97, 10);
  218. MD5STEP(F4, c, d, a, b, buffer[14] + 0xab9423a7, 15);
  219. MD5STEP(F4, b, c, d, a, buffer[5] + 0xfc93a039, 21);
  220. MD5STEP(F4, a, b, c, d, buffer[12] + 0x655b59c3, 6);
  221. MD5STEP(F4, d, a, b, c, buffer[3] + 0x8f0ccc92, 10);
  222. MD5STEP(F4, c, d, a, b, buffer[10] + 0xffeff47d, 15);
  223. MD5STEP(F4, b, c, d, a, buffer[1] + 0x85845dd1, 21);
  224. MD5STEP(F4, a, b, c, d, buffer[8] + 0x6fa87e4f, 6);
  225. MD5STEP(F4, d, a, b, c, buffer[15] + 0xfe2ce6e0, 10);
  226. MD5STEP(F4, c, d, a, b, buffer[6] + 0xa3014314, 15);
  227. MD5STEP(F4, b, c, d, a, buffer[13] + 0x4e0811a1, 21);
  228. MD5STEP(F4, a, b, c, d, buffer[4] + 0xf7537e82, 6);
  229. MD5STEP(F4, d, a, b, c, buffer[11] + 0xbd3af235, 10);
  230. MD5STEP(F4, c, d, a, b, buffer[2] + 0x2ad7d2bb, 15);
  231. MD5STEP(F4, b, c, d, a, buffer[9] + 0xeb86d391, 21);
  232. /* Add the working vars back into digest state[] */
  233. md5->digest[0] += a;
  234. md5->digest[1] += b;
  235. md5->digest[2] += c;
  236. md5->digest[3] += d;
  237. return 0;
  238. }
  239. #endif /* NEED_SOFT_MD5 */
  240. #ifndef HAVE_MD5_CUST_API
  241. static WC_INLINE void AddLength(wc_Md5* md5, word32 len)
  242. {
  243. word32 tmp = md5->loLen;
  244. if ((md5->loLen += len) < tmp) {
  245. md5->hiLen++; /* carry low to high */
  246. }
  247. }
  248. static int _InitMd5(wc_Md5* md5)
  249. {
  250. int ret = 0;
  251. md5->digest[0] = 0x67452301L;
  252. md5->digest[1] = 0xefcdab89L;
  253. md5->digest[2] = 0x98badcfeL;
  254. md5->digest[3] = 0x10325476L;
  255. md5->buffLen = 0;
  256. md5->loLen = 0;
  257. md5->hiLen = 0;
  258. #ifdef WOLFSSL_HASH_FLAGS
  259. md5->flags = 0;
  260. #endif
  261. return ret;
  262. }
  263. int wc_InitMd5_ex(wc_Md5* md5, void* heap, int devId)
  264. {
  265. int ret = 0;
  266. if (md5 == NULL)
  267. return BAD_FUNC_ARG;
  268. md5->heap = heap;
  269. ret = _InitMd5(md5);
  270. if (ret != 0)
  271. return ret;
  272. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
  273. ret = wolfAsync_DevCtxInit(&md5->asyncDev, WOLFSSL_ASYNC_MARKER_MD5,
  274. md5->heap, devId);
  275. #else
  276. (void)devId;
  277. #endif
  278. return ret;
  279. }
  280. /* do block size increments/updates */
  281. int wc_Md5Update(wc_Md5* md5, const byte* data, word32 len)
  282. {
  283. int ret = 0;
  284. word32 blocksLen;
  285. byte* local;
  286. if (md5 == NULL || (data == NULL && len > 0)) {
  287. return BAD_FUNC_ARG;
  288. }
  289. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
  290. if (md5->asyncDev.marker == WOLFSSL_ASYNC_MARKER_MD5) {
  291. #if defined(HAVE_INTEL_QA)
  292. return IntelQaSymMd5(&md5->asyncDev, NULL, data, len);
  293. #endif
  294. }
  295. #endif /* WOLFSSL_ASYNC_CRYPT */
  296. /* check that internal buffLen is valid */
  297. if (md5->buffLen >= WC_MD5_BLOCK_SIZE)
  298. return BUFFER_E;
  299. if (data == NULL && len == 0) {
  300. /* valid, but do nothing */
  301. return 0;
  302. }
  303. /* add length for final */
  304. AddLength(md5, len);
  305. local = (byte*)md5->buffer;
  306. /* process any remainder from previous operation */
  307. if (md5->buffLen > 0) {
  308. blocksLen = min(len, WC_MD5_BLOCK_SIZE - md5->buffLen);
  309. XMEMCPY(&local[md5->buffLen], data, blocksLen);
  310. md5->buffLen += blocksLen;
  311. data += blocksLen;
  312. len -= blocksLen;
  313. if (md5->buffLen == WC_MD5_BLOCK_SIZE) {
  314. #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  315. ByteReverseWords(md5->buffer, md5->buffer, WC_MD5_BLOCK_SIZE);
  316. #endif
  317. ret = XTRANSFORM(md5, (const byte*)local);
  318. if (ret != 0)
  319. return ret;
  320. md5->buffLen = 0;
  321. }
  322. }
  323. /* process blocks */
  324. #ifdef XTRANSFORM_LEN
  325. /* get number of blocks */
  326. /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
  327. /* len (masked by 0xFFFFFFC0) returns block aligned length */
  328. blocksLen = len & ~(WC_MD5_BLOCK_SIZE-1);
  329. if (blocksLen > 0) {
  330. /* Byte reversal performed in function if required. */
  331. XTRANSFORM_LEN(md5, data, blocksLen);
  332. data += blocksLen;
  333. len -= blocksLen;
  334. }
  335. #else
  336. while (len >= WC_MD5_BLOCK_SIZE) {
  337. word32* local32 = md5->buffer;
  338. /* optimization to avoid memcpy if data pointer is properly aligned */
  339. /* Big Endian requires byte swap, so can't use data directly */
  340. #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(BIG_ENDIAN_ORDER)
  341. if (((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
  342. local32 = (word32*)data;
  343. }
  344. else
  345. #endif
  346. {
  347. XMEMCPY(local32, data, WC_MD5_BLOCK_SIZE);
  348. }
  349. data += WC_MD5_BLOCK_SIZE;
  350. len -= WC_MD5_BLOCK_SIZE;
  351. #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  352. ByteReverseWords(local32, local32, WC_MD5_BLOCK_SIZE);
  353. #endif
  354. ret = XTRANSFORM(md5, (const byte*)local32);
  355. }
  356. #endif /* XTRANSFORM_LEN */
  357. /* save remainder */
  358. if (len > 0) {
  359. XMEMCPY(local, data, len);
  360. md5->buffLen = len;
  361. }
  362. return ret;
  363. }
  364. int wc_Md5Final(wc_Md5* md5, byte* hash)
  365. {
  366. byte* local;
  367. if (md5 == NULL || hash == NULL) {
  368. return BAD_FUNC_ARG;
  369. }
  370. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
  371. if (md5->asyncDev.marker == WOLFSSL_ASYNC_MARKER_MD5) {
  372. #if defined(HAVE_INTEL_QA)
  373. return IntelQaSymMd5(&md5->asyncDev, hash, NULL, WC_MD5_DIGEST_SIZE);
  374. #endif
  375. }
  376. #endif /* WOLFSSL_ASYNC_CRYPT */
  377. local = (byte*)md5->buffer;
  378. local[md5->buffLen++] = 0x80; /* add 1 */
  379. /* pad with zeros */
  380. if (md5->buffLen > WC_MD5_PAD_SIZE) {
  381. XMEMSET(&local[md5->buffLen], 0, WC_MD5_BLOCK_SIZE - md5->buffLen);
  382. md5->buffLen += WC_MD5_BLOCK_SIZE - md5->buffLen;
  383. #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  384. ByteReverseWords(md5->buffer, md5->buffer, WC_MD5_BLOCK_SIZE);
  385. #endif
  386. XTRANSFORM(md5, local);
  387. md5->buffLen = 0;
  388. }
  389. XMEMSET(&local[md5->buffLen], 0, WC_MD5_PAD_SIZE - md5->buffLen);
  390. #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
  391. ByteReverseWords(md5->buffer, md5->buffer, WC_MD5_BLOCK_SIZE);
  392. #endif
  393. /* put lengths in bits */
  394. md5->hiLen = (md5->loLen >> (8 * sizeof(md5->loLen) - 3)) +
  395. (md5->hiLen << 3);
  396. md5->loLen = md5->loLen << 3;
  397. /* store lengths */
  398. /* ! length ordering dependent on digest endian type ! */
  399. XMEMCPY(&local[WC_MD5_PAD_SIZE], &md5->loLen, sizeof(word32));
  400. XMEMCPY(&local[WC_MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32));
  401. /* final transform and result to hash */
  402. XTRANSFORM(md5, local);
  403. #ifdef BIG_ENDIAN_ORDER
  404. ByteReverseWords(md5->digest, md5->digest, WC_MD5_DIGEST_SIZE);
  405. #endif
  406. XMEMCPY(hash, md5->digest, WC_MD5_DIGEST_SIZE);
  407. return _InitMd5(md5); /* reset state */
  408. }
  409. #endif /* !HAVE_MD5_CUST_API */
  410. int wc_InitMd5(wc_Md5* md5)
  411. {
  412. if (md5 == NULL) {
  413. return BAD_FUNC_ARG;
  414. }
  415. return wc_InitMd5_ex(md5, NULL, INVALID_DEVID);
  416. }
  417. void wc_Md5Free(wc_Md5* md5)
  418. {
  419. if (md5 == NULL)
  420. return;
  421. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
  422. wolfAsync_DevCtxFree(&md5->asyncDev, WOLFSSL_ASYNC_MARKER_MD5);
  423. #endif /* WOLFSSL_ASYNC_CRYPT */
  424. #ifdef WOLFSSL_PIC32MZ_HASH
  425. wc_Md5Pic32Free(md5);
  426. #endif
  427. }
  428. int wc_Md5GetHash(wc_Md5* md5, byte* hash)
  429. {
  430. int ret;
  431. wc_Md5 tmpMd5;
  432. if (md5 == NULL || hash == NULL)
  433. return BAD_FUNC_ARG;
  434. ret = wc_Md5Copy(md5, &tmpMd5);
  435. if (ret == 0) {
  436. ret = wc_Md5Final(&tmpMd5, hash);
  437. }
  438. return ret;
  439. }
  440. int wc_Md5Copy(wc_Md5* src, wc_Md5* dst)
  441. {
  442. int ret = 0;
  443. if (src == NULL || dst == NULL)
  444. return BAD_FUNC_ARG;
  445. XMEMCPY(dst, src, sizeof(wc_Md5));
  446. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
  447. ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
  448. #endif
  449. #ifdef WOLFSSL_PIC32MZ_HASH
  450. ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
  451. #endif
  452. #ifdef WOLFSSL_HASH_FLAGS
  453. dst->flags |= WC_HASH_FLAG_ISCOPY;
  454. #endif
  455. return ret;
  456. }
  457. #ifdef OPENSSL_EXTRA
  458. /* Apply MD5 transformation to the data */
  459. /* @param md5 a pointer to wc_MD5 structure */
  460. /* @param data data to be applied MD5 transformation */
  461. /* @return 0 on successful, otherwise non-zero on failure */
  462. int wc_Md5Transform(wc_Md5* md5, const byte* data)
  463. {
  464. /* sanity check */
  465. if (md5 == NULL || data == NULL) {
  466. return BAD_FUNC_ARG;
  467. }
  468. #ifndef HAVE_MD5_CUST_API
  469. return Transform(md5, data);
  470. #else
  471. return NOT_COMPILED_IN;
  472. #endif
  473. }
  474. #endif /* OPENSSL_EXTRA */
  475. #ifdef WOLFSSL_HASH_FLAGS
  476. int wc_Md5SetFlags(wc_Md5* md5, word32 flags)
  477. {
  478. if (md5) {
  479. md5->flags = flags;
  480. }
  481. return 0;
  482. }
  483. int wc_Md5GetFlags(wc_Md5* md5, word32* flags)
  484. {
  485. if (md5 && flags) {
  486. *flags = md5->flags;
  487. }
  488. return 0;
  489. }
  490. #endif
  491. #endif /* WOLFSSL_TI_HASH */
  492. #endif /* NO_MD5 */