• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <abartlet@samba.org> 2002-2003
10    Modified by Steve French (sfrench@us.ibm.com) 2002-2003
11 
12 */
13 
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/fips.h>
17 #include <linux/fs.h>
18 #include <linux/string.h>
19 #include <linux/kernel.h>
20 #include <linux/random.h>
21 #include <crypto/des.h>
22 #include "cifs_fs_sb.h"
23 #include "cifs_unicode.h"
24 #include "cifspdu.h"
25 #include "cifsglob.h"
26 #include "cifs_debug.h"
27 #include "cifsproto.h"
28 
29 #ifndef false
30 #define false 0
31 #endif
32 #ifndef true
33 #define true 1
34 #endif
35 
36 /* following came from the other byteorder.h to avoid include conflicts */
37 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
38 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
39 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))
40 
41 static void
str_to_key(unsigned char * str,unsigned char * key)42 str_to_key(unsigned char *str, unsigned char *key)
43 {
44 	int i;
45 
46 	key[0] = str[0] >> 1;
47 	key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2);
48 	key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3);
49 	key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4);
50 	key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5);
51 	key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6);
52 	key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7);
53 	key[7] = str[6] & 0x7F;
54 	for (i = 0; i < 8; i++)
55 		key[i] = (key[i] << 1);
56 }
57 
58 static int
smbhash(unsigned char * out,const unsigned char * in,unsigned char * key)59 smbhash(unsigned char *out, const unsigned char *in, unsigned char *key)
60 {
61 	unsigned char key2[8];
62 	struct des_ctx ctx;
63 
64 	str_to_key(key, key2);
65 
66 	if (fips_enabled) {
67 		cifs_dbg(VFS, "FIPS compliance enabled: DES not permitted\n");
68 		return -ENOENT;
69 	}
70 
71 	des_expand_key(&ctx, key2, DES_KEY_SIZE);
72 	des_encrypt(&ctx, out, in);
73 	memzero_explicit(&ctx, sizeof(ctx));
74 
75 	return 0;
76 }
77 
78 static int
E_P16(unsigned char * p14,unsigned char * p16)79 E_P16(unsigned char *p14, unsigned char *p16)
80 {
81 	int rc;
82 	unsigned char sp8[8] =
83 	    { 0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
84 
85 	rc = smbhash(p16, sp8, p14);
86 	if (rc)
87 		return rc;
88 	rc = smbhash(p16 + 8, sp8, p14 + 7);
89 	return rc;
90 }
91 
92 static int
E_P24(unsigned char * p21,const unsigned char * c8,unsigned char * p24)93 E_P24(unsigned char *p21, const unsigned char *c8, unsigned char *p24)
94 {
95 	int rc;
96 
97 	rc = smbhash(p24, c8, p21);
98 	if (rc)
99 		return rc;
100 	rc = smbhash(p24 + 8, c8, p21 + 7);
101 	if (rc)
102 		return rc;
103 	rc = smbhash(p24 + 16, c8, p21 + 14);
104 	return rc;
105 }
106 
107 /* produce a md4 message digest from data of length n bytes */
108 int
mdfour(unsigned char * md4_hash,unsigned char * link_str,int link_len)109 mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
110 {
111 	int rc;
112 	struct crypto_shash *md4 = NULL;
113 	struct sdesc *sdescmd4 = NULL;
114 
115 	rc = cifs_alloc_hash("md4", &md4, &sdescmd4);
116 	if (rc)
117 		goto mdfour_err;
118 
119 	rc = crypto_shash_init(&sdescmd4->shash);
120 	if (rc) {
121 		cifs_dbg(VFS, "%s: Could not init md4 shash\n", __func__);
122 		goto mdfour_err;
123 	}
124 	rc = crypto_shash_update(&sdescmd4->shash, link_str, link_len);
125 	if (rc) {
126 		cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
127 		goto mdfour_err;
128 	}
129 	rc = crypto_shash_final(&sdescmd4->shash, md4_hash);
130 	if (rc)
131 		cifs_dbg(VFS, "%s: Could not generate md4 hash\n", __func__);
132 
133 mdfour_err:
134 	cifs_free_hash(&md4, &sdescmd4);
135 	return rc;
136 }
137 
138 /*
139    This implements the X/Open SMB password encryption
140    It takes a password, a 8 byte "crypt key" and puts 24 bytes of
141    encrypted password into p24 */
142 /* Note that password must be uppercased and null terminated */
143 int
SMBencrypt(unsigned char * passwd,const unsigned char * c8,unsigned char * p24)144 SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24)
145 {
146 	int rc;
147 	unsigned char p14[14], p16[16], p21[21];
148 
149 	memset(p14, '\0', 14);
150 	memset(p16, '\0', 16);
151 	memset(p21, '\0', 21);
152 
153 	memcpy(p14, passwd, 14);
154 	rc = E_P16(p14, p16);
155 	if (rc)
156 		return rc;
157 
158 	memcpy(p21, p16, 16);
159 	rc = E_P24(p21, c8, p24);
160 
161 	return rc;
162 }
163 
164 /*
165  * Creates the MD4 Hash of the users password in NT UNICODE.
166  */
167 
168 int
E_md4hash(const unsigned char * passwd,unsigned char * p16,const struct nls_table * codepage)169 E_md4hash(const unsigned char *passwd, unsigned char *p16,
170 	const struct nls_table *codepage)
171 {
172 	int rc;
173 	int len;
174 	__le16 wpwd[129];
175 
176 	/* Password cannot be longer than 128 characters */
177 	if (passwd) /* Password must be converted to NT unicode */
178 		len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
179 	else {
180 		len = 0;
181 		*wpwd = 0; /* Ensure string is null terminated */
182 	}
183 
184 	rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
185 	memzero_explicit(wpwd, sizeof(wpwd));
186 
187 	return rc;
188 }
189 
190 /* Does the NT MD4 hash then des encryption. */
191 int
SMBNTencrypt(unsigned char * passwd,unsigned char * c8,unsigned char * p24,const struct nls_table * codepage)192 SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24,
193 		const struct nls_table *codepage)
194 {
195 	int rc;
196 	unsigned char p16[16], p21[21];
197 
198 	memset(p16, '\0', 16);
199 	memset(p21, '\0', 21);
200 
201 	rc = E_md4hash(passwd, p16, codepage);
202 	if (rc) {
203 		cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
204 			 __func__, rc);
205 		return rc;
206 	}
207 	memcpy(p21, p16, 16);
208 	rc = E_P24(p21, c8, p24);
209 	return rc;
210 }
211