root/src/vfs/smbfs/helpers/libsmb/smbencrypt.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. SMBencrypt
  2. _my_wcslen
  3. _my_mbstowcs
  4. E_md4hash
  5. nt_lm_owf_gen
  6. SMBOWFencrypt
  7. NTLMSSPOWFencrypt
  8. SMBNTencrypt

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

/* [previous][next][first][last][top][bottom][index][help]  */