/[muddleftpd]/muddleftpd/modules/auth/authlibsmb/smbval/smbencrypt.c
ViewVC logotype

Diff of /muddleftpd/modules/auth/authlibsmb/smbval/smbencrypt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by rugger, Sun Oct 20 12:00:41 2002 UTC revision 1.1.4.1 by ganneff, Mon Oct 21 19:52:58 2002 UTC
# Line 1  Line 1 
1    
2  /*  /*
3     Unix SMB/Netbios implementation.     Unix SMB/Netbios implementation.
4     Version 1.9.     Version 1.9.
# Line 33  extern int DEBUGLEVEL; Line 34  extern int DEBUGLEVEL;
34    
35  #include "byteorder.h"  #include "byteorder.h"
36    
37  char *StrnCpy(char *dest,char *src,int n);  char *StrnCpy(char *dest,
38                              char *src,
39                              int n);
40  void strupper(char *s);  void strupper(char *s);
41    
42  /*  /*
43     This implements the X/Open SMB password encryption     This implements the X/Open SMB password encryption
44     It takes a password, a 8 byte "crypt key" and puts 24 bytes of     It takes a password, a 8 byte "crypt key" and puts 24 bytes of
45     encrypted password into p24 */     encrypted password into p24 */
46  void SMBencrypt(uchar *passwd, uchar *c8, uchar *p24)  void SMBencrypt(uchar * passwd,
47  {                                  uchar * c8,
48    uchar p14[15], p21[21];                                  uchar * p24)
49    {
50    memset(p21,'\0',21);          uchar p14[15], p21[21];
51    memset(p14,'\0',14);  
52    StrnCpy((char *)p14,(char *)passwd,14);          memset(p21, '\0', 21);
53            memset(p14, '\0', 14);
54            StrnCpy((char *) p14, (char *) passwd, 14);
55    
56    strupper((char *)p14);          strupper((char *) p14);
57    E_P16(p14, p21);          E_P16(p14, p21);
58    E_P24(p21, c8, p24);          E_P24(p21, c8, p24);
59  }  }
60    
61  /* Routines for Windows NT MD4 Hash functions. */  /* Routines for Windows NT MD4 Hash functions. */
62  static int _my_wcslen(int16 *str)  static int _my_wcslen(int16 * str)
63  {  {
64          int len = 0;          int len = 0;
65          while(*str++ != 0)  
66            while (*str++ != 0)
67                  len++;                  len++;
68          return len;          return len;
69  }  }
# Line 68  static int _my_wcslen(int16 *str) Line 74  static int _my_wcslen(int16 *str)
74   * this must be in intel (little-endian)   * this must be in intel (little-endian)
75   * format.   * format.
76   */   */
77    
78  static int _my_mbstowcs(int16 *dst, uchar *src, int len)  static int _my_mbstowcs(int16 * dst,
79                                                    uchar * src,
80                                                    int len)
81  {  {
82          int i;          int i;
83          int16 val;          int16 val;
84    
85          for(i = 0; i < len; i++) {          for (i = 0; i < len; i++)
86            {
87                  val = *src;                  val = *src;
88                  SSVAL(dst,0,val);                  SSVAL(dst, 0, val);
89                  dst++;                  dst++;
90                  src++;                  src++;
91                  if(val == 0)                  if (val == 0)
92                          break;                          break;
93          }          }
94          return i;          return i;
# Line 88  static int _my_mbstowcs(int16 *dst, ucha Line 97  static int _my_mbstowcs(int16 *dst, ucha
97  /*  /*
98   * Creates the MD4 Hash of the users password in NT UNICODE.   * Creates the MD4 Hash of the users password in NT UNICODE.
99   */   */
100    
101  void E_md4hash(uchar *passwd, uchar *p16)  void E_md4hash(uchar * passwd,
102                               uchar * p16)
103  {  {
104          int len;          int len;
105          int16 wpwd[129];          int16 wpwd[129];
106            
107          /* Password cannot be longer than 128 characters */          /*
108          len = strlen((char *)passwd);           * Password cannot be longer than 128 characters
109          if(len > 128)           */
110            len = strlen((char *) passwd);
111            if (len > 128)
112                  len = 128;                  len = 128;
113          /* Password must be converted to NT unicode */          /*
114             * Password must be converted to NT unicode
115             */
116          _my_mbstowcs(wpwd, passwd, len);          _my_mbstowcs(wpwd, passwd, len);
117          wpwd[len] = 0; /* Ensure string is null terminated */          wpwd[len] = 0;                          /* Ensure string is null terminated */
118          /* Calculate length in bytes */          /*
119             * Calculate length in bytes
120             */
121          len = _my_wcslen(wpwd) * sizeof(int16);          len = _my_wcslen(wpwd) * sizeof(int16);
122    
123          mdfour(p16, (unsigned char *)wpwd, len);          mdfour(p16, (unsigned char *) wpwd, len);
124  }  }
125    
126  /* Does the NT MD4 hash then des encryption. */  /* Does the NT MD4 hash then des encryption. */
127    
128  void SMBNTencrypt(uchar *passwd, uchar *c8, uchar *p24)  void SMBNTencrypt(uchar * passwd,
129                                      uchar * c8,
130                                      uchar * p24)
131  {  {
132          uchar p21[21];          uchar p21[21];
133    
134          memset(p21,'\0',21);          memset(p21, '\0', 21);
135    
136          E_md4hash(passwd, p21);              E_md4hash(passwd, p21);
137          E_P24(p21, c8, p24);          E_P24(p21, c8, p24);
138  }  }
139    
140  /* Does both the NT and LM owfs of a user's password */  /* Does both the NT and LM owfs of a user's password */
141    
142  void nt_lm_owf_gen(char *pwd, char *nt_p16, char *p16)  void nt_lm_owf_gen(char *pwd,
143                                       char *nt_p16,
144                                       char *p16)
145  {  {
146          char passwd[130];          char passwd[130];
         StrnCpy(passwd, pwd, sizeof(passwd)-1);  
147    
148          /* Calculate the MD4 hash (NT compatible) of the password */          StrnCpy(passwd, pwd, sizeof(passwd) - 1);
149    
150            /*
151             * Calculate the MD4 hash (NT compatible) of the password
152             */
153          memset(nt_p16, '\0', 16);          memset(nt_p16, '\0', 16);
154          E_md4hash((uchar *)passwd, (uchar *)nt_p16);          E_md4hash((uchar *) passwd, (uchar *) nt_p16);
155    
156          /* Mangle the passwords into Lanman format */          /*
157             * Mangle the passwords into Lanman format
158             */
159          passwd[14] = '\0';          passwd[14] = '\0';
160          strupper(passwd);          strupper(passwd);
161    
162          /* Calculate the SMB (lanman) hash functions of the password */          /*
163             * Calculate the SMB (lanman) hash functions of the password
164             */
165    
166          memset(p16, '\0', 16);          memset(p16, '\0', 16);
167          E_P16((uchar *) passwd, (uchar *)p16);          E_P16((uchar *) passwd, (uchar *) p16);
168    
169          /* clear out local copy of user's password (just being paranoid). */          /*
170             * clear out local copy of user's password (just being paranoid).
171             */
172          bzero(passwd, sizeof(passwd));          bzero(passwd, sizeof(passwd));
173  }  }
174    
175  /****************************************************************************  /****************************************************************************
176  line strncpy but always null terminates. Make sure there is room!  line strncpy but always null terminates. Make sure there is room!
177  ****************************************************************************/  ****************************************************************************/
178  char *StrnCpy(char *dest,char *src,int n)  char *StrnCpy(char *dest,
179  {                            char *src,
180    char *d = dest;                            int n)
181    if (!dest) return(NULL);  {
182    if (!src) {          char *d = dest;
183      *dest = 0;  
184      return(dest);          if (!dest)
185    }                  return (NULL);
186    while (n-- && (*d++ = *src++)) ;          if (!src)
187    *d = 0;          {
188    return(dest);                  *dest = 0;
189                    return (dest);
190            }
191            while (n-- && (*d++ = *src++));
192            *d = 0;
193            return (dest);
194  }  }
195    
196  void strupper(char *s)  void strupper(char *s)
197  {  {
198    while (*s)          while (*s)
199    {          {
200      /*                  /*
201  #if !defined(KANJI_WIN95_COMPATIBILITY)                   * #if !defined(KANJI_WIN95_COMPATIBILITY)
202      if(lp_client_code_page() == KANJI_CODEPAGE)                   * if(lp_client_code_page() == KANJI_CODEPAGE)
203      {                   * {
204                           *
205        if (is_shift_jis (*s))                   * if (is_shift_jis (*s))
206        {                   * {
207          if (is_sj_lower (s[0], s[1]))                   * if (is_sj_lower (s[0], s[1]))
208            s[1] = sj_toupper2 (s[1]);                   * s[1] = sj_toupper2 (s[1]);
209          s += 2;                   * s += 2;
210        }                   * }
211        else if (is_kana (*s))                   * else if (is_kana (*s))
212        {                   * {
213          s++;                   * s++;
214        }                   * }
215        else                   * else
216        {                   * {
217          if (islower(*s))                                             * if (islower(*s))                          
218            *s = toupper(*s);                   * *s = toupper(*s);
219          s++;                   * s++;
220        }                   * }
221      }                   * }
222      else                   * else
223  #endif */ /* KANJI_WIN95_COMPATIBILITY */                   * #endif
224      {                   *//*
225        if (islower(*s))                   * KANJI_WIN95_COMPATIBILITY
226          *s = toupper(*s);                   */
227        s++;                  {
228      }                          if (islower(*s))
229    }                                  *s = toupper(*s);
230  }                                                s++;
231                    }
232            }
233    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.4.1

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26