Skip to contentSkip to conversation

crypto: add 3des-ede support when using libgcrypt/nettle

PATCH In review Longpeng(Mike) 4 participants 6 messages Dec 8, 2016 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 crypto: add 3des-ede support when using libgcrypt/nettle

Libgcrypt and nettle support 3des-ede, so this patch add 3des-ede support when using libgcrypt or nettle.

---
Changes since v1:
- rename 3des-ede to 3des. [Daniel]
- add (since 2.9) in comment. [Gonglei]
- add ctr(3des) testcase. [Gonglei]
- split original patchset. [Gonglei]

---
 crypto/cipher-gcrypt.c     |   6 +++
 crypto/cipher-nettle.c     |  37 ++++++++++++++
 crypto/cipher.c            |   7 ++-
 qapi/crypto.json           |   3 +-
 tests/test-crypto-cipher.c | 119 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 169 insertions(+), 3 deletions(-)
crypto/cipher-gcrypt.c +6 -0
index c550db9..6487eca 100644
@@ -29,6 +29,7 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
 {
     switch (alg) {
     case QCRYPTO_CIPHER_ALG_DES_RFB:
+    case QCRYPTO_CIPHER_ALG_3DES:
     case QCRYPTO_CIPHER_ALG_AES_128:
     case QCRYPTO_CIPHER_ALG_AES_192:
     case QCRYPTO_CIPHER_ALG_AES_256:
@@ -99,6 +100,10 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
         gcryalg = GCRY_CIPHER_DES;
         break;
 
+    case QCRYPTO_CIPHER_ALG_3DES:
+        gcryalg = GCRY_CIPHER_3DES;
+        break;
+
     case QCRYPTO_CIPHER_ALG_AES_128:
         gcryalg = GCRY_CIPHER_AES128;
         break;
@@ -200,6 +205,7 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
         case QCRYPTO_CIPHER_ALG_TWOFISH_256:
             ctx->blocksize = 16;
             break;
+        case QCRYPTO_CIPHER_ALG_3DES:
         case QCRYPTO_CIPHER_ALG_CAST5_128:
             ctx->blocksize = 8;
             break;
crypto/cipher-nettle.c +37 -0
index cd094cd..781c11d 100644
@@ -78,6 +78,18 @@ static void des_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
     des_decrypt(ctx, length, dst, src);
 }
 
+static void des3_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
+                                uint8_t *dst, const uint8_t *src)
+{
+    des3_encrypt(ctx, length, dst, src);
+}
+
+static void des3_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
+                                uint8_t *dst, const uint8_t *src)
+{
+    des3_decrypt(ctx, length, dst, src);
+}
+
 static void cast128_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
                                    uint8_t *dst, const uint8_t *src)
 {
@@ -140,6 +152,18 @@ static void des_decrypt_wrapper(const void *ctx, size_t length,
     des_decrypt(ctx, length, dst, src);
 }
 
+static void des3_encrypt_wrapper(const void *ctx, size_t length,
+                                uint8_t *dst, const uint8_t *src)
+{
+    des3_encrypt(ctx, length, dst, src);
+}
+
+static void des3_decrypt_wrapper(const void *ctx, size_t length,
+                                uint8_t *dst, const uint8_t *src)
+{
+    des3_decrypt(ctx, length, dst, src);
+}
+
 static void cast128_encrypt_wrapper(const void *ctx, size_t length,
                                     uint8_t *dst, const uint8_t *src)
 {
@@ -197,6 +221,7 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
 {
     switch (alg) {
     case QCRYPTO_CIPHER_ALG_DES_RFB:
+    case QCRYPTO_CIPHER_ALG_3DES:
     case QCRYPTO_CIPHER_ALG_AES_128:
     case QCRYPTO_CIPHER_ALG_AES_192:
     case QCRYPTO_CIPHER_ALG_AES_256:
@@ -270,6 +295,18 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
         ctx->blocksize = DES_BLOCK_SIZE;
         break;
 
+    case QCRYPTO_CIPHER_ALG_3DES:
+        ctx->ctx = g_new0(struct des3_ctx, 1);
+        des3_set_key(ctx->ctx, key);
+
+        ctx->alg_encrypt_native = des3_encrypt_native;
+        ctx->alg_decrypt_native = des3_decrypt_native;
+        ctx->alg_encrypt_wrapper = des3_encrypt_wrapper;
+        ctx->alg_decrypt_wrapper = des3_decrypt_wrapper;
+
+        ctx->blocksize = DES3_BLOCK_SIZE;
+        break;
+
     case QCRYPTO_CIPHER_ALG_AES_128:
     case QCRYPTO_CIPHER_ALG_AES_192:
     case QCRYPTO_CIPHER_ALG_AES_256:
crypto/cipher.c +5 -2
index a9bca41..9ecaff7 100644
@@ -28,6 +28,7 @@ static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
     [QCRYPTO_CIPHER_ALG_AES_192] = 24,
     [QCRYPTO_CIPHER_ALG_AES_256] = 32,
     [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
+    [QCRYPTO_CIPHER_ALG_3DES] = 24,
     [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
     [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
     [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
@@ -42,6 +43,7 @@ static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
     [QCRYPTO_CIPHER_ALG_AES_192] = 16,
     [QCRYPTO_CIPHER_ALG_AES_256] = 16,
     [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
+    [QCRYPTO_CIPHER_ALG_3DES] = 8,
     [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
     [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
     [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
@@ -107,8 +109,9 @@ qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
     }
 
     if (mode == QCRYPTO_CIPHER_MODE_XTS) {
-        if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
-            error_setg(errp, "XTS mode not compatible with DES-RFB");
+        if (alg == QCRYPTO_CIPHER_ALG_DES_RFB
+                || alg == QCRYPTO_CIPHER_ALG_3DES) {
+            error_setg(errp, "XTS mode not compatible with DES-RFB/3DES");
             return false;
         }
         if (nkey % 2) {
qapi/crypto.json +2 -1
index 15d296e..f4fd93b 100644
@@ -63,6 +63,7 @@
 # @aes-192: AES with 192 bit / 24 byte keys
 # @aes-256: AES with 256 bit / 32 byte keys
 # @des-rfb: RFB specific variant of single DES. Do not use except in VNC.
+# @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9)
 # @cast5-128: Cast5 with 128 bit / 16 byte keys
 # @serpent-128: Serpent with 128 bit / 16 byte keys
 # @serpent-192: Serpent with 192 bit / 24 byte keys
@@ -75,7 +76,7 @@
 { 'enum': 'QCryptoCipherAlgorithm',
   'prefix': 'QCRYPTO_CIPHER_ALG',
   'data': ['aes-128', 'aes-192', 'aes-256',
-           'des-rfb',
+           'des-rfb', '3des',
            'cast5-128',
            'serpent-128', 'serpent-192', 'serpent-256',
            'twofish-128', 'twofish-192', 'twofish-256']}
tests/test-crypto-cipher.c +119 -0

125 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 19 lines

From: longpeng
Sent: Thursday, December 08, 2016 10:33 AM
To: [email protected]; [email protected]; [email protected]
Cc: Wubin (H); Zhoujian (jay, Euler); Gonglei (Arei); [email protected]; longpeng
Subject: [PATCH for-2.9 v2] crypto: add 3des-ede support when using libgcrypt/nettle

Libgcrypt and nettle support 3des-ede, so this patch add 3des-ede support when using libgcrypt or nettle.

---
Changes since v1:
- rename 3des-ede to 3des. [Daniel]
- add (since 2.9) in comment. [Gonglei]
- add ctr(3des) testcase. [Gonglei]
- split original patchset. [Gonglei]

Quoted 9 lines
---
 crypto/cipher-gcrypt.c     |   6 +++
 crypto/cipher-nettle.c     |  37 ++++++++++++++
 crypto/cipher.c            |   7 ++-
 qapi/crypto.json           |   3 +-
 tests/test-crypto-cipher.c | 119
+++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 169 insertions(+), 3 deletions(-)
#2 lore raw
In reply to Longpeng(Mike)
Quoted 11 lines · Longpeng(Mike)

Libgcrypt and nettle support 3des-ede, so this patch add 3des-ede support when using libgcrypt or nettle.

---
Changes since v1:
- rename 3des-ede to 3des. [Daniel]
- add (since 2.9) in comment. [Gonglei]
- add ctr(3des) testcase. [Gonglei]
- split original patchset. [Gonglei]

Quoted 7 lines
---
 crypto/cipher-gcrypt.c     |   6 +++
 crypto/cipher-nettle.c     |  37 ++++++++++++++
 crypto/cipher.c            |   7 ++-
 qapi/crypto.json           |   3 +-
 tests/test-crypto-cipher.c | 119 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 169 insertions(+), 3 deletions(-)

Added to my crypto-next queue

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/ :|
#3 lore raw
Quoted 20 lines · Daniel P. Berrange
Quoted 11 lines · Longpeng(Mike)

Libgcrypt and nettle support 3des-ede, so this patch add 3des-ede support when using libgcrypt or nettle.

---
Changes since v1:
- rename 3des-ede to 3des. [Daniel]
- add (since 2.9) in comment. [Gonglei]
- add ctr(3des) testcase. [Gonglei]
- split original patchset. [Gonglei]

Quoted 7 lines
---
 crypto/cipher-gcrypt.c     |   6 +++
 crypto/cipher-nettle.c     |  37 ++++++++++++++
 crypto/cipher.c            |   7 ++-
 qapi/crypto.json           |   3 +-
 tests/test-crypto-cipher.c | 119 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 169 insertions(+), 3 deletions(-)
Quoted 4 lines

Added to my crypto-next queue

Thanks! :)

Quoted 3 lines

Regards,
Daniel

Signature · 2 lines
Regards,
Longpeng(Mike)
#4 lore raw
In reply to Longpeng(Mike)
Quoted 16 lines · Longpeng(Mike)

Libgcrypt and nettle support 3des-ede, so this patch add 3des-ede support when using libgcrypt or nettle.

---

+++ b/qapi/crypto.json

qapi/crypto.json +1 -0
@@ -63,6 +63,7 @@
 # @aes-192: AES with 192 bit / 24 byte keys
 # @aes-256: AES with 256 bit / 32 byte keys
 # @des-rfb: RFB specific variant of single DES. Do not use except in VNC.
+# @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9)
 # @cast5-128: Cast5 with 128 bit / 16 byte keys
 # @serpent-128: Serpent with 128 bit / 16 byte keys
 # @serpent-192: Serpent with 192 bit / 24 byte keys

qapi/crypto.json line 79

Quoted 5 lines
qapi/crypto.json +1 -1
@@ -75,7 +76,7 @@
 { 'enum': 'QCryptoCipherAlgorithm',
   'prefix': 'QCRYPTO_CIPHER_ALG',
   'data': ['aes-128', 'aes-192', 'aes-256',
-           'des-rfb',
+           'des-rfb', '3des',

Not your fault that this is yet another enum name that is not a valid C identifier. QAPI at least handles it when referring to the enum constant (since we already have other such enums, in describing key codes), but it might make it awkward if we later want to use the enum type as a discriminator of a flat union (where I'm not sure that QAPI will still do the right thing). Is there some name we can pick that starts with a letter, to reduce the worry? But if not, I can live with '3des'.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw
Quoted 23 lines · Eric Blake
Quoted 16 lines · Longpeng(Mike)

Libgcrypt and nettle support 3des-ede, so this patch add 3des-ede support when using libgcrypt or nettle.

---

+++ b/qapi/crypto.json

qapi/crypto.json +1 -0
@@ -63,6 +63,7 @@
 # @aes-192: AES with 192 bit / 24 byte keys
 # @aes-256: AES with 256 bit / 32 byte keys
 # @des-rfb: RFB specific variant of single DES. Do not use except in VNC.
+# @3des: 3DES(EDE) with 192 bit / 24 byte keys (since 2.9)
 # @cast5-128: Cast5 with 128 bit / 16 byte keys
 # @serpent-128: Serpent with 128 bit / 16 byte keys
 # @serpent-192: Serpent with 192 bit / 24 byte keys

qapi/crypto.json line 79

Quoted 5 lines
qapi/crypto.json +1 -1
@@ -75,7 +76,7 @@
 { 'enum': 'QCryptoCipherAlgorithm',
   'prefix': 'QCRYPTO_CIPHER_ALG',
   'data': ['aes-128', 'aes-192', 'aes-256',
-           'des-rfb',
+           'des-rfb', '3des',
Quoted 7 lines

Not your fault that this is yet another enum name that is not a valid C identifier. QAPI at least handles it when referring to the enum constant (since we already have other such enums, in describing key codes), but it might make it awkward if we later want to use the enum type as a discriminator of a flat union (where I'm not sure that QAPI will still do the right thing). Is there some name we can pick that starts with a letter, to reduce the worry? But if not, I can live with '3des'.

I can make it 'des3' easily enough if you'd prefer :-)

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/ :|