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

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