smbencrypt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. SMB parameters and setup
  5. Copyright (C) Andrew Tridgell 1992-1998
  6. Modified by Jeremy Allison 1995.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "includes.h"
  20. extern int DEBUGLEVEL;
  21. #include "byteorder.h"
  22. /*
  23. This implements the X/Open SMB password encryption
  24. It takes a password, a 8 byte "crypt key" and puts 24 bytes of
  25. encrypted password into p24 */
  26. void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)
  27. {
  28. uchar p14[15], p21[21];
  29. memset(p21,'\0',21);
  30. memset(p14,'\0',14);
  31. StrnCpy((char *)p14,(char *)passwd,14);
  32. strupper((char *)p14);
  33. E_P16(p14, p21);
  34. SMBOWFencrypt(p21, c8, p24);
  35. #ifdef DEBUG_PASSWORD
  36. DEBUG(100,("SMBencrypt: lm#, challenge, response\n"));
  37. dump_data(100, (char *)p21, 16);
  38. dump_data(100, (char *)c8, 8);
  39. dump_data(100, (char *)p24, 24);
  40. #endif
  41. }
  42. /* Routines for Windows NT MD4 Hash functions. */
  43. static int _my_wcslen(int16 *str)
  44. {
  45. int len = 0;
  46. while(*str++ != 0)
  47. len++;
  48. return len;
  49. }
  50. /*
  51. * Convert a string into an NT UNICODE string.
  52. * Note that regardless of processor type
  53. * this must be in intel (little-endian)
  54. * format.
  55. */
  56. static int _my_mbstowcs(int16 *dst, uchar *src, int len)
  57. {
  58. int i;
  59. int16 val;
  60. for(i = 0; i < len; i++) {
  61. val = *src;
  62. SSVAL(dst,0,val);
  63. dst++;
  64. src++;
  65. if(val == 0)
  66. break;
  67. }
  68. return i;
  69. }
  70. /*
  71. * Creates the MD4 Hash of the users password in NT UNICODE.
  72. */
  73. void E_md4hash(uchar *passwd, uchar *p16)
  74. {
  75. int len;
  76. int16 wpwd[129];
  77. /* Password cannot be longer than 128 characters */
  78. len = strlen((char *)passwd);
  79. if(len > 128)
  80. len = 128;
  81. /* Password must be converted to NT unicode */
  82. _my_mbstowcs(wpwd, passwd, len);
  83. wpwd[len] = 0; /* Ensure string is null terminated */
  84. /* Calculate length in bytes */
  85. len = _my_wcslen(wpwd) * sizeof(int16);
  86. mdfour(p16, (unsigned char *)wpwd, len);
  87. }
  88. /* Does both the NT and LM owfs of a user's password */
  89. void nt_lm_owf_gen(char *pwd, uchar nt_p16[16], uchar p16[16])
  90. {
  91. char passwd[130];
  92. memset(passwd,'\0',130);
  93. safe_strcpy( passwd, pwd, sizeof(passwd)-1);
  94. /* Calculate the MD4 hash (NT compatible) of the password */
  95. memset(nt_p16, '\0', 16);
  96. E_md4hash((uchar *)passwd, nt_p16);
  97. #ifdef DEBUG_PASSWORD
  98. DEBUG(100,("nt_lm_owf_gen: pwd, nt#\n"));
  99. dump_data(120, passwd, strlen(passwd));
  100. dump_data(100, (char *)nt_p16, 16);
  101. #endif
  102. /* Mangle the passwords into Lanman format */
  103. passwd[14] = '\0';
  104. strupper(passwd);
  105. /* Calculate the SMB (lanman) hash functions of the password */
  106. memset(p16, '\0', 16);
  107. E_P16((uchar *) passwd, (uchar *)p16);
  108. #ifdef DEBUG_PASSWORD
  109. DEBUG(100,("nt_lm_owf_gen: pwd, lm#\n"));
  110. dump_data(120, passwd, strlen(passwd));
  111. dump_data(100, (char *)p16, 16);
  112. #endif
  113. /* clear out local copy of user's password (just being paranoid). */
  114. memset(passwd, '\0', sizeof(passwd));
  115. }
  116. /* Does the des encryption from the NT or LM MD4 hash. */
  117. void SMBOWFencrypt(uchar passwd[16], uchar *c8, uchar p24[24])
  118. {
  119. uchar p21[21];
  120. memset(p21,'\0',21);
  121. memcpy(p21, passwd, 16);
  122. E_P24(p21, c8, p24);
  123. }
  124. /* Does the des encryption from the FIRST 8 BYTES of the NT or LM MD4 hash. */
  125. void NTLMSSPOWFencrypt(uchar passwd[8], uchar *ntlmchalresp, uchar p24[24])
  126. {
  127. uchar p21[21];
  128. memset(p21,'\0',21);
  129. memcpy(p21, passwd, 8);
  130. memset(p21 + 8, 0xbd, 8);
  131. E_P24(p21, ntlmchalresp, p24);
  132. #ifdef DEBUG_PASSWORD
  133. DEBUG(100,("NTLMSSPOWFencrypt: p21, c8, p24\n"));
  134. dump_data(100, (char *)p21, 21);
  135. dump_data(100, (char *)ntlmchalresp, 8);
  136. dump_data(100, (char *)p24, 24);
  137. #endif
  138. }
  139. /* Does the NT MD4 hash then des encryption. */
  140. void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)
  141. {
  142. uchar p21[21];
  143. memset(p21,'\0',21);
  144. E_md4hash(passwd, p21);
  145. SMBOWFencrypt(p21, c8, p24);
  146. #ifdef DEBUG_PASSWORD
  147. DEBUG(100,("SMBNTencrypt: nt#, challenge, response\n"));
  148. dump_data(100, (char *)p21, 16);
  149. dump_data(100, (char *)c8, 8);
  150. dump_data(100, (char *)p24, 24);
  151. #endif
  152. }