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 "crypto/init.h"
15 #include "crypto/cipher.h"
17 static void test_cipher_speed(const void *opaque
)
19 QCryptoCipher
*cipher
;
22 size_t chunk_size
= (size_t)opaque
;
23 uint8_t *key
= NULL
, *iv
= NULL
;
24 uint8_t *plaintext
= NULL
, *ciphertext
= NULL
;
25 size_t nkey
= qcrypto_cipher_get_key_len(QCRYPTO_CIPHER_ALG_AES_128
);
26 size_t niv
= qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALG_AES_128
,
27 QCRYPTO_CIPHER_MODE_CBC
);
29 key
= g_new0(uint8_t, nkey
);
30 memset(key
, g_test_rand_int(), nkey
);
32 iv
= g_new0(uint8_t, niv
);
33 memset(iv
, g_test_rand_int(), niv
);
35 ciphertext
= g_new0(uint8_t, chunk_size
);
37 plaintext
= g_new0(uint8_t, chunk_size
);
38 memset(plaintext
, g_test_rand_int(), chunk_size
);
40 cipher
= qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128
,
41 QCRYPTO_CIPHER_MODE_CBC
,
43 g_assert(cipher
!= NULL
);
45 g_assert(qcrypto_cipher_setiv(cipher
,
51 g_assert(qcrypto_cipher_encrypt(cipher
,
57 } while (g_test_timer_elapsed() < 5.0);
59 total
/= 1024 * 1024; /* to MB */
61 g_print("cbc(aes128): ");
62 g_print("Testing chunk_size %ld 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 * 1204); i
*= 2) {
82 memset(name
, 0 , sizeof(name
));
83 snprintf(name
, sizeof(name
), "/crypto/cipher/speed-%lu", i
);
84 g_test_add_data_func(name
, (void *)i
, test_cipher_speed
);