1 /*
2 * Copyright 2004 Hans Leidekker
3 *
4 * Based on LMHash.c from libcifs
5 *
6 * Copyright (C) 2004 by Christopher R. Hertel
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include <stdarg.h>
24
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winternl.h"
29
30 #include "crypt.h"
31
32 static const unsigned char CRYPT_LMhash_Magic[8] =
33 { 'K', 'G', 'S', '!', '@', '#', '$', '%' };
34
35 static void CRYPT_LMhash( unsigned char *dst, const unsigned char *pwd, const int len )
36 {
37 int i, max = 14;
38 unsigned char tmp_pwd[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
39
40 max = len > max ? max : len;
41
42 for (i = 0; i < max; i++)
43 tmp_pwd[i] = pwd[i];
44
45 CRYPT_DEShash( dst, tmp_pwd, CRYPT_LMhash_Magic );
46 CRYPT_DEShash( &dst[8], &tmp_pwd[7], CRYPT_LMhash_Magic );
47 }
48
49 NTSTATUS WINAPI SystemFunction006( LPCSTR password, LPSTR hash )
50 {
51 CRYPT_LMhash( (unsigned char*)hash, (const unsigned char*)password, strlen(password) );
52
53 return STATUS_SUCCESS;
54 }
55
56 /******************************************************************************
57 * SystemFunction008 [ADVAPI32.@]
58 *
59 * Creates a LM response from a challenge and a password hash
60 *
61 * PARAMS
62 * challenge [I] Challenge from authentication server
63 * hash [I] NTLM hash (from SystemFunction006)
64 * response [O] response to send back to the server
65 *
66 * RETURNS
67 * Success: STATUS_SUCCESS
68 * Failure: STATUS_UNSUCCESSFUL
69 *
70 * NOTES
71 * see http://davenport.sourceforge.net/ntlm.html#theLmResponse
72 *
73 */
74 NTSTATUS WINAPI SystemFunction008(const BYTE *challenge, const BYTE *hash, LPBYTE response)
75 {
76 BYTE key[7*3];
77
78 if (!challenge || !response)
79 return STATUS_UNSUCCESSFUL;
80
81 memset(key, 0, sizeof key);
82 memcpy(key, hash, 0x10);
83
84 CRYPT_DEShash(response, key, challenge);
85 CRYPT_DEShash(response+8, key+7, challenge);
86 CRYPT_DEShash(response+16, key+14, challenge);
87
88 return STATUS_SUCCESS;
89 }
90
91 /******************************************************************************
92 * SystemFunction009 [ADVAPI32.@]
93 *
94 * Seems to do the same as SystemFunction008 ...
95 */
96 NTSTATUS WINAPI SystemFunction009(const BYTE *challenge, const BYTE *hash, LPBYTE response)
97 {
98 return SystemFunction008(challenge, hash, response);
99 }
100
101 /******************************************************************************
102 * SystemFunction001 [ADVAPI32.@]
103 *
104 * Encrypts a single block of data using DES
105 *
106 * PARAMS
107 * data [I] data to encrypt (8 bytes)
108 * key [I] key data (7 bytes)
109 * output [O] the encrypted data (8 bytes)
110 *
111 * RETURNS
112 * Success: STATUS_SUCCESS
113 * Failure: STATUS_UNSUCCESSFUL
114 *
115 */
116 NTSTATUS WINAPI SystemFunction001(const BYTE *data, const BYTE *key, LPBYTE output)
117 {
118 if (!data || !output)
119 return STATUS_UNSUCCESSFUL;
120 CRYPT_DEShash(output, key, data);
121 return STATUS_SUCCESS;
122 }
123
124 /******************************************************************************
125 * SystemFunction002 [ADVAPI32.@]
126 *
127 * Decrypts a single block of data using DES
128 *
129 * PARAMS
130 * data [I] data to decrypt (8 bytes)
131 * key [I] key data (7 bytes)
132 * output [O] the decrypted data (8 bytes)
133 *
134 * RETURNS
135 * Success: STATUS_SUCCESS
136 * Failure: STATUS_UNSUCCESSFUL
137 *
138 */
139 NTSTATUS WINAPI SystemFunction002(const BYTE *data, const BYTE *key, LPBYTE output)
140 {
141 if (!data || !output)
142 return STATUS_UNSUCCESSFUL;
143 CRYPT_DESunhash(output, key, data);
144 return STATUS_SUCCESS;
145 }
146
147 /******************************************************************************
148 * SystemFunction003 [ADVAPI32.@]
149 *
150 * Hashes a key using DES and a fixed datablock
151 *
152 * PARAMS
153 * key [I] key data (7 bytes)
154 * output [O] hashed key (8 bytes)
155 *
156 * RETURNS
157 * Success: STATUS_SUCCESS
158 * Failure: STATUS_UNSUCCESSFUL
159 *
160 */
161 NTSTATUS WINAPI SystemFunction003(const BYTE *key, LPBYTE output)
162 {
163 if (!output)
164 return STATUS_UNSUCCESSFUL;
165 CRYPT_DEShash(output, key, CRYPT_LMhash_Magic);
166 return STATUS_SUCCESS;
167 }
168
169 /******************************************************************************
170 * SystemFunction004 [ADVAPI32.@]
171 *
172 * Encrypts a block of data with DES in ECB mode, preserving the length
173 *
174 * PARAMS
175 * data [I] data to encrypt
176 * key [I] key data (up to 7 bytes)
177 * output [O] buffer to receive encrypted data
178 *
179 * RETURNS
180 * Success: STATUS_SUCCESS
181 * Failure: STATUS_BUFFER_TOO_SMALL if the output buffer is too small
182 * Failure: STATUS_INVALID_PARAMETER_2 if the key is zero length
183 *
184 * NOTES
185 * Encrypt buffer size should be input size rounded up to 8 bytes
186 * plus an extra 8 bytes.
187 */
188 NTSTATUS WINAPI SystemFunction004(const struct ustring *in,
189 const struct ustring *key,
190 struct ustring *out)
191 {
192 union {
193 unsigned char uc[8];
194 unsigned int ui[2];
195 } data;
196 unsigned char deskey[7];
197 unsigned int crypt_len, ofs;
198
199 if (key->Length<=0)
200 return STATUS_INVALID_PARAMETER_2;
201
202 crypt_len = ((in->Length+7)&~7);
203 if (out->MaximumLength < (crypt_len+8))
204 return STATUS_BUFFER_TOO_SMALL;
205
206 data.ui[0] = in->Length;
207 data.ui[1] = 1;
208
209 if (key->Length<sizeof deskey)
210 {
211 memset(deskey, 0, sizeof deskey);
212 memcpy(deskey, key->Buffer, key->Length);
213 }
214 else
215 memcpy(deskey, key->Buffer, sizeof deskey);
216
217 CRYPT_DEShash(out->Buffer, deskey, data.uc);
218
219 for(ofs=0; ofs<(crypt_len-8); ofs+=8)
220 CRYPT_DEShash(out->Buffer+8+ofs, deskey, in->Buffer+ofs);
221
222 memset(data.uc, 0, sizeof data.uc);
223 memcpy(data.uc, in->Buffer+ofs, in->Length +8-crypt_len);
224 CRYPT_DEShash(out->Buffer+8+ofs, deskey, data.uc);
225
226 out->Length = crypt_len+8;
227
228 return STATUS_SUCCESS;
229 }
230
231 /******************************************************************************
232 * SystemFunction005 [ADVAPI32.@]
233 *
234 * Decrypts a block of data with DES in ECB mode
235 *
236 * PARAMS
237 * data [I] data to decrypt
238 * key [I] key data (up to 7 bytes)
239 * output [O] buffer to receive decrypted data
240 *
241 * RETURNS
242 * Success: STATUS_SUCCESS
243 * Failure: STATUS_BUFFER_TOO_SMALL if the output buffer is too small
244 * Failure: STATUS_INVALID_PARAMETER_2 if the key is zero length
245 *
246 */
247 NTSTATUS WINAPI SystemFunction005(const struct ustring *in,
248 const struct ustring *key,
249 struct ustring *out)
250 {
251 union {
252 unsigned char uc[8];
253 unsigned int ui[2];
254 } data;
255 unsigned char deskey[7];
256 unsigned int ofs, crypt_len;
257
258 if (key->Length<=0)
259 return STATUS_INVALID_PARAMETER_2;
260
261 if (key->Length<sizeof deskey)
262 {
263 memset(deskey, 0, sizeof deskey);
264 memcpy(deskey, key->Buffer, key->Length);
265 }
266 else
267 memcpy(deskey, key->Buffer, sizeof deskey);
268
269 CRYPT_DESunhash(data.uc, deskey, in->Buffer);
270
271 if (data.ui[1] != 1)
272 return STATUS_UNKNOWN_REVISION;
273
274 crypt_len = data.ui[0];
275 if (crypt_len > out->MaximumLength)
276 return STATUS_BUFFER_TOO_SMALL;
277
278 for (ofs=0; (ofs+8)<crypt_len; ofs+=8)
279 CRYPT_DESunhash(out->Buffer+ofs, deskey, in->Buffer+ofs+8);
280
281 if (ofs<crypt_len)
282 {
283 CRYPT_DESunhash(data.uc, deskey, in->Buffer+ofs+8);
284 memcpy(out->Buffer+ofs, data.uc, crypt_len-ofs);
285 }
286
287 out->Length = crypt_len;
288
289 return STATUS_SUCCESS;
290 }
291
292 /******************************************************************************
293 * SystemFunction012 [ADVAPI32.@]
294 * SystemFunction014 [ADVAPI32.@]
295 * SystemFunction016 [ADVAPI32.@]
296 * SystemFunction018 [ADVAPI32.@]
297 * SystemFunction020 [ADVAPI32.@]
298 * SystemFunction022 [ADVAPI32.@]
299 *
300 * Encrypts two DES blocks with two keys
301 *
302 * PARAMS
303 * data [I] data to encrypt (16 bytes)
304 * key [I] key data (two lots of 7 bytes)
305 * output [O] buffer to receive encrypted data (16 bytes)
306 *
307 * RETURNS
308 * Success: STATUS_SUCCESS
309 * Failure: STATUS_UNSUCCESSFUL if the input or output buffer is NULL
310 */
311 NTSTATUS WINAPI SystemFunction012(const BYTE *in, const BYTE *key, LPBYTE out)
312 {
313 if (!in || !out)
314 return STATUS_UNSUCCESSFUL;
315
316 CRYPT_DEShash(out, key, in);
317 CRYPT_DEShash(out+8, key+7, in+8);
318 return STATUS_SUCCESS;
319 }
320
321 /******************************************************************************
322 * SystemFunction013 [ADVAPI32.@]
323 * SystemFunction015 [ADVAPI32.@]
324 * SystemFunction017 [ADVAPI32.@]
325 * SystemFunction019 [ADVAPI32.@]
326 * SystemFunction021 [ADVAPI32.@]
327 * SystemFunction023 [ADVAPI32.@]
328 *
329 * Decrypts two DES blocks with two keys
330 *
331 * PARAMS
332 * data [I] data to decrypt (16 bytes)
333 * key [I] key data (two lots of 7 bytes)
334 * output [O] buffer to receive decrypted data (16 bytes)
335 *
336 * RETURNS
337 * Success: STATUS_SUCCESS
338 * Failure: STATUS_UNSUCCESSFUL if the input or output buffer is NULL
339 */
340 NTSTATUS WINAPI SystemFunction013(const BYTE *in, const BYTE *key, LPBYTE out)
341 {
342 if (!in || !out)
343 return STATUS_UNSUCCESSFUL;
344
345 CRYPT_DESunhash(out, key, in);
346 CRYPT_DESunhash(out+8, key+7, in+8);
347 return STATUS_SUCCESS;
348 }
349
350 /******************************************************************************
351 * SystemFunction024 [ADVAPI32.@]
352 *
353 * Encrypts two DES blocks with a 32 bit key...
354 *
355 * PARAMS
356 * data [I] data to encrypt (16 bytes)
357 * key [I] key data (4 bytes)
358 * output [O] buffer to receive encrypted data (16 bytes)
359 *
360 * RETURNS
361 * Success: STATUS_SUCCESS
362 */
363 NTSTATUS WINAPI SystemFunction024(const BYTE *in, const BYTE *key, LPBYTE out)
364 {
365 BYTE deskey[0x10];
366
367 memcpy(deskey, key, 4);
368 memcpy(deskey+4, key, 4);
369 memcpy(deskey+8, key, 4);
370 memcpy(deskey+12, key, 4);
371
372 CRYPT_DEShash(out, deskey, in);
373 CRYPT_DEShash(out+8, deskey+7, in+8);
374
375 return STATUS_SUCCESS;
376 }
377
378 /******************************************************************************
379 * SystemFunction025 [ADVAPI32.@]
380 *
381 * Decrypts two DES blocks with a 32 bit key...
382 *
383 * PARAMS
384 * data [I] data to encrypt (16 bytes)
385 * key [I] key data (4 bytes)
386 * output [O] buffer to receive encrypted data (16 bytes)
387 *
388 * RETURNS
389 * Success: STATUS_SUCCESS
390 */
391 NTSTATUS WINAPI SystemFunction025(const BYTE *in, const BYTE *key, LPBYTE out)
392 {
393 BYTE deskey[0x10];
394
395 memcpy(deskey, key, 4);
396 memcpy(deskey+4, key, 4);
397 memcpy(deskey+8, key, 4);
398 memcpy(deskey+12, key, 4);
399
400 CRYPT_DESunhash(out, deskey, in);
401 CRYPT_DESunhash(out+8, deskey+7, in+8);
402
403 return STATUS_SUCCESS;
404 }
405
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.