2 * QEMU Crypto cipher speed benchmark
4 * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
7 * Longpeng(Mike) <longpeng2@huawei.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.
13 #include "qemu/osdep.h"
14 #include "qemu/units.h"
15 #include "crypto/init.h"
16 #include "crypto/cipher.h"
18 static void test_cipher_speed(size_t chunk_size
,
19 QCryptoCipherMode mode
,
20 QCryptoCipherAlgorithm alg
)
22 QCryptoCipher
*cipher
;
24 uint8_t *key
= NULL
, *iv
= NULL
;
25 uint8_t *plaintext
= NULL
, *ciphertext
= NULL
;
28 const size_t total
= 2 * GiB
;
31 if (!qcrypto_cipher_supports(alg
, mode
)) {
35 nkey
= qcrypto_cipher_get_key_len(alg
);
36 niv
= qcrypto_cipher_get_iv_len(alg
, mode
);
37 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
41 key
= g_new0(uint8_t, nkey
);
42 memset(key
, g_test_rand_int(), nkey
);
44 iv
= g_new0(uint8_t, niv
);
45 memset(iv
, g_test_rand_int(), niv
);
47 ciphertext
= g_new0(uint8_t, chunk_size
);
49 plaintext
= g_new0(uint8_t, chunk_size
);
50 memset(plaintext
, g_test_rand_int(), chunk_size
);
52 cipher
= qcrypto_cipher_new(alg
, mode
,
54 g_assert(cipher
!= NULL
);
56 if (mode
!= QCRYPTO_CIPHER_MODE_ECB
)
57 g_assert(qcrypto_cipher_setiv(cipher
,
64 g_assert(qcrypto_cipher_encrypt(cipher
,
71 g_test_timer_elapsed();
73 g_test_message("Enc chunk %zu bytes ", chunk_size
);
74 g_test_message("%.2f MB/sec ", (double)total
/ MiB
/ g_test_timer_last());
79 g_assert(qcrypto_cipher_decrypt(cipher
,
86 g_test_timer_elapsed();
88 g_test_message("Dec chunk %zu bytes ", chunk_size
);
89 g_test_message("%.2f MB/sec ", (double)total
/ MiB
/ g_test_timer_last());
91 qcrypto_cipher_free(cipher
);
99 static void test_cipher_speed_ecb_aes_128(const void *opaque
)
101 size_t chunk_size
= (size_t)opaque
;
102 test_cipher_speed(chunk_size
,
103 QCRYPTO_CIPHER_MODE_ECB
,
104 QCRYPTO_CIPHER_ALG_AES_128
);
107 static void test_cipher_speed_ecb_aes_256(const void *opaque
)
109 size_t chunk_size
= (size_t)opaque
;
110 test_cipher_speed(chunk_size
,
111 QCRYPTO_CIPHER_MODE_ECB
,
112 QCRYPTO_CIPHER_ALG_AES_256
);
115 static void test_cipher_speed_cbc_aes_128(const void *opaque
)
117 size_t chunk_size
= (size_t)opaque
;
118 test_cipher_speed(chunk_size
,
119 QCRYPTO_CIPHER_MODE_CBC
,
120 QCRYPTO_CIPHER_ALG_AES_128
);
123 static void test_cipher_speed_cbc_aes_256(const void *opaque
)
125 size_t chunk_size
= (size_t)opaque
;
126 test_cipher_speed(chunk_size
,
127 QCRYPTO_CIPHER_MODE_CBC
,
128 QCRYPTO_CIPHER_ALG_AES_256
);
131 static void test_cipher_speed_ctr_aes_128(const void *opaque
)
133 size_t chunk_size
= (size_t)opaque
;
134 test_cipher_speed(chunk_size
,
135 QCRYPTO_CIPHER_MODE_CTR
,
136 QCRYPTO_CIPHER_ALG_AES_128
);
139 static void test_cipher_speed_ctr_aes_256(const void *opaque
)
141 size_t chunk_size
= (size_t)opaque
;
142 test_cipher_speed(chunk_size
,
143 QCRYPTO_CIPHER_MODE_CTR
,
144 QCRYPTO_CIPHER_ALG_AES_256
);
147 static void test_cipher_speed_xts_aes_128(const void *opaque
)
149 size_t chunk_size
= (size_t)opaque
;
150 test_cipher_speed(chunk_size
,
151 QCRYPTO_CIPHER_MODE_XTS
,
152 QCRYPTO_CIPHER_ALG_AES_128
);
155 static void test_cipher_speed_xts_aes_256(const void *opaque
)
157 size_t chunk_size
= (size_t)opaque
;
158 test_cipher_speed(chunk_size
,
159 QCRYPTO_CIPHER_MODE_XTS
,
160 QCRYPTO_CIPHER_ALG_AES_256
);
164 int main(int argc
, char **argv
)
168 g_test_init(&argc
, &argv
, NULL
);
169 g_assert(qcrypto_init(NULL
) == 0);
171 #define ADD_TEST(mode, cipher, keysize, chunk) \
172 if ((!alg || g_str_equal(alg, #mode)) && \
173 (!size || g_str_equal(size, #chunk))) \
174 g_test_add_data_func( \
175 "/crypto/cipher/" #mode "-" #cipher "-" #keysize "/chunk-" #chunk, \
177 test_cipher_speed_ ## mode ## _ ## cipher ## _ ## keysize)
186 #define ADD_TESTS(chunk) \
188 ADD_TEST(ecb, aes, 128, chunk); \
189 ADD_TEST(ecb, aes, 256, chunk); \
190 ADD_TEST(cbc, aes, 128, chunk); \
191 ADD_TEST(cbc, aes, 256, chunk); \
192 ADD_TEST(ctr, aes, 128, chunk); \
193 ADD_TEST(ctr, aes, 256, chunk); \
194 ADD_TEST(xts, aes, 128, chunk); \
195 ADD_TEST(xts, aes, 256, chunk); \