1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Unix SMB/Netbios implementation.
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
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/fips.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"
26 #include "cifs_debug.h"
27 #include "cifsproto.h"
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)))
42 str_to_key(unsigned char *str, unsigned char *key)
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);
59 smbhash(unsigned char *out, const unsigned char *in, unsigned char *key)
61 unsigned char key2[8];
64 str_to_key(key, key2);
67 cifs_dbg(VFS, "FIPS compliance enabled: DES not permitted\n");
71 des_expand_key(&ctx, key2, DES_KEY_SIZE);
72 des_encrypt(&ctx, out, in);
73 memzero_explicit(&ctx, sizeof(ctx));
79 E_P16(unsigned char *p14, unsigned char *p16)
82 unsigned char sp8[8] =
83 { 0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
85 rc = smbhash(p16, sp8, p14);
88 rc = smbhash(p16 + 8, sp8, p14 + 7);
93 E_P24(unsigned char *p21, const unsigned char *c8, unsigned char *p24)
97 rc = smbhash(p24, c8, p21);
100 rc = smbhash(p24 + 8, c8, p21 + 7);
103 rc = smbhash(p24 + 16, c8, p21 + 14);
107 /* produce a md4 message digest from data of length n bytes */
109 mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
112 struct crypto_shash *md4 = NULL;
113 struct sdesc *sdescmd4 = NULL;
115 rc = cifs_alloc_hash("md4", &md4, &sdescmd4);
119 rc = crypto_shash_init(&sdescmd4->shash);
121 cifs_dbg(VFS, "%s: Could not init md4 shash\n", __func__);
124 rc = crypto_shash_update(&sdescmd4->shash, link_str, link_len);
126 cifs_dbg(VFS, "%s: Could not update with link_str\n", __func__);
129 rc = crypto_shash_final(&sdescmd4->shash, md4_hash);
131 cifs_dbg(VFS, "%s: Could not generate md4 hash\n", __func__);
134 cifs_free_hash(&md4, &sdescmd4);
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 */
144 SMBencrypt(unsigned char *passwd, const unsigned char *c8, unsigned char *p24)
147 unsigned char p14[14], p16[16], p21[21];
149 memset(p14, '\0', 14);
150 memset(p16, '\0', 16);
151 memset(p21, '\0', 21);
153 memcpy(p14, passwd, 14);
154 rc = E_P16(p14, p16);
158 memcpy(p21, p16, 16);
159 rc = E_P24(p21, c8, p24);
165 * Creates the MD4 Hash of the users password in NT UNICODE.
169 E_md4hash(const unsigned char *passwd, unsigned char *p16,
170 const struct nls_table *codepage)
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);
181 *wpwd = 0; /* Ensure string is null terminated */
184 rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
185 memzero_explicit(wpwd, sizeof(wpwd));
190 /* Does the NT MD4 hash then des encryption. */
192 SMBNTencrypt(unsigned char *passwd, unsigned char *c8, unsigned char *p24,
193 const struct nls_table *codepage)
196 unsigned char p16[16], p21[21];
198 memset(p16, '\0', 16);
199 memset(p21, '\0', 21);
201 rc = E_md4hash(passwd, p16, codepage);
203 cifs_dbg(FYI, "%s Can't generate NT hash, error: %d\n",
207 memcpy(p21, p16, 16);
208 rc = E_P24(p21, c8, p24);