smbencrypt.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Unix SMB/Netbios implementation.
  4. Version 1.9.
  5. SMB parameters and setup
  6. Copyright (C) Andrew Tridgell 1992-2000
  7. Copyright (C) Luke Kenneth Casson Leighton 1996-2000
  8. Modified by Jeremy Allison 1995.
  9. Copyright (C) Andrew Bartlett <[email protected]> 2002-2003
  10. Modified by Steve French ([email protected]) 2002-2003
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/fips.h>
  15. #include <linux/fs.h>
  16. #include <linux/string.h>
  17. #include <linux/kernel.h>
  18. #include <linux/random.h>
  19. #include "cifs_fs_sb.h"
  20. #include "cifs_unicode.h"
  21. #include "cifspdu.h"
  22. #include "cifsglob.h"
  23. #include "cifs_debug.h"
  24. #include "cifsproto.h"
  25. #include "../common/md4.h"
  26. #ifndef false
  27. #define false 0
  28. #endif
  29. #ifndef true
  30. #define true 1
  31. #endif
  32. /* following came from the other byteorder.h to avoid include conflicts */
  33. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  34. #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
  35. #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))
  36. /* produce a md4 message digest from data of length n bytes */
  37. static int
  38. mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
  39. {
  40. int rc;
  41. struct md4_ctx mctx;
  42. rc = cifs_md4_init(&mctx);
  43. if (rc) {
  44. cifs_dbg(VFS, "%s: Could not init MD4\n", __func__);
  45. goto mdfour_err;
  46. }
  47. rc = cifs_md4_update(&mctx, link_str, link_len);
  48. if (rc) {
  49. cifs_dbg(VFS, "%s: Could not update MD4\n", __func__);
  50. goto mdfour_err;
  51. }
  52. rc = cifs_md4_final(&mctx, md4_hash);
  53. if (rc)
  54. cifs_dbg(VFS, "%s: Could not finalize MD4\n", __func__);
  55. mdfour_err:
  56. return rc;
  57. }
  58. /*
  59. * Creates the MD4 Hash of the users password in NT UNICODE.
  60. */
  61. int
  62. E_md4hash(const unsigned char *passwd, unsigned char *p16,
  63. const struct nls_table *codepage)
  64. {
  65. int rc;
  66. int len;
  67. __le16 wpwd[129];
  68. /* Password cannot be longer than 128 characters */
  69. if (passwd) /* Password must be converted to NT unicode */
  70. len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
  71. else {
  72. len = 0;
  73. *wpwd = 0; /* Ensure string is null terminated */
  74. }
  75. rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
  76. memzero_explicit(wpwd, sizeof(wpwd));
  77. return rc;
  78. }