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(%s-%s) chunk %zu bytes %.2f MB/sec ",
74 QCryptoCipherAlgorithm_str(alg),
75 QCryptoCipherMode_str(mode),
76 chunk_size, (double)total / MiB / g_test_timer_last());
81 g_assert(qcrypto_cipher_decrypt(cipher,
88 g_test_timer_elapsed();
90 g_test_message("dec(%s-%s) chunk %zu bytes %.2f MB/sec ",
91 QCryptoCipherAlgorithm_str(alg),
92 QCryptoCipherMode_str(mode),
93 chunk_size, (double)total / MiB / g_test_timer_last());
95 qcrypto_cipher_free(cipher);
103 static void test_cipher_speed_ecb_aes_128(const void *opaque)
105 size_t chunk_size = (size_t)opaque;
106 test_cipher_speed(chunk_size,
107 QCRYPTO_CIPHER_MODE_ECB,
108 QCRYPTO_CIPHER_ALG_AES_128);
111 static void test_cipher_speed_ecb_aes_256(const void *opaque)
113 size_t chunk_size = (size_t)opaque;
114 test_cipher_speed(chunk_size,
115 QCRYPTO_CIPHER_MODE_ECB,
116 QCRYPTO_CIPHER_ALG_AES_256);
119 static void test_cipher_speed_cbc_aes_128(const void *opaque)
121 size_t chunk_size = (size_t)opaque;
122 test_cipher_speed(chunk_size,
123 QCRYPTO_CIPHER_MODE_CBC,
124 QCRYPTO_CIPHER_ALG_AES_128);
127 static void test_cipher_speed_cbc_aes_256(const void *opaque)
129 size_t chunk_size = (size_t)opaque;
130 test_cipher_speed(chunk_size,
131 QCRYPTO_CIPHER_MODE_CBC,
132 QCRYPTO_CIPHER_ALG_AES_256);
135 static void test_cipher_speed_ctr_aes_128(const void *opaque)
137 size_t chunk_size = (size_t)opaque;
138 test_cipher_speed(chunk_size,
139 QCRYPTO_CIPHER_MODE_CTR,
140 QCRYPTO_CIPHER_ALG_AES_128);
143 static void test_cipher_speed_ctr_aes_256(const void *opaque)
145 size_t chunk_size = (size_t)opaque;
146 test_cipher_speed(chunk_size,
147 QCRYPTO_CIPHER_MODE_CTR,
148 QCRYPTO_CIPHER_ALG_AES_256);
151 static void test_cipher_speed_xts_aes_128(const void *opaque)
153 size_t chunk_size = (size_t)opaque;
154 test_cipher_speed(chunk_size,
155 QCRYPTO_CIPHER_MODE_XTS,
156 QCRYPTO_CIPHER_ALG_AES_128);
159 static void test_cipher_speed_xts_aes_256(const void *opaque)
161 size_t chunk_size = (size_t)opaque;
162 test_cipher_speed(chunk_size,
163 QCRYPTO_CIPHER_MODE_XTS,
164 QCRYPTO_CIPHER_ALG_AES_256);
168 int main(int argc, char **argv)
172 g_test_init(&argc, &argv, NULL);
173 g_assert(qcrypto_init(NULL) == 0);
175 #define ADD_TEST(mode, cipher, keysize, chunk) \
176 if ((!alg || g_str_equal(alg, #mode)) && \
177 (!size || g_str_equal(size, #chunk))) \
178 g_test_add_data_func( \
179 "/crypto/cipher/" #mode "-" #cipher "-" #keysize "/chunk-" #chunk, \
181 test_cipher_speed_ ## mode ## _ ## cipher ## _ ## keysize)
190 #define ADD_TESTS(chunk) \
192 ADD_TEST(ecb, aes, 128, chunk); \
193 ADD_TEST(ecb, aes, 256, chunk); \
194 ADD_TEST(cbc, aes, 128, chunk); \
195 ADD_TEST(cbc, aes, 256, chunk); \
196 ADD_TEST(ctr, aes, 128, chunk); \
197 ADD_TEST(ctr, aes, 256, chunk); \
198 ADD_TEST(xts, aes, 128, chunk); \
199 ADD_TEST(xts, aes, 256, chunk); \