curl_des.c 2.0 KB

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