Blame SOURCES/kvm-crypto-add-testing-for-unaligned-buffers-with-XTS-ci.patch

Pablo Greco e6a3ae
From a5301e637be3cdd123a3688901118e8d8099d29c Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: "Daniel P. Berrange" <berrange@redhat.com>
Pablo Greco e6a3ae
Date: Wed, 24 Apr 2019 09:56:43 +0100
Pablo Greco e6a3ae
Subject: [PATCH 9/9] crypto: add testing for unaligned buffers with XTS cipher
Pablo Greco e6a3ae
 mode
Pablo Greco e6a3ae
MIME-Version: 1.0
Pablo Greco e6a3ae
Content-Type: text/plain; charset=UTF-8
Pablo Greco e6a3ae
Content-Transfer-Encoding: 8bit
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: Daniel P. Berrange <berrange@redhat.com>
Pablo Greco e6a3ae
Message-id: <20190424095643.796-10-berrange@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 85886
Pablo Greco e6a3ae
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 9/9] crypto: add testing for unaligned buffers with XTS cipher mode
Pablo Greco e6a3ae
Bugzilla: 1680231
Pablo Greco e6a3ae
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: John Snow <jsnow@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Eric Blake <eblake@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Validate that the XTS cipher mode will correctly operate with plain
Pablo Greco e6a3ae
text, cipher text and IV buffers that are not 64-bit aligned.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Reviewed-by: Alberto Garcia <berto@igalia.com>
Pablo Greco e6a3ae
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Pablo Greco e6a3ae
(cherry picked from commit 1e0fa32c6c952d2ce9c19d35717c609804dd55d5)
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 tests/test-crypto-xts.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++
Pablo Greco e6a3ae
 1 file changed, 86 insertions(+)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/tests/test-crypto-xts.c b/tests/test-crypto-xts.c
Pablo Greco e6a3ae
index 81606d9..6fb61cf 100644
Pablo Greco e6a3ae
--- a/tests/test-crypto-xts.c
Pablo Greco e6a3ae
+++ b/tests/test-crypto-xts.c
Pablo Greco e6a3ae
@@ -416,6 +416,88 @@ static void test_xts_split(const void *opaque)
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
+static void test_xts_unaligned(const void *opaque)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+#define BAD_ALIGN 3
Pablo Greco e6a3ae
+    const QCryptoXTSTestData *data = opaque;
Pablo Greco e6a3ae
+    uint8_t in[512 + BAD_ALIGN], out[512 + BAD_ALIGN];
Pablo Greco e6a3ae
+    uint8_t Torg[16], T[16 + BAD_ALIGN];
Pablo Greco e6a3ae
+    uint64_t seq;
Pablo Greco e6a3ae
+    struct TestAES aesdata;
Pablo Greco e6a3ae
+    struct TestAES aestweak;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    AES_set_encrypt_key(data->key1, data->keylen / 2 * 8, &aesdata.enc);
Pablo Greco e6a3ae
+    AES_set_decrypt_key(data->key1, data->keylen / 2 * 8, &aesdata.dec);
Pablo Greco e6a3ae
+    AES_set_encrypt_key(data->key2, data->keylen / 2 * 8, &aestweak.enc);
Pablo Greco e6a3ae
+    AES_set_decrypt_key(data->key2, data->keylen / 2 * 8, &aestweak.dec);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    seq = data->seqnum;
Pablo Greco e6a3ae
+    STORE64L(seq, Torg);
Pablo Greco e6a3ae
+    memset(Torg + 8, 0, 8);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    /* IV not aligned */
Pablo Greco e6a3ae
+    memcpy(T + BAD_ALIGN, Torg, 16);
Pablo Greco e6a3ae
+    memcpy(in, data->PTX, data->PTLEN);
Pablo Greco e6a3ae
+    xts_encrypt(&aesdata, &aestweak,
Pablo Greco e6a3ae
+                test_xts_aes_encrypt,
Pablo Greco e6a3ae
+                test_xts_aes_decrypt,
Pablo Greco e6a3ae
+                T + BAD_ALIGN, data->PTLEN, out, in);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    g_assert(memcmp(out, data->CTX, data->PTLEN) == 0);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    /* plain text not aligned */
Pablo Greco e6a3ae
+    memcpy(T, Torg, 16);
Pablo Greco e6a3ae
+    memcpy(in + BAD_ALIGN, data->PTX, data->PTLEN);
Pablo Greco e6a3ae
+    xts_encrypt(&aesdata, &aestweak,
Pablo Greco e6a3ae
+                test_xts_aes_encrypt,
Pablo Greco e6a3ae
+                test_xts_aes_decrypt,
Pablo Greco e6a3ae
+                T, data->PTLEN, out, in + BAD_ALIGN);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    g_assert(memcmp(out, data->CTX, data->PTLEN) == 0);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    /* cipher text not aligned */
Pablo Greco e6a3ae
+    memcpy(T, Torg, 16);
Pablo Greco e6a3ae
+    memcpy(in, data->PTX, data->PTLEN);
Pablo Greco e6a3ae
+    xts_encrypt(&aesdata, &aestweak,
Pablo Greco e6a3ae
+                test_xts_aes_encrypt,
Pablo Greco e6a3ae
+                test_xts_aes_decrypt,
Pablo Greco e6a3ae
+                T, data->PTLEN, out + BAD_ALIGN, in);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    g_assert(memcmp(out + BAD_ALIGN, data->CTX, data->PTLEN) == 0);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    /* IV not aligned */
Pablo Greco e6a3ae
+    memcpy(T + BAD_ALIGN, Torg, 16);
Pablo Greco e6a3ae
+    memcpy(in, data->CTX, data->PTLEN);
Pablo Greco e6a3ae
+    xts_decrypt(&aesdata, &aestweak,
Pablo Greco e6a3ae
+                test_xts_aes_encrypt,
Pablo Greco e6a3ae
+                test_xts_aes_decrypt,
Pablo Greco e6a3ae
+                T + BAD_ALIGN, data->PTLEN, out, in);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    g_assert(memcmp(out, data->PTX, data->PTLEN) == 0);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    /* cipher text not aligned */
Pablo Greco e6a3ae
+    memcpy(T, Torg, 16);
Pablo Greco e6a3ae
+    memcpy(in + BAD_ALIGN, data->CTX, data->PTLEN);
Pablo Greco e6a3ae
+    xts_decrypt(&aesdata, &aestweak,
Pablo Greco e6a3ae
+                test_xts_aes_encrypt,
Pablo Greco e6a3ae
+                test_xts_aes_decrypt,
Pablo Greco e6a3ae
+                T, data->PTLEN, out, in + BAD_ALIGN);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    g_assert(memcmp(out, data->PTX, data->PTLEN) == 0);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    /* plain text not aligned */
Pablo Greco e6a3ae
+    memcpy(T, Torg, 16);
Pablo Greco e6a3ae
+    memcpy(in, data->CTX, data->PTLEN);
Pablo Greco e6a3ae
+    xts_decrypt(&aesdata, &aestweak,
Pablo Greco e6a3ae
+                test_xts_aes_encrypt,
Pablo Greco e6a3ae
+                test_xts_aes_decrypt,
Pablo Greco e6a3ae
+                T, data->PTLEN, out + BAD_ALIGN, in);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    g_assert(memcmp(out + BAD_ALIGN, data->PTX, data->PTLEN) == 0);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
 int main(int argc, char **argv)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
     size_t i;
Pablo Greco e6a3ae
@@ -437,6 +519,10 @@ int main(int argc, char **argv)
Pablo Greco e6a3ae
             g_test_add_data_func(path, &test_data[i], test_xts_split);
Pablo Greco e6a3ae
             g_free(path);
Pablo Greco e6a3ae
         }
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+        path = g_strdup_printf("%s/unaligned", test_data[i].path);
Pablo Greco e6a3ae
+        g_test_add_data_func(path, &test_data[i], test_xts_unaligned);
Pablo Greco e6a3ae
+        g_free(path);
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     return g_test_run();
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae