Blame SOURCES/kvm-crypto-refactor-XTS-cipher-mode-test-suite.patch

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