13b472e71SThomas Huth /*
23b472e71SThomas Huth * QEMU Crypto hash speed benchmark
33b472e71SThomas Huth *
43b472e71SThomas Huth * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
53b472e71SThomas Huth *
63b472e71SThomas Huth * Authors:
73b472e71SThomas Huth * Longpeng(Mike) <longpeng2@huawei.com>
83b472e71SThomas Huth *
93b472e71SThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or
103b472e71SThomas Huth * (at your option) any later version. See the COPYING file in the
113b472e71SThomas Huth * top-level directory.
123b472e71SThomas Huth */
133b472e71SThomas Huth #include "qemu/osdep.h"
143b472e71SThomas Huth #include "qemu/units.h"
153b472e71SThomas Huth #include "crypto/init.h"
163b472e71SThomas Huth #include "crypto/hash.h"
173b472e71SThomas Huth
183b472e71SThomas Huth typedef struct QCryptoHashOpts {
193b472e71SThomas Huth size_t chunk_size;
20*ef834aa2SMarkus Armbruster QCryptoHashAlgo alg;
213b472e71SThomas Huth } QCryptoHashOpts;
223b472e71SThomas Huth
test_hash_speed(const void * opaque)233b472e71SThomas Huth static void test_hash_speed(const void *opaque)
243b472e71SThomas Huth {
253b472e71SThomas Huth const QCryptoHashOpts *opts = opaque;
263b472e71SThomas Huth uint8_t *in = NULL, *out = NULL;
273b472e71SThomas Huth size_t out_len = 0;
283b472e71SThomas Huth const size_t total = 2 * GiB;
293b472e71SThomas Huth size_t remain;
303b472e71SThomas Huth struct iovec iov;
313b472e71SThomas Huth int ret;
323b472e71SThomas Huth
333b472e71SThomas Huth in = g_new0(uint8_t, opts->chunk_size);
343b472e71SThomas Huth memset(in, g_test_rand_int(), opts->chunk_size);
353b472e71SThomas Huth
363b472e71SThomas Huth iov.iov_base = (char *)in;
373b472e71SThomas Huth iov.iov_len = opts->chunk_size;
383b472e71SThomas Huth
393b472e71SThomas Huth g_test_timer_start();
403b472e71SThomas Huth remain = total;
413b472e71SThomas Huth while (remain) {
423b472e71SThomas Huth ret = qcrypto_hash_bytesv(opts->alg,
433b472e71SThomas Huth &iov, 1, &out, &out_len,
443b472e71SThomas Huth NULL);
453b472e71SThomas Huth g_assert(ret == 0);
463b472e71SThomas Huth
473b472e71SThomas Huth remain -= opts->chunk_size;
483b472e71SThomas Huth }
493b472e71SThomas Huth g_test_timer_elapsed();
503b472e71SThomas Huth
513b472e71SThomas Huth g_test_message("hash(%s): chunk %zu bytes %.2f MB/sec",
52*ef834aa2SMarkus Armbruster QCryptoHashAlgo_str(opts->alg),
533b472e71SThomas Huth opts->chunk_size, total / g_test_timer_last());
543b472e71SThomas Huth
553b472e71SThomas Huth g_free(out);
563b472e71SThomas Huth g_free(in);
573b472e71SThomas Huth }
583b472e71SThomas Huth
main(int argc,char ** argv)593b472e71SThomas Huth int main(int argc, char **argv)
603b472e71SThomas Huth {
613b472e71SThomas Huth char name[64];
623b472e71SThomas Huth
633b472e71SThomas Huth g_test_init(&argc, &argv, NULL);
643b472e71SThomas Huth g_assert(qcrypto_init(NULL) == 0);
653b472e71SThomas Huth
663b472e71SThomas Huth #define TEST_ONE(a, c) \
673b472e71SThomas Huth QCryptoHashOpts opts ## a ## c = { \
68*ef834aa2SMarkus Armbruster .alg = QCRYPTO_HASH_ALGO_ ## a, .chunk_size = c, \
693b472e71SThomas Huth }; \
703b472e71SThomas Huth memset(name, 0 , sizeof(name)); \
713b472e71SThomas Huth snprintf(name, sizeof(name), \
723b472e71SThomas Huth "/crypto/benchmark/hash/%s/bufsize-%d", \
73*ef834aa2SMarkus Armbruster QCryptoHashAlgo_str(QCRYPTO_HASH_ALGO_ ## a), \
743b472e71SThomas Huth c); \
75*ef834aa2SMarkus Armbruster if (qcrypto_hash_supports(QCRYPTO_HASH_ALGO_ ## a)) \
763b472e71SThomas Huth g_test_add_data_func(name, \
773b472e71SThomas Huth &opts ## a ## c, \
783b472e71SThomas Huth test_hash_speed);
793b472e71SThomas Huth
803b472e71SThomas Huth TEST_ONE(MD5, 512);
813b472e71SThomas Huth TEST_ONE(MD5, 1024);
823b472e71SThomas Huth TEST_ONE(MD5, 4096);
833b472e71SThomas Huth TEST_ONE(MD5, 16384);
843b472e71SThomas Huth
853b472e71SThomas Huth TEST_ONE(SHA1, 512);
863b472e71SThomas Huth TEST_ONE(SHA1, 1024);
873b472e71SThomas Huth TEST_ONE(SHA1, 4096);
883b472e71SThomas Huth TEST_ONE(SHA1, 16384);
893b472e71SThomas Huth
903b472e71SThomas Huth TEST_ONE(SHA224, 512);
913b472e71SThomas Huth TEST_ONE(SHA224, 1024);
923b472e71SThomas Huth TEST_ONE(SHA224, 4096);
933b472e71SThomas Huth TEST_ONE(SHA224, 16384);
943b472e71SThomas Huth
953b472e71SThomas Huth TEST_ONE(SHA384, 512);
963b472e71SThomas Huth TEST_ONE(SHA384, 1024);
973b472e71SThomas Huth TEST_ONE(SHA384, 4096);
983b472e71SThomas Huth TEST_ONE(SHA384, 16384);
993b472e71SThomas Huth
1003b472e71SThomas Huth TEST_ONE(SHA256, 512);
1013b472e71SThomas Huth TEST_ONE(SHA256, 1024);
1023b472e71SThomas Huth TEST_ONE(SHA256, 4096);
1033b472e71SThomas Huth TEST_ONE(SHA256, 16384);
1043b472e71SThomas Huth
1053b472e71SThomas Huth TEST_ONE(SHA512, 512);
1063b472e71SThomas Huth TEST_ONE(SHA512, 1024);
1073b472e71SThomas Huth TEST_ONE(SHA512, 4096);
1083b472e71SThomas Huth TEST_ONE(SHA512, 16384);
1093b472e71SThomas Huth
1103b472e71SThomas Huth TEST_ONE(RIPEMD160, 512);
1113b472e71SThomas Huth TEST_ONE(RIPEMD160, 1024);
1123b472e71SThomas Huth TEST_ONE(RIPEMD160, 4096);
1133b472e71SThomas Huth TEST_ONE(RIPEMD160, 16384);
1143b472e71SThomas Huth
1153b472e71SThomas Huth return g_test_run();
1163b472e71SThomas Huth }
117