Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 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 |
Thomas Huth | b7cbb87 | 2019-02-13 16:54:59 +0100 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 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 Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 24 | #include "qapi/error.h" |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 25 | |
Geert Martin Ijewski | a372781 | 2017-04-26 00:15:01 +0200 | [diff] [blame] | 26 | #ifdef _WIN32 |
Gerd Hoffmann | 612fc05 | 2017-05-16 07:24:39 +0200 | [diff] [blame] | 27 | #include <wincrypt.h> |
Geert Martin Ijewski | a372781 | 2017-04-26 00:15:01 +0200 | [diff] [blame] | 28 | static HCRYPTPROV hCryptProv; |
| 29 | #else |
Richard Henderson | db1ed1a | 2019-03-13 20:57:28 -0700 | [diff] [blame] | 30 | # ifdef CONFIG_GETRANDOM |
| 31 | # include <sys/random.h> |
| 32 | # endif |
| 33 | /* This is -1 for getrandom(), or a file handle for /dev/{u,}random. */ |
| 34 | static int fd; |
Geert Martin Ijewski | a372781 | 2017-04-26 00:15:01 +0200 | [diff] [blame] | 35 | #endif |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 36 | |
Geert Martin Ijewski | a372781 | 2017-04-26 00:15:01 +0200 | [diff] [blame] | 37 | int qcrypto_random_init(Error **errp) |
| 38 | { |
Richard Henderson | 14a356f | 2019-03-13 20:38:51 -0700 | [diff] [blame] | 39 | #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 Henderson | db1ed1a | 2019-03-13 20:57:28 -0700 | [diff] [blame] | 47 | # 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 Henderson | e9979ca | 2019-03-13 21:05:54 -0700 | [diff] [blame] | 55 | fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 56 | if (fd == -1 && errno == ENOENT) { |
Richard Henderson | e9979ca | 2019-03-13 21:05:54 -0700 | [diff] [blame] | 57 | fd = open("/dev/random", O_RDONLY | O_CLOEXEC); |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 58 | } |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 59 | if (fd < 0) { |
Richard Henderson | db1ed1a | 2019-03-13 20:57:28 -0700 | [diff] [blame] | 60 | error_setg_errno(errp, errno, "No /dev/urandom or /dev/random"); |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 61 | return -1; |
| 62 | } |
Geert Martin Ijewski | a372781 | 2017-04-26 00:15:01 +0200 | [diff] [blame] | 63 | #endif |
Geert Martin Ijewski | a372781 | 2017-04-26 00:15:01 +0200 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
Richard Henderson | d049b1f | 2019-03-13 19:33:48 -0700 | [diff] [blame] | 67 | int qcrypto_random_bytes(void *buf, |
| 68 | size_t buflen, |
Geert Martin Ijewski | a372781 | 2017-04-26 00:15:01 +0200 | [diff] [blame] | 69 | Error **errp) |
| 70 | { |
Richard Henderson | 14a356f | 2019-03-13 20:38:51 -0700 | [diff] [blame] | 71 | #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 Henderson | 14a356f | 2019-03-13 20:38:51 -0700 | [diff] [blame] | 77 | #else |
Richard Henderson | db1ed1a | 2019-03-13 20:57:28 -0700 | [diff] [blame] | 78 | # 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 Henderson | 25fb26e | 2019-03-13 20:47:32 -0700 | [diff] [blame] | 96 | while (1) { |
| 97 | ssize_t got = read(fd, buf, buflen); |
| 98 | if (likely(got == buflen)) { |
| 99 | return 0; |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 100 | } |
Richard Henderson | 25fb26e | 2019-03-13 20:47:32 -0700 | [diff] [blame] | 101 | 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. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 111 | } |
Geert Martin Ijewski | a372781 | 2017-04-26 00:15:01 +0200 | [diff] [blame] | 112 | #endif |
Richard Henderson | 25fb26e | 2019-03-13 20:47:32 -0700 | [diff] [blame] | 113 | return 0; |
Daniel P. Berrange | f3c8355 | 2016-07-21 10:37:14 +0100 | [diff] [blame] | 114 | } |