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