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
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()
}
return 1;
}
-#endif
+#endif /* defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL) */
+
static struct {
int fd;
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.
{
int fd;
Py_ssize_t n;
-#ifdef PY_GETRANDOM
+#if defined(PY_GETRANDOM) || defined(PY_GETENTROPY)
int res;
-#endif
-
- assert(size > 0);
-#ifdef PY_GETRANDOM
+#ifdef PY_GETENTROPY
+ res = py_getentropy(buffer, size, raise);
+#else
res = py_getrandom(buffer, size, blocking, raise);
+#endif
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
urandom_cache.fd = -1;
}
}
+#endif /* !MS_WINDOWS */
-#endif
/* Fill buffer with pseudo-random bytes generated by a linear congruent
generator (LCG):
#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
CryptReleaseContext(hCryptProv, 0);
hCryptProv = 0;
}
-#elif defined(PY_GETENTROPY)
- /* nothing to clean */
#else
dev_urandom_close();
#endif