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 "cifspdu.h"
24 #include "cifsglob.h"
25 #include "cifs_debug.h"
26 #include "cifsproto.h"
27 #include "../smbfs_common/md4.h"
28 
29 #ifndef false
30 #define false 0
31 #endif
32 #ifndef true
33 #define true 1
34 #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 
41 /* produce a md4 message digest from data of length n bytes */
42 static int
mdfour(unsigned char * md4_hash,unsigned char * link_str,int link_len)43 mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
44 {
45 	int rc;
46 	struct md4_ctx mctx;
47 
48 	rc = cifs_md4_init(&mctx);
49 	if (rc) {
50 		cifs_dbg(VFS, "%s: Could not init MD4\n", __func__);
51 		goto mdfour_err;
52 	}
53 	rc = cifs_md4_update(&mctx, link_str, link_len);
54 	if (rc) {
55 		cifs_dbg(VFS, "%s: Could not update MD4\n", __func__);
56 		goto mdfour_err;
57 	}
58 	rc = cifs_md4_final(&mctx, md4_hash);
59 	if (rc)
60 		cifs_dbg(VFS, "%s: Could not finalize MD4\n", __func__);
61 
62 
63 mdfour_err:
64 	return rc;
65 }
66 
67 /*
68  * Creates the MD4 Hash of the users password in NT UNICODE.
69  */
70 
71 int
E_md4hash(const unsigned char * passwd,unsigned char * p16,const struct nls_table * codepage)72 E_md4hash(const unsigned char *passwd, unsigned char *p16,
73 	const struct nls_table *codepage)
74 {
75 	int rc;
76 	int len;
77 	__le16 wpwd[129];
78 
79 	/* Password cannot be longer than 128 characters */
80 	if (passwd) /* Password must be converted to NT unicode */
81 		len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
82 	else {
83 		len = 0;
84 		*wpwd = 0; /* Ensure string is null terminated */
85 	}
86 
87 	rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
88 	memzero_explicit(wpwd, sizeof(wpwd));
89 
90 	return rc;
91 }
92