Skip to contentSkip to conversation

Merge qcrypto patch queue

GIT PULL Applied Daniel P. Berrange 2 participants 8 messages Jul 4, 2016 Crypto API

lore
Share & tools
lore
#0 lore raw

The following changes since commit 96b39d8327883094f52b0a3d3f25dba83d6d1d63:

Merge remote-tracking branch 'remotes/gkurz/tags/for-upstream' into staging (2016-07-01 19:29:27 +0100)

are available in the git repository at:

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

for you to fetch changes up to a1c5e949ddc3234dcb85a44b9cb9312cd9f3522f:

crypto: allow default TLS priority to be chosen at build time (2016-07-04 15:53:19 +0100)

----------------------------------------------------------------
Merge qcrypto 2016/07/04 v1

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

Daniel P. Berrange (6):
crypto: fix handling of iv generator hash defaults
crypto: rename OUT to out in xts test to avoid clash on MinGW
crypto: switch hash code to use nettle/gcrypt directly
crypto: implement sha224, sha384, sha512 and ripemd160 hashes
crypto: add support for TLS priority string override
crypto: allow default TLS priority to be chosen at build time

 configure                  |  20 ++--
 crypto/Makefile.objs       |   3 +
 crypto/block-luks.c        |  21 ++++
 crypto/hash-gcrypt.c       | 110 +++++++++++++++++++++
 crypto/hash-nettle.c       | 155 +++++++++++++++++++++++++++++
 crypto/hash-stub.c         |  41 ++++++++
 crypto/hash.c              | 109 +-------------------
 crypto/tlscreds.c          |  26 +++++
 crypto/tlssession.c        |  26 +++--
 include/crypto/tlscreds.h  |   1 +
 qapi/crypto.json           |   6 +-
 tests/Makefile.include     |   2 +-
 tests/qemu-iotests/149     |  12 +++
 tests/qemu-iotests/149.out | 240 +++++++++++++++++++++++++++++++++++++++++++++
 tests/test-crypto-hash.c   |  53 +++++++++-
 tests/test-crypto-xts.c    |  18 ++--
 16 files changed, 704 insertions(+), 139 deletions(-)
 create mode 100644 crypto/hash-gcrypt.c
 create mode 100644 crypto/hash-nettle.c
 create mode 100644 crypto/hash-stub.c
Signature · 1 line
2.7.4
#1 lore raw

[Qemu-devel] [PULL v1 2/6] crypto: rename OUT to out in xts test to avoid clash on MinGW

On MinGW one of the system headers already has "OUT" defined which causes a compile failure of the test suite. Rename the test suite var to 'out' to avoid this clash

---
 tests/test-crypto-xts.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tests/test-crypto-xts.c +9 -9
index 7f68b06..1f1412c 100644
@@ -340,7 +340,7 @@ static void test_xts_aes_decrypt(const void *ctx,
 static void test_xts(const void *opaque)
 {
     const QCryptoXTSTestData *data = opaque;
-    unsigned char OUT[512], Torg[16], T[16];
+    unsigned char out[512], Torg[16], T[16];
     uint64_t seq;
     int j;
     unsigned long len;
@@ -371,38 +371,38 @@ static void test_xts(const void *opaque)
             xts_encrypt(&aesdata, &aestweak,
                         test_xts_aes_encrypt,
                         test_xts_aes_decrypt,
-                        T, data->PTLEN, OUT, data->PTX);
+                        T, data->PTLEN, out, data->PTX);
         } else {
             xts_encrypt(&aesdata, &aestweak,
                         test_xts_aes_encrypt,
                         test_xts_aes_decrypt,
-                        T, len, OUT, data->PTX);
+                        T, len, out, data->PTX);
             xts_encrypt(&aesdata, &aestweak,
                         test_xts_aes_encrypt,
                         test_xts_aes_decrypt,
-                        T, len, &OUT[len], &data->PTX[len]);
+                        T, len, &out[len], &data->PTX[len]);
         }
 
-        g_assert(memcmp(OUT, data->CTX, data->PTLEN) == 0);
+        g_assert(memcmp(out, data->CTX, data->PTLEN) == 0);
 
         memcpy(T, Torg, sizeof(T));
         if (j == 0) {
             xts_decrypt(&aesdata, &aestweak,
                         test_xts_aes_encrypt,
                         test_xts_aes_decrypt,
-                        T, data->PTLEN, OUT, data->CTX);
+                        T, data->PTLEN, out, data->CTX);
         } else {
             xts_decrypt(&aesdata, &aestweak,
                         test_xts_aes_encrypt,
                         test_xts_aes_decrypt,
-                        T, len, OUT, data->CTX);
+                        T, len, out, data->CTX);
             xts_decrypt(&aesdata, &aestweak,
                         test_xts_aes_encrypt,
                         test_xts_aes_decrypt,
-                        T, len, &OUT[len], &data->CTX[len]);
+                        T, len, &out[len], &data->CTX[len]);
         }
 
-        g_assert(memcmp(OUT, data->PTX, data->PTLEN) == 0);
+        g_assert(memcmp(out, data->PTX, data->PTLEN) == 0);
     }
 }
 
Signature · 1 line
2.7.4
#2 lore raw

[Qemu-devel] [PULL v1 1/6] crypto: fix handling of iv generator hash defaults

In reply to Daniel P. Berrange

When opening an existing LUKS volume, if the iv generator is essiv, then the iv hash algorithm is mandatory to provide. We must report an error if it is omitted in the cipher mode spec, not silently default to hash 0 (md5). If the iv generator is not essiv, then we explicitly ignore any iv hash algorithm, rather than report an error, for compatibility with dm-crypt.

When creating a new LUKS volume, if the iv generator is essiv and no iv hsah algorithm is provided, we should default to using the sha256 hash.

---
 crypto/block-luks.c        |  21 ++++
 tests/qemu-iotests/149     |  12 +++
 tests/qemu-iotests/149.out | 240 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 273 insertions(+)
crypto/block-luks.c +21 -0
index 63649f1..fcf3b04 100644
@@ -776,6 +776,11 @@ qcrypto_block_luks_open(QCryptoBlock *block,
     }
 
     if (ivalg == QCRYPTO_IVGEN_ALG_ESSIV) {
+        if (!ivhash_name) {
+            ret = -EINVAL;
+            error_setg(errp, "Missing IV generator hash specification");
+            goto fail;
+        }
         ivcipheralg = qcrypto_block_luks_essiv_cipher(cipheralg,
                                                       ivhash,
                                                       &local_err);
@@ -785,6 +790,13 @@ qcrypto_block_luks_open(QCryptoBlock *block,
             goto fail;
         }
     } else {
+        /* Note we parsed the ivhash_name earlier in the cipher_mode
+         * spec string even with plain/plain64 ivgens, but we
+         * will ignore it, since it is irrelevant for these ivgens.
+         * This is for compat with dm-crypt which will silently
+         * ignore hash names with these ivgens rather than report
+         * an error about the invalid usage
+         */
         ivcipheralg = cipheralg;
     }
 
@@ -904,6 +916,15 @@ qcrypto_block_luks_create(QCryptoBlock *block,
     if (!luks_opts.has_hash_alg) {
         luks_opts.hash_alg = QCRYPTO_HASH_ALG_SHA256;
     }
+    if (luks_opts.ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
+        if (!luks_opts.has_ivgen_hash_alg) {
+            luks_opts.ivgen_hash_alg = QCRYPTO_HASH_ALG_SHA256;
+            luks_opts.has_ivgen_hash_alg = true;
+        }
+    }
+    /* Note we're allowing ivgen_hash_alg to be set even for
+     * non-essiv iv generators that don't need a hash. It will
+     * be silently ignored, for compatibility with dm-crypt */
 
     if (!options->u.luks.key_secret) {
         error_setg(errp, "Parameter 'key-secret' is required for cipher");
tests/qemu-iotests/149 +12 -0
index 52e23d2..8407251 100755
@@ -153,6 +153,8 @@ def cryptsetup_format(config):
     cipher = config.cipher + "-" + config.mode + "-" + config.ivgen
     if config.ivgen_hash is not None:
         cipher = cipher + ":" + config.ivgen_hash
+    elif config.ivgen == "essiv":
+        cipher = cipher + ":" + "sha256"
     args.extend(["--cipher", cipher])
     if config.mode == "xts":
         args.extend(["--key-size", str(config.keylen * 2)])
@@ -479,6 +481,16 @@ configs = [
                    "6": "slot6",
                    "7": "slot7",
                }),
+
+    # Check handling of default hash alg (sha256) with essiv
+    LUKSConfig("aes-256-cbc-essiv-auto-sha1",
+               "aes", 256, "cbc", "essiv", None, "sha1"),
+
+    # Check that a useless hash provided for 'plain64' iv gen
+    # is ignored and no error raised
+    LUKSConfig("aes-256-cbc-plain64-sha256-sha1",
+               "aes", 256, "cbc", "plain64", "sha256", "sha1"),
+
 ]
 
 blacklist = [
tests/qemu-iotests/149.out +240 -0

243 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

[Qemu-devel] [PULL v1 4/6] crypto: implement sha224, sha384, sha512 and ripemd160 hashes

In reply to Daniel P. Berrange

Wire up the nettle and gcrypt hash backends so that they can support the sha224, sha384, sha512 and ripemd160 hash algorithms.

---
 crypto/hash-gcrypt.c     |  4 ++++
 crypto/hash-nettle.c     | 29 +++++++++++++++++++++++++++++
 crypto/hash.c            |  4 ++++
 qapi/crypto.json         |  6 +++++-
 tests/test-crypto-hash.c | 29 +++++++++++++++++++++++++++++
 5 files changed, 71 insertions(+), 1 deletion(-)
crypto/hash-gcrypt.c +4 -0
index e045689..8ea5aff 100644
@@ -27,7 +27,11 @@
 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] = {
     [QCRYPTO_HASH_ALG_MD5] = GCRY_MD_MD5,
     [QCRYPTO_HASH_ALG_SHA1] = GCRY_MD_SHA1,
+    [QCRYPTO_HASH_ALG_SHA224] = GCRY_MD_SHA224,
     [QCRYPTO_HASH_ALG_SHA256] = GCRY_MD_SHA256,
+    [QCRYPTO_HASH_ALG_SHA384] = GCRY_MD_SHA384,
+    [QCRYPTO_HASH_ALG_SHA512] = GCRY_MD_SHA512,
+    [QCRYPTO_HASH_ALG_RIPEMD160] = GCRY_MD_RMD160,
 };
 
 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
crypto/hash-nettle.c +29 -0
index 8ec5572..4c6f50b 100644
@@ -23,6 +23,7 @@
 #include "crypto/hash.h"
 #include <nettle/md5.h>
 #include <nettle/sha.h>
+#include <nettle/ripemd160.h>
 
 typedef void (*qcrypto_nettle_init)(void *ctx);
 typedef void (*qcrypto_nettle_write)(void *ctx,
@@ -35,7 +36,11 @@ typedef void (*qcrypto_nettle_result)(void *ctx,
 union qcrypto_hash_ctx {
     struct md5_ctx md5;
     struct sha1_ctx sha1;
+    struct sha224_ctx sha224;
     struct sha256_ctx sha256;
+    struct sha384_ctx sha384;
+    struct sha512_ctx sha512;
+    struct ripemd160_ctx ripemd160;
 };
 
 struct qcrypto_hash_alg {
@@ -56,12 +61,36 @@ struct qcrypto_hash_alg {
         .result = (qcrypto_nettle_result)sha1_digest,
         .len = SHA1_DIGEST_SIZE,
     },
+    [QCRYPTO_HASH_ALG_SHA224] = {
+        .init = (qcrypto_nettle_init)sha224_init,
+        .write = (qcrypto_nettle_write)sha224_update,
+        .result = (qcrypto_nettle_result)sha224_digest,
+        .len = SHA224_DIGEST_SIZE,
+    },
     [QCRYPTO_HASH_ALG_SHA256] = {
         .init = (qcrypto_nettle_init)sha256_init,
         .write = (qcrypto_nettle_write)sha256_update,
         .result = (qcrypto_nettle_result)sha256_digest,
         .len = SHA256_DIGEST_SIZE,
     },
+    [QCRYPTO_HASH_ALG_SHA384] = {
+        .init = (qcrypto_nettle_init)sha384_init,
+        .write = (qcrypto_nettle_write)sha384_update,
+        .result = (qcrypto_nettle_result)sha384_digest,
+        .len = SHA384_DIGEST_SIZE,
+    },
+    [QCRYPTO_HASH_ALG_SHA512] = {
+        .init = (qcrypto_nettle_init)sha512_init,
+        .write = (qcrypto_nettle_write)sha512_update,
+        .result = (qcrypto_nettle_result)sha512_digest,
+        .len = SHA512_DIGEST_SIZE,
+    },
+    [QCRYPTO_HASH_ALG_RIPEMD160] = {
+        .init = (qcrypto_nettle_init)ripemd160_init,
+        .write = (qcrypto_nettle_write)ripemd160_update,
+        .result = (qcrypto_nettle_result)ripemd160_digest,
+        .len = RIPEMD160_DIGEST_SIZE,
+    },
 };
 
 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
crypto/hash.c +4 -0
index aa2a26e..0f1ceac 100644
@@ -25,7 +25,11 @@
 static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG__MAX] = {
     [QCRYPTO_HASH_ALG_MD5] = 16,
     [QCRYPTO_HASH_ALG_SHA1] = 20,
+    [QCRYPTO_HASH_ALG_SHA224] = 28,
     [QCRYPTO_HASH_ALG_SHA256] = 32,
+    [QCRYPTO_HASH_ALG_SHA384] = 48,
+    [QCRYPTO_HASH_ALG_SHA512] = 64,
+    [QCRYPTO_HASH_ALG_RIPEMD160] = 20,
 };
 
 size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg)
qapi/crypto.json +5 -1
index 760d0c0..4c4a3e0 100644
@@ -42,12 +42,16 @@
 #
 # @md5: MD5. Should not be used in any new code, legacy compat only
 # @sha1: SHA-1. Should not be used in any new code, legacy compat only
+# @sha224: SHA-224. (since 2.7)
 # @sha256: SHA-256. Current recommended strong hash.
+# @sha384: SHA-384. (since 2.7)
+# @sha512: SHA-512. (since 2.7)
+# @ripemd160: RIPEMD-160. (since 2.7)
 # Since: 2.6
 ##
 { 'enum': 'QCryptoHashAlgorithm',
   'prefix': 'QCRYPTO_HASH_ALG',
-  'data': ['md5', 'sha1', 'sha256']}
+  'data': ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'ripemd160']}
 
 
 ##
tests/test-crypto-hash.c +29 -0
index 8a55e74..42fc77a 100644
@@ -30,27 +30,56 @@
 
 #define OUTPUT_MD5 "628d206371563035ab8ef62f492bdec9"
 #define OUTPUT_SHA1 "b2e74f26758a3a421e509cee045244b78753cc02"
+#define OUTPUT_SHA224 "e2f7415aad33ef79f6516b0986d7175f" \
+                      "9ca3389a85bf6cfed078737b"
 #define OUTPUT_SHA256 "bc757abb0436586f392b437e5dd24096" \
                       "f7f224de6b74d4d86e2abc6121b160d0"
+#define OUTPUT_SHA384 "887ce52efb4f46700376356583b7e279" \
+                      "4f612bd024e4495087ddb946c448c69d" \
+                      "56dbf7152a94a5e63a80f3ba9f0eed78"
+#define OUTPUT_SHA512 "3a90d79638235ec6c4c11bebd84d83c0" \
+                      "549bc1e84edc4b6ec7086487641256cb" \
+                      "63b54e4cb2d2032b393994aa263c0dbb" \
+                      "e00a9f2fe9ef6037352232a1eec55ee7"
+#define OUTPUT_RIPEMD160 "f3d658fad3fdfb2b52c9369cf0d441249ddfa8a0"
 
 #define OUTPUT_MD5_B64 "Yo0gY3FWMDWrjvYvSSveyQ=="
 #define OUTPUT_SHA1_B64 "sudPJnWKOkIeUJzuBFJEt4dTzAI="
+#define OUTPUT_SHA224_B64 "4vdBWq0z73n2UWsJhtcXX5yjOJqFv2z+0Hhzew=="
 #define OUTPUT_SHA256_B64 "vHV6uwQ2WG85K0N+XdJAlvfyJN5rdNTYbiq8YSGxYNA="
+#define OUTPUT_SHA384_B64 "iHzlLvtPRnADdjVlg7fieU9hK9Ak5ElQh925RsRI" \
+                          "xp1W2/cVKpSl5jqA87qfDu14"
+#define OUTPUT_SHA512_B64 "OpDXljgjXsbEwRvr2E2DwFSbwehO3Etuxwhkh2QS" \
+                          "VstjtU5MstIDKzk5lKomPA274AqfL+nvYDc1IjKh" \
+                          "7sVe5w=="
+#define OUTPUT_RIPEMD160_B64 "89ZY+tP9+ytSyTac8NRBJJ3fqKA="
 
 static const char *expected_outputs[] = {
     [QCRYPTO_HASH_ALG_MD5] = OUTPUT_MD5,
     [QCRYPTO_HASH_ALG_SHA1] = OUTPUT_SHA1,
+    [QCRYPTO_HASH_ALG_SHA224] = OUTPUT_SHA224,
     [QCRYPTO_HASH_ALG_SHA256] = OUTPUT_SHA256,
+    [QCRYPTO_HASH_ALG_SHA384] = OUTPUT_SHA384,
+    [QCRYPTO_HASH_ALG_SHA512] = OUTPUT_SHA512,
+    [QCRYPTO_HASH_ALG_RIPEMD160] = OUTPUT_RIPEMD160,
 };
 static const char *expected_outputs_b64[] = {
     [QCRYPTO_HASH_ALG_MD5] = OUTPUT_MD5_B64,
     [QCRYPTO_HASH_ALG_SHA1] = OUTPUT_SHA1_B64,
+    [QCRYPTO_HASH_ALG_SHA224] = OUTPUT_SHA224_B64,
     [QCRYPTO_HASH_ALG_SHA256] = OUTPUT_SHA256_B64,
+    [QCRYPTO_HASH_ALG_SHA384] = OUTPUT_SHA384_B64,
+    [QCRYPTO_HASH_ALG_SHA512] = OUTPUT_SHA512_B64,
+    [QCRYPTO_HASH_ALG_RIPEMD160] = OUTPUT_RIPEMD160_B64,
 };
 static const int expected_lens[] = {
     [QCRYPTO_HASH_ALG_MD5] = 16,
     [QCRYPTO_HASH_ALG_SHA1] = 20,
+    [QCRYPTO_HASH_ALG_SHA224] = 28,
     [QCRYPTO_HASH_ALG_SHA256] = 32,
+    [QCRYPTO_HASH_ALG_SHA384] = 48,
+    [QCRYPTO_HASH_ALG_SHA512] = 64,
+    [QCRYPTO_HASH_ALG_RIPEMD160] = 20,
 };
 
 static const char hex[] = "0123456789abcdef";
Signature · 1 line
2.7.4
#4 lore raw

[Qemu-devel] [PULL v1 5/6] crypto: add support for TLS priority string override

In reply to Daniel P. Berrange

The gnutls default priority is either "NORMAL" (most historical versions of gnutls) which is a built-in label in gnutls code, or "@SYSTEM" (latest gnutls on Fedora at least) which refers to an admin customizable entry in a gnutls config file.

Regardless of which default is used by a distro, they are both global defaults applying to all applications using gnutls. If a single application on the system needs to use a weaker set of crypto priorities, this potentially forces the weakness onto all applications. Or conversely if a single application wants a strong default than all others, it can't do this via the global config file.

This adds an extra parameter to the tls credential object which allows the mgmt app / user to explicitly provide a priority string to QEMU when configuring TLS.

For example, to use the "NORMAL" priority, but disable SSL 3.0 one can now configure QEMU thus:

  $QEMU -object tls-creds-x509,id=tls0,dir=/home/berrange/qemutls,\
                priority="NORMAL:-VERS-SSL3.0" \
        ..other args...

If creating tls-creds-anon, whatever priority the user specifies will always have "+ANON-DH" appended to it, since that's mandatory to make the anonymous credentials work.

---
 crypto/tlscreds.c         | 26 ++++++++++++++++++++++++++
 crypto/tlssession.c       | 26 +++++++++++++++++++-------
 include/crypto/tlscreds.h |  1 +
 3 files changed, 46 insertions(+), 7 deletions(-)
crypto/tlscreds.c +26 -0
index 1620e12..a896553 100644
@@ -179,6 +179,27 @@ qcrypto_tls_creds_prop_get_dir(Object *obj,
 
 
 static void
+qcrypto_tls_creds_prop_set_priority(Object *obj,
+                                    const char *value,
+                                    Error **errp G_GNUC_UNUSED)
+{
+    QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
+
+    creds->priority = g_strdup(value);
+}
+
+
+static char *
+qcrypto_tls_creds_prop_get_priority(Object *obj,
+                                    Error **errp G_GNUC_UNUSED)
+{
+    QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
+
+    return g_strdup(creds->priority);
+}
+
+
+static void
 qcrypto_tls_creds_prop_set_endpoint(Object *obj,
                                     int value,
                                     Error **errp G_GNUC_UNUSED)
@@ -216,6 +237,10 @@ qcrypto_tls_creds_class_init(ObjectClass *oc, void *data)
                                    qcrypto_tls_creds_prop_get_endpoint,
                                    qcrypto_tls_creds_prop_set_endpoint,
                                    NULL);
+    object_class_property_add_str(oc, "priority",
+                                  qcrypto_tls_creds_prop_get_priority,
+                                  qcrypto_tls_creds_prop_set_priority,
+                                  NULL);
 }
 
 
@@ -234,6 +259,7 @@ qcrypto_tls_creds_finalize(Object *obj)
     QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
 
     g_free(creds->dir);
+    g_free(creds->priority);
 }
 
 
crypto/tlssession.c +19 -7
index a543e5a..2112d29 100644
@@ -132,14 +132,22 @@ qcrypto_tls_session_new(QCryptoTLSCreds *creds,
     if (object_dynamic_cast(OBJECT(creds),
                             TYPE_QCRYPTO_TLS_CREDS_ANON)) {
         QCryptoTLSCredsAnon *acreds = QCRYPTO_TLS_CREDS_ANON(creds);
+        char *prio;
 
-        ret = gnutls_priority_set_direct(session->handle,
-                                         "NORMAL:+ANON-DH", NULL);
+        if (creds->priority != NULL) {
+            prio = g_strdup_printf("%s:+ANON-DH", creds->priority);
+        } else {
+            prio = g_strdup("NORMAL:+ANON-DH");
+        }
+
+        ret = gnutls_priority_set_direct(session->handle, prio, NULL);
         if (ret < 0) {
-            error_setg(errp, "Unable to set TLS session priority: %s",
-                       gnutls_strerror(ret));
+            error_setg(errp, "Unable to set TLS session priority %s: %s",
+                       prio, gnutls_strerror(ret));
+            g_free(prio);
             goto error;
         }
+        g_free(prio);
         if (creds->endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
             ret = gnutls_credentials_set(session->handle,
                                          GNUTLS_CRD_ANON,
@@ -157,11 +165,15 @@ qcrypto_tls_session_new(QCryptoTLSCreds *creds,
     } else if (object_dynamic_cast(OBJECT(creds),
                                    TYPE_QCRYPTO_TLS_CREDS_X509)) {
         QCryptoTLSCredsX509 *tcreds = QCRYPTO_TLS_CREDS_X509(creds);
+        const char *prio = creds->priority;
+        if (!prio) {
+            prio = "NORMAL";
+        }
 
-        ret = gnutls_set_default_priority(session->handle);
+        ret = gnutls_priority_set_direct(session->handle, prio, NULL);
         if (ret < 0) {
-            error_setg(errp, "Cannot set default TLS session priority: %s",
-                       gnutls_strerror(ret));
+            error_setg(errp, "Cannot set default TLS session priority %s: %s",
+                       prio, gnutls_strerror(ret));
             goto error;
         }
         ret = gnutls_credentials_set(session->handle,
include/crypto/tlscreds.h +1 -0
index 8e2babd..59e9187 100644
@@ -54,6 +54,7 @@ struct QCryptoTLSCreds {
     gnutls_dh_params_t dh_params;
 #endif
     bool verifyPeer;
+    char *priority;
 };
 
 
Signature · 1 line
2.7.4
#5 lore raw

[Qemu-devel] [PULL v1 3/6] crypto: switch hash code to use nettle/gcrypt directly

In reply to Daniel P. Berrange

Currently the internal hash code is using the gnutls hash APIs. GNUTLS in turn is wrapping either nettle or gcrypt. Not only were the GNUTLS hash APIs not added until GNUTLS 2.9.10, but they don't expose support for all the algorithms QEMU needs to use with LUKS.

Address this by directly wrapping nettle/gcrypt in QEMU and avoiding GNUTLS's extra layer of indirection. This gives us support for hash functions on a much wider range of platforms and opens up ability to support more hash functions. It also avoids a GNUTLS bug which would not correctly handle hashing of large data blocks if int != size_t.

---
 configure                |  14 ------
 crypto/Makefile.objs     |   3 ++
 crypto/hash-gcrypt.c     | 106 +++++++++++++++++++++++++++++++++++++++
 crypto/hash-nettle.c     | 126 +++++++++++++++++++++++++++++++++++++++++++++++
 crypto/hash-stub.c       |  41 +++++++++++++++
 crypto/hash.c            | 105 ---------------------------------------
 tests/Makefile.include   |   2 +-
 tests/test-crypto-hash.c |  24 ++++++++-
 8 files changed, 299 insertions(+), 122 deletions(-)
 create mode 100644 crypto/hash-gcrypt.c
 create mode 100644 crypto/hash-nettle.c
 create mode 100644 crypto/hash-stub.c
configure +0 -14
index 93e4c95..65bd1ff 100755
@@ -306,7 +306,6 @@ gtk=""
 gtkabi=""
 gtk_gl="no"
 gnutls=""
-gnutls_hash=""
 gnutls_rnd=""
 nettle=""
 nettle_kdf="no"
@@ -2218,13 +2217,6 @@ if test "$gnutls" != "no"; then
 	QEMU_CFLAGS="$QEMU_CFLAGS $gnutls_cflags"
         gnutls="yes"
 
-	# gnutls_hash_init requires >= 2.9.10
-	if $pkg_config --exists "gnutls >= 2.9.10"; then
-            gnutls_hash="yes"
-	else
-	    gnutls_hash="no"
-	fi
-
 	# gnutls_rnd requires >= 2.11.0
 	if $pkg_config --exists "gnutls >= 2.11.0"; then
 	    gnutls_rnd="yes"
@@ -2258,11 +2250,9 @@ if test "$gnutls" != "no"; then
 	feature_not_found "gnutls" "Install gnutls devel"
     else
         gnutls="no"
-        gnutls_hash="no"
         gnutls_rnd="no"
     fi
 else
-    gnutls_hash="no"
     gnutls_rnd="no"
 fi
 
@@ -4813,7 +4803,6 @@ echo "GTK support       $gtk $(echo_version $gtk $gtk_version)"
 echo "GTK GL support    $gtk_gl"
 echo "VTE support       $vte $(echo_version $vte $vteversion)"
 echo "GNUTLS support    $gnutls"
-echo "GNUTLS hash       $gnutls_hash"
 echo "GNUTLS rnd        $gnutls_rnd"
 echo "libgcrypt         $gcrypt"
 echo "libgcrypt kdf     $gcrypt_kdf"
@@ -5179,9 +5168,6 @@ fi
 if test "$gnutls" = "yes" ; then
   echo "CONFIG_GNUTLS=y" >> $config_host_mak
 fi
-if test "$gnutls_hash" = "yes" ; then
-  echo "CONFIG_GNUTLS_HASH=y" >> $config_host_mak
-fi
 if test "$gnutls_rnd" = "yes" ; then
   echo "CONFIG_GNUTLS_RND=y" >> $config_host_mak
 fi
crypto/Makefile.objs +3 -0
index 0737f48..1f86f4f 100644
@@ -1,5 +1,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-y += aes.o
 crypto-obj-y += desrfb.o
 crypto-obj-y += cipher.o
@@ -28,3 +30,4 @@ 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/hash-gcrypt.c new file +106 -0

106 lines not on this page · raw message

crypto/hash-nettle.c new file +126 -0

126 lines not on this page · raw message

crypto/hash-stub.c new file +41 -0

41 lines not on this page · raw message

crypto/hash.c +0 -105

117 lines not on this page · raw message

tests/Makefile.include +1 -1
index 6c09962..f8e3c6b 100644
@@ -86,7 +86,7 @@ check-unit-y += tests/test-qemu-opts$(EXESUF)
 gcov-files-test-qemu-opts-y = qom/test-qemu-opts.c
 check-unit-y += tests/test-write-threshold$(EXESUF)
 gcov-files-test-write-threshold-y = block/write-threshold.c
-check-unit-$(CONFIG_GNUTLS_HASH) += tests/test-crypto-hash$(EXESUF)
+check-unit-y += tests/test-crypto-hash$(EXESUF)
 check-unit-y += tests/test-crypto-cipher$(EXESUF)
 check-unit-y += tests/test-crypto-secret$(EXESUF)
 check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF)
tests/test-crypto-hash.c +22 -2
index 6e0e89f..8a55e74 100644
@@ -68,6 +68,10 @@ static void test_hash_alloc(void)
         int ret;
         size_t j;
 
+        if (!qcrypto_hash_supports(i)) {
+            continue;
+        }
+
         ret = qcrypto_hash_bytes(i,
                                  INPUT_TEXT,
                                  strlen(INPUT_TEXT),
@@ -98,6 +102,10 @@ static void test_hash_prealloc(void)
         int ret;
         size_t j;
 
+        if (!qcrypto_hash_supports(i)) {
+            continue;
+        }
+
         resultlen = expected_lens[i];
         result = g_new0(uint8_t, resultlen);
 
@@ -137,6 +145,10 @@ static void test_hash_iov(void)
         int ret;
         size_t j;
 
+        if (!qcrypto_hash_supports(i)) {
+            continue;
+        }
+
         ret = qcrypto_hash_bytesv(i,
                                   iov, 3,
                                   &result,
@@ -165,6 +177,10 @@ static void test_hash_digest(void)
         char *digest;
         size_t digestsize;
 
+        if (!qcrypto_hash_supports(i)) {
+            continue;
+        }
+
         digestsize = qcrypto_hash_digest_len(i);
 
         g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[i]));
@@ -175,7 +191,7 @@ static void test_hash_digest(void)
                                   &digest,
                                   NULL);
         g_assert(ret == 0);
-        g_assert(g_str_equal(digest, expected_outputs[i]));
+        g_assert_cmpstr(digest, ==, expected_outputs[i]);
         g_free(digest);
     }
 }
@@ -191,13 +207,17 @@ static void test_hash_base64(void)
         int ret;
         char *digest;
 
+        if (!qcrypto_hash_supports(i)) {
+            continue;
+        }
+
         ret = qcrypto_hash_base64(i,
                                   INPUT_TEXT,
                                   strlen(INPUT_TEXT),
                                   &digest,
                                   NULL);
         g_assert(ret == 0);
-        g_assert(g_str_equal(digest, expected_outputs_b64[i]));
+        g_assert_cmpstr(digest, ==, expected_outputs_b64[i]);
         g_free(digest);
     }
 }
Signature · 1 line
2.7.4
#6 lore raw

[Qemu-devel] [PULL v1 6/6] crypto: allow default TLS priority to be chosen at build time

In reply to Daniel P. Berrange

Modern gnutls can use a global config file to control the crypto priority settings for TLS connections. For example the priority string "@SYSTEM" instructs gnutls to find the priority setting named "SYSTEM" in the global config file.

Latest gnutls GIT codebase gained the ability to reference multiple priority strings in the config file, with the first one that is found to existing winning. This means it is now possible to configure QEMU out of the box with a default priority of "@QEMU,SYSTEM", which says to look for the settings "QEMU" first, and if not found, use the "SYSTEM" settings.

To make use of this facility, we introduce the ability to set the QEMU default priority at build time via a new configure argument. It is anticipated that distro vendors will set this when building QEMU to a suitable value for use with distro crypto policy setup. eg current Fedora would run

./configure --tls-priority=@SYSTEM

while future Fedora would run

./configure --tls-priority=@QEMU,SYSTEM

---
 configure           | 6 ++++++
 crypto/tlssession.c | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)
configure +6 -0
index 65bd1ff..67beb47 100755
@@ -305,6 +305,7 @@ archipelago="no"
 gtk=""
 gtkabi=""
 gtk_gl="no"
+tls_priority="NORMAL"
 gnutls=""
 gnutls_rnd=""
 nettle=""
@@ -1096,6 +1097,8 @@ for opt do
   ;;
   --enable-gtk) gtk="yes"
   ;;
+  --tls-priority=*) tls_priority="$optarg"
+  ;;
   --disable-gnutls) gnutls="no"
   ;;
   --enable-gnutls) gnutls="yes"
@@ -1307,6 +1310,7 @@ Advanced options (experts only):
   --disable-blobs          disable installing provided firmware blobs
   --with-vss-sdk=SDK-path  enable Windows VSS support in QEMU Guest Agent
   --with-win-sdk=SDK-path  path to Windows Platform SDK (to build VSS .tlb)
+  --tls-priority           default TLS protocol/cipher priority string
 
 Optional features, enabled with --enable-FEATURE and
 disabled with --disable-FEATURE, default is enabled if available:
@@ -4802,6 +4806,7 @@ echo "SDL support       $sdl $(echo_version $sdl $sdlversion)"
 echo "GTK support       $gtk $(echo_version $gtk $gtk_version)"
 echo "GTK GL support    $gtk_gl"
 echo "VTE support       $vte $(echo_version $vte $vteversion)"
+echo "TLS priority      $tls_priority"
 echo "GNUTLS support    $gnutls"
 echo "GNUTLS rnd        $gnutls_rnd"
 echo "libgcrypt         $gcrypt"
@@ -5165,6 +5170,7 @@ if test "$gtk" = "yes" ; then
     echo "CONFIG_GTK_GL=y" >> $config_host_mak
   fi
 fi
+echo "CONFIG_TLS_PRIORITY=\"$tls_priority\"" >> $config_host_mak
 if test "$gnutls" = "yes" ; then
   echo "CONFIG_GNUTLS=y" >> $config_host_mak
 fi
crypto/tlssession.c +2 -2
index 2112d29..2de42c6 100644
@@ -137,7 +137,7 @@ qcrypto_tls_session_new(QCryptoTLSCreds *creds,
         if (creds->priority != NULL) {
             prio = g_strdup_printf("%s:+ANON-DH", creds->priority);
         } else {
-            prio = g_strdup("NORMAL:+ANON-DH");
+            prio = g_strdup(CONFIG_TLS_PRIORITY ":+ANON-DH");
         }
 
         ret = gnutls_priority_set_direct(session->handle, prio, NULL);
@@ -167,7 +167,7 @@ qcrypto_tls_session_new(QCryptoTLSCreds *creds,
         QCryptoTLSCredsX509 *tcreds = QCRYPTO_TLS_CREDS_X509(creds);
         const char *prio = creds->priority;
         if (!prio) {
-            prio = "NORMAL";
+            prio = CONFIG_TLS_PRIORITY;
         }
 
         ret = gnutls_priority_set_direct(session->handle, prio, NULL);
Signature · 1 line
2.7.4
#7 lore raw
In reply to Daniel P. Berrange
Quoted 17 lines · Daniel P. Berrange

The following changes since commit 96b39d8327883094f52b0a3d3f25dba83d6d1d63:

Merge remote-tracking branch 'remotes/gkurz/tags/for-upstream' into staging (2016-07-01 19:29:27 +0100)

are available in the git repository at:

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

for you to fetch changes up to a1c5e949ddc3234dcb85a44b9cb9312cd9f3522f:

crypto: allow default TLS priority to be chosen at build time (2016-07-04 15:53:19 +0100)

----------------------------------------------------------------
Merge qcrypto 2016/07/04 v1

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

Quoted 7 lines

Daniel P. Berrange (6):
crypto: fix handling of iv generator hash defaults
crypto: rename OUT to out in xts test to avoid clash on MinGW
crypto: switch hash code to use nettle/gcrypt directly
crypto: implement sha224, sha384, sha512 and ripemd160 hashes
crypto: add support for TLS priority string override
crypto: allow default TLS priority to be chosen at build time

Applied, thanks.

-- PMM