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 300876a7a6
Follow-up to 8cc70db2db

Closes #19209
This commit is contained in:
Viktor Szakats 2025-10-24 02:28:46 +02:00
parent 4a6fbd5e1d
commit c96b7c4636
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
5 changed files with 45 additions and 114 deletions

2
.github/labeler.yml vendored
View File

@ -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.*\
}"

View File

@ -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 \

View File

@ -1,68 +0,0 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Steve Holme, <steve_holme@hotmail.com>.
*
* 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

View File

@ -1,39 +0,0 @@
#ifndef HEADER_CURL_DES_H
#define HEADER_CURL_DES_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Steve Holme, <steve_holme@hotmail.com>.
*
* 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 */

View File

@ -99,6 +99,7 @@
#elif defined(USE_GNUTLS)
# include <nettle/des.h>
# 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 <wincrypt.h>
# 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, <steve_holme@hotmail.com>
*
* 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)) {