diff options
| author | Peter Maydell <peter.maydell@linaro.org> | 2016-07-21 11:48:49 +0100 |
|---|---|---|
| committer | Peter Maydell <peter.maydell@linaro.org> | 2016-07-21 11:48:49 +0100 |
| commit | 7239247a2ba2fd1c269edda3b6fd816c5fd51baf (patch) | |
| tree | 49f4b26b7975d3d24c5ed6ffbe901b23c4913f49 /crypto/random-platform.c | |
| parent | 61ead113ae53a4dae63b5377ace1300cb8705682 (diff) | |
| parent | 760328971218bace4ab14b01f619825607fab9c3 (diff) | |
| download | focaccia-qemu-7239247a2ba2fd1c269edda3b6fd816c5fd51baf.tar.gz focaccia-qemu-7239247a2ba2fd1c269edda3b6fd816c5fd51baf.zip | |
Merge remote-tracking branch 'remotes/berrange/tags/pull-qcrypto-2016-07-21-1' into staging
Merge qcrypto-next 2016/07/21 v1 # gpg: Signature made Thu 21 Jul 2016 11:07:36 BST # gpg: using RSA key 0xBE86EBB415104FDF # gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>" # gpg: aka "Daniel P. Berrange <berrange@redhat.com>" # Primary key fingerprint: DAF3 A6FD B26B 6291 2D0E 8E3F BE86 EBB4 1510 4FDF * remotes/berrange/tags/pull-qcrypto-2016-07-21-1: crypto: don't open-code qcrypto_hash_supports crypto: use glib as fallback for hash algorithm crypto: use /dev/[u]random as a final fallback random source Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'crypto/random-platform.c')
| -rw-r--r-- | crypto/random-platform.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/crypto/random-platform.c b/crypto/random-platform.c new file mode 100644 index 0000000000..82b755afad --- /dev/null +++ b/crypto/random-platform.c @@ -0,0 +1,64 @@ +/* + * QEMU Crypto random number provider + * + * Copyright (c) 2015-2016 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see <http://www.gnu.org/licenses/>. + * + */ + +#include "qemu/osdep.h" + +#include "crypto/random.h" + +int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED, + size_t buflen G_GNUC_UNUSED, + Error **errp) +{ + int fd; + int ret = -1; + int got; + + /* TBD perhaps also add support for BSD getentropy / Linux + * getrandom syscalls directly */ + fd = open("/dev/urandom", O_RDONLY); + if (fd == -1 && errno == ENOENT) { + fd = open("/dev/random", O_RDONLY); + } + + if (fd < 0) { + error_setg(errp, "No /dev/urandom or /dev/random found"); + return -1; + } + + while (buflen > 0) { + got = read(fd, buf, buflen); + if (got < 0) { + error_setg_errno(errp, errno, + "Unable to read random bytes"); + goto cleanup; + } else if (!got) { + error_setg(errp, + "Unexpected EOF reading random bytes"); + goto cleanup; + } + buflen -= got; + buf += got; + } + + ret = 0; + cleanup: + close(fd); + return ret; +} |