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