]>
Commit | Line | Data |
---|---|---|
1efd9d5e LM |
1 | /* |
2 | * QEMU Crypto cipher speed benchmark | |
3 | * | |
4 | * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD. | |
5 | * | |
6 | * Authors: | |
7 | * Longpeng(Mike) <[email protected]> | |
8 | * | |
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. | |
12 | */ | |
13 | #include "qemu/osdep.h" | |
14 | #include "crypto/init.h" | |
15 | #include "crypto/cipher.h" | |
16 | ||
17 | static void test_cipher_speed(const void *opaque) | |
18 | { | |
19 | QCryptoCipher *cipher; | |
20 | Error *err = NULL; | |
21 | double total = 0.0; | |
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); | |
28 | ||
29 | key = g_new0(uint8_t, nkey); | |
30 | memset(key, g_test_rand_int(), nkey); | |
31 | ||
32 | iv = g_new0(uint8_t, niv); | |
33 | memset(iv, g_test_rand_int(), niv); | |
34 | ||
35 | ciphertext = g_new0(uint8_t, chunk_size); | |
36 | ||
37 | plaintext = g_new0(uint8_t, chunk_size); | |
38 | memset(plaintext, g_test_rand_int(), chunk_size); | |
39 | ||
40 | cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALG_AES_128, | |
41 | QCRYPTO_CIPHER_MODE_CBC, | |
42 | key, nkey, &err); | |
43 | g_assert(cipher != NULL); | |
44 | ||
45 | g_assert(qcrypto_cipher_setiv(cipher, | |
46 | iv, niv, | |
47 | &err) == 0); | |
48 | ||
49 | g_test_timer_start(); | |
50 | do { | |
51 | g_assert(qcrypto_cipher_encrypt(cipher, | |
52 | plaintext, | |
53 | ciphertext, | |
54 | chunk_size, | |
55 | &err) == 0); | |
56 | total += chunk_size; | |
57 | } while (g_test_timer_elapsed() < 5.0); | |
58 | ||
59 | total /= 1024 * 1024; /* to MB */ | |
60 | ||
61 | g_print("cbc(aes128): "); | |
8c0a6dc9 | 62 | g_print("Testing chunk_size %zu bytes ", chunk_size); |
1efd9d5e LM |
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()); | |
65 | ||
66 | qcrypto_cipher_free(cipher); | |
67 | g_free(plaintext); | |
68 | g_free(ciphertext); | |
69 | g_free(iv); | |
70 | g_free(key); | |
71 | } | |
72 | ||
73 | int main(int argc, char **argv) | |
74 | { | |
75 | size_t i; | |
76 | char name[64]; | |
77 | ||
78 | g_test_init(&argc, &argv, NULL); | |
79 | g_assert(qcrypto_init(NULL) == 0); | |
80 | ||
81 | for (i = 512; i <= (64 * 1204); i *= 2) { | |
82 | memset(name, 0 , sizeof(name)); | |
8c0a6dc9 | 83 | snprintf(name, sizeof(name), "/crypto/cipher/speed-%zu", i); |
1efd9d5e LM |
84 | g_test_add_data_func(name, (void *)i, test_cipher_speed); |
85 | } | |
86 | ||
87 | return g_test_run(); | |
88 | } |