Coverage Report

Created: 2025-10-10 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/curl/lib/md4.c
Line
Count
Source
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
25
#include "curl_setup.h"
26
27
#ifdef USE_CURL_NTLM_CORE
28
29
#include <string.h>
30
31
#include "strdup.h"
32
#include "curl_md4.h"
33
#include "curlx/warnless.h"
34
35
#ifdef USE_OPENSSL
36
#include <openssl/opensslv.h>
37
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(USE_AMISSL)
38
/* OpenSSL 3.0.0 marks the MD4 functions as deprecated */
39
#define OPENSSL_NO_MD4
40
#else
41
/* Cover also OPENSSL_NO_MD4 configured in openssl */
42
#include <openssl/opensslconf.h>
43
#endif
44
#endif /* USE_OPENSSL */
45
46
#ifdef USE_WOLFSSL
47
#include <wolfssl/options.h>
48
#define VOID_MD4_INIT
49
#ifdef NO_MD4
50
#define WOLFSSL_NO_MD4
51
#endif
52
#endif
53
54
#ifdef USE_MBEDTLS
55
#include <mbedtls/version.h>
56
#if MBEDTLS_VERSION_NUMBER < 0x03020000
57
  #error "mbedTLS 3.2.0 or later required"
58
#endif
59
#include <mbedtls/mbedtls_config.h>
60
#endif /* USE_MBEDTLS */
61
62
/* When OpenSSL or wolfSSL is available, we use their MD4 functions. */
63
#if defined(USE_WOLFSSL) && !defined(WOLFSSL_NO_MD4)
64
#include <wolfssl/openssl/md4.h>
65
#elif defined(USE_OPENSSL) && !defined(OPENSSL_NO_MD4)
66
#include <openssl/md4.h>
67
#elif (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && \
68
              (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1040) && \
69
       defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
70
              (__MAC_OS_X_VERSION_MIN_REQUIRED < 101500)) || \
71
      (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \
72
              (__IPHONE_OS_VERSION_MAX_ALLOWED >= 20000) && \
73
       defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
74
              (__IPHONE_OS_VERSION_MIN_REQUIRED < 130000))
75
#define AN_APPLE_OS
76
#include <CommonCrypto/CommonDigest.h>
77
#elif defined(USE_WIN32_CRYPTO)
78
#include <wincrypt.h>
79
#elif defined(USE_GNUTLS)
80
#include <nettle/md4.h>
81
#elif(defined(USE_MBEDTLS) && defined(MBEDTLS_MD4_C))
82
#include <mbedtls/md4.h>
83
#endif
84
85
/* The last 2 #include files should be in this order */
86
#include "curl_memory.h"
87
#include "memdebug.h"
88
89
90
#if defined(USE_WOLFSSL) && !defined(WOLFSSL_NO_MD4)
91
92
#ifdef OPENSSL_COEXIST
93
  #define MD4_CTX WOLFSSL_MD4_CTX
94
  #define MD4_Init wolfSSL_MD4_Init
95
  #define MD4_Update wolfSSL_MD4_Update
96
  #define MD4_Final wolfSSL_MD4_Final
97
#endif
98
99
#elif defined(USE_OPENSSL) && !defined(OPENSSL_NO_MD4)
100
101
#elif defined(AN_APPLE_OS)
102
typedef CC_MD4_CTX MD4_CTX;
103
104
static int MD4_Init(MD4_CTX *ctx)
105
{
106
  return CC_MD4_Init(ctx);
107
}
108
109
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
110
{
111
  (void)CC_MD4_Update(ctx, data, (CC_LONG)size);
112
}
113
114
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
115
{
116
  (void)CC_MD4_Final(result, ctx);
117
}
118
119
#elif defined(USE_WIN32_CRYPTO)
120
121
struct md4_ctx {
122
  HCRYPTPROV hCryptProv;
123
  HCRYPTHASH hHash;
124
};
125
typedef struct md4_ctx MD4_CTX;
126
127
static int MD4_Init(MD4_CTX *ctx)
128
{
129
  ctx->hCryptProv = 0;
130
  ctx->hHash = 0;
131
132
  if(!CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, PROV_RSA_FULL,
133
                          CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
134
    return 0;
135
136
  if(!CryptCreateHash(ctx->hCryptProv, CALG_MD4, 0, 0, &ctx->hHash)) {
137
    CryptReleaseContext(ctx->hCryptProv, 0);
138
    ctx->hCryptProv = 0;
139
    return 0;
140
  }
141
142
  return 1;
143
}
144
145
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
146
{
147
#ifdef __MINGW32CE__
148
  CryptHashData(ctx->hHash, (BYTE *)CURL_UNCONST(data),
149
                (unsigned int) size, 0);
150
#else
151
  CryptHashData(ctx->hHash, (const BYTE *)data, (unsigned int) size, 0);
152
#endif
153
}
154
155
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
156
{
157
  unsigned long length = 0;
158
159
  CryptGetHashParam(ctx->hHash, HP_HASHVAL, NULL, &length, 0);
160
  if(length == MD4_DIGEST_LENGTH)
161
    CryptGetHashParam(ctx->hHash, HP_HASHVAL, result, &length, 0);
162
163
  if(ctx->hHash)
164
    CryptDestroyHash(ctx->hHash);
165
166
  if(ctx->hCryptProv)
167
    CryptReleaseContext(ctx->hCryptProv, 0);
168
}
169
170
#elif defined(USE_GNUTLS)
171
172
typedef struct md4_ctx MD4_CTX;
173
174
static int MD4_Init(MD4_CTX *ctx)
175
{
176
  md4_init(ctx);
177
  return 1;
178
}
179
180
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
181
{
182
  md4_update(ctx, size, data);
183
}
184
185
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
186
{
187
  md4_digest(ctx, MD4_DIGEST_SIZE, result);
188
}
189
190
#elif(defined(USE_MBEDTLS) && defined(MBEDTLS_MD4_C))
191
192
struct md4_ctx {
193
  void *data;
194
  unsigned long size;
195
};
196
typedef struct md4_ctx MD4_CTX;
197
198
static int MD4_Init(MD4_CTX *ctx)
199
{
200
  ctx->data = NULL;
201
  ctx->size = 0;
202
  return 1;
203
}
204
205
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
206
{
207
  if(!ctx->data) {
208
    ctx->data = Curl_memdup(data, size);
209
    if(ctx->data)
210
      ctx->size = size;
211
  }
212
}
213
214
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
215
{
216
  if(ctx->data) {
217
    mbedtls_md4(ctx->data, ctx->size, result);
218
    Curl_safefree(ctx->data);
219
    ctx->size = 0;
220
  }
221
}
222
223
#else
224
/* When no other crypto library is available, or the crypto library does not
225
 * support MD4, we use this code segment this implementation of it
226
 *
227
 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
228
 * MD4 Message-Digest Algorithm (RFC 1320).
229
 *
230
 * Homepage:
231
 https://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
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. In case
238
 * this attempt to disclaim copyright and place the software in the public
239
 * domain is deemed null and void, then the software is Copyright (c) 2001
240
 * Alexander Peslyak and it is hereby released to the general public under the
241
 * 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
251
/* Any 32-bit or wider unsigned integer data type will do */
252
typedef unsigned int MD4_u32plus;
253
254
struct md4_ctx {
255
  MD4_u32plus lo, hi;
256
  MD4_u32plus a, b, c, d;
257
  unsigned char buffer[64];
258
  MD4_u32plus block[16];
259
};
260
typedef struct md4_ctx MD4_CTX;
261
262
static int MD4_Init(MD4_CTX *ctx);
263
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size);
264
static void MD4_Final(unsigned char *result, MD4_CTX *ctx);
265
266
/*
267
 * The basic MD4 functions.
268
 *
269
 * F and G are optimized compared to their RFC 1320 definitions, with the
270
 * optimization for F borrowed from Colin Plumb's MD5 implementation.
271
 */
272
#define MD4_F(x, y, z)                  ((z) ^ ((x) & ((y) ^ (z))))
273
#define MD4_G(x, y, z)                  (((x) & ((y) | (z))) | ((y) & (z)))
274
#define MD4_H(x, y, z)                  ((x) ^ (y) ^ (z))
275
276
/*
277
 * The MD4 transformation for all three rounds.
278
 */
279
#define MD4_STEP(f, a, b, c, d, x, s) \
280
        (a) += f((b), (c), (d)) + (x); \
281
        (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));
282
283
/*
284
 * SET reads 4 input bytes in little-endian byte order and stores them
285
 * in a properly aligned word in host byte order.
286
 *
287
 * The check for little-endian architectures that tolerate unaligned
288
 * memory accesses is just an optimization. Nothing will break if it
289
 * does not work.
290
 */
291
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
292
#define MD4_SET(n) \
293
        (*(const MD4_u32plus *)(const void *)&ptr[(n) * 4])
294
#define MD4_GET(n) \
295
        MD4_SET(n)
296
#else
297
#define MD4_SET(n) \
298
        (ctx->block[(n)] = \
299
          (MD4_u32plus)ptr[(n) * 4] | \
300
          ((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \
301
          ((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \
302
          ((MD4_u32plus)ptr[(n) * 4 + 3] << 24))
303
#define MD4_GET(n) \
304
        (ctx->block[(n)])
305
#endif
306
307
/*
308
 * This processes one or more 64-byte data blocks, but does NOT update
309
 * the bit counters. There are no alignment requirements.
310
 */
311
static const void *my_md4_body(MD4_CTX *ctx,
312
                               const void *data, unsigned long size)
313
{
314
  const unsigned char *ptr;
315
  MD4_u32plus a, b, c, d;
316
317
  ptr = (const unsigned char *)data;
318
319
  a = ctx->a;
320
  b = ctx->b;
321
  c = ctx->c;
322
  d = ctx->d;
323
324
  do {
325
    MD4_u32plus saved_a, saved_b, saved_c, saved_d;
326
327
    saved_a = a;
328
    saved_b = b;
329
    saved_c = c;
330
    saved_d = d;
331
332
    /* Round 1 */
333
    MD4_STEP(MD4_F, a, b, c, d, MD4_SET(0), 3)
334
    MD4_STEP(MD4_F, d, a, b, c, MD4_SET(1), 7)
335
    MD4_STEP(MD4_F, c, d, a, b, MD4_SET(2), 11)
336
    MD4_STEP(MD4_F, b, c, d, a, MD4_SET(3), 19)
337
    MD4_STEP(MD4_F, a, b, c, d, MD4_SET(4), 3)
338
    MD4_STEP(MD4_F, d, a, b, c, MD4_SET(5), 7)
339
    MD4_STEP(MD4_F, c, d, a, b, MD4_SET(6), 11)
340
    MD4_STEP(MD4_F, b, c, d, a, MD4_SET(7), 19)
341
    MD4_STEP(MD4_F, a, b, c, d, MD4_SET(8), 3)
342
    MD4_STEP(MD4_F, d, a, b, c, MD4_SET(9), 7)
343
    MD4_STEP(MD4_F, c, d, a, b, MD4_SET(10), 11)
344
    MD4_STEP(MD4_F, b, c, d, a, MD4_SET(11), 19)
345
    MD4_STEP(MD4_F, a, b, c, d, MD4_SET(12), 3)
346
    MD4_STEP(MD4_F, d, a, b, c, MD4_SET(13), 7)
347
    MD4_STEP(MD4_F, c, d, a, b, MD4_SET(14), 11)
348
    MD4_STEP(MD4_F, b, c, d, a, MD4_SET(15), 19)
349
350
    /* Round 2 */
351
    MD4_STEP(MD4_G, a, b, c, d, MD4_GET(0) + 0x5a827999, 3)
352
    MD4_STEP(MD4_G, d, a, b, c, MD4_GET(4) + 0x5a827999, 5)
353
    MD4_STEP(MD4_G, c, d, a, b, MD4_GET(8) + 0x5a827999, 9)
354
    MD4_STEP(MD4_G, b, c, d, a, MD4_GET(12) + 0x5a827999, 13)
355
    MD4_STEP(MD4_G, a, b, c, d, MD4_GET(1) + 0x5a827999, 3)
356
    MD4_STEP(MD4_G, d, a, b, c, MD4_GET(5) + 0x5a827999, 5)
357
    MD4_STEP(MD4_G, c, d, a, b, MD4_GET(9) + 0x5a827999, 9)
358
    MD4_STEP(MD4_G, b, c, d, a, MD4_GET(13) + 0x5a827999, 13)
359
    MD4_STEP(MD4_G, a, b, c, d, MD4_GET(2) + 0x5a827999, 3)
360
    MD4_STEP(MD4_G, d, a, b, c, MD4_GET(6) + 0x5a827999, 5)
361
    MD4_STEP(MD4_G, c, d, a, b, MD4_GET(10) + 0x5a827999, 9)
362
    MD4_STEP(MD4_G, b, c, d, a, MD4_GET(14) + 0x5a827999, 13)
363
    MD4_STEP(MD4_G, a, b, c, d, MD4_GET(3) + 0x5a827999, 3)
364
    MD4_STEP(MD4_G, d, a, b, c, MD4_GET(7) + 0x5a827999, 5)
365
    MD4_STEP(MD4_G, c, d, a, b, MD4_GET(11) + 0x5a827999, 9)
366
    MD4_STEP(MD4_G, b, c, d, a, MD4_GET(15) + 0x5a827999, 13)
367
368
    /* Round 3 */
369
    MD4_STEP(MD4_H, a, b, c, d, MD4_GET(0) + 0x6ed9eba1, 3)
370
    MD4_STEP(MD4_H, d, a, b, c, MD4_GET(8) + 0x6ed9eba1, 9)
371
    MD4_STEP(MD4_H, c, d, a, b, MD4_GET(4) + 0x6ed9eba1, 11)
372
    MD4_STEP(MD4_H, b, c, d, a, MD4_GET(12) + 0x6ed9eba1, 15)
373
    MD4_STEP(MD4_H, a, b, c, d, MD4_GET(2) + 0x6ed9eba1, 3)
374
    MD4_STEP(MD4_H, d, a, b, c, MD4_GET(10) + 0x6ed9eba1, 9)
375
    MD4_STEP(MD4_H, c, d, a, b, MD4_GET(6) + 0x6ed9eba1, 11)
376
    MD4_STEP(MD4_H, b, c, d, a, MD4_GET(14) + 0x6ed9eba1, 15)
377
    MD4_STEP(MD4_H, a, b, c, d, MD4_GET(1) + 0x6ed9eba1, 3)
378
    MD4_STEP(MD4_H, d, a, b, c, MD4_GET(9) + 0x6ed9eba1, 9)
379
    MD4_STEP(MD4_H, c, d, a, b, MD4_GET(5) + 0x6ed9eba1, 11)
380
    MD4_STEP(MD4_H, b, c, d, a, MD4_GET(13) + 0x6ed9eba1, 15)
381
    MD4_STEP(MD4_H, a, b, c, d, MD4_GET(3) + 0x6ed9eba1, 3)
382
    MD4_STEP(MD4_H, d, a, b, c, MD4_GET(11) + 0x6ed9eba1, 9)
383
    MD4_STEP(MD4_H, c, d, a, b, MD4_GET(7) + 0x6ed9eba1, 11)
384
    MD4_STEP(MD4_H, b, c, d, a, MD4_GET(15) + 0x6ed9eba1, 15)
385
386
    a += saved_a;
387
    b += saved_b;
388
    c += saved_c;
389
    d += saved_d;
390
391
    ptr += 64;
392
  } while(size -= 64);
393
394
  ctx->a = a;
395
  ctx->b = b;
396
  ctx->c = c;
397
  ctx->d = d;
398
399
  return ptr;
400
}
401
402
static int MD4_Init(MD4_CTX *ctx)
403
{
404
  ctx->a = 0x67452301;
405
  ctx->b = 0xefcdab89;
406
  ctx->c = 0x98badcfe;
407
  ctx->d = 0x10325476;
408
409
  ctx->lo = 0;
410
  ctx->hi = 0;
411
  return 1;
412
}
413
414
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
415
{
416
  MD4_u32plus saved_lo;
417
  unsigned long used;
418
419
  saved_lo = ctx->lo;
420
  ctx->lo = (saved_lo + size) & 0x1fffffff;
421
  if(ctx->lo < saved_lo)
422
    ctx->hi++;
423
  ctx->hi += (MD4_u32plus)size >> 29;
424
425
  used = saved_lo & 0x3f;
426
427
  if(used) {
428
    unsigned long available = 64 - used;
429
430
    if(size < available) {
431
      memcpy(&ctx->buffer[used], data, size);
432
      return;
433
    }
434
435
    memcpy(&ctx->buffer[used], data, available);
436
    data = (const unsigned char *)data + available;
437
    size -= available;
438
    my_md4_body(ctx, ctx->buffer, 64);
439
  }
440
441
  if(size >= 64) {
442
    data = my_md4_body(ctx, data, size & ~(unsigned long)0x3f);
443
    size &= 0x3f;
444
  }
445
446
  memcpy(ctx->buffer, data, size);
447
}
448
449
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
450
{
451
  unsigned long used, available;
452
453
  used = ctx->lo & 0x3f;
454
455
  ctx->buffer[used++] = 0x80;
456
457
  available = 64 - used;
458
459
  if(available < 8) {
460
    memset(&ctx->buffer[used], 0, available);
461
    my_md4_body(ctx, ctx->buffer, 64);
462
    used = 0;
463
    available = 64;
464
  }
465
466
  memset(&ctx->buffer[used], 0, available - 8);
467
468
  ctx->lo <<= 3;
469
  ctx->buffer[56] = curlx_ultouc((ctx->lo)&0xff);
470
  ctx->buffer[57] = curlx_ultouc((ctx->lo >> 8)&0xff);
471
  ctx->buffer[58] = curlx_ultouc((ctx->lo >> 16)&0xff);
472
  ctx->buffer[59] = curlx_ultouc((ctx->lo >> 24)&0xff);
473
  ctx->buffer[60] = curlx_ultouc((ctx->hi)&0xff);
474
  ctx->buffer[61] = curlx_ultouc((ctx->hi >> 8)&0xff);
475
  ctx->buffer[62] = curlx_ultouc((ctx->hi >> 16)&0xff);
476
  ctx->buffer[63] = curlx_ultouc(ctx->hi >> 24);
477
478
  my_md4_body(ctx, ctx->buffer, 64);
479
480
  result[0] = curlx_ultouc((ctx->a)&0xff);
481
  result[1] = curlx_ultouc((ctx->a >> 8)&0xff);
482
  result[2] = curlx_ultouc((ctx->a >> 16)&0xff);
483
  result[3] = curlx_ultouc(ctx->a >> 24);
484
  result[4] = curlx_ultouc((ctx->b)&0xff);
485
  result[5] = curlx_ultouc((ctx->b >> 8)&0xff);
486
  result[6] = curlx_ultouc((ctx->b >> 16)&0xff);
487
  result[7] = curlx_ultouc(ctx->b >> 24);
488
  result[8] = curlx_ultouc((ctx->c)&0xff);
489
  result[9] = curlx_ultouc((ctx->c >> 8)&0xff);
490
  result[10] = curlx_ultouc((ctx->c >> 16)&0xff);
491
  result[11] = curlx_ultouc(ctx->c >> 24);
492
  result[12] = curlx_ultouc((ctx->d)&0xff);
493
  result[13] = curlx_ultouc((ctx->d >> 8)&0xff);
494
  result[14] = curlx_ultouc((ctx->d >> 16)&0xff);
495
  result[15] = curlx_ultouc(ctx->d >> 24);
496
497
  memset(ctx, 0, sizeof(*ctx));
498
}
499
500
#endif /* CRYPTO LIBS */
501
502
CURLcode Curl_md4it(unsigned char *output, const unsigned char *input,
503
                    const size_t len)
504
0
{
505
0
  MD4_CTX ctx;
506
507
#ifdef VOID_MD4_INIT
508
  MD4_Init(&ctx);
509
#else
510
0
  if(!MD4_Init(&ctx))
511
0
    return CURLE_FAILED_INIT;
512
0
#endif
513
514
0
  MD4_Update(&ctx, input, curlx_uztoui(len));
515
0
  MD4_Final(output, &ctx);
516
0
  return CURLE_OK;
517
0
}
518
519
#endif /* USE_CURL_NTLM_CORE */