md5.c 18 KB

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