blob: f92f96987d7d262047c7604b169a7fdf11236107 [file] [log] [blame]
Daniel P. Berrangef3c83552016-07-21 10:37:14 +01001/*
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
Thomas Huthb7cbb872019-02-13 16:54:59 +01009 * version 2.1 of the License, or (at your option) any later version.
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010010 *
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"
Markus Armbrustere688df62018-02-01 12:18:31 +010024#include "qapi/error.h"
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010025
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020026#ifdef _WIN32
Gerd Hoffmann612fc052017-05-16 07:24:39 +020027#include <wincrypt.h>
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020028static HCRYPTPROV hCryptProv;
29#else
Richard Hendersondb1ed1a2019-03-13 20:57:28 -070030# ifdef CONFIG_GETRANDOM
31# include <sys/random.h>
32# endif
33/* This is -1 for getrandom(), or a file handle for /dev/{u,}random. */
34static int fd;
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020035#endif
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010036
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020037int qcrypto_random_init(Error **errp)
38{
Richard Henderson14a356f2019-03-13 20:38:51 -070039#ifdef _WIN32
40 if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
41 CRYPT_SILENT | CRYPT_VERIFYCONTEXT)) {
42 error_setg_win32(errp, GetLastError(),
43 "Unable to create cryptographic provider");
44 return -1;
45 }
46#else
Richard Hendersondb1ed1a2019-03-13 20:57:28 -070047# ifdef CONFIG_GETRANDOM
48 if (getrandom(NULL, 0, 0) == 0) {
49 /* Use getrandom() */
50 fd = -1;
51 return 0;
52 }
53 /* Fall through to /dev/urandom case. */
54# endif
Richard Hendersone9979ca2019-03-13 21:05:54 -070055 fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010056 if (fd == -1 && errno == ENOENT) {
Richard Hendersone9979ca2019-03-13 21:05:54 -070057 fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010058 }
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010059 if (fd < 0) {
Richard Hendersondb1ed1a2019-03-13 20:57:28 -070060 error_setg_errno(errp, errno, "No /dev/urandom or /dev/random");
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010061 return -1;
62 }
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020063#endif
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020064 return 0;
65}
66
Richard Hendersond049b1f2019-03-13 19:33:48 -070067int qcrypto_random_bytes(void *buf,
68 size_t buflen,
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020069 Error **errp)
70{
Richard Henderson14a356f2019-03-13 20:38:51 -070071#ifdef _WIN32
72 if (!CryptGenRandom(hCryptProv, buflen, buf)) {
73 error_setg_win32(errp, GetLastError(),
74 "Unable to read random bytes");
75 return -1;
76 }
Richard Henderson14a356f2019-03-13 20:38:51 -070077#else
Richard Hendersondb1ed1a2019-03-13 20:57:28 -070078# ifdef CONFIG_GETRANDOM
79 if (likely(fd < 0)) {
80 while (1) {
81 ssize_t got = getrandom(buf, buflen, 0);
82 if (likely(got == buflen)) {
83 return 0;
84 }
85 if (got >= 0) {
86 buflen -= got;
87 buf += got;
88 } else if (errno != EINTR) {
89 error_setg_errno(errp, errno, "getrandom");
90 return -1;
91 }
92 }
93 }
94 /* Fall through to /dev/urandom case. */
95# endif
Richard Henderson25fb26e2019-03-13 20:47:32 -070096 while (1) {
97 ssize_t got = read(fd, buf, buflen);
98 if (likely(got == buflen)) {
99 return 0;
Daniel P. Berrangef3c83552016-07-21 10:37:14 +0100100 }
Richard Henderson25fb26e2019-03-13 20:47:32 -0700101 if (got > 0) {
102 buflen -= got;
103 buf += got;
104 } else if (got == 0) {
105 error_setg(errp, "Unexpected EOF reading random bytes");
106 return -1;
107 } else if (errno != EINTR) {
108 error_setg_errno(errp, errno, "Unable to read random bytes");
109 return -1;
110 }
Daniel P. Berrangef3c83552016-07-21 10:37:14 +0100111 }
Geert Martin Ijewskia3727812017-04-26 00:15:01 +0200112#endif
Richard Henderson25fb26e2019-03-13 20:47:32 -0700113 return 0;
Daniel P. Berrangef3c83552016-07-21 10:37:14 +0100114}