smbencrypt.c 2.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
S
Steve French 已提交
2
/*
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10
   Unix SMB/Netbios implementation.
   Version 1.9.
   SMB parameters and setup
   Copyright (C) Andrew Tridgell 1992-2000
   Copyright (C) Luke Kenneth Casson Leighton 1996-2000
   Modified by Jeremy Allison 1995.
   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2002-2003
   Modified by Steve French (sfrench@us.ibm.com) 2002-2003
11

L
Linus Torvalds 已提交
12 13 14
*/

#include <linux/module.h>
15
#include <linux/slab.h>
16
#include <linux/fips.h>
L
Linus Torvalds 已提交
17 18 19 20
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/random.h>
21
#include "cifs_fs_sb.h"
L
Linus Torvalds 已提交
22 23
#include "cifs_unicode.h"
#include "cifspdu.h"
24
#include "cifsglob.h"
L
Linus Torvalds 已提交
25
#include "cifs_debug.h"
26
#include "cifsproto.h"
27
#include "../common/md4.h"
L
Linus Torvalds 已提交
28

29 30
#ifndef false
#define false 0
L
Linus Torvalds 已提交
31
#endif
32 33
#ifndef true
#define true 1
L
Linus Torvalds 已提交
34 35 36 37 38 39 40
#endif

/* following came from the other byteorder.h to avoid include conflicts */
#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
#define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((__u16)(val)))

41
/* produce a md4 message digest from data of length n bytes */
42
static int
43 44 45
mdfour(unsigned char *md4_hash, unsigned char *link_str, int link_len)
{
	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 52
		goto mdfour_err;
	}
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 57
		goto mdfour_err;
	}
58
	rc = cifs_md4_final(&mctx, md4_hash);
59
	if (rc)
60 61
		cifs_dbg(VFS, "%s: Could not finalize MD4\n", __func__);

L
Linus Torvalds 已提交
62

63 64 65 66
mdfour_err:
	return rc;
}

S
Steve French 已提交
67
/*
L
Linus Torvalds 已提交
68 69 70
 * Creates the MD4 Hash of the users password in NT UNICODE.
 */

71
int
72 73
E_md4hash(const unsigned char *passwd, unsigned char *p16,
	const struct nls_table *codepage)
L
Linus Torvalds 已提交
74
{
75
	int rc;
L
Linus Torvalds 已提交
76
	int len;
77
	__le16 wpwd[129];
L
Linus Torvalds 已提交
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 {
L
Linus Torvalds 已提交
83
		len = 0;
84 85
		*wpwd = 0; /* Ensure string is null terminated */
	}
L
Linus Torvalds 已提交
86

87
	rc = mdfour(p16, (unsigned char *) wpwd, len * sizeof(__le16));
88
	memzero_explicit(wpwd, sizeof(wpwd));
89 90

	return rc;
L
Linus Torvalds 已提交
91
}