Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Ard Biesheuvel | 9a394d1 | 2019-08-15 12:01:12 +0300 | [diff] [blame] | 16 | #include <linux/fips.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/fs.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/random.h> |
Ard Biesheuvel | 9a394d1 | 2019-08-15 12:01:12 +0300 | [diff] [blame] | 21 | #include <crypto/des.h> |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 22 | #include "cifs_fs_sb.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "cifs_unicode.h" |
| 24 | #include "cifspdu.h" |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 25 | #include "cifsglob.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "cifs_debug.h" |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 27 | #include "cifsproto.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 29 | #ifndef false |
| 30 | #define false 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #endif |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 32 | #ifndef true |
| 33 | #define true 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 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 | { |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 61 | unsigned char key2[8]; |
Ard Biesheuvel | 9a394d1 | 2019-08-15 12:01:12 +0300 | [diff] [blame] | 62 | struct des_ctx ctx; |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 63 | |
| 64 | str_to_key(key, key2); |
| 65 | |
Ard Biesheuvel | 9a394d1 | 2019-08-15 12:01:12 +0300 | [diff] [blame] | 66 | if (fips_enabled) { |
| 67 | cifs_dbg(VFS, "FIPS compliance enabled: DES not permitted\n"); |
| 68 | return -ENOENT; |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Ard Biesheuvel | 9a394d1 | 2019-08-15 12:01:12 +0300 | [diff] [blame] | 71 | des_expand_key(&ctx, key2, DES_KEY_SIZE); |
| 72 | des_encrypt(&ctx, out, in); |
| 73 | memzero_explicit(&ctx, sizeof(ctx)); |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 74 | |
Andy Lutomirski | 06deeec | 2016-12-12 12:54:37 -0800 | [diff] [blame] | 75 | return 0; |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 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 | |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 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; |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 112 | struct crypto_shash *md4 = NULL; |
| 113 | struct sdesc *sdescmd4 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 115 | rc = cifs_alloc_hash("md4", &md4, &sdescmd4); |
| 116 | if (rc) |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 117 | goto mdfour_err; |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 118 | |
| 119 | rc = crypto_shash_init(&sdescmd4->shash); |
| 120 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 121 | cifs_dbg(VFS, "%s: Could not init md4 shash\n", __func__); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 122 | goto mdfour_err; |
| 123 | } |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 124 | rc = crypto_shash_update(&sdescmd4->shash, link_str, link_len); |
| 125 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 126 | cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 127 | goto mdfour_err; |
| 128 | } |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 129 | rc = crypto_shash_final(&sdescmd4->shash, md4_hash); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 130 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 131 | cifs_dbg(VFS, "%s: Could not generate md4 hash\n", __func__); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 132 | |
| 133 | mdfour_err: |
Aurelien Aptel | 82fb82b | 2018-02-16 19:19:27 +0100 | [diff] [blame] | 134 | cifs_free_hash(&md4, &sdescmd4); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 135 | return rc; |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* |
| 139 | This implements the X/Open SMB password encryption |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 140 | It takes a password, a 8 byte "crypt key" and puts 24 bytes of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | encrypted password into p24 */ |
| 142 | /* Note that password must be uppercased and null terminated */ |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 143 | int |
Jeff Layton | 4e53a3f | 2008-12-05 20:41:21 -0500 | [diff] [blame] | 144 | SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 146 | int rc; |
| 147 | unsigned char p14[14], p16[16], p21[21]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | memset(p14, '\0', 14); |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 150 | memset(p16, '\0', 16); |
| 151 | memset(p21, '\0', 21); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 153 | memcpy(p14, passwd, 14); |
| 154 | rc = E_P16(p14, p16); |
| 155 | if (rc) |
| 156 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 158 | memcpy(p21, p16, 16); |
| 159 | rc = E_P24(p21, c8, p24); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 160 | |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 161 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 164 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | * Creates the MD4 Hash of the users password in NT UNICODE. |
| 166 | */ |
| 167 | |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 168 | int |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 169 | E_md4hash(const unsigned char *passwd, unsigned char *p16, |
| 170 | const struct nls_table *codepage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 172 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | int len; |
Steve French | 9c32c63 | 2011-11-10 12:48:20 -0600 | [diff] [blame] | 174 | __le16 wpwd[129]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* Password cannot be longer than 128 characters */ |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 177 | if (passwd) /* Password must be converted to NT unicode */ |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 178 | len = cifs_strtoUTF16(wpwd, passwd, 128, codepage); |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 179 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | len = 0; |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 181 | *wpwd = 0; /* Ensure string is null terminated */ |
| 182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Steve French | 9c32c63 | 2011-11-10 12:48:20 -0600 | [diff] [blame] | 184 | rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16)); |
Giel van Schijndel | f99dbfa | 2015-01-06 22:37:00 +0100 | [diff] [blame] | 185 | memzero_explicit(wpwd, sizeof(wpwd)); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 186 | |
| 187 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* Does the NT MD4 hash then des encryption. */ |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 191 | int |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 192 | SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24, |
| 193 | const struct nls_table *codepage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 195 | int rc; |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 196 | unsigned char p16[16], p21[21]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 198 | memset(p16, '\0', 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | memset(p21, '\0', 21); |
| 200 | |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 201 | rc = E_md4hash(passwd, p16, codepage); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 202 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 203 | cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n", |
| 204 | __func__, rc); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 205 | return rc; |
| 206 | } |
Steve French | 43988d7 | 2011-04-19 18:23:31 +0000 | [diff] [blame] | 207 | memcpy(p21, p16, 16); |
| 208 | rc = E_P24(p21, c8, p24); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 209 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |