Make rand_win.c UNICODE savvy.

This commit is contained in:
Andy Polyakov 2004-07-21 17:17:30 +00:00
parent 64ba6cf222
commit 89c53672c2

View file

@ -125,7 +125,7 @@
* http://developer.intel.com/design/security/rng/redist_license.htm * http://developer.intel.com/design/security/rng/redist_license.htm
*/ */
#define PROV_INTEL_SEC 22 #define PROV_INTEL_SEC 22
#define INTEL_DEF_PROV TEXT("Intel Hardware Cryptographic Service Provider") #define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
static void readtimer(void); static void readtimer(void);
static void readscreen(void); static void readscreen(void);
@ -152,7 +152,7 @@ typedef struct tagCURSORINFO
#define CURSOR_SHOWING 0x00000001 #define CURSOR_SHOWING 0x00000001
#endif /* CURSOR_SHOWING */ #endif /* CURSOR_SHOWING */
typedef BOOL (WINAPI *CRYPTACQUIRECONTEXT)(HCRYPTPROV *, LPCTSTR, LPCTSTR, typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTW)(HCRYPTPROV *, LPCWSTR, LPCWSTR,
DWORD, DWORD); DWORD, DWORD);
typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *); typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *);
typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV, DWORD); typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV, DWORD);
@ -194,7 +194,7 @@ int RAND_poll(void)
HWND h; HWND h;
HMODULE advapi, kernel, user, netapi; HMODULE advapi, kernel, user, netapi;
CRYPTACQUIRECONTEXT acquire = 0; CRYPTACQUIRECONTEXTW acquire = 0;
CRYPTGENRANDOM gen = 0; CRYPTGENRANDOM gen = 0;
CRYPTRELEASECONTEXT release = 0; CRYPTRELEASECONTEXT release = 0;
#if 1 /* There was previously a problem with NETSTATGET. Currently, this #if 1 /* There was previously a problem with NETSTATGET. Currently, this
@ -213,6 +213,9 @@ int RAND_poll(void)
GetVersionEx( &osverinfo ) ; GetVersionEx( &osverinfo ) ;
#if defined(OPENSSL_SYS_WINCE) && WCEPLATFORM!=MS_HPC_PRO #if defined(OPENSSL_SYS_WINCE) && WCEPLATFORM!=MS_HPC_PRO
#ifndef CryptAcquireContext
#define CryptAcquireContext CryptAcquireContextW
#endif
/* poll the CryptoAPI PRNG */ /* poll the CryptoAPI PRNG */
/* The CryptoAPI returns sizeof(buf) bytes of randomness */ /* The CryptoAPI returns sizeof(buf) bytes of randomness */
if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
@ -223,21 +226,35 @@ int RAND_poll(void)
} }
#endif #endif
#ifndef OPENSSL_SYS_WINCE
/*
* None of below libraries are present on Windows CE, which is
* why we #ifndef the whole section. This also excuses us from
* handling the GetProcAddress issue. The trouble is that in
* real Win32 API GetProcAddress is available in ANSI flavor
* only. In WinCE on the other hand GetProcAddress is a macro
* most commonly defined as GetProcAddressW, which accepts
* Unicode argument. If we were to call GetProcAddress under
* WinCE, I'd recommend to either redefine GetProcAddress as
* GetProcAddressA (there seem to be one in common CE spec) or
* implement own shim routine, which would accept ANSI argument
* and expand it to Unicode.
*/
/* load functions dynamically - not available on all systems */ /* load functions dynamically - not available on all systems */
advapi = LoadLibrary(TEXT("ADVAPI32.DLL")); advapi = LoadLibrary(TEXT("ADVAPI32.DLL"));
kernel = LoadLibrary(TEXT("KERNEL32.DLL")); kernel = LoadLibrary(TEXT("KERNEL32.DLL"));
user = LoadLibrary(TEXT("USER32.DLL")); user = LoadLibrary(TEXT("USER32.DLL"));
netapi = LoadLibrary(TEXT("NETAPI32.DLL")); netapi = LoadLibrary(TEXT("NETAPI32.DLL"));
#ifndef OPENSSL_SYS_WINCE
#if 1 /* There was previously a problem with NETSTATGET. Currently, this #if 1 /* There was previously a problem with NETSTATGET. Currently, this
* section is still experimental, but if all goes well, this conditional * section is still experimental, but if all goes well, this conditional
* will be removed * will be removed
*/ */
if (netapi) if (netapi)
{ {
netstatget = (NETSTATGET) GetProcAddress(netapi,TEXT("NetStatisticsGet")); netstatget = (NETSTATGET) GetProcAddress(netapi,"NetStatisticsGet");
netfree = (NETFREE) GetProcAddress(netapi,TEXT("NetApiBufferFree")); netfree = (NETFREE) GetProcAddress(netapi,"NetApiBufferFree");
} }
if (netstatget && netfree) if (netstatget && netfree)
@ -264,9 +281,7 @@ int RAND_poll(void)
if (netapi) if (netapi)
FreeLibrary(netapi); FreeLibrary(netapi);
#endif /* 1 */ #endif /* 1 */
#endif /* !OPENSSL_SYS_WINCE */
#ifndef OPENSSL_SYS_WINCE
/* It appears like this can cause an exception deep within ADVAPI32.DLL /* It appears like this can cause an exception deep within ADVAPI32.DLL
* at random times on Windows 2000. Reported by Jeffrey Altman. * at random times on Windows 2000. Reported by Jeffrey Altman.
* Only use it on NT. * Only use it on NT.
@ -321,16 +336,20 @@ int RAND_poll(void)
free(buf); free(buf);
} }
#endif #endif
#endif /* !OPENSSL_SYS_WINCE */
if (advapi) if (advapi)
{ {
acquire = (CRYPTACQUIRECONTEXT) GetProcAddress(advapi, /*
TEXT("CryptAcquireContextA")); * If it's available, then it's available in both ANSI
* and UNICODE flavors even in Win9x, documentation says.
* We favor Unicode...
*/
acquire = (CRYPTACQUIRECONTEXTW) GetProcAddress(advapi,
"CryptAcquireContextW");
gen = (CRYPTGENRANDOM) GetProcAddress(advapi, gen = (CRYPTGENRANDOM) GetProcAddress(advapi,
TEXT("CryptGenRandom")); "CryptGenRandom");
release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi, release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi,
TEXT("CryptReleaseContext")); "CryptReleaseContext");
} }
if (acquire && gen && release) if (acquire && gen && release)
@ -367,26 +386,15 @@ int RAND_poll(void)
if (advapi) if (advapi)
FreeLibrary(advapi); FreeLibrary(advapi);
/* timer data */
readtimer();
/* memory usage statistics */
GlobalMemoryStatus(&m);
RAND_add(&m, sizeof(m), 1);
/* process ID */
w = GetCurrentProcessId();
RAND_add(&w, sizeof(w), 1);
if (user) if (user)
{ {
GETCURSORINFO cursor; GETCURSORINFO cursor;
GETFOREGROUNDWINDOW win; GETFOREGROUNDWINDOW win;
GETQUEUESTATUS queue; GETQUEUESTATUS queue;
win = (GETFOREGROUNDWINDOW) GetProcAddress(user, TEXT("GetForegroundWindow")); win = (GETFOREGROUNDWINDOW) GetProcAddress(user, "GetForegroundWindow");
cursor = (GETCURSORINFO) GetProcAddress(user, TEXT("GetCursorInfo")); cursor = (GETCURSORINFO) GetProcAddress(user, "GetCursorInfo");
queue = (GETQUEUESTATUS) GetProcAddress(user, TEXT("GetQueueStatus")); queue = (GETQUEUESTATUS) GetProcAddress(user, "GetQueueStatus");
if (win) if (win)
{ {
@ -458,19 +466,19 @@ int RAND_poll(void)
MODULEENTRY32 m; MODULEENTRY32 m;
snap = (CREATETOOLHELP32SNAPSHOT) snap = (CREATETOOLHELP32SNAPSHOT)
GetProcAddress(kernel, TEXT("CreateToolhelp32Snapshot")); GetProcAddress(kernel, "CreateToolhelp32Snapshot");
close_snap = (CLOSETOOLHELP32SNAPSHOT) close_snap = (CLOSETOOLHELP32SNAPSHOT)
GetProcAddress(kernel, TEXT("CloseToolhelp32Snapshot")); GetProcAddress(kernel, "CloseToolhelp32Snapshot");
heap_first = (HEAP32FIRST) GetProcAddress(kernel, TEXT("Heap32First")); heap_first = (HEAP32FIRST) GetProcAddress(kernel, "Heap32First");
heap_next = (HEAP32NEXT) GetProcAddress(kernel, TEXT("Heap32Next")); heap_next = (HEAP32NEXT) GetProcAddress(kernel, "Heap32Next");
heaplist_first = (HEAP32LIST) GetProcAddress(kernel, TEXT("Heap32ListFirst")); heaplist_first = (HEAP32LIST) GetProcAddress(kernel, "Heap32ListFirst");
heaplist_next = (HEAP32LIST) GetProcAddress(kernel, TEXT("Heap32ListNext")); heaplist_next = (HEAP32LIST) GetProcAddress(kernel, "Heap32ListNext");
process_first = (PROCESS32) GetProcAddress(kernel, TEXT("Process32First")); process_first = (PROCESS32) GetProcAddress(kernel, "Process32First");
process_next = (PROCESS32) GetProcAddress(kernel, TEXT("Process32Next")); process_next = (PROCESS32) GetProcAddress(kernel, "Process32Next");
thread_first = (THREAD32) GetProcAddress(kernel, TEXT("Thread32First")); thread_first = (THREAD32) GetProcAddress(kernel, "Thread32First");
thread_next = (THREAD32) GetProcAddress(kernel, TEXT("Thread32Next")); thread_next = (THREAD32) GetProcAddress(kernel, "Thread32Next");
module_first = (MODULE32) GetProcAddress(kernel, TEXT("Module32First")); module_first = (MODULE32) GetProcAddress(kernel, "Module32First");
module_next = (MODULE32) GetProcAddress(kernel, TEXT("Module32Next")); module_next = (MODULE32) GetProcAddress(kernel, "Module32Next");
if (snap && heap_first && heap_next && heaplist_first && if (snap && heap_first && heap_next && heaplist_first &&
heaplist_next && process_first && process_next && heaplist_next && process_first && process_next &&
@ -546,6 +554,18 @@ int RAND_poll(void)
FreeLibrary(kernel); FreeLibrary(kernel);
} }
#endif /* !OPENSSL_SYS_WINCE */
/* timer data */
readtimer();
/* memory usage statistics */
GlobalMemoryStatus(&m);
RAND_add(&m, sizeof(m), 1);
/* process ID */
w = GetCurrentProcessId();
RAND_add(&w, sizeof(w), 1);
#if 0 #if 0
printf("Exiting RAND_poll\n"); printf("Exiting RAND_poll\n");
@ -607,7 +627,7 @@ static void readtimer(void)
DWORD w; DWORD w;
LARGE_INTEGER l; LARGE_INTEGER l;
static int have_perfc = 1; static int have_perfc = 1;
#if defined(_MSC_VER) && !defined(OPENSSL_SYS_WINCE) #if defined(_MSC_VER) && defined(_M_X86)
static int have_tsc = 1; static int have_tsc = 1;
DWORD cyclecount; DWORD cyclecount;