3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: dll/win32/advapi32/misc/sysfun.c
6 * PURPOSE: advapi32.dll system functions (undocumented)
7 * PROGRAMMER: Emanuele Aliberti
11 * 20080424 Ported from WINE
15 #include <crypt/crypt.h>
17 static const unsigned char CRYPT_LMhash_Magic
[8] =
18 { 'K', 'G', 'S', '!', '@', '#', '$', '%' };
20 /******************************************************************************
21 * SystemFunction001 [ADVAPI32.@]
23 * Encrypts a single block of data using DES
26 * data [I] data to encrypt (8 bytes)
27 * key [I] key data (7 bytes)
28 * output [O] the encrypted data (8 bytes)
31 * Success: STATUS_SUCCESS
32 * Failure: STATUS_UNSUCCESSFUL
36 WINAPI
SystemFunction001(const BYTE
*data
, const BYTE
*key
, LPBYTE output
)
39 return STATUS_UNSUCCESSFUL
;
40 CRYPT_DEShash(output
, key
, data
);
41 return STATUS_SUCCESS
;
45 /******************************************************************************
46 * SystemFunction002 [ADVAPI32.@]
48 * Decrypts a single block of data using DES
51 * data [I] data to decrypt (8 bytes)
52 * key [I] key data (7 bytes)
53 * output [O] the decrypted data (8 bytes)
56 * Success: STATUS_SUCCESS
57 * Failure: STATUS_UNSUCCESSFUL
61 WINAPI
SystemFunction002(const BYTE
*data
, const BYTE
*key
, LPBYTE output
)
64 return STATUS_UNSUCCESSFUL
;
65 CRYPT_DESunhash(output
, key
, data
);
66 return STATUS_SUCCESS
;
70 /******************************************************************************
71 * SystemFunction003 [ADVAPI32.@]
73 * Hashes a key using DES and a fixed datablock
76 * key [I] key data (7 bytes)
77 * output [O] hashed key (8 bytes)
80 * Success: STATUS_SUCCESS
81 * Failure: STATUS_UNSUCCESSFUL
85 WINAPI
SystemFunction003(const BYTE
*key
, LPBYTE output
)
88 return STATUS_UNSUCCESSFUL
;
89 CRYPT_DEShash(output
, key
, CRYPT_LMhash_Magic
);
90 return STATUS_SUCCESS
;
94 /******************************************************************************
95 * SystemFunction004 [ADVAPI32.@]
97 * Encrypts a block of data with DES in ECB mode, preserving the length
100 * data [I] data to encrypt
101 * key [I] key data (up to 7 bytes)
102 * output [O] buffer to receive encrypted data
105 * Success: STATUS_SUCCESS
106 * Failure: STATUS_BUFFER_TOO_SMALL if the output buffer is too small
107 * Failure: STATUS_INVALID_PARAMETER_2 if the key is zero length
110 * Encrypt buffer size should be input size rounded up to 8 bytes
111 * plus an extra 8 bytes.
114 WINAPI
SystemFunction004(const struct ustring
*in
,
115 const struct ustring
*key
,
122 unsigned char deskey
[7];
123 unsigned int crypt_len
, ofs
;
126 return STATUS_INVALID_PARAMETER_2
;
128 crypt_len
= ((in
->Length
+7)&~7);
129 if (out
->MaximumLength
< (crypt_len
+8))
130 return STATUS_BUFFER_TOO_SMALL
;
132 data
.ui
[0] = in
->Length
;
135 if (key
->Length
<sizeof deskey
)
137 memset(deskey
, 0, sizeof deskey
);
138 memcpy(deskey
, key
->Buffer
, key
->Length
);
141 memcpy(deskey
, key
->Buffer
, sizeof deskey
);
143 CRYPT_DEShash(out
->Buffer
, deskey
, data
.uc
);
145 for(ofs
=0; ofs
<(crypt_len
-8); ofs
+=8)
146 CRYPT_DEShash(out
->Buffer
+8+ofs
, deskey
, in
->Buffer
+ofs
);
148 memset(data
.uc
, 0, sizeof data
.uc
);
149 memcpy(data
.uc
, in
->Buffer
+ofs
, in
->Length
+8-crypt_len
);
150 CRYPT_DEShash(out
->Buffer
+8+ofs
, deskey
, data
.uc
);
152 out
->Length
= crypt_len
+8;
154 return STATUS_SUCCESS
;
157 /******************************************************************************
158 * SystemFunction005 [ADVAPI32.@]
160 * Decrypts a block of data with DES in ECB mode
163 * data [I] data to decrypt
164 * key [I] key data (up to 7 bytes)
165 * output [O] buffer to receive decrypted data
168 * Success: STATUS_SUCCESS
169 * Failure: STATUS_BUFFER_TOO_SMALL if the output buffer is too small
170 * Failure: STATUS_INVALID_PARAMETER_2 if the key is zero length
174 WINAPI
SystemFunction005(const struct ustring
*in
,
175 const struct ustring
*key
,
182 unsigned char deskey
[7];
183 unsigned int ofs
, crypt_len
;
186 return STATUS_INVALID_PARAMETER_2
;
188 if (key
->Length
<sizeof deskey
)
190 memset(deskey
, 0, sizeof deskey
);
191 memcpy(deskey
, key
->Buffer
, key
->Length
);
194 memcpy(deskey
, key
->Buffer
, sizeof deskey
);
196 CRYPT_DESunhash(data
.uc
, deskey
, in
->Buffer
);
199 return STATUS_UNKNOWN_REVISION
;
201 crypt_len
= data
.ui
[0];
202 if (crypt_len
> out
->MaximumLength
)
203 return STATUS_BUFFER_TOO_SMALL
;
205 for (ofs
=0; (ofs
+8)<crypt_len
; ofs
+=8)
206 CRYPT_DESunhash(out
->Buffer
+ofs
, deskey
, in
->Buffer
+ofs
+8);
210 CRYPT_DESunhash(data
.uc
, deskey
, in
->Buffer
+ofs
+8);
211 memcpy(out
->Buffer
+ofs
, data
.uc
, crypt_len
-ofs
);
214 out
->Length
= crypt_len
;
216 return STATUS_SUCCESS
;
219 /******************************************************************************
220 * SystemFunction007 [ADVAPI32.@]
222 * MD4 hash a unicode string
225 * string [I] the string to hash
226 * output [O] the md4 hash of the string (16 bytes)
229 * Success: STATUS_SUCCESS
230 * Failure: STATUS_UNSUCCESSFUL
234 WINAPI
SystemFunction007(const UNICODE_STRING
*string
, LPBYTE hash
)
239 MD4Update( &ctx
, (const BYTE
*)string
->Buffer
, string
->Length
);
241 memcpy( hash
, ctx
.digest
, 0x10 );
243 return STATUS_SUCCESS
;
246 /******************************************************************************
247 * SystemFunction008 [ADVAPI32.@]
249 * Creates a LM response from a challenge and a password hash
252 * challenge [I] Challenge from authentication server
253 * hash [I] NTLM hash (from SystemFunction006)
254 * response [O] response to send back to the server
257 * Success: STATUS_SUCCESS
258 * Failure: STATUS_UNSUCCESSFUL
261 * see http://davenport.sourceforge.net/ntlm.html#theLmResponse
265 WINAPI
SystemFunction008(const BYTE
*challenge
, const BYTE
*hash
, LPBYTE response
)
269 if (!challenge
|| !response
)
270 return STATUS_UNSUCCESSFUL
;
272 memset(key
, 0, sizeof key
);
273 memcpy(key
, hash
, 0x10);
275 CRYPT_DEShash(response
, key
, challenge
);
276 CRYPT_DEShash(response
+8, key
+7, challenge
);
277 CRYPT_DEShash(response
+16, key
+14, challenge
);
279 return STATUS_SUCCESS
;
282 /******************************************************************************
283 * SystemFunction009 [ADVAPI32.@]
285 * Seems to do the same as SystemFunction008...
288 WINAPI
SystemFunction009(const BYTE
*challenge
, const BYTE
*hash
, LPBYTE response
)
290 return SystemFunction008(challenge
, hash
, response
);
293 /******************************************************************************
294 * SystemFunction010 [ADVAPI32.@]
295 * SystemFunction011 [ADVAPI32.@]
297 * MD4 hashes 16 bytes of data
300 * unknown [] seems to have no effect on the output
301 * data [I] pointer to data to hash (16 bytes)
302 * output [O] the md4 hash of the data (16 bytes)
305 * Success: STATUS_SUCCESS
306 * Failure: STATUS_UNSUCCESSFUL
310 WINAPI
SystemFunction010(LPVOID unknown
, const BYTE
*data
, LPBYTE hash
)
315 MD4Update( &ctx
, data
, 0x10 );
317 memcpy( hash
, ctx
.digest
, 0x10 );
319 return STATUS_SUCCESS
;
322 /******************************************************************************
323 * SystemFunction012 [ADVAPI32.@]
324 * SystemFunction014 [ADVAPI32.@]
325 * SystemFunction016 [ADVAPI32.@]
326 * SystemFunction018 [ADVAPI32.@]
327 * SystemFunction020 [ADVAPI32.@]
328 * SystemFunction022 [ADVAPI32.@]
330 * Encrypts two DES blocks with two keys
333 * data [I] data to encrypt (16 bytes)
334 * key [I] key data (two lots of 7 bytes)
335 * output [O] buffer to receive encrypted data (16 bytes)
338 * Success: STATUS_SUCCESS
339 * Failure: STATUS_UNSUCCESSFUL if the input or output buffer is NULL
342 WINAPI
SystemFunction012(const BYTE
*in
, const BYTE
*key
, LPBYTE out
)
345 return STATUS_UNSUCCESSFUL
;
347 CRYPT_DEShash(out
, key
, in
);
348 CRYPT_DEShash(out
+8, key
+7, in
+8);
349 return STATUS_SUCCESS
;
352 /******************************************************************************
353 * SystemFunction013 [ADVAPI32.@]
354 * SystemFunction015 [ADVAPI32.@]
355 * SystemFunction017 [ADVAPI32.@]
356 * SystemFunction019 [ADVAPI32.@]
357 * SystemFunction021 [ADVAPI32.@]
358 * SystemFunction023 [ADVAPI32.@]
360 * Decrypts two DES blocks with two keys
363 * data [I] data to decrypt (16 bytes)
364 * key [I] key data (two lots of 7 bytes)
365 * output [O] buffer to receive decrypted data (16 bytes)
368 * Success: STATUS_SUCCESS
369 * Failure: STATUS_UNSUCCESSFUL if the input or output buffer is NULL
372 WINAPI
SystemFunction013(const BYTE
*in
, const BYTE
*key
, LPBYTE out
)
375 return STATUS_UNSUCCESSFUL
;
376 CRYPT_DESunhash(out
, key
, in
);
377 CRYPT_DESunhash(out
+8, key
+7, in
+8);
378 return STATUS_SUCCESS
;
381 /******************************************************************************
382 * SystemFunction024 [ADVAPI32.@]
384 * Encrypts two DES blocks with a 32 bit key...
387 * data [I] data to encrypt (16 bytes)
388 * key [I] key data (4 bytes)
389 * output [O] buffer to receive encrypted data (16 bytes)
392 * Success: STATUS_SUCCESS
395 WINAPI
SystemFunction024(const BYTE
*in
, const BYTE
*key
, LPBYTE out
)
399 memcpy(deskey
, key
, 4);
400 memcpy(deskey
+4, key
, 4);
401 memcpy(deskey
+8, key
, 4);
402 memcpy(deskey
+12, key
, 4);
404 CRYPT_DEShash(out
, deskey
, in
);
405 CRYPT_DEShash(out
+8, deskey
+7, in
+8);
407 return STATUS_SUCCESS
;
410 /******************************************************************************
411 * SystemFunction025 [ADVAPI32.@]
413 * Decrypts two DES blocks with a 32 bit key...
416 * data [I] data to encrypt (16 bytes)
417 * key [I] key data (4 bytes)
418 * output [O] buffer to receive encrypted data (16 bytes)
421 * Success: STATUS_SUCCESS
424 WINAPI
SystemFunction025(const BYTE
*in
, const BYTE
*key
, LPBYTE out
)
428 memcpy(deskey
, key
, 4);
429 memcpy(deskey
+4, key
, 4);
430 memcpy(deskey
+8, key
, 4);
431 memcpy(deskey
+12, key
, 4);
433 CRYPT_DESunhash(out
, deskey
, in
);
434 CRYPT_DESunhash(out
+8, deskey
+7, in
+8);
436 return STATUS_SUCCESS
;
439 /**********************************************************************
445 SystemFunction028(INT a
, INT b
)
447 //NDRCContextBinding()
448 //SystemFunction034()
449 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
454 /**********************************************************************
460 SystemFunction029(INT a
, INT b
)
462 //I_RpcBindingIsClientLocal()
463 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
468 /******************************************************************************
469 * SystemFunction030 (ADVAPI32.@)
471 * Tests if two blocks of 16 bytes are equal
474 * b1,b2 [I] block of 16 bytes
477 * TRUE if blocks are the same
478 * FALSE if blocks are different
481 WINAPI
SystemFunction030(PVOID b1
, PVOID b2
)
483 return !memcmp(b1
, b2
, 0x10);
487 /******************************************************************************
488 * SystemFunction032 [ADVAPI32.@]
490 * Encrypts a string data using ARC4
493 * data [I/O] data to encrypt
497 * Success: STATUS_SUCCESS
498 * Failure: STATUS_UNSUCCESSFUL
501 * see http://web.it.kth.se/~rom/ntsec.html#crypto-strongavail
504 WINAPI
SystemFunction032(struct ustring
*data
, struct ustring
*key
)
508 arc4_init(&a4i
, key
->Buffer
, key
->Length
);
509 arc4_ProcessString(&a4i
, data
->Buffer
, data
->Length
);
511 return STATUS_SUCCESS
;
515 /**********************************************************************
521 SystemFunction033(INT a
, INT b
)
523 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
527 /**********************************************************************
533 SystemFunction034(INT a
, INT b
)
535 //RpcBindingToStringBindingW
536 //I_RpcMapWin32Status
537 //RpcStringBindingParseW
539 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
544 /**********************************************************************
550 SystemFunction035(LPCSTR lpszDllFilePath
)
555 /******************************************************************************
556 * SystemFunction036 (ADVAPI32.@)
558 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h
561 * pbBuffer [O] Pointer to memory to receive random bytes.
562 * dwLen [I] Number of random bytes to fetch.
565 * Always TRUE in my tests
569 SystemFunction036(PVOID pbBuffer
, ULONG dwLen
)
571 ////////////////////////////////////////////////////////////////
572 //////////////////// B I G W A R N I N G !!! ////////////////
573 // This function will output numbers based on the tick count. //
574 // It will NOT OUPUT CRYPTOGRAPHIC-SAFE RANDOM NUMBERS !!! //
575 ////////////////////////////////////////////////////////////////
581 if(!pbBuffer
|| !dwLen
)
583 /* This function always returns TRUE, even if invalid parameters were passed. (verified under WinXP SP2) */
587 /* Get the first seed from the tick count */
588 dwSeed
= GetTickCount();
590 /* We will access the buffer bytewise */
591 pBuffer
= (PBYTE
)pbBuffer
;
595 /* Use the pseudo random number generator RtlRandom, which outputs a 4-byte value and a new seed */
596 uPseudoRandom
= RtlRandom(&dwSeed
);
600 /* Get each byte from the pseudo random number and store it in the buffer */
601 *pBuffer
= (BYTE
)(uPseudoRandom
>> 8 * (dwLen
% 4) & 0xFF);
603 } while(--dwLen
% 4);