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