Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygensysfunc.c
Go to the documentation of this file.
00001 /* 00002 * 00003 * COPYRIGHT: See COPYING in the top level directory 00004 * PROJECT: ReactOS system libraries 00005 * FILE: dll/win32/advapi32/misc/sysfun.c 00006 * PURPOSE: advapi32.dll system functions (undocumented) 00007 * PROGRAMMER: Emanuele Aliberti 00008 * UPDATE HISTORY: 00009 * 19990413 EA created 00010 * 19990415 EA 00011 * 20080424 Ported from WINE 00012 */ 00013 00014 #include <advapi32.h> 00015 00016 static const unsigned char CRYPT_LMhash_Magic[8] = 00017 { 'K', 'G', 'S', '!', '@', '#', '$', '%' }; 00018 00019 /****************************************************************************** 00020 * SystemFunction001 [ADVAPI32.@] 00021 * 00022 * Encrypts a single block of data using DES 00023 * 00024 * PARAMS 00025 * data [I] data to encrypt (8 bytes) 00026 * key [I] key data (7 bytes) 00027 * output [O] the encrypted data (8 bytes) 00028 * 00029 * RETURNS 00030 * Success: STATUS_SUCCESS 00031 * Failure: STATUS_UNSUCCESSFUL 00032 * 00033 */ 00034 NTSTATUS 00035 WINAPI SystemFunction001(const BYTE *data, const BYTE *key, LPBYTE output) 00036 { 00037 if (!data || !output) 00038 return STATUS_UNSUCCESSFUL; 00039 CRYPT_DEShash(output, key, data); 00040 return STATUS_SUCCESS; 00041 } 00042 00043 00044 /****************************************************************************** 00045 * SystemFunction002 [ADVAPI32.@] 00046 * 00047 * Decrypts a single block of data using DES 00048 * 00049 * PARAMS 00050 * data [I] data to decrypt (8 bytes) 00051 * key [I] key data (7 bytes) 00052 * output [O] the decrypted data (8 bytes) 00053 * 00054 * RETURNS 00055 * Success: STATUS_SUCCESS 00056 * Failure: STATUS_UNSUCCESSFUL 00057 * 00058 */ 00059 NTSTATUS 00060 WINAPI SystemFunction002(const BYTE *data, const BYTE *key, LPBYTE output) 00061 { 00062 if (!data || !output) 00063 return STATUS_UNSUCCESSFUL; 00064 CRYPT_DESunhash(output, key, data); 00065 return STATUS_SUCCESS; 00066 } 00067 00068 00069 /****************************************************************************** 00070 * SystemFunction003 [ADVAPI32.@] 00071 * 00072 * Hashes a key using DES and a fixed datablock 00073 * 00074 * PARAMS 00075 * key [I] key data (7 bytes) 00076 * output [O] hashed key (8 bytes) 00077 * 00078 * RETURNS 00079 * Success: STATUS_SUCCESS 00080 * Failure: STATUS_UNSUCCESSFUL 00081 * 00082 */ 00083 NTSTATUS 00084 WINAPI SystemFunction003(const BYTE *key, LPBYTE output) 00085 { 00086 if (!output) 00087 return STATUS_UNSUCCESSFUL; 00088 CRYPT_DEShash(output, key, CRYPT_LMhash_Magic); 00089 return STATUS_SUCCESS; 00090 } 00091 00092 00093 /****************************************************************************** 00094 * SystemFunction004 [ADVAPI32.@] 00095 * 00096 * Encrypts a block of data with DES in ECB mode, preserving the length 00097 * 00098 * PARAMS 00099 * data [I] data to encrypt 00100 * key [I] key data (up to 7 bytes) 00101 * output [O] buffer to receive encrypted data 00102 * 00103 * RETURNS 00104 * Success: STATUS_SUCCESS 00105 * Failure: STATUS_BUFFER_TOO_SMALL if the output buffer is too small 00106 * Failure: STATUS_INVALID_PARAMETER_2 if the key is zero length 00107 * 00108 * NOTES 00109 * Encrypt buffer size should be input size rounded up to 8 bytes 00110 * plus an extra 8 bytes. 00111 */ 00112 NTSTATUS 00113 WINAPI SystemFunction004(const struct ustring *in, 00114 const struct ustring *key, 00115 struct ustring *out) 00116 { 00117 union { 00118 unsigned char uc[8]; 00119 unsigned int ui[2]; 00120 } data; 00121 unsigned char deskey[7]; 00122 unsigned int crypt_len, ofs; 00123 00124 if (key->Length<=0) 00125 return STATUS_INVALID_PARAMETER_2; 00126 00127 crypt_len = ((in->Length+7)&~7); 00128 if (out->MaximumLength < (crypt_len+8)) 00129 return STATUS_BUFFER_TOO_SMALL; 00130 00131 data.ui[0] = in->Length; 00132 data.ui[1] = 1; 00133 00134 if (key->Length<sizeof deskey) 00135 { 00136 memset(deskey, 0, sizeof deskey); 00137 memcpy(deskey, key->Buffer, key->Length); 00138 } 00139 else 00140 memcpy(deskey, key->Buffer, sizeof deskey); 00141 00142 CRYPT_DEShash(out->Buffer, deskey, data.uc); 00143 00144 for(ofs=0; ofs<(crypt_len-8); ofs+=8) 00145 CRYPT_DEShash(out->Buffer+8+ofs, deskey, in->Buffer+ofs); 00146 00147 memset(data.uc, 0, sizeof data.uc); 00148 memcpy(data.uc, in->Buffer+ofs, in->Length +8-crypt_len); 00149 CRYPT_DEShash(out->Buffer+8+ofs, deskey, data.uc); 00150 00151 out->Length = crypt_len+8; 00152 00153 return STATUS_SUCCESS; 00154 } 00155 00156 /****************************************************************************** 00157 * SystemFunction005 [ADVAPI32.@] 00158 * 00159 * Decrypts a block of data with DES in ECB mode 00160 * 00161 * PARAMS 00162 * data [I] data to decrypt 00163 * key [I] key data (up to 7 bytes) 00164 * output [O] buffer to receive decrypted data 00165 * 00166 * RETURNS 00167 * Success: STATUS_SUCCESS 00168 * Failure: STATUS_BUFFER_TOO_SMALL if the output buffer is too small 00169 * Failure: STATUS_INVALID_PARAMETER_2 if the key is zero length 00170 * 00171 */ 00172 NTSTATUS 00173 WINAPI SystemFunction005(const struct ustring *in, 00174 const struct ustring *key, 00175 struct ustring *out) 00176 { 00177 union { 00178 unsigned char uc[8]; 00179 unsigned int ui[2]; 00180 } data; 00181 unsigned char deskey[7]; 00182 unsigned int ofs, crypt_len; 00183 00184 if (key->Length<=0) 00185 return STATUS_INVALID_PARAMETER_2; 00186 00187 if (key->Length<sizeof deskey) 00188 { 00189 memset(deskey, 0, sizeof deskey); 00190 memcpy(deskey, key->Buffer, key->Length); 00191 } 00192 else 00193 memcpy(deskey, key->Buffer, sizeof deskey); 00194 00195 CRYPT_DESunhash(data.uc, deskey, in->Buffer); 00196 00197 if (data.ui[1] != 1) 00198 return STATUS_UNKNOWN_REVISION; 00199 00200 crypt_len = data.ui[0]; 00201 if (crypt_len > out->MaximumLength) 00202 return STATUS_BUFFER_TOO_SMALL; 00203 00204 for (ofs=0; (ofs+8)<crypt_len; ofs+=8) 00205 CRYPT_DESunhash(out->Buffer+ofs, deskey, in->Buffer+ofs+8); 00206 00207 if (ofs<crypt_len) 00208 { 00209 CRYPT_DESunhash(data.uc, deskey, in->Buffer+ofs+8); 00210 memcpy(out->Buffer+ofs, data.uc, crypt_len-ofs); 00211 } 00212 00213 out->Length = crypt_len; 00214 00215 return STATUS_SUCCESS; 00216 } 00217 00218 /****************************************************************************** 00219 * SystemFunction007 [ADVAPI32.@] 00220 * 00221 * MD4 hash a unicode string 00222 * 00223 * PARAMS 00224 * string [I] the string to hash 00225 * output [O] the md4 hash of the string (16 bytes) 00226 * 00227 * RETURNS 00228 * Success: STATUS_SUCCESS 00229 * Failure: STATUS_UNSUCCESSFUL 00230 * 00231 */ 00232 NTSTATUS 00233 WINAPI SystemFunction007(const UNICODE_STRING *string, LPBYTE hash) 00234 { 00235 MD4_CTX ctx; 00236 00237 MD4Init( &ctx ); 00238 MD4Update( &ctx, (const BYTE *)string->Buffer, string->Length ); 00239 MD4Final( &ctx ); 00240 memcpy( hash, ctx.digest, 0x10 ); 00241 00242 return STATUS_SUCCESS; 00243 } 00244 00245 /****************************************************************************** 00246 * SystemFunction008 [ADVAPI32.@] 00247 * 00248 * Creates a LM response from a challenge and a password hash 00249 * 00250 * PARAMS 00251 * challenge [I] Challenge from authentication server 00252 * hash [I] NTLM hash (from SystemFunction006) 00253 * response [O] response to send back to the server 00254 * 00255 * RETURNS 00256 * Success: STATUS_SUCCESS 00257 * Failure: STATUS_UNSUCCESSFUL 00258 * 00259 * NOTES 00260 * see http://davenport.sourceforge.net/ntlm.html#theLmResponse 00261 * 00262 */ 00263 NTSTATUS 00264 WINAPI SystemFunction008(const BYTE *challenge, const BYTE *hash, LPBYTE response) 00265 { 00266 BYTE key[7*3]; 00267 00268 if (!challenge || !response) 00269 return STATUS_UNSUCCESSFUL; 00270 00271 memset(key, 0, sizeof key); 00272 memcpy(key, hash, 0x10); 00273 00274 CRYPT_DEShash(response, key, challenge); 00275 CRYPT_DEShash(response+8, key+7, challenge); 00276 CRYPT_DEShash(response+16, key+14, challenge); 00277 00278 return STATUS_SUCCESS; 00279 } 00280 00281 /****************************************************************************** 00282 * SystemFunction009 [ADVAPI32.@] 00283 * 00284 * Seems to do the same as SystemFunction008... 00285 */ 00286 NTSTATUS 00287 WINAPI SystemFunction009(const BYTE *challenge, const BYTE *hash, LPBYTE response) 00288 { 00289 return SystemFunction008(challenge, hash, response); 00290 } 00291 00292 /****************************************************************************** 00293 * SystemFunction010 [ADVAPI32.@] 00294 * SystemFunction011 [ADVAPI32.@] 00295 * 00296 * MD4 hashes 16 bytes of data 00297 * 00298 * PARAMS 00299 * unknown [] seems to have no effect on the output 00300 * data [I] pointer to data to hash (16 bytes) 00301 * output [O] the md4 hash of the data (16 bytes) 00302 * 00303 * RETURNS 00304 * Success: STATUS_SUCCESS 00305 * Failure: STATUS_UNSUCCESSFUL 00306 * 00307 */ 00308 NTSTATUS 00309 WINAPI SystemFunction010(LPVOID unknown, const BYTE *data, LPBYTE hash) 00310 { 00311 MD4_CTX ctx; 00312 00313 MD4Init( &ctx ); 00314 MD4Update( &ctx, data, 0x10 ); 00315 MD4Final( &ctx ); 00316 memcpy( hash, ctx.digest, 0x10 ); 00317 00318 return STATUS_SUCCESS; 00319 } 00320 00321 /****************************************************************************** 00322 * SystemFunction012 [ADVAPI32.@] 00323 * SystemFunction014 [ADVAPI32.@] 00324 * SystemFunction016 [ADVAPI32.@] 00325 * SystemFunction018 [ADVAPI32.@] 00326 * SystemFunction020 [ADVAPI32.@] 00327 * SystemFunction022 [ADVAPI32.@] 00328 * 00329 * Encrypts two DES blocks with two keys 00330 * 00331 * PARAMS 00332 * data [I] data to encrypt (16 bytes) 00333 * key [I] key data (two lots of 7 bytes) 00334 * output [O] buffer to receive encrypted data (16 bytes) 00335 * 00336 * RETURNS 00337 * Success: STATUS_SUCCESS 00338 * Failure: STATUS_UNSUCCESSFUL if the input or output buffer is NULL 00339 */ 00340 NTSTATUS 00341 WINAPI SystemFunction012(const BYTE *in, const BYTE *key, LPBYTE out) 00342 { 00343 if (!in || !out) 00344 return STATUS_UNSUCCESSFUL; 00345 00346 CRYPT_DEShash(out, key, in); 00347 CRYPT_DEShash(out+8, key+7, in+8); 00348 return STATUS_SUCCESS; 00349 } 00350 00351 /****************************************************************************** 00352 * SystemFunction013 [ADVAPI32.@] 00353 * SystemFunction015 [ADVAPI32.@] 00354 * SystemFunction017 [ADVAPI32.@] 00355 * SystemFunction019 [ADVAPI32.@] 00356 * SystemFunction021 [ADVAPI32.@] 00357 * SystemFunction023 [ADVAPI32.@] 00358 * 00359 * Decrypts two DES blocks with two keys 00360 * 00361 * PARAMS 00362 * data [I] data to decrypt (16 bytes) 00363 * key [I] key data (two lots of 7 bytes) 00364 * output [O] buffer to receive decrypted data (16 bytes) 00365 * 00366 * RETURNS 00367 * Success: STATUS_SUCCESS 00368 * Failure: STATUS_UNSUCCESSFUL if the input or output buffer is NULL 00369 */ 00370 NTSTATUS 00371 WINAPI SystemFunction013(const BYTE *in, const BYTE *key, LPBYTE out) 00372 { 00373 if (!in || !out) 00374 return STATUS_UNSUCCESSFUL; 00375 CRYPT_DESunhash(out, key, in); 00376 CRYPT_DESunhash(out+8, key+7, in+8); 00377 return STATUS_SUCCESS; 00378 } 00379 00380 /****************************************************************************** 00381 * SystemFunction024 [ADVAPI32.@] 00382 * 00383 * Encrypts two DES blocks with a 32 bit key... 00384 * 00385 * PARAMS 00386 * data [I] data to encrypt (16 bytes) 00387 * key [I] key data (4 bytes) 00388 * output [O] buffer to receive encrypted data (16 bytes) 00389 * 00390 * RETURNS 00391 * Success: STATUS_SUCCESS 00392 */ 00393 NTSTATUS 00394 WINAPI SystemFunction024(const BYTE *in, const BYTE *key, LPBYTE out) 00395 { 00396 BYTE deskey[0x10]; 00397 00398 memcpy(deskey, key, 4); 00399 memcpy(deskey+4, key, 4); 00400 memcpy(deskey+8, key, 4); 00401 memcpy(deskey+12, key, 4); 00402 00403 CRYPT_DEShash(out, deskey, in); 00404 CRYPT_DEShash(out+8, deskey+7, in+8); 00405 00406 return STATUS_SUCCESS; 00407 } 00408 00409 /****************************************************************************** 00410 * SystemFunction025 [ADVAPI32.@] 00411 * 00412 * Decrypts two DES blocks with a 32 bit key... 00413 * 00414 * PARAMS 00415 * data [I] data to encrypt (16 bytes) 00416 * key [I] key data (4 bytes) 00417 * output [O] buffer to receive encrypted data (16 bytes) 00418 * 00419 * RETURNS 00420 * Success: STATUS_SUCCESS 00421 */ 00422 NTSTATUS 00423 WINAPI SystemFunction025(const BYTE *in, const BYTE *key, LPBYTE out) 00424 { 00425 BYTE deskey[0x10]; 00426 00427 memcpy(deskey, key, 4); 00428 memcpy(deskey+4, key, 4); 00429 memcpy(deskey+8, key, 4); 00430 memcpy(deskey+12, key, 4); 00431 00432 CRYPT_DESunhash(out, deskey, in); 00433 CRYPT_DESunhash(out+8, deskey+7, in+8); 00434 00435 return STATUS_SUCCESS; 00436 } 00437 00438 /********************************************************************** 00439 * 00440 * @unimplemented 00441 */ 00442 INT 00443 WINAPI 00444 SystemFunction028(INT a, INT b) 00445 { 00446 //NDRCContextBinding() 00447 //SystemFunction034() 00448 SetLastError(ERROR_CALL_NOT_IMPLEMENTED); 00449 return 28; 00450 } 00451 00452 00453 /********************************************************************** 00454 * 00455 * @unimplemented 00456 */ 00457 INT 00458 WINAPI 00459 SystemFunction029(INT a, INT b) 00460 { 00461 //I_RpcBindingIsClientLocal() 00462 SetLastError(ERROR_CALL_NOT_IMPLEMENTED); 00463 return 29; 00464 } 00465 00466 00467 /****************************************************************************** 00468 * SystemFunction030 (ADVAPI32.@) 00469 * 00470 * Tests if two blocks of 16 bytes are equal 00471 * 00472 * PARAMS 00473 * b1,b2 [I] block of 16 bytes 00474 * 00475 * RETURNS 00476 * TRUE if blocks are the same 00477 * FALSE if blocks are different 00478 */ 00479 BOOL 00480 WINAPI SystemFunction030(PVOID b1, PVOID b2) 00481 { 00482 return !memcmp(b1, b2, 0x10); 00483 } 00484 00485 00486 /****************************************************************************** 00487 * SystemFunction032 [ADVAPI32.@] 00488 * 00489 * Encrypts a string data using ARC4 00490 * 00491 * PARAMS 00492 * data [I/O] data to encrypt 00493 * key [I] key data 00494 * 00495 * RETURNS 00496 * Success: STATUS_SUCCESS 00497 * Failure: STATUS_UNSUCCESSFUL 00498 * 00499 * NOTES 00500 * see http://web.it.kth.se/~rom/ntsec.html#crypto-strongavail 00501 */ 00502 NTSTATUS 00503 WINAPI SystemFunction032(struct ustring *data, struct ustring *key) 00504 { 00505 arc4_info a4i; 00506 00507 arc4_init(&a4i, key->Buffer, key->Length); 00508 arc4_ProcessString(&a4i, data->Buffer, data->Length); 00509 00510 return STATUS_SUCCESS; 00511 } 00512 00513 00514 /********************************************************************** 00515 * 00516 * @unimplemented 00517 */ 00518 INT 00519 WINAPI 00520 SystemFunction033(INT a, INT b) 00521 { 00522 SetLastError(ERROR_CALL_NOT_IMPLEMENTED); 00523 return 33; 00524 } 00525 00526 /********************************************************************** 00527 * 00528 * @unimplemented 00529 */ 00530 INT 00531 WINAPI 00532 SystemFunction034(INT a, INT b) 00533 { 00534 //RpcBindingToStringBindingW 00535 //I_RpcMapWin32Status 00536 //RpcStringBindingParseW 00537 //RpcStringFreeW 00538 SetLastError(ERROR_CALL_NOT_IMPLEMENTED); 00539 return 34; 00540 } 00541 00542 00543 /********************************************************************** 00544 * 00545 * @unimplemented 00546 */ 00547 BOOL 00548 WINAPI 00549 SystemFunction035(LPCSTR lpszDllFilePath) 00550 { 00551 return TRUE; 00552 } 00553 00554 /****************************************************************************** 00555 * SystemFunction036 (ADVAPI32.@) 00556 * 00557 * MSDN documents this function as RtlGenRandom and declares it in ntsecapi.h 00558 * 00559 * PARAMS 00560 * pbBuffer [O] Pointer to memory to receive random bytes. 00561 * dwLen [I] Number of random bytes to fetch. 00562 * 00563 * RETURNS 00564 * Always TRUE in my tests 00565 */ 00566 BOOLEAN 00567 WINAPI 00568 SystemFunction036(PVOID pbBuffer, ULONG dwLen) 00569 { 00572 // This function will output numbers based on the tick count. // 00573 // It will NOT OUPUT CRYPTOGRAPHIC-SAFE RANDOM NUMBERS !!! // 00575 00576 DWORD dwSeed; 00577 PBYTE pBuffer; 00578 ULONG uPseudoRandom; 00579 LARGE_INTEGER time; 00580 static ULONG uCounter = 17; 00581 00582 if(!pbBuffer || !dwLen) 00583 { 00584 /* This function always returns TRUE, even if invalid parameters were passed. (verified under WinXP SP2) */ 00585 return TRUE; 00586 } 00587 00588 /* Get the first seed from the performance counter */ 00589 QueryPerformanceCounter(&time); 00590 dwSeed = time.LowPart ^ time.HighPart ^ RtlUlongByteSwap(uCounter++); 00591 00592 /* We will access the buffer bytewise */ 00593 pBuffer = (PBYTE)pbBuffer; 00594 00595 do 00596 { 00597 /* Use the pseudo random number generator RtlRandom, which outputs a 4-byte value and a new seed */ 00598 uPseudoRandom = RtlRandom(&dwSeed); 00599 00600 do 00601 { 00602 /* Get each byte from the pseudo random number and store it in the buffer */ 00603 *pBuffer = (BYTE)(uPseudoRandom >> 8 * (dwLen % 4) & 0xFF); 00604 ++pBuffer; 00605 } while(--dwLen % 4); 00606 } while(dwLen); 00607 00608 return TRUE; 00609 } 00610 00611 /* 00612 These functions have nearly identical prototypes to CryptProtectMemory and CryptUnprotectMemory, 00613 in crypt32.dll. 00614 */ 00615 00616 /****************************************************************************** 00617 * SystemFunction040 (ADVAPI32.@) 00618 * 00619 * PARAMS: 00620 * memory : pointer to memory to encrypt 00621 * length : length of region to encrypt, in bytes. must be multiple of RTL_ENCRYPT_MEMORY_SIZE 00622 * flags : RTL_ENCRYPT_OPTION_SAME_PROCESS | RTL_ENCRYPT_OPTION_CROSS_PROCESS, | RTL_ENCRYPT_OPTION_SAME_LOGON 00623 * control whether other processes are able to decrypt the memory. The same value must be given 00624 * when decrypting the memory. 00625 */ 00626 NTSTATUS WINAPI SystemFunction040(PVOID memory, ULONG length, ULONG flags) /* RtlEncryptMemory */ 00627 { 00628 //FIXME("(%p, %lx, %lx): stub [RtlEncryptMemory]\n", memory, length, flags); 00629 return STATUS_SUCCESS; 00630 } 00631 00632 /****************************************************************************** 00633 * SystemFunction041 (ADVAPI32.@) 00634 * 00635 * PARAMS: 00636 * memory : pointer to memory to decrypt 00637 * length : length of region to decrypt, in bytes. must be multiple of RTL_ENCRYPT_MEMORY_SIZE 00638 * flags : RTL_ENCRYPT_OPTION_SAME_PROCESS | RTL_ENCRYPT_OPTION_CROSS_PROCESS, | RTL_ENCRYPT_OPTION_SAME_LOGON 00639 * control whether other processes are able to decrypt the memory. The same value must be given 00640 * when encrypting the memory. 00641 */ 00642 NTSTATUS WINAPI SystemFunction041(PVOID memory, ULONG length, ULONG flags) /* RtlDecryptMemory */ 00643 { 00644 //FIXME("(%p, %lx, %lx): stub [RtlDecryptMemory]\n", memory, length, flags); 00645 return STATUS_SUCCESS; 00646 } 00647 00648 /* EOF */ Generated on Mon May 28 2012 04:22:14 for ReactOS by
1.7.6.1
|