178 #include <Processes.h>
180 #include <Resources.h>
190 extern "C" void RAND_add (
const void *buf,
int num,
double entropy);
197 #define kMouseResolution 6 // Mouse position has to differ
200 #define kMousePositionEntropy 5.16 // log2 (kMouseResolution**2)
201 #define kTypicalMouseIdleTicks 300.0 // I am guessing that a typical
204 #define kVolumeBytesEntropy 12.0 // about log2 (20000/4),
208 #define kApplicationUpTimeEntropy 6.0 // Variance > 1 second, uptime
210 #define kSysStartupEntropy 7.0 // Entropy for machine startup
220 mSupportsLargeVolumes =
221 (Gestalt(gestaltFSAttr, &result) == noErr) &&
222 ((result & (1L << gestaltFSSupports2TBVols)) != 0);
224 if (Gestalt (gestaltNativeCPUtype, &result) != noErr)
231 mIs601 = (result == gestaltCPU601);
232 mIsPowerPC = (result >= gestaltCPU601);
234 mLastMouse.h = mLastMouse.v = -10;
236 mLastPeriodicTicks = TickCount();
237 GetTimeBaseResolution ();
240 AddTimeSinceMachineStartup ();
241 AddAbsoluteSystemStartupTime ();
242 AddStartupVolumeInfo ();
250 mLastPeriodicTicks = TickCount();
255 void CRandomizer::AddCurrentMouse (
void)
258 unsigned long lastCheck;
261 #if TARGET_API_MAC_CARBON
262 GetGlobalMouse (&mouseLoc);
264 mouseLoc = LMGetMouseLocation();
269 AddBytes (&mouseLoc,
sizeof (mouseLoc),
272 if (mLastMouse.h == mouseLoc.h && mLastMouse.v == mouseLoc.v)
282 lastCheck = TickCount() - mLastPeriodicTicks;
289 AddBytes (&mMouseStill,
sizeof (mMouseStill), entropy);
292 mLastMouse = mouseLoc;
295 void CRandomizer::AddAbsoluteSystemStartupTime (
void)
300 now -= TickCount() / 60;
305 void CRandomizer::AddTimeSinceMachineStartup (
void)
312 void CRandomizer::AddAppRunningTime (
void)
314 ProcessSerialNumber PSN;
315 ProcessInfoRec ProcessInfo;
317 ProcessInfo.processInfoLength =
sizeof (ProcessInfoRec);
318 ProcessInfo.processName = nil;
319 ProcessInfo.processAppSpec = nil;
321 GetCurrentProcess (&PSN);
322 GetProcessInformation (&PSN, &ProcessInfo);
327 AddBytes (&ProcessInfo,
sizeof (ProcessInfoRec),
331 void CRandomizer::AddStartupVolumeInfo (
void)
338 if (!mSupportsLargeVolumes)
341 FindFolder (kOnSystemDisk, kSystemFolderType, kDontCreateFolder,
343 pb.ioVRefNum = vRefNum;
347 err = PBXGetVolInfoSync (&pb);
356 AddBytes (&pb,
sizeof (pb),
358 log2l (((pb.ioVTotalBytes.hi - pb.ioVFreeBytes.hi)
360 (pb.ioVTotalBytes.lo - pb.ioVFreeBytes.lo))
361 / pb.ioVAlBlkSiz - 3.0));
372 void CRandomizer::AddFiller (
void)
376 ProcessSerialNumber psn;
378 RGBColor hiliteRGBValue;
389 GetNextProcess ((ProcessSerialNumber*) kNoProcess);
390 while (GetNextProcess (&
data.psn) == noErr)
392 GetFrontProcess (&
data.psn);
393 LMGetHiliteRGB (&
data.hiliteRGBValue);
394 Gestalt (gestaltProcClkSpeed, &
data.cpuSpeed);
395 Gestalt (gestaltLogicalRAMSize, &
data.totalMemory);
396 Gestalt (gestaltSystemVersion, &
data.systemVersion);
397 data.resFile = CurResFile ();
413 void CRandomizer::AddBytes (
void *
data,
long size,
double entropy)
415 RAND_add (data, size, entropy * 0.125);
419 void CRandomizer::AddNow (
double millisecondUncertainty)
421 long time = SysTimer();
422 AddBytes (&time,
sizeof (time), log2l (millisecondUncertainty *
423 mTimebaseTicksPerMillisec));
428 void CRandomizer::GetTimeBaseResolution (
void)
434 if (Gestalt (gestaltProcClkSpeed, &speed) != noErr)
437 mTimebaseTicksPerMillisec = 6000.0D;
440 mTimebaseTicksPerMillisec = speed / 1.0e4D;
443 mTimebaseTicksPerMillisec = 783.360D;
447 unsigned long CRandomizer::SysTimer (
void)
454 Microseconds (&usec);
466 asm unsigned long GetPPCTimer (
register bool is601)