blob: c97d5e71002a240d3a0c88cd2ff377fd3c8de1f0 [file] [log] [blame]
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001#include "Python.h"
2#ifdef MS_WINDOWS
Victor Stinner59f7fb22015-03-18 14:39:33 +01003# include <windows.h>
Martin Panterd2f87472016-07-29 04:00:44 +00004/* All sample MSDN wincrypt programs include the header below. It is at least
5 * required with Min GW. */
6# include <wincrypt.h>
Georg Brandl2daf6ae2012-02-20 19:54:16 +01007#else
Victor Stinner59f7fb22015-03-18 14:39:33 +01008# include <fcntl.h>
9# ifdef HAVE_SYS_STAT_H
10# include <sys/stat.h>
11# endif
Victor Stinnerdddf4842016-06-07 11:21:42 +020012# ifdef HAVE_LINUX_RANDOM_H
13# include <linux/random.h>
14# endif
Benjamin Peterson493ac1b2017-01-01 22:29:36 -060015# if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY))
Victor Stinnerbae2d622015-10-01 09:47:30 +020016# include <sys/random.h>
Ned Deily7ae41122016-11-12 16:35:48 -050017# endif
18# if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL)
Victor Stinner59f7fb22015-03-18 14:39:33 +010019# include <sys/syscall.h>
Victor Stinner59f7fb22015-03-18 14:39:33 +010020# endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010021#endif
22
Benjamin Peterson69e97272012-02-21 11:08:50 -050023#ifdef Py_DEBUG
24int _Py_HashSecret_Initialized = 0;
25#else
26static int _Py_HashSecret_Initialized = 0;
27#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010028
29#ifdef MS_WINDOWS
Georg Brandl2daf6ae2012-02-20 19:54:16 +010030static HCRYPTPROV hCryptProv = 0;
31
32static int
33win32_urandom_init(int raise)
34{
Georg Brandl2daf6ae2012-02-20 19:54:16 +010035 /* Acquire context */
Martin v. Löwis3f50bf62013-01-25 14:06:18 +010036 if (!CryptAcquireContext(&hCryptProv, NULL, NULL,
37 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010038 goto error;
39
40 return 0;
41
42error:
Victor Stinner4bad3b62016-08-16 15:23:58 +020043 if (raise) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +010044 PyErr_SetFromWindowsErr(0);
Victor Stinner4bad3b62016-08-16 15:23:58 +020045 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +010046 return -1;
47}
48
49/* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen
Victor Stinner4d6a3d62014-12-21 01:16:38 +010050 API. Return 0 on success, or raise an exception and return -1 on error. */
Georg Brandl2daf6ae2012-02-20 19:54:16 +010051static int
52win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
53{
54 Py_ssize_t chunk;
55
56 if (hCryptProv == 0)
57 {
Victor Stinner4bad3b62016-08-16 15:23:58 +020058 if (win32_urandom_init(raise) == -1) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +010059 return -1;
Victor Stinner4bad3b62016-08-16 15:23:58 +020060 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +010061 }
62
63 while (size > 0)
64 {
65 chunk = size > INT_MAX ? INT_MAX : size;
Victor Stinner0c083462013-11-15 23:26:25 +010066 if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010067 {
68 /* CryptGenRandom() failed */
Victor Stinner4bad3b62016-08-16 15:23:58 +020069 if (raise) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +010070 PyErr_SetFromWindowsErr(0);
Victor Stinner4bad3b62016-08-16 15:23:58 +020071 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +010072 return -1;
73 }
74 buffer += chunk;
75 size -= chunk;
76 }
77 return 0;
78}
Georg Brandl2daf6ae2012-02-20 19:54:16 +010079
Victor Stinnerff558f52017-01-07 00:07:45 +010080#else /* !MS_WINDOWS */
Victor Stinner59f7fb22015-03-18 14:39:33 +010081
Victor Stinnerbae2d622015-10-01 09:47:30 +020082#if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL)
83#define PY_GETRANDOM 1
84
Victor Stinnerff558f52017-01-07 00:07:45 +010085/* Call getrandom() to get random bytes:
86
Victor Stinner6974cf22016-08-16 18:46:38 +020087 - Return 1 on success
Victor Stinnerff558f52017-01-07 00:07:45 +010088 - Return 0 if getrandom() is not available (failed with ENOSYS or EPERM),
89 or if getrandom(GRND_NONBLOCK) failed with EAGAIN (system urandom not
90 initialized yet) and raise=0.
Victor Stinner6974cf22016-08-16 18:46:38 +020091 - Raise an exception (if raise is non-zero) and return -1 on error:
Victor Stinnerff558f52017-01-07 00:07:45 +010092 if getrandom() failed with EINTR, raise is non-zero and the Python signal
93 handler raised an exception, or if getrandom() failed with a different
94 error.
95
96 getrandom() is retried if it failed with EINTR: interrupted by a signal. */
Victor Stinner59f7fb22015-03-18 14:39:33 +010097static int
Victor Stinnere66987e2016-09-06 16:33:52 -070098py_getrandom(void *buffer, Py_ssize_t size, int blocking, int raise)
Victor Stinner59f7fb22015-03-18 14:39:33 +010099{
Victor Stinnere66987e2016-09-06 16:33:52 -0700100 /* Is getrandom() supported by the running kernel? Set to 0 if getrandom()
Victor Stinner6d8bc462016-09-20 22:46:02 +0200101 failed with ENOSYS or EPERM. Need Linux kernel 3.17 or newer, or Solaris
Victor Stinneraf597322016-09-20 22:26:18 +0200102 11.3 or newer */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100103 static int getrandom_works = 1;
Victor Stinnere66987e2016-09-06 16:33:52 -0700104 int flags;
Victor Stinnercfb19612016-06-08 10:16:50 +0200105 char *dest;
Victor Stinnerec721f32016-06-16 23:53:47 +0200106 long n;
Victor Stinner59f7fb22015-03-18 14:39:33 +0100107
Victor Stinner6974cf22016-08-16 18:46:38 +0200108 if (!getrandom_works) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100109 return 0;
Victor Stinner6974cf22016-08-16 18:46:38 +0200110 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100111
Victor Stinnere66987e2016-09-06 16:33:52 -0700112 flags = blocking ? 0 : GRND_NONBLOCK;
Victor Stinnercfb19612016-06-08 10:16:50 +0200113 dest = buffer;
Victor Stinner59f7fb22015-03-18 14:39:33 +0100114 while (0 < size) {
Victor Stinner9d242712016-04-12 22:28:49 +0200115#ifdef sun
116 /* Issue #26735: On Solaris, getrandom() is limited to returning up
Victor Stinnerff558f52017-01-07 00:07:45 +0100117 to 1024 bytes. Call it multiple times if more bytes are
118 requested. */
Victor Stinner9d242712016-04-12 22:28:49 +0200119 n = Py_MIN(size, 1024);
120#else
Victor Stinnerec721f32016-06-16 23:53:47 +0200121 n = Py_MIN(size, LONG_MAX);
Victor Stinner9d242712016-04-12 22:28:49 +0200122#endif
Victor Stinner79b74ae2015-03-30 11:16:40 +0200123
Victor Stinner9d242712016-04-12 22:28:49 +0200124 errno = 0;
Victor Stinnerbae2d622015-10-01 09:47:30 +0200125#ifdef HAVE_GETRANDOM
126 if (raise) {
127 Py_BEGIN_ALLOW_THREADS
Victor Stinnercfb19612016-06-08 10:16:50 +0200128 n = getrandom(dest, n, flags);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200129 Py_END_ALLOW_THREADS
130 }
131 else {
Victor Stinnercfb19612016-06-08 10:16:50 +0200132 n = getrandom(dest, n, flags);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200133 }
134#else
135 /* On Linux, use the syscall() function because the GNU libc doesn't
Victor Stinner6974cf22016-08-16 18:46:38 +0200136 expose the Linux getrandom() syscall yet. See:
137 https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */
Victor Stinner79b74ae2015-03-30 11:16:40 +0200138 if (raise) {
139 Py_BEGIN_ALLOW_THREADS
Victor Stinnercfb19612016-06-08 10:16:50 +0200140 n = syscall(SYS_getrandom, dest, n, flags);
Victor Stinner79b74ae2015-03-30 11:16:40 +0200141 Py_END_ALLOW_THREADS
142 }
143 else {
Victor Stinnercfb19612016-06-08 10:16:50 +0200144 n = syscall(SYS_getrandom, dest, n, flags);
Victor Stinner79b74ae2015-03-30 11:16:40 +0200145 }
Victor Stinnerbae2d622015-10-01 09:47:30 +0200146#endif
Victor Stinner79b74ae2015-03-30 11:16:40 +0200147
Victor Stinner59f7fb22015-03-18 14:39:33 +0100148 if (n < 0) {
Victor Stinnerff558f52017-01-07 00:07:45 +0100149 /* ENOSYS: the syscall is not supported by the kernel.
150 EPERM: the syscall is blocked by a security policy (ex: SECCOMP)
151 or something else. */
Victor Stinner6d8bc462016-09-20 22:46:02 +0200152 if (errno == ENOSYS || errno == EPERM) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100153 getrandom_works = 0;
154 return 0;
155 }
Victor Stinner6974cf22016-08-16 18:46:38 +0200156
Victor Stinnere66987e2016-09-06 16:33:52 -0700157 /* getrandom(GRND_NONBLOCK) fails with EAGAIN if the system urandom
Victor Stinnerff558f52017-01-07 00:07:45 +0100158 is not initialiazed yet. For _PyRandom_Init(), we ignore the
Victor Stinnere66987e2016-09-06 16:33:52 -0700159 error and fall back on reading /dev/urandom which never blocks,
Victor Stinnerff558f52017-01-07 00:07:45 +0100160 even if the system urandom is not initialized yet:
161 see the PEP 524. */
Victor Stinnere66987e2016-09-06 16:33:52 -0700162 if (errno == EAGAIN && !raise && !blocking) {
Victor Stinnerdddf4842016-06-07 11:21:42 +0200163 return 0;
164 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100165
166 if (errno == EINTR) {
Victor Stinnercecdd962016-08-16 15:19:09 +0200167 if (raise) {
168 if (PyErr_CheckSignals()) {
169 return -1;
170 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100171 }
Victor Stinnercecdd962016-08-16 15:19:09 +0200172
173 /* retry getrandom() if it was interrupted by a signal */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100174 continue;
175 }
176
Victor Stinner4bad3b62016-08-16 15:23:58 +0200177 if (raise) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100178 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner4bad3b62016-08-16 15:23:58 +0200179 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100180 return -1;
181 }
182
Victor Stinnercfb19612016-06-08 10:16:50 +0200183 dest += n;
Victor Stinner59f7fb22015-03-18 14:39:33 +0100184 size -= n;
185 }
186 return 1;
187}
Victor Stinnerff558f52017-01-07 00:07:45 +0100188
189#elif defined(HAVE_GETENTROPY)
190#define PY_GETENTROPY 1
191
192/* Fill buffer with size pseudo-random bytes generated by getentropy():
193
194 - Return 1 on success
195 - Return 0 if getentropy() syscall is not available (failed with ENOSYS or
196 EPERM).
197 - Raise an exception (if raise is non-zero) and return -1 on error:
198 if getentropy() failed with EINTR, raise is non-zero and the Python signal
199 handler raised an exception, or if getentropy() failed with a different
200 error.
201
202 getentropy() is retried if it failed with EINTR: interrupted by a signal. */
203static int
204py_getentropy(char *buffer, Py_ssize_t size, int raise)
205{
206 /* Is getentropy() supported by the running kernel? Set to 0 if
207 getentropy() failed with ENOSYS or EPERM. */
208 static int getentropy_works = 1;
209
210 if (!getentropy_works) {
211 return 0;
212 }
213
214 while (size > 0) {
215 /* getentropy() is limited to returning up to 256 bytes. Call it
216 multiple times if more bytes are requested. */
217 Py_ssize_t len = Py_MIN(size, 256);
218 int res;
219
220 if (raise) {
221 Py_BEGIN_ALLOW_THREADS
222 res = getentropy(buffer, len);
223 Py_END_ALLOW_THREADS
224 }
225 else {
226 res = getentropy(buffer, len);
227 }
228
229 if (res < 0) {
230 /* ENOSYS: the syscall is not supported by the running kernel.
231 EPERM: the syscall is blocked by a security policy (ex: SECCOMP)
232 or something else. */
233 if (errno == ENOSYS || errno == EPERM) {
234 getentropy_works = 0;
235 return 0;
236 }
237
238 if (errno == EINTR) {
239 if (raise) {
240 if (PyErr_CheckSignals()) {
241 return -1;
242 }
243 }
244
245 /* retry getentropy() if it was interrupted by a signal */
246 continue;
247 }
248
249 if (raise) {
250 PyErr_SetFromErrno(PyExc_OSError);
251 }
252 return -1;
253 }
254
255 buffer += len;
256 size -= len;
257 }
258 return 1;
259}
260#endif /* defined(HAVE_GETENTROPY) && !defined(sun) */
261
Victor Stinner59f7fb22015-03-18 14:39:33 +0100262
Antoine Pitroue472aea2014-04-26 14:33:03 +0200263static struct {
264 int fd;
265 dev_t st_dev;
266 ino_t st_ino;
267} urandom_cache = { -1 };
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100268
Victor Stinnerff558f52017-01-07 00:07:45 +0100269/* Read random bytes from the /dev/urandom device:
Victor Stinner59f7fb22015-03-18 14:39:33 +0100270
Victor Stinnerff558f52017-01-07 00:07:45 +0100271 - Return 0 on success
272 - Raise an exception (if raise is non-zero) and return -1 on error
Victor Stinner6974cf22016-08-16 18:46:38 +0200273
Victor Stinnerff558f52017-01-07 00:07:45 +0100274 Possible causes of errors:
275
276 - open() failed with ENOENT, ENXIO, ENODEV, EACCES: the /dev/urandom device
277 was not found. For example, it was removed manually or not exposed in a
278 chroot or container.
279 - open() failed with a different error
280 - fstat() failed
281 - read() failed or returned 0
282
283 read() is retried if it failed with EINTR: interrupted by a signal.
284
285 The file descriptor of the device is kept open between calls to avoid using
286 many file descriptors when run in parallel from multiple threads:
287 see the issue #18756.
288
289 st_dev and st_ino fields of the file descriptor (from fstat()) are cached to
290 check if the file descriptor was replaced by a different file (which is
291 likely a bug in the application): see the issue #21207.
292
293 If the file descriptor was closed or replaced, open a new file descriptor
294 but don't close the old file descriptor: it probably points to something
295 important for some third-party code. */
Victor Stinner4bad3b62016-08-16 15:23:58 +0200296static int
Victor Stinnerff558f52017-01-07 00:07:45 +0100297dev_urandom(char *buffer, Py_ssize_t size, int raise)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100298{
299 int fd;
300 Py_ssize_t n;
Victor Stinner6974cf22016-08-16 18:46:38 +0200301
302 if (raise) {
303 struct _Py_stat_struct st;
304
Antoine Pitroue472aea2014-04-26 14:33:03 +0200305 if (urandom_cache.fd >= 0) {
Victor Stinner6974cf22016-08-16 18:46:38 +0200306 /* Does the fd point to the same thing as before? (issue #21207) */
307 if (_Py_fstat_noraise(urandom_cache.fd, &st)
308 || st.st_dev != urandom_cache.st_dev
309 || st.st_ino != urandom_cache.st_ino) {
310 /* Something changed: forget the cached fd (but don't close it,
311 since it probably points to something important for some
312 third-party code). */
313 urandom_cache.fd = -1;
314 }
Antoine Pitrou4879a962013-08-31 00:26:02 +0200315 }
Victor Stinner6974cf22016-08-16 18:46:38 +0200316 if (urandom_cache.fd >= 0)
317 fd = urandom_cache.fd;
Antoine Pitroue472aea2014-04-26 14:33:03 +0200318 else {
Victor Stinner6974cf22016-08-16 18:46:38 +0200319 fd = _Py_open("/dev/urandom", O_RDONLY);
320 if (fd < 0) {
321 if (errno == ENOENT || errno == ENXIO ||
Victor Stinnerff558f52017-01-07 00:07:45 +0100322 errno == ENODEV || errno == EACCES) {
Victor Stinner6974cf22016-08-16 18:46:38 +0200323 PyErr_SetString(PyExc_NotImplementedError,
324 "/dev/urandom (or equivalent) not found");
Victor Stinnerff558f52017-01-07 00:07:45 +0100325 }
Victor Stinner6974cf22016-08-16 18:46:38 +0200326 /* otherwise, keep the OSError exception raised by _Py_open() */
Antoine Pitroue472aea2014-04-26 14:33:03 +0200327 return -1;
328 }
Victor Stinner6974cf22016-08-16 18:46:38 +0200329 if (urandom_cache.fd >= 0) {
330 /* urandom_fd was initialized by another thread while we were
331 not holding the GIL, keep it. */
332 close(fd);
333 fd = urandom_cache.fd;
334 }
Antoine Pitroue472aea2014-04-26 14:33:03 +0200335 else {
Victor Stinner6974cf22016-08-16 18:46:38 +0200336 if (_Py_fstat(fd, &st)) {
337 close(fd);
338 return -1;
339 }
340 else {
341 urandom_cache.fd = fd;
342 urandom_cache.st_dev = st.st_dev;
343 urandom_cache.st_ino = st.st_ino;
344 }
Antoine Pitroue472aea2014-04-26 14:33:03 +0200345 }
346 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100347
Victor Stinner6974cf22016-08-16 18:46:38 +0200348 do {
349 n = _Py_read(fd, buffer, (size_t)size);
350 if (n == -1)
351 return -1;
352 if (n == 0) {
353 PyErr_Format(PyExc_RuntimeError,
354 "Failed to read %zi bytes from /dev/urandom",
355 size);
356 return -1;
357 }
358
359 buffer += n;
360 size -= n;
361 } while (0 < size);
362 }
363 else {
364 fd = _Py_open_noraise("/dev/urandom", O_RDONLY);
365 if (fd < 0) {
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100366 return -1;
367 }
368
Victor Stinner6974cf22016-08-16 18:46:38 +0200369 while (0 < size)
370 {
371 do {
372 n = read(fd, buffer, (size_t)size);
373 } while (n < 0 && errno == EINTR);
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100374
Victor Stinner6974cf22016-08-16 18:46:38 +0200375 if (n <= 0) {
376 /* stop on error or if read(size) returned 0 */
Victor Stinner3ee933f2016-08-16 18:27:44 +0200377 close(fd);
Victor Stinner6974cf22016-08-16 18:46:38 +0200378 return -1;
379 }
380
381 buffer += n;
382 size -= n;
383 }
384 close(fd);
385 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100386 return 0;
387}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200388
389static void
390dev_urandom_close(void)
391{
Antoine Pitroue472aea2014-04-26 14:33:03 +0200392 if (urandom_cache.fd >= 0) {
393 close(urandom_cache.fd);
394 urandom_cache.fd = -1;
Antoine Pitrou4879a962013-08-31 00:26:02 +0200395 }
396}
Victor Stinnerff558f52017-01-07 00:07:45 +0100397#endif /* !MS_WINDOWS */
Antoine Pitrou4879a962013-08-31 00:26:02 +0200398
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100399
400/* Fill buffer with pseudo-random bytes generated by a linear congruent
401 generator (LCG):
402
403 x(n+1) = (x(n) * 214013 + 2531011) % 2^32
404
405 Use bits 23..16 of x(n) to generate a byte. */
406static void
407lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size)
408{
409 size_t index;
410 unsigned int x;
411
412 x = x0;
413 for (index=0; index < size; index++) {
414 x *= 214013;
415 x += 2531011;
416 /* modulo 2 ^ (8 * sizeof(int)) */
417 buffer[index] = (x >> 16) & 0xff;
418 }
419}
420
Victor Stinnerff558f52017-01-07 00:07:45 +0100421/* Read random bytes:
422
423 - Return 0 on success
424 - Raise an exception (if raise is non-zero) and return -1 on error
425
426 Used sources of entropy ordered by preference, preferred source first:
427
428 - CryptGenRandom() on Windows
429 - getrandom() function (ex: Linux and Solaris): call py_getrandom()
430 - getentropy() function (ex: OpenBSD): call py_getentropy()
431 - /dev/urandom device
432
433 Read from the /dev/urandom device if getrandom() or getentropy() function
434 is not available or does not work.
435
436 Prefer getrandom() over getentropy() because getrandom() supports blocking
437 and non-blocking mode: see the PEP 524. Python requires non-blocking RNG at
438 startup to initialize its hash secret, but os.urandom() must block until the
439 system urandom is initialized (at least on Linux 3.17 and newer).
440
441 Prefer getrandom() and getentropy() over reading directly /dev/urandom
442 because these functions don't need file descriptors and so avoid ENFILE or
443 EMFILE errors (too many open files): see the issue #18756.
444
445 Only the getrandom() function supports non-blocking mode.
446
447 Only use RNG running in the kernel. They are more secure because it is
448 harder to get the internal state of a RNG running in the kernel land than a
449 RNG running in the user land. The kernel has a direct access to the hardware
450 and has access to hardware RNG, they are used as entropy sources.
451
452 Note: the OpenSSL RAND_pseudo_bytes() function does not automatically reseed
453 its RNG on fork(), two child processes (with the same pid) generate the same
454 random numbers: see issue #18747. Kernel RNGs don't have this issue,
455 they have access to good quality entropy sources.
456
457 If raise is zero:
458
459 - Don't raise an exception on error
460 - Don't call the Python signal handler (don't call PyErr_CheckSignals()) if
461 a function fails with EINTR: retry directly the interrupted function
462 - Don't release the GIL to call functions.
463*/
Victor Stinner4bad3b62016-08-16 15:23:58 +0200464static int
Victor Stinnere66987e2016-09-06 16:33:52 -0700465pyurandom(void *buffer, Py_ssize_t size, int blocking, int raise)
Victor Stinner4bad3b62016-08-16 15:23:58 +0200466{
Victor Stinnerff558f52017-01-07 00:07:45 +0100467#if defined(PY_GETRANDOM) || defined(PY_GETENTROPY)
468 int res;
469#endif
470
Victor Stinner4bad3b62016-08-16 15:23:58 +0200471 if (size < 0) {
472 if (raise) {
473 PyErr_Format(PyExc_ValueError,
474 "negative argument not allowed");
475 }
476 return -1;
477 }
478
479 if (size == 0) {
480 return 0;
481 }
482
483#ifdef MS_WINDOWS
484 return win32_urandom((unsigned char *)buffer, size, raise);
Victor Stinner4bad3b62016-08-16 15:23:58 +0200485#else
Victor Stinnerff558f52017-01-07 00:07:45 +0100486
487#if defined(PY_GETRANDOM) || defined(PY_GETENTROPY)
488#ifdef PY_GETRANDOM
489 res = py_getrandom(buffer, size, blocking, raise);
490#else
491 res = py_getentropy(buffer, size, raise);
492#endif
493 if (res < 0) {
494 return -1;
495 }
496 if (res == 1) {
497 return 0;
498 }
499 /* getrandom() or getentropy() function is not available: failed with
500 ENOSYS or EPERM. Fall back on reading from /dev/urandom. */
501#endif
502
503 return dev_urandom(buffer, size, raise);
Victor Stinner4bad3b62016-08-16 15:23:58 +0200504#endif
505}
506
Georg Brandlc6a2c9b2013-10-06 18:43:19 +0200507/* Fill buffer with size pseudo-random bytes from the operating system random
Serhiy Storchaka56a6d852014-12-01 18:28:43 +0200508 number generator (RNG). It is suitable for most cryptographic purposes
Georg Brandlc6a2c9b2013-10-06 18:43:19 +0200509 except long living private keys for asymmetric encryption.
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100510
Victor Stinnere66987e2016-09-06 16:33:52 -0700511 On Linux 3.17 and newer, the getrandom() syscall is used in blocking mode:
512 block until the system urandom entropy pool is initialized (128 bits are
513 collected by the kernel).
514
515 Return 0 on success. Raise an exception and return -1 on error. */
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100516int
517_PyOS_URandom(void *buffer, Py_ssize_t size)
518{
Victor Stinnere66987e2016-09-06 16:33:52 -0700519 return pyurandom(buffer, size, 1, 1);
520}
521
522/* Fill buffer with size pseudo-random bytes from the operating system random
523 number generator (RNG). It is not suitable for cryptographic purpose.
524
525 On Linux 3.17 and newer (when getrandom() syscall is used), if the system
526 urandom is not initialized yet, the function returns "weak" entropy read
527 from /dev/urandom.
528
529 Return 0 on success. Raise an exception and return -1 on error. */
530int
531_PyOS_URandomNonblock(void *buffer, Py_ssize_t size)
532{
533 return pyurandom(buffer, size, 0, 1);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100534}
535
536void
537_PyRandom_Init(void)
538{
539 char *env;
Christian Heimes985ecdc2013-11-20 11:46:18 +0100540 unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500541 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200542 Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc));
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100543
Benjamin Peterson69e97272012-02-21 11:08:50 -0500544 if (_Py_HashSecret_Initialized)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100545 return;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500546 _Py_HashSecret_Initialized = 1;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100547
548 /*
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100549 Hash randomization is enabled. Generate a per-process secret,
550 using PYTHONHASHSEED if provided.
551 */
552
553 env = Py_GETENV("PYTHONHASHSEED");
Georg Brandl12897d72012-02-20 23:49:29 +0100554 if (env && *env != '\0' && strcmp(env, "random") != 0) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100555 char *endptr = env;
556 unsigned long seed;
557 seed = strtoul(env, &endptr, 10);
558 if (*endptr != '\0'
559 || seed > 4294967295UL
560 || (errno == ERANGE && seed == ULONG_MAX))
561 {
562 Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
563 "in range [0; 4294967295]");
564 }
565 if (seed == 0) {
566 /* disable the randomized hash */
567 memset(secret, 0, secret_size);
568 }
569 else {
Christian Heimes985ecdc2013-11-20 11:46:18 +0100570 lcg_urandom(seed, secret, secret_size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100571 }
572 }
573 else {
Victor Stinner4bad3b62016-08-16 15:23:58 +0200574 int res;
575
576 /* _PyRandom_Init() is called very early in the Python initialization
Victor Stinnere66987e2016-09-06 16:33:52 -0700577 and so exceptions cannot be used (use raise=0).
578
579 _PyRandom_Init() must not block Python initialization: call
580 pyurandom() is non-blocking mode (blocking=0): see the PEP 524. */
581 res = pyurandom(secret, secret_size, 0, 0);
Victor Stinner4bad3b62016-08-16 15:23:58 +0200582 if (res < 0) {
583 Py_FatalError("failed to get random numbers to initialize Python");
584 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100585 }
586}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200587
588void
589_PyRandom_Fini(void)
590{
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200591#ifdef MS_WINDOWS
592 if (hCryptProv) {
Tim Goldenb8ac3e12014-05-06 13:29:45 +0100593 CryptReleaseContext(hCryptProv, 0);
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200594 hCryptProv = 0;
595 }
596#else
Antoine Pitrou4879a962013-08-31 00:26:02 +0200597 dev_urandom_close();
598#endif
599}