qemu
1/*
2* QEMU Crypto random number provider
3*
4* Copyright (c) 2015-2016 Red Hat, Inc.
5*
6* This library is free software; you can redistribute it and/or
7* modify it under the terms of the GNU Lesser General Public
8* License as published by the Free Software Foundation; either
9* version 2.1 of the License, or (at your option) any later version.
10*
11* This library is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14* Lesser General Public License for more details.
15*
16* You should have received a copy of the GNU Lesser General Public
17* License along with this library; if not, see <http://www.gnu.org/licenses/>.
18*
19*/
20
21#include "qemu/osdep.h"22
23#include "crypto/random.h"24#include "qapi/error.h"25
26#ifdef _WIN3227#include <wincrypt.h>28static HCRYPTPROV hCryptProv;29#else30# ifdef CONFIG_GETRANDOM31# include <sys/random.h>32# endif33/* This is -1 for getrandom(), or a file handle for /dev/{u,}random. */
34static int fd;35#endif36
37int qcrypto_random_init(Error **errp)38{
39#ifdef _WIN3240if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,41CRYPT_SILENT | CRYPT_VERIFYCONTEXT)) {42error_setg_win32(errp, GetLastError(),43"Unable to create cryptographic provider");44return -1;45}46#else47# ifdef CONFIG_GETRANDOM48if (getrandom(NULL, 0, 0) == 0) {49/* Use getrandom() */50fd = -1;51return 0;52}53/* Fall through to /dev/urandom case. */54# endif55fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);56if (fd == -1 && errno == ENOENT) {57fd = open("/dev/random", O_RDONLY | O_CLOEXEC);58}59if (fd < 0) {60error_setg_errno(errp, errno, "No /dev/urandom or /dev/random");61return -1;62}63#endif64return 0;65}
66
67int qcrypto_random_bytes(void *buf,68size_t buflen,69Error **errp)70{
71#ifdef _WIN3272if (!CryptGenRandom(hCryptProv, buflen, buf)) {73error_setg_win32(errp, GetLastError(),74"Unable to read random bytes");75return -1;76}77#else78# ifdef CONFIG_GETRANDOM79if (likely(fd < 0)) {80while (1) {81ssize_t got = getrandom(buf, buflen, 0);82if (likely(got == buflen)) {83return 0;84}85if (got >= 0) {86buflen -= got;87buf += got;88} else if (errno != EINTR) {89error_setg_errno(errp, errno, "getrandom");90return -1;91}92}93}94/* Fall through to /dev/urandom case. */95# endif96while (1) {97ssize_t got = read(fd, buf, buflen);98if (likely(got == buflen)) {99return 0;100}101if (got > 0) {102buflen -= got;103buf += got;104} else if (got == 0) {105error_setg(errp, "Unexpected EOF reading random bytes");106return -1;107} else if (errno != EINTR) {108error_setg_errno(errp, errno, "Unable to read random bytes");109return -1;110}111}112#endif113return 0;114}
115