Coverage Report

Created: 2025-12-05 06:38

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