Skip to contentSkip to conversation

Merge qcrypto-next 2016/07/21

GIT PULL Applied Daniel P. Berrange 2 participants 5 messages Jul 21, 2016 Crypto API

lore
Share & tools
lore
#0 lore raw

The following changes since commit e66b05e9ca471715388182613217503160ed7a4d:

Merge remote-tracking branch 'remotes/ehabkost/tags/x86-pull-request' into staging (2016-07-20 21:32:56 +0100)

are available in the git repository at:

git://github.com/berrange/qemu tags/pull-qcrypto-2016-07-21-1

for you to fetch changes up to 760328971218bace4ab14b01f619825607fab9c3:

crypto: don't open-code qcrypto_hash_supports (2016-07-21 10:46:27 +0100)

----------------------------------------------------------------
Merge qcrypto-next 2016/07/21 v1

----------------------------------------------------------------

Daniel P. Berrange (3):
crypto: use /dev/[u]random as a final fallback random source
crypto: use glib as fallback for hash algorithm
crypto: don't open-code qcrypto_hash_supports

 crypto/Makefile.objs                        |  4 +-
 crypto/hash-gcrypt.c                        |  3 +-
 crypto/hash-glib.c                          | 97 +++++++++++++++++++++++++++++
 crypto/hash-nettle.c                        |  3 +-
 crypto/hash-stub.c                          | 41 ------------
 crypto/{random-stub.c => random-platform.c} | 37 ++++++++++-
 6 files changed, 136 insertions(+), 49 deletions(-)
 create mode 100644 crypto/hash-glib.c
 delete mode 100644 crypto/hash-stub.c
 rename crypto/{random-stub.c => random-platform.c} (52%)
Signature · 1 line
2.7.4
#1 lore raw

[Qemu-devel] [PULL v1 1/3] crypto: use /dev/[u]random as a final fallback random source

If neither gcrypt or gnutls are available to provide a cryptographic random number generator, fallback to consuming bytes directly from /dev/[u]random.

---
 crypto/Makefile.objs                        |  2 +-
 crypto/{random-stub.c => random-platform.c} | 37 +++++++++++++++++++++++++++--
 2 files changed, 36 insertions(+), 3 deletions(-)
 rename crypto/{random-stub.c => random-platform.c} (52%)
crypto/Makefile.objs +1 -1
index 1f86f4f..febffba 100644
@@ -12,6 +12,7 @@ crypto-obj-y += tlssession.o
 crypto-obj-y += secret.o
 crypto-obj-$(CONFIG_GCRYPT) += random-gcrypt.o
 crypto-obj-$(if $(CONFIG_GCRYPT),n,$(CONFIG_GNUTLS_RND)) += random-gnutls.o
+crypto-obj-$(if $(CONFIG_GCRYPT),n,$(if $(CONFIG_GNUTLS_RND),n,y)) += random-platform.o
 crypto-obj-y += pbkdf.o
 crypto-obj-$(CONFIG_NETTLE_KDF) += pbkdf-nettle.o
 crypto-obj-$(if $(CONFIG_NETTLE_KDF),n,$(CONFIG_GCRYPT_KDF)) += pbkdf-gcrypt.o
@@ -28,6 +29,5 @@ crypto-obj-y += block-luks.o
 # Let the userspace emulators avoid linking gnutls/etc
 crypto-aes-obj-y = aes.o
 
-stub-obj-y += random-stub.o
 stub-obj-y += pbkdf-stub.o
 stub-obj-y += hash-stub.o
crypto/random-platform.c renamed from crypto/random-stub.c +35 -2
similarity index 52%
rename from crypto/random-stub.c
rename to crypto/random-platform.c
index 63bbf41..82b755a 100644
@@ -26,6 +26,39 @@ int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED,
                          size_t buflen G_GNUC_UNUSED,
                          Error **errp)
 {
-    error_setg(errp, "No random byte source provided in this build");
-    return -1;
+    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;
 }
Signature · 1 line
2.7.4
#2 lore raw

[Qemu-devel] [PULL v1 2/3] crypto: use glib as fallback for hash algorithm

In reply to Daniel P. Berrange

GLib >= 2.16 provides GChecksum API which is good enough for md5, sha1, sha256 and sha512. Use this as a final fallback if neither nettle or gcrypt are available. This lets us remove the stub hash impl, and so callers can be sure those 4 algs are always available at compile time. They may still be disabled at runtime, so a check for qcrypto_hash_supports() is still best practice to report good error messages.

---
 crypto/Makefile.objs |  2 +-
 crypto/hash-glib.c   | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/hash-stub.c   | 41 ----------------------
 3 files changed, 99 insertions(+), 42 deletions(-)
 create mode 100644 crypto/hash-glib.c
 delete mode 100644 crypto/hash-stub.c
crypto/Makefile.objs +1 -1
index febffba..a36d2d9 100644
@@ -2,6 +2,7 @@ crypto-obj-y = init.o
 crypto-obj-y += hash.o
 crypto-obj-$(CONFIG_NETTLE) += hash-nettle.o
 crypto-obj-$(if $(CONFIG_NETTLE),n,$(CONFIG_GCRYPT)) += hash-gcrypt.o
+crypto-obj-$(if $(CONFIG_NETTLE),n,$(if $(CONFIG_GCRYPT),n,y)) += hash-glib.o
 crypto-obj-y += aes.o
 crypto-obj-y += desrfb.o
 crypto-obj-y += cipher.o
@@ -30,4 +31,3 @@ crypto-obj-y += block-luks.o
 crypto-aes-obj-y = aes.o
 
 stub-obj-y += pbkdf-stub.o
-stub-obj-y += hash-stub.o
crypto/hash-glib.c new file +98 -0

98 lines not on this page · raw message

crypto/hash-stub.c deleted +0 -41

41 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

[Qemu-devel] [PULL v1 3/3] crypto: don't open-code qcrypto_hash_supports

In reply to Daniel P. Berrange

Call the existing qcrypto_hash_supports method from qcrypto_hash_bytesv instead of open-coding it again.

---
 crypto/hash-gcrypt.c | 3 +--
 crypto/hash-glib.c   | 3 +--
 crypto/hash-nettle.c | 3 +--
 3 files changed, 3 insertions(+), 6 deletions(-)
crypto/hash-gcrypt.c +1 -2
index ed6f842..7690690 100644
@@ -55,8 +55,7 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
     gcry_md_hd_t md;
     unsigned char *digest;
 
-    if (alg >= G_N_ELEMENTS(qcrypto_hash_alg_map) ||
-        qcrypto_hash_alg_map[alg] == GCRY_MD_NONE) {
+    if (!qcrypto_hash_supports(alg)) {
         error_setg(errp,
                    "Unknown hash algorithm %d",
                    alg);
crypto/hash-glib.c +1 -2
index cb68a3b..ec99ac9 100644
@@ -57,8 +57,7 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
     int i, ret;
     GChecksum *cs;
 
-    if (alg >= G_N_ELEMENTS(qcrypto_hash_alg_map) ||
-        qcrypto_hash_alg_map[alg] == -1) {
+    if (!qcrypto_hash_supports(alg)) {
         error_setg(errp,
                    "Unknown hash algorithm %d",
                    alg);
crypto/hash-nettle.c +1 -2
index 4c6f50b..6a206dc 100644
@@ -113,8 +113,7 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
     int i;
     union qcrypto_hash_ctx ctx;
 
-    if (alg >= G_N_ELEMENTS(qcrypto_hash_alg_map) ||
-        qcrypto_hash_alg_map[alg].init == NULL) {
+    if (!qcrypto_hash_supports(alg)) {
         error_setg(errp,
                    "Unknown hash algorithm %d",
                    alg);
Signature · 1 line
2.7.4
#4 lore raw
In reply to Daniel P. Berrange
Quoted 8 lines · Daniel P. Berrange

The following changes since commit e66b05e9ca471715388182613217503160ed7a4d:

Merge remote-tracking branch 'remotes/ehabkost/tags/x86-pull-request' into staging (2016-07-20 21:32:56 +0100)

are available in the git repository at:

git://github.com/berrange/qemu tags/pull-qcrypto-2016-07-21-1

Quoted 8 lines

for you to fetch changes up to 760328971218bace4ab14b01f619825607fab9c3:

crypto: don't open-code qcrypto_hash_supports (2016-07-21 10:46:27 +0100)

----------------------------------------------------------------
Merge qcrypto-next 2016/07/21 v1

----------------------------------------------------------------

Applied, thanks.

-- PMM