xref: /dragonfly/sys/netproto/smb/smb_crypt.c (revision b40e316c)
1 /*
2  * Copyright (c) 2000-2001, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/netsmb/smb_crypt.c,v 1.1.2.3 2001/09/03 08:55:11 bp Exp $
33  * $DragonFly: src/sys/netproto/smb/smb_crypt.c,v 1.3 2003/08/07 21:17:38 dillon Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/proc.h>
41 #include <sys/fcntl.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/sysctl.h>
45 
46 #include <sys/md4.h>
47 #include <sys/iconv.h>
48 
49 #include "smb.h"
50 #include "smb_conn.h"
51 #include "smb_subr.h"
52 #include "smb_dev.h"
53 
54 #include "opt_netsmb.h"
55 
56 #ifdef NETSMBCRYPTO
57 
58 #include <crypto/des/des.h>
59 
60 static u_char N8[] = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
61 
62 
63 static void
64 smb_E(const u_char *key, u_char *data, u_char *dest)
65 {
66 	des_key_schedule *ksp;
67 	u_char kk[8];
68 
69 	kk[0] = key[0] & 0xfe;
70 	kk[1] = key[0] << 7 | (key[1] >> 1 & 0xfe);
71 	kk[2] = key[1] << 6 | (key[2] >> 2 & 0xfe);
72 	kk[3] = key[2] << 5 | (key[3] >> 3 & 0xfe);
73 	kk[4] = key[3] << 4 | (key[4] >> 4 & 0xfe);
74 	kk[5] = key[4] << 3 | (key[5] >> 5 & 0xfe);
75 	kk[6] = key[5] << 2 | (key[6] >> 6 & 0xfe);
76 	kk[7] = key[6] << 1;
77 	ksp = malloc(sizeof(des_key_schedule), M_SMBTEMP, M_WAITOK);
78 	des_set_key((des_cblock *)kk, *ksp);
79 	des_ecb_encrypt((des_cblock *)data, (des_cblock *)dest, *ksp, 1);
80 	free(ksp, M_SMBTEMP);
81 }
82 #endif
83 
84 
85 int
86 smb_encrypt(const u_char *apwd, u_char *C8, u_char *RN)
87 {
88 #ifdef NETSMBCRYPTO
89 	u_char *p, *P14, *S21;
90 
91 	p = malloc(14 + 21, M_SMBTEMP, M_WAITOK);
92 	bzero(p, 14 + 21);
93 	P14 = p;
94 	S21 = p + 14;
95 	bcopy(apwd, P14, min(14, strlen(apwd)));
96 	/*
97 	 * S21 = concat(Ex(P14, N8), zeros(5));
98 	 */
99 	smb_E(P14, N8, S21);
100 	smb_E(P14 + 7, N8, S21 + 8);
101 
102 	smb_E(S21, C8, RN);
103 	smb_E(S21 + 7, C8, RN + 8);
104 	smb_E(S21 + 14, C8, RN + 16);
105 	free(p, M_SMBTEMP);
106 	return 0;
107 #else
108 	SMBERROR("password encryption is not available\n");
109 	bzero(RN, 24);
110 	return EAUTH;
111 #endif
112 }
113 
114 int
115 smb_ntencrypt(const u_char *apwd, u_char *C8, u_char *RN)
116 {
117 #ifdef NETSMBCRYPTO
118 	u_char S21[21];
119 	u_int16_t *unipwd;
120 	MD4_CTX *ctxp;
121 	int len;
122 
123 	len = strlen(apwd);
124 	unipwd = malloc((len + 1) * sizeof(u_int16_t), M_SMBTEMP, M_WAITOK);
125 	/*
126 	 * S21 = concat(MD4(U(apwd)), zeros(5));
127 	 */
128 	smb_strtouni(unipwd, apwd);
129 	ctxp = malloc(sizeof(MD4_CTX), M_SMBTEMP, M_WAITOK);
130 	MD4Init(ctxp);
131 	MD4Update(ctxp, (u_char*)unipwd, len * sizeof(u_int16_t));
132 	free(unipwd, M_SMBTEMP);
133 	bzero(S21, 21);
134 	MD4Final(S21, ctxp);
135 	free(ctxp, M_SMBTEMP);
136 
137 	smb_E(S21, C8, RN);
138 	smb_E(S21 + 7, C8, RN + 8);
139 	smb_E(S21 + 14, C8, RN + 16);
140 	return 0;
141 #else
142 	SMBERROR("password encryption is not available\n");
143 	bzero(RN, 24);
144 	return EAUTH;
145 #endif
146 }
147 
148