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(const void *opaque
)
20 QCryptoCipher
*cipher
;
23 size_t chunk_size
= (size_t)opaque
;
24 uint8_t *key
= NULL
, *iv
= NULL
;
25 uint8_t *plaintext
= NULL
, *ciphertext
= NULL
;
26 size_t nkey
= qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128
);
27 size_t niv
= qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128
,
28 QCRYPTO_CIPHER_MODE_CBC
);
30 key
= g_new0(uint8_t, nkey
);
31 memset(key
, g_test_rand_int(), nkey
);
33 iv
= g_new0(uint8_t, niv
);
34 memset(iv
, g_test_rand_int(), niv
);
36 ciphertext
= g_new0(uint8_t, chunk_size
);
38 plaintext
= g_new0(uint8_t, chunk_size
);
39 memset(plaintext
, g_test_rand_int(), chunk_size
);
41 cipher
= qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128
,
42 QCRYPTO_CIPHER_MODE_CBC
,
44 g_assert(cipher
!= NULL
);
46 g_assert(qcrypto_cipher_setiv(cipher
,
52 g_assert(qcrypto_cipher_encrypt(cipher
,
58 } while (g_test_timer_elapsed() < 5.0);
61 g_print("cbc(aes128): ");
62 g_print("Testing chunk_size %zu bytes ", chunk_size
);
63 g_print("done: %.2f MB in %.2f secs: ", total
, g_test_timer_last());
64 g_print("%.2f MB/sec\n", total
/ g_test_timer_last());
66 qcrypto_cipher_free(cipher
);
73 int main(int argc
, char **argv
)
78 g_test_init(&argc
, &argv
, NULL
);
79 g_assert(qcrypto_init(NULL
) == 0);
81 for (i
= 512; i
<= 64 * KiB
; i
*= 2) {
82 memset(name
, 0 , sizeof(name
));
83 snprintf(name
, sizeof(name
), "/crypto/cipher/speed-%zu", i
);
84 g_test_add_data_func(name
, (void *)i
, test_cipher_speed
);