md5.c 20 KB

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