Provide getrandom() in the Windows OS shim using BCryptGenRandom() with the system preferred generator, the same primitive as the getentropy() shim.
BCryptGenRandom() either fills the entire buffer or fails, so the shim never returns a short count and has no 256 byte limit. The flags argument is ignored since not needed. This is needed by the coming rte_random_bytes() change. Signed-off-by: Stephen Hemminger <[email protected]> --- lib/eal/windows/include/rte_os_shim.h | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h index 4f9611d642..87819045e8 100644 --- a/lib/eal/windows/include/rte_os_shim.h +++ b/lib/eal/windows/include/rte_os_shim.h @@ -4,6 +4,7 @@ #define _RTE_OS_SHIM_ #include <errno.h> +#include <limits.h> #include <time.h> #include <rte_os.h> @@ -154,4 +155,37 @@ rte_getentropy(void *buffer, size_t length) } #define getentropy(buffer, length) rte_getentropy(buffer, length) +/* + * Windows has no getrandom(), use the same system preferred random + * generator as the getentropy() shim above. + * + * BCryptGenRandom() either fills the whole buffer or fails, so unlike + * the system call this never returns a short count. The flags argument + * is ignored, there is no Windows equivalent of GRND_NONBLOCK and the + * system preferred generator does not block. + */ +static inline ssize_t +rte_getrandom(void *buffer, size_t length, unsigned int flags) +{ + NTSTATUS status; + + RTE_SET_USED(flags); + + /* The count is a ULONG, a longer request is served short like + * the system call is allowed to do. + */ + if (length > ULONG_MAX) + length = ULONG_MAX; + + status = BCryptGenRandom(NULL, (PUCHAR)buffer, (ULONG)length, + BCRYPT_USE_SYSTEM_PREFERRED_RNG); + if (!BCRYPT_SUCCESS(status)) { + errno = EIO; + return -1; + } + + return (ssize_t)length; +} +#define getrandom(buffer, length, flags) rte_getrandom(buffer, length, flags) + #endif /* _RTE_OS_SHIM_ */ -- 2.53.0

