winemine: Updated Slovenian translation.
[wine/gsoc_dplay.git] / dlls / advapi32 / crypt_lmhash.c
blob345c89ddb738c6cc2e8dfe793628144b3a46e994
1 /*
2 * Copyright 2004 Hans Leidekker
4 * Based on LMHash.c from libcifs
6 * Copyright (C) 2004 by Christopher R. Hertel
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.
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.
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
23 #include <stdarg.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winternl.h"
30 #include "crypt.h"
32 static const unsigned char CRYPT_LMhash_Magic[8] =
33 { 'K', 'G', 'S', '!', '@', '#', '$', '%' };
35 static void CRYPT_LMhash( unsigned char *dst, const unsigned char *pwd, const int len )
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 };
40 max = len > max ? max : len;
42 for (i = 0; i < max; i++)
43 tmp_pwd[i] = pwd[i];
45 CRYPT_DEShash( dst, tmp_pwd, CRYPT_LMhash_Magic );
46 CRYPT_DEShash( &dst[8], &tmp_pwd[7], CRYPT_LMhash_Magic );
49 NTSTATUS WINAPI SystemFunction006( LPCSTR password, LPSTR hash )
51 CRYPT_LMhash( (unsigned char*)hash, (const unsigned char*)password, strlen(password) );
53 return STATUS_SUCCESS;
56 /******************************************************************************
57 * SystemFunction008 [ADVAPI32.@]
59 * Creates a LM response from a challenge and a password hash
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
66 * RETURNS
67 * Success: STATUS_SUCCESS
68 * Failure: STATUS_UNSUCCESSFUL
70 * NOTES
71 * see http://davenport.sourceforge.net/ntlm.html#theLmResponse
74 NTSTATUS WINAPI SystemFunction008(const BYTE *challenge, const BYTE *hash, LPBYTE response)
76 BYTE key[7*3];
78 if (!challenge || !response)
79 return STATUS_UNSUCCESSFUL;
81 memset(key, 0, sizeof key);
82 memcpy(key, hash, 0x10);
84 CRYPT_DEShash(response, key, challenge);
85 CRYPT_DEShash(response+8, key+7, challenge);
86 CRYPT_DEShash(response+16, key+14, challenge);
88 return STATUS_SUCCESS;
91 /******************************************************************************
92 * SystemFunction009 [ADVAPI32.@]
94 * Seems to do the same as SystemFunction008 ...
96 NTSTATUS WINAPI SystemFunction009(const BYTE *challenge, const BYTE *hash, LPBYTE response)
98 return SystemFunction008(challenge, hash, response);
101 /******************************************************************************
102 * SystemFunction001 [ADVAPI32.@]
104 * Encrypts a single block of data using DES
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)
111 * RETURNS
112 * Success: STATUS_SUCCESS
113 * Failure: STATUS_UNSUCCESSFUL
116 NTSTATUS WINAPI SystemFunction001(const BYTE *data, const BYTE *key, LPBYTE output)
118 if (!data || !output)
119 return STATUS_UNSUCCESSFUL;
120 CRYPT_DEShash(output, key, data);
121 return STATUS_SUCCESS;
124 /******************************************************************************
125 * SystemFunction002 [ADVAPI32.@]
127 * Decrypts a single block of data using DES
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)
134 * RETURNS
135 * Success: STATUS_SUCCESS
136 * Failure: STATUS_UNSUCCESSFUL
139 NTSTATUS WINAPI SystemFunction002(const BYTE *data, const BYTE *key, LPBYTE output)
141 if (!data || !output)
142 return STATUS_UNSUCCESSFUL;
143 CRYPT_DESunhash(output, key, data);
144 return STATUS_SUCCESS;
147 /******************************************************************************
148 * SystemFunction003 [ADVAPI32.@]
150 * Hashes a key using DES and a fixed datablock
152 * PARAMS
153 * key [I] key data (7 bytes)
154 * output [O] hashed key (8 bytes)
156 * RETURNS
157 * Success: STATUS_SUCCESS
158 * Failure: STATUS_UNSUCCESSFUL
161 NTSTATUS WINAPI SystemFunction003(const BYTE *key, LPBYTE output)
163 if (!output)
164 return STATUS_UNSUCCESSFUL;
165 CRYPT_DEShash(output, key, CRYPT_LMhash_Magic);
166 return STATUS_SUCCESS;
169 /******************************************************************************
170 * SystemFunction004 [ADVAPI32.@]
172 * Encrypts a block of data with DES in ECB mode, preserving the length
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
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
184 * NOTES
185 * Encrypt buffer size should be input size rounded up to 8 bytes
186 * plus an extra 8 bytes.
188 NTSTATUS WINAPI SystemFunction004(const struct ustring *in,
189 const struct ustring *key,
190 struct ustring *out)
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;
199 if (key->Length<=0)
200 return STATUS_INVALID_PARAMETER_2;
202 crypt_len = ((in->Length+7)&~7);
203 if (out->MaximumLength < (crypt_len+8))
204 return STATUS_BUFFER_TOO_SMALL;
206 data.ui[0] = in->Length;
207 data.ui[1] = 1;
209 if (key->Length<sizeof deskey)
211 memset(deskey, 0, sizeof deskey);
212 memcpy(deskey, key->Buffer, key->Length);
214 else
215 memcpy(deskey, key->Buffer, sizeof deskey);
217 CRYPT_DEShash(out->Buffer, deskey, data.uc);
219 for(ofs=0; ofs<(crypt_len-8); ofs+=8)
220 CRYPT_DEShash(out->Buffer+8+ofs, deskey, in->Buffer+ofs);
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);
226 out->Length = crypt_len+8;
228 return STATUS_SUCCESS;
231 /******************************************************************************
232 * SystemFunction005 [ADVAPI32.@]
234 * Decrypts a block of data with DES in ECB mode
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
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
247 NTSTATUS WINAPI SystemFunction005(const struct ustring *in,
248 const struct ustring *key,
249 struct ustring *out)
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;
258 if (key->Length<=0)
259 return STATUS_INVALID_PARAMETER_2;
261 if (key->Length<sizeof deskey)
263 memset(deskey, 0, sizeof deskey);
264 memcpy(deskey, key->Buffer, key->Length);
266 else
267 memcpy(deskey, key->Buffer, sizeof deskey);
269 CRYPT_DESunhash(data.uc, deskey, in->Buffer);
271 if (data.ui[1] != 1)
272 return STATUS_UNKNOWN_REVISION;
274 crypt_len = data.ui[0];
275 if (crypt_len > out->MaximumLength)
276 return STATUS_BUFFER_TOO_SMALL;
278 for (ofs=0; (ofs+8)<crypt_len; ofs+=8)
279 CRYPT_DESunhash(out->Buffer+ofs, deskey, in->Buffer+ofs+8);
281 if (ofs<crypt_len)
283 CRYPT_DESunhash(data.uc, deskey, in->Buffer+ofs+8);
284 memcpy(out->Buffer+ofs, data.uc, crypt_len-ofs);
287 out->Length = crypt_len;
289 return STATUS_SUCCESS;
292 /******************************************************************************
293 * SystemFunction012 [ADVAPI32.@]
294 * SystemFunction014 [ADVAPI32.@]
295 * SystemFunction016 [ADVAPI32.@]
296 * SystemFunction018 [ADVAPI32.@]
297 * SystemFunction020 [ADVAPI32.@]
298 * SystemFunction022 [ADVAPI32.@]
300 * Encrypts two DES blocks with two keys
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)
307 * RETURNS
308 * Success: STATUS_SUCCESS
309 * Failure: STATUS_UNSUCCESSFUL if the input or output buffer is NULL
311 NTSTATUS WINAPI SystemFunction012(const BYTE *in, const BYTE *key, LPBYTE out)
313 if (!in || !out)
314 return STATUS_UNSUCCESSFUL;
316 CRYPT_DEShash(out, key, in);
317 CRYPT_DEShash(out+8, key+7, in+8);
318 return STATUS_SUCCESS;
321 /******************************************************************************
322 * SystemFunction013 [ADVAPI32.@]
323 * SystemFunction015 [ADVAPI32.@]
324 * SystemFunction017 [ADVAPI32.@]
325 * SystemFunction019 [ADVAPI32.@]
326 * SystemFunction021 [ADVAPI32.@]
327 * SystemFunction023 [ADVAPI32.@]
329 * Decrypts two DES blocks with two keys
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)
336 * RETURNS
337 * Success: STATUS_SUCCESS
338 * Failure: STATUS_UNSUCCESSFUL if the input or output buffer is NULL
340 NTSTATUS WINAPI SystemFunction013(const BYTE *in, const BYTE *key, LPBYTE out)
342 if (!in || !out)
343 return STATUS_UNSUCCESSFUL;
345 CRYPT_DESunhash(out, key, in);
346 CRYPT_DESunhash(out+8, key+7, in+8);
347 return STATUS_SUCCESS;
350 /******************************************************************************
351 * SystemFunction024 [ADVAPI32.@]
353 * Encrypts two DES blocks with a 32 bit key...
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)
360 * RETURNS
361 * Success: STATUS_SUCCESS
363 NTSTATUS WINAPI SystemFunction024(const BYTE *in, const BYTE *key, LPBYTE out)
365 BYTE deskey[0x10];
367 memcpy(deskey, key, 4);
368 memcpy(deskey+4, key, 4);
369 memcpy(deskey+8, key, 4);
370 memcpy(deskey+12, key, 4);
372 CRYPT_DEShash(out, deskey, in);
373 CRYPT_DEShash(out+8, deskey+7, in+8);
375 return STATUS_SUCCESS;
378 /******************************************************************************
379 * SystemFunction025 [ADVAPI32.@]
381 * Decrypts two DES blocks with a 32 bit key...
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)
388 * RETURNS
389 * Success: STATUS_SUCCESS
391 NTSTATUS WINAPI SystemFunction025(const BYTE *in, const BYTE *key, LPBYTE out)
393 BYTE deskey[0x10];
395 memcpy(deskey, key, 4);
396 memcpy(deskey+4, key, 4);
397 memcpy(deskey+8, key, 4);
398 memcpy(deskey+12, key, 4);
400 CRYPT_DESunhash(out, deskey, in);
401 CRYPT_DESunhash(out+8, deskey+7, in+8);
403 return STATUS_SUCCESS;