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
;
25 uint8_t *key
= NULL
, *iv
= NULL
;
26 uint8_t *plaintext
= NULL
, *ciphertext
= NULL
;
30 if (!qcrypto_cipher_supports(alg
, mode
)) {
34 nkey
= qcrypto_cipher_get_key_len(alg
);
35 niv
= qcrypto_cipher_get_iv_len(alg
, mode
);
36 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
40 key
= g_new0(uint8_t, nkey
);
41 memset(key
, g_test_rand_int(), nkey
);
43 iv
= g_new0(uint8_t, niv
);
44 memset(iv
, g_test_rand_int(), niv
);
46 ciphertext
= g_new0(uint8_t, chunk_size
);
48 plaintext
= g_new0(uint8_t, chunk_size
);
49 memset(plaintext
, g_test_rand_int(), chunk_size
);
51 cipher
= qcrypto_cipher_new(alg
, mode
,
53 g_assert(cipher
!= NULL
);
55 if (mode
!= QCRYPTO_CIPHER_MODE_ECB
)
56 g_assert(qcrypto_cipher_setiv(cipher
,
62 g_assert(qcrypto_cipher_encrypt(cipher
,
68 } while (g_test_timer_elapsed() < 1.0);
71 g_print("Enc chunk %zu bytes ", chunk_size
);
72 g_print("%.2f MB/sec ", total
/ g_test_timer_last());
77 g_assert(qcrypto_cipher_decrypt(cipher
,
83 } while (g_test_timer_elapsed() < 1.0);
86 g_print("Dec chunk %zu bytes ", chunk_size
);
87 g_print("%.2f MB/sec ", total
/ g_test_timer_last());
89 qcrypto_cipher_free(cipher
);
97 static void test_cipher_speed_ecb_aes_128(const void *opaque
)
99 size_t chunk_size
= (size_t)opaque
;
100 test_cipher_speed(chunk_size
,
101 QCRYPTO_CIPHER_MODE_ECB
,
102 QCRYPTO_CIPHER_ALG_AES_128
);
105 static void test_cipher_speed_ecb_aes_256(const void *opaque
)
107 size_t chunk_size
= (size_t)opaque
;
108 test_cipher_speed(chunk_size
,
109 QCRYPTO_CIPHER_MODE_ECB
,
110 QCRYPTO_CIPHER_ALG_AES_256
);
113 static void test_cipher_speed_cbc_aes_128(const void *opaque
)
115 size_t chunk_size
= (size_t)opaque
;
116 test_cipher_speed(chunk_size
,
117 QCRYPTO_CIPHER_MODE_CBC
,
118 QCRYPTO_CIPHER_ALG_AES_128
);
121 static void test_cipher_speed_cbc_aes_256(const void *opaque
)
123 size_t chunk_size
= (size_t)opaque
;
124 test_cipher_speed(chunk_size
,
125 QCRYPTO_CIPHER_MODE_CBC
,
126 QCRYPTO_CIPHER_ALG_AES_256
);
129 static void test_cipher_speed_ctr_aes_128(const void *opaque
)
131 size_t chunk_size
= (size_t)opaque
;
132 test_cipher_speed(chunk_size
,
133 QCRYPTO_CIPHER_MODE_CTR
,
134 QCRYPTO_CIPHER_ALG_AES_128
);
137 static void test_cipher_speed_ctr_aes_256(const void *opaque
)
139 size_t chunk_size
= (size_t)opaque
;
140 test_cipher_speed(chunk_size
,
141 QCRYPTO_CIPHER_MODE_CTR
,
142 QCRYPTO_CIPHER_ALG_AES_256
);
145 static void test_cipher_speed_xts_aes_128(const void *opaque
)
147 size_t chunk_size
= (size_t)opaque
;
148 test_cipher_speed(chunk_size
,
149 QCRYPTO_CIPHER_MODE_XTS
,
150 QCRYPTO_CIPHER_ALG_AES_128
);
153 static void test_cipher_speed_xts_aes_256(const void *opaque
)
155 size_t chunk_size
= (size_t)opaque
;
156 test_cipher_speed(chunk_size
,
157 QCRYPTO_CIPHER_MODE_XTS
,
158 QCRYPTO_CIPHER_ALG_AES_256
);
162 int main(int argc
, char **argv
)
164 g_test_init(&argc
, &argv
, NULL
);
165 g_assert(qcrypto_init(NULL
) == 0);
167 #define ADD_TEST(mode, cipher, keysize, chunk) \
168 g_test_add_data_func( \
169 "/crypto/cipher/" #mode "-" #cipher "-" #keysize "/chunk-" #chunk, \
171 test_cipher_speed_ ## mode ## _ ## cipher ## _ ## keysize)
173 #define ADD_TESTS(chunk) \
175 ADD_TEST(ecb, aes, 128, chunk); \
176 ADD_TEST(ecb, aes, 256, chunk); \
177 ADD_TEST(cbc, aes, 128, chunk); \
178 ADD_TEST(cbc, aes, 256, chunk); \
179 ADD_TEST(ctr, aes, 128, chunk); \
180 ADD_TEST(ctr, aes, 256, chunk); \
181 ADD_TEST(xts, aes, 128, chunk); \
182 ADD_TEST(xts, aes, 256, chunk); \