Fortuna PRNG C++ Source Code

Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

MachineSig.cpp

Go to the documentation of this file.
00001 /*
00002         (c) Citadel Software Inc, 2003, www.citadelsoftware.ca
00003 */
00004 
00012 #include "stdafx.h"
00013 #include "MachineSig.h"
00014 #include "FortunaUtils.h"
00015 #include "Timer.h"
00016 #include "Psapi.h"
00017 #include "ProcessInfo.h"
00018 #include "Sha2.h"
00019 #include "iphlpapi.h"
00020 
00021 namespace CitadelSoftwareInc {
00022 
00023                 // add in the current process data
00024 void AddProcessData(vecuc& vData)
00025 {
00026         Timer hpTimer;
00027 
00028         DWORD dwNeeded = 0;
00029         const int MAXSIZE=1024;
00030         std::vector<DWORD>dwPids;
00031         dwPids.resize(MAXSIZE);
00032 
00033         hpTimer.Start();
00034         BOOL bStatus = EnumProcesses(&dwPids[0], MAXSIZE*sizeof(DWORD), &dwNeeded);
00035         hpTimer.Stop();
00036         hpTimer.AddElapsedTime(vData);
00037 
00038         if (!bStatus)
00039                 return;
00040 
00041         DWORD dwNumPids = dwNeeded / sizeof(DWORD);
00042 
00043         DWORD dwPid=0;
00044         DWORD i=0;
00045         for (i=0; i<dwNumPids; ++i)
00046         {
00047                 dwPid = dwPids[i];
00048                 ProcessInfoIoCounter piioc;
00049                 hpTimer.Start();
00050                 piioc.GetData(vData, dwPid, NULL);
00051                 hpTimer.Stop();
00052                 hpTimer.AddElapsedTime(vData);
00053 
00054                 // next...
00055                 ProcessInfoTimes pitimes;
00056                 hpTimer.Start();
00057                 pitimes.GetData(vData, dwPid, NULL);
00058                 hpTimer.Stop();
00059                 hpTimer.AddElapsedTime(vData);
00060 
00061                 ProcessInfoMemory pim;
00062                 hpTimer.Start();
00063                 pim.GetData(vData, dwPid, NULL);
00064                 hpTimer.Stop();
00065                 hpTimer.AddElapsedTime(vData);
00066 
00067                 ProcessInfoPerformance pip;
00068                 hpTimer.Start();
00069                 pip.GetData(vData, dwPid, NULL);
00070                 hpTimer.Stop();
00071                 hpTimer.AddElapsedTime(vData);
00072         }
00073 
00074         // hash the process data
00075         vecuc vHash;
00076         HashVector(vData, vHash);
00077         EraseVector(vData);
00078 
00079         // copy the hash to vData and then erase vHash
00080         vData.resize(vHash.size());
00081         std::copy(vHash.begin(), vHash.end(), vData.begin());
00082         EraseVector(vHash);
00083 }
00084 
00085 void AddComputerName(vecuc& vData)
00086 {
00087         COMPUTER_NAME_FORMAT names[8] = {ComputerNameNetBIOS, ComputerNameDnsHostname, ComputerNameDnsDomain, ComputerNameDnsFullyQualified,
00088                                                                          ComputerNamePhysicalNetBIOS, ComputerNamePhysicalDnsHostname, ComputerNamePhysicalDnsDomain,
00089                                                                          ComputerNamePhysicalDnsFullyQualified};
00090 
00091         const int MAXSIZE=1024;
00092         char buffer[MAXSIZE+1];
00093         memset(buffer,0,sizeof(buffer));
00094         DWORD dwSize = MAXSIZE;
00095 
00096         BOOL bStatus = TRUE;
00097         for(int i=0; i<8; ++i)
00098         {
00099                 dwSize = MAXSIZE;
00100                 bStatus = GetComputerNameEx(names[i], buffer, &dwSize);
00101                 if (bStatus)
00102                 {
00103                         AddString(vData, (const unsigned char*)buffer);
00104                 }
00105         }
00106 
00107 }
00108 
00109 void AddWindowsVersion(vecuc& vData)
00110 {
00111         OSVERSIONINFO version;
00112         memset(&version,0,sizeof(OSVERSIONINFO));
00113         version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00114 
00115         BOOL bStatus = GetVersionEx(&version);
00116 
00117         if (bStatus)
00118         {
00119                 int size = sizeof(OSVERSIONINFO);
00120                 const unsigned char* pData = (unsigned char*)&version;
00121                 AddBinaryData(vData, pData, size);
00122         }
00123 
00124 }
00125 
00126 void AddSystemInfo(vecuc& vData)
00127 {
00128         SYSTEM_INFO info;
00129         memset(&info,0,sizeof(SYSTEM_INFO));
00130 
00131         GetSystemInfo(&info);
00132 
00133         const unsigned char* pData = (unsigned char*)&info;
00134         int size = sizeof(SYSTEM_INFO);
00135 
00136         AddBinaryData(vData, pData, size);
00137 }
00138 
00139 void AddCurrentHwProfile(vecuc& vData)
00140 {
00141         HW_PROFILE_INFO info;
00142         memset(&info, 0, sizeof(HW_PROFILE_INFO));
00143         BOOL bStatus = GetCurrentHwProfile(&info);
00144 
00145         if(bStatus)
00146         {
00147                 const unsigned char* pData = (unsigned char*)&info;
00148                 int size = sizeof(HW_PROFILE_INFO);
00149                 AddBinaryData(vData, pData, size);
00150         }
00151 
00152 }
00153 
00154 void AddEnvStringData(vecuc& vData)
00155 {
00156         char* pVoid = GetEnvironmentStrings();
00157 
00158         if (!pVoid)
00159                 return;
00160 
00161         unsigned char uc=0;
00162         const char* pString = (const char*)pVoid;
00163 
00164         for (; *pString; ++pString)
00165         {
00166                 while(*pString)
00167                 {
00168                         uc = *pString++;
00169                         vData.push_back(uc);
00170                 }
00171         }
00172 
00173         BOOL bStatus = FreeEnvironmentStrings(pVoid);
00174         if (!bStatus)
00175         {
00176                 assert(0);
00177         }
00178 }
00179 
00180 
00181 void AddMacAddresses(vecuc& vData)
00182 {
00183         IP_ADAPTER_INFO AdapterInfo[16];       // Allocate information 
00184                                            // for up to 16 NICs
00185         DWORD dwBufLen = sizeof(AdapterInfo);  // Save memory size of buffer
00186 
00187         DWORD dwStatus = GetAdaptersInfo(      // Call GetAdapterInfo
00188                         AdapterInfo,                   // [out] buffer to receive data
00189                         &dwBufLen);                    // [in] size of receive data buffer
00190   
00191         assert(dwStatus == ERROR_SUCCESS);  // Verify return value is 
00192                                         // valid, no buffer overflow
00193 
00194   PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; // Contains pointer to
00195 
00196   int size = sizeof(IP_ADAPTER_INFO);
00197   
00198   do {
00199         const unsigned char* pData = (unsigned char*)pAdapterInfo;
00200 
00201         AddBinaryData(vData, pData, size);
00202 
00203     pAdapterInfo = pAdapterInfo->Next;    // Progress through 
00204                                           // linked list
00205   }
00206   while(pAdapterInfo);                    
00207 
00208 
00209 }
00210 
00211 void AddStartupInfo(vecuc& vData)
00212 {
00213         STARTUPINFO info;
00214         info.cb = sizeof(STARTUPINFO);
00215 
00216         GetStartupInfo(&info);
00217 
00218         int size = sizeof(info);
00219         const unsigned char* pData =(unsigned char*)&info;
00220         AddBinaryData(vData, pData, size);
00221 }
00222 
00223 void AddGlobalMemoryStatus(vecuc& vData)
00224 {
00225 
00226         MEMORYSTATUSEX status;
00227         status.dwLength = sizeof(MEMORYSTATUSEX);
00228         BOOL bStatus = GlobalMemoryStatusEx(&status);
00229         if (bStatus)
00230         {
00231                 int size = sizeof(MEMORYSTATUSEX);
00232                 const unsigned char* pData = (unsigned char*)&status;
00233                 AddBinaryData(vData, pData, size);
00234         }
00235 }
00236 
00237 
00238 void AddWindowingInfo(vecuc& vData)
00239 {
00240         AddToVector2(vData, GetActiveWindow());
00241         AddToVector2(vData, GetCapture());
00242         AddToVector2(vData, GetClipboardOwner());
00243         AddToVector2(vData, GetClipboardViewer());
00244         AddToVector2(vData, GetCurrentProcess());
00245         AddToVector2(vData, GetCurrentProcessId());
00246         AddToVector2(vData, GetCurrentThread());
00247         AddToVector2(vData, GetCurrentProcessId());
00248         AddToVector2(vData, GetDesktopWindow());
00249         AddToVector2(vData, GetFocus());
00250         AddToVector2(vData, GetInputState());
00251         AddToVector2(vData, GetMessagePos());
00252         AddToVector2(vData, GetMessageTime());
00253         AddToVector2(vData, GetOpenClipboardWindow());
00254         AddToVector2(vData, GetProcessHeap());
00255         AddToVector2(vData, GetProcessWindowStation());
00256         AddToVector2(vData, GetQueueStatus(QS_ALLEVENTS));
00257         AddToVector2(vData, GetTickCount());
00258 }
00259 
00260 
00261         // Get a hash of some quantities that will provide a machine signature.
00262         // This is used after the seed file is processed to add something diffent to the pool hashes stored in the 
00263         // machine file to avoid provlems with reusing the same seed file.
00264         void GetMachineSignature(vecuc& vData)
00265         {
00266                 {
00267                         unsigned int count = 0;
00268                         Timer hpTimer;
00269                         for (int i=0; i<100; ++i)
00270                         {
00271                                 hpTimer.Start();
00272                                 Sleep(0);
00273                                 hpTimer.Stop();
00274                                 count += hpTimer.AddElapsedTimeDifference(vData);
00275                         }
00276                         assert(count);
00277                 }
00278 
00279 
00280                 Timer hpTimer;
00281                 hpTimer.Start();
00282 
00283                 vData.clear();
00284                 vData.reserve(10240);
00285 
00286                 // add in the current time data
00287                 AddTimeData(vData);
00288 
00289                 // add in the frequency of the high performance timer
00290                 LARGE_INTEGER liTemp;
00291                 BOOL bStatus = QueryPerformanceFrequency(&liTemp);
00292                 if (bStatus)
00293                 {
00294                         AddToVector2(vData, liTemp);
00295                 }
00296 
00297                 // add in the user name
00298                 char buffer[1025];
00299                 memset(buffer,0,sizeof(buffer));
00300                 DWORD dwSize = 1024;
00301                 bStatus = GetUserName(buffer, &dwSize);
00302                 if (bStatus)
00303                 {
00304                         AddString(vData, (const unsigned char*)buffer);
00305                 }
00306 
00307                 hpTimer.Start();
00308                 AddProcessData(vData);
00309                 hpTimer.Stop();
00310                 hpTimer.AddElapsedTime(vData);
00311 
00312                 hpTimer.Start();
00313                 AddComputerName(vData);
00314                 hpTimer.Stop();
00315                 hpTimer.AddElapsedTime(vData);
00316 
00317                 hpTimer.Start();
00318                 AddWindowsVersion(vData);
00319                 hpTimer.Stop();
00320                 hpTimer.AddElapsedTime(vData);
00321 
00322                 hpTimer.Start();
00323                 AddSystemInfo(vData);
00324                 hpTimer.Stop();
00325                 hpTimer.AddElapsedTime(vData);
00326 
00327                 hpTimer.Start();
00328                 AddCurrentHwProfile(vData);
00329                 hpTimer.Stop();
00330                 hpTimer.AddElapsedTime(vData);
00331 
00332                 hpTimer.Start();
00333                 AddEnvStringData(vData);
00334                 hpTimer.Stop();
00335                 hpTimer.AddElapsedTime(vData);
00336 
00337                 hpTimer.Start();
00338                 AddMacAddresses(vData);
00339                 hpTimer.Stop();
00340                 hpTimer.AddElapsedTime(vData);
00341 
00342                 hpTimer.Start();
00343                 AddStartupInfo(vData);
00344                 hpTimer.Stop();
00345                 hpTimer.AddElapsedTime(vData);
00346 
00347                 hpTimer.Start();
00348                 AddGlobalMemoryStatus(vData);
00349                 hpTimer.Stop();
00350                 hpTimer.AddElapsedTime(vData);
00351 
00352                 hpTimer.Start();
00353                 AddWindowingInfo(vData);
00354                 hpTimer.Stop();
00355                 hpTimer.AddElapsedTime(vData);
00356 
00357                 vecuc vHash;
00358                 HashVector(vData, vHash);
00359                 EraseVector(vData);
00360                 AddToVector(vData,vHash);
00361                 EraseVector(vHash);
00362         }
00363 
00364 }       // end namespace CitadelSoftwareInc

Generated on Sat Feb 28 17:24:40 2004 for Fortuna by doxygen 1.3.5