blob: f995fc0ef179dd46fff1aa59e65f2d4074bb966a [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 */
45 fd = open("/dev/urandom", O_RDONLY);
46 if (fd == -1 && errno == ENOENT) {
47 fd = open("/dev/random", O_RDONLY);
48 }
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 }
68
69 return 0;
70#else
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020071 int ret = -1;
72 int got;
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010073
74 while (buflen > 0) {
75 got = read(fd, buf, buflen);
76 if (got < 0) {
77 error_setg_errno(errp, errno,
78 "Unable to read random bytes");
79 goto cleanup;
80 } else if (!got) {
81 error_setg(errp,
82 "Unexpected EOF reading random bytes");
83 goto cleanup;
84 }
85 buflen -= got;
86 buf += got;
87 }
88
89 ret = 0;
90 cleanup:
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010091 return ret;
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020092#endif
Daniel P. Berrangef3c83552016-07-21 10:37:14 +010093}