Skip to contentSkip to conversation

crypto: ensure XTS is only used with ciphers with 16 byte blocks

PATCH Superseded Daniel P. Berrange 2 participants 3 messages Aug 26, 2016 Crypto API

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: ensure XTS is only used with ciphers with 16 byte blocks

The XTS cipher mode needs to be used with a cipher which has a block size of 16 bytes. If a mis-matching block size is used, the code will either corrupt memory beyond the IV array, or not fully encrypt/decrypt the IV.

This fixes a memory curruption crash when attempting to use cast5-128 with xts, since the former has an 8 byte block size.

A test case is added to ensure the cipher creation fails with such an invalid combination.

---
 crypto/cipher-gcrypt.c     |  6 ++++++
 crypto/cipher-nettle.c     | 12 +++++++-----
 tests/test-crypto-cipher.c | 44 ++++++++++++++++++++++++++++++++++++--------
 3 files changed, 49 insertions(+), 13 deletions(-)
crypto/cipher-gcrypt.c +6 -0
index ede2f70..3652aa1 100644
@@ -192,6 +192,12 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
     }
 
     if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
+        if (ctx->blocksize != XTS_BLOCK_SIZE) {
+            error_setg(errp,
+                       "Cipher block size %zu must equal XTS block size %d",
+                       ctx->blocksize, XTS_BLOCK_SIZE);
+            goto error;
+        }
         ctx->iv = g_new0(uint8_t, ctx->blocksize);
     }
 
crypto/cipher-nettle.c +7 -5
index 70909fb..0267da5 100644
@@ -361,6 +361,13 @@ QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
         goto error;
     }
 
+    if (mode == QCRYPTO_CIPHER_MODE_XTS &&
+        ctx->blocksize != XTS_BLOCK_SIZE) {
+        error_setg(errp, "Cipher block size %zu must equal XTS block size %d",
+                   ctx->blocksize, XTS_BLOCK_SIZE);
+        goto error;
+    }
+
     ctx->iv = g_new0(uint8_t, ctx->blocksize);
     cipher->opaque = ctx;
 
@@ -456,11 +463,6 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
         break;
 
     case QCRYPTO_CIPHER_MODE_XTS:
-        if (ctx->blocksize != XTS_BLOCK_SIZE) {
-            error_setg(errp, "Block size must be %d not %zu",
-                       XTS_BLOCK_SIZE, ctx->blocksize);
-            return -1;
-        }
         xts_decrypt(ctx->ctx, ctx->ctx_tweak,
                     ctx->alg_encrypt_wrapper, ctx->alg_decrypt_wrapper,
                     ctx->iv, len, out, in);
tests/test-crypto-cipher.c +36 -8

71 lines not on this page · raw message

Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Daniel P. Berrange

The XTS cipher mode needs to be used with a cipher which has a block size of 16 bytes. If a mis-matching block size is used, the code will either corrupt memory beyond the IV array, or not fully encrypt/decrypt the IV.

This fixes a memory curruption crash when attempting to use

s/curruption/corruption/

Quoted 11 lines

cast5-128 with xts, since the former has an 8 byte block size.

A test case is added to ensure the cipher creation fails with such an invalid combination.

---
 crypto/cipher-gcrypt.c     |  6 ++++++
 crypto/cipher-nettle.c     | 12 +++++++-----
 tests/test-crypto-cipher.c | 44 ++++++++++++++++++++++++++++++++++++--------
 3 files changed, 49 insertions(+), 13 deletions(-)

Are you aiming for a last-minute 2.7 fix, or should this just be 2.8 material and cc qemu-stable?

Quoted 6 lines

+++ b/tests/test-crypto-cipher.c

tests/test-crypto-cipher.c +0 -0
@@ -370,6 +370,17 @@ static QCryptoCipherTestData test_data[] = {
 
@@ -449,8 +468,16 @@ static void test_cipher(const void *opaque)
     cipher = qcrypto_cipher_new(
         data->alg, data->mode,

tests/test-crypto-cipher.c static void test_cipher(const void *opaque) · line 477

Quoted 10 lines
tests/test-crypto-cipher.c +7 -2
         key, nkey,
-        &error_abort);
-    g_assert(cipher != NULL);
+        &err);
+    if (data->plaintext) {
+        g_assert(err == NULL);
+        g_assert(cipher != NULL);
+    } else {
+        g_assert(err != NULL);
+        error_free(err);

Could shorten these two lines as error_free_or_abort(&err), but that's cosmetic.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 22 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrange

The XTS cipher mode needs to be used with a cipher which has a block size of 16 bytes. If a mis-matching block size is used, the code will either corrupt memory beyond the IV array, or not fully encrypt/decrypt the IV.

This fixes a memory curruption crash when attempting to use

s/curruption/corruption/

Quoted 11 lines

cast5-128 with xts, since the former has an 8 byte block size.

A test case is added to ensure the cipher creation fails with such an invalid combination.

---
 crypto/cipher-gcrypt.c     |  6 ++++++
 crypto/cipher-nettle.c     | 12 +++++++-----
 tests/test-crypto-cipher.c | 44 ++++++++++++++++++++++++++++++++++++--------
 3 files changed, 49 insertions(+), 13 deletions(-)
Quoted 2 lines

Are you aiming for a last-minute 2.7 fix, or should this just be 2.8 material and cc qemu-stable?

This isn't critical for 2.7, as this is already invalid usage. IOW anyone who hits the crash, simply shouldn't use this combination.

Quoted 6 lines
Quoted 2 lines

+++ b/tests/test-crypto-cipher.c

(quoted diff) +0 -0
@@ -370,6 +370,17 @@ static QCryptoCipherTestData test_data[] = {
Quoted 16 lines

tests/test-crypto-cipher.c static void test_cipher(const void *opaque) · line 477

Quoted 13 lines
tests/test-crypto-cipher.c +7 -2
@@ -449,8 +468,16 @@ static void test_cipher(const void *opaque)
     cipher = qcrypto_cipher_new(
         data->alg, data->mode,
         key, nkey,
-        &error_abort);
-    g_assert(cipher != NULL);
+        &err);
+    if (data->plaintext) {
+        g_assert(err == NULL);
+        g_assert(cipher != NULL);
+    } else {
+        g_assert(err != NULL);
+        error_free(err);

Could shorten these two lines as error_free_or_abort(&err), but that's cosmetic.

Will do.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|