"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /* mod_ntlm file: $Id: smbdes.inc.c,v 1.2 2003/02/21 01:55:14 casz Exp $ */
2
3
4 /*
5 * Unix SMB/Netbios implementation. Version 1.9.
6 *
7 * a partial implementation of DES designed for use in the SMB
8 * authentication protocol
9 *
10 * Copyright (C) Andrew Tridgell 1997 This program is free software; you
11 * can redistribute it and/or modify it under the terms of the GNU General
12 * Public License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version. This
14 * program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details. You should have received a copy of the GNU General
18 * Public License along with this program; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /* NOTES:
22 *
23 * This code makes no attempt to be fast! In fact, it is a very slow
24 * implementation
25 *
26 * This code is NOT a complete DES implementation. It implements only the
27 * minimum necessary for SMB authentication, as used by all SMB products
28 * (including every copy of Microsoft Windows95 ever sold)
29 *
30 * In particular, it can only do a unchained forward DES pass. This means
31 * it is not possible to use this code for encryption/decryption of data,
32 * instead it is only useful as a "hash" algorithm.
33 *
34 * There is no entry point into this code that allows normal DES
35 * operation.
36 *
37 * I believe this means that this code does not come under ITAR
38 * regulations but this is NOT a legal opinion. If you are concerned about
39 * the applicability of ITAR regulations to this code then you should
40 * confirm it for yourself (and maybe let me know if you come up with a
41 * different answer to the one above) */
42
43 static int perm1[56] =
44 {57, 49, 41, 33, 25, 17, 9,
45 1, 58, 50, 42, 34, 26, 18,
46 10, 2, 59, 51, 43, 35, 27,
47 19, 11, 3, 60, 52, 44, 36,
48 63, 55, 47, 39, 31, 23, 15,
49 7, 62, 54, 46, 38, 30, 22,
50 14, 6, 61, 53, 45, 37, 29,
51 21, 13, 5, 28, 20, 12, 4};
52
53 static int perm2[48] =
54 {14, 17, 11, 24, 1, 5,
55 3, 28, 15, 6, 21, 10,
56 23, 19, 12, 4, 26, 8,
57 16, 7, 27, 20, 13, 2,
58 41, 52, 31, 37, 47, 55,
59 30, 40, 51, 45, 33, 48,
60 44, 49, 39, 56, 34, 53,
61 46, 42, 50, 36, 29, 32};
62
63 static int perm3[64] =
64 {58, 50, 42, 34, 26, 18, 10, 2,
65 60, 52, 44, 36, 28, 20, 12, 4,
66 62, 54, 46, 38, 30, 22, 14, 6,
67 64, 56, 48, 40, 32, 24, 16, 8,
68 57, 49, 41, 33, 25, 17, 9, 1,
69 59, 51, 43, 35, 27, 19, 11, 3,
70 61, 53, 45, 37, 29, 21, 13, 5,
71 63, 55, 47, 39, 31, 23, 15, 7};
72
73 static int perm4[48] =
74 {32, 1, 2, 3, 4, 5,
75 4, 5, 6, 7, 8, 9,
76 8, 9, 10, 11, 12, 13,
77 12, 13, 14, 15, 16, 17,
78 16, 17, 18, 19, 20, 21,
79 20, 21, 22, 23, 24, 25,
80 24, 25, 26, 27, 28, 29,
81 28, 29, 30, 31, 32, 1};
82
83 static int perm5[32] =
84 {16, 7, 20, 21,
85 29, 12, 28, 17,
86 1, 15, 23, 26,
87 5, 18, 31, 10,
88 2, 8, 24, 14,
89 32, 27, 3, 9,
90 19, 13, 30, 6,
91 22, 11, 4, 25};
92
93 static int perm6[64] =
94 {40, 8, 48, 16, 56, 24, 64, 32,
95 39, 7, 47, 15, 55, 23, 63, 31,
96 38, 6, 46, 14, 54, 22, 62, 30,
97 37, 5, 45, 13, 53, 21, 61, 29,
98 36, 4, 44, 12, 52, 20, 60, 28,
99 35, 3, 43, 11, 51, 19, 59, 27,
100 34, 2, 42, 10, 50, 18, 58, 26,
101 33, 1, 41, 9, 49, 17, 57, 25};
102
103 static int sc[16] =
104 {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
105
106 static int sbox[8][4][16] =
107 {
108 {
109 {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
110 {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
111 {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
112 {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}},
113 {
114 {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
115 {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
116 {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
117 {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}},
118 {
119 {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
120 {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
121 {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
122 {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}},
123 {
124 {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
125 {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
126 {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
127 {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}},
128 {
129 {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
130 {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
131 {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
132 {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}},
133 {
134 {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
135 {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
136 {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
137 {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}},
138 {
139 {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
140 {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
141 {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
142 {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}},
143 {
144 {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
145 {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
146 {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
147 {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}
148 }
149 };
150
151 static void
152 permute(char *out, char *in, int *p, int n)
153 {
154 int i;
155 for (i = 0; i < n; i++)
156 out[i] = in[p[i] - 1];
157 }
158
159 static void
160 l_shift(char *d, int count, int n)
161 {
162 char out[64];
163 int i;
164 for (i = 0; i < n; i++)
165 out[i] = d[(i + count) % n];
166 for (i = 0; i < n; i++)
167 d[i] = out[i];
168 }
169
170 static void
171 concat(char *out, char *in1, char *in2, int l1, int l2)
172 {
173 while (l1--)
174 *out++ = *in1++;
175 while (l2--)
176 *out++ = *in2++;
177 }
178
179 static void
180 xor(char *out, char *in1, char *in2, int n)
181 {
182 int i;
183 for (i = 0; i < n; i++)
184 out[i] = in1[i] ^ in2[i];
185 }
186
187 static void
188 dohash(char *out, char *in, char *key)
189 {
190 int i, j, k;
191 char pk1[56];
192 char c[28];
193 char d[28];
194 char cd[56];
195 char ki[16][48];
196 char pd1[64];
197 char l[32], r[32];
198 char rl[64];
199
200 permute(pk1, key, perm1, 56);
201
202 for (i = 0; i < 28; i++)
203 c[i] = pk1[i];
204 for (i = 0; i < 28; i++)
205 d[i] = pk1[i + 28];
206
207 for (i = 0; i < 16; i++) {
208 l_shift(c, sc[i], 28);
209 l_shift(d, sc[i], 28);
210
211 concat(cd, c, d, 28, 28);
212 permute(ki[i], cd, perm2, 48);
213 }
214
215 permute(pd1, in, perm3, 64);
216
217 for (j = 0; j < 32; j++) {
218 l[j] = pd1[j];
219 r[j] = pd1[j + 32];
220 }
221
222 for (i = 0; i < 16; i++) {
223 char er[48];
224 char erk[48];
225 char b[8][6];
226 char cb[32];
227 char pcb[32];
228 char r2[32];
229
230 permute(er, r, perm4, 48);
231
232 xor(erk, er, ki[i], 48);
233
234 for (j = 0; j < 8; j++)
235 for (k = 0; k < 6; k++)
236 b[j][k] = erk[j * 6 + k];
237
238 for (j = 0; j < 8; j++) {
239 int m, n;
240 m = (b[j][0] << 1) | b[j][5];
241
242 n = (b[j][1] << 3) | (b[j][2] << 2) | (b[j][3] << 1) | b[j][4];
243
244 for (k = 0; k < 4; k++)
245 b[j][k] = (sbox[j][m][n] & (1 << (3 - k))) ? 1 : 0;
246 }
247
248 for (j = 0; j < 8; j++)
249 for (k = 0; k < 4; k++)
250 cb[j * 4 + k] = b[j][k];
251 permute(pcb, cb, perm5, 32);
252
253 xor(r2, l, pcb, 32);
254
255 for (j = 0; j < 32; j++)
256 l[j] = r[j];
257
258 for (j = 0; j < 32; j++)
259 r[j] = r2[j];
260 }
261
262 concat(rl, r, l, 32, 32);
263
264 permute(out, rl, perm6, 64);
265 }
266
267 static void
268 str_to_key(unsigned char *str, unsigned char *key)
269 {
270 int i;
271
272 key[0] = str[0] >> 1;
273 key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2);
274 key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3);
275 key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4);
276 key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5);
277 key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6);
278 key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7);
279 key[7] = str[6] & 0x7F;
280 for (i = 0; i < 8; i++) {
281 key[i] = (key[i] << 1);
282 }
283 }
284
285 static void
286 smbhash(unsigned char *out, unsigned char *in, unsigned char *key)
287 {
288 int i;
289 char outb[64];
290 char inb[64];
291 char keyb[64];
292 unsigned char key2[8];
293
294 str_to_key(key, key2);
295
296 for (i = 0; i < 64; i++) {
297 inb[i] = (in[i / 8] & (1 << (7 - (i % 8)))) ? 1 : 0;
298 keyb[i] = (key2[i / 8] & (1 << (7 - (i % 8)))) ? 1 : 0;
299 outb[i] = 0;
300 }
301
302 dohash(outb, inb, keyb);
303
304 for (i = 0; i < 8; i++) {
305 out[i] = 0;
306 }
307
308 for (i = 0; i < 64; i++) {
309 if (outb[i])
310 out[i / 8] |= (1 << (7 - (i % 8)));
311 }
312 }
313
314 static void
315 E_P16(unsigned char *p14, unsigned char *p16)
316 {
317 unsigned char sp8[8] =
318 {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
319 smbhash(p16, sp8, p14);
320 smbhash(p16 + 8, sp8, p14 + 7);
321 }
322
323 static void
324 E_P24(unsigned char *p21, unsigned char *c8, unsigned char *p24)
325 {
326 smbhash(p24, c8, p21);
327 smbhash(p24 + 8, c8, p21 + 7);
328 smbhash(p24 + 16, c8, p21 + 14);
329 }