113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#if defined(USE_CURL_NTLM_CORE)
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#include <string.h>
3013498266Sopenharmony_ci
3113498266Sopenharmony_ci#include "curl_md4.h"
3213498266Sopenharmony_ci#include "warnless.h"
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci#ifdef USE_OPENSSL
3513498266Sopenharmony_ci#include <openssl/opensslv.h>
3613498266Sopenharmony_ci#if (OPENSSL_VERSION_NUMBER >= 0x30000000L) && !defined(USE_AMISSL)
3713498266Sopenharmony_ci/* OpenSSL 3.0.0 marks the MD4 functions as deprecated */
3813498266Sopenharmony_ci#define OPENSSL_NO_MD4
3913498266Sopenharmony_ci#endif
4013498266Sopenharmony_ci#endif /* USE_OPENSSL */
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci#ifdef USE_WOLFSSL
4313498266Sopenharmony_ci#include <wolfssl/options.h>
4413498266Sopenharmony_ci#define VOID_MD4_INIT
4513498266Sopenharmony_ci#ifdef NO_MD4
4613498266Sopenharmony_ci#define WOLFSSL_NO_MD4
4713498266Sopenharmony_ci#endif
4813498266Sopenharmony_ci#endif
4913498266Sopenharmony_ci
5013498266Sopenharmony_ci#ifdef USE_MBEDTLS
5113498266Sopenharmony_ci#include <mbedtls/version.h>
5213498266Sopenharmony_ci#if MBEDTLS_VERSION_NUMBER >= 0x03000000
5313498266Sopenharmony_ci#include <mbedtls/mbedtls_config.h>
5413498266Sopenharmony_ci#else
5513498266Sopenharmony_ci#include <mbedtls/config.h>
5613498266Sopenharmony_ci#endif
5713498266Sopenharmony_ci#if(MBEDTLS_VERSION_NUMBER >= 0x02070000)
5813498266Sopenharmony_ci  #define HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS
5913498266Sopenharmony_ci#endif
6013498266Sopenharmony_ci#endif /* USE_MBEDTLS */
6113498266Sopenharmony_ci
6213498266Sopenharmony_ci#if defined(USE_GNUTLS)
6313498266Sopenharmony_ci#include <nettle/md4.h>
6413498266Sopenharmony_ci/* When OpenSSL or wolfSSL is available, we use their MD4 functions. */
6513498266Sopenharmony_ci#elif defined(USE_WOLFSSL) && !defined(WOLFSSL_NO_MD4)
6613498266Sopenharmony_ci#include <wolfssl/openssl/md4.h>
6713498266Sopenharmony_ci#elif defined(USE_OPENSSL) && !defined(OPENSSL_NO_MD4)
6813498266Sopenharmony_ci#include <openssl/md4.h>
6913498266Sopenharmony_ci#elif (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && \
7013498266Sopenharmony_ci              (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1040) && \
7113498266Sopenharmony_ci       defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
7213498266Sopenharmony_ci              (__MAC_OS_X_VERSION_MIN_REQUIRED < 101500)) || \
7313498266Sopenharmony_ci      (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \
7413498266Sopenharmony_ci              (__IPHONE_OS_VERSION_MAX_ALLOWED >= 20000) && \
7513498266Sopenharmony_ci       defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
7613498266Sopenharmony_ci              (__IPHONE_OS_VERSION_MIN_REQUIRED < 130000))
7713498266Sopenharmony_ci#define AN_APPLE_OS
7813498266Sopenharmony_ci#include <CommonCrypto/CommonDigest.h>
7913498266Sopenharmony_ci#elif defined(USE_WIN32_CRYPTO)
8013498266Sopenharmony_ci#include <wincrypt.h>
8113498266Sopenharmony_ci#elif(defined(USE_MBEDTLS) && defined(MBEDTLS_MD4_C))
8213498266Sopenharmony_ci#include <mbedtls/md4.h>
8313498266Sopenharmony_ci#endif
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci/* The last 3 #include files should be in this order */
8613498266Sopenharmony_ci#include "curl_printf.h"
8713498266Sopenharmony_ci#include "curl_memory.h"
8813498266Sopenharmony_ci#include "memdebug.h"
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci
9113498266Sopenharmony_ci#if defined(USE_GNUTLS)
9213498266Sopenharmony_ci
9313498266Sopenharmony_citypedef struct md4_ctx MD4_CTX;
9413498266Sopenharmony_ci
9513498266Sopenharmony_cistatic int MD4_Init(MD4_CTX *ctx)
9613498266Sopenharmony_ci{
9713498266Sopenharmony_ci  md4_init(ctx);
9813498266Sopenharmony_ci  return 1;
9913498266Sopenharmony_ci}
10013498266Sopenharmony_ci
10113498266Sopenharmony_cistatic void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
10213498266Sopenharmony_ci{
10313498266Sopenharmony_ci  md4_update(ctx, size, data);
10413498266Sopenharmony_ci}
10513498266Sopenharmony_ci
10613498266Sopenharmony_cistatic void MD4_Final(unsigned char *result, MD4_CTX *ctx)
10713498266Sopenharmony_ci{
10813498266Sopenharmony_ci  md4_digest(ctx, MD4_DIGEST_SIZE, result);
10913498266Sopenharmony_ci}
11013498266Sopenharmony_ci
11113498266Sopenharmony_ci#elif defined(USE_WOLFSSL) && !defined(WOLFSSL_NO_MD4)
11213498266Sopenharmony_ci
11313498266Sopenharmony_ci#elif defined(USE_OPENSSL) && !defined(OPENSSL_NO_MD4)
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci#elif defined(AN_APPLE_OS)
11613498266Sopenharmony_citypedef CC_MD4_CTX MD4_CTX;
11713498266Sopenharmony_ci
11813498266Sopenharmony_cistatic int MD4_Init(MD4_CTX *ctx)
11913498266Sopenharmony_ci{
12013498266Sopenharmony_ci  return CC_MD4_Init(ctx);
12113498266Sopenharmony_ci}
12213498266Sopenharmony_ci
12313498266Sopenharmony_cistatic void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
12413498266Sopenharmony_ci{
12513498266Sopenharmony_ci  (void)CC_MD4_Update(ctx, data, (CC_LONG)size);
12613498266Sopenharmony_ci}
12713498266Sopenharmony_ci
12813498266Sopenharmony_cistatic void MD4_Final(unsigned char *result, MD4_CTX *ctx)
12913498266Sopenharmony_ci{
13013498266Sopenharmony_ci  (void)CC_MD4_Final(result, ctx);
13113498266Sopenharmony_ci}
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci#elif defined(USE_WIN32_CRYPTO)
13413498266Sopenharmony_ci
13513498266Sopenharmony_cistruct md4_ctx {
13613498266Sopenharmony_ci  HCRYPTPROV hCryptProv;
13713498266Sopenharmony_ci  HCRYPTHASH hHash;
13813498266Sopenharmony_ci};
13913498266Sopenharmony_citypedef struct md4_ctx MD4_CTX;
14013498266Sopenharmony_ci
14113498266Sopenharmony_cistatic int MD4_Init(MD4_CTX *ctx)
14213498266Sopenharmony_ci{
14313498266Sopenharmony_ci  ctx->hCryptProv = 0;
14413498266Sopenharmony_ci  ctx->hHash = 0;
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci  if(!CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, PROV_RSA_FULL,
14713498266Sopenharmony_ci                          CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
14813498266Sopenharmony_ci    return 0;
14913498266Sopenharmony_ci
15013498266Sopenharmony_ci  if(!CryptCreateHash(ctx->hCryptProv, CALG_MD4, 0, 0, &ctx->hHash)) {
15113498266Sopenharmony_ci    CryptReleaseContext(ctx->hCryptProv, 0);
15213498266Sopenharmony_ci    ctx->hCryptProv = 0;
15313498266Sopenharmony_ci    return 0;
15413498266Sopenharmony_ci  }
15513498266Sopenharmony_ci
15613498266Sopenharmony_ci  return 1;
15713498266Sopenharmony_ci}
15813498266Sopenharmony_ci
15913498266Sopenharmony_cistatic void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
16013498266Sopenharmony_ci{
16113498266Sopenharmony_ci  CryptHashData(ctx->hHash, (BYTE *)data, (unsigned int) size, 0);
16213498266Sopenharmony_ci}
16313498266Sopenharmony_ci
16413498266Sopenharmony_cistatic void MD4_Final(unsigned char *result, MD4_CTX *ctx)
16513498266Sopenharmony_ci{
16613498266Sopenharmony_ci  unsigned long length = 0;
16713498266Sopenharmony_ci
16813498266Sopenharmony_ci  CryptGetHashParam(ctx->hHash, HP_HASHVAL, NULL, &length, 0);
16913498266Sopenharmony_ci  if(length == MD4_DIGEST_LENGTH)
17013498266Sopenharmony_ci    CryptGetHashParam(ctx->hHash, HP_HASHVAL, result, &length, 0);
17113498266Sopenharmony_ci
17213498266Sopenharmony_ci  if(ctx->hHash)
17313498266Sopenharmony_ci    CryptDestroyHash(ctx->hHash);
17413498266Sopenharmony_ci
17513498266Sopenharmony_ci  if(ctx->hCryptProv)
17613498266Sopenharmony_ci    CryptReleaseContext(ctx->hCryptProv, 0);
17713498266Sopenharmony_ci}
17813498266Sopenharmony_ci
17913498266Sopenharmony_ci#elif(defined(USE_MBEDTLS) && defined(MBEDTLS_MD4_C))
18013498266Sopenharmony_ci
18113498266Sopenharmony_cistruct md4_ctx {
18213498266Sopenharmony_ci  void *data;
18313498266Sopenharmony_ci  unsigned long size;
18413498266Sopenharmony_ci};
18513498266Sopenharmony_citypedef struct md4_ctx MD4_CTX;
18613498266Sopenharmony_ci
18713498266Sopenharmony_cistatic int MD4_Init(MD4_CTX *ctx)
18813498266Sopenharmony_ci{
18913498266Sopenharmony_ci  ctx->data = NULL;
19013498266Sopenharmony_ci  ctx->size = 0;
19113498266Sopenharmony_ci  return 1;
19213498266Sopenharmony_ci}
19313498266Sopenharmony_ci
19413498266Sopenharmony_cistatic void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
19513498266Sopenharmony_ci{
19613498266Sopenharmony_ci  if(!ctx->data) {
19713498266Sopenharmony_ci    ctx->data = Curl_memdup(data, size);
19813498266Sopenharmony_ci    if(ctx->data)
19913498266Sopenharmony_ci      ctx->size = size;
20013498266Sopenharmony_ci  }
20113498266Sopenharmony_ci}
20213498266Sopenharmony_ci
20313498266Sopenharmony_cistatic void MD4_Final(unsigned char *result, MD4_CTX *ctx)
20413498266Sopenharmony_ci{
20513498266Sopenharmony_ci  if(ctx->data) {
20613498266Sopenharmony_ci#if !defined(HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS)
20713498266Sopenharmony_ci    mbedtls_md4(ctx->data, ctx->size, result);
20813498266Sopenharmony_ci#else
20913498266Sopenharmony_ci    (void) mbedtls_md4_ret(ctx->data, ctx->size, result);
21013498266Sopenharmony_ci#endif
21113498266Sopenharmony_ci
21213498266Sopenharmony_ci    Curl_safefree(ctx->data);
21313498266Sopenharmony_ci    ctx->size = 0;
21413498266Sopenharmony_ci  }
21513498266Sopenharmony_ci}
21613498266Sopenharmony_ci
21713498266Sopenharmony_ci#else
21813498266Sopenharmony_ci/* When no other crypto library is available, or the crypto library doesn't
21913498266Sopenharmony_ci * support MD4, we use this code segment this implementation of it
22013498266Sopenharmony_ci *
22113498266Sopenharmony_ci * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
22213498266Sopenharmony_ci * MD4 Message-Digest Algorithm (RFC 1320).
22313498266Sopenharmony_ci *
22413498266Sopenharmony_ci * Homepage:
22513498266Sopenharmony_ci https://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
22613498266Sopenharmony_ci *
22713498266Sopenharmony_ci * Author:
22813498266Sopenharmony_ci * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
22913498266Sopenharmony_ci *
23013498266Sopenharmony_ci * This software was written by Alexander Peslyak in 2001.  No copyright is
23113498266Sopenharmony_ci * claimed, and the software is hereby placed in the public domain.  In case
23213498266Sopenharmony_ci * this attempt to disclaim copyright and place the software in the public
23313498266Sopenharmony_ci * domain is deemed null and void, then the software is Copyright (c) 2001
23413498266Sopenharmony_ci * Alexander Peslyak and it is hereby released to the general public under the
23513498266Sopenharmony_ci * following terms:
23613498266Sopenharmony_ci *
23713498266Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
23813498266Sopenharmony_ci * modification, are permitted.
23913498266Sopenharmony_ci *
24013498266Sopenharmony_ci * There's ABSOLUTELY NO WARRANTY, express or implied.
24113498266Sopenharmony_ci *
24213498266Sopenharmony_ci * (This is a heavily cut-down "BSD license".)
24313498266Sopenharmony_ci *
24413498266Sopenharmony_ci * This differs from Colin Plumb's older public domain implementation in that
24513498266Sopenharmony_ci * no exactly 32-bit integer data type is required (any 32-bit or wider
24613498266Sopenharmony_ci * unsigned integer data type will do), there's no compile-time endianness
24713498266Sopenharmony_ci * configuration, and the function prototypes match OpenSSL's.  No code from
24813498266Sopenharmony_ci * Colin Plumb's implementation has been reused; this comment merely compares
24913498266Sopenharmony_ci * the properties of the two independent implementations.
25013498266Sopenharmony_ci *
25113498266Sopenharmony_ci * The primary goals of this implementation are portability and ease of use.
25213498266Sopenharmony_ci * It is meant to be fast, but not as fast as possible.  Some known
25313498266Sopenharmony_ci * optimizations are not included to reduce source code size and avoid
25413498266Sopenharmony_ci * compile-time configuration.
25513498266Sopenharmony_ci */
25613498266Sopenharmony_ci
25713498266Sopenharmony_ci/* Any 32-bit or wider unsigned integer data type will do */
25813498266Sopenharmony_citypedef unsigned int MD4_u32plus;
25913498266Sopenharmony_ci
26013498266Sopenharmony_cistruct md4_ctx {
26113498266Sopenharmony_ci  MD4_u32plus lo, hi;
26213498266Sopenharmony_ci  MD4_u32plus a, b, c, d;
26313498266Sopenharmony_ci  unsigned char buffer[64];
26413498266Sopenharmony_ci  MD4_u32plus block[16];
26513498266Sopenharmony_ci};
26613498266Sopenharmony_citypedef struct md4_ctx MD4_CTX;
26713498266Sopenharmony_ci
26813498266Sopenharmony_cistatic int MD4_Init(MD4_CTX *ctx);
26913498266Sopenharmony_cistatic void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size);
27013498266Sopenharmony_cistatic void MD4_Final(unsigned char *result, MD4_CTX *ctx);
27113498266Sopenharmony_ci
27213498266Sopenharmony_ci/*
27313498266Sopenharmony_ci * The basic MD4 functions.
27413498266Sopenharmony_ci *
27513498266Sopenharmony_ci * F and G are optimized compared to their RFC 1320 definitions, with the
27613498266Sopenharmony_ci * optimization for F borrowed from Colin Plumb's MD5 implementation.
27713498266Sopenharmony_ci */
27813498266Sopenharmony_ci#define F(x, y, z)                      ((z) ^ ((x) & ((y) ^ (z))))
27913498266Sopenharmony_ci#define G(x, y, z)                      (((x) & ((y) | (z))) | ((y) & (z)))
28013498266Sopenharmony_ci#define H(x, y, z)                      ((x) ^ (y) ^ (z))
28113498266Sopenharmony_ci
28213498266Sopenharmony_ci/*
28313498266Sopenharmony_ci * The MD4 transformation for all three rounds.
28413498266Sopenharmony_ci */
28513498266Sopenharmony_ci#define STEP(f, a, b, c, d, x, s) \
28613498266Sopenharmony_ci        (a) += f((b), (c), (d)) + (x); \
28713498266Sopenharmony_ci        (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));
28813498266Sopenharmony_ci
28913498266Sopenharmony_ci/*
29013498266Sopenharmony_ci * SET reads 4 input bytes in little-endian byte order and stores them
29113498266Sopenharmony_ci * in a properly aligned word in host byte order.
29213498266Sopenharmony_ci *
29313498266Sopenharmony_ci * The check for little-endian architectures that tolerate unaligned
29413498266Sopenharmony_ci * memory accesses is just an optimization.  Nothing will break if it
29513498266Sopenharmony_ci * doesn't work.
29613498266Sopenharmony_ci */
29713498266Sopenharmony_ci#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
29813498266Sopenharmony_ci#define SET(n) \
29913498266Sopenharmony_ci        (*(MD4_u32plus *)(void *)&ptr[(n) * 4])
30013498266Sopenharmony_ci#define GET(n) \
30113498266Sopenharmony_ci        SET(n)
30213498266Sopenharmony_ci#else
30313498266Sopenharmony_ci#define SET(n) \
30413498266Sopenharmony_ci        (ctx->block[(n)] = \
30513498266Sopenharmony_ci        (MD4_u32plus)ptr[(n) * 4] | \
30613498266Sopenharmony_ci        ((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \
30713498266Sopenharmony_ci        ((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \
30813498266Sopenharmony_ci        ((MD4_u32plus)ptr[(n) * 4 + 3] << 24))
30913498266Sopenharmony_ci#define GET(n) \
31013498266Sopenharmony_ci        (ctx->block[(n)])
31113498266Sopenharmony_ci#endif
31213498266Sopenharmony_ci
31313498266Sopenharmony_ci/*
31413498266Sopenharmony_ci * This processes one or more 64-byte data blocks, but does NOT update
31513498266Sopenharmony_ci * the bit counters.  There are no alignment requirements.
31613498266Sopenharmony_ci */
31713498266Sopenharmony_cistatic const void *body(MD4_CTX *ctx, const void *data, unsigned long size)
31813498266Sopenharmony_ci{
31913498266Sopenharmony_ci  const unsigned char *ptr;
32013498266Sopenharmony_ci  MD4_u32plus a, b, c, d;
32113498266Sopenharmony_ci
32213498266Sopenharmony_ci  ptr = (const unsigned char *)data;
32313498266Sopenharmony_ci
32413498266Sopenharmony_ci  a = ctx->a;
32513498266Sopenharmony_ci  b = ctx->b;
32613498266Sopenharmony_ci  c = ctx->c;
32713498266Sopenharmony_ci  d = ctx->d;
32813498266Sopenharmony_ci
32913498266Sopenharmony_ci  do {
33013498266Sopenharmony_ci    MD4_u32plus saved_a, saved_b, saved_c, saved_d;
33113498266Sopenharmony_ci
33213498266Sopenharmony_ci    saved_a = a;
33313498266Sopenharmony_ci    saved_b = b;
33413498266Sopenharmony_ci    saved_c = c;
33513498266Sopenharmony_ci    saved_d = d;
33613498266Sopenharmony_ci
33713498266Sopenharmony_ci/* Round 1 */
33813498266Sopenharmony_ci    STEP(F, a, b, c, d, SET(0), 3)
33913498266Sopenharmony_ci    STEP(F, d, a, b, c, SET(1), 7)
34013498266Sopenharmony_ci    STEP(F, c, d, a, b, SET(2), 11)
34113498266Sopenharmony_ci    STEP(F, b, c, d, a, SET(3), 19)
34213498266Sopenharmony_ci    STEP(F, a, b, c, d, SET(4), 3)
34313498266Sopenharmony_ci    STEP(F, d, a, b, c, SET(5), 7)
34413498266Sopenharmony_ci    STEP(F, c, d, a, b, SET(6), 11)
34513498266Sopenharmony_ci    STEP(F, b, c, d, a, SET(7), 19)
34613498266Sopenharmony_ci    STEP(F, a, b, c, d, SET(8), 3)
34713498266Sopenharmony_ci    STEP(F, d, a, b, c, SET(9), 7)
34813498266Sopenharmony_ci    STEP(F, c, d, a, b, SET(10), 11)
34913498266Sopenharmony_ci    STEP(F, b, c, d, a, SET(11), 19)
35013498266Sopenharmony_ci    STEP(F, a, b, c, d, SET(12), 3)
35113498266Sopenharmony_ci    STEP(F, d, a, b, c, SET(13), 7)
35213498266Sopenharmony_ci    STEP(F, c, d, a, b, SET(14), 11)
35313498266Sopenharmony_ci    STEP(F, b, c, d, a, SET(15), 19)
35413498266Sopenharmony_ci
35513498266Sopenharmony_ci/* Round 2 */
35613498266Sopenharmony_ci    STEP(G, a, b, c, d, GET(0) + 0x5a827999, 3)
35713498266Sopenharmony_ci    STEP(G, d, a, b, c, GET(4) + 0x5a827999, 5)
35813498266Sopenharmony_ci    STEP(G, c, d, a, b, GET(8) + 0x5a827999, 9)
35913498266Sopenharmony_ci    STEP(G, b, c, d, a, GET(12) + 0x5a827999, 13)
36013498266Sopenharmony_ci    STEP(G, a, b, c, d, GET(1) + 0x5a827999, 3)
36113498266Sopenharmony_ci    STEP(G, d, a, b, c, GET(5) + 0x5a827999, 5)
36213498266Sopenharmony_ci    STEP(G, c, d, a, b, GET(9) + 0x5a827999, 9)
36313498266Sopenharmony_ci    STEP(G, b, c, d, a, GET(13) + 0x5a827999, 13)
36413498266Sopenharmony_ci    STEP(G, a, b, c, d, GET(2) + 0x5a827999, 3)
36513498266Sopenharmony_ci    STEP(G, d, a, b, c, GET(6) + 0x5a827999, 5)
36613498266Sopenharmony_ci    STEP(G, c, d, a, b, GET(10) + 0x5a827999, 9)
36713498266Sopenharmony_ci    STEP(G, b, c, d, a, GET(14) + 0x5a827999, 13)
36813498266Sopenharmony_ci    STEP(G, a, b, c, d, GET(3) + 0x5a827999, 3)
36913498266Sopenharmony_ci    STEP(G, d, a, b, c, GET(7) + 0x5a827999, 5)
37013498266Sopenharmony_ci    STEP(G, c, d, a, b, GET(11) + 0x5a827999, 9)
37113498266Sopenharmony_ci    STEP(G, b, c, d, a, GET(15) + 0x5a827999, 13)
37213498266Sopenharmony_ci
37313498266Sopenharmony_ci/* Round 3 */
37413498266Sopenharmony_ci    STEP(H, a, b, c, d, GET(0) + 0x6ed9eba1, 3)
37513498266Sopenharmony_ci    STEP(H, d, a, b, c, GET(8) + 0x6ed9eba1, 9)
37613498266Sopenharmony_ci    STEP(H, c, d, a, b, GET(4) + 0x6ed9eba1, 11)
37713498266Sopenharmony_ci    STEP(H, b, c, d, a, GET(12) + 0x6ed9eba1, 15)
37813498266Sopenharmony_ci    STEP(H, a, b, c, d, GET(2) + 0x6ed9eba1, 3)
37913498266Sopenharmony_ci    STEP(H, d, a, b, c, GET(10) + 0x6ed9eba1, 9)
38013498266Sopenharmony_ci    STEP(H, c, d, a, b, GET(6) + 0x6ed9eba1, 11)
38113498266Sopenharmony_ci    STEP(H, b, c, d, a, GET(14) + 0x6ed9eba1, 15)
38213498266Sopenharmony_ci    STEP(H, a, b, c, d, GET(1) + 0x6ed9eba1, 3)
38313498266Sopenharmony_ci    STEP(H, d, a, b, c, GET(9) + 0x6ed9eba1, 9)
38413498266Sopenharmony_ci    STEP(H, c, d, a, b, GET(5) + 0x6ed9eba1, 11)
38513498266Sopenharmony_ci    STEP(H, b, c, d, a, GET(13) + 0x6ed9eba1, 15)
38613498266Sopenharmony_ci    STEP(H, a, b, c, d, GET(3) + 0x6ed9eba1, 3)
38713498266Sopenharmony_ci    STEP(H, d, a, b, c, GET(11) + 0x6ed9eba1, 9)
38813498266Sopenharmony_ci    STEP(H, c, d, a, b, GET(7) + 0x6ed9eba1, 11)
38913498266Sopenharmony_ci    STEP(H, b, c, d, a, GET(15) + 0x6ed9eba1, 15)
39013498266Sopenharmony_ci
39113498266Sopenharmony_ci    a += saved_a;
39213498266Sopenharmony_ci    b += saved_b;
39313498266Sopenharmony_ci    c += saved_c;
39413498266Sopenharmony_ci    d += saved_d;
39513498266Sopenharmony_ci
39613498266Sopenharmony_ci    ptr += 64;
39713498266Sopenharmony_ci  } while(size -= 64);
39813498266Sopenharmony_ci
39913498266Sopenharmony_ci  ctx->a = a;
40013498266Sopenharmony_ci  ctx->b = b;
40113498266Sopenharmony_ci  ctx->c = c;
40213498266Sopenharmony_ci  ctx->d = d;
40313498266Sopenharmony_ci
40413498266Sopenharmony_ci  return ptr;
40513498266Sopenharmony_ci}
40613498266Sopenharmony_ci
40713498266Sopenharmony_cistatic int MD4_Init(MD4_CTX *ctx)
40813498266Sopenharmony_ci{
40913498266Sopenharmony_ci  ctx->a = 0x67452301;
41013498266Sopenharmony_ci  ctx->b = 0xefcdab89;
41113498266Sopenharmony_ci  ctx->c = 0x98badcfe;
41213498266Sopenharmony_ci  ctx->d = 0x10325476;
41313498266Sopenharmony_ci
41413498266Sopenharmony_ci  ctx->lo = 0;
41513498266Sopenharmony_ci  ctx->hi = 0;
41613498266Sopenharmony_ci  return 1;
41713498266Sopenharmony_ci}
41813498266Sopenharmony_ci
41913498266Sopenharmony_cistatic void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
42013498266Sopenharmony_ci{
42113498266Sopenharmony_ci  MD4_u32plus saved_lo;
42213498266Sopenharmony_ci  unsigned long used;
42313498266Sopenharmony_ci
42413498266Sopenharmony_ci  saved_lo = ctx->lo;
42513498266Sopenharmony_ci  ctx->lo = (saved_lo + size) & 0x1fffffff;
42613498266Sopenharmony_ci  if(ctx->lo < saved_lo)
42713498266Sopenharmony_ci    ctx->hi++;
42813498266Sopenharmony_ci  ctx->hi += (MD4_u32plus)size >> 29;
42913498266Sopenharmony_ci
43013498266Sopenharmony_ci  used = saved_lo & 0x3f;
43113498266Sopenharmony_ci
43213498266Sopenharmony_ci  if(used) {
43313498266Sopenharmony_ci    unsigned long available = 64 - used;
43413498266Sopenharmony_ci
43513498266Sopenharmony_ci    if(size < available) {
43613498266Sopenharmony_ci      memcpy(&ctx->buffer[used], data, size);
43713498266Sopenharmony_ci      return;
43813498266Sopenharmony_ci    }
43913498266Sopenharmony_ci
44013498266Sopenharmony_ci    memcpy(&ctx->buffer[used], data, available);
44113498266Sopenharmony_ci    data = (const unsigned char *)data + available;
44213498266Sopenharmony_ci    size -= available;
44313498266Sopenharmony_ci    body(ctx, ctx->buffer, 64);
44413498266Sopenharmony_ci  }
44513498266Sopenharmony_ci
44613498266Sopenharmony_ci  if(size >= 64) {
44713498266Sopenharmony_ci    data = body(ctx, data, size & ~(unsigned long)0x3f);
44813498266Sopenharmony_ci    size &= 0x3f;
44913498266Sopenharmony_ci  }
45013498266Sopenharmony_ci
45113498266Sopenharmony_ci  memcpy(ctx->buffer, data, size);
45213498266Sopenharmony_ci}
45313498266Sopenharmony_ci
45413498266Sopenharmony_cistatic void MD4_Final(unsigned char *result, MD4_CTX *ctx)
45513498266Sopenharmony_ci{
45613498266Sopenharmony_ci  unsigned long used, available;
45713498266Sopenharmony_ci
45813498266Sopenharmony_ci  used = ctx->lo & 0x3f;
45913498266Sopenharmony_ci
46013498266Sopenharmony_ci  ctx->buffer[used++] = 0x80;
46113498266Sopenharmony_ci
46213498266Sopenharmony_ci  available = 64 - used;
46313498266Sopenharmony_ci
46413498266Sopenharmony_ci  if(available < 8) {
46513498266Sopenharmony_ci    memset(&ctx->buffer[used], 0, available);
46613498266Sopenharmony_ci    body(ctx, ctx->buffer, 64);
46713498266Sopenharmony_ci    used = 0;
46813498266Sopenharmony_ci    available = 64;
46913498266Sopenharmony_ci  }
47013498266Sopenharmony_ci
47113498266Sopenharmony_ci  memset(&ctx->buffer[used], 0, available - 8);
47213498266Sopenharmony_ci
47313498266Sopenharmony_ci  ctx->lo <<= 3;
47413498266Sopenharmony_ci  ctx->buffer[56] = curlx_ultouc((ctx->lo)&0xff);
47513498266Sopenharmony_ci  ctx->buffer[57] = curlx_ultouc((ctx->lo >> 8)&0xff);
47613498266Sopenharmony_ci  ctx->buffer[58] = curlx_ultouc((ctx->lo >> 16)&0xff);
47713498266Sopenharmony_ci  ctx->buffer[59] = curlx_ultouc((ctx->lo >> 24)&0xff);
47813498266Sopenharmony_ci  ctx->buffer[60] = curlx_ultouc((ctx->hi)&0xff);
47913498266Sopenharmony_ci  ctx->buffer[61] = curlx_ultouc((ctx->hi >> 8)&0xff);
48013498266Sopenharmony_ci  ctx->buffer[62] = curlx_ultouc((ctx->hi >> 16)&0xff);
48113498266Sopenharmony_ci  ctx->buffer[63] = curlx_ultouc(ctx->hi >> 24);
48213498266Sopenharmony_ci
48313498266Sopenharmony_ci  body(ctx, ctx->buffer, 64);
48413498266Sopenharmony_ci
48513498266Sopenharmony_ci  result[0] = curlx_ultouc((ctx->a)&0xff);
48613498266Sopenharmony_ci  result[1] = curlx_ultouc((ctx->a >> 8)&0xff);
48713498266Sopenharmony_ci  result[2] = curlx_ultouc((ctx->a >> 16)&0xff);
48813498266Sopenharmony_ci  result[3] = curlx_ultouc(ctx->a >> 24);
48913498266Sopenharmony_ci  result[4] = curlx_ultouc((ctx->b)&0xff);
49013498266Sopenharmony_ci  result[5] = curlx_ultouc((ctx->b >> 8)&0xff);
49113498266Sopenharmony_ci  result[6] = curlx_ultouc((ctx->b >> 16)&0xff);
49213498266Sopenharmony_ci  result[7] = curlx_ultouc(ctx->b >> 24);
49313498266Sopenharmony_ci  result[8] = curlx_ultouc((ctx->c)&0xff);
49413498266Sopenharmony_ci  result[9] = curlx_ultouc((ctx->c >> 8)&0xff);
49513498266Sopenharmony_ci  result[10] = curlx_ultouc((ctx->c >> 16)&0xff);
49613498266Sopenharmony_ci  result[11] = curlx_ultouc(ctx->c >> 24);
49713498266Sopenharmony_ci  result[12] = curlx_ultouc((ctx->d)&0xff);
49813498266Sopenharmony_ci  result[13] = curlx_ultouc((ctx->d >> 8)&0xff);
49913498266Sopenharmony_ci  result[14] = curlx_ultouc((ctx->d >> 16)&0xff);
50013498266Sopenharmony_ci  result[15] = curlx_ultouc(ctx->d >> 24);
50113498266Sopenharmony_ci
50213498266Sopenharmony_ci  memset(ctx, 0, sizeof(*ctx));
50313498266Sopenharmony_ci}
50413498266Sopenharmony_ci
50513498266Sopenharmony_ci#endif /* CRYPTO LIBS */
50613498266Sopenharmony_ci
50713498266Sopenharmony_ciCURLcode Curl_md4it(unsigned char *output, const unsigned char *input,
50813498266Sopenharmony_ci                    const size_t len)
50913498266Sopenharmony_ci{
51013498266Sopenharmony_ci  MD4_CTX ctx;
51113498266Sopenharmony_ci
51213498266Sopenharmony_ci#ifdef VOID_MD4_INIT
51313498266Sopenharmony_ci  MD4_Init(&ctx);
51413498266Sopenharmony_ci#else
51513498266Sopenharmony_ci  if(!MD4_Init(&ctx))
51613498266Sopenharmony_ci    return CURLE_FAILED_INIT;
51713498266Sopenharmony_ci#endif
51813498266Sopenharmony_ci
51913498266Sopenharmony_ci  MD4_Update(&ctx, input, curlx_uztoui(len));
52013498266Sopenharmony_ci  MD4_Final(output, &ctx);
52113498266Sopenharmony_ci  return CURLE_OK;
52213498266Sopenharmony_ci}
52313498266Sopenharmony_ci
52413498266Sopenharmony_ci#endif /* USE_CURL_NTLM_CORE */
525