Merge tag 'pci-v5.16-fixes-1' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux-block.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
14#include <linux/module.h>
5a0e3ad6 15#include <linux/slab.h>
9a394d12 16#include <linux/fips.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"
23e91d8b 27#include "../smbfs_common/md4.h"
1da177e4 28
4b18f2a9
SF
29#ifndef false
30#define false 0
1da177e4 31#endif
4b18f2a9
SF
32#ifndef true
33#define true 1
1da177e4
LT
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
ee2c9258 41/* produce a md4 message digest from data of length n bytes */
76a3c92e 42static int
ee2c9258
SP
43mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
44{
45 int rc;
42c21973 46 struct md4_ctx mctx;
82fb82be 47
42c21973 48 rc = cifs_md4_init(&mctx);
ee2c9258 49 if (rc) {
42c21973 50 cifs_dbg(VFS, "%s: Could not init MD4\n", __func__);
ee2c9258
SP
51 goto mdfour_err;
52 }
42c21973 53 rc = cifs_md4_update(&mctx, link_str, link_len);
14cae324 54 if (rc) {
42c21973 55 cifs_dbg(VFS, "%s: Could not update MD4\n", __func__);
14cae324
SP
56 goto mdfour_err;
57 }
42c21973 58 rc = cifs_md4_final(&mctx, md4_hash);
14cae324 59 if (rc)
42c21973
RS
60 cifs_dbg(VFS, "%s: Could not finalize MD4\n", __func__);
61
1da177e4 62
ee2c9258 63mdfour_err:
ee2c9258
SP
64 return rc;
65}
66
790fe579 67/*
1da177e4
LT
68 * Creates the MD4 Hash of the users password in NT UNICODE.
69 */
70
ee2c9258 71int
9ef5992e
SP
72E_md4hash(const unsigned char *passwd, unsigned char *p16,
73 const struct nls_table *codepage)
1da177e4 74{
ee2c9258 75 int rc;
1da177e4 76 int len;
9c32c63b 77 __le16 wpwd[129];
1da177e4
LT
78
79 /* Password cannot be longer than 128 characters */
9ef5992e 80 if (passwd) /* Password must be converted to NT unicode */
acbbb76a 81 len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
9ef5992e 82 else {
1da177e4 83 len = 0;
9ef5992e
SP
84 *wpwd = 0; /* Ensure string is null terminated */
85 }
1da177e4 86
9c32c63b 87 rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
f99dbfa4 88 memzero_explicit(wpwd, sizeof(wpwd));
ee2c9258
SP
89
90 return rc;
1da177e4 91}