yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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