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