2019-04-14 03:55:18 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 15:51:48 +03:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2019-04-14 03:55:18 +03:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 16:02:01 +03:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2019-04-14 03:55:18 +03:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 12:16:50 +03:00
|
|
|
*
|
2019-04-14 03:55:18 +03:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
2023-05-11 10:06:33 +03:00
|
|
|
#if defined(USE_CURL_NTLM_CORE)
|
2019-04-14 21:24:28 +03:00
|
|
|
|
2022-10-25 00:27:31 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2024-02-05 17:22:08 +03:00
|
|
|
#include "strdup.h"
|
2019-04-14 21:24:28 +03:00
|
|
|
#include "curl_md4.h"
|
|
|
|
#include "warnless.h"
|
|
|
|
|
2019-04-14 03:55:18 +03:00
|
|
|
#ifdef USE_OPENSSL
|
2023-11-08 07:39:54 +03:00
|
|
|
#include <openssl/opensslv.h>
|
|
|
|
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L) && !defined(USE_AMISSL)
|
2020-03-23 14:28:20 +03:00
|
|
|
/* OpenSSL 3.0.0 marks the MD4 functions as deprecated */
|
|
|
|
#define OPENSSL_NO_MD4
|
2024-07-18 07:41:51 +03:00
|
|
|
#else
|
|
|
|
/* Cover also OPENSSL_NO_MD4 configured in openssl */
|
|
|
|
#include <openssl/opensslconf.h>
|
2020-03-23 14:28:20 +03:00
|
|
|
#endif
|
2020-02-26 21:24:31 +03:00
|
|
|
#endif /* USE_OPENSSL */
|
|
|
|
|
2021-10-02 02:06:55 +03:00
|
|
|
#ifdef USE_WOLFSSL
|
|
|
|
#include <wolfssl/options.h>
|
2023-03-10 12:01:44 +03:00
|
|
|
#define VOID_MD4_INIT
|
2021-10-02 02:06:55 +03:00
|
|
|
#ifdef NO_MD4
|
2022-05-17 07:55:24 +03:00
|
|
|
#define WOLFSSL_NO_MD4
|
2021-10-02 02:06:55 +03:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2019-04-14 03:55:18 +03:00
|
|
|
#ifdef USE_MBEDTLS
|
2020-02-26 21:24:31 +03:00
|
|
|
#include <mbedtls/version.h>
|
2021-07-30 17:57:11 +03:00
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
|
|
|
#include <mbedtls/mbedtls_config.h>
|
|
|
|
#else
|
|
|
|
#include <mbedtls/config.h>
|
|
|
|
#endif
|
2024-04-15 21:39:15 +03:00
|
|
|
#if(MBEDTLS_VERSION_NUMBER >= 0x02070000) && \
|
|
|
|
(MBEDTLS_VERSION_NUMBER < 0x03000000)
|
2020-02-26 21:24:31 +03:00
|
|
|
#define HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS
|
2019-04-14 03:55:18 +03:00
|
|
|
#endif
|
2020-02-26 21:24:31 +03:00
|
|
|
#endif /* USE_MBEDTLS */
|
2019-04-14 03:55:18 +03:00
|
|
|
|
2021-02-18 13:42:46 +03:00
|
|
|
#if defined(USE_GNUTLS)
|
2019-04-14 03:55:18 +03:00
|
|
|
#include <nettle/md4.h>
|
2022-10-25 00:27:31 +03:00
|
|
|
/* When OpenSSL or wolfSSL is available, we use their MD4 functions. */
|
|
|
|
#elif defined(USE_WOLFSSL) && !defined(WOLFSSL_NO_MD4)
|
|
|
|
#include <wolfssl/openssl/md4.h>
|
|
|
|
#elif defined(USE_OPENSSL) && !defined(OPENSSL_NO_MD4)
|
|
|
|
#include <openssl/md4.h>
|
|
|
|
#elif (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && \
|
|
|
|
(__MAC_OS_X_VERSION_MAX_ALLOWED >= 1040) && \
|
2023-05-11 10:23:13 +03:00
|
|
|
defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
|
|
|
|
(__MAC_OS_X_VERSION_MIN_REQUIRED < 101500)) || \
|
2022-10-25 00:27:31 +03:00
|
|
|
(defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \
|
2023-05-11 10:23:13 +03:00
|
|
|
(__IPHONE_OS_VERSION_MAX_ALLOWED >= 20000) && \
|
|
|
|
defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
|
|
|
|
(__IPHONE_OS_VERSION_MIN_REQUIRED < 130000))
|
2022-10-25 00:27:31 +03:00
|
|
|
#define AN_APPLE_OS
|
|
|
|
#include <CommonCrypto/CommonDigest.h>
|
|
|
|
#elif defined(USE_WIN32_CRYPTO)
|
|
|
|
#include <wincrypt.h>
|
|
|
|
#elif(defined(USE_MBEDTLS) && defined(MBEDTLS_MD4_C))
|
|
|
|
#include <mbedtls/md4.h>
|
|
|
|
#endif
|
2019-04-14 03:55:18 +03:00
|
|
|
|
2022-10-25 00:27:31 +03:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
2019-04-14 03:55:18 +03:00
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2022-10-25 00:27:31 +03:00
|
|
|
|
2022-12-22 19:40:26 +03:00
|
|
|
#if defined(USE_GNUTLS)
|
2022-10-25 00:27:31 +03:00
|
|
|
|
2019-04-14 03:55:18 +03:00
|
|
|
typedef struct md4_ctx MD4_CTX;
|
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
static int MD4_Init(MD4_CTX *ctx)
|
2019-04-14 03:55:18 +03:00
|
|
|
{
|
|
|
|
md4_init(ctx);
|
2023-03-10 12:01:44 +03:00
|
|
|
return 1;
|
2019-04-14 03:55:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
|
|
|
|
{
|
|
|
|
md4_update(ctx, size, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
|
|
|
|
{
|
|
|
|
md4_digest(ctx, MD4_DIGEST_SIZE, result);
|
|
|
|
}
|
|
|
|
|
2022-12-22 19:40:26 +03:00
|
|
|
#elif defined(USE_WOLFSSL) && !defined(WOLFSSL_NO_MD4)
|
|
|
|
|
|
|
|
#elif defined(USE_OPENSSL) && !defined(OPENSSL_NO_MD4)
|
|
|
|
|
2022-10-25 00:27:31 +03:00
|
|
|
#elif defined(AN_APPLE_OS)
|
2020-02-24 05:23:52 +03:00
|
|
|
typedef CC_MD4_CTX MD4_CTX;
|
2019-04-14 04:45:02 +03:00
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
static int MD4_Init(MD4_CTX *ctx)
|
2019-04-14 04:45:02 +03:00
|
|
|
{
|
2023-03-10 12:01:44 +03:00
|
|
|
return CC_MD4_Init(ctx);
|
2019-04-14 04:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
|
|
|
|
{
|
2020-02-24 05:23:52 +03:00
|
|
|
(void)CC_MD4_Update(ctx, data, (CC_LONG)size);
|
2019-04-14 04:45:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
|
|
|
|
{
|
2020-02-24 05:23:52 +03:00
|
|
|
(void)CC_MD4_Final(result, ctx);
|
2019-04-14 04:45:02 +03:00
|
|
|
}
|
|
|
|
|
2019-04-14 05:17:23 +03:00
|
|
|
#elif defined(USE_WIN32_CRYPTO)
|
|
|
|
|
2020-05-14 01:05:04 +03:00
|
|
|
struct md4_ctx {
|
2019-04-14 05:17:23 +03:00
|
|
|
HCRYPTPROV hCryptProv;
|
|
|
|
HCRYPTHASH hHash;
|
2020-05-14 01:05:04 +03:00
|
|
|
};
|
|
|
|
typedef struct md4_ctx MD4_CTX;
|
2019-04-14 05:17:23 +03:00
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
static int MD4_Init(MD4_CTX *ctx)
|
2019-04-14 05:17:23 +03:00
|
|
|
{
|
|
|
|
ctx->hCryptProv = 0;
|
|
|
|
ctx->hHash = 0;
|
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
if(!CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(!CryptCreateHash(ctx->hCryptProv, CALG_MD4, 0, 0, &ctx->hHash)) {
|
|
|
|
CryptReleaseContext(ctx->hCryptProv, 0);
|
|
|
|
ctx->hCryptProv = 0;
|
|
|
|
return 0;
|
2019-04-14 05:17:23 +03:00
|
|
|
}
|
2023-03-10 12:01:44 +03:00
|
|
|
|
|
|
|
return 1;
|
2019-04-14 05:17:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
|
|
|
|
{
|
2019-12-14 01:23:11 +03:00
|
|
|
CryptHashData(ctx->hHash, (BYTE *)data, (unsigned int) size, 0);
|
2019-04-14 05:17:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
|
|
|
|
{
|
|
|
|
unsigned long length = 0;
|
|
|
|
|
|
|
|
CryptGetHashParam(ctx->hHash, HP_HASHVAL, NULL, &length, 0);
|
|
|
|
if(length == MD4_DIGEST_LENGTH)
|
|
|
|
CryptGetHashParam(ctx->hHash, HP_HASHVAL, result, &length, 0);
|
|
|
|
|
|
|
|
if(ctx->hHash)
|
|
|
|
CryptDestroyHash(ctx->hHash);
|
|
|
|
|
|
|
|
if(ctx->hCryptProv)
|
|
|
|
CryptReleaseContext(ctx->hCryptProv, 0);
|
|
|
|
}
|
|
|
|
|
2019-04-14 05:27:21 +03:00
|
|
|
#elif(defined(USE_MBEDTLS) && defined(MBEDTLS_MD4_C))
|
|
|
|
|
2020-05-14 01:05:04 +03:00
|
|
|
struct md4_ctx {
|
2019-04-14 05:27:21 +03:00
|
|
|
void *data;
|
|
|
|
unsigned long size;
|
2020-05-14 01:05:04 +03:00
|
|
|
};
|
|
|
|
typedef struct md4_ctx MD4_CTX;
|
2019-04-14 05:27:21 +03:00
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
static int MD4_Init(MD4_CTX *ctx)
|
2019-04-14 05:27:21 +03:00
|
|
|
{
|
|
|
|
ctx->data = NULL;
|
|
|
|
ctx->size = 0;
|
2023-03-10 12:01:44 +03:00
|
|
|
return 1;
|
2019-04-14 05:27:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
|
|
|
|
{
|
2021-04-19 11:46:11 +03:00
|
|
|
if(!ctx->data) {
|
2023-12-05 17:55:35 +03:00
|
|
|
ctx->data = Curl_memdup(data, size);
|
|
|
|
if(ctx->data)
|
2019-04-14 05:27:21 +03:00
|
|
|
ctx->size = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
|
|
|
|
{
|
2021-12-25 23:41:14 +03:00
|
|
|
if(ctx->data) {
|
2020-02-26 21:24:31 +03:00
|
|
|
#if !defined(HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS)
|
2019-04-14 05:27:21 +03:00
|
|
|
mbedtls_md4(ctx->data, ctx->size, result);
|
2020-02-26 21:24:31 +03:00
|
|
|
#else
|
|
|
|
(void) mbedtls_md4_ret(ctx->data, ctx->size, result);
|
|
|
|
#endif
|
2019-04-14 05:27:21 +03:00
|
|
|
|
|
|
|
Curl_safefree(ctx->data);
|
|
|
|
ctx->size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 21:24:28 +03:00
|
|
|
#else
|
2024-07-01 17:47:21 +03:00
|
|
|
/* When no other crypto library is available, or the crypto library does not
|
2019-04-14 21:24:28 +03:00
|
|
|
* support MD4, we use this code segment this implementation of it
|
2019-04-14 03:55:18 +03:00
|
|
|
*
|
2015-02-03 11:55:47 +03:00
|
|
|
* This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
|
|
|
|
* MD4 Message-Digest Algorithm (RFC 1320).
|
|
|
|
*
|
|
|
|
* Homepage:
|
2018-09-14 21:48:35 +03:00
|
|
|
https://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
|
2015-02-03 11:55:47 +03:00
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
|
|
|
|
*
|
2024-07-01 17:47:21 +03:00
|
|
|
* This software was written by Alexander Peslyak in 2001. No copyright is
|
|
|
|
* claimed, and the software is hereby placed in the public domain. In case
|
2015-02-03 12:00:54 +03:00
|
|
|
* this attempt to disclaim copyright and place the software in the public
|
|
|
|
* domain is deemed null and void, then the software is Copyright (c) 2001
|
|
|
|
* Alexander Peslyak and it is hereby released to the general public under the
|
|
|
|
* following terms:
|
2015-02-03 11:55:47 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted.
|
|
|
|
*
|
2024-07-01 17:47:21 +03:00
|
|
|
* There is ABSOLUTELY NO WARRANTY, express or implied.
|
2015-02-03 11:55:47 +03:00
|
|
|
*
|
|
|
|
* (This is a heavily cut-down "BSD license".)
|
|
|
|
*
|
|
|
|
* This differs from Colin Plumb's older public domain implementation in that
|
|
|
|
* no exactly 32-bit integer data type is required (any 32-bit or wider
|
2024-07-01 17:47:21 +03:00
|
|
|
* unsigned integer data type will do), there is no compile-time endianness
|
|
|
|
* configuration, and the function prototypes match OpenSSL's. No code from
|
2015-02-03 11:55:47 +03:00
|
|
|
* Colin Plumb's implementation has been reused; this comment merely compares
|
|
|
|
* the properties of the two independent implementations.
|
|
|
|
*
|
|
|
|
* The primary goals of this implementation are portability and ease of use.
|
2024-07-01 17:47:21 +03:00
|
|
|
* It is meant to be fast, but not as fast as possible. Some known
|
2015-02-03 11:55:47 +03:00
|
|
|
* optimizations are not included to reduce source code size and avoid
|
|
|
|
* compile-time configuration.
|
2010-06-27 05:38:01 +04:00
|
|
|
*/
|
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
/* Any 32-bit or wider unsigned integer data type will do */
|
|
|
|
typedef unsigned int MD4_u32plus;
|
|
|
|
|
2020-05-14 01:05:04 +03:00
|
|
|
struct md4_ctx {
|
2015-02-03 11:55:47 +03:00
|
|
|
MD4_u32plus lo, hi;
|
|
|
|
MD4_u32plus a, b, c, d;
|
|
|
|
unsigned char buffer[64];
|
|
|
|
MD4_u32plus block[16];
|
2020-05-14 01:05:04 +03:00
|
|
|
};
|
|
|
|
typedef struct md4_ctx MD4_CTX;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
static int MD4_Init(MD4_CTX *ctx);
|
2015-02-06 16:26:01 +03:00
|
|
|
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size);
|
|
|
|
static void MD4_Final(unsigned char *result, MD4_CTX *ctx);
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
/*
|
|
|
|
* The basic MD4 functions.
|
|
|
|
*
|
|
|
|
* F and G are optimized compared to their RFC 1320 definitions, with the
|
|
|
|
* optimization for F borrowed from Colin Plumb's MD5 implementation.
|
2010-06-27 05:38:01 +04:00
|
|
|
*/
|
2024-05-21 23:31:12 +03:00
|
|
|
#define MD4_F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
|
|
|
|
#define MD4_G(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
|
|
|
|
#define MD4_H(x, y, z) ((x) ^ (y) ^ (z))
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
/*
|
|
|
|
* The MD4 transformation for all three rounds.
|
2010-06-27 05:38:01 +04:00
|
|
|
*/
|
2024-05-21 23:31:12 +03:00
|
|
|
#define MD4_STEP(f, a, b, c, d, x, s) \
|
2015-02-03 12:00:54 +03:00
|
|
|
(a) += f((b), (c), (d)) + (x); \
|
|
|
|
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));
|
2015-02-03 11:55:47 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SET reads 4 input bytes in little-endian byte order and stores them
|
|
|
|
* in a properly aligned word in host byte order.
|
|
|
|
*
|
|
|
|
* The check for little-endian architectures that tolerate unaligned
|
2024-07-01 17:47:21 +03:00
|
|
|
* memory accesses is just an optimization. Nothing will break if it
|
|
|
|
* does not work.
|
2015-02-03 11:55:47 +03:00
|
|
|
*/
|
|
|
|
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
|
2024-05-21 23:31:12 +03:00
|
|
|
#define MD4_SET(n) \
|
2017-06-02 22:58:48 +03:00
|
|
|
(*(MD4_u32plus *)(void *)&ptr[(n) * 4])
|
2024-05-21 23:31:12 +03:00
|
|
|
#define MD4_GET(n) \
|
|
|
|
MD4_SET(n)
|
2015-02-03 11:55:47 +03:00
|
|
|
#else
|
2024-05-21 23:31:12 +03:00
|
|
|
#define MD4_SET(n) \
|
2015-02-03 12:00:54 +03:00
|
|
|
(ctx->block[(n)] = \
|
|
|
|
(MD4_u32plus)ptr[(n) * 4] | \
|
|
|
|
((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \
|
|
|
|
((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \
|
|
|
|
((MD4_u32plus)ptr[(n) * 4 + 3] << 24))
|
2024-05-21 23:31:12 +03:00
|
|
|
#define MD4_GET(n) \
|
2015-02-03 12:00:54 +03:00
|
|
|
(ctx->block[(n)])
|
2015-02-03 11:55:47 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This processes one or more 64-byte data blocks, but does NOT update
|
2024-07-01 17:47:21 +03:00
|
|
|
* the bit counters. There are no alignment requirements.
|
2015-02-03 11:55:47 +03:00
|
|
|
*/
|
2024-05-21 23:31:12 +03:00
|
|
|
static const void *my_md4_body(MD4_CTX *ctx,
|
|
|
|
const void *data, unsigned long size)
|
2010-06-27 05:38:01 +04:00
|
|
|
{
|
2015-02-03 11:55:47 +03:00
|
|
|
const unsigned char *ptr;
|
|
|
|
MD4_u32plus a, b, c, d;
|
|
|
|
|
|
|
|
ptr = (const unsigned char *)data;
|
|
|
|
|
|
|
|
a = ctx->a;
|
|
|
|
b = ctx->b;
|
|
|
|
c = ctx->c;
|
|
|
|
d = ctx->d;
|
|
|
|
|
|
|
|
do {
|
2019-05-11 22:42:48 +03:00
|
|
|
MD4_u32plus saved_a, saved_b, saved_c, saved_d;
|
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
saved_a = a;
|
|
|
|
saved_b = b;
|
|
|
|
saved_c = c;
|
|
|
|
saved_d = d;
|
|
|
|
|
|
|
|
/* Round 1 */
|
2024-05-21 23:31:12 +03:00
|
|
|
MD4_STEP(MD4_F, a, b, c, d, MD4_SET(0), 3)
|
|
|
|
MD4_STEP(MD4_F, d, a, b, c, MD4_SET(1), 7)
|
|
|
|
MD4_STEP(MD4_F, c, d, a, b, MD4_SET(2), 11)
|
|
|
|
MD4_STEP(MD4_F, b, c, d, a, MD4_SET(3), 19)
|
|
|
|
MD4_STEP(MD4_F, a, b, c, d, MD4_SET(4), 3)
|
|
|
|
MD4_STEP(MD4_F, d, a, b, c, MD4_SET(5), 7)
|
|
|
|
MD4_STEP(MD4_F, c, d, a, b, MD4_SET(6), 11)
|
|
|
|
MD4_STEP(MD4_F, b, c, d, a, MD4_SET(7), 19)
|
|
|
|
MD4_STEP(MD4_F, a, b, c, d, MD4_SET(8), 3)
|
|
|
|
MD4_STEP(MD4_F, d, a, b, c, MD4_SET(9), 7)
|
|
|
|
MD4_STEP(MD4_F, c, d, a, b, MD4_SET(10), 11)
|
|
|
|
MD4_STEP(MD4_F, b, c, d, a, MD4_SET(11), 19)
|
|
|
|
MD4_STEP(MD4_F, a, b, c, d, MD4_SET(12), 3)
|
|
|
|
MD4_STEP(MD4_F, d, a, b, c, MD4_SET(13), 7)
|
|
|
|
MD4_STEP(MD4_F, c, d, a, b, MD4_SET(14), 11)
|
|
|
|
MD4_STEP(MD4_F, b, c, d, a, MD4_SET(15), 19)
|
2015-02-03 11:55:47 +03:00
|
|
|
|
|
|
|
/* Round 2 */
|
2024-05-21 23:31:12 +03:00
|
|
|
MD4_STEP(MD4_G, a, b, c, d, MD4_GET(0) + 0x5a827999, 3)
|
|
|
|
MD4_STEP(MD4_G, d, a, b, c, MD4_GET(4) + 0x5a827999, 5)
|
|
|
|
MD4_STEP(MD4_G, c, d, a, b, MD4_GET(8) + 0x5a827999, 9)
|
|
|
|
MD4_STEP(MD4_G, b, c, d, a, MD4_GET(12) + 0x5a827999, 13)
|
|
|
|
MD4_STEP(MD4_G, a, b, c, d, MD4_GET(1) + 0x5a827999, 3)
|
|
|
|
MD4_STEP(MD4_G, d, a, b, c, MD4_GET(5) + 0x5a827999, 5)
|
|
|
|
MD4_STEP(MD4_G, c, d, a, b, MD4_GET(9) + 0x5a827999, 9)
|
|
|
|
MD4_STEP(MD4_G, b, c, d, a, MD4_GET(13) + 0x5a827999, 13)
|
|
|
|
MD4_STEP(MD4_G, a, b, c, d, MD4_GET(2) + 0x5a827999, 3)
|
|
|
|
MD4_STEP(MD4_G, d, a, b, c, MD4_GET(6) + 0x5a827999, 5)
|
|
|
|
MD4_STEP(MD4_G, c, d, a, b, MD4_GET(10) + 0x5a827999, 9)
|
|
|
|
MD4_STEP(MD4_G, b, c, d, a, MD4_GET(14) + 0x5a827999, 13)
|
|
|
|
MD4_STEP(MD4_G, a, b, c, d, MD4_GET(3) + 0x5a827999, 3)
|
|
|
|
MD4_STEP(MD4_G, d, a, b, c, MD4_GET(7) + 0x5a827999, 5)
|
|
|
|
MD4_STEP(MD4_G, c, d, a, b, MD4_GET(11) + 0x5a827999, 9)
|
|
|
|
MD4_STEP(MD4_G, b, c, d, a, MD4_GET(15) + 0x5a827999, 13)
|
2015-02-03 11:55:47 +03:00
|
|
|
|
|
|
|
/* Round 3 */
|
2024-05-21 23:31:12 +03:00
|
|
|
MD4_STEP(MD4_H, a, b, c, d, MD4_GET(0) + 0x6ed9eba1, 3)
|
|
|
|
MD4_STEP(MD4_H, d, a, b, c, MD4_GET(8) + 0x6ed9eba1, 9)
|
|
|
|
MD4_STEP(MD4_H, c, d, a, b, MD4_GET(4) + 0x6ed9eba1, 11)
|
|
|
|
MD4_STEP(MD4_H, b, c, d, a, MD4_GET(12) + 0x6ed9eba1, 15)
|
|
|
|
MD4_STEP(MD4_H, a, b, c, d, MD4_GET(2) + 0x6ed9eba1, 3)
|
|
|
|
MD4_STEP(MD4_H, d, a, b, c, MD4_GET(10) + 0x6ed9eba1, 9)
|
|
|
|
MD4_STEP(MD4_H, c, d, a, b, MD4_GET(6) + 0x6ed9eba1, 11)
|
|
|
|
MD4_STEP(MD4_H, b, c, d, a, MD4_GET(14) + 0x6ed9eba1, 15)
|
|
|
|
MD4_STEP(MD4_H, a, b, c, d, MD4_GET(1) + 0x6ed9eba1, 3)
|
|
|
|
MD4_STEP(MD4_H, d, a, b, c, MD4_GET(9) + 0x6ed9eba1, 9)
|
|
|
|
MD4_STEP(MD4_H, c, d, a, b, MD4_GET(5) + 0x6ed9eba1, 11)
|
|
|
|
MD4_STEP(MD4_H, b, c, d, a, MD4_GET(13) + 0x6ed9eba1, 15)
|
|
|
|
MD4_STEP(MD4_H, a, b, c, d, MD4_GET(3) + 0x6ed9eba1, 3)
|
|
|
|
MD4_STEP(MD4_H, d, a, b, c, MD4_GET(11) + 0x6ed9eba1, 9)
|
|
|
|
MD4_STEP(MD4_H, c, d, a, b, MD4_GET(7) + 0x6ed9eba1, 11)
|
|
|
|
MD4_STEP(MD4_H, b, c, d, a, MD4_GET(15) + 0x6ed9eba1, 15)
|
2019-04-14 16:08:15 +03:00
|
|
|
|
|
|
|
a += saved_a;
|
2015-02-03 11:55:47 +03:00
|
|
|
b += saved_b;
|
|
|
|
c += saved_c;
|
|
|
|
d += saved_d;
|
|
|
|
|
|
|
|
ptr += 64;
|
2015-02-03 12:00:54 +03:00
|
|
|
} while(size -= 64);
|
2015-02-03 11:55:47 +03:00
|
|
|
|
|
|
|
ctx->a = a;
|
|
|
|
ctx->b = b;
|
|
|
|
ctx->c = c;
|
|
|
|
ctx->d = d;
|
|
|
|
|
|
|
|
return ptr;
|
2010-06-27 05:38:01 +04:00
|
|
|
}
|
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
static int MD4_Init(MD4_CTX *ctx)
|
2010-06-27 05:38:01 +04:00
|
|
|
{
|
2015-02-03 11:55:47 +03:00
|
|
|
ctx->a = 0x67452301;
|
|
|
|
ctx->b = 0xefcdab89;
|
|
|
|
ctx->c = 0x98badcfe;
|
|
|
|
ctx->d = 0x10325476;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
ctx->lo = 0;
|
|
|
|
ctx->hi = 0;
|
2023-03-10 12:01:44 +03:00
|
|
|
return 1;
|
2010-06-27 05:38:01 +04:00
|
|
|
}
|
|
|
|
|
2015-02-06 16:26:01 +03:00
|
|
|
static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
|
2010-06-27 05:38:01 +04:00
|
|
|
{
|
2015-02-03 11:55:47 +03:00
|
|
|
MD4_u32plus saved_lo;
|
2019-05-11 22:42:48 +03:00
|
|
|
unsigned long used;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
saved_lo = ctx->lo;
|
2016-12-14 03:29:44 +03:00
|
|
|
ctx->lo = (saved_lo + size) & 0x1fffffff;
|
|
|
|
if(ctx->lo < saved_lo)
|
2015-02-03 11:55:47 +03:00
|
|
|
ctx->hi++;
|
2015-02-03 12:00:54 +03:00
|
|
|
ctx->hi += (MD4_u32plus)size >> 29;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
used = saved_lo & 0x3f;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 12:00:54 +03:00
|
|
|
if(used) {
|
2019-05-11 22:42:48 +03:00
|
|
|
unsigned long available = 64 - used;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 12:00:54 +03:00
|
|
|
if(size < available) {
|
2015-02-03 11:55:47 +03:00
|
|
|
memcpy(&ctx->buffer[used], data, size);
|
|
|
|
return;
|
|
|
|
}
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
memcpy(&ctx->buffer[used], data, available);
|
|
|
|
data = (const unsigned char *)data + available;
|
|
|
|
size -= available;
|
2024-05-21 23:31:12 +03:00
|
|
|
my_md4_body(ctx, ctx->buffer, 64);
|
2015-02-03 11:55:47 +03:00
|
|
|
}
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 12:00:54 +03:00
|
|
|
if(size >= 64) {
|
2024-05-21 23:31:12 +03:00
|
|
|
data = my_md4_body(ctx, data, size & ~(unsigned long)0x3f);
|
2015-02-03 11:55:47 +03:00
|
|
|
size &= 0x3f;
|
|
|
|
}
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
memcpy(ctx->buffer, data, size);
|
2010-06-27 05:38:01 +04:00
|
|
|
}
|
|
|
|
|
2015-02-06 16:26:01 +03:00
|
|
|
static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
|
2010-06-27 05:38:01 +04:00
|
|
|
{
|
2015-02-03 11:55:47 +03:00
|
|
|
unsigned long used, available;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
used = ctx->lo & 0x3f;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 11:55:47 +03:00
|
|
|
ctx->buffer[used++] = 0x80;
|
|
|
|
|
|
|
|
available = 64 - used;
|
2010-06-27 05:38:01 +04:00
|
|
|
|
2015-02-03 12:00:54 +03:00
|
|
|
if(available < 8) {
|
2015-02-03 11:55:47 +03:00
|
|
|
memset(&ctx->buffer[used], 0, available);
|
2024-05-21 23:31:12 +03:00
|
|
|
my_md4_body(ctx, ctx->buffer, 64);
|
2015-02-03 11:55:47 +03:00
|
|
|
used = 0;
|
|
|
|
available = 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&ctx->buffer[used], 0, available - 8);
|
|
|
|
|
|
|
|
ctx->lo <<= 3;
|
2015-02-03 12:00:54 +03:00
|
|
|
ctx->buffer[56] = curlx_ultouc((ctx->lo)&0xff);
|
|
|
|
ctx->buffer[57] = curlx_ultouc((ctx->lo >> 8)&0xff);
|
|
|
|
ctx->buffer[58] = curlx_ultouc((ctx->lo >> 16)&0xff);
|
|
|
|
ctx->buffer[59] = curlx_ultouc((ctx->lo >> 24)&0xff);
|
|
|
|
ctx->buffer[60] = curlx_ultouc((ctx->hi)&0xff);
|
|
|
|
ctx->buffer[61] = curlx_ultouc((ctx->hi >> 8)&0xff);
|
|
|
|
ctx->buffer[62] = curlx_ultouc((ctx->hi >> 16)&0xff);
|
|
|
|
ctx->buffer[63] = curlx_ultouc(ctx->hi >> 24);
|
2015-02-03 11:55:47 +03:00
|
|
|
|
2024-05-21 23:31:12 +03:00
|
|
|
my_md4_body(ctx, ctx->buffer, 64);
|
2015-02-03 11:55:47 +03:00
|
|
|
|
2015-02-03 12:00:54 +03:00
|
|
|
result[0] = curlx_ultouc((ctx->a)&0xff);
|
|
|
|
result[1] = curlx_ultouc((ctx->a >> 8)&0xff);
|
|
|
|
result[2] = curlx_ultouc((ctx->a >> 16)&0xff);
|
|
|
|
result[3] = curlx_ultouc(ctx->a >> 24);
|
|
|
|
result[4] = curlx_ultouc((ctx->b)&0xff);
|
|
|
|
result[5] = curlx_ultouc((ctx->b >> 8)&0xff);
|
|
|
|
result[6] = curlx_ultouc((ctx->b >> 16)&0xff);
|
|
|
|
result[7] = curlx_ultouc(ctx->b >> 24);
|
|
|
|
result[8] = curlx_ultouc((ctx->c)&0xff);
|
|
|
|
result[9] = curlx_ultouc((ctx->c >> 8)&0xff);
|
|
|
|
result[10] = curlx_ultouc((ctx->c >> 16)&0xff);
|
|
|
|
result[11] = curlx_ultouc(ctx->c >> 24);
|
|
|
|
result[12] = curlx_ultouc((ctx->d)&0xff);
|
|
|
|
result[13] = curlx_ultouc((ctx->d >> 8)&0xff);
|
|
|
|
result[14] = curlx_ultouc((ctx->d >> 16)&0xff);
|
|
|
|
result[15] = curlx_ultouc(ctx->d >> 24);
|
2015-02-03 11:55:47 +03:00
|
|
|
|
|
|
|
memset(ctx, 0, sizeof(*ctx));
|
2010-06-27 05:38:01 +04:00
|
|
|
}
|
|
|
|
|
2019-04-14 03:55:18 +03:00
|
|
|
#endif /* CRYPTO LIBS */
|
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
CURLcode Curl_md4it(unsigned char *output, const unsigned char *input,
|
|
|
|
const size_t len)
|
2010-06-27 05:38:01 +04:00
|
|
|
{
|
|
|
|
MD4_CTX ctx;
|
2020-02-23 21:46:24 +03:00
|
|
|
|
2023-03-10 12:01:44 +03:00
|
|
|
#ifdef VOID_MD4_INIT
|
2015-02-03 12:00:54 +03:00
|
|
|
MD4_Init(&ctx);
|
2023-03-10 12:01:44 +03:00
|
|
|
#else
|
|
|
|
if(!MD4_Init(&ctx))
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
#endif
|
|
|
|
|
2015-02-03 12:00:54 +03:00
|
|
|
MD4_Update(&ctx, input, curlx_uztoui(len));
|
|
|
|
MD4_Final(output, &ctx);
|
2023-03-10 12:01:44 +03:00
|
|
|
return CURLE_OK;
|
2010-06-27 05:38:01 +04:00
|
|
|
}
|
2019-04-13 23:47:56 +03:00
|
|
|
|
2023-05-11 10:06:33 +03:00
|
|
|
#endif /* USE_CURL_NTLM_CORE */
|