From: Viktor Szakats Date: Fri, 24 Oct 2025 00:28:46 +0000 (+0200) Subject: des: merge curl_des into `curl_ntlm_core.c` X-Git-Tag: curl-8_17_0~114 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c96b7c4636b03c0a9c4e8303852553c4217446ee;p=thirdparty%2Fcurl.git des: merge curl_des into `curl_ntlm_core.c` `curl_des.c` contained a single, short, function `Curl_des_set_odd_parity()`, called from `curl_ntlm_core.c` alone. Move it there, and define it only when needed. Follow-up to 300876a7a62ff598c3be359e45a00b79cf9944ad Follow-up to 8cc70db2db5f58e519a1bdfed266ca6514013145 Closes #19209 --- diff --git a/.github/labeler.yml b/.github/labeler.yml index 770b293e3f..75ea8686da 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -162,9 +162,9 @@ cryptography: docs/libcurl/opts/CURLOPT_EGDSOCKET*,\ lib/*sha256*,\ lib/*sha512*,\ - lib/curl_des.*,\ lib/curl_hmac.*,\ lib/curl_md?.*,\ + lib/curl_ntlm_core.*,\ lib/md?.*,\ lib/rand.*\ }" diff --git a/lib/Makefile.inc b/lib/Makefile.inc index f06af2ca70..8b506febcf 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -160,7 +160,6 @@ LIB_CFILES = \ cookie.c \ cshutdn.c \ curl_addrinfo.c \ - curl_des.c \ curl_endian.c \ curl_fnmatch.c \ curl_fopen.c \ @@ -290,7 +289,6 @@ LIB_HFILES = \ cookie.h \ curl_addrinfo.h \ curl_ctype.h \ - curl_des.h \ curl_endian.h \ curl_fnmatch.h \ curl_fopen.h \ diff --git a/lib/curl_des.c b/lib/curl_des.c deleted file mode 100644 index a202dd3fe4..0000000000 --- a/lib/curl_des.c +++ /dev/null @@ -1,68 +0,0 @@ -/*************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ - * \___|\___/|_| \_\_____| - * - * Copyright (C) Steve Holme, . - * - * This software is licensed as described in the file COPYING, which - * you should have received as part of this distribution. The terms - * are also available at https://curl.se/docs/copyright.html. - * - * 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 - * - ***************************************************************************/ - -#include "curl_setup.h" - -#if defined(USE_CURL_NTLM_CORE) && \ - (defined(USE_GNUTLS) || \ - defined(USE_OS400CRYPTO) || \ - defined(USE_WIN32_CRYPTO)) - -#include "curl_des.h" - -/* - * Curl_des_set_odd_parity() - * - * This is used to apply odd parity to the given byte array. It is typically - * used by when a cryptography engine does not have its own version. - * - * The function is a port of the Java based oddParity() function over at: - * - * https://davenport.sourceforge.net/ntlm.html - * - * Parameters: - * - * bytes [in/out] - The data whose parity bits are to be adjusted for - * odd parity. - * len [out] - The length of the data. - */ -void Curl_des_set_odd_parity(unsigned char *bytes, size_t len) -{ - size_t i; - - for(i = 0; i < len; i++) { - unsigned char b = bytes[i]; - - bool needs_parity = (((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ - (b >> 4) ^ (b >> 3) ^ (b >> 2) ^ - (b >> 1)) & 0x01) == 0; - - if(needs_parity) - bytes[i] |= 0x01; - else - bytes[i] &= 0xfe; - } -} - -#endif diff --git a/lib/curl_des.h b/lib/curl_des.h deleted file mode 100644 index c50aaf45b1..0000000000 --- a/lib/curl_des.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef HEADER_CURL_DES_H -#define HEADER_CURL_DES_H -/*************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ - * \___|\___/|_| \_\_____| - * - * Copyright (C) Steve Holme, . - * - * This software is licensed as described in the file COPYING, which - * you should have received as part of this distribution. The terms - * are also available at https://curl.se/docs/copyright.html. - * - * 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 - * - ***************************************************************************/ - -#include "curl_setup.h" - -#if defined(USE_CURL_NTLM_CORE) && \ - (defined(USE_GNUTLS) || \ - defined(USE_OS400CRYPTO) || \ - defined(USE_WIN32_CRYPTO)) - -/* Applies odd parity to the given byte array */ -void Curl_des_set_odd_parity(unsigned char *bytes, size_t length); - -#endif - -#endif /* HEADER_CURL_DES_H */ diff --git a/lib/curl_ntlm_core.c b/lib/curl_ntlm_core.c index be273f0c90..72a0f24b77 100644 --- a/lib/curl_ntlm_core.c +++ b/lib/curl_ntlm_core.c @@ -99,6 +99,7 @@ #elif defined(USE_GNUTLS) # include +# define USE_CURL_DES_SET_ODD_PARITY #elif defined(USE_MBEDTLS_DES) @@ -106,8 +107,10 @@ #elif defined(USE_OS400CRYPTO) # include "cipher.mih" /* mih/cipher */ +# define USE_CURL_DES_SET_ODD_PARITY #elif defined(USE_WIN32_CRYPTO) # include +# define USE_CURL_DES_SET_ODD_PARITY #else # error "cannot compile NTLM support without a crypto library with DES." #endif @@ -119,13 +122,50 @@ #include "curl_hmac.h" #include "curlx/warnless.h" #include "curl_endian.h" -#include "curl_des.h" #include "curl_md4.h" /* The last 2 #include files should be in this order */ #include "curl_memory.h" #include "memdebug.h" +#ifdef USE_CURL_DES_SET_ODD_PARITY +/* + * curl_des_set_odd_parity() + * + * Copyright (C) Steve Holme, + * + * This is used to apply odd parity to the given byte array. It is typically + * used by when a cryptography engine does not have its own version. + * + * The function is a port of the Java based oddParity() function over at: + * + * https://davenport.sourceforge.net/ntlm.html + * + * Parameters: + * + * bytes [in/out] - The data whose parity bits are to be adjusted for + * odd parity. + * len [out] - The length of the data. + */ +static void curl_des_set_odd_parity(unsigned char *bytes, size_t len) +{ + size_t i; + + for(i = 0; i < len; i++) { + unsigned char b = bytes[i]; + + bool needs_parity = (((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ + (b >> 4) ^ (b >> 3) ^ (b >> 2) ^ + (b >> 1)) & 0x01) == 0; + + if(needs_parity) + bytes[i] |= 0x01; + else + bytes[i] &= 0xfe; + } +} +#endif /* USE_CURL_DES_SET_ODD_PARITY */ + /* * Turns a 56-bit key into being 64-bit wide. */ @@ -172,7 +212,7 @@ static void setup_des_key(const unsigned char *key_56, extend_key_56_to_64(key_56, key); /* Set the key parity to odd */ - Curl_des_set_odd_parity((unsigned char *) key, sizeof(key)); + curl_des_set_odd_parity((unsigned char *) key, sizeof(key)); /* Set the key */ des_set_key(des, (const uint8_t *) key); @@ -214,7 +254,7 @@ static bool encrypt_des(const unsigned char *in, unsigned char *out, extend_key_56_to_64(key_56, ctl.Crypto_Key); /* Set the key parity to odd */ - Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len); + curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len); /* Perform the encryption */ _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in); @@ -252,7 +292,7 @@ static bool encrypt_des(const unsigned char *in, unsigned char *out, extend_key_56_to_64(key_56, blob.key); /* Set the key parity to odd */ - Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key)); + curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key)); /* Import the key */ if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {