All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: zhenwei pi <pizhenwei@bytedance.com>
Cc: mst@redhat.com, arei.gonglei@huawei.com, qemu-devel@nongnu.org,
	virtualization@lists.linux-foundation.org,
	linux-crypto@vger.kernel.org, helei.sig11@bytedance.com,
	jasowang@redhat.com, cohuck@redhat.com
Subject: Re: [PATCH v6 7/9] test/crypto: Add test suite for crypto akcipher
Date: Mon, 23 May 2022 10:45:19 +0100	[thread overview]
Message-ID: <YotXr4K39xsiS//O@redhat.com> (raw)
In-Reply-To: <20220514005504.1042884-8-pizhenwei@bytedance.com>

On Sat, May 14, 2022 at 08:55:02AM +0800, zhenwei pi wrote:
> From: Lei He <helei.sig11@bytedance.com>
> 
> Add unit test and benchmark test for crypto akcipher.
> 
> Signed-off-by: lei he <helei.sig11@bytedance.com>
> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  tests/bench/benchmark-crypto-akcipher.c | 157 ++++++
>  tests/bench/meson.build                 |   1 +
>  tests/bench/test_akcipher_keys.inc      | 537 ++++++++++++++++++
>  tests/unit/meson.build                  |   1 +
>  tests/unit/test-crypto-akcipher.c       | 711 ++++++++++++++++++++++++
>  5 files changed, 1407 insertions(+)
>  create mode 100644 tests/bench/benchmark-crypto-akcipher.c
>  create mode 100644 tests/bench/test_akcipher_keys.inc
>  create mode 100644 tests/unit/test-crypto-akcipher.c
> 
> diff --git a/tests/bench/benchmark-crypto-akcipher.c b/tests/bench/benchmark-crypto-akcipher.c
> new file mode 100644
> index 0000000000..c6c80c0be1
> --- /dev/null
> +++ b/tests/bench/benchmark-crypto-akcipher.c
> @@ -0,0 +1,157 @@
> +/*
> + * QEMU Crypto akcipher speed benchmark
> + *
> + * Copyright (c) 2022 Bytedance
> + *
> + * Authors:
> + *    lei he <helei.sig11@bytedance.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or
> + * (at your option) any later version.  See the COPYING file in the
> + * top-level directory.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "crypto/init.h"
> +#include "crypto/akcipher.h"
> +#include "standard-headers/linux/virtio_crypto.h"
> +
> +#include "test_akcipher_keys.inc"
> +
> +static bool keep_running;
> +
> +static void alarm_handler(int sig)
> +{
> +    keep_running = false;
> +}
> +
> +static QCryptoAkCipher *create_rsa_akcipher(const uint8_t *priv_key,
> +                                            size_t keylen,
> +                                            QCryptoRSAPaddingAlgorithm padding,
> +                                            QCryptoHashAlgorithm hash)
> +{
> +    QCryptoAkCipherOptions opt;
> +    QCryptoAkCipher *rsa;
> +
> +    opt.alg = QCRYPTO_AKCIPHER_ALG_RSA;
> +    opt.u.rsa.padding_alg = padding;
> +    opt.u.rsa.hash_alg = hash;
> +    rsa = qcrypto_akcipher_new(&opt, QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE,
> +                               priv_key, keylen, &error_abort);
> +    return rsa;
> +}
> +
> +static void test_rsa_speed(const uint8_t *priv_key, size_t keylen,
> +                           size_t key_size)
> +{
> +#define BYTE 8
> +#define SHA1_DGST_LEN 20
> +#define DURATION_SECONDS 10
> +#define PADDING QCRYPTO_RSA_PADDING_ALG_PKCS1
> +#define HASH QCRYPTO_HASH_ALG_SHA1
> +
> +    g_autoptr(QCryptoAkCipher) rsa =
> +        create_rsa_akcipher(priv_key, keylen, PADDING, HASH);
> +    g_autofree uint8_t *dgst = NULL;
> +    g_autofree uint8_t *signature = NULL;
> +    size_t count;
> +
> +    dgst = g_new0(uint8_t, SHA1_DGST_LEN);
> +    memset(dgst, g_test_rand_int(), SHA1_DGST_LEN);
> +    signature = g_new0(uint8_t, key_size / BYTE);
> +
> +    g_test_message("benchmark rsa%lu (%s-%s) sign in %d seconds", key_size,
> +                   QCryptoRSAPaddingAlgorithm_str(PADDING),
> +                   QCryptoHashAlgorithm_str(HASH),
> +                   DURATION_SECONDS);

Needs to be '%zu' here and several other places in this file for any
parameter which is 'size_t'.

> +    alarm(DURATION_SECONDS);
> +    g_test_timer_start();
> +    for (keep_running = true, count = 0; keep_running; ++count) {
> +        g_assert(qcrypto_akcipher_sign(rsa, dgst, SHA1_DGST_LEN,
> +                                       signature, key_size / BYTE,
> +                                       &error_abort) > 0);
> +    }
> +    g_test_timer_elapsed();
> +    g_test_message("rsa%lu (%s-%s) sign %lu times in %.2f seconds,"
> +                   " %.2f times/sec ",
> +                   key_size,  QCryptoRSAPaddingAlgorithm_str(PADDING),
> +                   QCryptoHashAlgorithm_str(HASH),
> +                   count, g_test_timer_last(),
> +                   (double)count / g_test_timer_last());
> +
> +    g_test_message("benchmark rsa%lu (%s-%s) verify in %d seconds", key_size,
> +                   QCryptoRSAPaddingAlgorithm_str(PADDING),
> +                   QCryptoHashAlgorithm_str(HASH),
> +                   DURATION_SECONDS);
> +    alarm(DURATION_SECONDS);
> +    g_test_timer_start();
> +    for (keep_running = true, count = 0; keep_running; ++count) {
> +        g_assert(qcrypto_akcipher_verify(rsa, signature, key_size / BYTE,
> +                                         dgst, SHA1_DGST_LEN,
> +                                         &error_abort) == 0);
> +    }
> +    g_test_timer_elapsed();
> +    g_test_message("rsa%lu (%s-%s) verify %lu times in %.2f seconds,"
> +                   " %.2f times/sec ",
> +                   key_size, QCryptoRSAPaddingAlgorithm_str(PADDING),
> +                   QCryptoHashAlgorithm_str(HASH),
> +                   count, g_test_timer_last(),
> +                   (double)count / g_test_timer_last());
> +}
> +
> +static void test_rsa_1024_speed(const void *opaque)
> +{
> +    size_t key_size = (size_t)opaque;
> +    test_rsa_speed(rsa1024_priv_key, sizeof(rsa1024_priv_key), key_size);
> +}
> +
> +static void test_rsa_2048_speed(const void *opaque)
> +{
> +    size_t key_size = (size_t)opaque;
> +    test_rsa_speed(rsa2048_priv_key, sizeof(rsa2048_priv_key), key_size);
> +}
> +
> +static void test_rsa_4096_speed(const void *opaque)
> +{
> +    size_t key_size = (size_t)opaque;
> +    test_rsa_speed(rsa4096_priv_key, sizeof(rsa4096_priv_key), key_size);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    char *alg = NULL;
> +    char *size = NULL;
> +    g_test_init(&argc, &argv, NULL);
> +    g_assert(qcrypto_init(NULL) == 0);
> +    struct sigaction new_action, old_action;
> +
> +    new_action.sa_handler = alarm_handler;
> +
> +    /* Set up the structure to specify the new action. */
> +    sigemptyset(&new_action.sa_mask);
> +    new_action.sa_flags = 0;
> +    sigaction(SIGALRM, NULL, &old_action);
> +    g_assert(old_action.sa_handler != SIG_IGN);
> +    sigaction(SIGALRM, &new_action, NULL);

sigaction doesn't exist on Windows so this fails to compile.
I'd suggest processing a constant amount of data, as the other
benchmark programs do, rather than trying to run for a constant
amount of time.

> +
> +#define ADD_TEST(asym_alg, keysize)                    \
> +    if ((!alg || g_str_equal(alg, #asym_alg)) &&       \
> +        (!size || g_str_equal(size, #keysize)))        \
> +        g_test_add_data_func(                          \
> +        "/crypto/akcipher/" #asym_alg "-" #keysize,    \
> +        (void *)keysize,                               \
> +        test_ ## asym_alg ## _ ## keysize ## _speed)
> +
> +    if (argc >= 2) {
> +        alg = argv[1];
> +    }
> +    if (argc >= 3) {
> +        size = argv[2];
> +    }
> +
> +    ADD_TEST(rsa, 1024);
> +    ADD_TEST(rsa, 2048);
> +    ADD_TEST(rsa, 4096);
> +
> +    return g_test_run();
> +}


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


WARNING: multiple messages have this Message-ID (diff)
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: zhenwei pi <pizhenwei@bytedance.com>
Cc: helei.sig11@bytedance.com, mst@redhat.com, cohuck@redhat.com,
	qemu-devel@nongnu.org, virtualization@lists.linux-foundation.org,
	linux-crypto@vger.kernel.org
Subject: Re: [PATCH v6 7/9] test/crypto: Add test suite for crypto akcipher
Date: Mon, 23 May 2022 10:45:19 +0100	[thread overview]
Message-ID: <YotXr4K39xsiS//O@redhat.com> (raw)
In-Reply-To: <20220514005504.1042884-8-pizhenwei@bytedance.com>

On Sat, May 14, 2022 at 08:55:02AM +0800, zhenwei pi wrote:
> From: Lei He <helei.sig11@bytedance.com>
> 
> Add unit test and benchmark test for crypto akcipher.
> 
> Signed-off-by: lei he <helei.sig11@bytedance.com>
> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  tests/bench/benchmark-crypto-akcipher.c | 157 ++++++
>  tests/bench/meson.build                 |   1 +
>  tests/bench/test_akcipher_keys.inc      | 537 ++++++++++++++++++
>  tests/unit/meson.build                  |   1 +
>  tests/unit/test-crypto-akcipher.c       | 711 ++++++++++++++++++++++++
>  5 files changed, 1407 insertions(+)
>  create mode 100644 tests/bench/benchmark-crypto-akcipher.c
>  create mode 100644 tests/bench/test_akcipher_keys.inc
>  create mode 100644 tests/unit/test-crypto-akcipher.c
> 
> diff --git a/tests/bench/benchmark-crypto-akcipher.c b/tests/bench/benchmark-crypto-akcipher.c
> new file mode 100644
> index 0000000000..c6c80c0be1
> --- /dev/null
> +++ b/tests/bench/benchmark-crypto-akcipher.c
> @@ -0,0 +1,157 @@
> +/*
> + * QEMU Crypto akcipher speed benchmark
> + *
> + * Copyright (c) 2022 Bytedance
> + *
> + * Authors:
> + *    lei he <helei.sig11@bytedance.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or
> + * (at your option) any later version.  See the COPYING file in the
> + * top-level directory.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "crypto/init.h"
> +#include "crypto/akcipher.h"
> +#include "standard-headers/linux/virtio_crypto.h"
> +
> +#include "test_akcipher_keys.inc"
> +
> +static bool keep_running;
> +
> +static void alarm_handler(int sig)
> +{
> +    keep_running = false;
> +}
> +
> +static QCryptoAkCipher *create_rsa_akcipher(const uint8_t *priv_key,
> +                                            size_t keylen,
> +                                            QCryptoRSAPaddingAlgorithm padding,
> +                                            QCryptoHashAlgorithm hash)
> +{
> +    QCryptoAkCipherOptions opt;
> +    QCryptoAkCipher *rsa;
> +
> +    opt.alg = QCRYPTO_AKCIPHER_ALG_RSA;
> +    opt.u.rsa.padding_alg = padding;
> +    opt.u.rsa.hash_alg = hash;
> +    rsa = qcrypto_akcipher_new(&opt, QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE,
> +                               priv_key, keylen, &error_abort);
> +    return rsa;
> +}
> +
> +static void test_rsa_speed(const uint8_t *priv_key, size_t keylen,
> +                           size_t key_size)
> +{
> +#define BYTE 8
> +#define SHA1_DGST_LEN 20
> +#define DURATION_SECONDS 10
> +#define PADDING QCRYPTO_RSA_PADDING_ALG_PKCS1
> +#define HASH QCRYPTO_HASH_ALG_SHA1
> +
> +    g_autoptr(QCryptoAkCipher) rsa =
> +        create_rsa_akcipher(priv_key, keylen, PADDING, HASH);
> +    g_autofree uint8_t *dgst = NULL;
> +    g_autofree uint8_t *signature = NULL;
> +    size_t count;
> +
> +    dgst = g_new0(uint8_t, SHA1_DGST_LEN);
> +    memset(dgst, g_test_rand_int(), SHA1_DGST_LEN);
> +    signature = g_new0(uint8_t, key_size / BYTE);
> +
> +    g_test_message("benchmark rsa%lu (%s-%s) sign in %d seconds", key_size,
> +                   QCryptoRSAPaddingAlgorithm_str(PADDING),
> +                   QCryptoHashAlgorithm_str(HASH),
> +                   DURATION_SECONDS);

Needs to be '%zu' here and several other places in this file for any
parameter which is 'size_t'.

> +    alarm(DURATION_SECONDS);
> +    g_test_timer_start();
> +    for (keep_running = true, count = 0; keep_running; ++count) {
> +        g_assert(qcrypto_akcipher_sign(rsa, dgst, SHA1_DGST_LEN,
> +                                       signature, key_size / BYTE,
> +                                       &error_abort) > 0);
> +    }
> +    g_test_timer_elapsed();
> +    g_test_message("rsa%lu (%s-%s) sign %lu times in %.2f seconds,"
> +                   " %.2f times/sec ",
> +                   key_size,  QCryptoRSAPaddingAlgorithm_str(PADDING),
> +                   QCryptoHashAlgorithm_str(HASH),
> +                   count, g_test_timer_last(),
> +                   (double)count / g_test_timer_last());
> +
> +    g_test_message("benchmark rsa%lu (%s-%s) verify in %d seconds", key_size,
> +                   QCryptoRSAPaddingAlgorithm_str(PADDING),
> +                   QCryptoHashAlgorithm_str(HASH),
> +                   DURATION_SECONDS);
> +    alarm(DURATION_SECONDS);
> +    g_test_timer_start();
> +    for (keep_running = true, count = 0; keep_running; ++count) {
> +        g_assert(qcrypto_akcipher_verify(rsa, signature, key_size / BYTE,
> +                                         dgst, SHA1_DGST_LEN,
> +                                         &error_abort) == 0);
> +    }
> +    g_test_timer_elapsed();
> +    g_test_message("rsa%lu (%s-%s) verify %lu times in %.2f seconds,"
> +                   " %.2f times/sec ",
> +                   key_size, QCryptoRSAPaddingAlgorithm_str(PADDING),
> +                   QCryptoHashAlgorithm_str(HASH),
> +                   count, g_test_timer_last(),
> +                   (double)count / g_test_timer_last());
> +}
> +
> +static void test_rsa_1024_speed(const void *opaque)
> +{
> +    size_t key_size = (size_t)opaque;
> +    test_rsa_speed(rsa1024_priv_key, sizeof(rsa1024_priv_key), key_size);
> +}
> +
> +static void test_rsa_2048_speed(const void *opaque)
> +{
> +    size_t key_size = (size_t)opaque;
> +    test_rsa_speed(rsa2048_priv_key, sizeof(rsa2048_priv_key), key_size);
> +}
> +
> +static void test_rsa_4096_speed(const void *opaque)
> +{
> +    size_t key_size = (size_t)opaque;
> +    test_rsa_speed(rsa4096_priv_key, sizeof(rsa4096_priv_key), key_size);
> +}
> +
> +int main(int argc, char **argv)
> +{
> +    char *alg = NULL;
> +    char *size = NULL;
> +    g_test_init(&argc, &argv, NULL);
> +    g_assert(qcrypto_init(NULL) == 0);
> +    struct sigaction new_action, old_action;
> +
> +    new_action.sa_handler = alarm_handler;
> +
> +    /* Set up the structure to specify the new action. */
> +    sigemptyset(&new_action.sa_mask);
> +    new_action.sa_flags = 0;
> +    sigaction(SIGALRM, NULL, &old_action);
> +    g_assert(old_action.sa_handler != SIG_IGN);
> +    sigaction(SIGALRM, &new_action, NULL);

sigaction doesn't exist on Windows so this fails to compile.
I'd suggest processing a constant amount of data, as the other
benchmark programs do, rather than trying to run for a constant
amount of time.

> +
> +#define ADD_TEST(asym_alg, keysize)                    \
> +    if ((!alg || g_str_equal(alg, #asym_alg)) &&       \
> +        (!size || g_str_equal(size, #keysize)))        \
> +        g_test_add_data_func(                          \
> +        "/crypto/akcipher/" #asym_alg "-" #keysize,    \
> +        (void *)keysize,                               \
> +        test_ ## asym_alg ## _ ## keysize ## _speed)
> +
> +    if (argc >= 2) {
> +        alg = argv[1];
> +    }
> +    if (argc >= 3) {
> +        size = argv[2];
> +    }
> +
> +    ADD_TEST(rsa, 1024);
> +    ADD_TEST(rsa, 2048);
> +    ADD_TEST(rsa, 4096);
> +
> +    return g_test_run();
> +}


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

  reply	other threads:[~2022-05-23  9:45 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-14  0:54 [PATCH v6 0/9] Introduce akcipher service for virtio-crypto zhenwei pi
2022-05-14  0:54 ` zhenwei pi
2022-05-14  0:54 ` [PATCH v6 1/9] virtio-crypto: header update zhenwei pi
2022-05-14  0:54   ` zhenwei pi
2022-05-14  0:54 ` [PATCH v6 2/9] qapi: crypto-akcipher: Introduce akcipher types to qapi zhenwei pi
2022-05-14  0:54   ` zhenwei pi
2022-05-14  0:54 ` [PATCH v6 3/9] crypto: Introduce akcipher crypto class zhenwei pi
2022-05-14  0:54   ` zhenwei pi
2022-05-14  0:54 ` [PATCH v6 4/9] crypto: add ASN.1 DER decoder zhenwei pi
2022-05-14  0:54   ` zhenwei pi
2022-05-23  9:46   ` Daniel P. Berrangé
2022-05-23  9:46     ` Daniel P. Berrangé
2022-05-14  0:55 ` [PATCH v6 5/9] crypto: Implement RSA algorithm by hogweed zhenwei pi
2022-05-14  0:55   ` zhenwei pi
2022-05-23  9:41   ` Daniel P. Berrangé
2022-05-23  9:41     ` Daniel P. Berrangé
2022-05-23  9:43   ` Daniel P. Berrangé
2022-05-23  9:43     ` Daniel P. Berrangé
2022-05-14  0:55 ` [PATCH v6 6/9] crypto: Implement RSA algorithm by gcrypt zhenwei pi
2022-05-14  0:55   ` zhenwei pi
2022-05-23  9:40   ` Daniel P. Berrangé
2022-05-23  9:40     ` Daniel P. Berrangé
2022-05-14  0:55 ` [PATCH v6 7/9] test/crypto: Add test suite for crypto akcipher zhenwei pi
2022-05-14  0:55   ` zhenwei pi
2022-05-23  9:45   ` Daniel P. Berrangé [this message]
2022-05-23  9:45     ` Daniel P. Berrangé
2022-05-14  0:55 ` [PATCH v6 8/9] tests/crypto: Add test suite for RSA keys zhenwei pi
2022-05-14  0:55   ` zhenwei pi
2022-05-14  0:55 ` [PATCH v6 9/9] crypto: Introduce RSA algorithm zhenwei pi
2022-05-14  0:55   ` zhenwei pi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YotXr4K39xsiS//O@redhat.com \
    --to=berrange@redhat.com \
    --cc=arei.gonglei@huawei.com \
    --cc=cohuck@redhat.com \
    --cc=helei.sig11@bytedance.com \
    --cc=jasowang@redhat.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=pizhenwei@bytedance.com \
    --cc=qemu-devel@nongnu.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.