blob: 4a048775386910936400a2d2e052116cf6754db0 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Steve French790fe572007-07-07 19:25:05 +00002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 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
Steve French50c2f752007-07-13 00:33:32 +000011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012*/
13
14#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Ard Biesheuvel9a394d12019-08-15 12:01:12 +030016#include <linux/fips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/fs.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/random.h>
Steve French2baa2682014-09-27 02:19:01 -050021#include "cifs_fs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "cifs_unicode.h"
23#include "cifspdu.h"
Steve French39798772006-05-31 22:40:51 +000024#include "cifsglob.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "cifs_debug.h"
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060026#include "cifsproto.h"
Steve French23e91d82021-09-08 23:59:26 -050027#include "../smbfs_common/md4.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Steve French4b18f2a2008-04-29 00:06:05 +000029#ifndef false
30#define false 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#endif
Steve French4b18f2a2008-04-29 00:06:05 +000032#ifndef true
33#define true 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060041/* produce a md4 message digest from data of length n bytes */
Ronnie Sahlberg76a3c922021-08-19 20:34:58 +100042static int
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060043mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
44{
45 int rc;
Ronnie Sahlberg42c21972021-08-20 09:32:56 +100046 struct md4_ctx mctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ronnie Sahlberg42c21972021-08-20 09:32:56 +100048 rc = cifs_md4_init(&mctx);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060049 if (rc) {
Ronnie Sahlberg42c21972021-08-20 09:32:56 +100050 cifs_dbg(VFS, "%s: Could not init MD4\n", __func__);
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060051 goto mdfour_err;
52 }
Ronnie Sahlberg42c21972021-08-20 09:32:56 +100053 rc = cifs_md4_update(&mctx, link_str, link_len);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050054 if (rc) {
Ronnie Sahlberg42c21972021-08-20 09:32:56 +100055 cifs_dbg(VFS, "%s: Could not update MD4\n", __func__);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050056 goto mdfour_err;
57 }
Ronnie Sahlberg42c21972021-08-20 09:32:56 +100058 rc = cifs_md4_final(&mctx, md4_hash);
Shirish Pargaonkar14cae322011-06-20 16:14:03 -050059 if (rc)
Ronnie Sahlberg42c21972021-08-20 09:32:56 +100060 cifs_dbg(VFS, "%s: Could not finalize MD4\n", __func__);
61
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060062
63mdfour_err:
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060064 return rc;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * Creates the MD4 Hash of the users password in NT UNICODE.
69 */
70
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060071int
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -050072E_md4hash(const unsigned char *passwd, unsigned char *p16,
73 const struct nls_table *codepage)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060075 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int len;
Steve French9c32c632011-11-10 12:48:20 -060077 __le16 wpwd[129];
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 /* Password cannot be longer than 128 characters */
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -050080 if (passwd) /* Password must be converted to NT unicode */
Steve Frenchacbbb762012-01-18 22:32:33 -060081 len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -050082 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 len = 0;
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -050084 *wpwd = 0; /* Ensure string is null terminated */
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Steve French9c32c632011-11-10 12:48:20 -060087 rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
Giel van Schijndelf99dbfa2015-01-06 22:37:00 +010088 memzero_explicit(wpwd, sizeof(wpwd));
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -060089
90 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}