1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/* rndw32ce.c - W32CE entropy gatherer
* Copyright (C) 2010 Free Software Foundation, Inc.
*
* Libgcrypt is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Libgcrypt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <windows.h>
#include <wincrypt.h>
#include "types.h"
#include "g10lib.h"
#include "rand-internal.h"
/* The Microsoft docs say that it is suggested to see the buffer with
some extra random. We do this, despite that it is a questionable
suggestion as the OS as better means of collecting entropy than an
application. */
static size_t filler_used;
static size_t filler_length;
static unsigned char *filler_buffer;
static void
filler (const void *data, size_t datalen, enum random_origins dummy)
{
(void)dummy;
if (filler_used + datalen > filler_length)
datalen = filler_length - filler_used;
memcpy (filler_buffer + filler_used, data, datalen);
filler_used += datalen;
}
static void
fillup_buffer (unsigned char *buffer, size_t length)
{
filler_used = 0;
filler_length = length;
filler_buffer = buffer;
while (filler_used < length)
_gcry_rndw32ce_gather_random_fast (filler, 0);
}
int
_gcry_rndw32ce_gather_random (void (*add)(const void*, size_t,
enum random_origins),
enum random_origins origin,
size_t length, int level )
{
HCRYPTPROV prov;
unsigned char buffer [256];
DWORD buflen;
if (!level)
return 0;
/* Note that LENGTH is not really important because the caller
checks the returned lengths and calls this function until it
feels that enough entropy has been gathered. */
buflen = sizeof buffer;
if (length+8 < buflen)
buflen = length+8; /* Return a bit more than requested. */
if (!CryptAcquireContext (&prov, NULL, NULL, PROV_RSA_FULL,
(CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) )
log_debug ("CryptAcquireContext failed: rc=%d\n", (int)GetLastError ());
else
{
fillup_buffer (buffer, buflen);
if (!CryptGenRandom (prov, buflen, buffer))
log_debug ("CryptGenRandom(%d) failed: rc=%d\n",
(int)buflen, (int)GetLastError ());
else
(*add) (buffer, buflen, origin);
CryptReleaseContext (prov, 0);
wipememory (buffer, sizeof buffer);
}
return 0;
}
void
_gcry_rndw32ce_gather_random_fast (void (*add)(const void*, size_t,
enum random_origins),
enum random_origins origin)
{
/* Add word sized values. */
{
# define ADD(t,f) do { \
t along = (f); \
memcpy (bufptr, &along, sizeof (along)); \
bufptr += sizeof (along); \
} while (0)
unsigned char buffer[20*sizeof(ulong)], *bufptr;
bufptr = buffer;
ADD (HWND, GetActiveWindow ());
ADD (HWND, GetCapture ());
ADD (HWND, GetClipboardOwner ());
ADD (HANDLE, GetCurrentProcess ());
ADD (DWORD, GetCurrentProcessId ());
ADD (HANDLE, GetCurrentThread ());
ADD (DWORD, GetCurrentThreadId ());
ADD (HWND, GetDesktopWindow ());
ADD (HWND, GetFocus ());
ADD (DWORD, GetMessagePos ());
ADD (HWND, GetOpenClipboardWindow ());
ADD (HWND, GetProcessHeap ());
ADD (DWORD, GetQueueStatus (QS_ALLEVENTS));
ADD (DWORD, GetTickCount ());
gcry_assert ( bufptr-buffer < sizeof (buffer) );
(*add) ( buffer, bufptr-buffer, origin );
# undef ADD
}
/* Get multiword system information: Current caret position, current
mouse cursor position. */
{
POINT point;
GetCaretPos (&point);
(*add) ( &point, sizeof (point), origin );
GetCursorPos (&point);
(*add) ( &point, sizeof (point), origin );
}
/* Get percent of memory in use, bytes of physical memory, bytes of
free physical memory, bytes in paging file, free bytes in paging
file, user bytes of address space, and free user bytes. */
{
MEMORYSTATUS memoryStatus;
memoryStatus.dwLength = sizeof (MEMORYSTATUS);
GlobalMemoryStatus (&memoryStatus);
(*add) ( &memoryStatus, sizeof (memoryStatus), origin );
}
/* Get thread and process creation time, exit time, time in kernel
mode, and time in user mode in 100ns intervals. */
{
HANDLE handle;
FILETIME creationTime, exitTime, kernelTime, userTime;
handle = GetCurrentThread ();
GetThreadTimes (handle, &creationTime, &exitTime,
&kernelTime, &userTime);
(*add) ( &creationTime, sizeof (creationTime), origin );
(*add) ( &exitTime, sizeof (exitTime), origin );
(*add) ( &kernelTime, sizeof (kernelTime), origin );
(*add) ( &userTime, sizeof (userTime), origin );
handle = GetCurrentThread ();
GetThreadTimes (handle, &creationTime, &exitTime,
&kernelTime, &userTime);
(*add) ( &creationTime, sizeof (creationTime), origin );
(*add) ( &exitTime, sizeof (exitTime), origin );
(*add) ( &kernelTime, sizeof (kernelTime), origin );
(*add) ( &userTime, sizeof (userTime), origin );
}
/* In case the OEM provides a high precision timer get this. If
none is available the default implementation returns the
GetTickCount. */
{
LARGE_INTEGER performanceCount;
if (QueryPerformanceCounter (&performanceCount))
(*add) (&performanceCount, sizeof (performanceCount), origin);
}
}
|