| 1 |
/* rndw32.c - W32 entropy gatherer |
| 2 |
* Copyright (C) 1999, 2000, 2002, 2003, 2007, |
| 3 |
* 2010 Free Software Foundation, Inc. |
| 4 |
* Copyright Peter Gutmann, Matt Thomlinson and Blake Coverett 1996-2006 |
| 5 |
* |
| 6 |
* This file is part of Libgcrypt. |
| 7 |
* |
| 8 |
************************************************************************* |
| 9 |
* The code here is based on code from Cryptlib 3.0 beta by Peter Gutmann. |
| 10 |
* Source file misc/rndwin32.c "Win32 Randomness-Gathering Code" with this |
| 11 |
* copyright notice: |
| 12 |
* |
| 13 |
* This module is part of the cryptlib continuously seeded pseudorandom |
| 14 |
* number generator. For usage conditions, see lib_rand.c |
| 15 |
* |
| 16 |
* [Here is the notice from lib_rand.c, which is now called dev_sys.c] |
| 17 |
* |
| 18 |
* This module and the misc/rnd*.c modules represent the cryptlib |
| 19 |
* continuously seeded pseudorandom number generator (CSPRNG) as described in |
| 20 |
* my 1998 Usenix Security Symposium paper "The generation of random numbers |
| 21 |
* for cryptographic purposes". |
| 22 |
* |
| 23 |
* The CSPRNG code is copyright Peter Gutmann (and various others) 1996, |
| 24 |
* 1997, 1998, 1999, all rights reserved. Redistribution of the CSPRNG |
| 25 |
* modules and use in source and binary forms, with or without modification, |
| 26 |
* are permitted provided that the following conditions are met: |
| 27 |
* |
| 28 |
* 1. Redistributions of source code must retain the above copyright notice |
| 29 |
* and this permission notice in its entirety. |
| 30 |
* |
| 31 |
* 2. Redistributions in binary form must reproduce the copyright notice in |
| 32 |
* the documentation and/or other materials provided with the distribution. |
| 33 |
* |
| 34 |
* 3. A copy of any bugfixes or enhancements made must be provided to the |
| 35 |
* author, <[email protected]> to allow them to be added to the |
| 36 |
* baseline version of the code. |
| 37 |
* |
| 38 |
* ALTERNATIVELY, the code may be distributed under the terms of the |
| 39 |
* GNU Lesser General Public License, version 2.1 or any later version |
| 40 |
* published by the Free Software Foundation, in which case the |
| 41 |
* provisions of the GNU LGPL are required INSTEAD OF the above |
| 42 |
* restrictions. |
| 43 |
* |
| 44 |
* Although not required under the terms of the LGPL, it would still |
| 45 |
* be nice if you could make any changes available to the author to |
| 46 |
* allow a consistent code base to be maintained. |
| 47 |
************************************************************************* |
| 48 |
* The above alternative was changed from GPL to LGPL on 2007-08-22 with |
| 49 |
* permission from Peter Gutmann: |
| 50 |
*========== |
| 51 |
From: pgut001 <[email protected]> |
| 52 |
Subject: Re: LGPL for the windows entropy gatherer |
| 53 |
To: [email protected] |
| 54 |
Date: Wed, 22 Aug 2007 03:05:42 +1200 |
| 55 |
|
| 56 |
Hi, |
| 57 |
|
| 58 |
>As of now libgcrypt is GPL under Windows due to that module and some people |
| 59 |
>would really like to see it under LGPL too. Can you do such a license change |
| 60 |
>to LGPL version 2? Note that LGPL give the user the option to relicense it |
| 61 |
>under GPL, so the change would be pretty easy and backwar compatible. |
| 62 |
|
| 63 |
Sure. I assumed that since GPG was GPLd, you'd prefer the GPL for the entropy |
| 64 |
code as well, but Ian asked for LGPL as an option so as of the next release |
| 65 |
I'll have LGPL in there. You can consider it to be retroactive, so your |
| 66 |
current version will be LGPLd as well. |
| 67 |
|
| 68 |
Peter. |
| 69 |
*========== |
| 70 |
*/ |
| 71 |
|
| 72 |
#include <config.h> |
| 73 |
#include <stdio.h> |
| 74 |
#include <stdlib.h> |
| 75 |
#include <errno.h> |
| 76 |
#include <string.h> |
| 77 |
#ifdef __GNUC__ |
| 78 |
#include <stdint.h> |
| 79 |
#endif |
| 80 |
|
| 81 |
#include <winsock2.h> |
| 82 |
#include <windows.h> |
| 83 |
|
| 84 |
|
| 85 |
#include "types.h" |
| 86 |
#include "g10lib.h" |
| 87 |
#include "rand-internal.h" |
| 88 |
|
| 89 |
|
| 90 |
/* Definitions which are missing from the current GNU Windows32Api. */ |
| 91 |
#ifndef IOCTL_DISK_PERFORMANCE |
| 92 |
#define IOCTL_DISK_PERFORMANCE 0x00070020 |
| 93 |
#endif |
| 94 |
|
| 95 |
/* This used to be (6*8+5*4+8*2), but Peter Gutmann figured a larger |
| 96 |
value in a newer release. So we use a far larger value. */ |
| 97 |
#define SIZEOF_DISK_PERFORMANCE_STRUCT 256 |
| 98 |
|
| 99 |
/* We don't include wincrypt.h so define it here. */ |
| 100 |
#define HCRYPTPROV HANDLE |
| 101 |
|
| 102 |
|
| 103 |
/* When we query the performance counters, we allocate an initial buffer and |
| 104 |
* then reallocate it as required until RegQueryValueEx() stops returning |
| 105 |
* ERROR_MORE_DATA. The following values define the initial buffer size and |
| 106 |
* step size by which the buffer is increased |
| 107 |
*/ |
| 108 |
#define PERFORMANCE_BUFFER_SIZE 65536 /* Start at 64K */ |
| 109 |
#define PERFORMANCE_BUFFER_STEP 16384 /* Step by 16K */ |
| 110 |
|
| 111 |
|
| 112 |
/* The number of bytes to read from the system RNG on each slow poll. */ |
| 113 |
#define SYSTEMRNG_BYTES 64 |
| 114 |
|
| 115 |
/* Intel Chipset CSP type and name */ |
| 116 |
#define PROV_INTEL_SEC 22 |
| 117 |
#define INTEL_DEF_PROV "Intel Hardware Cryptographic Service Provider" |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
/* Type definitions for function pointers to call NetAPI32 functions. */ |
| 123 |
typedef DWORD (WINAPI *NETSTATISTICSGET)(LPWSTR szServer, LPWSTR szService, |
| 124 |
DWORD dwLevel, DWORD dwOptions, |
| 125 |
LPBYTE *lpBuffer); |
| 126 |
typedef DWORD (WINAPI *NETAPIBUFFERSIZE)(LPVOID lpBuffer, LPDWORD cbBuffer); |
| 127 |
typedef DWORD (WINAPI *NETAPIBUFFERFREE)(LPVOID lpBuffer); |
| 128 |
|
| 129 |
/* Type definitions for function pointers to call native NT functions. */ |
| 130 |
typedef DWORD (WINAPI *NTQUERYSYSTEMINFORMATION)(DWORD systemInformationClass, |
| 131 |
PVOID systemInformation, |
| 132 |
ULONG systemInformationLength, |
| 133 |
PULONG returnLength); |
| 134 |
typedef DWORD (WINAPI *NTQUERYINFORMATIONPROCESS) |
| 135 |
(HANDLE processHandle, DWORD processInformationClass, |
| 136 |
PVOID processInformation, ULONG processInformationLength, |
| 137 |
PULONG returnLength); |
| 138 |
typedef DWORD (WINAPI *NTPOWERINFORMATION) |
| 139 |
(DWORD powerInformationClass, PVOID inputBuffer, |
| 140 |
ULONG inputBufferLength, PVOID outputBuffer, ULONG outputBufferLength ); |
| 141 |
|
| 142 |
/* Type definitions for function pointers to call CryptoAPI functions. */ |
| 143 |
typedef BOOL (WINAPI *CRYPTACQUIRECONTEXT)(HCRYPTPROV *phProv, |
| 144 |
LPCTSTR pszContainer, |
| 145 |
LPCTSTR pszProvider, |
| 146 |
DWORD dwProvType, |
| 147 |
DWORD dwFlags); |
| 148 |
typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen, |
| 149 |
BYTE *pbBuffer); |
| 150 |
typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV hProv, DWORD dwFlags); |
| 151 |
|
| 152 |
/* Somewhat alternative functionality available as a direct call, for |
| 153 |
Windows XP and newer. This is the CryptoAPI RNG, which isn't anywhere |
| 154 |
near as good as the HW RNG, but we use it if it's present on the basis |
| 155 |
that at least it can't make things any worse. This direct access version |
| 156 |
is only available under Windows XP, we don't go out of our way to access |
| 157 |
the more general CryptoAPI one since the main purpose of using it is to |
| 158 |
take advantage of any possible future hardware RNGs that may be added, |
| 159 |
for example via TCPA devices. */ |
| 160 |
typedef BOOL (WINAPI *RTLGENRANDOM)(PVOID RandomBuffer, |
| 161 |
ULONG RandomBufferLength); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
/* MBM data structures, originally by Alexander van Kaam, converted to C by |
| 166 |
[email protected], finally updated by Chris Zahrt <[email protected]> */ |
| 167 |
#define BusType char |
| 168 |
#define SMBType char |
| 169 |
#define SensorType char |
| 170 |
|
| 171 |
typedef struct |
| 172 |
{ |
| 173 |
SensorType iType; /* Type of sensor. */ |
| 174 |
int Count; /* Number of sensor for that type. */ |
| 175 |
} SharedIndex; |
| 176 |
|
| 177 |
typedef struct |
| 178 |
{ |
| 179 |
SensorType ssType; /* Type of sensor */ |
| 180 |
unsigned char ssName[12]; /* Name of sensor */ |
| 181 |
char sspadding1[3]; /* Padding of 3 bytes */ |
| 182 |
double ssCurrent; /* Current value */ |
| 183 |
double ssLow; /* Lowest readout */ |
| 184 |
double ssHigh; /* Highest readout */ |
| 185 |
long ssCount; /* Total number of readout */ |
| 186 |
char sspadding2[4]; /* Padding of 4 bytes */ |
| 187 |
long double ssTotal; /* Total amount of all readouts */ |
| 188 |
char sspadding3[6]; /* Padding of 6 bytes */ |
| 189 |
double ssAlarm1; /* Temp & fan: high alarm; voltage: % off */ |
| 190 |
double ssAlarm2; /* Temp: low alarm */ |
| 191 |
} SharedSensor; |
| 192 |
|
| 193 |
typedef struct |
| 194 |
{ |
| 195 |
short siSMB_Base; /* SMBus base address */ |
| 196 |
BusType siSMB_Type; /* SMBus/Isa bus used to access chip */ |
| 197 |
SMBType siSMB_Code; /* SMBus sub type, Intel, AMD or ALi */ |
| 198 |
char siSMB_Addr; /* Address of sensor chip on SMBus */ |
| 199 |
unsigned char siSMB_Name[41]; /* Nice name for SMBus */ |
| 200 |
short siISA_Base; /* ISA base address of sensor chip on ISA */ |
| 201 |
int siChipType; /* Chip nr, connects with Chipinfo.ini */ |
| 202 |
char siVoltageSubType; /* Subvoltage option selected */ |
| 203 |
} SharedInfo; |
| 204 |
|
| 205 |
typedef struct |
| 206 |
{ |
| 207 |
double sdVersion; /* Version number (example: 51090) */ |
| 208 |
SharedIndex sdIndex[10]; /* Sensor index */ |
| 209 |
SharedSensor sdSensor[100]; /* Sensor info */ |
| 210 |
SharedInfo sdInfo; /* Misc.info */ |
| 211 |
unsigned char sdStart[41]; /* Start time */ |
| 212 |
|
| 213 |
/* We don't use the next two fields both because they're not random |
| 214 |
and because it provides a nice safety margin in case of data size |
| 215 |
mis- estimates (we always under-estimate the buffer size). */ |
| 216 |
#if 0 |
| 217 |
unsigned char sdCurrent[41]; /* Current time */ |
| 218 |
unsigned char sdPath[256]; /* MBM path */ |
| 219 |
#endif /*0*/ |
| 220 |
} SharedData; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
/* One time initialized handles and function pointers. We use dynamic |
| 225 |
loading of the DLLs to do without them in case libgcrypt does not |
| 226 |
need any random. */ |
| 227 |
static HANDLE hNetAPI32; |
| 228 |
static NETSTATISTICSGET pNetStatisticsGet; |
| 229 |
static NETAPIBUFFERSIZE pNetApiBufferSize; |
| 230 |
static NETAPIBUFFERFREE pNetApiBufferFree; |
| 231 |
|
| 232 |
static HANDLE hNTAPI; |
| 233 |
static NTQUERYSYSTEMINFORMATION pNtQuerySystemInformation; |
| 234 |
static NTQUERYINFORMATIONPROCESS pNtQueryInformationProcess; |
| 235 |
static NTPOWERINFORMATION pNtPowerInformation; |
| 236 |
|
| 237 |
static HANDLE hAdvAPI32; |
| 238 |
static CRYPTACQUIRECONTEXT pCryptAcquireContext; |
| 239 |
static CRYPTGENRANDOM pCryptGenRandom; |
| 240 |
static CRYPTRELEASECONTEXT pCryptReleaseContext; |
| 241 |
static RTLGENRANDOM pRtlGenRandom; |
| 242 |
|
| 243 |
|
| 244 |
/* Other module global variables. */ |
| 245 |
static int system_rng_available; /* Whether a system RNG is available. */ |
| 246 |
static HCRYPTPROV hRNGProv; /* Handle to Intel RNG CSP. */ |
| 247 |
|
| 248 |
/* The debug flag. Debugging is enabled if the value of the envvar |
| 249 |
* GCRY_RNDW32_DBG is a positive number.*/ |
| 250 |
static int debug_me; |
| 251 |
|
| 252 |
static int system_is_w2000; /* True if running on W2000. */ |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
/* Try and connect to the system RNG if there's one present. */ |
| 257 |
static void |
| 258 |
init_system_rng (void) |
| 259 |
{ |
| 260 |
system_rng_available = 0; |
| 261 |
hRNGProv = NULL; |
| 262 |
|
| 263 |
hAdvAPI32 = GetModuleHandle ("AdvAPI32.dll"); |
| 264 |
if (!hAdvAPI32) |
| 265 |
return; |
| 266 |
|
| 267 |
pCryptAcquireContext = (CRYPTACQUIRECONTEXT)(void *) |
| 268 |
GetProcAddress (hAdvAPI32, "CryptAcquireContextA"); |
| 269 |
pCryptGenRandom = (CRYPTGENRANDOM)(void *) |
| 270 |
GetProcAddress (hAdvAPI32, "CryptGenRandom"); |
| 271 |
pCryptReleaseContext = (CRYPTRELEASECONTEXT)(void *) |
| 272 |
GetProcAddress (hAdvAPI32, "CryptReleaseContext"); |
| 273 |
|
| 274 |
/* Get a pointer to the native randomness function if it's available. |
| 275 |
This isn't exported by name, so we have to get it by ordinal. */ |
| 276 |
pRtlGenRandom = (RTLGENRANDOM)(void *) |
| 277 |
GetProcAddress (hAdvAPI32, "SystemFunction036"); |
| 278 |
|
| 279 |
/* Try and connect to the PIII RNG CSP. The AMD 768 southbridge (from |
| 280 |
the 760 MP chipset) also has a hardware RNG, but there doesn't appear |
| 281 |
to be any driver support for this as there is for the Intel RNG so we |
| 282 |
can't do much with it. OTOH the Intel RNG is also effectively dead |
| 283 |
as well, mostly due to virtually nonexistent support/marketing by |
| 284 |
Intel, it's included here mostly for form's sake. */ |
| 285 |
if ( (!pCryptAcquireContext || !pCryptGenRandom || !pCryptReleaseContext |
| 286 |
|| !pCryptAcquireContext (&hRNGProv, NULL, INTEL_DEF_PROV, |
| 287 |
PROV_INTEL_SEC, 0) ) |
| 288 |
&& !pRtlGenRandom) |
| 289 |
{ |
| 290 |
hAdvAPI32 = NULL; |
| 291 |
} |
| 292 |
else |
| 293 |
system_rng_available = 1; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
/* Read data from the system RNG if availavle. */ |
| 298 |
static void |
| 299 |
read_system_rng (void (*add)(const void*, size_t, enum random_origins), |
| 300 |
enum random_origins requester) |
| 301 |
{ |
| 302 |
BYTE buffer[ SYSTEMRNG_BYTES + 8 ]; |
| 303 |
int quality = 0; |
| 304 |
|
| 305 |
if (!system_rng_available) |
| 306 |
return; |
| 307 |
|
| 308 |
/* Read SYSTEMRNG_BYTES bytes from the system RNG. We don't rely on |
| 309 |
this for all our randomness requirements (particularly the |
| 310 |
software RNG) in case it's broken in some way. */ |
| 311 |
if (hRNGProv) |
| 312 |
{ |
| 313 |
if (pCryptGenRandom (hRNGProv, SYSTEMRNG_BYTES, buffer)) |
| 314 |
quality = 80; |
| 315 |
} |
| 316 |
else if (pRtlGenRandom) |
| 317 |
{ |
| 318 |
if ( pRtlGenRandom (buffer, SYSTEMRNG_BYTES)) |
| 319 |
quality = 50; |
| 320 |
} |
| 321 |
if (quality > 0) |
| 322 |
{ |
| 323 |
if (debug_me) |
| 324 |
log_debug ("rndw32#read_system_rng: got %d bytes of quality %d\n", |
| 325 |
SYSTEMRNG_BYTES, quality); |
| 326 |
(*add) (buffer, SYSTEMRNG_BYTES, requester); |
| 327 |
wipememory (buffer, SYSTEMRNG_BYTES); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
/* Read data from MBM. This communicates via shared memory, so all we |
| 333 |
need to do is map a file and read the data out. */ |
| 334 |
static void |
| 335 |
read_mbm_data (void (*add)(const void*, size_t, enum random_origins), |
| 336 |
enum random_origins requester) |
| 337 |
{ |
| 338 |
HANDLE hMBMData; |
| 339 |
SharedData *mbmDataPtr; |
| 340 |
|
| 341 |
hMBMData = OpenFileMapping (FILE_MAP_READ, FALSE, "$M$B$M$5$S$D$" ); |
| 342 |
if (hMBMData) |
| 343 |
{ |
| 344 |
mbmDataPtr = (SharedData*)MapViewOfFile (hMBMData, FILE_MAP_READ,0,0,0); |
| 345 |
if (mbmDataPtr) |
| 346 |
{ |
| 347 |
if (debug_me) |
| 348 |
log_debug ("rndw32#read_mbm_data: got %d bytes\n", |
| 349 |
(int)sizeof (SharedData)); |
| 350 |
(*add) (mbmDataPtr, sizeof (SharedData), requester); |
| 351 |
UnmapViewOfFile (mbmDataPtr); |
| 352 |
} |
| 353 |
CloseHandle (hMBMData); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
/* Fallback method using the registry to poll the statistics. */ |
| 359 |
static void |
| 360 |
registry_poll (void (*add)(const void*, size_t, enum random_origins), |
| 361 |
enum random_origins requester) |
| 362 |
{ |
| 363 |
static int cbPerfData = PERFORMANCE_BUFFER_SIZE; |
| 364 |
int iterations; |
| 365 |
DWORD dwSize, status; |
| 366 |
PERF_DATA_BLOCK *pPerfData; |
| 367 |
|
| 368 |
/* Get information from the system performance counters. This can take a |
| 369 |
few seconds to do. In some environments the call to RegQueryValueEx() |
| 370 |
can produce an access violation at some random time in the future, in |
| 371 |
some cases adding a short delay after the following code block makes |
| 372 |
the problem go away. This problem is extremely difficult to |
| 373 |
reproduce, I haven't been able to get it to occur despite running it |
| 374 |
on a number of machines. MS knowledge base article Q178887 covers |
| 375 |
this type of problem, it's typically caused by an external driver or |
| 376 |
other program that adds its own values under the |
| 377 |
HKEY_PERFORMANCE_DATA key. The NT kernel, via Advapi32.dll, calls the |
| 378 |
required external module to map in the data inside an SEH try/except |
| 379 |
block, so problems in the module's collect function don't pop up until |
| 380 |
after it has finished, so the fault appears to occur in Advapi32.dll. |
| 381 |
There may be problems in the NT kernel as well though, a low-level |
| 382 |
memory checker indicated that ExpandEnvironmentStrings() in |
| 383 |
Kernel32.dll, called an interminable number of calls down inside |
| 384 |
RegQueryValueEx(), was overwriting memory (it wrote twice the |
| 385 |
allocated size of a buffer to a buffer allocated by the NT kernel). |
| 386 |
OTOH this could be coming from the external module calling back into |
| 387 |
the kernel, which eventually causes the problem described above. |
| 388 |
|
| 389 |
Possibly as an extension of the problem that the krnlWaitSemaphore() |
| 390 |
call above works around, running two instances of cryptlib (e.g. two |
| 391 |
applications that use it) under NT4 can result in one of them hanging |
| 392 |
in the RegQueryValueEx() call. This happens only under NT4 and is |
| 393 |
hard to reproduce in any consistent manner. |
| 394 |
|
| 395 |
One workaround that helps a bit is to read the registry as a remote |
| 396 |
(rather than local) registry, it's possible that the use of a network |
| 397 |
RPC call isolates the calling app from the problem in that whatever |
| 398 |
service handles the RPC is taking the hit and not affecting the |
| 399 |
calling app. Since this would require another round of extensive |
| 400 |
testing to verify and the NT native API call is working fine, we'll |
| 401 |
stick with the native API call for now. |
| 402 |
|
| 403 |
Some versions of NT4 had a problem where the amount of data returned |
| 404 |
was mis-reported and would never settle down, because of this the code |
| 405 |
below includes a safety-catch that bails out after 10 attempts have |
| 406 |
been made, this results in no data being returned but at does ensure |
| 407 |
that the thread will terminate. |
| 408 |
|
| 409 |
In addition to these problems the code in RegQueryValueEx() that |
| 410 |
estimates the amount of memory required to return the performance |
| 411 |
counter information isn't very accurate (it's much worse than the |
| 412 |
"slightly-inaccurate" level that the MS docs warn about, it's usually |
| 413 |
wildly off) since it always returns a worst-case estimate which is |
| 414 |
usually nowhere near the actual amount required. For example it may |
| 415 |
report that 128K of memory is required, but only return 64K of data. |
| 416 |
|
| 417 |
Even worse than the registry-based performance counters is the |
| 418 |
performance data helper (PDH) shim that tries to make the counters |
| 419 |
look like the old Win16 API (which is also used by Win95). Under NT |
| 420 |
this can consume tens of MB of memory and huge amounts of CPU time |
| 421 |
while it gathers its data, and even running once can still consume |
| 422 |
about 1/2MB of memory */ |
| 423 |
if (getenv ("GCRYPT_RNDW32_NOPERF")) |
| 424 |
{ |
| 425 |
static int shown; |
| 426 |
|
| 427 |
if (!shown) |
| 428 |
{ |
| 429 |
shown = 1; |
| 430 |
log_info ("note: get performance data has been disabled\n"); |
| 431 |
} |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
pPerfData = xmalloc (cbPerfData); |
| 436 |
for (iterations=0; iterations < 10; iterations++) |
| 437 |
{ |
| 438 |
dwSize = cbPerfData; |
| 439 |
if ( debug_me ) |
| 440 |
log_debug ("rndw32#slow_gatherer_nt: get perf data\n" ); |
| 441 |
|
| 442 |
status = RegQueryValueEx (HKEY_PERFORMANCE_DATA, "Global", NULL, |
| 443 |
NULL, (LPBYTE) pPerfData, &dwSize); |
| 444 |
if (status == ERROR_SUCCESS) |
| 445 |
{ |
| 446 |
if (!memcmp (pPerfData->Signature, L"PERF", 8)) |
| 447 |
(*add) ( pPerfData, dwSize, requester ); |
| 448 |
else |
| 449 |
log_debug ("rndw32: no PERF signature\n"); |
| 450 |
break; |
| 451 |
} |
| 452 |
else if (status == ERROR_MORE_DATA) |
| 453 |
{ |
| 454 |
cbPerfData += PERFORMANCE_BUFFER_STEP; |
| 455 |
pPerfData = xrealloc (pPerfData, cbPerfData); |
| 456 |
} |
| 457 |
else |
| 458 |
{ |
| 459 |
static int been_here; |
| 460 |
|
| 461 |
/* Silence the error message. In particular under Wine (as |
| 462 |
of 2008) we would get swamped with such diagnotiscs. One |
| 463 |
such diagnotiscs should be enough. */ |
| 464 |
if (been_here != status) |
| 465 |
{ |
| 466 |
been_here = status; |
| 467 |
log_debug ("rndw32: get performance data problem: ec=%ld\n", |
| 468 |
status); |
| 469 |
} |
| 470 |
break; |
| 471 |
} |
| 472 |
} |
| 473 |
xfree (pPerfData); |
| 474 |
} |
| 475 |
|
| 476 |
/* Although this isn't documented in the Win32 API docs, it's necessary |
| 477 |
to explicitly close the HKEY_PERFORMANCE_DATA key after use (it's |
| 478 |
implicitly opened on the first call to RegQueryValueEx()). If this |
| 479 |
isn't done then any system components which provide performance data |
| 480 |
can't be removed or changed while the handle remains active. */ |
| 481 |
RegCloseKey (HKEY_PERFORMANCE_DATA); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
static void |
| 486 |
slow_gatherer ( void (*add)(const void*, size_t, enum random_origins), |
| 487 |
enum random_origins requester ) |
| 488 |
{ |
| 489 |
static int is_initialized = 0; |
| 490 |
static int is_workstation = 1; |
| 491 |
HANDLE hDevice; |
| 492 |
DWORD dwType, dwSize, dwResult; |
| 493 |
ULONG ulSize; |
| 494 |
int drive_no, status; |
| 495 |
int no_results = 0; |
| 496 |
void *buffer; |
| 497 |
|
| 498 |
if ( !is_initialized ) |
| 499 |
{ |
| 500 |
HKEY hKey; |
| 501 |
|
| 502 |
if ( debug_me ) |
| 503 |
log_debug ("rndw32#slow_gatherer: init toolkit\n" ); |
| 504 |
/* Find out whether this is an NT server or workstation if necessary */ |
| 505 |
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, |
| 506 |
"SYSTEM\\CurrentControlSet\\Control\\ProductOptions", |
| 507 |
0, KEY_READ, &hKey) == ERROR_SUCCESS) |
| 508 |
{ |
| 509 |
BYTE szValue[32 + 8]; |
| 510 |
dwSize = 32; |
| 511 |
|
| 512 |
if ( debug_me ) |
| 513 |
log_debug ("rndw32#slow_gatherer: check product options\n" ); |
| 514 |
|
| 515 |
status = RegQueryValueEx (hKey, "ProductType", 0, NULL, |
| 516 |
szValue, &dwSize); |
| 517 |
if (status == ERROR_SUCCESS && stricmp ((char*)szValue, "WinNT")) |
| 518 |
{ |
| 519 |
/* Note: There are (at least) three cases for ProductType: |
| 520 |
WinNT = NT Workstation, ServerNT = NT Server, LanmanNT = |
| 521 |
NT Server acting as a Domain Controller. */ |
| 522 |
is_workstation = 0; |
| 523 |
if ( debug_me ) |
| 524 |
log_debug ("rndw32: this is a NT server\n"); |
| 525 |
} |
| 526 |
RegCloseKey (hKey); |
| 527 |
} |
| 528 |
|
| 529 |
/* The following are fixed for the lifetime of the process so we |
| 530 |
only add them once */ |
| 531 |
/* readPnPData (); - we have not implemented that. */ |
| 532 |
|
| 533 |
/* Initialize the NetAPI32 function pointers if necessary */ |
| 534 |
hNetAPI32 = LoadLibrary ("NETAPI32.DLL"); |
| 535 |
if (hNetAPI32) |
| 536 |
{ |
| 537 |
if (debug_me) |
| 538 |
log_debug ("rndw32#slow_gatherer: netapi32 loaded\n" ); |
| 539 |
pNetStatisticsGet = (NETSTATISTICSGET)(void *) |
| 540 |
GetProcAddress (hNetAPI32, "NetStatisticsGet"); |
| 541 |
pNetApiBufferSize = (NETAPIBUFFERSIZE)(void *) |
| 542 |
GetProcAddress (hNetAPI32, "NetApiBufferSize"); |
| 543 |
pNetApiBufferFree = (NETAPIBUFFERFREE)(void *) |
| 544 |
GetProcAddress (hNetAPI32, "NetApiBufferFree"); |
| 545 |
|
| 546 |
if (!pNetStatisticsGet || !pNetApiBufferSize || !pNetApiBufferFree) |
| 547 |
{ |
| 548 |
FreeLibrary (hNetAPI32); |
| 549 |
hNetAPI32 = NULL; |
| 550 |
log_debug ("rndw32: No NETAPI found\n" ); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
/* Initialize the NT kernel native API function pointers if necessary */ |
| 555 |
hNTAPI = GetModuleHandle ("NTDll.dll"); |
| 556 |
if (hNTAPI) |
| 557 |
{ |
| 558 |
/* Get a pointer to the NT native information query functions */ |
| 559 |
pNtQuerySystemInformation = (NTQUERYSYSTEMINFORMATION)(void *) |
| 560 |
GetProcAddress (hNTAPI, "NtQuerySystemInformation"); |
| 561 |
pNtQueryInformationProcess = (NTQUERYINFORMATIONPROCESS)(void *) |
| 562 |
GetProcAddress (hNTAPI, "NtQueryInformationProcess"); |
| 563 |
pNtPowerInformation = (NTPOWERINFORMATION)(void *) |
| 564 |
GetProcAddress(hNTAPI, "NtPowerInformation"); |
| 565 |
|
| 566 |
if (!pNtQuerySystemInformation || !pNtQueryInformationProcess) |
| 567 |
hNTAPI = NULL; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
is_initialized = 1; |
| 572 |
} |
| 573 |
|
| 574 |
read_system_rng ( add, requester ); |
| 575 |
read_mbm_data ( add, requester ); |
| 576 |
|
| 577 |
/* Get network statistics. Note: Both NT Workstation and NT Server by |
| 578 |
default will be running both the workstation and server services. The |
| 579 |
heuristic below is probably useful though on the assumption that the |
| 580 |
majority of the network traffic will be via the appropriate service. |
| 581 |
In any case the network statistics return almost no randomness. */ |
| 582 |
{ |
| 583 |
LPBYTE lpBuffer; |
| 584 |
|
| 585 |
if (hNetAPI32 |
| 586 |
&& !pNetStatisticsGet (NULL, |
| 587 |
(LPWSTR)(is_workstation ? L"LanmanWorkstation" : |
| 588 |
L"LanmanServer"), 0, 0, &lpBuffer)) |
| 589 |
{ |
| 590 |
if ( debug_me ) |
| 591 |
log_debug ("rndw32#slow_gatherer: get netstats\n" ); |
| 592 |
pNetApiBufferSize (lpBuffer, &dwSize); |
| 593 |
(*add) ( lpBuffer, dwSize, requester ); |
| 594 |
pNetApiBufferFree (lpBuffer); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
/* Get disk I/O statistics for all the hard drives. 100 is an |
| 599 |
arbitrary failsafe limit. */ |
| 600 |
for (drive_no = 0; drive_no < 100 ; drive_no++) |
| 601 |
{ |
| 602 |
char diskPerformance[SIZEOF_DISK_PERFORMANCE_STRUCT + 8]; |
| 603 |
char szDevice[50]; |
| 604 |
|
| 605 |
/* Check whether we can access this device. */ |
| 606 |
snprintf (szDevice, sizeof szDevice, "\\\\.\\PhysicalDrive%d", |
| 607 |
drive_no); |
| 608 |
hDevice = CreateFile (szDevice, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 609 |
NULL, OPEN_EXISTING, 0, NULL); |
| 610 |
if (hDevice == INVALID_HANDLE_VALUE) |
| 611 |
break; /* No more drives. */ |
| 612 |
|
| 613 |
/* Note: This only works if you have turned on the disk performance |
| 614 |
counters with 'diskperf -y'. These counters are off by default. */ |
| 615 |
dwSize = sizeof diskPerformance; |
| 616 |
if (DeviceIoControl (hDevice, IOCTL_DISK_PERFORMANCE, NULL, 0, |
| 617 |
diskPerformance, SIZEOF_DISK_PERFORMANCE_STRUCT, |
| 618 |
&dwSize, NULL)) |
| 619 |
{ |
| 620 |
if ( debug_me ) |
| 621 |
log_debug ("rndw32#slow_gatherer: iostat drive %d\n", |
| 622 |
drive_no); |
| 623 |
(*add) (diskPerformance, dwSize, requester); |
| 624 |
} |
| 625 |
else |
| 626 |
{ |
| 627 |
DWORD err_code = GetLastError (); |
| 628 |
|
| 629 |
if (err_code == 0x32) |
| 630 |
/* If it's ERROR_NOT_SUPPORTED, skip the message, as it |
| 631 |
won't work. */ |
| 632 |
; |
| 633 |
else |
| 634 |
log_info ("NOTE: you should run 'diskperf -y' " |
| 635 |
"to enable the disk statistics\n"); |
| 636 |
} |
| 637 |
CloseHandle (hDevice); |
| 638 |
} |
| 639 |
|
| 640 |
/* In theory we should be using the Win32 performance query API to obtain |
| 641 |
unpredictable data from the system, however this is so unreliable (see |
| 642 |
the multiple sets of comments in registryPoll()) that it's too risky |
| 643 |
to rely on it except as a fallback in emergencies. Instead, we rely |
| 644 |
mostly on the NT native API function NtQuerySystemInformation(), which |
| 645 |
has the dual advantages that it doesn't have as many (known) problems |
| 646 |
as the Win32 equivalent and that it doesn't access the data indirectly |
| 647 |
via pseudo-registry keys, which means that it's much faster. Note |
| 648 |
that the Win32 equivalent actually works almost all of the time, the |
| 649 |
problem is that on one or two systems it can fail in strange ways that |
| 650 |
are never the same and can't be reproduced on any other system, which |
| 651 |
is why we use the native API here. Microsoft officially documented |
| 652 |
this function in early 2003, so it'll be fairly safe to use. */ |
| 653 |
if ( !hNTAPI ) |
| 654 |
{ |
| 655 |
registry_poll (add, requester); |
| 656 |
return; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
/* Scan the first 64 possible information types (we don't bother with |
| 661 |
increasing the buffer size as we do with the Win32 version of the |
| 662 |
performance data read, we may miss a few classes but it's no big deal). |
| 663 |
This scan typically yields around 20 pieces of data, there's nothing |
| 664 |
in the range 65...128 so chances are there won't be anything above |
| 665 |
there either. */ |
| 666 |
buffer = xmalloc (PERFORMANCE_BUFFER_SIZE); |
| 667 |
for (dwType = 0; dwType < 64; dwType++) |
| 668 |
{ |
| 669 |
switch (dwType) |
| 670 |
{ |
| 671 |
/* ID 17 = SystemObjectInformation hangs on some win2k systems. */ |
| 672 |
case 17: |
| 673 |
if (system_is_w2000) |
| 674 |
continue; |
| 675 |
break; |
| 676 |
|
| 677 |
/* Some information types are write-only (the IDs are shared with |
| 678 |
a set-information call), we skip these. */ |
| 679 |
case 26: case 27: case 38: case 46: case 47: case 48: case 52: |
| 680 |
continue; |
| 681 |
|
| 682 |
/* ID 53 = SystemSessionProcessInformation reads input from the |
| 683 |
output buffer, which has to contain a session ID and pointer |
| 684 |
to the actual buffer in which to store the session information. |
| 685 |
Because this isn't a standard query, we skip this. */ |
| 686 |
case 53: |
| 687 |
continue; |
| 688 |
} |
| 689 |
|
| 690 |
/* Query the info for this ID. Some results (for example for |
| 691 |
ID = 6, SystemCallCounts) are only available in checked builds |
| 692 |
of the kernel. A smaller subcless of results require that |
| 693 |
certain system config flags be set, for example |
| 694 |
SystemObjectInformation requires that the |
| 695 |
FLG_MAINTAIN_OBJECT_TYPELIST be set in NtGlobalFlags. To avoid |
| 696 |
having to special-case all of these, we try reading each one and |
| 697 |
only use those for which we get a success status. */ |
| 698 |
dwResult = pNtQuerySystemInformation (dwType, buffer, |
| 699 |
PERFORMANCE_BUFFER_SIZE - 2048, |
| 700 |
&ulSize); |
| 701 |
if (dwResult != ERROR_SUCCESS) |
| 702 |
continue; |
| 703 |
|
| 704 |
/* Some calls (e.g. ID = 23, SystemProcessorStatistics, and ID = 24, |
| 705 |
SystemDpcInformation) incorrectly return a length of zero, so we |
| 706 |
manually adjust the length to the correct value. */ |
| 707 |
if ( !ulSize ) |
| 708 |
{ |
| 709 |
if (dwType == 23) |
| 710 |
ulSize = 6 * sizeof (ULONG); |
| 711 |
else if (dwType == 24) |
| 712 |
ulSize = 5 * sizeof (ULONG); |
| 713 |
} |
| 714 |
|
| 715 |
/* If we got some data back, add it to the entropy pool. */ |
| 716 |
if (ulSize > 0 && ulSize <= PERFORMANCE_BUFFER_SIZE - 2048) |
| 717 |
{ |
| 718 |
if (debug_me) |
| 719 |
log_debug ("rndw32#slow_gatherer: %lu bytes from sysinfo %ld\n", |
| 720 |
ulSize, dwType); |
| 721 |
(*add) (buffer, ulSize, requester); |
| 722 |
no_results++; |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
/* Now we would do the same for the process information. This |
| 727 |
call would rather ugly in that it requires an exact length |
| 728 |
match for the data returned, failing with a |
| 729 |
STATUS_INFO_LENGTH_MISMATCH error code (0xC0000004) if the |
| 730 |
length isn't an exact match. It requires a compiler to handle |
| 731 |
complex nested structs, alignment issues, and so on, and |
| 732 |
without the headers in which the entries are declared it's |
| 733 |
almost impossible to do. Thus we don't. */ |
| 734 |
|
| 735 |
|
| 736 |
/* Finally, do the same for the system power status information. There |
| 737 |
are only a limited number of useful information types available so we |
| 738 |
restrict ourselves to the useful types. In addition since this |
| 739 |
function doesn't return length information, we have to hardcode in |
| 740 |
length data. */ |
| 741 |
if (pNtPowerInformation) |
| 742 |
{ |
| 743 |
static const struct { int type; int size; } powerInfo[] = { |
| 744 |
{ 0, 128 }, /* SystemPowerPolicyAc */ |
| 745 |
{ 1, 128 }, /* SystemPowerPolicyDc */ |
| 746 |
{ 4, 64 }, /* SystemPowerCapabilities */ |
| 747 |
{ 5, 48 }, /* SystemBatteryState */ |
| 748 |
{ 11, 48 }, /* ProcessorInformation */ |
| 749 |
{ 12, 24 }, /* SystemPowerInformation */ |
| 750 |
{ -1, -1 } |
| 751 |
}; |
| 752 |
int i; |
| 753 |
|
| 754 |
/* The 100 is a failsafe limit. */ |
| 755 |
for (i = 0; powerInfo[i].type != -1 && i < 100; i++ ) |
| 756 |
{ |
| 757 |
/* Query the info for this ID */ |
| 758 |
dwResult = pNtPowerInformation (powerInfo[i].type, NULL, 0, buffer, |
| 759 |
PERFORMANCE_BUFFER_SIZE - 2048); |
| 760 |
if (dwResult != ERROR_SUCCESS) |
| 761 |
continue; |
| 762 |
if (debug_me) |
| 763 |
log_debug ("rndw32#slow_gatherer: %u bytes from powerinfo %d\n", |
| 764 |
powerInfo[i].size, i); |
| 765 |
(*add) (buffer, powerInfo[i].size, requester); |
| 766 |
no_results++; |
| 767 |
} |
| 768 |
gcry_assert (i < 100); |
| 769 |
} |
| 770 |
xfree (buffer); |
| 771 |
|
| 772 |
/* We couldn't get enough results from the kernel, fall back to the |
| 773 |
somewhat troublesome registry poll. */ |
| 774 |
if (no_results < 15) |
| 775 |
registry_poll (add, requester); |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
int |
| 780 |
_gcry_rndw32_gather_random (void (*add)(const void*, size_t, |
| 781 |
enum random_origins), |
| 782 |
enum random_origins origin, |
| 783 |
size_t length, int level ) |
| 784 |
{ |
| 785 |
static int is_initialized; |
| 786 |
size_t n; |
| 787 |
|
| 788 |
if (!level) |
| 789 |
return 0; |
| 790 |
|
| 791 |
/* We don't differentiate between level 1 and 2 here because there |
| 792 |
is no internal entropy pool as a scary resource. It may all work |
| 793 |
slower, but because our entropy source will never block but |
| 794 |
deliver some not easy to measure entropy, we assume level 2. */ |
| 795 |
|
| 796 |
if (!is_initialized) |
| 797 |
{ |
| 798 |
OSVERSIONINFO osvi = { sizeof( osvi ) }; |
| 799 |
const char *s; |
| 800 |
|
| 801 |
if ((s = getenv ("GCRYPT_RNDW32_DBG")) && atoi (s) > 0) |
| 802 |
debug_me = 1; |
| 803 |
|
| 804 |
GetVersionEx( &osvi ); |
| 805 |
if (osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) |
| 806 |
log_fatal ("can only run on a Windows NT platform\n" ); |
| 807 |
system_is_w2000 = (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0); |
| 808 |
|
| 809 |
init_system_rng (); |
| 810 |
is_initialized = 1; |
| 811 |
} |
| 812 |
|
| 813 |
if (debug_me) |
| 814 |
log_debug ("rndw32#gather_random: ori=%d len=%u lvl=%d\n", |
| 815 |
origin, (unsigned int)length, level ); |
| 816 |
|
| 817 |
slow_gatherer (add, origin); |
| 818 |
|
| 819 |
/* Round requested LENGTH up to full 32 bytes. */ |
| 820 |
n = _gcry_rndjent_poll (add, origin, ((length + 31) / 32) * 32); |
| 821 |
|
| 822 |
if (debug_me) |
| 823 |
log_debug ("rndw32#gather_random: jent contributed extra %u bytes\n", |
| 824 |
(unsigned int)n); |
| 825 |
|
| 826 |
return 0; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
void |
| 832 |
_gcry_rndw32_gather_random_fast (void (*add)(const void*, size_t, |
| 833 |
enum random_origins), |
| 834 |
enum random_origins origin) |
| 835 |
{ |
| 836 |
static int addedFixedItems = 0; |
| 837 |
|
| 838 |
if ( debug_me ) |
| 839 |
log_debug ("rndw32#gather_random_fast: ori=%d\n", origin ); |
| 840 |
|
| 841 |
/* Get various basic pieces of system information: Handle of active |
| 842 |
window, handle of window with mouse capture, handle of clipboard |
| 843 |
owner handle of start of clpboard viewer list, pseudohandle of |
| 844 |
current process, current process ID, pseudohandle of current |
| 845 |
thread, current thread ID, handle of desktop window, handle of |
| 846 |
window with keyboard focus, whether system queue has any events, |
| 847 |
cursor position for last message, 1 ms time for last message, |
| 848 |
handle of window with clipboard open, handle of process heap, |
| 849 |
handle of procs window station, types of events in input queue, |
| 850 |
and milliseconds since Windows was started. On 64-bit platform |
| 851 |
some of these return values are pointers and thus 64-bit wide. |
| 852 |
We discard the upper 32-bit of those values. */ |
| 853 |
|
| 854 |
{ |
| 855 |
byte buffer[20*sizeof(unsigned long)], *bufptr; |
| 856 |
|
| 857 |
bufptr = buffer; |
| 858 |
#define ADDINT(f) do { unsigned long along = (unsigned long)(f); \ |
| 859 |
memcpy (bufptr, &along, sizeof (along) ); \ |
| 860 |
bufptr += sizeof (along); \ |
| 861 |
} while (0) |
| 862 |
#define ADDPTR(f) do { void *aptr = (f); \ |
| 863 |
ADDINT((SIZE_T)aptr); \ |
| 864 |
} while (0) |
| 865 |
|
| 866 |
ADDPTR ( GetActiveWindow ()); |
| 867 |
ADDPTR ( GetCapture ()); |
| 868 |
ADDPTR ( GetClipboardOwner ()); |
| 869 |
ADDPTR ( GetClipboardViewer ()); |
| 870 |
ADDPTR ( GetCurrentProcess ()); |
| 871 |
ADDINT ( GetCurrentProcessId ()); |
| 872 |
ADDPTR ( GetCurrentThread ()); |
| 873 |
ADDINT ( GetCurrentThreadId ()); |
| 874 |
ADDPTR ( GetDesktopWindow ()); |
| 875 |
ADDPTR ( GetFocus ()); |
| 876 |
ADDINT ( GetInputState ()); |
| 877 |
ADDINT ( GetMessagePos ()); |
| 878 |
ADDINT ( GetMessageTime ()); |
| 879 |
ADDPTR ( GetOpenClipboardWindow ()); |
| 880 |
ADDPTR ( GetProcessHeap ()); |
| 881 |
ADDPTR ( GetProcessWindowStation ()); |
| 882 |
/* Following function in some cases stops returning events, and cannot |
| 883 |
be used as an entropy source. */ |
| 884 |
/*ADDINT ( GetQueueStatus (QS_ALLEVENTS));*/ |
| 885 |
ADDINT ( GetTickCount ()); |
| 886 |
|
| 887 |
gcry_assert ( bufptr-buffer < sizeof (buffer) ); |
| 888 |
(*add) ( buffer, bufptr-buffer, origin ); |
| 889 |
#undef ADDINT |
| 890 |
#undef ADDPTR |
| 891 |
} |
| 892 |
|
| 893 |
/* Get multiword system information: Current caret position, current |
| 894 |
mouse cursor position. */ |
| 895 |
{ |
| 896 |
POINT point; |
| 897 |
|
| 898 |
GetCaretPos (&point); |
| 899 |
(*add) ( &point, sizeof (point), origin ); |
| 900 |
GetCursorPos (&point); |
| 901 |
(*add) ( &point, sizeof (point), origin ); |
| 902 |
} |
| 903 |
|
| 904 |
/* Get percent of memory in use, bytes of physical memory, bytes of |
| 905 |
free physical memory, bytes in paging file, free bytes in paging |
| 906 |
file, user bytes of address space, and free user bytes. */ |
| 907 |
{ |
| 908 |
MEMORYSTATUS memoryStatus; |
| 909 |
|
| 910 |
memoryStatus.dwLength = sizeof (MEMORYSTATUS); |
| 911 |
GlobalMemoryStatus (&memoryStatus); |
| 912 |
(*add) ( &memoryStatus, sizeof (memoryStatus), origin ); |
| 913 |
} |
| 914 |
|
| 915 |
/* Get thread and process creation time, exit time, time in kernel |
| 916 |
mode, and time in user mode in 100ns intervals. */ |
| 917 |
{ |
| 918 |
HANDLE handle; |
| 919 |
FILETIME creationTime, exitTime, kernelTime, userTime; |
| 920 |
SIZE_T minimumWorkingSetSize, maximumWorkingSetSize; |
| 921 |
|
| 922 |
handle = GetCurrentThread (); |
| 923 |
GetThreadTimes (handle, &creationTime, &exitTime, |
| 924 |
&kernelTime, &userTime); |
| 925 |
(*add) ( &creationTime, sizeof (creationTime), origin ); |
| 926 |
(*add) ( &exitTime, sizeof (exitTime), origin ); |
| 927 |
(*add) ( &kernelTime, sizeof (kernelTime), origin ); |
| 928 |
(*add) ( &userTime, sizeof (userTime), origin ); |
| 929 |
|
| 930 |
handle = GetCurrentProcess (); |
| 931 |
GetProcessTimes (handle, &creationTime, &exitTime, |
| 932 |
&kernelTime, &userTime); |
| 933 |
(*add) ( &creationTime, sizeof (creationTime), origin ); |
| 934 |
(*add) ( &exitTime, sizeof (exitTime), origin ); |
| 935 |
(*add) ( &kernelTime, sizeof (kernelTime), origin ); |
| 936 |
(*add) ( &userTime, sizeof (userTime), origin ); |
| 937 |
|
| 938 |
/* Get the minimum and maximum working set size for the current |
| 939 |
process. */ |
| 940 |
GetProcessWorkingSetSize (handle, &minimumWorkingSetSize, |
| 941 |
&maximumWorkingSetSize); |
| 942 |
/* On 64-bit system, discard the high 32-bits. */ |
| 943 |
(*add) ( &minimumWorkingSetSize, sizeof (int), origin ); |
| 944 |
(*add) ( &maximumWorkingSetSize, sizeof (int), origin ); |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
/* The following are fixed for the lifetime of the process so we only |
| 949 |
* add them once */ |
| 950 |
if (!addedFixedItems) |
| 951 |
{ |
| 952 |
STARTUPINFO startupInfo; |
| 953 |
|
| 954 |
/* Get name of desktop, console window title, new window |
| 955 |
position and size, window flags, and handles for stdin, |
| 956 |
stdout, and stderr. */ |
| 957 |
startupInfo.cb = sizeof (STARTUPINFO); |
| 958 |
GetStartupInfo (&startupInfo); |
| 959 |
(*add) ( &startupInfo, sizeof (STARTUPINFO), origin ); |
| 960 |
addedFixedItems = 1; |
| 961 |
} |
| 962 |
|
| 963 |
/* The performance of QPC varies depending on the architecture it's |
| 964 |
running on and on the OS, the MS documentation is vague about the |
| 965 |
details because it varies so much. Under Win9x/ME it reads the |
| 966 |
1.193180 MHz PIC timer. Under NT/Win2K/XP it may or may not read the |
| 967 |
64-bit TSC depending on the HAL and assorted other circumstances, |
| 968 |
generally on machines with a uniprocessor HAL |
| 969 |
KeQueryPerformanceCounter() uses a 3.579545MHz timer and on machines |
| 970 |
with a multiprocessor or APIC HAL it uses the TSC (the exact time |
| 971 |
source is controlled by the HalpUse8254 flag in the kernel). That |
| 972 |
choice of time sources is somewhat peculiar because on a |
| 973 |
multiprocessor machine it's theoretically possible to get completely |
| 974 |
different TSC readings depending on which CPU you're currently |
| 975 |
running on, while for uniprocessor machines it's not a problem. |
| 976 |
However, the kernel appears to synchronise the TSCs across CPUs at |
| 977 |
boot time (it resets the TSC as part of its system init), so this |
| 978 |
shouldn't really be a problem. Under WinCE it's completely platform- |
| 979 |
dependent, if there's no hardware performance counter available, it |
| 980 |
uses the 1ms system timer. |
| 981 |
|
| 982 |
Another feature of the TSC (although it doesn't really affect us here) |
| 983 |
is that mobile CPUs will turn off the TSC when they idle, Pentiums |
| 984 |
will change the rate of the counter when they clock-throttle (to |
| 985 |
match the current CPU speed), and hyperthreading Pentiums will turn |
| 986 |
it off when both threads are idle (this more or less makes sense, |
| 987 |
since the CPU will be in the halted state and not executing any |
| 988 |
instructions to count). |
| 989 |
|
| 990 |
To make things unambiguous, we detect a CPU new enough to call RDTSC |
| 991 |
directly by checking for CPUID capabilities, and fall back to QPC if |
| 992 |
this isn't present. |
| 993 |
|
| 994 |
On AMD64, TSC is always available and intrinsic is provided for accessing |
| 995 |
it. */ |
| 996 |
#ifdef __x86_64__ |
| 997 |
{ |
| 998 |
unsigned __int64 aint64; |
| 999 |
|
| 1000 |
/* Note: cryptlib does not discard upper 32 bits of TSC on WIN64, but does |
| 1001 |
* on WIN32. Is this correct? */ |
| 1002 |
aint64 = __rdtsc(); |
| 1003 |
(*add) (&aint64, sizeof(aint64), origin); |
| 1004 |
} |
| 1005 |
#else |
| 1006 |
#ifdef __GNUC__ |
| 1007 |
/* FIXME: We would need to implement the CPU feature tests first. */ |
| 1008 |
/* if (cpu_has_feature_rdtsc) */ |
| 1009 |
/* { */ |
| 1010 |
/* uint32_t lo, hi; */ |
| 1011 |
/* We cannot use "=A", since this would use %rax on x86_64. */ |
| 1012 |
/* __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); */ |
| 1013 |
/* Ignore high 32 bits, hwich are >1s res. */ |
| 1014 |
/* (*add) (&lo, 4, origin ); */ |
| 1015 |
/* } */ |
| 1016 |
/* else */ |
| 1017 |
#endif /*!__GNUC__*/ |
| 1018 |
{ |
| 1019 |
LARGE_INTEGER performanceCount; |
| 1020 |
|
| 1021 |
if (QueryPerformanceCounter (&performanceCount)) |
| 1022 |
{ |
| 1023 |
if ( debug_me ) |
| 1024 |
log_debug ("rndw32#gather_random_fast: perf data\n"); |
| 1025 |
(*add) (&performanceCount, sizeof (performanceCount), origin); |
| 1026 |
} |
| 1027 |
else |
| 1028 |
{ |
| 1029 |
/* Millisecond accuracy at best... */ |
| 1030 |
DWORD aword = GetTickCount (); |
| 1031 |
(*add) (&aword, sizeof (aword), origin ); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
#endif /*__x86_64__*/ |
| 1035 |
|
| 1036 |
|
| 1037 |
} |