yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

b38b0f
From 9f7d3aa69a0ab745301f6811df498e7aa2a51a26 Mon Sep 17 00:00:00 2001
b38b0f
From: "Daniel P. Berrange" <berrange@redhat.com>
b38b0f
Date: Wed, 24 Apr 2019 09:56:36 +0100
b38b0f
Subject: [PATCH 2/9] crypto: expand algorithm coverage for cipher benchmark
b38b0f
MIME-Version: 1.0
b38b0f
Content-Type: text/plain; charset=UTF-8
b38b0f
Content-Transfer-Encoding: 8bit
b38b0f
b38b0f
RH-Author: Daniel P. Berrange <berrange@redhat.com>
b38b0f
Message-id: <20190424095643.796-3-berrange@redhat.com>
b38b0f
Patchwork-id: 85880
b38b0f
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 2/9] crypto: expand algorithm coverage for cipher benchmark
b38b0f
Bugzilla: 1680231
b38b0f
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
b38b0f
RH-Acked-by: John Snow <jsnow@redhat.com>
b38b0f
RH-Acked-by: Eric Blake <eblake@redhat.com>
b38b0f
b38b0f
Add testing coverage for AES with XTS, ECB and CTR modes
b38b0f
b38b0f
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
b38b0f
Reviewed-by: Alberto Garcia <berto@igalia.com>
b38b0f
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
b38b0f
(cherry picked from commit a9e08155bd4ae096688a8ff19669023ab0fbc170)
b38b0f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
b38b0f
---
b38b0f
 tests/benchmark-crypto-cipher.c | 149 +++++++++++++++++++++++++++++++++-------
b38b0f
 1 file changed, 126 insertions(+), 23 deletions(-)
b38b0f
b38b0f
diff --git a/tests/benchmark-crypto-cipher.c b/tests/benchmark-crypto-cipher.c
b38b0f
index f5a0d0b..67fdf8c 100644
b38b0f
--- a/tests/benchmark-crypto-cipher.c
b38b0f
+++ b/tests/benchmark-crypto-cipher.c
b38b0f
@@ -15,17 +15,27 @@
b38b0f
 #include "crypto/init.h"
b38b0f
 #include "crypto/cipher.h"
b38b0f
 
b38b0f
-static void test_cipher_speed(const void *opaque)
b38b0f
+static void test_cipher_speed(size_t chunk_size,
b38b0f
+                              QCryptoCipherMode mode,
b38b0f
+                              QCryptoCipherAlgorithm alg)
b38b0f
 {
b38b0f
     QCryptoCipher *cipher;
b38b0f
     Error *err = NULL;
b38b0f
     double total = 0.0;
b38b0f
-    size_t chunk_size = (size_t)opaque;
b38b0f
     uint8_t *key = NULL, *iv = NULL;
b38b0f
     uint8_t *plaintext = NULL, *ciphertext = NULL;
b38b0f
-    size_t nkey = qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128);
b38b0f
-    size_t niv = qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128,
b38b0f
-                                           QCRYPTO_CIPHER_MODE_CBC);
b38b0f
+    size_t nkey;
b38b0f
+    size_t niv;
b38b0f
+
b38b0f
+    if (!qcrypto_cipher_supports(alg, mode)) {
b38b0f
+        return;
b38b0f
+    }
b38b0f
+
b38b0f
+    nkey = qcrypto_cipher_get_key_len(alg);
b38b0f
+    niv = qcrypto_cipher_get_iv_len(alg, mode);
b38b0f
+    if (mode == QCRYPTO_CIPHER_MODE_XTS) {
b38b0f
+        nkey *= 2;
b38b0f
+    }
b38b0f
 
b38b0f
     key = g_new0(uint8_t, nkey);
b38b0f
     memset(key, g_test_rand_int(), nkey);
b38b0f
@@ -38,14 +48,14 @@ static void test_cipher_speed(const void *opaque)
b38b0f
     plaintext = g_new0(uint8_t, chunk_size);
b38b0f
     memset(plaintext, g_test_rand_int(), chunk_size);
b38b0f
 
b38b0f
-    cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128,
b38b0f
-                                QCRYPTO_CIPHER_MODE_CBC,
b38b0f
+    cipher = qcrypto_cipher_new(alg, mode,
b38b0f
                                 key, nkey, &err;;
b38b0f
     g_assert(cipher != NULL);
b38b0f
 
b38b0f
-    g_assert(qcrypto_cipher_setiv(cipher,
b38b0f
-                                  iv, niv,
b38b0f
-                                  &err) == 0);
b38b0f
+    if (mode != QCRYPTO_CIPHER_MODE_ECB)
b38b0f
+        g_assert(qcrypto_cipher_setiv(cipher,
b38b0f
+                                      iv, niv,
b38b0f
+                                      &err) == 0);
b38b0f
 
b38b0f
     g_test_timer_start();
b38b0f
     do {
b38b0f
@@ -55,13 +65,26 @@ static void test_cipher_speed(const void *opaque)
b38b0f
                                         chunk_size,
b38b0f
                                         &err) == 0);
b38b0f
         total += chunk_size;
b38b0f
-    } while (g_test_timer_elapsed() < 5.0);
b38b0f
+    } while (g_test_timer_elapsed() < 1.0);
b38b0f
 
b38b0f
     total /= MiB;
b38b0f
-    g_print("cbc(aes128): ");
b38b0f
-    g_print("Testing chunk_size %zu bytes ", chunk_size);
b38b0f
-    g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last());
b38b0f
-    g_print("%.2f MB/sec\n", total / g_test_timer_last());
b38b0f
+    g_print("Enc chunk %zu bytes ", chunk_size);
b38b0f
+    g_print("%.2f MB/sec ", total / g_test_timer_last());
b38b0f
+
b38b0f
+    total = 0.0;
b38b0f
+    g_test_timer_start();
b38b0f
+    do {
b38b0f
+        g_assert(qcrypto_cipher_decrypt(cipher,
b38b0f
+                                        plaintext,
b38b0f
+                                        ciphertext,
b38b0f
+                                        chunk_size,
b38b0f
+                                        &err) == 0);
b38b0f
+        total += chunk_size;
b38b0f
+    } while (g_test_timer_elapsed() < 1.0);
b38b0f
+
b38b0f
+    total /= MiB;
b38b0f
+    g_print("Dec chunk %zu bytes ", chunk_size);
b38b0f
+    g_print("%.2f MB/sec ", total / g_test_timer_last());
b38b0f
 
b38b0f
     qcrypto_cipher_free(cipher);
b38b0f
     g_free(plaintext);
b38b0f
@@ -70,19 +93,99 @@ static void test_cipher_speed(const void *opaque)
b38b0f
     g_free(key);
b38b0f
 }
b38b0f
 
b38b0f
-int main(int argc, char **argv)
b38b0f
+
b38b0f
+static void test_cipher_speed_ecb_aes_128(const void *opaque)
b38b0f
+{
b38b0f
+    size_t chunk_size = (size_t)opaque;
b38b0f
+    test_cipher_speed(chunk_size,
b38b0f
+                      QCRYPTO_CIPHER_MODE_ECB,
b38b0f
+                      QCRYPTO_CIPHER_ALG_AES_128);
b38b0f
+}
b38b0f
+
b38b0f
+static void test_cipher_speed_ecb_aes_256(const void *opaque)
b38b0f
 {
b38b0f
-    size_t i;
b38b0f
-    char name[64];
b38b0f
+    size_t chunk_size = (size_t)opaque;
b38b0f
+    test_cipher_speed(chunk_size,
b38b0f
+                      QCRYPTO_CIPHER_MODE_ECB,
b38b0f
+                      QCRYPTO_CIPHER_ALG_AES_256);
b38b0f
+}
b38b0f
+
b38b0f
+static void test_cipher_speed_cbc_aes_128(const void *opaque)
b38b0f
+{
b38b0f
+    size_t chunk_size = (size_t)opaque;
b38b0f
+    test_cipher_speed(chunk_size,
b38b0f
+                      QCRYPTO_CIPHER_MODE_CBC,
b38b0f
+                      QCRYPTO_CIPHER_ALG_AES_128);
b38b0f
+}
b38b0f
 
b38b0f
+static void test_cipher_speed_cbc_aes_256(const void *opaque)
b38b0f
+{
b38b0f
+    size_t chunk_size = (size_t)opaque;
b38b0f
+    test_cipher_speed(chunk_size,
b38b0f
+                      QCRYPTO_CIPHER_MODE_CBC,
b38b0f
+                      QCRYPTO_CIPHER_ALG_AES_256);
b38b0f
+}
b38b0f
+
b38b0f
+static void test_cipher_speed_ctr_aes_128(const void *opaque)
b38b0f
+{
b38b0f
+    size_t chunk_size = (size_t)opaque;
b38b0f
+    test_cipher_speed(chunk_size,
b38b0f
+                      QCRYPTO_CIPHER_MODE_CTR,
b38b0f
+                      QCRYPTO_CIPHER_ALG_AES_128);
b38b0f
+}
b38b0f
+
b38b0f
+static void test_cipher_speed_ctr_aes_256(const void *opaque)
b38b0f
+{
b38b0f
+    size_t chunk_size = (size_t)opaque;
b38b0f
+    test_cipher_speed(chunk_size,
b38b0f
+                      QCRYPTO_CIPHER_MODE_CTR,
b38b0f
+                      QCRYPTO_CIPHER_ALG_AES_256);
b38b0f
+}
b38b0f
+
b38b0f
+static void test_cipher_speed_xts_aes_128(const void *opaque)
b38b0f
+{
b38b0f
+    size_t chunk_size = (size_t)opaque;
b38b0f
+    test_cipher_speed(chunk_size,
b38b0f
+                      QCRYPTO_CIPHER_MODE_XTS,
b38b0f
+                      QCRYPTO_CIPHER_ALG_AES_128);
b38b0f
+}
b38b0f
+
b38b0f
+static void test_cipher_speed_xts_aes_256(const void *opaque)
b38b0f
+{
b38b0f
+    size_t chunk_size = (size_t)opaque;
b38b0f
+    test_cipher_speed(chunk_size,
b38b0f
+                      QCRYPTO_CIPHER_MODE_XTS,
b38b0f
+                      QCRYPTO_CIPHER_ALG_AES_256);
b38b0f
+}
b38b0f
+
b38b0f
+
b38b0f
+int main(int argc, char **argv)
b38b0f
+{
b38b0f
     g_test_init(&argc, &argv, NULL);
b38b0f
     g_assert(qcrypto_init(NULL) == 0);
b38b0f
 
b38b0f
-    for (i = 512; i <= 64 * KiB; i *= 2) {
b38b0f
-        memset(name, 0 , sizeof(name));
b38b0f
-        snprintf(name, sizeof(name), "/crypto/cipher/speed-%zu", i);
b38b0f
-        g_test_add_data_func(name, (void *)i, test_cipher_speed);
b38b0f
-    }
b38b0f
+#define ADD_TEST(mode, cipher, keysize, chunk)                          \
b38b0f
+    g_test_add_data_func(                                               \
b38b0f
+        "/crypto/cipher/" #mode "-" #cipher "-" #keysize "/chunk-" #chunk, \
b38b0f
+        (void *)chunk,                                                  \
b38b0f
+        test_cipher_speed_ ## mode ## _ ## cipher ## _ ## keysize)
b38b0f
+
b38b0f
+#define ADD_TESTS(chunk)                        \
b38b0f
+    do {                                        \
b38b0f
+        ADD_TEST(ecb, aes, 128, chunk);         \
b38b0f
+        ADD_TEST(ecb, aes, 256, chunk);         \
b38b0f
+        ADD_TEST(cbc, aes, 128, chunk);         \
b38b0f
+        ADD_TEST(cbc, aes, 256, chunk);         \
b38b0f
+        ADD_TEST(ctr, aes, 128, chunk);         \
b38b0f
+        ADD_TEST(ctr, aes, 256, chunk);         \
b38b0f
+        ADD_TEST(xts, aes, 128, chunk);         \
b38b0f
+        ADD_TEST(xts, aes, 256, chunk);         \
b38b0f
+    } while (0)
b38b0f
+
b38b0f
+    ADD_TESTS(512);
b38b0f
+    ADD_TESTS(4096);
b38b0f
+    ADD_TESTS(16384);
b38b0f
+    ADD_TESTS(65536);
b38b0f
 
b38b0f
     return g_test_run();
b38b0f
 }
b38b0f
-- 
b38b0f
1.8.3.1
b38b0f