[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PATCH WIP 21/30] crypto: add cryptographic random byte sou
From: |
Daniel P. Berrange |
Subject: |
[Qemu-block] [PATCH WIP 21/30] crypto: add cryptographic random byte source |
Date: |
Fri, 20 Nov 2015 18:04:21 +0000 |
Signed-off-by: Daniel P. Berrange <address@hidden>
---
crypto/Makefile.objs | 1 +
crypto/random.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++
include/crypto/random.h | 43 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 94 insertions(+)
create mode 100644 crypto/random.c
create mode 100644 include/crypto/random.h
diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs
index a3135f1..5f38d2d 100644
--- a/crypto/Makefile.objs
+++ b/crypto/Makefile.objs
@@ -8,6 +8,7 @@ crypto-obj-y += tlscredsanon.o
crypto-obj-y += tlscredsx509.o
crypto-obj-y += tlssession.o
crypto-obj-y += secret.o
+crypto-obj-y += random.o
# Let the userspace emulators avoid linking gnutls/etc
crypto-aes-obj-y = aes.o
diff --git a/crypto/random.c b/crypto/random.c
new file mode 100644
index 0000000..8257d24
--- /dev/null
+++ b/crypto/random.c
@@ -0,0 +1,50 @@
+/*
+ * QEMU Crypto random number provider
+ *
+ * Copyright (c) 2015 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 <config-host.h>
+
+#include "crypto/random.h"
+
+int qcrypto_random_bytes(uint8_t *buf,
+ size_t buflen,
+ Error **errp)
+{
+ ssize_t ret;
+ int fd = open("/dev/random", O_RDONLY);
+ if (fd < 0) {
+ error_setg_errno(errp, errno,
+ "Unable to open /dev/random");
+ return -1;
+ }
+
+ while (buflen) {
+ ret = read(fd, buf, buflen);
+ if (ret < 0) {
+ error_setg_errno(errp, errno,
+ "Unable to read random bytes");
+ close(fd);
+ return -1;
+ }
+ buflen -= ret;
+ }
+
+ close(fd);
+ return 0;
+}
diff --git a/include/crypto/random.h b/include/crypto/random.h
new file mode 100644
index 0000000..ce1626b
--- /dev/null
+++ b/include/crypto/random.h
@@ -0,0 +1,43 @@
+/*
+ * QEMU Crypto random number provider
+ *
+ * Copyright (c) 2015 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/>.
+ *
+ */
+
+#ifndef QCRYPTO_RANDOM_H__
+#define QCRYPTO_RANDOM_H__
+
+#include "qemu-common.h"
+#include "qapi/error.h"
+
+
+/**
+ * qcrypto_random_bytes:
+ * @buf: the buffer to fill
+ * @buflen: length of @buf in bytes
+ * @errp: pointer to uninitialized error objet
+ *
+ * Fill @buf with @buflen bytes of random data
+ *
+ * Returns 0 on sucess, -1 on error
+ */
+int qcrypto_random_bytes(uint8_t *buf,
+ size_t buflen,
+ Error **errp);
+
+
+#endif /* QCRYPTO_RANDOM_H__ */
--
2.5.0
- [Qemu-block] [PATCH WIP 05/30] qom: add user_creatable_add & user_creatable_del methods, (continued)
- [Qemu-block] [PATCH WIP 05/30] qom: add user_creatable_add & user_creatable_del methods, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 07/30] qemu-nbd: add support for --object command line arg, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 06/30] qemu-img: add support for --object command line arg, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 08/30] qemu-io: add support for --object command line arg, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 09/30] qemu-io: allow specifying image as a set of options args, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 16/30] crypto: add ability to query the cipher key, block & IV lens, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 15/30] qcow2: make qcow2_encrypt_sectors encrypt in place, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 17/30] crypto: add method for querying hash digest size, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 13/30] block: remove all encryption handling APIs, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 10/30] qemu-nbd: allow specifying image as a set of options args, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 21/30] crypto: add cryptographic random byte source,
Daniel P. Berrange <=
- [Qemu-block] [PATCH WIP 18/30] crypto: move QCryptoHashAlgorithm enum definition into QAPI, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 23/30] crypto: add support for generating initialization vectors, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 14/30] block: remove support for writing to qcow/qcow2 encrypted images, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 12/30] block: rip out all traces of password prompting, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 11/30] qemu-img: allow specifying image as a set of options args, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 24/30] crypto: add support for anti-forensic split algorithm, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 20/30] crypto: ensure qapi/crypto.json is listed in qapi-modules, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 28/30] block: add generic full disk encryption driver, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 22/30] crypto: add support for PBKDF2 algorithm, Daniel P. Berrange, 2015/11/20
- [Qemu-block] [PATCH WIP 19/30] crypto: move QCryptoCipherAlgorithm/Mode enum definitions into QAPI, Daniel P. Berrange, 2015/11/20