diff mbox series

[1/6] crypto: expand algorithm coverage for cipher benchmark

Message ID 20181009125541.24455-2-berrange@redhat.com (mailing list archive)
State New, archived
Headers show
Series crypto: improve performance of XTS cipher mode | expand

Commit Message

Daniel P. Berrangé Oct. 9, 2018, 12:55 p.m. UTC
Add testing coverage for AES with XTS, ECB and CTR modes

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 tests/benchmark-crypto-cipher.c | 149 +++++++++++++++++++++++++++-----
 1 file changed, 126 insertions(+), 23 deletions(-)

Comments

Marc-André Lureau Oct. 9, 2018, 2:04 p.m. UTC | #1
Hi

On Tue, Oct 9, 2018 at 4:57 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
>
> Add testing coverage for AES with XTS, ECB and CTR modes
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  tests/benchmark-crypto-cipher.c | 149 +++++++++++++++++++++++++++-----

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

>  1 file changed, 126 insertions(+), 23 deletions(-)
>
> diff --git a/tests/benchmark-crypto-cipher.c b/tests/benchmark-crypto-cipher.c
> index f5a0d0bc32..a8325a9510 100644
> --- a/tests/benchmark-crypto-cipher.c
> +++ b/tests/benchmark-crypto-cipher.c
> @@ -15,17 +15,27 @@
>  #include "crypto/init.h"
>  #include "crypto/cipher.h"
>
> -static void test_cipher_speed(const void *opaque)
> +static void test_cipher_speed(size_t chunk_size,
> +                              QCryptoCipherMode mode,
> +                              QCryptoCipherAlgorithm alg)
>  {
>      QCryptoCipher *cipher;
>      Error *err = NULL;
>      double total = 0.0;
> -    size_t chunk_size = (size_t)opaque;
>      uint8_t *key = NULL, *iv = NULL;
>      uint8_t *plaintext = NULL, *ciphertext = NULL;
> -    size_t nkey = qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128);
> -    size_t niv = qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128,
> -                                           QCRYPTO_CIPHER_MODE_CBC);
> +    size_t nkey;
> +    size_t niv;
> +
> +    if (!qcrypto_cipher_supports(alg, mode)) {
> +        return;
> +    }
> +
> +    nkey = qcrypto_cipher_get_key_len(alg);
> +    niv = qcrypto_cipher_get_iv_len(alg, mode);
> +    if (mode == QCRYPTO_CIPHER_MODE_XTS) {
> +        nkey *= 2;
> +    }
>
>      key = g_new0(uint8_t, nkey);
>      memset(key, g_test_rand_int(), nkey);
> @@ -38,14 +48,14 @@ static void test_cipher_speed(const void *opaque)
>      plaintext = g_new0(uint8_t, chunk_size);
>      memset(plaintext, g_test_rand_int(), chunk_size);
>
> -    cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128,
> -                                QCRYPTO_CIPHER_MODE_CBC,
> +    cipher = qcrypto_cipher_new(alg, mode,
>                                  key, nkey, &err);
>      g_assert(cipher != NULL);
>
> -    g_assert(qcrypto_cipher_setiv(cipher,
> -                                  iv, niv,
> -                                  &err) == 0);
> +    if (mode != QCRYPTO_CIPHER_MODE_ECB)
> +        g_assert(qcrypto_cipher_setiv(cipher,
> +                                      iv, niv,
> +                                      &err) == 0);
>
>      g_test_timer_start();
>      do {
> @@ -55,13 +65,26 @@ static void test_cipher_speed(const void *opaque)
>                                          chunk_size,
>                                          &err) == 0);
>          total += chunk_size;
> -    } while (g_test_timer_elapsed() < 5.0);
> +    } while (g_test_timer_elapsed() < 1.0);
>
>      total /= MiB;
> -    g_print("cbc(aes128): ");
> -    g_print("Testing chunk_size %zu bytes ", chunk_size);
> -    g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last());
> -    g_print("%.2f MB/sec\n", total / g_test_timer_last());
> +    g_print("Enc chunk %zu bytes ", chunk_size);
> +    g_print("%.2f MB/sec ", total / g_test_timer_last());
> +
> +    total = 0.0;
> +    g_test_timer_start();
> +    do {
> +        g_assert(qcrypto_cipher_decrypt(cipher,
> +                                        plaintext,
> +                                        ciphertext,
> +                                        chunk_size,
> +                                        &err) == 0);
> +        total += chunk_size;
> +    } while (g_test_timer_elapsed() < 1.0);
> +
> +    total /= MiB;
> +    g_print("Dec chunk %zu bytes ", chunk_size);
> +    g_print("%.2f MB/sec ", total / g_test_timer_last());
>
>      qcrypto_cipher_free(cipher);
>      g_free(plaintext);
> @@ -70,19 +93,99 @@ static void test_cipher_speed(const void *opaque)
>      g_free(key);
>  }
>
> -int main(int argc, char **argv)
> +
> +static void test_cipher_speed_ecb_aes_128(const void *opaque)
> +{
> +    size_t chunk_size = (size_t)opaque;
> +    test_cipher_speed(chunk_size,
> +                      QCRYPTO_CIPHER_MODE_ECB,
> +                      QCRYPTO_CIPHER_ALG_AES_128);
> +}
> +
> +static void test_cipher_speed_ecb_aes_256(const void *opaque)
>  {
> -    size_t i;
> -    char name[64];
> +    size_t chunk_size = (size_t)opaque;
> +    test_cipher_speed(chunk_size,
> +                      QCRYPTO_CIPHER_MODE_ECB,
> +                      QCRYPTO_CIPHER_ALG_AES_256);
> +}
> +
> +static void test_cipher_speed_cbc_aes_128(const void *opaque)
> +{
> +    size_t chunk_size = (size_t)opaque;
> +    test_cipher_speed(chunk_size,
> +                      QCRYPTO_CIPHER_MODE_CBC,
> +                      QCRYPTO_CIPHER_ALG_AES_128);
> +}
>
> +static void test_cipher_speed_cbc_aes_256(const void *opaque)
> +{
> +    size_t chunk_size = (size_t)opaque;
> +    test_cipher_speed(chunk_size,
> +                      QCRYPTO_CIPHER_MODE_CBC,
> +                      QCRYPTO_CIPHER_ALG_AES_256);
> +}
> +
> +static void test_cipher_speed_ctr_aes_128(const void *opaque)
> +{
> +    size_t chunk_size = (size_t)opaque;
> +    test_cipher_speed(chunk_size,
> +                      QCRYPTO_CIPHER_MODE_CTR,
> +                      QCRYPTO_CIPHER_ALG_AES_128);
> +}
> +
> +static void test_cipher_speed_ctr_aes_256(const void *opaque)
> +{
> +    size_t chunk_size = (size_t)opaque;
> +    test_cipher_speed(chunk_size,
> +                      QCRYPTO_CIPHER_MODE_CTR,
> +                      QCRYPTO_CIPHER_ALG_AES_256);
> +}
> +
> +static void test_cipher_speed_xts_aes_128(const void *opaque)
> +{
> +    size_t chunk_size = (size_t)opaque;
> +    test_cipher_speed(chunk_size,
> +                      QCRYPTO_CIPHER_MODE_XTS,
> +                      QCRYPTO_CIPHER_ALG_AES_128);
> +}
> +
> +static void test_cipher_speed_xts_aes_256(const void *opaque)
> +{
> +    size_t chunk_size = (size_t)opaque;
> +    test_cipher_speed(chunk_size,
> +                      QCRYPTO_CIPHER_MODE_XTS,
> +                      QCRYPTO_CIPHER_ALG_AES_256);
> +}
> +
> +
> +int main(int argc, char **argv)
> +{
>      g_test_init(&argc, &argv, NULL);
>      g_assert(qcrypto_init(NULL) == 0);
>
> -    for (i = 512; i <= 64 * KiB; i *= 2) {
> -        memset(name, 0 , sizeof(name));
> -        snprintf(name, sizeof(name), "/crypto/cipher/speed-%zu", i);
> -        g_test_add_data_func(name, (void *)i, test_cipher_speed);
> -    }
> +#define ADD_TEST(mode, cipher, keysize, chunk)                          \
> +    g_test_add_data_func(                                               \
> +        "/crypto/cipher/" #mode "-" #cipher "-" #keysize "/chunk-" #chunk, \
> +        (void *)chunk,                                                  \
> +        test_cipher_speed_ ## mode ## _ ## cipher ## _ ## keysize)
> +
> +#define ADD_TESTS(chunk)                        \
> +    do {                                        \
> +        ADD_TEST(ecb, aes, 128, chunk);         \
> +        ADD_TEST(ecb, aes, 256, chunk);         \
> +        ADD_TEST(cbc, aes, 128, chunk);         \
> +        ADD_TEST(cbc, aes, 256, chunk);         \
> +        ADD_TEST(ctr, aes, 128, chunk);         \
> +        ADD_TEST(ctr, aes, 256, chunk);         \
> +        ADD_TEST(xts, aes, 128, chunk);         \
> +        ADD_TEST(xts, aes, 256, chunk); \
> +    } while (0)
> +
> +    ADD_TESTS(512);
> +    ADD_TESTS(4096);
> +    ADD_TESTS(16384);
> +    ADD_TESTS(65536);
>
>      return g_test_run();
>  }
> --
> 2.17.1
>
>


--
Marc-André Lureau
Alberto Garcia Oct. 10, 2018, 11:45 a.m. UTC | #2
On Tue 09 Oct 2018 02:55:36 PM CEST, Daniel P. Berrangé wrote:
> Add testing coverage for AES with XTS, ECB and CTR modes
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Alberto Garcia <berto@igalia.com>

Berto
diff mbox series

Patch

diff --git a/tests/benchmark-crypto-cipher.c b/tests/benchmark-crypto-cipher.c
index f5a0d0bc32..a8325a9510 100644
--- a/tests/benchmark-crypto-cipher.c
+++ b/tests/benchmark-crypto-cipher.c
@@ -15,17 +15,27 @@ 
 #include "crypto/init.h"
 #include "crypto/cipher.h"
 
-static void test_cipher_speed(const void *opaque)
+static void test_cipher_speed(size_t chunk_size,
+                              QCryptoCipherMode mode,
+                              QCryptoCipherAlgorithm alg)
 {
     QCryptoCipher *cipher;
     Error *err = NULL;
     double total = 0.0;
-    size_t chunk_size = (size_t)opaque;
     uint8_t *key = NULL, *iv = NULL;
     uint8_t *plaintext = NULL, *ciphertext = NULL;
-    size_t nkey = qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128);
-    size_t niv = qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128,
-                                           QCRYPTO_CIPHER_MODE_CBC);
+    size_t nkey;
+    size_t niv;
+
+    if (!qcrypto_cipher_supports(alg, mode)) {
+        return;
+    }
+
+    nkey = qcrypto_cipher_get_key_len(alg);
+    niv = qcrypto_cipher_get_iv_len(alg, mode);
+    if (mode == QCRYPTO_CIPHER_MODE_XTS) {
+        nkey *= 2;
+    }
 
     key = g_new0(uint8_t, nkey);
     memset(key, g_test_rand_int(), nkey);
@@ -38,14 +48,14 @@  static void test_cipher_speed(const void *opaque)
     plaintext = g_new0(uint8_t, chunk_size);
     memset(plaintext, g_test_rand_int(), chunk_size);
 
-    cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128,
-                                QCRYPTO_CIPHER_MODE_CBC,
+    cipher = qcrypto_cipher_new(alg, mode,
                                 key, nkey, &err);
     g_assert(cipher != NULL);
 
-    g_assert(qcrypto_cipher_setiv(cipher,
-                                  iv, niv,
-                                  &err) == 0);
+    if (mode != QCRYPTO_CIPHER_MODE_ECB)
+        g_assert(qcrypto_cipher_setiv(cipher,
+                                      iv, niv,
+                                      &err) == 0);
 
     g_test_timer_start();
     do {
@@ -55,13 +65,26 @@  static void test_cipher_speed(const void *opaque)
                                         chunk_size,
                                         &err) == 0);
         total += chunk_size;
-    } while (g_test_timer_elapsed() < 5.0);
+    } while (g_test_timer_elapsed() < 1.0);
 
     total /= MiB;
-    g_print("cbc(aes128): ");
-    g_print("Testing chunk_size %zu bytes ", chunk_size);
-    g_print("done: %.2f MB in %.2f secs: ", total, g_test_timer_last());
-    g_print("%.2f MB/sec\n", total / g_test_timer_last());
+    g_print("Enc chunk %zu bytes ", chunk_size);
+    g_print("%.2f MB/sec ", total / g_test_timer_last());
+
+    total = 0.0;
+    g_test_timer_start();
+    do {
+        g_assert(qcrypto_cipher_decrypt(cipher,
+                                        plaintext,
+                                        ciphertext,
+                                        chunk_size,
+                                        &err) == 0);
+        total += chunk_size;
+    } while (g_test_timer_elapsed() < 1.0);
+
+    total /= MiB;
+    g_print("Dec chunk %zu bytes ", chunk_size);
+    g_print("%.2f MB/sec ", total / g_test_timer_last());
 
     qcrypto_cipher_free(cipher);
     g_free(plaintext);
@@ -70,19 +93,99 @@  static void test_cipher_speed(const void *opaque)
     g_free(key);
 }
 
-int main(int argc, char **argv)
+
+static void test_cipher_speed_ecb_aes_128(const void *opaque)
+{
+    size_t chunk_size = (size_t)opaque;
+    test_cipher_speed(chunk_size,
+                      QCRYPTO_CIPHER_MODE_ECB,
+                      QCRYPTO_CIPHER_ALG_AES_128);
+}
+
+static void test_cipher_speed_ecb_aes_256(const void *opaque)
 {
-    size_t i;
-    char name[64];
+    size_t chunk_size = (size_t)opaque;
+    test_cipher_speed(chunk_size,
+                      QCRYPTO_CIPHER_MODE_ECB,
+                      QCRYPTO_CIPHER_ALG_AES_256);
+}
+
+static void test_cipher_speed_cbc_aes_128(const void *opaque)
+{
+    size_t chunk_size = (size_t)opaque;
+    test_cipher_speed(chunk_size,
+                      QCRYPTO_CIPHER_MODE_CBC,
+                      QCRYPTO_CIPHER_ALG_AES_128);
+}
 
+static void test_cipher_speed_cbc_aes_256(const void *opaque)
+{
+    size_t chunk_size = (size_t)opaque;
+    test_cipher_speed(chunk_size,
+                      QCRYPTO_CIPHER_MODE_CBC,
+                      QCRYPTO_CIPHER_ALG_AES_256);
+}
+
+static void test_cipher_speed_ctr_aes_128(const void *opaque)
+{
+    size_t chunk_size = (size_t)opaque;
+    test_cipher_speed(chunk_size,
+                      QCRYPTO_CIPHER_MODE_CTR,
+                      QCRYPTO_CIPHER_ALG_AES_128);
+}
+
+static void test_cipher_speed_ctr_aes_256(const void *opaque)
+{
+    size_t chunk_size = (size_t)opaque;
+    test_cipher_speed(chunk_size,
+                      QCRYPTO_CIPHER_MODE_CTR,
+                      QCRYPTO_CIPHER_ALG_AES_256);
+}
+
+static void test_cipher_speed_xts_aes_128(const void *opaque)
+{
+    size_t chunk_size = (size_t)opaque;
+    test_cipher_speed(chunk_size,
+                      QCRYPTO_CIPHER_MODE_XTS,
+                      QCRYPTO_CIPHER_ALG_AES_128);
+}
+
+static void test_cipher_speed_xts_aes_256(const void *opaque)
+{
+    size_t chunk_size = (size_t)opaque;
+    test_cipher_speed(chunk_size,
+                      QCRYPTO_CIPHER_MODE_XTS,
+                      QCRYPTO_CIPHER_ALG_AES_256);
+}
+
+
+int main(int argc, char **argv)
+{
     g_test_init(&argc, &argv, NULL);
     g_assert(qcrypto_init(NULL) == 0);
 
-    for (i = 512; i <= 64 * KiB; i *= 2) {
-        memset(name, 0 , sizeof(name));
-        snprintf(name, sizeof(name), "/crypto/cipher/speed-%zu", i);
-        g_test_add_data_func(name, (void *)i, test_cipher_speed);
-    }
+#define ADD_TEST(mode, cipher, keysize, chunk)                          \
+    g_test_add_data_func(                                               \
+        "/crypto/cipher/" #mode "-" #cipher "-" #keysize "/chunk-" #chunk, \
+        (void *)chunk,                                                  \
+        test_cipher_speed_ ## mode ## _ ## cipher ## _ ## keysize)
+
+#define ADD_TESTS(chunk)                        \
+    do {                                        \
+        ADD_TEST(ecb, aes, 128, chunk);         \
+        ADD_TEST(ecb, aes, 256, chunk);         \
+        ADD_TEST(cbc, aes, 128, chunk);         \
+        ADD_TEST(cbc, aes, 256, chunk);         \
+        ADD_TEST(ctr, aes, 128, chunk);         \
+        ADD_TEST(ctr, aes, 256, chunk);         \
+        ADD_TEST(xts, aes, 128, chunk);         \
+        ADD_TEST(xts, aes, 256, chunk); \
+    } while (0)
+
+    ADD_TESTS(512);
+    ADD_TESTS(4096);
+    ADD_TESTS(16384);
+    ADD_TESTS(65536);
 
     return g_test_run();
 }