Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

es_win32.cpp
Go to the documentation of this file.
1 /*
2 * Win32 EntropySource
3 * (C) 1999-2009,2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/internal/es_win32.h>
9 #define NOMINMAX 1
10 #include <windows.h>
11 #include <tlhelp32.h>
12
13 namespace Botan {
14
15 /**
16 * Win32 poll using stats functions including Tooltip32
17 */
18 size_t Win32_EntropySource::poll(RandomNumberGenerator& rng)
19 {
20 /*
21 First query a bunch of basic statistical stuff
22 */
23 rng.add_entropy_T(::GetTickCount());
24 rng.add_entropy_T(::GetMessagePos());
25 rng.add_entropy_T(::GetMessageTime());
26 rng.add_entropy_T(::GetInputState());
27
28 rng.add_entropy_T(::GetCurrentProcessId());
29 rng.add_entropy_T(::GetCurrentThreadId());
30
31 SYSTEM_INFO sys_info;
32 ::GetSystemInfo(&sys_info);
33 rng.add_entropy_T(sys_info);
34
35 MEMORYSTATUSEX mem_info;
36 ::GlobalMemoryStatusEx(&mem_info);
37 rng.add_entropy_T(mem_info);
38
39 POINT point;
40 ::GetCursorPos(&point);
41 rng.add_entropy_T(point);
42
43 ::GetCaretPos(&point);
44 rng.add_entropy_T(point);
45
46 /*
47 Now use the Tooltip library to iterate through various objects on
48 the system, including processes, threads, and heap objects.
49 */
50
51 HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
52 size_t bits = 0;
53
54 #define TOOLHELP32_ITER(DATA_TYPE, FUNC_FIRST, FUNC_NEXT) \
55 if(bits < 256) \
56 { \
57 DATA_TYPE info; \
58 info.dwSize = sizeof(DATA_TYPE); \
59 if(FUNC_FIRST(snapshot, &info)) \
60 { \
61 do \
62 { \
63 rng.add_entropy_T(info); \
64 bits += 4; \
65 } while(FUNC_NEXT(snapshot, &info)); \
66 } \
67 }
68
69 TOOLHELP32_ITER(MODULEENTRY32, ::Module32First, ::Module32Next);
70 TOOLHELP32_ITER(PROCESSENTRY32, ::Process32First, ::Process32Next);
71 TOOLHELP32_ITER(THREADENTRY32, ::Thread32First, ::Thread32Next);
72
73 #undef TOOLHELP32_ITER
74
75 if(bits <= 256)
76 {
77 HEAPLIST32 heap_list;
78 heap_list.dwSize = sizeof(HEAPLIST32);
79
80 if(::Heap32ListFirst(snapshot, &heap_list))
81 {
82 do
83 {
84 rng.add_entropy_T(heap_list);
85
86 HEAPENTRY32 heap_entry;
87 heap_entry.dwSize = sizeof(HEAPENTRY32);
88 if(::Heap32First(&heap_entry,
89 heap_list.th32ProcessID,
90 heap_list.th32HeapID))
91 {
92 do
93 {
94 rng.add_entropy_T(heap_entry);
95 bits += 4;
96 } while(::Heap32Next(&heap_entry));
97 }
98
99 if(bits >= 256)
100 break;
101
102 } while(::Heap32ListNext(snapshot, &heap_list));
103 }
104 }
105
106 ::CloseHandle(snapshot);
107
108 return bits;
109 }
110
111 }
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::RandomNumberGenerator::add_entropy_T
void add_entropy_T(const T &t)
Definition: rng.h:60
Botan::Win32_EntropySource::poll
size_t poll(RandomNumberGenerator &rng) override
Definition: es_win32.cpp:18
Botan
Definition: alg_id.cpp:13
TOOLHELP32_ITER
#define TOOLHELP32_ITER(DATA_TYPE, FUNC_FIRST, FUNC_NEXT)