Blame SOURCES/kvm-crypto-expand-algorithm-coverage-for-cipher-benchmar.patch

Pablo Greco e6a3ae
From 9f7d3aa69a0ab745301f6811df498e7aa2a51a26 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:36 +0100
Pablo Greco e6a3ae
Subject: [PATCH 2/9] crypto: expand algorithm coverage for cipher benchmark
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-3-berrange@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 85880
Pablo Greco e6a3ae
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 2/9] crypto: expand algorithm coverage for cipher benchmark
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
Add testing coverage for AES with XTS, ECB and CTR modes
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
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 a9e08155bd4ae096688a8ff19669023ab0fbc170)
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 tests/benchmark-crypto-cipher.c | 149 +++++++++++++++++++++++++++++++++-------
Pablo Greco e6a3ae
 1 file changed, 126 insertions(+), 23 deletions(-)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/tests/benchmark-crypto-cipher.c b/tests/benchmark-crypto-cipher.c
Pablo Greco e6a3ae
index f5a0d0b..67fdf8c 100644
Pablo Greco e6a3ae
--- a/tests/benchmark-crypto-cipher.c
Pablo Greco e6a3ae
+++ b/tests/benchmark-crypto-cipher.c
Pablo Greco e6a3ae
@@ -15,17 +15,27 @@
Pablo Greco e6a3ae
 #include "crypto/init.h"
Pablo Greco e6a3ae
 #include "crypto/cipher.h"
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-static void test_cipher_speed(const void *opaque)
Pablo Greco e6a3ae
+static void test_cipher_speed(size_t chunk_size,
Pablo Greco e6a3ae
+                              QCryptoCipherMode mode,
Pablo Greco e6a3ae
+                              QCryptoCipherAlgorithm alg)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
     QCryptoCipher *cipher;
Pablo Greco e6a3ae
     Error *err = NULL;
Pablo Greco e6a3ae
     double total = 0.0;
Pablo Greco e6a3ae
-    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
     uint8_t *key = NULL, *iv = NULL;
Pablo Greco e6a3ae
     uint8_t *plaintext = NULL, *ciphertext = NULL;
Pablo Greco e6a3ae
-    size_t nkey = qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128);
Pablo Greco e6a3ae
-    size_t niv = qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128,
Pablo Greco e6a3ae
-                                           QCRYPTO_CIPHER_MODE_CBC);
Pablo Greco e6a3ae
+    size_t nkey;
Pablo Greco e6a3ae
+    size_t niv;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    if (!qcrypto_cipher_supports(alg, mode)) {
Pablo Greco e6a3ae
+        return;
Pablo Greco e6a3ae
+    }
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    nkey = qcrypto_cipher_get_key_len(alg);
Pablo Greco e6a3ae
+    niv = qcrypto_cipher_get_iv_len(alg, mode);
Pablo Greco e6a3ae
+    if (mode == QCRYPTO_CIPHER_MODE_XTS) {
Pablo Greco e6a3ae
+        nkey *= 2;
Pablo Greco e6a3ae
+    }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     key = g_new0(uint8_t, nkey);
Pablo Greco e6a3ae
     memset(key, g_test_rand_int(), nkey);
Pablo Greco e6a3ae
@@ -38,14 +48,14 @@ static void test_cipher_speed(const void *opaque)
Pablo Greco e6a3ae
     plaintext = g_new0(uint8_t, chunk_size);
Pablo Greco e6a3ae
     memset(plaintext, g_test_rand_int(), chunk_size);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128,
Pablo Greco e6a3ae
-                                QCRYPTO_CIPHER_MODE_CBC,
Pablo Greco e6a3ae
+    cipher = qcrypto_cipher_new(alg, mode,
Pablo Greco e6a3ae
                                 key, nkey, &err;;
Pablo Greco e6a3ae
     g_assert(cipher != NULL);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    g_assert(qcrypto_cipher_setiv(cipher,
Pablo Greco e6a3ae
-                                  iv, niv,
Pablo Greco e6a3ae
-                                  &err) == 0);
Pablo Greco e6a3ae
+    if (mode != QCRYPTO_CIPHER_MODE_ECB)
Pablo Greco e6a3ae
+        g_assert(qcrypto_cipher_setiv(cipher,
Pablo Greco e6a3ae
+                                      iv, niv,
Pablo Greco e6a3ae
+                                      &err) == 0);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     g_test_timer_start();
Pablo Greco e6a3ae
     do {
Pablo Greco e6a3ae
@@ -55,13 +65,26 @@ static void test_cipher_speed(const void *opaque)
Pablo Greco e6a3ae
                                         chunk_size,
Pablo Greco e6a3ae
                                         &err) == 0);
Pablo Greco e6a3ae
         total += chunk_size;
Pablo Greco e6a3ae
-    } while (g_test_timer_elapsed() < 5.0);
Pablo Greco e6a3ae
+    } while (g_test_timer_elapsed() < 1.0);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     total /= MiB;
Pablo Greco e6a3ae
-    g_print("cbc(aes128): ");
Pablo Greco e6a3ae
-    g_print("Testing chunk_size %zu bytes ", chunk_size);
Pablo Greco e6a3ae
-    g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last());
Pablo Greco e6a3ae
-    g_print("%.2f MB/sec\n", total / g_test_timer_last());
Pablo Greco e6a3ae
+    g_print("Enc chunk %zu bytes ", chunk_size);
Pablo Greco e6a3ae
+    g_print("%.2f MB/sec ", total / g_test_timer_last());
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    total = 0.0;
Pablo Greco e6a3ae
+    g_test_timer_start();
Pablo Greco e6a3ae
+    do {
Pablo Greco e6a3ae
+        g_assert(qcrypto_cipher_decrypt(cipher,
Pablo Greco e6a3ae
+                                        plaintext,
Pablo Greco e6a3ae
+                                        ciphertext,
Pablo Greco e6a3ae
+                                        chunk_size,
Pablo Greco e6a3ae
+                                        &err) == 0);
Pablo Greco e6a3ae
+        total += chunk_size;
Pablo Greco e6a3ae
+    } while (g_test_timer_elapsed() < 1.0);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    total /= MiB;
Pablo Greco e6a3ae
+    g_print("Dec chunk %zu bytes ", chunk_size);
Pablo Greco e6a3ae
+    g_print("%.2f MB/sec ", total / g_test_timer_last());
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     qcrypto_cipher_free(cipher);
Pablo Greco e6a3ae
     g_free(plaintext);
Pablo Greco e6a3ae
@@ -70,19 +93,99 @@ static void test_cipher_speed(const void *opaque)
Pablo Greco e6a3ae
     g_free(key);
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-int main(int argc, char **argv)
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static void test_cipher_speed_ecb_aes_128(const void *opaque)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
+    test_cipher_speed(chunk_size,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_MODE_ECB,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_ALG_AES_128);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static void test_cipher_speed_ecb_aes_256(const void *opaque)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
-    size_t i;
Pablo Greco e6a3ae
-    char name[64];
Pablo Greco e6a3ae
+    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
+    test_cipher_speed(chunk_size,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_MODE_ECB,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_ALG_AES_256);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static void test_cipher_speed_cbc_aes_128(const void *opaque)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
+    test_cipher_speed(chunk_size,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_MODE_CBC,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_ALG_AES_128);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
+static void test_cipher_speed_cbc_aes_256(const void *opaque)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
+    test_cipher_speed(chunk_size,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_MODE_CBC,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_ALG_AES_256);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static void test_cipher_speed_ctr_aes_128(const void *opaque)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
+    test_cipher_speed(chunk_size,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_MODE_CTR,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_ALG_AES_128);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static void test_cipher_speed_ctr_aes_256(const void *opaque)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
+    test_cipher_speed(chunk_size,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_MODE_CTR,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_ALG_AES_256);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static void test_cipher_speed_xts_aes_128(const void *opaque)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
+    test_cipher_speed(chunk_size,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_MODE_XTS,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_ALG_AES_128);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static void test_cipher_speed_xts_aes_256(const void *opaque)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    size_t chunk_size = (size_t)opaque;
Pablo Greco e6a3ae
+    test_cipher_speed(chunk_size,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_MODE_XTS,
Pablo Greco e6a3ae
+                      QCRYPTO_CIPHER_ALG_AES_256);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+int main(int argc, char **argv)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
     g_test_init(&argc, &argv, NULL);
Pablo Greco e6a3ae
     g_assert(qcrypto_init(NULL) == 0);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    for (i = 512; i <= 64 * KiB; i *= 2) {
Pablo Greco e6a3ae
-        memset(name, 0 , sizeof(name));
Pablo Greco e6a3ae
-        snprintf(name, sizeof(name), "/crypto/cipher/speed-%zu", i);
Pablo Greco e6a3ae
-        g_test_add_data_func(name, (void *)i, test_cipher_speed);
Pablo Greco e6a3ae
-    }
Pablo Greco e6a3ae
+#define ADD_TEST(mode, cipher, keysize, chunk)                          \
Pablo Greco e6a3ae
+    g_test_add_data_func(                                               \
Pablo Greco e6a3ae
+        "/crypto/cipher/" #mode "-" #cipher "-" #keysize "/chunk-" #chunk, \
Pablo Greco e6a3ae
+        (void *)chunk,                                                  \
Pablo Greco e6a3ae
+        test_cipher_speed_ ## mode ## _ ## cipher ## _ ## keysize)
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+#define ADD_TESTS(chunk)                        \
Pablo Greco e6a3ae
+    do {                                        \
Pablo Greco e6a3ae
+        ADD_TEST(ecb, aes, 128, chunk);         \
Pablo Greco e6a3ae
+        ADD_TEST(ecb, aes, 256, chunk);         \
Pablo Greco e6a3ae
+        ADD_TEST(cbc, aes, 128, chunk);         \
Pablo Greco e6a3ae
+        ADD_TEST(cbc, aes, 256, chunk);         \
Pablo Greco e6a3ae
+        ADD_TEST(ctr, aes, 128, chunk);         \
Pablo Greco e6a3ae
+        ADD_TEST(ctr, aes, 256, chunk);         \
Pablo Greco e6a3ae
+        ADD_TEST(xts, aes, 128, chunk);         \
Pablo Greco e6a3ae
+        ADD_TEST(xts, aes, 256, chunk);         \
Pablo Greco e6a3ae
+    } while (0)
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    ADD_TESTS(512);
Pablo Greco e6a3ae
+    ADD_TESTS(4096);
Pablo Greco e6a3ae
+    ADD_TESTS(16384);
Pablo Greco e6a3ae
+    ADD_TESTS(65536);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     return g_test_run();
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae