blob: 39a938443e3e3470848772389520c8b0e07c933e [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Steve French790fe572007-07-07 19:25:05 +00002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 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 French50c2f752007-07-13 00:33:32 +000011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012*/
13
14#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Ard Biesheuvel9a394d12019-08-15 12:01:12 +030016#include <linux/fips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/fs.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/random.h>
Ard Biesheuvel9a394d12019-08-15 12:01:12 +030021#include <crypto/des.h>
Steve French2baa2682014-09-27 02:19:01 -050022#include "cifs_fs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "cifs_unicode.h"
24#include "cifspdu.h"
Steve French39798772006-05-31 22:40:51 +000025#include "cifsglob.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "cifs_debug.h"
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060027#include "cifsproto.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Steve French4b18f2a2008-04-29 00:06:05 +000029#ifndef false
30#define false 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#endif
Steve French4b18f2a2008-04-29 00:06:05 +000032#ifndef true
33#define true 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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 French43988d72011-04-19 18:23:31 +000041static void
42str_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
58static int
59smbhash(unsigned char *out, const unsigned char *in, unsigned char *key)
60{
Steve French43988d72011-04-19 18:23:31 +000061 unsigned char key2[8];
Ard Biesheuvel9a394d12019-08-15 12:01:12 +030062 struct des_ctx ctx;
Steve French43988d72011-04-19 18:23:31 +000063
64 str_to_key(key, key2);
65
Ard Biesheuvel9a394d12019-08-15 12:01:12 +030066 if (fips_enabled) {
67 cifs_dbg(VFS, "FIPS compliance enabled: DES not permitted\n");
68 return -ENOENT;
Steve French43988d72011-04-19 18:23:31 +000069 }
70
Ard Biesheuvel9a394d12019-08-15 12:01:12 +030071 des_expand_key(&ctx, key2, DES_KEY_SIZE);
72 des_encrypt(&ctx, out, in);
73 memzero_explicit(&ctx, sizeof(ctx));
Steve French43988d72011-04-19 18:23:31 +000074
Andy Lutomirski06deeec2016-12-12 12:54:37 -080075 return 0;
Steve French43988d72011-04-19 18:23:31 +000076}
77
78static int
79E_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
92static int
93E_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 Pargaonkaree2c9252011-01-27 09:58:04 -0600107/* produce a md4 message digest from data of length n bytes */
108int
109mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
110{
111 int rc;
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100112 struct crypto_shash *md4 = NULL;
113 struct sdesc *sdescmd4 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100115 rc = cifs_alloc_hash("md4", &md4, &sdescmd4);
116 if (rc)
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600117 goto mdfour_err;
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600118
119 rc = crypto_shash_init(&sdescmd4->shash);
120 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500121 cifs_dbg(VFS, "%s: Could not init md4 shash\n", __func__);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600122 goto mdfour_err;
123 }
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500124 rc = crypto_shash_update(&sdescmd4->shash, link_str, link_len);
125 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500126 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500127 goto mdfour_err;
128 }
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600129 rc = crypto_shash_final(&sdescmd4->shash, md4_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -0500130 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500131 cifs_dbg(VFS, "%s: Could not generate md4 hash\n", __func__);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600132
133mdfour_err:
Aurelien Aptel82fb82b2018-02-16 19:19:27 +0100134 cifs_free_hash(&md4, &sdescmd4);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600135 return rc;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 This implements the X/Open SMB password encryption
Steve French790fe572007-07-07 19:25:05 +0000140 It takes a password, a 8 byte "crypt key" and puts 24 bytes of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 encrypted password into p24 */
142/* Note that password must be uppercased and null terminated */
Steve French43988d72011-04-19 18:23:31 +0000143int
Jeff Layton4e53a3f2008-12-05 20:41:21 -0500144SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Steve French43988d72011-04-19 18:23:31 +0000146 int rc;
147 unsigned char p14[14], p16[16], p21[21];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 memset(p14, '\0', 14);
Steve French43988d72011-04-19 18:23:31 +0000150 memset(p16, '\0', 16);
151 memset(p21, '\0', 21);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Steve French43988d72011-04-19 18:23:31 +0000153 memcpy(p14, passwd, 14);
154 rc = E_P16(p14, p16);
155 if (rc)
156 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Steve French43988d72011-04-19 18:23:31 +0000158 memcpy(p21, p16, 16);
159 rc = E_P24(p21, c8, p24);
Steve French50c2f752007-07-13 00:33:32 +0000160
Steve French43988d72011-04-19 18:23:31 +0000161 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Steve French790fe572007-07-07 19:25:05 +0000164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * Creates the MD4 Hash of the users password in NT UNICODE.
166 */
167
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600168int
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500169E_md4hash(const unsigned char *passwd, unsigned char *p16,
170 const struct nls_table *codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600172 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int len;
Steve French9c32c632011-11-10 12:48:20 -0600174 __le16 wpwd[129];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /* Password cannot be longer than 128 characters */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500177 if (passwd) /* Password must be converted to NT unicode */
Steve Frenchacbbb762012-01-18 22:32:33 -0600178 len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500179 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 len = 0;
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500181 *wpwd = 0; /* Ensure string is null terminated */
182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Steve French9c32c632011-11-10 12:48:20 -0600184 rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
Giel van Schijndelf99dbfa2015-01-06 22:37:00 +0100185 memzero_explicit(wpwd, sizeof(wpwd));
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600186
187 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* Does the NT MD4 hash then des encryption. */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600191int
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500192SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24,
193 const struct nls_table *codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600195 int rc;
Steve French43988d72011-04-19 18:23:31 +0000196 unsigned char p16[16], p21[21];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Steve French43988d72011-04-19 18:23:31 +0000198 memset(p16, '\0', 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 memset(p21, '\0', 21);
200
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -0500201 rc = E_md4hash(passwd, p16, codepage);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600202 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500203 cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
204 __func__, rc);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600205 return rc;
206 }
Steve French43988d72011-04-19 18:23:31 +0000207 memcpy(p21, p16, 16);
208 rc = E_P24(p21, c8, p24);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -0600209 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}