xref: /openbmc/linux/fs/smb/client/smbencrypt.c (revision a5df7820)
1*38c8a9a5SSteve French // SPDX-License-Identifier: GPL-2.0-or-later
2*38c8a9a5SSteve French /*
3*38c8a9a5SSteve French    Unix SMB/Netbios implementation.
4*38c8a9a5SSteve French    Version 1.9.
5*38c8a9a5SSteve French    SMB parameters and setup
6*38c8a9a5SSteve French    Copyright (C) Andrew Tridgell 1992-2000
7*38c8a9a5SSteve French    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
8*38c8a9a5SSteve French    Modified by Jeremy Allison 1995.
9*38c8a9a5SSteve French    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
10*38c8a9a5SSteve French    Modified by Steve French (sfrench@us.ibm.com) 2002-2003
11*38c8a9a5SSteve French 
12*38c8a9a5SSteve French */
13*38c8a9a5SSteve French 
14*38c8a9a5SSteve French #include <linux/module.h>
15*38c8a9a5SSteve French #include <linux/slab.h>
16*38c8a9a5SSteve French #include <linux/fips.h>
17*38c8a9a5SSteve French #include <linux/fs.h>
18*38c8a9a5SSteve French #include <linux/string.h>
19*38c8a9a5SSteve French #include <linux/kernel.h>
20*38c8a9a5SSteve French #include <linux/random.h>
21*38c8a9a5SSteve French #include "cifs_fs_sb.h"
22*38c8a9a5SSteve French #include "cifs_unicode.h"
23*38c8a9a5SSteve French #include "cifspdu.h"
24*38c8a9a5SSteve French #include "cifsglob.h"
25*38c8a9a5SSteve French #include "cifs_debug.h"
26*38c8a9a5SSteve French #include "cifsproto.h"
27*38c8a9a5SSteve French #include "../common/md4.h"
28*38c8a9a5SSteve French 
29*38c8a9a5SSteve French /* following came from the other byteorder.h to avoid include conflicts */
30*38c8a9a5SSteve French #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
31*38c8a9a5SSteve French #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
32*38c8a9a5SSteve French #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))
33*38c8a9a5SSteve French 
34*38c8a9a5SSteve French /* produce a md4 message digest from data of length n bytes */
35*38c8a9a5SSteve French static int
mdfour(unsigned char * md4_hash,unsigned char * link_str,int link_len)36*38c8a9a5SSteve French mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
37*38c8a9a5SSteve French {
38*38c8a9a5SSteve French 	int rc;
39*38c8a9a5SSteve French 	struct md4_ctx mctx;
40*38c8a9a5SSteve French 
41*38c8a9a5SSteve French 	rc = cifs_md4_init(&mctx);
42*38c8a9a5SSteve French 	if (rc) {
43*38c8a9a5SSteve French 		cifs_dbg(VFS, "%s: Could not init MD4\n", __func__);
44*38c8a9a5SSteve French 		goto mdfour_err;
45*38c8a9a5SSteve French 	}
46*38c8a9a5SSteve French 	rc = cifs_md4_update(&mctx, link_str, link_len);
47*38c8a9a5SSteve French 	if (rc) {
48*38c8a9a5SSteve French 		cifs_dbg(VFS, "%s: Could not update MD4\n", __func__);
49*38c8a9a5SSteve French 		goto mdfour_err;
50*38c8a9a5SSteve French 	}
51*38c8a9a5SSteve French 	rc = cifs_md4_final(&mctx, md4_hash);
52*38c8a9a5SSteve French 	if (rc)
53*38c8a9a5SSteve French 		cifs_dbg(VFS, "%s: Could not finalize MD4\n", __func__);
54*38c8a9a5SSteve French 
55*38c8a9a5SSteve French 
56*38c8a9a5SSteve French mdfour_err:
57*38c8a9a5SSteve French 	return rc;
58*38c8a9a5SSteve French }
59*38c8a9a5SSteve French 
60*38c8a9a5SSteve French /*
61*38c8a9a5SSteve French  * Creates the MD4 Hash of the users password in NT UNICODE.
62*38c8a9a5SSteve French  */
63*38c8a9a5SSteve French 
64*38c8a9a5SSteve French int
E_md4hash(const unsigned char * passwd,unsigned char * p16,const struct nls_table * codepage)65*38c8a9a5SSteve French E_md4hash(const unsigned char *passwd, unsigned char *p16,
66*38c8a9a5SSteve French 	const struct nls_table *codepage)
67*38c8a9a5SSteve French {
68*38c8a9a5SSteve French 	int rc;
69*38c8a9a5SSteve French 	int len;
70*38c8a9a5SSteve French 	__le16 wpwd[129];
71*38c8a9a5SSteve French 
72*38c8a9a5SSteve French 	/* Password cannot be longer than 128 characters */
73*38c8a9a5SSteve French 	if (passwd) /* Password must be converted to NT unicode */
74*38c8a9a5SSteve French 		len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
75*38c8a9a5SSteve French 	else {
76*38c8a9a5SSteve French 		len = 0;
77*38c8a9a5SSteve French 		*wpwd = 0; /* Ensure string is null terminated */
78*38c8a9a5SSteve French 	}
79*38c8a9a5SSteve French 
80*38c8a9a5SSteve French 	rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
81*38c8a9a5SSteve French 	memzero_explicit(wpwd, sizeof(wpwd));
82*38c8a9a5SSteve French 
83*38c8a9a5SSteve French 	return rc;
84*38c8a9a5SSteve French }
85