yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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