]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/PyMod-2.7.10/Python/random.c
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 5/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / PyMod-2.7.10 / Python / random.c
1 #include "Python.h"
2 #ifdef MS_WINDOWS
3 #include <windows.h>
4 #else
5 #include <fcntl.h>
6 #endif
7
8 #ifdef Py_DEBUG
9 int _Py_HashSecret_Initialized = 0;
10 #else
11 static int _Py_HashSecret_Initialized = 0;
12 #endif
13
14 #ifdef MS_WINDOWS
15 typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
16 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
17 DWORD dwFlags );
18 typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
19 BYTE *pbBuffer );
20
21 static CRYPTGENRANDOM pCryptGenRandom = NULL;
22 /* This handle is never explicitly released. Instead, the operating
23 system will release it when the process terminates. */
24 static HCRYPTPROV hCryptProv = 0;
25
26 static int
27 win32_urandom_init(int raise)
28 {
29 HINSTANCE hAdvAPI32 = NULL;
30 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
31
32 /* Obtain handle to the DLL containing CryptoAPI. This should not fail. */
33 hAdvAPI32 = GetModuleHandle("advapi32.dll");
34 if(hAdvAPI32 == NULL)
35 goto error;
36
37 /* Obtain pointers to the CryptoAPI functions. This will fail on some early
38 versions of Win95. */
39 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
40 hAdvAPI32, "CryptAcquireContextA");
41 if (pCryptAcquireContext == NULL)
42 goto error;
43
44 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(hAdvAPI32,
45 "CryptGenRandom");
46 if (pCryptGenRandom == NULL)
47 goto error;
48
49 /* Acquire context */
50 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
51 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
52 goto error;
53
54 return 0;
55
56 error:
57 if (raise)
58 PyErr_SetFromWindowsErr(0);
59 else
60 Py_FatalError("Failed to initialize Windows random API (CryptoGen)");
61 return -1;
62 }
63
64 /* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen
65 API. Return 0 on success, or -1 on error. */
66 static int
67 win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
68 {
69 Py_ssize_t chunk;
70
71 if (hCryptProv == 0)
72 {
73 if (win32_urandom_init(raise) == -1)
74 return -1;
75 }
76
77 while (size > 0)
78 {
79 chunk = size > INT_MAX ? INT_MAX : size;
80 if (!pCryptGenRandom(hCryptProv, chunk, buffer))
81 {
82 /* CryptGenRandom() failed */
83 if (raise)
84 PyErr_SetFromWindowsErr(0);
85 else
86 Py_FatalError("Failed to initialized the randomized hash "
87 "secret using CryptoGen)");
88 return -1;
89 }
90 buffer += chunk;
91 size -= chunk;
92 }
93 return 0;
94 }
95
96 #elif HAVE_GETENTROPY
97 /* Fill buffer with size pseudo-random bytes generated by getentropy().
98 Return 0 on success, or raise an exception and return -1 on error.
99 If fatal is nonzero, call Py_FatalError() instead of raising an exception
100 on error. */
101 static int
102 py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
103 {
104 while (size > 0) {
105 Py_ssize_t len = size < 256 ? size : 256;
106 int res;
107
108 if (!fatal) {
109 Py_BEGIN_ALLOW_THREADS
110 res = getentropy(buffer, len);
111 Py_END_ALLOW_THREADS
112
113 if (res < 0) {
114 PyErr_SetFromErrno(PyExc_OSError);
115 return -1;
116 }
117 }
118 else {
119 res = getentropy(buffer, len);
120 if (res < 0)
121 Py_FatalError("getentropy() failed");
122 }
123
124 buffer += len;
125 size -= len;
126 }
127 return 0;
128 }
129 #endif
130
131 #ifdef __VMS
132 /* Use openssl random routine */
133 #include <openssl/rand.h>
134 static int
135 vms_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
136 {
137 if (RAND_pseudo_bytes(buffer, size) < 0) {
138 if (raise) {
139 PyErr_Format(PyExc_ValueError,
140 "RAND_pseudo_bytes");
141 } else {
142 Py_FatalError("Failed to initialize the randomized hash "
143 "secret using RAND_pseudo_bytes");
144 }
145 return -1;
146 }
147 return 0;
148 }
149 #endif /* __VMS */
150
151
152 #if !defined(MS_WINDOWS) && !defined(__VMS)
153
154 static struct {
155 int fd;
156 dev_t st_dev;
157 ino_t st_ino;
158 } urandom_cache = { -1 };
159
160 /* Read size bytes from /dev/urandom into buffer.
161 Call Py_FatalError() on error. */
162 static void
163 dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size)
164 {
165 int fd;
166 Py_ssize_t n;
167
168 assert (0 < size);
169
170 fd = open("/dev/urandom", O_RDONLY);
171 if (fd < 0)
172 Py_FatalError("Failed to open /dev/urandom");
173
174 while (0 < size)
175 {
176 do {
177 n = read(fd, buffer, (size_t)size);
178 } while (n < 0 && errno == EINTR);
179 if (n <= 0)
180 {
181 /* stop on error or if read(size) returned 0 */
182 Py_FatalError("Failed to read bytes from /dev/urandom");
183 break;
184 }
185 buffer += n;
186 size -= (Py_ssize_t)n;
187 }
188 close(fd);
189 }
190
191 /* Read size bytes from /dev/urandom into buffer.
192 Return 0 on success, raise an exception and return -1 on error. */
193 static int
194 dev_urandom_python(char *buffer, Py_ssize_t size)
195 {
196 int fd;
197 Py_ssize_t n;
198 struct stat st;
199 int attr;
200
201 if (size <= 0)
202 return 0;
203
204 if (urandom_cache.fd >= 0) {
205 /* Does the fd point to the same thing as before? (issue #21207) */
206 if (fstat(urandom_cache.fd, &st)
207 || st.st_dev != urandom_cache.st_dev
208 || st.st_ino != urandom_cache.st_ino) {
209 /* Something changed: forget the cached fd (but don't close it,
210 since it probably points to something important for some
211 third-party code). */
212 urandom_cache.fd = -1;
213 }
214 }
215 if (urandom_cache.fd >= 0)
216 fd = urandom_cache.fd;
217 else {
218 Py_BEGIN_ALLOW_THREADS
219 fd = open("/dev/urandom", O_RDONLY);
220 Py_END_ALLOW_THREADS
221 if (fd < 0)
222 {
223 if (errno == ENOENT || errno == ENXIO ||
224 errno == ENODEV || errno == EACCES)
225 PyErr_SetString(PyExc_NotImplementedError,
226 "/dev/urandom (or equivalent) not found");
227 else
228 PyErr_SetFromErrno(PyExc_OSError);
229 return -1;
230 }
231
232 /* try to make the file descriptor non-inheritable, ignore errors */
233 attr = fcntl(fd, F_GETFD);
234 if (attr >= 0) {
235 attr |= FD_CLOEXEC;
236 (void)fcntl(fd, F_SETFD, attr);
237 }
238
239 if (urandom_cache.fd >= 0) {
240 /* urandom_fd was initialized by another thread while we were
241 not holding the GIL, keep it. */
242 close(fd);
243 fd = urandom_cache.fd;
244 }
245 else {
246 if (fstat(fd, &st)) {
247 PyErr_SetFromErrno(PyExc_OSError);
248 close(fd);
249 return -1;
250 }
251 else {
252 urandom_cache.fd = fd;
253 urandom_cache.st_dev = st.st_dev;
254 urandom_cache.st_ino = st.st_ino;
255 }
256 }
257 }
258
259 Py_BEGIN_ALLOW_THREADS
260 do {
261 do {
262 n = read(fd, buffer, (size_t)size);
263 } while (n < 0 && errno == EINTR);
264 if (n <= 0)
265 break;
266 buffer += n;
267 size -= (Py_ssize_t)n;
268 } while (0 < size);
269 Py_END_ALLOW_THREADS
270
271 if (n <= 0)
272 {
273 /* stop on error or if read(size) returned 0 */
274 if (n < 0)
275 PyErr_SetFromErrno(PyExc_OSError);
276 else
277 PyErr_Format(PyExc_RuntimeError,
278 "Failed to read %zi bytes from /dev/urandom",
279 size);
280 return -1;
281 }
282 return 0;
283 }
284
285 static void
286 dev_urandom_close(void)
287 {
288 if (urandom_cache.fd >= 0) {
289 close(urandom_cache.fd);
290 urandom_cache.fd = -1;
291 }
292 }
293
294
295 #endif /* !defined(MS_WINDOWS) && !defined(__VMS) */
296
297 /* Fill buffer with pseudo-random bytes generated by a linear congruent
298 generator (LCG):
299
300 x(n+1) = (x(n) * 214013 + 2531011) % 2^32
301
302 Use bits 23..16 of x(n) to generate a byte. */
303 static void
304 lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size)
305 {
306 size_t index;
307 unsigned int x;
308
309 x = x0;
310 for (index=0; index < size; index++) {
311 x *= 214013;
312 x += 2531011;
313 /* modulo 2 ^ (8 * sizeof(int)) */
314 buffer[index] = (x >> 16) & 0xff;
315 }
316 }
317
318 /* Fill buffer with size pseudo-random bytes from the operating system random
319 number generator (RNG). It is suitable for most cryptographic purposes
320 except long living private keys for asymmetric encryption.
321
322 Return 0 on success, raise an exception and return -1 on error. */
323 int
324 _PyOS_URandom(void *buffer, Py_ssize_t size)
325 {
326 if (size < 0) {
327 PyErr_Format(PyExc_ValueError,
328 "negative argument not allowed");
329 return -1;
330 }
331 if (size == 0)
332 return 0;
333
334 #ifdef MS_WINDOWS
335 return win32_urandom((unsigned char *)buffer, size, 1);
336 #elif HAVE_GETENTROPY
337 return py_getentropy(buffer, size, 0);
338 #else
339 # ifdef __VMS
340 return vms_urandom((unsigned char *)buffer, size, 1);
341 # else
342 return dev_urandom_python((char*)buffer, size);
343 # endif
344 #endif
345 }
346
347 void
348 _PyRandom_Init(void)
349 {
350 char *env;
351 void *secret = &_Py_HashSecret;
352 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
353
354 if (_Py_HashSecret_Initialized)
355 return;
356 _Py_HashSecret_Initialized = 1;
357
358 /*
359 By default, hash randomization is disabled, and only
360 enabled if PYTHONHASHSEED is set to non-empty or if
361 "-R" is provided at the command line:
362 */
363 if (!Py_HashRandomizationFlag) {
364 /* Disable the randomized hash: */
365 memset(secret, 0, secret_size);
366 return;
367 }
368
369 /*
370 Hash randomization is enabled. Generate a per-process secret,
371 using PYTHONHASHSEED if provided.
372 */
373
374 env = Py_GETENV("PYTHONHASHSEED");
375 if (env && *env != '\0' && strcmp(env, "random") != 0) {
376 char *endptr = env;
377 unsigned long seed;
378 seed = strtoul(env, &endptr, 10);
379 if (*endptr != '\0'
380 || seed > 4294967295UL
381 || (errno == ERANGE && seed == ULONG_MAX))
382 {
383 Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
384 "in range [0; 4294967295]");
385 }
386 if (seed == 0) {
387 /* disable the randomized hash */
388 memset(secret, 0, secret_size);
389 }
390 else {
391 lcg_urandom(seed, (unsigned char*)secret, secret_size);
392 }
393 }
394 else {
395 #ifdef MS_WINDOWS
396 (void)win32_urandom((unsigned char *)secret, secret_size, 0);
397 #elif __VMS
398 vms_urandom((unsigned char *)secret, secret_size, 0);
399 #elif HAVE_GETENTROPY
400 (void)py_getentropy(secret, secret_size, 1);
401 #else
402 dev_urandom_noraise(secret, secret_size);
403 #endif
404 }
405 }
406
407 void
408 _PyRandom_Fini(void)
409 {
410 #ifdef MS_WINDOWS
411 if (hCryptProv) {
412 CryptReleaseContext(hCryptProv, 0);
413 hCryptProv = 0;
414 }
415 #elif HAVE_GETENTROPY
416 /* nothing to clean */
417 #else
418 dev_urandom_close();
419 #endif
420 }