Botan: src/entropy/win32_stats/es_win32.cpp Source File

Botan 1.10.17

es_win32.cpp
Go to the documentation of this file.
1 /*
2 * Win32 EntropySource
3 * (C) 1999-2009 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/es_win32.h>
9 #include <windows.h>
10 #include <tlhelp32.h>
11
12 namespace Botan {
13
14 /**
15 * Win32 poll using stats functions including Tooltip32
16 */
17 void Win32_EntropySource::poll(Entropy_Accumulator& accum)
18 {
19 /*
20 First query a bunch of basic statistical stuff, though
21 don't count it for much in terms of contributed entropy.
22 */
23 accum.add(GetTickCount(), 0);
24 accum.add(GetMessagePos(), 0);
25 accum.add(GetMessageTime(), 0);
26 accum.add(GetInputState(), 0);
27 accum.add(GetCurrentProcessId(), 0);
28 accum.add(GetCurrentThreadId(), 0);
29
30 SYSTEM_INFO sys_info;
31 GetSystemInfo(&sys_info);
32 accum.add(sys_info, 1);
33
34 MEMORYSTATUS mem_info;
35 GlobalMemoryStatus(&mem_info);
36 accum.add(mem_info, 1);
37
38 POINT point;
39 GetCursorPos(&point);
40 accum.add(point, 1);
41
42 GetCaretPos(&point);
43 accum.add(point, 1);
44
45 LARGE_INTEGER perf_counter;
46 QueryPerformanceCounter(&perf_counter);
47 accum.add(perf_counter, 0);
48
49 /*
50 Now use the Tooltip library to iterate throug various objects on
51 the system, including processes, threads, and heap objects.
52 */
53
54 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
55
56 #define TOOLHELP32_ITER(DATA_TYPE, FUNC_FIRST, FUNC_NEXT) \
57 if(!accum.polling_goal_achieved()) \
58 { \
59 DATA_TYPE info; \
60 info.dwSize = sizeof(DATA_TYPE); \
61 if(FUNC_FIRST(snapshot, &info)) \
62 { \
63 do \
64 { \
65 accum.add(info, 1); \
66 } while(FUNC_NEXT(snapshot, &info)); \
67 } \
68 }
69
70 TOOLHELP32_ITER(MODULEENTRY32, Module32First, Module32Next);
71 TOOLHELP32_ITER(PROCESSENTRY32, Process32First, Process32Next);
72 TOOLHELP32_ITER(THREADENTRY32, Thread32First, Thread32Next);
73
74 #undef TOOLHELP32_ITER
75
76 if(!accum.polling_goal_achieved())
77 {
78 size_t heap_lists_found = 0;
79 HEAPLIST32 heap_list;
80 heap_list.dwSize = sizeof(HEAPLIST32);
81
82 const size_t HEAP_LISTS_MAX = 32;
83 const size_t HEAP_OBJS_PER_LIST = 128;
84
85 if(Heap32ListFirst(snapshot, &heap_list))
86 {
87 do
88 {
89 accum.add(heap_list, 1);
90
91 if(++heap_lists_found > HEAP_LISTS_MAX)
92 break;
93
94 size_t heap_objs_found = 0;
95 HEAPENTRY32 heap_entry;
96 heap_entry.dwSize = sizeof(HEAPENTRY32);
97 if(Heap32First(&heap_entry, heap_list.th32ProcessID,
98 heap_list.th32HeapID))
99 {
100 do
101 {
102 if(heap_objs_found++ > HEAP_OBJS_PER_LIST)
103 break;
104 accum.add(heap_entry, 1);
105 } while(Heap32Next(&heap_entry));
106 }
107
108 if(accum.polling_goal_achieved())
109 break;
110
111 } while(Heap32ListNext(snapshot, &heap_list));
112 }
113 }
114
115 CloseHandle(snapshot);
116 }
117
118 }
Botan::Entropy_Accumulator::add
void add(const void *bytes, size_t length, double entropy_bits_per_byte)
Definition: entropy_src.h:70
Botan
Definition: algo_base.h:14
Botan::Entropy_Accumulator::polling_goal_achieved
bool polling_goal_achieved() const
Definition: entropy_src.h:50
TOOLHELP32_ITER
#define TOOLHELP32_ITER(DATA_TYPE, FUNC_FIRST, FUNC_NEXT)
Botan::Entropy_Accumulator
Definition: entropy_src.h:19
Botan::Win32_EntropySource::poll
void poll(Entropy_Accumulator &accum)
Definition: es_win32.cpp:17