Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003    Unix SMB/Netbios implementation.
0004    Version 1.9.
0005    SMB parameters and setup
0006    Copyright (C) Andrew Tridgell 1992-2000
0007    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
0008    Modified by Jeremy Allison 1995.
0009    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
0010    Modified by Steve French (sfrench@us.ibm.com) 2002-2003
0011 
0012 */
0013 
0014 #include <linux/module.h>
0015 #include <linux/slab.h>
0016 #include <linux/fips.h>
0017 #include <linux/fs.h>
0018 #include <linux/string.h>
0019 #include <linux/kernel.h>
0020 #include <linux/random.h>
0021 #include "cifs_fs_sb.h"
0022 #include "cifs_unicode.h"
0023 #include "cifspdu.h"
0024 #include "cifsglob.h"
0025 #include "cifs_debug.h"
0026 #include "cifsproto.h"
0027 #include "../smbfs_common/md4.h"
0028 
0029 #ifndef false
0030 #define false 0
0031 #endif
0032 #ifndef true
0033 #define true 1
0034 #endif
0035 
0036 /* following came from the other byteorder.h to avoid include conflicts */
0037 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
0038 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
0039 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))
0040 
0041 /* produce a md4 message digest from data of length n bytes */
0042 static int
0043 mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
0044 {
0045     int rc;
0046     struct md4_ctx mctx;
0047 
0048     rc = cifs_md4_init(&mctx);
0049     if (rc) {
0050         cifs_dbg(VFS, "%s: Could not init MD4\n", __func__);
0051         goto mdfour_err;
0052     }
0053     rc = cifs_md4_update(&mctx, link_str, link_len);
0054     if (rc) {
0055         cifs_dbg(VFS, "%s: Could not update MD4\n", __func__);
0056         goto mdfour_err;
0057     }
0058     rc = cifs_md4_final(&mctx, md4_hash);
0059     if (rc)
0060         cifs_dbg(VFS, "%s: Could not finalize MD4\n", __func__);
0061 
0062 
0063 mdfour_err:
0064     return rc;
0065 }
0066 
0067 /*
0068  * Creates the MD4 Hash of the users password in NT UNICODE.
0069  */
0070 
0071 int
0072 E_md4hash(const unsigned char *passwd, unsigned char *p16,
0073     const struct nls_table *codepage)
0074 {
0075     int rc;
0076     int len;
0077     __le16 wpwd[129];
0078 
0079     /* Password cannot be longer than 128 characters */
0080     if (passwd) /* Password must be converted to NT unicode */
0081         len = cifs_strtoUTF16(wpwd, passwd, 128, codepage);
0082     else {
0083         len = 0;
0084         *wpwd = 0; /* Ensure string is null terminated */
0085     }
0086 
0087     rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
0088     memzero_explicit(wpwd, sizeof(wpwd));
0089 
0090     return rc;
0091 }