md5.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifndef CURL_DISABLE_CRYPTO_AUTH
  24. #include <curl/curl.h>
  25. #include "curl_md5.h"
  26. #include "curl_hmac.h"
  27. #include "warnless.h"
  28. #ifdef USE_MBEDTLS
  29. #include <mbedtls/version.h>
  30. #if(MBEDTLS_VERSION_NUMBER >= 0x02070000) && \
  31. (MBEDTLS_VERSION_NUMBER < 0x03000000)
  32. #define HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS
  33. #endif
  34. #endif /* USE_MBEDTLS */
  35. #if defined(USE_GNUTLS)
  36. #include <nettle/md5.h>
  37. #include "curl_memory.h"
  38. /* The last #include file should be: */
  39. #include "memdebug.h"
  40. typedef struct md5_ctx MD5_CTX;
  41. static void MD5_Init(MD5_CTX *ctx)
  42. {
  43. md5_init(ctx);
  44. }
  45. static void MD5_Update(MD5_CTX *ctx,
  46. const unsigned char *input,
  47. unsigned int inputLen)
  48. {
  49. md5_update(ctx, inputLen, input);
  50. }
  51. static void MD5_Final(unsigned char *digest, MD5_CTX *ctx)
  52. {
  53. md5_digest(ctx, 16, digest);
  54. }
  55. #elif defined(USE_OPENSSL) && !defined(USE_AMISSL)
  56. /* When OpenSSL is available we use the MD5-function from OpenSSL */
  57. #include <openssl/md5.h>
  58. #include "curl_memory.h"
  59. /* The last #include file should be: */
  60. #include "memdebug.h"
  61. #elif defined(USE_MBEDTLS)
  62. #include <mbedtls/md5.h>
  63. #include "curl_memory.h"
  64. /* The last #include file should be: */
  65. #include "memdebug.h"
  66. typedef mbedtls_md5_context MD5_CTX;
  67. static void MD5_Init(MD5_CTX *ctx)
  68. {
  69. #if !defined(HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS)
  70. (void) mbedtls_md5_starts(ctx);
  71. #else
  72. (void) mbedtls_md5_starts_ret(ctx);
  73. #endif
  74. }
  75. static void MD5_Update(MD5_CTX *ctx,
  76. const unsigned char *data,
  77. unsigned int length)
  78. {
  79. #if !defined(HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS)
  80. (void) mbedtls_md5_update(ctx, data, length);
  81. #else
  82. (void) mbedtls_md5_update_ret(ctx, data, length);
  83. #endif
  84. }
  85. static void MD5_Final(unsigned char *digest, MD5_CTX *ctx)
  86. {
  87. #if !defined(HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS)
  88. (void) mbedtls_md5_finish(ctx, digest);
  89. #else
  90. (void) mbedtls_md5_finish_ret(ctx, digest);
  91. #endif
  92. }
  93. #elif (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && \
  94. (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1040) && \
  95. defined(__MAC_OS_X_VERSION_MIN_ALLOWED) && \
  96. (__MAC_OS_X_VERSION_MIN_ALLOWED < 101500)) || \
  97. (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \
  98. (__IPHONE_OS_VERSION_MAX_ALLOWED >= 20000))
  99. /* For Apple operating systems: CommonCrypto has the functions we need.
  100. These functions are available on Tiger and later, as well as iOS 2.0
  101. and later. If you're building for an older cat, well, sorry.
  102. Declaring the functions as static like this seems to be a bit more
  103. reliable than defining COMMON_DIGEST_FOR_OPENSSL on older cats. */
  104. # include <CommonCrypto/CommonDigest.h>
  105. # define MD5_CTX CC_MD5_CTX
  106. #include "curl_memory.h"
  107. /* The last #include file should be: */
  108. #include "memdebug.h"
  109. static void MD5_Init(MD5_CTX *ctx)
  110. {
  111. CC_MD5_Init(ctx);
  112. }
  113. static void MD5_Update(MD5_CTX *ctx,
  114. const unsigned char *input,
  115. unsigned int inputLen)
  116. {
  117. CC_MD5_Update(ctx, input, inputLen);
  118. }
  119. static void MD5_Final(unsigned char *digest, MD5_CTX *ctx)
  120. {
  121. CC_MD5_Final(digest, ctx);
  122. }
  123. #elif defined(USE_WIN32_CRYPTO)
  124. #include <wincrypt.h>
  125. #include "curl_memory.h"
  126. /* The last #include file should be: */
  127. #include "memdebug.h"
  128. struct md5_ctx {
  129. HCRYPTPROV hCryptProv;
  130. HCRYPTHASH hHash;
  131. };
  132. typedef struct md5_ctx MD5_CTX;
  133. static void MD5_Init(MD5_CTX *ctx)
  134. {
  135. if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, PROV_RSA_FULL,
  136. CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  137. CryptCreateHash(ctx->hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
  138. }
  139. }
  140. static void MD5_Update(MD5_CTX *ctx,
  141. const unsigned char *input,
  142. unsigned int inputLen)
  143. {
  144. CryptHashData(ctx->hHash, (unsigned char *)input, inputLen, 0);
  145. }
  146. static void MD5_Final(unsigned char *digest, MD5_CTX *ctx)
  147. {
  148. unsigned long length = 0;
  149. CryptGetHashParam(ctx->hHash, HP_HASHVAL, NULL, &length, 0);
  150. if(length == 16)
  151. CryptGetHashParam(ctx->hHash, HP_HASHVAL, digest, &length, 0);
  152. if(ctx->hHash)
  153. CryptDestroyHash(ctx->hHash);
  154. if(ctx->hCryptProv)
  155. CryptReleaseContext(ctx->hCryptProv, 0);
  156. }
  157. #else
  158. /* When no other crypto library is available we use this code segment */
  159. /*
  160. * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
  161. * MD5 Message-Digest Algorithm (RFC 1321).
  162. *
  163. * Homepage:
  164. https://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
  165. *
  166. * Author:
  167. * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
  168. *
  169. * This software was written by Alexander Peslyak in 2001. No copyright is
  170. * claimed, and the software is hereby placed in the public domain.
  171. * In case this attempt to disclaim copyright and place the software in the
  172. * public domain is deemed null and void, then the software is
  173. * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
  174. * general public under the following terms:
  175. *
  176. * Redistribution and use in source and binary forms, with or without
  177. * modification, are permitted.
  178. *
  179. * There's ABSOLUTELY NO WARRANTY, express or implied.
  180. *
  181. * (This is a heavily cut-down "BSD license".)
  182. *
  183. * This differs from Colin Plumb's older public domain implementation in that
  184. * no exactly 32-bit integer data type is required (any 32-bit or wider
  185. * unsigned integer data type will do), there's no compile-time endianness
  186. * configuration, and the function prototypes match OpenSSL's. No code from
  187. * Colin Plumb's implementation has been reused; this comment merely compares
  188. * the properties of the two independent implementations.
  189. *
  190. * The primary goals of this implementation are portability and ease of use.
  191. * It is meant to be fast, but not as fast as possible. Some known
  192. * optimizations are not included to reduce source code size and avoid
  193. * compile-time configuration.
  194. */
  195. #include <string.h>
  196. /* The last #include files should be: */
  197. #include "curl_memory.h"
  198. #include "memdebug.h"
  199. /* Any 32-bit or wider unsigned integer data type will do */
  200. typedef unsigned int MD5_u32plus;
  201. struct md5_ctx {
  202. MD5_u32plus lo, hi;
  203. MD5_u32plus a, b, c, d;
  204. unsigned char buffer[64];
  205. MD5_u32plus block[16];
  206. };
  207. typedef struct md5_ctx MD5_CTX;
  208. static void MD5_Init(MD5_CTX *ctx);
  209. static void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size);
  210. static void MD5_Final(unsigned char *result, MD5_CTX *ctx);
  211. /*
  212. * The basic MD5 functions.
  213. *
  214. * F and G are optimized compared to their RFC 1321 definitions for
  215. * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  216. * implementation.
  217. */
  218. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  219. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  220. #define H(x, y, z) (((x) ^ (y)) ^ (z))
  221. #define H2(x, y, z) ((x) ^ ((y) ^ (z)))
  222. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  223. /*
  224. * The MD5 transformation for all four rounds.
  225. */
  226. #define STEP(f, a, b, c, d, x, t, s) \
  227. (a) += f((b), (c), (d)) + (x) + (t); \
  228. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  229. (a) += (b);
  230. /*
  231. * SET reads 4 input bytes in little-endian byte order and stores them
  232. * in a properly aligned word in host byte order.
  233. *
  234. * The check for little-endian architectures that tolerate unaligned
  235. * memory accesses is just an optimization. Nothing will break if it
  236. * doesn't work.
  237. */
  238. #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
  239. #define SET(n) \
  240. (*(MD5_u32plus *)(void *)&ptr[(n) * 4])
  241. #define GET(n) \
  242. SET(n)
  243. #else
  244. #define SET(n) \
  245. (ctx->block[(n)] = \
  246. (MD5_u32plus)ptr[(n) * 4] | \
  247. ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
  248. ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
  249. ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
  250. #define GET(n) \
  251. (ctx->block[(n)])
  252. #endif
  253. /*
  254. * This processes one or more 64-byte data blocks, but does NOT update
  255. * the bit counters. There are no alignment requirements.
  256. */
  257. static const void *body(MD5_CTX *ctx, const void *data, unsigned long size)
  258. {
  259. const unsigned char *ptr;
  260. MD5_u32plus a, b, c, d;
  261. ptr = (const unsigned char *)data;
  262. a = ctx->a;
  263. b = ctx->b;
  264. c = ctx->c;
  265. d = ctx->d;
  266. do {
  267. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  268. saved_a = a;
  269. saved_b = b;
  270. saved_c = c;
  271. saved_d = d;
  272. /* Round 1 */
  273. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  274. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  275. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  276. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  277. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  278. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  279. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  280. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  281. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  282. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  283. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  284. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  285. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  286. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  287. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  288. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  289. /* Round 2 */
  290. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  291. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  292. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  293. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  294. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  295. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  296. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  297. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  298. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  299. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  300. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  301. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  302. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  303. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  304. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  305. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  306. /* Round 3 */
  307. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  308. STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11)
  309. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  310. STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23)
  311. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  312. STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  313. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  314. STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23)
  315. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  316. STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11)
  317. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  318. STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23)
  319. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  320. STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11)
  321. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  322. STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23)
  323. /* Round 4 */
  324. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  325. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  326. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  327. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  328. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  329. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  330. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  331. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  332. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  333. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  334. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  335. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  336. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  337. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  338. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  339. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  340. a += saved_a;
  341. b += saved_b;
  342. c += saved_c;
  343. d += saved_d;
  344. ptr += 64;
  345. } while(size -= 64);
  346. ctx->a = a;
  347. ctx->b = b;
  348. ctx->c = c;
  349. ctx->d = d;
  350. return ptr;
  351. }
  352. static void MD5_Init(MD5_CTX *ctx)
  353. {
  354. ctx->a = 0x67452301;
  355. ctx->b = 0xefcdab89;
  356. ctx->c = 0x98badcfe;
  357. ctx->d = 0x10325476;
  358. ctx->lo = 0;
  359. ctx->hi = 0;
  360. }
  361. static void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size)
  362. {
  363. MD5_u32plus saved_lo;
  364. unsigned long used;
  365. saved_lo = ctx->lo;
  366. ctx->lo = (saved_lo + size) & 0x1fffffff;
  367. if(ctx->lo < saved_lo)
  368. ctx->hi++;
  369. ctx->hi += (MD5_u32plus)size >> 29;
  370. used = saved_lo & 0x3f;
  371. if(used) {
  372. unsigned long available = 64 - used;
  373. if(size < available) {
  374. memcpy(&ctx->buffer[used], data, size);
  375. return;
  376. }
  377. memcpy(&ctx->buffer[used], data, available);
  378. data = (const unsigned char *)data + available;
  379. size -= available;
  380. body(ctx, ctx->buffer, 64);
  381. }
  382. if(size >= 64) {
  383. data = body(ctx, data, size & ~(unsigned long)0x3f);
  384. size &= 0x3f;
  385. }
  386. memcpy(ctx->buffer, data, size);
  387. }
  388. static void MD5_Final(unsigned char *result, MD5_CTX *ctx)
  389. {
  390. unsigned long used, available;
  391. used = ctx->lo & 0x3f;
  392. ctx->buffer[used++] = 0x80;
  393. available = 64 - used;
  394. if(available < 8) {
  395. memset(&ctx->buffer[used], 0, available);
  396. body(ctx, ctx->buffer, 64);
  397. used = 0;
  398. available = 64;
  399. }
  400. memset(&ctx->buffer[used], 0, available - 8);
  401. ctx->lo <<= 3;
  402. ctx->buffer[56] = curlx_ultouc((ctx->lo)&0xff);
  403. ctx->buffer[57] = curlx_ultouc((ctx->lo >> 8)&0xff);
  404. ctx->buffer[58] = curlx_ultouc((ctx->lo >> 16)&0xff);
  405. ctx->buffer[59] = curlx_ultouc(ctx->lo >> 24);
  406. ctx->buffer[60] = curlx_ultouc((ctx->hi)&0xff);
  407. ctx->buffer[61] = curlx_ultouc((ctx->hi >> 8)&0xff);
  408. ctx->buffer[62] = curlx_ultouc((ctx->hi >> 16)&0xff);
  409. ctx->buffer[63] = curlx_ultouc(ctx->hi >> 24);
  410. body(ctx, ctx->buffer, 64);
  411. result[0] = curlx_ultouc((ctx->a)&0xff);
  412. result[1] = curlx_ultouc((ctx->a >> 8)&0xff);
  413. result[2] = curlx_ultouc((ctx->a >> 16)&0xff);
  414. result[3] = curlx_ultouc(ctx->a >> 24);
  415. result[4] = curlx_ultouc((ctx->b)&0xff);
  416. result[5] = curlx_ultouc((ctx->b >> 8)&0xff);
  417. result[6] = curlx_ultouc((ctx->b >> 16)&0xff);
  418. result[7] = curlx_ultouc(ctx->b >> 24);
  419. result[8] = curlx_ultouc((ctx->c)&0xff);
  420. result[9] = curlx_ultouc((ctx->c >> 8)&0xff);
  421. result[10] = curlx_ultouc((ctx->c >> 16)&0xff);
  422. result[11] = curlx_ultouc(ctx->c >> 24);
  423. result[12] = curlx_ultouc((ctx->d)&0xff);
  424. result[13] = curlx_ultouc((ctx->d >> 8)&0xff);
  425. result[14] = curlx_ultouc((ctx->d >> 16)&0xff);
  426. result[15] = curlx_ultouc(ctx->d >> 24);
  427. memset(ctx, 0, sizeof(*ctx));
  428. }
  429. #endif /* CRYPTO LIBS */
  430. const struct HMAC_params Curl_HMAC_MD5[] = {
  431. {
  432. /* Hash initialization function. */
  433. CURLX_FUNCTION_CAST(HMAC_hinit_func, MD5_Init),
  434. /* Hash update function. */
  435. CURLX_FUNCTION_CAST(HMAC_hupdate_func, MD5_Update),
  436. /* Hash computation end function. */
  437. CURLX_FUNCTION_CAST(HMAC_hfinal_func, MD5_Final),
  438. /* Size of hash context structure. */
  439. sizeof(MD5_CTX),
  440. /* Maximum key length. */
  441. 64,
  442. /* Result size. */
  443. 16
  444. }
  445. };
  446. const struct MD5_params Curl_DIGEST_MD5[] = {
  447. {
  448. /* Digest initialization function */
  449. CURLX_FUNCTION_CAST(Curl_MD5_init_func, MD5_Init),
  450. /* Digest update function */
  451. CURLX_FUNCTION_CAST(Curl_MD5_update_func, MD5_Update),
  452. /* Digest computation end function */
  453. CURLX_FUNCTION_CAST(Curl_MD5_final_func, MD5_Final),
  454. /* Size of digest context struct */
  455. sizeof(MD5_CTX),
  456. /* Result size */
  457. 16
  458. }
  459. };
  460. /*
  461. * @unittest: 1601
  462. */
  463. void Curl_md5it(unsigned char *outbuffer, const unsigned char *input,
  464. const size_t len)
  465. {
  466. MD5_CTX ctx;
  467. MD5_Init(&ctx);
  468. MD5_Update(&ctx, input, curlx_uztoui(len));
  469. MD5_Final(outbuffer, &ctx);
  470. }
  471. struct MD5_context *Curl_MD5_init(const struct MD5_params *md5params)
  472. {
  473. struct MD5_context *ctxt;
  474. /* Create MD5 context */
  475. ctxt = malloc(sizeof(*ctxt));
  476. if(!ctxt)
  477. return ctxt;
  478. ctxt->md5_hashctx = malloc(md5params->md5_ctxtsize);
  479. if(!ctxt->md5_hashctx) {
  480. free(ctxt);
  481. return NULL;
  482. }
  483. ctxt->md5_hash = md5params;
  484. (*md5params->md5_init_func)(ctxt->md5_hashctx);
  485. return ctxt;
  486. }
  487. CURLcode Curl_MD5_update(struct MD5_context *context,
  488. const unsigned char *data,
  489. unsigned int len)
  490. {
  491. (*context->md5_hash->md5_update_func)(context->md5_hashctx, data, len);
  492. return CURLE_OK;
  493. }
  494. CURLcode Curl_MD5_final(struct MD5_context *context, unsigned char *result)
  495. {
  496. (*context->md5_hash->md5_final_func)(result, context->md5_hashctx);
  497. free(context->md5_hashctx);
  498. free(context);
  499. return CURLE_OK;
  500. }
  501. #endif /* CURL_DISABLE_CRYPTO_AUTH */