curl_des.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2015 - 2022, Steve Holme, <steve_holme@hotmail.com>.
  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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if defined(USE_CURL_NTLM_CORE) && !defined(USE_WOLFSSL) && \
  24. (defined(USE_GNUTLS) || \
  25. defined(USE_NSS) || \
  26. defined(USE_SECTRANSP) || \
  27. defined(USE_OS400CRYPTO) || \
  28. defined(USE_WIN32_CRYPTO))
  29. #include "curl_des.h"
  30. /*
  31. * Curl_des_set_odd_parity()
  32. *
  33. * This is used to apply odd parity to the given byte array. It is typically
  34. * used by when a cryptography engines doesn't have it's own version.
  35. *
  36. * The function is a port of the Java based oddParity() function over at:
  37. *
  38. * https://davenport.sourceforge.io/ntlm.html
  39. *
  40. * Parameters:
  41. *
  42. * bytes [in/out] - The data whose parity bits are to be adjusted for
  43. * odd parity.
  44. * len [out] - The length of the data.
  45. */
  46. void Curl_des_set_odd_parity(unsigned char *bytes, size_t len)
  47. {
  48. size_t i;
  49. for(i = 0; i < len; i++) {
  50. unsigned char b = bytes[i];
  51. bool needs_parity = (((b >> 7) ^ (b >> 6) ^ (b >> 5) ^
  52. (b >> 4) ^ (b >> 3) ^ (b >> 2) ^
  53. (b >> 1)) & 0x01) == 0;
  54. if(needs_parity)
  55. bytes[i] |= 0x01;
  56. else
  57. bytes[i] &= 0xfe;
  58. }
  59. }
  60. #endif