blob: 6df40744c7085a6bf6d2e74e6bf29168a2fa469f [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
9 * version 2 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"
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
30static int fd; /* a file handle to either /dev/urandom or /dev/random */
31#endif
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010032
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020033int qcrypto_random_init(Error **errp)
34{
Richard Henderson14a356f2019-03-13 20:38:51 -070035#ifdef _WIN32
36 if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
37 CRYPT_SILENT | CRYPT_VERIFYCONTEXT)) {
38 error_setg_win32(errp, GetLastError(),
39 "Unable to create cryptographic provider");
40 return -1;
41 }
42#else
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010043 /* TBD perhaps also add support for BSD getentropy / Linux
44 * getrandom syscalls directly */
Richard Hendersone9979ca2019-03-13 21:05:54 -070045 fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010046 if (fd == -1 && errno == ENOENT) {
Richard Hendersone9979ca2019-03-13 21:05:54 -070047 fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010048 }
49
50 if (fd < 0) {
51 error_setg(errp, "No /dev/urandom or /dev/random found");
52 return -1;
53 }
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020054#endif
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020055 return 0;
56}
57
58int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED,
59 size_t buflen G_GNUC_UNUSED,
60 Error **errp)
61{
Richard Henderson14a356f2019-03-13 20:38:51 -070062#ifdef _WIN32
63 if (!CryptGenRandom(hCryptProv, buflen, buf)) {
64 error_setg_win32(errp, GetLastError(),
65 "Unable to read random bytes");
66 return -1;
67 }
Richard Henderson14a356f2019-03-13 20:38:51 -070068#else
Richard Henderson25fb26e2019-03-13 20:47:32 -070069 while (1) {
70 ssize_t got = read(fd, buf, buflen);
71 if (likely(got == buflen)) {
72 return 0;
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010073 }
Richard Henderson25fb26e2019-03-13 20:47:32 -070074 if (got > 0) {
75 buflen -= got;
76 buf += got;
77 } else if (got == 0) {
78 error_setg(errp, "Unexpected EOF reading random bytes");
79 return -1;
80 } else if (errno != EINTR) {
81 error_setg_errno(errp, errno, "Unable to read random bytes");
82 return -1;
83 }
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010084 }
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020085#endif
Richard Henderson25fb26e2019-03-13 20:47:32 -070086 return 0;
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010087}