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