1 | /*************************************************************************** |
2 | * _ _ ____ _ |
3 | * Project ___| | | | _ \| | |
4 | * / __| | | | |_) | | |
5 | * | (__| |_| | _ <| |___ |
6 | * \___|\___/|_| \_\_____| |
7 | * |
8 | * Copyright (C) 2015 - 2022, Steve Holme, <[email protected]>. |
9 | * |
10 | * This software is licensed as described in the file COPYING, which |
11 | * you should have received as part of this distribution. The terms |
12 | * are also available at https://curl.se/docs/copyright.html. |
13 | * |
14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | * copies of the Software, and permit persons to whom the Software is |
16 | * furnished to do so, under the terms of the COPYING file. |
17 | * |
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | * KIND, either express or implied. |
20 | * |
21 | * SPDX-License-Identifier: curl |
22 | * |
23 | ***************************************************************************/ |
24 | |
25 | #include "curl_setup.h" |
26 | |
27 | #if defined(USE_CURL_NTLM_CORE) && !defined(USE_WOLFSSL) && \ |
28 | (defined(USE_GNUTLS) || \ |
29 | defined(USE_NSS) || \ |
30 | defined(USE_SECTRANSP) || \ |
31 | defined(USE_OS400CRYPTO) || \ |
32 | defined(USE_WIN32_CRYPTO)) |
33 | |
34 | #include "curl_des.h" |
35 | |
36 | /* |
37 | * Curl_des_set_odd_parity() |
38 | * |
39 | * This is used to apply odd parity to the given byte array. It is typically |
40 | * used by when a cryptography engines doesn't have it's own version. |
41 | * |
42 | * The function is a port of the Java based oddParity() function over at: |
43 | * |
44 | * https://davenport.sourceforge.io/ntlm.html |
45 | * |
46 | * Parameters: |
47 | * |
48 | * bytes [in/out] - The data whose parity bits are to be adjusted for |
49 | * odd parity. |
50 | * len [out] - The length of the data. |
51 | */ |
52 | void Curl_des_set_odd_parity(unsigned char *bytes, size_t len) |
53 | { |
54 | size_t i; |
55 | |
56 | for(i = 0; i < len; i++) { |
57 | unsigned char b = bytes[i]; |
58 | |
59 | bool needs_parity = (((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ |
60 | (b >> 4) ^ (b >> 3) ^ (b >> 2) ^ |
61 | (b >> 1)) & 0x01) == 0; |
62 | |
63 | if(needs_parity) |
64 | bytes[i] |= 0x01; |
65 | else |
66 | bytes[i] &= 0xfe; |
67 | } |
68 | } |
69 | |
70 | #endif |
71 | |