From c893e866bd7d929013a4b86005da5ff534f42964 Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Fri, 10 Jul 2026 14:01:29 +0300 Subject: [PATCH] Use Solar Designer's md4/md5 on non-Windows --- CMakeLists.txt | 12 +- Makefile.FreeBSD | 1 + Makefile.Linux | 1 + Makefile.Solaris | 1 + Makefile.unix | 1 + src/3proxy_crypt.c | 39 ------ src/Makefile.inc | 14 ++- src/authradius.c | 7 -- src/libs/md4.c | 268 ++++++++++++++++++++++++++++++++++++++++++ src/libs/md4.h | 47 ++++++++ src/libs/md5.c | 287 +++++++++++++++++++++++++++++++++++++++++++++ src/libs/md5.h | 43 +++++++ src/mdhash.c | 53 +++++---- src/proxy.h | 2 - src/ssllib.c | 17 +-- 15 files changed, 692 insertions(+), 101 deletions(-) create mode 100644 src/libs/md4.c create mode 100644 src/libs/md4.h create mode 100644 src/libs/md5.c create mode 100644 src/libs/md5.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a573048..25bcd76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -350,17 +350,13 @@ target_include_directories(common_obj PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) add_library(base64_obj OBJECT src/base64.c) target_include_directories(base64_obj PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) -# mdhash object library (MD4/MD5: CAPI on Windows, OpenSSL EVP elsewhere) -add_library(mdhash_obj OBJECT src/mdhash.c) -target_include_directories(mdhash_obj PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) +# mdhash object library (MD4/MD5: CAPI on Windows, bundled Solar Designer MD4/MD5 elsewhere) if(WIN32) - # CAPI lives in advapi32 (already in WINDOWS_LIBS); no extra include needed. + add_library(mdhash_obj OBJECT src/mdhash.c) else() - if(OpenSSL_FOUND) - target_include_directories(mdhash_obj PRIVATE ${OPENSSL_INCLUDE_DIR}) - target_compile_definitions(mdhash_obj PRIVATE WITH_SSL) - endif() + add_library(mdhash_obj OBJECT src/mdhash.c src/libs/md4.c src/libs/md5.c) endif() +target_include_directories(mdhash_obj PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) # ============================================================================ # Object libraries for 3proxy (compiled WITHOUT WITHMAIN) diff --git a/Makefile.FreeBSD b/Makefile.FreeBSD index 6f61c9e..a8c461d 100644 --- a/Makefile.FreeBSD +++ b/Makefile.FreeBSD @@ -66,6 +66,7 @@ ifeq ($(PAM_CHECK), true) PLUGINS += PamAuth endif +BUNDLED_HASH_OBJS = md4$(OBJSUFFICS) md5$(OBJSUFFICS) include Makefile.inc DESTDIR ?= diff --git a/Makefile.Linux b/Makefile.Linux index 50ba426..0578ccf 100644 --- a/Makefile.Linux +++ b/Makefile.Linux @@ -69,6 +69,7 @@ PAM_CHECK ?= $(shell echo "\#include \\n int main(){return ifeq ($(PAM_CHECK), true) PLUGINS += PamAuth endif +BUNDLED_HASH_OBJS = md4$(OBJSUFFICS) md5$(OBJSUFFICS) include Makefile.inc allplugins: diff --git a/Makefile.Solaris b/Makefile.Solaris index bbb611b..3d68d4e 100644 --- a/Makefile.Solaris +++ b/Makefile.Solaris @@ -44,6 +44,7 @@ ifeq ($(PCRE_CHECK), true) PCRE_LIBS = -Wl,-Bstatic -lpcre2-8 -Wl,-Bdynamic endif +BUNDLED_HASH_OBJS = md4$(OBJSUFFICS) md5$(OBJSUFFICS) include Makefile.inc allplugins: diff --git a/Makefile.unix b/Makefile.unix index 937c031..311a851 100644 --- a/Makefile.unix +++ b/Makefile.unix @@ -68,6 +68,7 @@ ifeq ($(PAM_CHECK), true) PLUGINS += PamAuth endif +BUNDLED_HASH_OBJS = md4$(OBJSUFFICS) md5$(OBJSUFFICS) include Makefile.inc DESTDIR ?= diff --git a/src/3proxy_crypt.c b/src/3proxy_crypt.c index 79ae11d..aa5356f 100644 --- a/src/3proxy_crypt.c +++ b/src/3proxy_crypt.c @@ -7,13 +7,6 @@ */ #include "libs/blake2.h" #include "mdhash.h" -#ifdef WITH_SSL -#include -#if OPENSSL_VERSION_NUMBER >= 0x30000000L -#include -#include -#endif -#endif #include #define MD5_SIZE 16 @@ -29,11 +22,6 @@ static unsigned char itoa64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; -#if defined(WITH_SSL) && !defined(_WIN32) && OPENSSL_VERSION_NUMBER >= 0x30000000L -EVP_MD *md4_hash = NULL; -EVP_MD *md5_hash = NULL; -#endif - void _crypt_to64(unsigned char *s, unsigned long v, int n) { @@ -44,7 +32,6 @@ _crypt_to64(unsigned char *s, unsigned long v, int n) } -#if defined(WITH_SSL) || defined(_WIN32) unsigned char * ntpwdhash (unsigned char *szHash, const unsigned char *szPassword, int ctohex) { unsigned char szUnicodePass[513]; @@ -80,7 +67,6 @@ unsigned char * ntpwdhash (unsigned char *szHash, const unsigned char *szPasswor memset(szUnicodePass, 0, sizeof szUnicodePass); return szHash; } -#endif unsigned char * mycrypt(const unsigned char *pw, const unsigned char *salt, unsigned char *passwd){ @@ -93,7 +79,6 @@ unsigned char * mycrypt(const unsigned char *pw, const unsigned char *salt, unsi int sl; unsigned long l; -#if defined(WITH_SSL) || defined(_WIN32) #ifndef WITHMAIN if(salt[0] == '$' && salt[1] == '1' && salt[2] == '$' && (ep = (unsigned char *)strchr((char *)salt+3, '$'))) { mdh_ctx *ctx, *ctx1; @@ -176,7 +161,6 @@ unsigned char * mycrypt(const unsigned char *pw, const unsigned char *salt, unsi mdh_free(ctx1); } else -#endif #endif if(salt[0] == '$' && salt[1] == '3' && salt[2] == '$' && (ep = (unsigned char *)strchr((char *)salt+3, '$'))) { sp = salt +3; @@ -220,9 +204,6 @@ unsigned char * mycrypt(const unsigned char *pw, const unsigned char *salt, unsi } #ifdef WITHMAIN -#if defined(WITH_SSL) && OPENSSL_VERSION_NUMBER >= 0x30000000L -#include -#endif #include int main(int argc, char* argv[]){ unsigned char buf1[128]; @@ -230,37 +211,17 @@ int main(int argc, char* argv[]){ unsigned i; if(argc < 2 || argc > 3) { fprintf(stderr, "usage: \n" -#if defined(WITH_SSL) || defined(_WIN32) "\t%s \n" -#endif "\t%s \n" -#if defined(WITH_SSL) || defined(_WIN32) "Performs NT crypt if no salt specified, BLAKE2 crypt with salt\n" -#else - "Performs BLAKE2 crypt with salt\n" -#endif , -#if defined(WITH_SSL) || defined(_WIN32) argv[0], -#endif argv[0]); return 1; } -#if defined(WITH_SSL) && !defined(_WIN32) && OPENSSL_VERSION_NUMBER >= 0x30000000L - OSSL_PROVIDER_load(NULL, "legacy"); - OSSL_PROVIDER_load(NULL, "default"); - md4_hash = EVP_MD_fetch(NULL, "MD4", NULL); - if (md4_hash == NULL) { - fprintf(stderr, "Error fetching MD4\n"); - } -#endif if(argc == 2) { -#if defined(WITH_SSL) || defined(_WIN32) { unsigned char *nt = ntpwdhash(buf1, (unsigned char *)argv[1], 1); if(nt) printf("NT:%s\n", nt); } -#else - fprintf(stderr, "NT crypt not available (compiled without OpenSSL)\n"); -#endif } else { unsigned char *cr; diff --git a/src/Makefile.inc b/src/Makefile.inc index 8acfc79..f8b10d8 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -158,8 +158,14 @@ blake2$(OBJSUFFICS): libs/blake2b-ref.c mdhash$(OBJSUFFICS): mdhash.c mdhash.h proxy.h structures.h $(CC) $(COUT)mdhash$(OBJSUFFICS) $(CFLAGS) mdhash.c -$(BUILDDIR)$(CRYPT_PREFIX)crypt$(EXESUFFICS): blake2$(OBJSUFFICS) 3proxy_cryptmain$(OBJSUFFICS) base64$(OBJSUFFICS) mdhash$(OBJSUFFICS) - $(LN) $(LNOUT)$(BUILDDIR)$(CRYPT_PREFIX)crypt$(EXESUFFICS) $(LDFLAGS) blake2$(OBJSUFFICS) base64$(OBJSUFFICS) mdhash$(OBJSUFFICS) 3proxy_cryptmain$(OBJSUFFICS) $(LIBS) +md4$(OBJSUFFICS): libs/md4.c libs/md4.h + $(CC) $(COUT)md4$(OBJSUFFICS) $(CFLAGS) libs/md4.c + +md5$(OBJSUFFICS): libs/md5.c libs/md5.h + $(CC) $(COUT)md5$(OBJSUFFICS) $(CFLAGS) libs/md5.c + +$(BUILDDIR)$(CRYPT_PREFIX)crypt$(EXESUFFICS): blake2$(OBJSUFFICS) 3proxy_cryptmain$(OBJSUFFICS) base64$(OBJSUFFICS) mdhash$(OBJSUFFICS) $(BUNDLED_HASH_OBJS) + $(LN) $(LNOUT)$(BUILDDIR)$(CRYPT_PREFIX)crypt$(EXESUFFICS) $(LDFLAGS) blake2$(OBJSUFFICS) base64$(OBJSUFFICS) mdhash$(OBJSUFFICS) $(BUNDLED_HASH_OBJS) 3proxy_cryptmain$(OBJSUFFICS) $(LIBS) stringtable$(OBJSUFFICS): stringtable.c $(CC) $(COUT)stringtable$(OBJSUFFICS) $(CFLAGS) stringtable.c @@ -173,6 +179,6 @@ ssl$(OBJSUFFICS): ssl.c structures.h proxy.h ssl.h pcre$(OBJSUFFICS): pcre.c structures.h $(CC) $(COUT)pcre$(OBJSUFFICS) $(CFLAGS) $(DEFINEOPTION)WITH_PCRE pcre.c -$(BUILDDIR)3proxy$(EXESUFFICS): 3proxy$(OBJSUFFICS) mainfunc$(OBJSUFFICS) srvproxy$(OBJSUFFICS) srvpop3p$(OBJSUFFICS) srvsmtpp$(OBJSUFFICS) srvftppr$(OBJSUFFICS) srvsocks$(OBJSUFFICS) srvtcppm$(OBJSUFFICS) srvtlspr$(OBJSUFFICS) srvauto$(OBJSUFFICS) srvudppm$(OBJSUFFICS) sockmap$(OBJSUFFICS) udpsockmap$(OBJSUFFICS) sockgetchar$(OBJSUFFICS) common$(OBJSUFFICS) auth$(OBJSUFFICS) acl$(OBJSUFFICS) limiter$(OBJSUFFICS) redirect$(OBJSUFFICS) authradius$(OBJSUFFICS) hash$(OBJSUFFICS) hashtables$(OBJSUFFICS) resolve$(OBJSUFFICS) sql$(OBJSUFFICS) conf$(OBJSUFFICS) log$(OBJSUFFICS) datatypes$(OBJSUFFICS) blake2$(OBJSUFFICS) 3proxy_crypt$(OBJSUFFICS) base64$(OBJSUFFICS) ftp$(OBJSUFFICS) stringtable$(OBJSUFFICS) srvwebadmin$(OBJSUFFICS) srvdnspr$(OBJSUFFICS) plugins$(OBJSUFFICS) mdhash$(OBJSUFFICS) $(SSL_OBJS) $(PCRE_OBJS) $(COMPATLIBS) $(VERSIONDEP) - $(LN) $(LNOUT)$(BUILDDIR)3proxy$(EXESUFFICS) $(LDFLAGS) $(VERFILE) 3proxy$(OBJSUFFICS) mainfunc$(OBJSUFFICS) auth$(OBJSUFFICS) acl$(OBJSUFFICS) limiter$(OBJSUFFICS) redirect$(OBJSUFFICS) authradius$(OBJSUFFICS) hash$(OBJSUFFICS) hashtables$(OBJSUFFICS) resolve$(OBJSUFFICS) sql$(OBJSUFFICS) conf$(OBJSUFFICS) datatypes$(OBJSUFFICS) srvauto$(OBJSUFFICS) srvproxy$(OBJSUFFICS) srvpop3p$(OBJSUFFICS) srvsmtpp$(OBJSUFFICS) srvftppr$(OBJSUFFICS) srvsocks$(OBJSUFFICS) srvtcppm$(OBJSUFFICS) srvtlspr$(OBJSUFFICS) srvudppm$(OBJSUFFICS) sockmap$(OBJSUFFICS) udpsockmap$(OBJSUFFICS) sockgetchar$(OBJSUFFICS) common$(OBJSUFFICS) log$(OBJSUFFICS) 3proxy_crypt$(OBJSUFFICS) blake2$(OBJSUFFICS) base64$(OBJSUFFICS) ftp$(OBJSUFFICS) stringtable$(OBJSUFFICS) srvwebadmin$(OBJSUFFICS) srvdnspr$(OBJSUFFICS) plugins$(OBJSUFFICS) mdhash$(OBJSUFFICS) $(SSL_OBJS) $(PCRE_OBJS) $(COMPATLIBS) $(LIBS) $(PCRE_LIBS) +$(BUILDDIR)3proxy$(EXESUFFICS): 3proxy$(OBJSUFFICS) mainfunc$(OBJSUFFICS) srvproxy$(OBJSUFFICS) srvpop3p$(OBJSUFFICS) srvsmtpp$(OBJSUFFICS) srvftppr$(OBJSUFFICS) srvsocks$(OBJSUFFICS) srvtcppm$(OBJSUFFICS) srvtlspr$(OBJSUFFICS) srvauto$(OBJSUFFICS) srvudppm$(OBJSUFFICS) sockmap$(OBJSUFFICS) udpsockmap$(OBJSUFFICS) sockgetchar$(OBJSUFFICS) common$(OBJSUFFICS) auth$(OBJSUFFICS) acl$(OBJSUFFICS) limiter$(OBJSUFFICS) redirect$(OBJSUFFICS) authradius$(OBJSUFFICS) hash$(OBJSUFFICS) hashtables$(OBJSUFFICS) resolve$(OBJSUFFICS) sql$(OBJSUFFICS) conf$(OBJSUFFICS) log$(OBJSUFFICS) datatypes$(OBJSUFFICS) blake2$(OBJSUFFICS) 3proxy_crypt$(OBJSUFFICS) base64$(OBJSUFFICS) ftp$(OBJSUFFICS) stringtable$(OBJSUFFICS) srvwebadmin$(OBJSUFFICS) srvdnspr$(OBJSUFFICS) plugins$(OBJSUFFICS) mdhash$(OBJSUFFICS) $(BUNDLED_HASH_OBJS) $(SSL_OBJS) $(PCRE_OBJS) $(COMPATLIBS) $(VERSIONDEP) + $(LN) $(LNOUT)$(BUILDDIR)3proxy$(EXESUFFICS) $(LDFLAGS) $(VERFILE) 3proxy$(OBJSUFFICS) mainfunc$(OBJSUFFICS) auth$(OBJSUFFICS) acl$(OBJSUFFICS) limiter$(OBJSUFFICS) redirect$(OBJSUFFICS) authradius$(OBJSUFFICS) hash$(OBJSUFFICS) hashtables$(OBJSUFFICS) resolve$(OBJSUFFICS) sql$(OBJSUFFICS) conf$(OBJSUFFICS) datatypes$(OBJSUFFICS) srvauto$(OBJSUFFICS) srvproxy$(OBJSUFFICS) srvpop3p$(OBJSUFFICS) srvsmtpp$(OBJSUFFICS) srvftppr$(OBJSUFFICS) srvsocks$(OBJSUFFICS) srvtcppm$(OBJSUFFICS) srvtlspr$(OBJSUFFICS) srvudppm$(OBJSUFFICS) sockmap$(OBJSUFFICS) udpsockmap$(OBJSUFFICS) sockgetchar$(OBJSUFFICS) common$(OBJSUFFICS) log$(OBJSUFFICS) 3proxy_crypt$(OBJSUFFICS) blake2$(OBJSUFFICS) base64$(OBJSUFFICS) ftp$(OBJSUFFICS) stringtable$(OBJSUFFICS) srvwebadmin$(OBJSUFFICS) srvdnspr$(OBJSUFFICS) plugins$(OBJSUFFICS) mdhash$(OBJSUFFICS) $(BUNDLED_HASH_OBJS) $(SSL_OBJS) $(PCRE_OBJS) $(COMPATLIBS) $(LIBS) $(PCRE_LIBS) diff --git a/src/authradius.c b/src/authradius.c index a784f13..4feadf7 100644 --- a/src/authradius.c +++ b/src/authradius.c @@ -9,7 +9,6 @@ #ifndef NORADIUS #include "proxy.h" #include "mdhash.h" -#include #define AUTH_VECTOR_LEN 16 #define MAX_STRING_LEN 254 @@ -186,12 +185,6 @@ char *strNcpy(char *dest, const char *src, int n) return dest; } -#if !defined(_WIN32) && OPENSSL_VERSION_NUMBER >= 0x30000000L -extern EVP_MD *md4_hash; -extern EVP_MD *md5_hash; -#endif - - void md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen) { diff --git a/src/libs/md4.c b/src/libs/md4.c new file mode 100644 index 0000000..e952081 --- /dev/null +++ b/src/libs/md4.c @@ -0,0 +1,268 @@ +/* + * This is an OpenSSL API compatible (but not ABI compatible) implementation + * of the RSA Data Security, Inc. MD4 Message-Digest Algorithm (RFC 1320). + * + * Homepage: + * https://openwall.info/wiki/people/solar/software/public-domain-source-code/md4 + * + * Author: + * Alexander Peslyak, better known as Solar Designer + * + * 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 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: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * (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 + * unsigned integer data type will do), there's no compile-time endianness + * configuration, and the function prototypes match OpenSSL's. No code from + * 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. + * It is meant to be fast, but not as fast as possible. Some known + * optimizations are not included to reduce source code size and avoid + * compile-time configuration. + */ + +#include + +#include "md4.h" + +/* + * 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. + */ +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define G(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) + +/* + * The MD4 transformation for all three rounds. + */ +#define STEP(f, a, b, c, d, x, s) \ + (a) += f((b), (c), (d)) + (x); \ + (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); + +/* + * 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 memory + * accesses is just an optimization. Nothing will break if it fails to detect + * a suitable architecture. + * + * Unfortunately, this optimization may be a C strict aliasing rules violation + * if the caller's data buffer has effective type that cannot be aliased by + * MD4_u32plus. In practice, this problem may occur if these MD4 routines are + * inlined into a calling function, or with future and dangerously advanced + * link-time optimizations. For the time being, keeping these MD4 routines in + * their own translation unit avoids the problem. + */ +#if defined(__i386__) || defined(__x86_64__) || defined(__vax__) +#define SET(n) \ + (*(MD4_u32plus *)&ptr[(n) * 4]) +#define GET(n) \ + SET(n) +#else +#define SET(n) \ + (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)) +#define GET(n) \ + (ctx->block[(n)]) +#endif + +/* + * This processes one or more 64-byte data blocks, but does NOT update the bit + * counters. There are no alignment requirements. + */ +static const void *body(MD4_CTX *ctx, const void *data, size_t size) +{ + const unsigned char *ptr; + MD4_u32plus a, b, c, d; + MD4_u32plus saved_a, saved_b, saved_c, saved_d; + const MD4_u32plus ac1 = 0x5a827999, ac2 = 0x6ed9eba1; + + ptr = (const unsigned char *)data; + + a = ctx->a; + b = ctx->b; + c = ctx->c; + d = ctx->d; + + do { + saved_a = a; + saved_b = b; + saved_c = c; + saved_d = d; + +/* Round 1 */ + STEP(F, a, b, c, d, SET(0), 3) + STEP(F, d, a, b, c, SET(1), 7) + STEP(F, c, d, a, b, SET(2), 11) + STEP(F, b, c, d, a, SET(3), 19) + STEP(F, a, b, c, d, SET(4), 3) + STEP(F, d, a, b, c, SET(5), 7) + STEP(F, c, d, a, b, SET(6), 11) + STEP(F, b, c, d, a, SET(7), 19) + STEP(F, a, b, c, d, SET(8), 3) + STEP(F, d, a, b, c, SET(9), 7) + STEP(F, c, d, a, b, SET(10), 11) + STEP(F, b, c, d, a, SET(11), 19) + STEP(F, a, b, c, d, SET(12), 3) + STEP(F, d, a, b, c, SET(13), 7) + STEP(F, c, d, a, b, SET(14), 11) + STEP(F, b, c, d, a, SET(15), 19) + +/* Round 2 */ + STEP(G, a, b, c, d, GET(0) + ac1, 3) + STEP(G, d, a, b, c, GET(4) + ac1, 5) + STEP(G, c, d, a, b, GET(8) + ac1, 9) + STEP(G, b, c, d, a, GET(12) + ac1, 13) + STEP(G, a, b, c, d, GET(1) + ac1, 3) + STEP(G, d, a, b, c, GET(5) + ac1, 5) + STEP(G, c, d, a, b, GET(9) + ac1, 9) + STEP(G, b, c, d, a, GET(13) + ac1, 13) + STEP(G, a, b, c, d, GET(2) + ac1, 3) + STEP(G, d, a, b, c, GET(6) + ac1, 5) + STEP(G, c, d, a, b, GET(10) + ac1, 9) + STEP(G, b, c, d, a, GET(14) + ac1, 13) + STEP(G, a, b, c, d, GET(3) + ac1, 3) + STEP(G, d, a, b, c, GET(7) + ac1, 5) + STEP(G, c, d, a, b, GET(11) + ac1, 9) + STEP(G, b, c, d, a, GET(15) + ac1, 13) + +/* Round 3 */ + STEP(H, a, b, c, d, GET(0) + ac2, 3) + STEP(H, d, a, b, c, GET(8) + ac2, 9) + STEP(H, c, d, a, b, GET(4) + ac2, 11) + STEP(H, b, c, d, a, GET(12) + ac2, 15) + STEP(H, a, b, c, d, GET(2) + ac2, 3) + STEP(H, d, a, b, c, GET(10) + ac2, 9) + STEP(H, c, d, a, b, GET(6) + ac2, 11) + STEP(H, b, c, d, a, GET(14) + ac2, 15) + STEP(H, a, b, c, d, GET(1) + ac2, 3) + STEP(H, d, a, b, c, GET(9) + ac2, 9) + STEP(H, c, d, a, b, GET(5) + ac2, 11) + STEP(H, b, c, d, a, GET(13) + ac2, 15) + STEP(H, a, b, c, d, GET(3) + ac2, 3) + STEP(H, d, a, b, c, GET(11) + ac2, 9) + STEP(H, c, d, a, b, GET(7) + ac2, 11) + STEP(H, b, c, d, a, GET(15) + ac2, 15) + + a += saved_a; + b += saved_b; + c += saved_c; + d += saved_d; + + ptr += 64; + } while (size -= 64); + + ctx->a = a; + ctx->b = b; + ctx->c = c; + ctx->d = d; + + return ptr; +} + +void MD4_Init(MD4_CTX *ctx) +{ + ctx->a = 0x67452301; + ctx->b = 0xefcdab89; + ctx->c = 0x98badcfe; + ctx->d = 0x10325476; + + ctx->lo = 0; + ctx->hi = 0; +} + +void MD4_Update(MD4_CTX *ctx, const void *data, size_t size) +{ + MD4_u32plus saved_lo; + size_t used, available; + + saved_lo = ctx->lo; + if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) + ctx->hi++; + ctx->hi += (MD4_u32plus)(size >> 29); + + used = saved_lo & 0x3f; + + if (used) { + available = 64 - used; + + if (size < available) { + memcpy(&ctx->buffer[used], data, size); + return; + } + + memcpy(&ctx->buffer[used], data, available); + data = (const unsigned char *)data + available; + size -= available; + body(ctx, ctx->buffer, 64); + } + + if (size >= 64) { + data = body(ctx, data, size & ~(size_t)0x3f); + size &= 0x3f; + } + + memcpy(ctx->buffer, data, size); +} + +#define OUT(dst, src) \ + (dst)[0] = (unsigned char)(src); \ + (dst)[1] = (unsigned char)((src) >> 8); \ + (dst)[2] = (unsigned char)((src) >> 16); \ + (dst)[3] = (unsigned char)((src) >> 24); + +void MD4_Final(unsigned char *result, MD4_CTX *ctx) +{ + size_t used, available; + + used = ctx->lo & 0x3f; + + ctx->buffer[used++] = 0x80; + + available = 64 - used; + + if (available < 8) { + memset(&ctx->buffer[used], 0, available); + body(ctx, ctx->buffer, 64); + used = 0; + available = 64; + } + + memset(&ctx->buffer[used], 0, available - 8); + + ctx->lo <<= 3; + OUT(&ctx->buffer[56], ctx->lo) + OUT(&ctx->buffer[60], ctx->hi) + + body(ctx, ctx->buffer, 64); + + OUT(&result[0], ctx->a) + OUT(&result[4], ctx->b) + OUT(&result[8], ctx->c) + OUT(&result[12], ctx->d) + +#if 0 + memset(ctx, 0, sizeof(*ctx)); +#endif +} diff --git a/src/libs/md4.h b/src/libs/md4.h new file mode 100644 index 0000000..7bc9ee4 --- /dev/null +++ b/src/libs/md4.h @@ -0,0 +1,47 @@ +/* + * This is an OpenSSL API compatible (but not ABI compatible) implementation + * of the RSA Data Security, Inc. MD4 Message-Digest Algorithm (RFC 1320). + * + * Homepage: + * https://openwall.info/wiki/people/solar/software/public-domain-source-code/md4 + * + * Author: + * Alexander Peslyak, better known as Solar Designer + * + * 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 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: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * See md4.c for more information. + */ + +#ifndef _MD4_H +#define _MD4_H + +#include /* for size_t */ + +/* Any 32-bit or wider unsigned integer data type will do */ +typedef unsigned int MD4_u32plus; + +typedef struct { + MD4_u32plus lo, hi; + MD4_u32plus a, b, c, d; + unsigned char buffer[64]; +#if !(defined(__i386__) || defined(__x86_64__) || defined(__vax__)) + MD4_u32plus block[16]; +#endif +} MD4_CTX; + +extern void MD4_Init(MD4_CTX *ctx); +extern void MD4_Update(MD4_CTX *ctx, const void *data, size_t size); +extern void MD4_Final(unsigned char *result, MD4_CTX *ctx); + +#endif diff --git a/src/libs/md5.c b/src/libs/md5.c new file mode 100644 index 0000000..a6a7ed6 --- /dev/null +++ b/src/libs/md5.c @@ -0,0 +1,287 @@ +/* + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. + * MD5 Message-Digest Algorithm (RFC 1321). + * + * Homepage: + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 + * + * Author: + * Alexander Peslyak, better known as Solar Designer + * + * 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 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: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * (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 + * unsigned integer data type will do), there's no compile-time endianness + * configuration, and the function prototypes match OpenSSL's. No code from + * 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. + * It is meant to be fast, but not as fast as possible. Some known + * optimizations are not included to reduce source code size and avoid + * compile-time configuration. + */ + +#include + +#include "md5.h" + +/* + * The basic MD5 functions. + * + * F and G are optimized compared to their RFC 1321 definitions for + * architectures that lack an AND-NOT instruction, just like in Colin Plumb's + * implementation. + */ +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) +#define H(x, y, z) (((x) ^ (y)) ^ (z)) +#define H2(x, y, z) ((x) ^ ((y) ^ (z))) +#define I(x, y, z) ((y) ^ ((x) | ~(z))) + +/* + * The MD5 transformation for all four rounds. + */ +#define STEP(f, a, b, c, d, x, t, s) \ + (a) += f((b), (c), (d)) + (x) + (t); \ + (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ + (a) += (b); + +/* + * 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 memory + * accesses is just an optimization. Nothing will break if it fails to detect + * a suitable architecture. + * + * Unfortunately, this optimization may be a C strict aliasing rules violation + * if the caller's data buffer has effective type that cannot be aliased by + * MD5_u32plus. In practice, this problem may occur if these MD5 routines are + * inlined into a calling function, or with future and dangerously advanced + * link-time optimizations. For the time being, keeping these MD5 routines in + * their own translation unit avoids the problem. + */ +#if defined(__i386__) || defined(__x86_64__) || defined(__vax__) +#define SET(n) \ + (*(MD5_u32plus *)&ptr[(n) * 4]) +#define GET(n) \ + SET(n) +#else +#define SET(n) \ + (ctx->block[(n)] = \ + (MD5_u32plus)ptr[(n) * 4] | \ + ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \ + ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \ + ((MD5_u32plus)ptr[(n) * 4 + 3] << 24)) +#define GET(n) \ + (ctx->block[(n)]) +#endif + +/* + * This processes one or more 64-byte data blocks, but does NOT update the bit + * counters. There are no alignment requirements. + */ +static const void *body(MD5_CTX *ctx, const void *data, unsigned long size) +{ + const unsigned char *ptr; + MD5_u32plus a, b, c, d; + MD5_u32plus saved_a, saved_b, saved_c, saved_d; + + ptr = (const unsigned char *)data; + + a = ctx->a; + b = ctx->b; + c = ctx->c; + d = ctx->d; + + do { + saved_a = a; + saved_b = b; + saved_c = c; + saved_d = d; + +/* Round 1 */ + STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) + STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) + STEP(F, c, d, a, b, SET(2), 0x242070db, 17) + STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) + STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) + STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) + STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) + STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) + STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) + STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) + STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) + STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) + STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) + STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) + STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) + STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) + +/* Round 2 */ + STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) + STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) + STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) + STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) + STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) + STEP(G, d, a, b, c, GET(10), 0x02441453, 9) + STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) + STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) + STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) + STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) + STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) + STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) + STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) + STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) + STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) + STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) + +/* Round 3 */ + STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) + STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11) + STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) + STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23) + STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) + STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11) + STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) + STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23) + STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) + STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11) + STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) + STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23) + STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) + STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11) + STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) + STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23) + +/* Round 4 */ + STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) + STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) + STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) + STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) + STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) + STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) + STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) + STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) + STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) + STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) + STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) + STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) + STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) + STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) + STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) + STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) + + a += saved_a; + b += saved_b; + c += saved_c; + d += saved_d; + + ptr += 64; + } while (size -= 64); + + ctx->a = a; + ctx->b = b; + ctx->c = c; + ctx->d = d; + + return ptr; +} + +void MD5_Init(MD5_CTX *ctx) +{ + ctx->a = 0x67452301; + ctx->b = 0xefcdab89; + ctx->c = 0x98badcfe; + ctx->d = 0x10325476; + + ctx->lo = 0; + ctx->hi = 0; +} + +void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size) +{ + MD5_u32plus saved_lo; + unsigned long used, available; + + saved_lo = ctx->lo; + if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) + ctx->hi++; + ctx->hi += size >> 29; + + used = saved_lo & 0x3f; + + if (used) { + available = 64 - used; + + if (size < available) { + memcpy(&ctx->buffer[used], data, size); + return; + } + + memcpy(&ctx->buffer[used], data, available); + data = (const unsigned char *)data + available; + size -= available; + body(ctx, ctx->buffer, 64); + } + + if (size >= 64) { + data = body(ctx, data, size & ~(unsigned long)0x3f); + size &= 0x3f; + } + + memcpy(ctx->buffer, data, size); +} + +#define OUT(dst, src) \ + (dst)[0] = (unsigned char)(src); \ + (dst)[1] = (unsigned char)((src) >> 8); \ + (dst)[2] = (unsigned char)((src) >> 16); \ + (dst)[3] = (unsigned char)((src) >> 24); + +void MD5_Final(unsigned char *result, MD5_CTX *ctx) +{ + unsigned long used, available; + + used = ctx->lo & 0x3f; + + ctx->buffer[used++] = 0x80; + + available = 64 - used; + + if (available < 8) { + memset(&ctx->buffer[used], 0, available); + body(ctx, ctx->buffer, 64); + used = 0; + available = 64; + } + + memset(&ctx->buffer[used], 0, available - 8); + + ctx->lo <<= 3; + OUT(&ctx->buffer[56], ctx->lo) + OUT(&ctx->buffer[60], ctx->hi) + + body(ctx, ctx->buffer, 64); + + OUT(&result[0], ctx->a) + OUT(&result[4], ctx->b) + OUT(&result[8], ctx->c) + OUT(&result[12], ctx->d) + + memset(ctx, 0, sizeof(*ctx)); +} diff --git a/src/libs/md5.h b/src/libs/md5.h new file mode 100644 index 0000000..7faca7d --- /dev/null +++ b/src/libs/md5.h @@ -0,0 +1,43 @@ +/* + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. + * MD5 Message-Digest Algorithm (RFC 1321). + * + * Homepage: + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 + * + * Author: + * Alexander Peslyak, better known as Solar Designer + * + * 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 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: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * See md5.c for more information. + */ + +#ifndef _MD5_H +#define _MD5_H + +/* Any 32-bit or wider unsigned integer data type will do */ +typedef unsigned int MD5_u32plus; + +typedef struct { + MD5_u32plus lo, hi; + MD5_u32plus a, b, c, d; + unsigned char buffer[64]; + MD5_u32plus block[16]; +} MD5_CTX; + +extern void MD5_Init(MD5_CTX *ctx); +extern void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size); +extern void MD5_Final(unsigned char *result, MD5_CTX *ctx); + +#endif diff --git a/src/mdhash.c b/src/mdhash.c index 1a00a04..6b3f313 100644 --- a/src/mdhash.c +++ b/src/mdhash.c @@ -6,7 +6,7 @@ MD4/MD5 hash implementation. - Windows: CryptoAPI (CAPI). Supports both CALG_MD4 and CALG_MD5. - - Other platforms: OpenSSL EVP (requires WITH_SSL). + - Other platforms: bundled public-domain MD4/MD5 (Solar Designer, 2001). */ #include "mdhash.h" #include @@ -77,53 +77,56 @@ void mdh_free(mdh_ctx *c) #else /* !_WIN32 */ -#ifdef WITH_SSL -#include +#include "libs/md4.h" +#include "libs/md5.h" +#include struct mdh_ctx { - EVP_MD_CTX *ctx; + int alg; /* MDH_MD4 or MDH_MD5 */ + union { + MD4_CTX md4; + MD5_CTX md5; + } u; }; mdh_ctx *mdh_init(mdh_alg alg) { - mdh_ctx *c; - const EVP_MD *md; - - c = (mdh_ctx *)malloc(sizeof(mdh_ctx)); + mdh_ctx *c = (mdh_ctx *)malloc(sizeof(mdh_ctx)); if(!c) return NULL; - c->ctx = EVP_MD_CTX_new(); - if(!c->ctx) { free(c); return NULL; } - md = (alg == MDH_MD4) ? EVP_md4() : EVP_md5(); - if(!EVP_DigestInit_ex(c->ctx, md, NULL)) { - EVP_MD_CTX_free(c->ctx); - free(c); - return NULL; - } + c->alg = alg; + if(alg == MDH_MD4) + MD4_Init(&c->u.md4); + else + MD5_Init(&c->u.md5); return c; } int mdh_update(mdh_ctx *c, const void *data, unsigned int len) { - if(!c || !c->ctx) return 0; - return EVP_DigestUpdate(c->ctx, data, (size_t)len) ? 1 : 0; + if(!c) return 0; + if(c->alg == MDH_MD4) + MD4_Update(&c->u.md4, data, (size_t)len); + else + MD5_Update(&c->u.md5, data, (unsigned long)len); + return 1; } int mdh_final(mdh_ctx *c, unsigned char *out, unsigned int *outlen) { - unsigned int l = 0; - if(!c || !c->ctx || !outlen) return 0; - if(!EVP_DigestFinal_ex(c->ctx, out, &l)) return 0; - *outlen = l; + if(!c || !outlen) return 0; + if(c->alg == MDH_MD4) + MD4_Final(out, &c->u.md4); + else + MD5_Final(out, &c->u.md5); + *outlen = 16; return 1; } void mdh_free(mdh_ctx *c) { if(!c) return; - if(c->ctx) EVP_MD_CTX_free(c->ctx); + memset(c, 0, sizeof(*c)); free(c); } -#endif /* WITH_SSL */ - #endif /* _WIN32 */ diff --git a/src/proxy.h b/src/proxy.h index a02a205..e7525e7 100644 --- a/src/proxy.h +++ b/src/proxy.h @@ -240,9 +240,7 @@ extern int paused; extern int demon; unsigned char * mycrypt(const unsigned char *key, const unsigned char *salt, unsigned char *buf); -#if defined(WITH_SSL) || defined(_WIN32) unsigned char * ntpwdhash (unsigned char *szHash, const unsigned char *szPassword, int tohex); -#endif int de64 (const unsigned char *in, unsigned char *out, int maxlen); unsigned char* en64 (const unsigned char *in, unsigned char *out, int inlen); void tohex(unsigned char *in, unsigned char *out, int len); diff --git a/src/ssllib.c b/src/ssllib.c index d42530f..41fa0ca 100644 --- a/src/ssllib.c +++ b/src/ssllib.c @@ -280,12 +280,6 @@ int ssl_file_init = 0; int ssl_init_done = 0; -#if !defined(_WIN32) && OPENSSL_VERSION_NUMBER >= 0x30000000L -extern EVP_MD *md4_hash; -extern EVP_MD *md5_hash; -#endif - - void ssl_init() { if(!ssl_init_done){ @@ -296,17 +290,8 @@ void ssl_init() SSL_load_error_strings(); _3proxy_mutex_init(&ssl_file_mutex); bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); -#if !defined(_WIN32) && OPENSSL_VERSION_NUMBER >= 0x30000000L - OSSL_PROVIDER_load(NULL, "legacy"); +#if OPENSSL_VERSION_NUMBER >= 0x30000000L OSSL_PROVIDER_load(NULL, "default"); - md4_hash = EVP_MD_fetch(NULL, "MD4", NULL); - if (md4_hash == NULL) { - fprintf(stderr, "Error fetching MD4\n"); - } - md5_hash = EVP_MD_fetch(NULL, "MD5", NULL); - if (md5_hash == NULL) { - fprintf(stderr, "Error fetching MD5\n"); - } #endif } }