2 * QEMU Crypto akcipher speed benchmark
4 * Copyright (c) 2022 Bytedance
7 * lei he <helei.sig11@bytedance.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * (at your option) any later version. See the COPYING file in the
11 * top-level directory.
14 #include "qemu/osdep.h"
15 #include "crypto/init.h"
16 #include "crypto/akcipher.h"
17 #include "standard-headers/linux/virtio_crypto.h"
19 #include "test_akcipher_keys.inc"
21 static QCryptoAkCipher
*create_rsa_akcipher(const uint8_t *priv_key
,
23 QCryptoRSAPaddingAlgorithm padding
,
24 QCryptoHashAlgorithm hash
)
26 QCryptoAkCipherOptions opt
;
29 opt
.alg
= QCRYPTO_AKCIPHER_ALG_RSA
;
30 opt
.u
.rsa
.padding_alg
= padding
;
31 opt
.u
.rsa
.hash_alg
= hash
;
32 rsa
= qcrypto_akcipher_new(&opt
, QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE
,
33 priv_key
, keylen
, &error_abort
);
37 static void test_rsa_speed(const uint8_t *priv_key
, size_t keylen
,
41 #define SHA1_DGST_LEN 20
42 #define SIGN_TIMES 10000
43 #define VERIFY_TIMES 100000
44 #define PADDING QCRYPTO_RSA_PADDING_ALG_PKCS1
45 #define HASH QCRYPTO_HASH_ALG_SHA1
47 g_autoptr(QCryptoAkCipher
) rsa
=
48 create_rsa_akcipher(priv_key
, keylen
, PADDING
, HASH
);
49 g_autofree
uint8_t *dgst
= NULL
;
50 g_autofree
uint8_t *signature
= NULL
;
53 dgst
= g_new0(uint8_t, SHA1_DGST_LEN
);
54 memset(dgst
, g_test_rand_int(), SHA1_DGST_LEN
);
55 signature
= g_new0(uint8_t, key_size
/ BYTE
);
57 g_test_message("benchmark rsa%zu (%s-%s) sign...", key_size
,
58 QCryptoRSAPaddingAlgorithm_str(PADDING
),
59 QCryptoHashAlgorithm_str(HASH
));
61 for (count
= 0; count
< SIGN_TIMES
; ++count
) {
62 g_assert(qcrypto_akcipher_sign(rsa
, dgst
, SHA1_DGST_LEN
,
63 signature
, key_size
/ BYTE
,
66 g_test_timer_elapsed();
67 g_test_message("rsa%zu (%s-%s) sign %zu times in %.2f seconds,"
69 key_size
, QCryptoRSAPaddingAlgorithm_str(PADDING
),
70 QCryptoHashAlgorithm_str(HASH
),
71 count
, g_test_timer_last(),
72 (double)count
/ g_test_timer_last());
74 g_test_message("benchmark rsa%zu (%s-%s) verification...", key_size
,
75 QCryptoRSAPaddingAlgorithm_str(PADDING
),
76 QCryptoHashAlgorithm_str(HASH
));
78 for (count
= 0; count
< VERIFY_TIMES
; ++count
) {
79 g_assert(qcrypto_akcipher_verify(rsa
, signature
, key_size
/ BYTE
,
83 g_test_timer_elapsed();
84 g_test_message("rsa%zu (%s-%s) verify %zu times in %.2f seconds,"
86 key_size
, QCryptoRSAPaddingAlgorithm_str(PADDING
),
87 QCryptoHashAlgorithm_str(HASH
),
88 count
, g_test_timer_last(),
89 (double)count
/ g_test_timer_last());
92 static void test_rsa_1024_speed(const void *opaque
)
94 size_t key_size
= (size_t)opaque
;
95 test_rsa_speed(rsa1024_priv_key
, sizeof(rsa1024_priv_key
), key_size
);
98 static void test_rsa_2048_speed(const void *opaque
)
100 size_t key_size
= (size_t)opaque
;
101 test_rsa_speed(rsa2048_priv_key
, sizeof(rsa2048_priv_key
), key_size
);
104 static void test_rsa_4096_speed(const void *opaque
)
106 size_t key_size
= (size_t)opaque
;
107 test_rsa_speed(rsa4096_priv_key
, sizeof(rsa4096_priv_key
), key_size
);
110 int main(int argc
, char **argv
)
114 g_test_init(&argc
, &argv
, NULL
);
115 g_assert(qcrypto_init(NULL
) == 0);
117 #define ADD_TEST(asym_alg, keysize) \
118 if ((!alg || g_str_equal(alg, #asym_alg)) && \
119 (!size || g_str_equal(size, #keysize))) \
120 g_test_add_data_func( \
121 "/crypto/akcipher/" #asym_alg "-" #keysize, \
123 test_ ## asym_alg ## _ ## keysize ## _speed)