[Python-checkins] cpython: Issue #29157: dev_urandom() now calls py_getentropy()
victor.stinner
python-checkins at python.org
Fri Jan 6 05:40:36 EST 2017
https://hg.python.org/cpython/rev/140f0459fe80
changeset: 106015:140f0459fe80
user: Victor Stinner <victor.stinner at gmail.com>
date: Fri Jan 06 11:16:20 2017 +0100
summary:
Issue #29157: dev_urandom() now calls py_getentropy()
Prepare the fallback to support getentropy() failure and falls back on reading
from /dev/urandom.
files:
Python/random.c | 38 ++++++++++++++++--------------------
1 files changed, 17 insertions(+), 21 deletions(-)
diff --git a/Python/random.c b/Python/random.c
--- a/Python/random.c
+++ b/Python/random.c
@@ -77,13 +77,15 @@
return 0;
}
+#else /* !MS_WINDOWS */
+
/* Issue #25003: Don't use getentropy() on Solaris (available since
* Solaris 11.3), it is blocking whereas os.urandom() should not block. */
-#elif defined(HAVE_GETENTROPY) && !defined(sun)
+#if defined(HAVE_GETENTROPY) && !defined(sun)
#define PY_GETENTROPY 1
/* Fill buffer with size pseudo-random bytes generated by getentropy().
- Return 0 on success, or raise an exception and return -1 on error.
+ Return 1 on success, or raise an exception and return -1 on error.
If raise is zero, don't raise an exception on error. */
static int
@@ -112,12 +114,10 @@
buffer += len;
size -= len;
}
- return 0;
+ return 1;
}
-#else
-
-#if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL)
+#elif defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL)
#define PY_GETRANDOM 1
/* Call getrandom()
@@ -217,7 +217,8 @@
}
return 1;
}
-#endif
+#endif /* defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL) */
+
static struct {
int fd;
@@ -225,7 +226,6 @@
ino_t st_ino;
} urandom_cache = { -1 };
-
/* Read 'size' random bytes from py_getrandom(). Fall back on reading from
/dev/urandom if getrandom() is not available.
@@ -236,22 +236,22 @@
{
int fd;
Py_ssize_t n;
-#ifdef PY_GETRANDOM
+#if defined(PY_GETRANDOM) || defined(PY_GETENTROPY)
int res;
+
+#ifdef PY_GETENTROPY
+ res = py_getentropy(buffer, size, raise);
+#else
+ res = py_getrandom(buffer, size, blocking, raise);
#endif
-
- assert(size > 0);
-
-#ifdef PY_GETRANDOM
- res = py_getrandom(buffer, size, blocking, raise);
if (res < 0) {
return -1;
}
if (res == 1) {
return 0;
}
- /* getrandom() failed with ENOSYS or EPERM,
- fall back on reading /dev/urandom */
+ /* getrandom() or getentropy() function is not available: failed with
+ ENOSYS or EPERM. Fall back on reading from /dev/urandom. */
#endif
@@ -349,8 +349,8 @@
urandom_cache.fd = -1;
}
}
+#endif /* !MS_WINDOWS */
-#endif
/* Fill buffer with pseudo-random bytes generated by a linear congruent
generator (LCG):
@@ -395,8 +395,6 @@
#ifdef MS_WINDOWS
return win32_urandom((unsigned char *)buffer, size, raise);
-#elif defined(PY_GETENTROPY)
- return py_getentropy(buffer, size, raise);
#else
return dev_urandom(buffer, size, blocking, raise);
#endif
@@ -491,8 +489,6 @@
CryptReleaseContext(hCryptProv, 0);
hCryptProv = 0;
}
-#elif defined(PY_GETENTROPY)
- /* nothing to clean */
#else
dev_urandom_close();
#endif
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list