b38b0f
From 8b0cd52cde37fa503ab36eb7ce31bdd3a8e88aac Mon Sep 17 00:00:00 2001
b38b0f
From: "Daniel P. Berrange" <berrange@redhat.com>
b38b0f
Date: Wed, 24 Apr 2019 09:56:42 +0100
b38b0f
Subject: [PATCH 8/9] crypto: refactor XTS cipher mode test suite
b38b0f
MIME-Version: 1.0
b38b0f
Content-Type: text/plain; charset=UTF-8
b38b0f
Content-Transfer-Encoding: 8bit
b38b0f
b38b0f
RH-Author: Daniel P. Berrange <berrange@redhat.com>
b38b0f
Message-id: <20190424095643.796-9-berrange@redhat.com>
b38b0f
Patchwork-id: 85885
b38b0f
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 8/9] crypto: refactor XTS cipher mode test suite
b38b0f
Bugzilla: 1680231
b38b0f
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
b38b0f
RH-Acked-by: John Snow <jsnow@redhat.com>
b38b0f
RH-Acked-by: Eric Blake <eblake@redhat.com>
b38b0f
b38b0f
The current XTS test overloads two different tests in a single function
b38b0f
making the code a little hard to follow. Split it into distinct test
b38b0f
cases.
b38b0f
b38b0f
Reviewed-by: Alberto Garcia <berto@igalia.com>
b38b0f
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
b38b0f
(cherry picked from commit a61f682fde664467c4b4dd498ea84338598c8cbd)
b38b0f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
b38b0f
---
b38b0f
 tests/test-crypto-xts.c | 140 +++++++++++++++++++++++++++---------------------
b38b0f
 1 file changed, 80 insertions(+), 60 deletions(-)
b38b0f
b38b0f
diff --git a/tests/test-crypto-xts.c b/tests/test-crypto-xts.c
b38b0f
index 1f1412c..81606d9 100644
b38b0f
--- a/tests/test-crypto-xts.c
b38b0f
+++ b/tests/test-crypto-xts.c
b38b0f
@@ -1,7 +1,7 @@
b38b0f
 /*
b38b0f
  * QEMU Crypto XTS cipher mode
b38b0f
  *
b38b0f
- * Copyright (c) 2015-2016 Red Hat, Inc.
b38b0f
+ * Copyright (c) 2015-2018 Red Hat, Inc.
b38b0f
  *
b38b0f
  * This library is free software; you can redistribute it and/or
b38b0f
  * modify it under the terms of the GNU Lesser General Public
b38b0f
@@ -340,70 +340,79 @@ static void test_xts_aes_decrypt(const void *ctx,
b38b0f
 static void test_xts(const void *opaque)
b38b0f
 {
b38b0f
     const QCryptoXTSTestData *data = opaque;
b38b0f
-    unsigned char out[512], Torg[16], T[16];
b38b0f
+    uint8_t out[512], Torg[16], T[16];
b38b0f
     uint64_t seq;
b38b0f
-    int j;
b38b0f
-    unsigned long len;
b38b0f
     struct TestAES aesdata;
b38b0f
     struct TestAES aestweak;
b38b0f
 
b38b0f
-    for (j = 0; j < 2; j++) {
b38b0f
-        /* skip the cases where
b38b0f
-         * the length is smaller than 2*blocklen
b38b0f
-         * or the length is not a multiple of 32
b38b0f
-         */
b38b0f
-        if ((j == 1) && ((data->PTLEN < 32) || (data->PTLEN % 32))) {
b38b0f
-            continue;
b38b0f
-        }
b38b0f
-        len = data->PTLEN / 2;
b38b0f
-
b38b0f
-        AES_set_encrypt_key(data->key1, data->keylen / 2 * 8, &aesdata.enc);
b38b0f
-        AES_set_decrypt_key(data->key1, data->keylen / 2 * 8, &aesdata.dec);
b38b0f
-        AES_set_encrypt_key(data->key2, data->keylen / 2 * 8, &aestweak.enc);
b38b0f
-        AES_set_decrypt_key(data->key2, data->keylen / 2 * 8, &aestweak.dec);
b38b0f
-
b38b0f
-        seq = data->seqnum;
b38b0f
-        STORE64L(seq, Torg);
b38b0f
-        memset(Torg + 8, 0, 8);
b38b0f
-
b38b0f
-        memcpy(T, Torg, sizeof(T));
b38b0f
-        if (j == 0) {
b38b0f
-            xts_encrypt(&aesdata, &aestweak,
b38b0f
-                        test_xts_aes_encrypt,
b38b0f
-                        test_xts_aes_decrypt,
b38b0f
-                        T, data->PTLEN, out, data->PTX);
b38b0f
-        } else {
b38b0f
-            xts_encrypt(&aesdata, &aestweak,
b38b0f
-                        test_xts_aes_encrypt,
b38b0f
-                        test_xts_aes_decrypt,
b38b0f
-                        T, len, out, data->PTX);
b38b0f
-            xts_encrypt(&aesdata, &aestweak,
b38b0f
-                        test_xts_aes_encrypt,
b38b0f
-                        test_xts_aes_decrypt,
b38b0f
-                        T, len, &out[len], &data->PTX[len]);
b38b0f
-        }
b38b0f
+    AES_set_encrypt_key(data->key1, data->keylen / 2 * 8, &aesdata.enc);
b38b0f
+    AES_set_decrypt_key(data->key1, data->keylen / 2 * 8, &aesdata.dec);
b38b0f
+    AES_set_encrypt_key(data->key2, data->keylen / 2 * 8, &aestweak.enc);
b38b0f
+    AES_set_decrypt_key(data->key2, data->keylen / 2 * 8, &aestweak.dec);
b38b0f
 
b38b0f
-        g_assert(memcmp(out, data->CTX, data->PTLEN) == 0);
b38b0f
-
b38b0f
-        memcpy(T, Torg, sizeof(T));
b38b0f
-        if (j == 0) {
b38b0f
-            xts_decrypt(&aesdata, &aestweak,
b38b0f
-                        test_xts_aes_encrypt,
b38b0f
-                        test_xts_aes_decrypt,
b38b0f
-                        T, data->PTLEN, out, data->CTX);
b38b0f
-        } else {
b38b0f
-            xts_decrypt(&aesdata, &aestweak,
b38b0f
-                        test_xts_aes_encrypt,
b38b0f
-                        test_xts_aes_decrypt,
b38b0f
-                        T, len, out, data->CTX);
b38b0f
-            xts_decrypt(&aesdata, &aestweak,
b38b0f
-                        test_xts_aes_encrypt,
b38b0f
-                        test_xts_aes_decrypt,
b38b0f
-                        T, len, &out[len], &data->CTX[len]);
b38b0f
-        }
b38b0f
+    seq = data->seqnum;
b38b0f
+    STORE64L(seq, Torg);
b38b0f
+    memset(Torg + 8, 0, 8);
b38b0f
 
b38b0f
-        g_assert(memcmp(out, data->PTX, data->PTLEN) == 0);
b38b0f
-    }
b38b0f
+    memcpy(T, Torg, sizeof(T));
b38b0f
+    xts_encrypt(&aesdata, &aestweak,
b38b0f
+                test_xts_aes_encrypt,
b38b0f
+                test_xts_aes_decrypt,
b38b0f
+                T, data->PTLEN, out, data->PTX);
b38b0f
+
b38b0f
+    g_assert(memcmp(out, data->CTX, data->PTLEN) == 0);
b38b0f
+
b38b0f
+    memcpy(T, Torg, sizeof(T));
b38b0f
+    xts_decrypt(&aesdata, &aestweak,
b38b0f
+                test_xts_aes_encrypt,
b38b0f
+                test_xts_aes_decrypt,
b38b0f
+                T, data->PTLEN, out, data->CTX);
b38b0f
+
b38b0f
+    g_assert(memcmp(out, data->PTX, data->PTLEN) == 0);
b38b0f
+}
b38b0f
+
b38b0f
+
b38b0f
+static void test_xts_split(const void *opaque)
b38b0f
+{
b38b0f
+    const QCryptoXTSTestData *data = opaque;
b38b0f
+    uint8_t out[512], Torg[16], T[16];
b38b0f
+    uint64_t seq;
b38b0f
+    unsigned long len = data->PTLEN / 2;
b38b0f
+    struct TestAES aesdata;
b38b0f
+    struct TestAES aestweak;
b38b0f
+
b38b0f
+    AES_set_encrypt_key(data->key1, data->keylen / 2 * 8, &aesdata.enc);
b38b0f
+    AES_set_decrypt_key(data->key1, data->keylen / 2 * 8, &aesdata.dec);
b38b0f
+    AES_set_encrypt_key(data->key2, data->keylen / 2 * 8, &aestweak.enc);
b38b0f
+    AES_set_decrypt_key(data->key2, data->keylen / 2 * 8, &aestweak.dec);
b38b0f
+
b38b0f
+    seq = data->seqnum;
b38b0f
+    STORE64L(seq, Torg);
b38b0f
+    memset(Torg + 8, 0, 8);
b38b0f
+
b38b0f
+    memcpy(T, Torg, sizeof(T));
b38b0f
+    xts_encrypt(&aesdata, &aestweak,
b38b0f
+                test_xts_aes_encrypt,
b38b0f
+                test_xts_aes_decrypt,
b38b0f
+                T, len, out, data->PTX);
b38b0f
+    xts_encrypt(&aesdata, &aestweak,
b38b0f
+                test_xts_aes_encrypt,
b38b0f
+                test_xts_aes_decrypt,
b38b0f
+                T, len, &out[len], &data->PTX[len]);
b38b0f
+
b38b0f
+    g_assert(memcmp(out, data->CTX, data->PTLEN) == 0);
b38b0f
+
b38b0f
+    memcpy(T, Torg, sizeof(T));
b38b0f
+    xts_decrypt(&aesdata, &aestweak,
b38b0f
+                test_xts_aes_encrypt,
b38b0f
+                test_xts_aes_decrypt,
b38b0f
+                T, len, out, data->CTX);
b38b0f
+    xts_decrypt(&aesdata, &aestweak,
b38b0f
+                test_xts_aes_encrypt,
b38b0f
+                test_xts_aes_decrypt,
b38b0f
+                T, len, &out[len], &data->CTX[len]);
b38b0f
+
b38b0f
+    g_assert(memcmp(out, data->PTX, data->PTLEN) == 0);
b38b0f
 }
b38b0f
 
b38b0f
 
b38b0f
@@ -416,7 +425,18 @@ int main(int argc, char **argv)
b38b0f
     g_assert(qcrypto_init(NULL) == 0);
b38b0f
 
b38b0f
     for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
b38b0f
-        g_test_add_data_func(test_data[i].path, &test_data[i], test_xts);
b38b0f
+        gchar *path = g_strdup_printf("%s/basic", test_data[i].path);
b38b0f
+        g_test_add_data_func(path, &test_data[i], test_xts);
b38b0f
+        g_free(path);
b38b0f
+
b38b0f
+        /* skip the cases where the length is smaller than 2*blocklen
b38b0f
+         * or the length is not a multiple of 32
b38b0f
+         */
b38b0f
+        if ((test_data[i].PTLEN >= 32) && !(test_data[i].PTLEN % 32)) {
b38b0f
+            path = g_strdup_printf("%s/split", test_data[i].path);
b38b0f
+            g_test_add_data_func(path, &test_data[i], test_xts_split);
b38b0f
+            g_free(path);
b38b0f
+        }
b38b0f
     }
b38b0f
 
b38b0f
     return g_test_run();
b38b0f
-- 
b38b0f
1.8.3.1
b38b0f