Skip to contentSkip to conversation

crypto:add AEAD algorithms support

PATCH RESEND In review Longpeng(Mike) 2 participants 10 messages Jan 5, 2017 – Jan 7, 2017 Crypto API

lore
Share & tools
lore
#0 lore raw

cover crypto:add AEAD algorithms support

Since QEMU has been supported cryptodev, so it is necessary to support more crypto algorithms(i.e. hmac,aead) in QEMU backend.

We have already added HMAC support, and this patchset adds AEAD algos support.
---

Hi Daniel,

Would you please review this patchset? AEAD is also an important algorithm, and it will be used by the virtio-crypto in the near future.

---
Longpeng(Mike) (6):
configure: add CONFIG_GCRYPT/NETTLE_AEAD item
crypto: add AEAD algorithms framework
crypto: implement nettle-backed AEAD algorithms
crypto: implement gcrypt-backed AEAD algorithms
crypto: implement other common funcs for AEAD algorithms
crypto: add AEAD algorithms testcases

 configure                |  36 +++++
 crypto/Makefile.objs     |   3 +
 crypto/aead-gcrypt.c     | 231 ++++++++++++++++++++++++++++++
 crypto/aead-nettle.c     | 232 ++++++++++++++++++++++++++++++
 crypto/aead.c            | 124 ++++++++++++++++
 crypto/aead.h            | 180 ++++++++++++++++++++++++
 qapi/crypto.json         |   4 +-
 tests/Makefile.include   |   2 +
 tests/test-crypto-aead.c | 357 +++++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 1168 insertions(+), 1 deletion(-)
 create mode 100644 crypto/aead-gcrypt.c
 create mode 100644 crypto/aead-nettle.c
 create mode 100644 crypto/aead.c
 create mode 100644 crypto/aead.h
 create mode 100644 tests/test-crypto-aead.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH RESEND 1/6 configure: add CONFIG_GCRYPT/NETTLE_AEAD item

This item will be used for gcrypt/nettle backed AEAD algorithms.

It's hardly to decide which version of gcrypt/nettle started supporting AEAD algorithms, but it's easily for us to making a test in configure to know whether current gcrypt/nettle support AEAD.

---
 configure | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
configure +36 -0
index 218df87..14f558c 100755
@@ -309,8 +309,10 @@ tls_priority="NORMAL"
 gnutls=""
 gnutls_rnd=""
 nettle=""
+nettle_aead="no"
 nettle_kdf="no"
 gcrypt=""
+gcrypt_aead="no"
 gcrypt_hmac="no"
 gcrypt_kdf="no"
 vte=""
@@ -2429,6 +2431,19 @@ EOF
         if compile_prog "$gcrypt_cflags" "$gcrypt_libs" ; then
             gcrypt_hmac=yes
         fi
+
+        cat > $TMPC << EOF
+#include <gcrypt.h>
+int main(void) {
+  gcry_cipher_hd_t handle;
+  gcry_cipher_open(&handle, 0, 0, 0);
+  gcry_cipher_authenticate(handle, NULL, 0);
+  return 0;
+}
+EOF
+        if compile_prog "$gcrypt_cflags" "$gcrypt_libs" ; then
+            gcrypt_aead=yes
+        fi
     else
         if test "$gcrypt" = "yes"; then
             feature_not_found "gcrypt" "Install gcrypt devel"
@@ -2460,6 +2475,21 @@ EOF
         if compile_prog "$nettle_cflags" "$nettle_libs" ; then
             nettle_kdf=yes
         fi
+
+        cat > $TMPC << EOF
+#include <stddef.h>
+#include <nettle/aes.h>
+#include <nettle/ccm.h>
+#include <nettle/gcm.h>
+int main(void) {
+     ccm_aes128_encrypt(NULL, 0, NULL, NULL);
+     gcm_aes128_encrypt(NULL, 0, NULL, NULL);
+     return 0;
+}
+EOF
+        if compile_prog "$nettle_cflags" "$nettle_libs" ; then
+            nettle_aead=yes
+        fi
     else
         if test "$nettle" = "yes"; then
             feature_not_found "nettle" "Install nettle devel"
@@ -5399,6 +5429,9 @@ if test "$gnutls_rnd" = "yes" ; then
 fi
 if test "$gcrypt" = "yes" ; then
   echo "CONFIG_GCRYPT=y" >> $config_host_mak
+  if test "$gcrypt_aead" = "yes" ; then
+    echo "CONFIG_GCRYPT_AEAD=y" >> $config_host_mak
+  fi
   if test "$gcrypt_hmac" = "yes" ; then
     echo "CONFIG_GCRYPT_HMAC=y" >> $config_host_mak
   fi
@@ -5409,6 +5442,9 @@ fi
 if test "$nettle" = "yes" ; then
   echo "CONFIG_NETTLE=y" >> $config_host_mak
   echo "CONFIG_NETTLE_VERSION_MAJOR=${nettle_version%%.*}" >> $config_host_mak
+  if test "$nettle_aead" = "yes" ; then
+    echo "CONFIG_NETTLE_AEAD=y" >> $config_host_mak
+  fi
   if test "$nettle_kdf" = "yes" ; then
     echo "CONFIG_NETTLE_KDF=y" >> $config_host_mak
   fi
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH RESEND 2/6 crypto: add AEAD algorithms framework

In reply to Longpeng(Mike)

This patch introduce AEAD algorithms framework.

We currently plan to support six basic AEAD algorithms, ccm(aes128/192/256) and gcm(aes128/192/256), so we need to add ccm/gcm mode in qapi/crypto.json simultaneously.

---
 crypto/Makefile.objs |   3 +
 crypto/aead-gcrypt.c |  70 ++++++++++++++++++++
 crypto/aead-nettle.c |  72 +++++++++++++++++++++
 crypto/aead.c        |  84 ++++++++++++++++++++++++
 crypto/aead.h        | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++
 qapi/crypto.json     |   4 +-
 6 files changed, 412 insertions(+), 1 deletion(-)
 create mode 100644 crypto/aead-gcrypt.c
 create mode 100644 crypto/aead-nettle.c
 create mode 100644 crypto/aead.c
 create mode 100644 crypto/aead.h
crypto/Makefile.objs +3 -0
index 1f749f2..2c5243d 100644
@@ -3,6 +3,9 @@ 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 += aead.o
+crypto-obj-$(CONFIG_NETTLE_AEAD) += aead-nettle.o
+crypto-obj-$(if $(CONFIG_NETTLE_AEAD),n,$(CONFIG_GCRYPT_AEAD)) += aead-gcrypt.o
 crypto-obj-y += hmac.o
 crypto-obj-$(CONFIG_NETTLE) += hmac-nettle.o
 crypto-obj-$(CONFIG_GCRYPT_HMAC) += hmac-gcrypt.o
crypto/aead-gcrypt.c new file +70 -0

70 lines not on this page · raw message

crypto/aead-nettle.c new file +72 -0

72 lines not on this page · raw message

crypto/aead.c new file +84 -0

84 lines not on this page · raw message

crypto/aead.h new file +180 -0

180 lines not on this page · raw message

qapi/crypto.json +3 -1
index f4fd93b..edb2962 100644
@@ -87,6 +87,8 @@
 #
 # The supported modes for content encryption ciphers
 #
+# @ccm: Counter with CBC-MAC Mode (Since 2.9)
+# @gcm: Galois Counter Mode (Since 2.9)
 # @ecb: Electronic Code Book
 # @cbc: Cipher Block Chaining
 # @xts: XEX with tweaked code book and ciphertext stealing
@@ -95,7 +97,7 @@
 ##
 { 'enum': 'QCryptoCipherMode',
   'prefix': 'QCRYPTO_CIPHER_MODE',
-  'data': ['ecb', 'cbc', 'xts', 'ctr']}
+  'data': ['ccm', 'gcm', 'ecb', 'cbc', 'xts', 'ctr']}
 
 
 ##
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [Qemu-devel] [PATCH RESEND 2/6] crypto: add AEAD algorithms framework

Quoted 1 line · Longpeng(Mike)

This patch introduce AEAD algorithms framework.

So AEAD is essentially just encryption with extra metadata on input and output stages. As such I don't think this should be implemented via new objects in QEMU.

Instead, we should add the extra APIs to the existing cipher.{c,h} file.

You've only implemented AES support here, so the code duplication is not obvious, but if we were to extend AEAD to other algorithms we'd see alot of duplication with the way you've done things.

So....

Quoted 5 lines
crypto/aead.h new file +5 -0
+int qcrypto_aead_set_nonce(QCryptoAead *aead,
+                           const uint8_t *nonce, size_t nonce_len,
+                           size_t aad_len, size_t in_len,
+                           size_t tag_len,
+                           Error **errp);

crypto/aead.h line 179

Quoted 10 lines
crypto/aead.h new file +8 -0
 
+int qcrypto_aead_authenticate(QCryptoAead *aead,
+                              const uint8_t *aad, size_t aad_len,
+                              Error **errp);
+
 
+int qcrypto_aead_get_tag(QCryptoAead *aead,
+                         uint8_t *tag, size_t tag_len,
+                         Error **errp);
+

Just add these three methods to cipher.h instead

Quoted 14 lines
qapi/crypto.json +2 -0
index f4fd93b..edb2962 100644
@@ -87,6 +87,8 @@
 #
 # The supported modes for content encryption ciphers
 #
+# @ccm: Counter with CBC-MAC Mode (Since 2.9)
+# @gcm: Galois Counter Mode (Since 2.9)
 # @ecb: Electronic Code Book
 # @cbc: Cipher Block Chaining
 # @xts: XEX with tweaked code book and ciphertext stealing

qapi/crypto.json line 100

Quoted 5 lines
qapi/crypto.json +1 -1
@@ -95,7 +97,7 @@
 ##
 { 'enum': 'QCryptoCipherMode',
   'prefix': 'QCRYPTO_CIPHER_MODE',
-  'data': ['ecb', 'cbc', 'xts', 'ctr']}
+  'data': ['ccm', 'gcm', 'ecb', 'cbc', 'xts', 'ctr']}

This is fine.

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#4 lore raw

Re: [Qemu-devel] [PATCH RESEND 2/6] crypto: add AEAD algorithms framework

Hi Daniel,

Quoted 7 lines · Daniel P. Berrange
Quoted 1 line · Longpeng(Mike)

This patch introduce AEAD algorithms framework.

So AEAD is essentially just encryption with extra metadata on input and output stages. As such I don't think this should be implemented via new objects in QEMU.

Quoted 9 lines

Instead, we should add the extra APIs to the existing cipher.{c,h} file.

You've only implemented AES support here, so the code duplication is not obvious, but if we were to extend AEAD to other algorithms we'd see alot of duplication with the way you've done things.

So....

All right, I will rework this recently. :)

Quoted 9 lines

qapi/crypto.json line 100

Quoted 4 lines

......

qapi/crypto.json +1 -1
-  'data': ['ecb', 'cbc', 'xts', 'ctr']}
+  'data': ['ccm', 'gcm', 'ecb', 'cbc', 'xts', 'ctr']}

This is fine.

Regards,
Daniel

Signature · 2 lines
Regards,
Longpeng(Mike)
#5 lore raw

PATCH RESEND 3/6 crypto: implement nettle-backed AEAD algorithms

In reply to Longpeng(Mike)

This patch add nettle-backed AEAD algorithms

---
 crypto/aead-nettle.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 167 insertions(+), 7 deletions(-)
crypto/aead-nettle.c +167 -7
index cfb9d33..7a2296d 100644
@@ -19,17 +19,124 @@
 #include <nettle/ccm.h>
 #include <nettle/gcm.h>
 
+typedef void (*qcrypto_nettle_aead_setkey)(void *ctx,
+              const uint8_t *key);
+
+typedef void (*qcrypto_nettle_aead_setiv)(void *ctx,
+              size_t length, const uint8_t *iv);
+
+typedef void (*qcrypto_nettle_aead_setnonce)(void *ctx,
+              size_t length, const uint8_t *nonce,
+              size_t authlen, size_t msglen, size_t taglen);
+
+typedef void (*qcrypto_nettle_aead_update)(void *ctx,
+              size_t length, const uint8_t *data);
+
+typedef void (*qcrypto_nettle_aead_encrypt)(void *ctx,
+              size_t length, uint8_t *dst, const uint8_t *src);
+
+typedef void (*qcrypto_nettle_aead_decrypt)(void *ctx,
+              size_t length, uint8_t *dst, const uint8_t *src);
+
+typedef void (*qcrypto_nettle_aead_digest)(void *ctx,
+              size_t length, uint8_t *digest);
+
+#define NETTLE_AEAD_SET_CCM_FN(alg) { \
+    .setkey = (qcrypto_nettle_aead_setkey)ccm_##alg##_set_key, \
+    .u_op.setnonce = (qcrypto_nettle_aead_setnonce)ccm_##alg##_set_nonce, \
+    .update = (qcrypto_nettle_aead_update)ccm_##alg##_update, \
+    .encrypt = (qcrypto_nettle_aead_encrypt)ccm_##alg##_encrypt, \
+    .decrypt = (qcrypto_nettle_aead_decrypt)ccm_##alg##_decrypt, \
+    .digest = (qcrypto_nettle_aead_digest)ccm_##alg##_digest, \
+}
+
+#define NETTLE_AEAD_SET_GCM_FN(alg) { \
+    .setkey = (qcrypto_nettle_aead_setkey)gcm_##alg##_set_key, \
+    .u_op.setiv = (qcrypto_nettle_aead_setiv)gcm_##alg##_set_iv, \
+    .update = (qcrypto_nettle_aead_update)gcm_##alg##_update, \
+    .encrypt = (qcrypto_nettle_aead_encrypt)gcm_##alg##_encrypt, \
+    .decrypt = (qcrypto_nettle_aead_decrypt)gcm_##alg##_decrypt, \
+    .digest = (qcrypto_nettle_aead_digest)gcm_##alg##_digest, \
+}
+
+static struct qcrypto_nettle_aead_alg {
+    qcrypto_nettle_aead_setkey setkey;
+    union {
+        qcrypto_nettle_aead_setnonce setnonce;
+        qcrypto_nettle_aead_setiv setiv;
+    } u_op;
+    qcrypto_nettle_aead_update update;
+    qcrypto_nettle_aead_encrypt encrypt;
+    qcrypto_nettle_aead_decrypt decrypt;
+    qcrypto_nettle_aead_digest digest;
+} qcrypto_aead_alg_map[][QCRYPTO_AEAD_ALG__MAX] = {
+    {
+        [QCRYPTO_CIPHER_ALG_AES_128] = NETTLE_AEAD_SET_CCM_FN(aes128),
+        [QCRYPTO_CIPHER_ALG_AES_192] = NETTLE_AEAD_SET_CCM_FN(aes192),
+        [QCRYPTO_CIPHER_ALG_AES_256] = NETTLE_AEAD_SET_CCM_FN(aes256),
+    },
+    {
+        [QCRYPTO_CIPHER_ALG_AES_128] = NETTLE_AEAD_SET_GCM_FN(aes128),
+        [QCRYPTO_CIPHER_ALG_AES_192] = NETTLE_AEAD_SET_GCM_FN(aes192),
+        [QCRYPTO_CIPHER_ALG_AES_256] = NETTLE_AEAD_SET_GCM_FN(aes256),
+    }
+};
+
+typedef struct QCryptoAeadNettle QCryptoAeadNettle;
+struct QCryptoAeadNettle {
+    union qcrypto_nettle_aead_ctx {
+        struct ccm_aes128_ctx c_aes128_ctx;
+        struct ccm_aes192_ctx c_aes192_ctx;
+        struct ccm_aes256_ctx c_aes256_ctx;
+        struct gcm_aes128_ctx g_aes128_ctx;
+        struct gcm_aes192_ctx g_aes192_ctx;
+        struct gcm_aes256_ctx g_aes256_ctx;
+    } u;
+};
+
 QCryptoAead *qcrypto_aead_new(QCryptoCipherAlgorithm alg,
                               QCryptoCipherMode mode,
                               const uint8_t *key, size_t nkey,
                               Error **errp)
 {
-    return NULL;
+    QCryptoAead *aead;
+    QCryptoAeadNettle *ctx;
+
+    if (!qcrypto_aead_supports(alg, mode)) {
+        return NULL;
+    }
+
+    if (nkey != qcrypto_aead_get_key_len(alg)) {
+        error_setg(errp, "Cipher key length %zu is invalid",
+                   nkey);
+        return NULL;
+    }
+
+    aead = g_new0(QCryptoAead, 1);
+    aead->alg = alg;
+    aead->mode = mode;
+
+    ctx = g_new0(QCryptoAeadNettle, 1);
+
+    qcrypto_aead_alg_map[mode][alg].setkey(&ctx->u, key);
+
+    aead->opaque = ctx;
+
+    return aead;
 }
 
 void qcrypto_aead_free(QCryptoAead *aead)
 {
-    return;
+    QCryptoAeadNettle *ctx;
+
+    if (!aead) {
+        return;
+    }
+
+    ctx = aead->opaque;
+
+    g_free(ctx);
+    g_free(aead);
 }
 
 int qcrypto_aead_set_nonce(QCryptoAead *aead,
@@ -38,14 +145,43 @@ int qcrypto_aead_set_nonce(QCryptoAead *aead,
                            size_t tag_len,
                            Error **errp)
 {
-    return -1;
+    QCryptoAeadNettle *ctx;
+    struct qcrypto_nettle_aead_alg *aead_ops;
+
+    ctx = (QCryptoAeadNettle *)aead->opaque;
+
+    switch (aead->mode) {
+    case QCRYPTO_CIPHER_MODE_CCM:
+        aead_ops = &qcrypto_aead_alg_map[aead->mode][aead->alg];
+        aead_ops->u_op.setnonce(&ctx->u, nonce_len, nonce, aad_len,
+                      in_len, tag_len);
+        break;
+    case QCRYPTO_CIPHER_MODE_GCM:
+        aead_ops = &qcrypto_aead_alg_map[aead->mode][aead->alg];
+        aead_ops->u_op.setiv(&ctx->u, nonce_len, nonce);
+        break;
+    default:
+        error_setg(errp, "Unsupported AEAD mode: %d",
+                   aead->mode);
+        return -1;
+    }
+
+    return 0;
 }
 
 int qcrypto_aead_authenticate(QCryptoAead *aead,
                               const uint8_t *aad, size_t aad_len,
                               Error **errp)
 {
-    return -1;
+    QCryptoAeadNettle *ctx;
+    struct qcrypto_nettle_aead_alg *aead_ops;
+
+    ctx = (QCryptoAeadNettle *)aead->opaque;
+    aead_ops = &qcrypto_aead_alg_map[aead->mode][aead->alg];
+
+    aead_ops->update(&ctx->u, aad_len, aad);
+
+    return 0;
 }
 
 int qcrypto_aead_encrypt(QCryptoAead *aead,
@@ -53,7 +189,15 @@ int qcrypto_aead_encrypt(QCryptoAead *aead,
                          uint8_t *out, size_t out_len,
                          Error **errp)
 {
-    return -1;
+    QCryptoAeadNettle *ctx;
+    struct qcrypto_nettle_aead_alg *aead_ops;
+
+    ctx = (QCryptoAeadNettle *)aead->opaque;
+    aead_ops = &qcrypto_aead_alg_map[aead->mode][aead->alg];
+
+    aead_ops->encrypt(&ctx->u, in_len, out, in);
+
+    return 0;
 }
 
 int qcrypto_aead_decrypt(QCryptoAead *aead,
@@ -61,12 +205,28 @@ int qcrypto_aead_decrypt(QCryptoAead *aead,
                          uint8_t *out, size_t out_len,
                          Error **errp)
 {
-    return -1;
+    QCryptoAeadNettle *ctx;
+    struct qcrypto_nettle_aead_alg *aead_ops;
+
+    ctx = (QCryptoAeadNettle *)aead->opaque;
+    aead_ops = &qcrypto_aead_alg_map[aead->mode][aead->alg];
+
+    aead_ops->decrypt(&ctx->u, in_len, out, in);
+
+    return 0;
 }
 
 int qcrypto_aead_get_tag(QCryptoAead *aead,
                          uint8_t *tag, size_t tag_len,
                          Error **errp)
 {
-    return -1;
+    QCryptoAeadNettle *ctx;
+    struct qcrypto_nettle_aead_alg *aead_ops;
+
+    ctx = (QCryptoAeadNettle *)aead->opaque;
+    aead_ops = &qcrypto_aead_alg_map[aead->mode][aead->alg];
+
+    aead_ops->digest(&ctx->u, tag_len, tag);
+
+    return 0;
 }
Signature · 1 line
1.8.3.1
#6 lore raw

PATCH RESEND 4/6 crypto: implement gcrypt-backed AEAD algorithms

In reply to Longpeng(Mike)

This patch add gcrypt-backed AEAD algorithms support

---
 crypto/aead-gcrypt.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 167 insertions(+), 6 deletions(-)
crypto/aead-gcrypt.c +167 -6
index 9465518..9892e3b 100644
@@ -17,17 +17,100 @@
 #include "crypto/aead.h"
 #include <gcrypt.h>
 
+typedef struct QCryptoAeadGcrypt QCryptoAeadGcrypt;
+struct QCryptoAeadGcrypt {
+    gcry_cipher_hd_t handle;
+};
+
 QCryptoAead *qcrypto_aead_new(QCryptoCipherAlgorithm alg,
                               QCryptoCipherMode mode,
                               const uint8_t *key, size_t nkey,
                               Error **errp)
 {
+    QCryptoAead *aead;
+    QCryptoAeadGcrypt *ctx;
+    gcry_error_t err;
+    int gcryalg, gcrymode;
+
+    switch (mode) {
+    case QCRYPTO_CIPHER_MODE_CCM:
+        gcrymode = GCRY_CIPHER_MODE_CCM;
+        break;
+    case QCRYPTO_CIPHER_MODE_GCM:
+        gcrymode = GCRY_CIPHER_MODE_GCM;
+        break;
+    default:
+        error_setg(errp, "Unsupported AEAD mode %s",
+                   QCryptoCipherMode_lookup[mode]);
+        return NULL;
+    }
+
+    if (nkey != qcrypto_aead_get_key_len(alg)) {
+        error_setg(errp, "Cipher key length %zu is invalid",
+                   nkey);
+        return NULL;
+    }
+
+    switch (alg) {
+    case QCRYPTO_CIPHER_ALG_AES_128:
+        gcryalg = GCRY_CIPHER_AES128;
+        break;
+    case QCRYPTO_CIPHER_ALG_AES_192:
+        gcryalg = GCRY_CIPHER_AES192;
+        break;
+    case QCRYPTO_CIPHER_ALG_AES_256:
+        gcryalg = GCRY_CIPHER_AES256;
+        break;
+    default:
+        error_setg(errp, "Unsupported AEAD algorithm %s",
+                   QCryptoCipherAlgorithm_lookup[alg]);
+        return NULL;
+    }
+
+    aead = g_new0(QCryptoAead, 1);
+    aead->alg = alg;
+    aead->mode = mode;
+
+    ctx = g_new0(QCryptoAeadGcrypt, 1);
+
+    err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0);
+    if (err) {
+        error_setg(errp, "Cannot initialize aead: %s",
+                   gcry_strerror(err));
+        goto error;
+    }
+
+    err = gcry_cipher_setkey(ctx->handle, key, nkey);
+    if (err) {
+        error_setg(errp, "Cannot set key: %s",
+                   gcry_strerror(err));
+        goto error;
+    }
+
+    aead->opaque = ctx;
+
+    return aead;
+
+error:
+    gcry_cipher_close(ctx->handle);
+    g_free(ctx);
+    g_free(aead);
     return NULL;
 }
 
 void qcrypto_aead_free(QCryptoAead *aead)
 {
-    return;
+    QCryptoAeadGcrypt *ctx;
+
+    if (!aead) {
+        return;
+    }
+
+    ctx = aead->opaque;
+
+    gcry_cipher_close(ctx->handle);
+    g_free(ctx);
+    g_free(aead);
 }
 
 int qcrypto_aead_set_nonce(QCryptoAead *aead,
@@ -36,14 +119,54 @@ int qcrypto_aead_set_nonce(QCryptoAead *aead,
                            size_t tag_len,
                            Error **errp)
 {
-    return -1;
+    QCryptoAeadGcrypt *ctx;
+    gcry_error_t err;
+
+    ctx = aead->opaque;
+
+    err = gcry_cipher_setiv(ctx->handle, nonce, nonce_len);
+    if (err) {
+        error_setg(errp, "Cannot set iv/nonce: %s",
+                   gcry_strerror(err));
+        return -1;
+    }
+
+    if (aead->mode == QCRYPTO_CIPHER_MODE_CCM) {
+        size_t ctl_para[3];
+
+        ctl_para[0] = in_len;
+        ctl_para[1] = aad_len;
+        ctl_para[2] = tag_len;
+
+        err = gcry_cipher_ctl(ctx->handle, GCRYCTL_SET_CCM_LENGTHS,
+                              ctl_para, sizeof(ctl_para));
+        if (err) {
+            error_setg(errp, "Cannot set lengths: %s",
+                       gcry_strerror(err));
+            return -1;
+        }
+    }
+
+    return 0;
 }
 
 int qcrypto_aead_authenticate(QCryptoAead *aead,
                               const uint8_t *aad, size_t aad_len,
                               Error **errp)
 {
-    return -1;
+    QCryptoAeadGcrypt *ctx;
+    gcry_error_t err;
+
+    ctx = aead->opaque;
+
+    err = gcry_cipher_authenticate(ctx->handle, aad, aad_len);
+    if (err) {
+        error_setg(errp, "Cannot set associated data: %s",
+                   gcry_strerror(err));
+        return -1;
+    }
+
+    return 0;
 }
 
 int qcrypto_aead_encrypt(QCryptoAead *aead,
@@ -51,7 +174,20 @@ int qcrypto_aead_encrypt(QCryptoAead *aead,
                          uint8_t *out, size_t out_len,
                          Error **errp)
 {
-    return -1;
+    QCryptoAeadGcrypt *ctx;
+    gcry_error_t err;
+
+    ctx = aead->opaque;
+
+    err = gcry_cipher_encrypt(ctx->handle, out, out_len,
+                              in, in_len);
+    if (err) {
+        error_setg(errp, "Cannot encrypt data: %s",
+                   gcry_strerror(err));
+        return -1;
+    }
+
+    return 0;
 }
 
 int qcrypto_aead_decrypt(QCryptoAead *aead,
@@ -59,12 +195,37 @@ int qcrypto_aead_decrypt(QCryptoAead *aead,
                          uint8_t *out, size_t out_len,
                          Error **errp)
 {
-    return -1;
+    QCryptoAeadGcrypt *ctx;
+    gcry_error_t err;
+
+    ctx = aead->opaque;
+
+    err = gcry_cipher_decrypt(ctx->handle, out, out_len,
+                              in, in_len);
+    if (err) {
+        error_setg(errp, "Cannot decrypt data: %s",
+                   gcry_strerror(err));
+        return -1;
+    }
+
+    return 0;
 }
 
 int qcrypto_aead_get_tag(QCryptoAead *aead,
                          uint8_t *tag, size_t tag_len,
                          Error **errp)
 {
-    return -1;
+    QCryptoAeadGcrypt *ctx;
+    gcry_error_t err;
+
+    ctx = aead->opaque;
+
+    err = gcry_cipher_gettag(ctx->handle, tag, tag_len);
+    if (err) {
+        error_setg(errp, "Cannot get tag: %s",
+                   gcry_strerror(err));
+        return -1;
+    }
+
+    return 0;
 }
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH RESEND 5/6 crypto: implement other common funcs for AEAD algorithms

In reply to Longpeng(Mike)

If currently gcrypt/nettle doesn't support AEAD alg, then we should implement some no-op funcs.

---
 crypto/aead.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)
crypto/aead.c +42 -2
index 47639b7..effe45e 100644
@@ -16,18 +16,58 @@
 #include "qapi/error.h"
 #include "crypto/aead.h"
 
+#if defined(CONFIG_NETTLE_AEAD) || defined(CONFIG_GCRYPT_AEAD)
+
+static size_t alg_key_len[QCRYPTO_AEAD_ALG__MAX] = {
+    [QCRYPTO_CIPHER_ALG_AES_128] = 16,
+    [QCRYPTO_CIPHER_ALG_AES_192] = 24,
+    [QCRYPTO_CIPHER_ALG_AES_256] = 32,
+};
+
 bool qcrypto_aead_supports(QCryptoCipherAlgorithm alg,
                            QCryptoCipherMode mode)
 {
+    switch (alg) {
+    case QCRYPTO_CIPHER_ALG_AES_128:
+    case QCRYPTO_CIPHER_ALG_AES_192:
+    case QCRYPTO_CIPHER_ALG_AES_256:
+        break;
+    default:
+        return false;
+    }
+
+    switch (mode) {
+    case QCRYPTO_CIPHER_MODE_CCM:
+    case QCRYPTO_CIPHER_MODE_GCM:
+        return true;
+    default:
+        break;
+    }
+
     return false;
 }
 
 size_t qcrypto_aead_get_key_len(QCryptoCipherAlgorithm alg)
 {
-    return -1;
+    if (alg > G_N_ELEMENTS(alg_key_len)) {
+        return 0;
+    }
+
+    return alg_key_len[alg];
 }
 
-#if !defined(CONFIG_NETTLE_AEAD) && !defined(CONFIG_GCRYPT_AEAD)
+#else
+
+bool qcrypto_aead_supports(QCryptoCipherAlgorithm alg,
+                           QCryptoCipherMode mode)
+{
+    return false;
+}
+
+size_t qcrypto_aead_get_key_len(QCryptoCipherAlgorithm alg)
+{
+    return -1;
+}
 
 QCryptoAead *qcrypto_aead_new(QCryptoCipherAlgorithm alg,
                               QCryptoCipherMode mode,
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH RESEND 6/6 crypto: add AEAD algorithms testcases

In reply to Longpeng(Mike)

This patch add some AEAD algorithms testcases

---
 tests/Makefile.include   |   2 +
 tests/test-crypto-aead.c | 357 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 359 insertions(+)
 create mode 100644 tests/test-crypto-aead.c
tests/Makefile.include +2 -0
index 4841d58..686ba30 100644
@@ -93,6 +93,7 @@ gcov-files-test-write-threshold-y = block/write-threshold.c
 check-unit-y += tests/test-crypto-hash$(EXESUF)
 check-unit-y += tests/test-crypto-hmac$(EXESUF)
 check-unit-y += tests/test-crypto-cipher$(EXESUF)
+check-unit-y += tests/test-crypto-aead$(EXESUF)
 check-unit-y += tests/test-crypto-secret$(EXESUF)
 check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF)
 check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlssession$(EXESUF)
@@ -574,6 +575,7 @@ tests/test-bitops$(EXESUF): tests/test-bitops.o $(test-util-obj-y)
 tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y)
 tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-obj-y)
 tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y)
+tests/test-crypto-aead$(EXESUF): tests/test-crypto-aead.o $(test-crypto-obj-y)
 tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypto-obj-y)
 tests/test-crypto-xts$(EXESUF): tests/test-crypto-xts.o $(test-crypto-obj-y)
 
tests/test-crypto-aead.c new file +357 -0

357 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#9 lore raw

Re: [Qemu-devel] [PATCH RESEND 6/6] crypto: add AEAD algorithms testcases

Quoted 8 lines · Longpeng(Mike)

This patch add some AEAD algorithms testcases

---
 tests/Makefile.include   |   2 +
 tests/test-crypto-aead.c | 357 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 359 insertions(+)
 create mode 100644 tests/test-crypto-aead.c

This should also be just part of test-crypto-cipher.c

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|