116 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
119 # define _WIN32_WINNT 0x0400
121 #include <wincrypt.h>
126 #define MAXDELAY 1000
131 #define PROV_INTEL_SEC 22
132 #define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
134 static void readtimer(
void);
135 static void readscreen(
void);
144 #ifndef CURSOR_SHOWING
148 typedef struct tagCURSORINFO
154 } CURSORINFO, *PCURSORINFO, *LPCURSORINFO;
156 #define CURSOR_SHOWING 0x00000001
159 #if !defined(OPENSSL_SYS_WINCE)
160 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTW)(HCRYPTPROV *, LPCWSTR, LPCWSTR,
162 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *);
163 typedef BOOL (WINAPI *CRYPTRELEASECONTEXT)(HCRYPTPROV, DWORD);
165 typedef HWND (WINAPI *GETFOREGROUNDWINDOW)(VOID);
166 typedef BOOL (WINAPI *GETCURSORINFO)(PCURSORINFO);
167 typedef DWORD (WINAPI *GETQUEUESTATUS)(UINT);
169 typedef HANDLE (WINAPI *CREATETOOLHELP32SNAPSHOT)(DWORD, DWORD);
170 typedef BOOL (WINAPI *CLOSETOOLHELP32SNAPSHOT)(HANDLE);
171 typedef BOOL (WINAPI *HEAP32FIRST)(
LPHEAPENTRY32, DWORD, size_t);
173 typedef BOOL (WINAPI *HEAP32LIST)(HANDLE,
LPHEAPLIST32);
186 typedef NET_API_STATUS (NET_API_FUNCTION * NETSTATGET)
187 (LPWSTR, LPWSTR, DWORD, DWORD, LPBYTE*);
188 typedef NET_API_STATUS (NET_API_FUNCTION * NETFREE)(LPBYTE);
195 HCRYPTPROV hProvider = 0;
202 OSVERSIONINFO osverinfo ;
203 osverinfo.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO) ;
204 GetVersionEx( &osverinfo ) ;
206 #if defined(OPENSSL_SYS_WINCE)
207 # if defined(_WIN32_WCE) && _WIN32_WCE>=300
214 if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
215 CRYPT_VERIFYCONTEXT))
217 if (CryptGenRandom(hProvider,
sizeof(buf), buf))
218 RAND_add(buf,
sizeof(buf),
sizeof(buf));
219 CryptReleaseContext(hProvider, 0);
239 HMODULE advapi = LoadLibrary(TEXT(
"ADVAPI32.DLL"));
240 HMODULE kernel = LoadLibrary(TEXT(
"KERNEL32.DLL"));
242 HMODULE netapi = LoadLibrary(TEXT(
"NETAPI32.DLL"));
243 CRYPTACQUIRECONTEXTW acquire = NULL;
244 CRYPTGENRANDOM
gen = NULL;
245 CRYPTRELEASECONTEXT release = NULL;
246 NETSTATGET netstatget = NULL;
247 NETFREE netfree = NULL;
252 netstatget = (NETSTATGET) GetProcAddress(netapi,
"NetStatisticsGet");
253 netfree = (NETFREE) GetProcAddress(netapi,
"NetApiBufferFree");
256 if (netstatget && netfree)
265 if (netstatget(NULL, L
"LanmanWorkstation", 0, 0, &outbuf) == 0)
267 RAND_add(outbuf,
sizeof(STAT_WORKSTATION_0), 45);
270 if (netstatget(NULL, L
"LanmanServer", 0, 0, &outbuf) == 0)
272 RAND_add(outbuf,
sizeof(STAT_SERVER_0), 17);
288 if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
289 osverinfo.dwMajorVersion < 5)
297 LONG rc=ERROR_MORE_DATA;
302 while (rc == ERROR_MORE_DATA)
304 buf = realloc(buf,bufsz+8192);
310 rc = RegQueryValueEx(HKEY_PERFORMANCE_DATA, TEXT(
"Global"),
311 NULL, NULL, buf, &length);
313 if (rc == ERROR_SUCCESS)
318 RAND_add(&length,
sizeof(length), 0);
319 RAND_add(buf, length, length / 4.0);
328 RegCloseKey(HKEY_PERFORMANCE_DATA);
342 acquire = (CRYPTACQUIRECONTEXTW) GetProcAddress(advapi,
343 "CryptAcquireContextW");
344 gen = (CRYPTGENRANDOM) GetProcAddress(advapi,
346 release = (CRYPTRELEASECONTEXT) GetProcAddress(advapi,
347 "CryptReleaseContext");
350 if (acquire && gen && release)
354 if (acquire(&hProvider, NULL, NULL, PROV_RSA_FULL,
355 CRYPT_VERIFYCONTEXT))
357 if (
gen(hProvider,
sizeof(buf), buf) != 0)
362 printf(
"randomness from PROV_RSA_FULL\n");
365 release(hProvider, 0);
369 if (acquire(&hProvider, 0, INTEL_DEF_PROV, PROV_INTEL_SEC, 0))
371 if (
gen(hProvider,
sizeof(buf), buf) != 0)
373 RAND_add(buf,
sizeof(buf),
sizeof(buf));
376 printf(
"randomness from PROV_INTEL_SEC\n");
379 release(hProvider, 0);
386 if ((osverinfo.dwPlatformId != VER_PLATFORM_WIN32_NT ||
388 (user = LoadLibrary(TEXT(
"USER32.DLL"))))
390 GETCURSORINFO cursor;
391 GETFOREGROUNDWINDOW win;
392 GETQUEUESTATUS queue;
394 win = (GETFOREGROUNDWINDOW) GetProcAddress(user,
"GetForegroundWindow");
395 cursor = (GETCURSORINFO) GetProcAddress(user,
"GetCursorInfo");
396 queue = (GETQUEUESTATUS) GetProcAddress(user,
"GetQueueStatus");
410 if ( osverinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
411 osverinfo.dwMajorVersion < 5)
419 ci.cbSize =
sizeof(CURSORINFO);
428 w = queue(QS_ALLEVENTS);
450 CREATETOOLHELP32SNAPSHOT snap;
451 CLOSETOOLHELP32SNAPSHOT close_snap;
454 HEAP32FIRST heap_first;
455 HEAP32NEXT heap_next;
456 HEAP32LIST heaplist_first, heaplist_next;
457 PROCESS32 process_first, process_next;
458 THREAD32 thread_first, thread_next;
459 MODULE32 module_first, module_next;
468 snap = (CREATETOOLHELP32SNAPSHOT)
469 GetProcAddress(kernel,
"CreateToolhelp32Snapshot");
470 close_snap = (CLOSETOOLHELP32SNAPSHOT)
471 GetProcAddress(kernel,
"CloseToolhelp32Snapshot");
472 heap_first = (HEAP32FIRST) GetProcAddress(kernel,
"Heap32First");
473 heap_next = (HEAP32NEXT) GetProcAddress(kernel,
"Heap32Next");
474 heaplist_first = (HEAP32LIST) GetProcAddress(kernel,
"Heap32ListFirst");
475 heaplist_next = (HEAP32LIST) GetProcAddress(kernel,
"Heap32ListNext");
476 process_first = (PROCESS32) GetProcAddress(kernel,
"Process32First");
477 process_next = (PROCESS32) GetProcAddress(kernel,
"Process32Next");
478 thread_first = (THREAD32) GetProcAddress(kernel,
"Thread32First");
479 thread_next = (THREAD32) GetProcAddress(kernel,
"Thread32Next");
480 module_first = (MODULE32) GetProcAddress(kernel,
"Module32First");
481 module_next = (MODULE32) GetProcAddress(kernel,
"Module32Next");
483 if (snap && heap_first && heap_next && heaplist_first &&
484 heaplist_next && process_first && process_next &&
485 thread_first && thread_next && module_first &&
487 != INVALID_HANDLE_VALUE)
499 if (good) starttime = GetTickCount();
501 if (heaplist_first(handle, &hlist))
513 int ex_cnt_limit = 42;
521 if (heap_first(&hentry,
529 while (heap_next(&hentry)
530 && (!good || (GetTickCount()-starttime)<MAXDELAY)
534 __except (EXCEPTION_EXECUTE_HANDLER)
539 }
while (heaplist_next(handle, &hlist)
540 && (!good || (GetTickCount()-starttime)<MAXDELAY)
541 && ex_cnt_limit > 0);
545 if (heaplist_first(handle, &hlist))
551 if (heap_first(&hentry,
559 while (heap_next(&hentry)
562 }
while (heaplist_next(handle, &hlist)
563 && (!good || (GetTickCount()-starttime)<MAXDELAY));
574 if (good) starttime = GetTickCount();
575 if (process_first(handle, &p))
578 while (process_next(handle, &p) && (!good || (GetTickCount()-starttime)<MAXDELAY));
586 if (good) starttime = GetTickCount();
587 if (thread_first(handle, &t))
590 while (thread_next(handle, &t) && (!good || (GetTickCount()-starttime)<MAXDELAY));
598 if (good) starttime = GetTickCount();
599 if (module_first(handle, &m))
602 while (module_next(handle, &m)
603 && (!good || (GetTickCount()-starttime)<MAXDELAY));
620 GlobalMemoryStatus(&m);
624 w = GetCurrentProcessId();
628 printf(
"Exiting RAND_poll\n");
634 int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
636 double add_entropy=0;
650 static int lastx,lasty,lastdx,lastdy;
657 if (dx != 0 && dy != 0 && dx-lastdx != 0 && dy-lastdy != 0)
660 lastdx=dx, lastdy=dy;
666 RAND_add(&iMsg,
sizeof(iMsg), add_entropy);
667 RAND_add(&wParam,
sizeof(wParam), 0);
668 RAND_add(&lParam,
sizeof(lParam), 0);
674 void RAND_screen(
void)
682 static void readtimer(
void)
686 static int have_perfc = 1;
687 #if defined(_MSC_VER) && defined(_M_X86)
688 static int have_tsc = 1;
698 RAND_add(&cyclecount,
sizeof(cyclecount), 1);
699 } __except(EXCEPTION_EXECUTE_HANDLER) {
708 if (QueryPerformanceCounter(&l) == 0)
714 if (!have_tsc && !have_perfc) {
738 static void readscreen(
void)
740 #if !defined(OPENSSL_SYS_WINCE) && !defined(OPENSSL_SYS_WIN32_CYGWIN)
757 hScrDC = CreateDC(TEXT(
"DISPLAY"), NULL, NULL, NULL);
758 hMemDC = CreateCompatibleDC(hScrDC);
761 w = GetDeviceCaps(hScrDC, HORZRES);
762 h = GetDeviceCaps(hScrDC, VERTRES);
765 hBitmap = CreateCompatibleBitmap(hScrDC, w, n);
768 hOldBitmap = SelectObject(hMemDC, hBitmap);
771 GetObject(hBitmap,
sizeof(BITMAP), (LPSTR)&bm);
772 size = (
unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
777 for (y = 0; y < h-n; y += n)
782 BitBlt(hMemDC, 0, 0, w, n, hScrDC, 0, y, SRCCOPY);
785 GetBitmapBits(hBitmap, size, bmbits);
798 hBitmap = SelectObject(hMemDC, hOldBitmap);
801 DeleteObject(hBitmap);