Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.1 KiB

  1. #define FUSION_PROFILING 1
  2. #define DBG 1
  3. #include "windows.h"
  4. #include "wincrypt.h"
  5. #include "stdlib.h"
  6. #include "stdio.h"
  7. #include "fastsha1.h"
  8. #include "fusionbuffer.h"
  9. #include "perfclocking.h"
  10. FASTSHA1_STATE ShaState;
  11. #define ASM_CPUID { __asm cpuid }
  12. #define ASM_RDTSC { __asm rdtsc }
  13. #define NUMBER_OF( n ) ( sizeof(n) / sizeof(*n) )
  14. #define INPUT_BLOCK_SIZE ( 4096*4 )
  15. inline VOID GetCpuIdLag( LARGE_INTEGER *ref )
  16. {
  17. LARGE_INTEGER temp, temp2;
  18. #if !defined(_WIN64)
  19. _asm
  20. {
  21. cpuid
  22. cpuid
  23. cpuid
  24. cpuid
  25. cpuid
  26. rdtsc
  27. mov temp.LowPart, eax
  28. mov temp.HighPart, edx
  29. cpuid
  30. rdtsc
  31. mov temp2.LowPart, eax
  32. mov temp2.HighPart, edx
  33. }
  34. ref->QuadPart = temp2.QuadPart - temp.QuadPart;
  35. #else
  36. ref->QuadPart = 0;
  37. #endif
  38. }
  39. LARGE_INTEGER CpuIdLag;
  40. BYTE TestCase1[] = { 0x61, 0x62, 0x63 };
  41. BYTE TestCase2[] = {
  42. 0x61, 0x62, 0x63, 0x64,
  43. 0x62, 0x63, 0x64, 0x65,
  44. 0x63, 0x64, 0x65, 0x66,
  45. 0x64, 0x65, 0x66, 0x67,
  46. 0x65, 0x66, 0x67, 0x68,
  47. 0x66, 0x67, 0x68, 0x69,
  48. 0x67, 0x68, 0x69, 0x6A,
  49. 0x68, 0x69, 0x6A, 0x6B,
  50. 0x69, 0x6A, 0x6B, 0x6C,
  51. 0x6A, 0x6B, 0x6C, 0x6D,
  52. 0x6B, 0x6C, 0x6D, 0x6E,
  53. 0x6C, 0x6D, 0x6E, 0x6F,
  54. 0x6D, 0x6E, 0x6F, 0x70,
  55. 0x6E, 0x6F, 0x70, 0x71
  56. };
  57. HCRYPTPROV hProvider;
  58. #define CHECKFAIL( f ) do { if ( !(f) ) return FALSE; } while ( 0 )
  59. BOOL ObtainFastSHA1OfFile( PBYTE pvBase, SIZE_T cbSize, BYTE bShaHashData[20], PSIZE_T pcbSize )
  60. {
  61. FUSION_PERF_INFO InfoSlots[4];
  62. FASTSHA1_STATE State;
  63. State.cbStruct = sizeof( State );
  64. PERFINFOTIME( &InfoSlots[0], CHECKFAIL( InitializeFastSHA1State( 0, &State ) ) );
  65. while ( cbSize )
  66. {
  67. DWORD dwThisSize = ( cbSize > INPUT_BLOCK_SIZE ) ? INPUT_BLOCK_SIZE : cbSize;
  68. PERFINFOTIME( &InfoSlots[1], CHECKFAIL( HashMoreFastSHA1Data(
  69. &State,
  70. pvBase,
  71. dwThisSize
  72. ) ) );
  73. pvBase += dwThisSize;
  74. cbSize -= dwThisSize;
  75. }
  76. PERFINFOTIME( &InfoSlots[2], CHECKFAIL( FinalizeFastSHA1State( 0, &State ) ) );
  77. PERFINFOTIME( &InfoSlots[3], CHECKFAIL( GetFastSHA1Result( &State, bShaHashData, pcbSize ) ) );
  78. FusionpReportPerfInfo(
  79. FUSIONPERF_DUMP_TO_STDOUT |
  80. FUSIONPERF_DUMP_ALL_STATISTICS |
  81. FUSIONPERF_DUMP_ALL_SOURCEINFO,
  82. InfoSlots,
  83. NUMBER_OF( InfoSlots )
  84. );
  85. printf("\n\n");
  86. return TRUE;
  87. }
  88. BOOL ObtainReferenceSHA1Hash( PBYTE pvBase, SIZE_T cbSize, BYTE bShaHashData[20], PDWORD pdwData )
  89. {
  90. FUSION_PERF_INFO InfoSlots[4];
  91. FASTSHA1_STATE State;
  92. DWORD dwDump;
  93. HCRYPTHASH hHash;
  94. PERFINFOTIME( &InfoSlots[0], CHECKFAIL( CryptCreateHash( hProvider, CALG_SHA1, NULL, 0, &hHash ) ) );
  95. while ( cbSize )
  96. {
  97. DWORD dwThisSize = ( cbSize > INPUT_BLOCK_SIZE ) ? INPUT_BLOCK_SIZE : cbSize;
  98. PERFINFOTIME( &InfoSlots[1], CHECKFAIL( CryptHashData( hHash, pvBase, dwThisSize, 0 ) ) );
  99. cbSize -= dwThisSize;
  100. pvBase += dwThisSize;
  101. }
  102. PERFINFOTIME( &InfoSlots[2], CHECKFAIL( CryptGetHashParam(
  103. hHash,
  104. HP_HASHVAL,
  105. bShaHashData,
  106. pdwData,
  107. 0
  108. ) ) );
  109. PERFINFOTIME( &InfoSlots[3], CHECKFAIL( CryptDestroyHash( hHash ) ) );
  110. FusionpReportPerfInfo(
  111. FUSIONPERF_DUMP_TO_STDOUT |
  112. FUSIONPERF_DUMP_ALL_STATISTICS |
  113. FUSIONPERF_DUMP_ALL_SOURCEINFO,
  114. InfoSlots,
  115. NUMBER_OF( InfoSlots )
  116. );
  117. printf("\n\n");
  118. return TRUE;
  119. }
  120. extern "C" int __cdecl wmain(int argc, wchar_t** argv)
  121. {
  122. BYTE bDestination[20];
  123. SIZE_T cbDestination;
  124. int i = 0;
  125. ShaState.cbStruct = sizeof( ShaState );
  126. InitializeFastSHA1State( 0, &ShaState );
  127. HashMoreFastSHA1Data( &ShaState, TestCase1, sizeof( TestCase1 ) );
  128. FinalizeFastSHA1State( 0, &ShaState );
  129. GetCpuIdLag( &CpuIdLag );
  130. CryptAcquireContextW( &hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_SILENT | CRYPT_VERIFYCONTEXT );
  131. BYTE bReferenceHash[20], bFastHash[20];
  132. SIZE_T cbReferenceHash, cbFastHash;
  133. HANDLE hFile, hFileMapping;
  134. PBYTE pbFileContents;
  135. DWORD dwFileContents;
  136. if ( GetFileAttributesW( argv[1] ) & FILE_ATTRIBUTE_DIRECTORY )
  137. {
  138. printf( "dir : %ls\n", argv[1] );
  139. return 0;
  140. }
  141. hFile = CreateFileW( argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  142. if ( hFile == INVALID_HANDLE_VALUE ) goto ErrorPath;
  143. hFileMapping = CreateFileMapping( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
  144. if ( ( hFileMapping == NULL ) || ( hFileMapping == INVALID_HANDLE_VALUE ) ) goto ErrorPath;
  145. pbFileContents = (PBYTE)MapViewOfFile( hFileMapping, FILE_MAP_READ, 0, 0, 0 );
  146. if ( pbFileContents == NULL ) goto ErrorPath;
  147. dwFileContents = GetFileSize( hFile, NULL );
  148. if ( !ObtainReferenceSHA1Hash(
  149. pbFileContents,
  150. dwFileContents,
  151. bReferenceHash,
  152. &(cbReferenceHash = sizeof(bReferenceHash))
  153. ) )
  154. {
  155. printf("(Reference failed) ");
  156. goto ErrorPath;
  157. }
  158. if ( !ObtainFastSHA1OfFile(
  159. pbFileContents,
  160. dwFileContents,
  161. bFastHash,
  162. &(cbFastHash = sizeof(bFastHash))
  163. ) )
  164. {
  165. printf( "(fastsha1 failed) " );
  166. goto ErrorPath;
  167. }
  168. if ( memcmp( bFastHash, bReferenceHash, 20 ) == 0 )
  169. {
  170. printf( "match: %ls\n", argv[1] );
  171. }
  172. else
  173. {
  174. printf( "fails: %ls\n", argv[1] );
  175. }
  176. CryptReleaseContext( hProvider, 0 );
  177. return 0;
  178. ErrorPath:
  179. printf( "Failure: 0x%08x on file %ls\n", GetLastError(), argv[1] );
  180. return 1;
  181. }