]>
Commit | Line | Data |
---|---|---|
74ba9207 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
790fe579 | 2 | /* |
1da177e4 LT |
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 | |
50c2f753 | 11 | |
1da177e4 LT |
12 | */ |
13 | ||
14 | #include <linux/module.h> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
9a394d12 | 16 | #include <linux/fips.h> |
1da177e4 LT |
17 | #include <linux/fs.h> |
18 | #include <linux/string.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/random.h> | |
9a394d12 | 21 | #include <crypto/des.h> |
2baa2682 | 22 | #include "cifs_fs_sb.h" |
1da177e4 LT |
23 | #include "cifs_unicode.h" |
24 | #include "cifspdu.h" | |
3979877e | 25 | #include "cifsglob.h" |
1da177e4 | 26 | #include "cifs_debug.h" |
ee2c9258 | 27 | #include "cifsproto.h" |
1da177e4 | 28 | |
4b18f2a9 SF |
29 | #ifndef false |
30 | #define false 0 | |
1da177e4 | 31 | #endif |
4b18f2a9 SF |
32 | #ifndef true |
33 | #define true 1 | |
1da177e4 LT |
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 | ||
43988d76 SF |
41 | static void |
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 | |
59 | smbhash(unsigned char *out, const unsigned char *in, unsigned char *key) | |
60 | { | |
43988d76 | 61 | unsigned char key2[8]; |
9a394d12 | 62 | struct des_ctx ctx; |
43988d76 SF |
63 | |
64 | str_to_key(key, key2); | |
65 | ||
9a394d12 AB |
66 | if (fips_enabled) { |
67 | cifs_dbg(VFS, "FIPS compliance enabled: DES not permitted\n"); | |
68 | return -ENOENT; | |
9651ddba | 69 | } |
43988d76 | 70 | |
9a394d12 AB |
71 | des_expand_key(&ctx, key2, DES_KEY_SIZE); |
72 | des_encrypt(&ctx, out, in); | |
73 | memzero_explicit(&ctx, sizeof(ctx)); | |
9651ddba | 74 | |
06deeec7 | 75 | return 0; |
43988d76 SF |
76 | } |
77 | ||
78 | static int | |
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 | |
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 | ||
ee2c9258 SP |
107 | /* produce a md4 message digest from data of length n bytes */ |
108 | int | |
109 | mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len) | |
110 | { | |
111 | int rc; | |
82fb82be AA |
112 | struct crypto_shash *md4 = NULL; |
113 | struct sdesc *sdescmd4 = NULL; | |
114 | ||
115 | rc = cifs_alloc_hash("md4", &md4, &sdescmd4); | |
116 | if (rc) | |
ee2c9258 | 117 | goto mdfour_err; |
ee2c9258 SP |
118 | |
119 | rc = crypto_shash_init(&sdescmd4->shash); | |
120 | if (rc) { | |
f96637be | 121 | cifs_dbg(VFS, "%s: Could not init md4 shash\n", __func__); |
ee2c9258 SP |
122 | goto mdfour_err; |
123 | } | |
14cae324 SP |
124 | rc = crypto_shash_update(&sdescmd4->shash, link_str, link_len); |
125 | if (rc) { | |
f96637be | 126 | cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__); |
14cae324 SP |
127 | goto mdfour_err; |
128 | } | |
ee2c9258 | 129 | rc = crypto_shash_final(&sdescmd4->shash, md4_hash); |
14cae324 | 130 | if (rc) |
f96637be | 131 | cifs_dbg(VFS, "%s: Could not generate md4 hash\n", __func__); |
1da177e4 | 132 | |
ee2c9258 | 133 | mdfour_err: |
82fb82be | 134 | cifs_free_hash(&md4, &sdescmd4); |
ee2c9258 SP |
135 | return rc; |
136 | } | |
137 | ||
1da177e4 LT |
138 | /* |
139 | This implements the X/Open SMB password encryption | |
790fe579 | 140 | It takes a password, a 8 byte "crypt key" and puts 24 bytes of |
1da177e4 LT |
141 | encrypted password into p24 */ |
142 | /* Note that password must be uppercased and null terminated */ | |
43988d76 | 143 | int |
4e53a3fb | 144 | SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24) |
1da177e4 | 145 | { |
43988d76 SF |
146 | int rc; |
147 | unsigned char p14[14], p16[16], p21[21]; | |
1da177e4 | 148 | |
1da177e4 | 149 | memset(p14, '\0', 14); |
43988d76 SF |
150 | memset(p16, '\0', 16); |
151 | memset(p21, '\0', 21); | |
1da177e4 | 152 | |
43988d76 SF |
153 | memcpy(p14, passwd, 14); |
154 | rc = E_P16(p14, p16); | |
155 | if (rc) | |
156 | return rc; | |
1da177e4 | 157 | |
43988d76 SF |
158 | memcpy(p21, p16, 16); |
159 | rc = E_P24(p21, c8, p24); | |
50c2f753 | 160 | |
43988d76 | 161 | return rc; |
1da177e4 LT |
162 | } |
163 | ||
790fe579 | 164 | /* |
1da177e4 LT |
165 | * Creates the MD4 Hash of the users password in NT UNICODE. |
166 | */ | |
167 | ||
ee2c9258 | 168 | int |
9ef5992e SP |
169 | E_md4hash(const unsigned char *passwd, unsigned char *p16, |
170 | const struct nls_table *codepage) | |
1da177e4 | 171 | { |
ee2c9258 | 172 | int rc; |
1da177e4 | 173 | int len; |
9c32c63b | 174 | __le16 wpwd[129]; |
1da177e4 LT |
175 | |
176 | /* Password cannot be longer than 128 characters */ | |
9ef5992e | 177 | if (passwd) /* Password must be converted to NT unicode */ |
acbbb76a | 178 | len = cifs_strtoUTF16(wpwd, passwd, 128, codepage); |
9ef5992e | 179 | else { |
1da177e4 | 180 | len = 0; |
9ef5992e SP |
181 | *wpwd = 0; /* Ensure string is null terminated */ |
182 | } | |
1da177e4 | 183 | |
9c32c63b | 184 | rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16)); |
f99dbfa4 | 185 | memzero_explicit(wpwd, sizeof(wpwd)); |
ee2c9258 SP |
186 | |
187 | return rc; | |
1da177e4 LT |
188 | } |
189 | ||
1da177e4 | 190 | /* Does the NT MD4 hash then des encryption. */ |
ee2c9258 | 191 | int |
9ef5992e SP |
192 | SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24, |
193 | const struct nls_table *codepage) | |
1da177e4 | 194 | { |
ee2c9258 | 195 | int rc; |
43988d76 | 196 | unsigned char p16[16], p21[21]; |
1da177e4 | 197 | |
43988d76 | 198 | memset(p16, '\0', 16); |
1da177e4 LT |
199 | memset(p21, '\0', 21); |
200 | ||
9ef5992e | 201 | rc = E_md4hash(passwd, p16, codepage); |
ee2c9258 | 202 | if (rc) { |
f96637be JP |
203 | cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n", |
204 | __func__, rc); | |
ee2c9258 SP |
205 | return rc; |
206 | } | |
43988d76 SF |
207 | memcpy(p21, p16, 16); |
208 | rc = E_P24(p21, c8, p24); | |
ee2c9258 | 209 | return rc; |
1da177e4 | 210 | } |