Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Ard Biesheuvel | 9a394d1 | 2019-08-15 12:01:12 +0300 | [diff] [blame] | 16 | #include <linux/fips.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/fs.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/random.h> |
Steve French | 2baa268 | 2014-09-27 02:19:01 -0500 | [diff] [blame] | 21 | #include "cifs_fs_sb.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "cifs_unicode.h" |
| 23 | #include "cifspdu.h" |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 24 | #include "cifsglob.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "cifs_debug.h" |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 26 | #include "cifsproto.h" |
Steve French | 23e91d8 | 2021-09-08 23:59:26 -0500 | [diff] [blame] | 27 | #include "../smbfs_common/md4.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 29 | #ifndef false |
| 30 | #define false 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #endif |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 32 | #ifndef true |
| 33 | #define true 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 41 | /* produce a md4 message digest from data of length n bytes */ |
Ronnie Sahlberg | 76a3c92 | 2021-08-19 20:34:58 +1000 | [diff] [blame] | 42 | static int |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 43 | mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len) |
| 44 | { |
| 45 | int rc; |
Ronnie Sahlberg | 42c2197 | 2021-08-20 09:32:56 +1000 | [diff] [blame] | 46 | struct md4_ctx mctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Ronnie Sahlberg | 42c2197 | 2021-08-20 09:32:56 +1000 | [diff] [blame] | 48 | rc = cifs_md4_init(&mctx); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 49 | if (rc) { |
Ronnie Sahlberg | 42c2197 | 2021-08-20 09:32:56 +1000 | [diff] [blame] | 50 | cifs_dbg(VFS, "%s: Could not init MD4\n", __func__); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 51 | goto mdfour_err; |
| 52 | } |
Ronnie Sahlberg | 42c2197 | 2021-08-20 09:32:56 +1000 | [diff] [blame] | 53 | rc = cifs_md4_update(&mctx, link_str, link_len); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 54 | if (rc) { |
Ronnie Sahlberg | 42c2197 | 2021-08-20 09:32:56 +1000 | [diff] [blame] | 55 | cifs_dbg(VFS, "%s: Could not update MD4\n", __func__); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 56 | goto mdfour_err; |
| 57 | } |
Ronnie Sahlberg | 42c2197 | 2021-08-20 09:32:56 +1000 | [diff] [blame] | 58 | rc = cifs_md4_final(&mctx, md4_hash); |
Shirish Pargaonkar | 14cae32 | 2011-06-20 16:14:03 -0500 | [diff] [blame] | 59 | if (rc) |
Ronnie Sahlberg | 42c2197 | 2021-08-20 09:32:56 +1000 | [diff] [blame] | 60 | cifs_dbg(VFS, "%s: Could not finalize MD4\n", __func__); |
| 61 | |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 62 | |
| 63 | mdfour_err: |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 64 | return rc; |
| 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | * Creates the MD4 Hash of the users password in NT UNICODE. |
| 69 | */ |
| 70 | |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 71 | int |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 72 | E_md4hash(const unsigned char *passwd, unsigned char *p16, |
| 73 | const struct nls_table *codepage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 75 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | int len; |
Steve French | 9c32c63 | 2011-11-10 12:48:20 -0600 | [diff] [blame] | 77 | __le16 wpwd[129]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | /* Password cannot be longer than 128 characters */ |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 80 | if (passwd) /* Password must be converted to NT unicode */ |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 81 | len = cifs_strtoUTF16(wpwd, passwd, 128, codepage); |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 82 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | len = 0; |
Shirish Pargaonkar | 9ef5992 | 2011-10-20 13:21:59 -0500 | [diff] [blame] | 84 | *wpwd = 0; /* Ensure string is null terminated */ |
| 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Steve French | 9c32c63 | 2011-11-10 12:48:20 -0600 | [diff] [blame] | 87 | rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16)); |
Giel van Schijndel | f99dbfa | 2015-01-06 22:37:00 +0100 | [diff] [blame] | 88 | memzero_explicit(wpwd, sizeof(wpwd)); |
Shirish Pargaonkar | ee2c925 | 2011-01-27 09:58:04 -0600 | [diff] [blame] | 89 | |
| 90 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |