]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/cifs/smbencrypt.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 61
[mirror_ubuntu-jammy-kernel.git] / fs / cifs / smbencrypt.c
CommitLineData
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 <abartlet@samba.org> 2002-2003
10 Modified by Steve French (sfrench@us.ibm.com) 2002-2003
50c2f753 11
1da177e4
LT
12*/
13
06deeec7 14#include <linux/crypto.h>
1da177e4 15#include <linux/module.h>
5a0e3ad6 16#include <linux/slab.h>
1da177e4
LT
17#include <linux/fs.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/random.h>
2baa2682 21#include "cifs_fs_sb.h"
1da177e4
LT
22#include "cifs_unicode.h"
23#include "cifspdu.h"
3979877e 24#include "cifsglob.h"
1da177e4 25#include "cifs_debug.h"
ee2c9258 26#include "cifsproto.h"
1da177e4 27
4b18f2a9
SF
28#ifndef false
29#define false 0
1da177e4 30#endif
4b18f2a9
SF
31#ifndef true
32#define true 1
1da177e4
LT
33#endif
34
35/* following came from the other byteorder.h to avoid include conflicts */
36#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
37#define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
38#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))
39
43988d76
SF
40static void
41str_to_key(unsigned char *str, unsigned char *key)
42{
43 int i;
44
45 key[0] = str[0] >> 1;
46 key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2);
47 key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3);
48 key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4);
49 key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5);
50 key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6);
51 key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7);
52 key[7] = str[6] & 0x7F;
53 for (i = 0; i < 8; i++)
54 key[i] = (key[i] << 1);
55}
56
57static int
58smbhash(unsigned char *out, const unsigned char *in, unsigned char *key)
59{
43988d76 60 unsigned char key2[8];
06deeec7 61 struct crypto_cipher *tfm_des;
43988d76
SF
62
63 str_to_key(key, key2);
64
06deeec7 65 tfm_des = crypto_alloc_cipher("des", 0, 0);
43988d76 66 if (IS_ERR(tfm_des)) {
9651ddba 67 cifs_dbg(VFS, "could not allocate des crypto API\n");
06deeec7 68 return PTR_ERR(tfm_des);
9651ddba 69 }
43988d76 70
06deeec7
AL
71 crypto_cipher_setkey(tfm_des, key2, 8);
72 crypto_cipher_encrypt_one(tfm_des, out, in);
73 crypto_free_cipher(tfm_des);
9651ddba 74
06deeec7 75 return 0;
43988d76
SF
76}
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
ee2c9258
SP
107/* 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;
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 133mdfour_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 143int
4e53a3fb 144SMBencrypt(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 168int
9ef5992e
SP
169E_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 191int
9ef5992e
SP
192SMBNTencrypt(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}