md5.c 19 KB

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