md5.c 17 KB

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