13b472e71SThomas Huth /*
23b472e71SThomas Huth * QEMU Crypto hmac 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/hmac.h"
173b472e71SThomas Huth
183b472e71SThomas Huth #define KEY "monkey monkey monkey monkey"
193b472e71SThomas Huth
test_hmac_speed(const void * opaque)203b472e71SThomas Huth static void test_hmac_speed(const void *opaque)
213b472e71SThomas Huth {
223b472e71SThomas Huth size_t chunk_size = (size_t)opaque;
233b472e71SThomas Huth QCryptoHmac *hmac = NULL;
243b472e71SThomas Huth uint8_t *in = NULL, *out = NULL;
253b472e71SThomas Huth size_t out_len = 0;
263b472e71SThomas Huth double total = 0.0;
273b472e71SThomas Huth struct iovec iov;
283b472e71SThomas Huth Error *err = NULL;
293b472e71SThomas Huth int ret;
303b472e71SThomas Huth
31*ef834aa2SMarkus Armbruster if (!qcrypto_hmac_supports(QCRYPTO_HASH_ALGO_SHA256)) {
323b472e71SThomas Huth return;
333b472e71SThomas Huth }
343b472e71SThomas Huth
353b472e71SThomas Huth in = g_new0(uint8_t, chunk_size);
363b472e71SThomas Huth memset(in, g_test_rand_int(), chunk_size);
373b472e71SThomas Huth
383b472e71SThomas Huth iov.iov_base = (char *)in;
393b472e71SThomas Huth iov.iov_len = chunk_size;
403b472e71SThomas Huth
413b472e71SThomas Huth g_test_timer_start();
423b472e71SThomas Huth do {
43*ef834aa2SMarkus Armbruster hmac = qcrypto_hmac_new(QCRYPTO_HASH_ALGO_SHA256,
443b472e71SThomas Huth (const uint8_t *)KEY, strlen(KEY), &err);
453b472e71SThomas Huth g_assert(err == NULL);
463b472e71SThomas Huth g_assert(hmac != NULL);
473b472e71SThomas Huth
483b472e71SThomas Huth ret = qcrypto_hmac_bytesv(hmac, &iov, 1, &out, &out_len, &err);
493b472e71SThomas Huth g_assert(ret == 0);
503b472e71SThomas Huth g_assert(err == NULL);
513b472e71SThomas Huth
523b472e71SThomas Huth qcrypto_hmac_free(hmac);
533b472e71SThomas Huth
543b472e71SThomas Huth total += chunk_size;
553b472e71SThomas Huth } while (g_test_timer_elapsed() < 5.0);
563b472e71SThomas Huth
573b472e71SThomas Huth total /= MiB;
583b472e71SThomas Huth g_test_message("hmac(%s): chunk %zu bytes %.2f MB/sec",
59*ef834aa2SMarkus Armbruster QCryptoHashAlgo_str(QCRYPTO_HASH_ALGO_SHA256),
603b472e71SThomas Huth chunk_size, total / g_test_timer_last());
613b472e71SThomas Huth
623b472e71SThomas Huth g_free(out);
633b472e71SThomas Huth g_free(in);
643b472e71SThomas Huth }
653b472e71SThomas Huth
main(int argc,char ** argv)663b472e71SThomas Huth int main(int argc, char **argv)
673b472e71SThomas Huth {
683b472e71SThomas Huth size_t i;
693b472e71SThomas Huth char name[64];
703b472e71SThomas Huth
713b472e71SThomas Huth g_test_init(&argc, &argv, NULL);
723b472e71SThomas Huth g_assert(qcrypto_init(NULL) == 0);
733b472e71SThomas Huth
743b472e71SThomas Huth for (i = 512; i <= 64 * KiB; i *= 2) {
753b472e71SThomas Huth memset(name, 0 , sizeof(name));
763b472e71SThomas Huth snprintf(name, sizeof(name), "/crypto/hmac/speed-%zu", i);
773b472e71SThomas Huth g_test_add_data_func(name, (void *)i, test_hmac_speed);
783b472e71SThomas Huth }
793b472e71SThomas Huth
803b472e71SThomas Huth return g_test_run();
813b472e71SThomas Huth }
82