From 3aaae083f7cbe3fbe7260081bbdccb544b60d7ba Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 22 Mar 2023 09:02:45 +0100 Subject: [PATCH] tst_bench_QCryptographicHash: add benchmarks for QMessageAuthenticationCode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We could add a tst_QMessageAuthenticationCode, but it would have to duplicate a lot of the tst_QCryptographicHash machinery, so just add it here. Pick-to: 6.5 6.2 5.15 Change-Id: Icc60de865c72c5e423cb3be57f58297c522791f7 Reviewed-by: Fabian Kosmale Reviewed-by: MÃ¥rten Nordheim --- .../tst_bench_qcryptographichash.cpp | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tests/benchmarks/corelib/tools/qcryptographichash/tst_bench_qcryptographichash.cpp b/tests/benchmarks/corelib/tools/qcryptographichash/tst_bench_qcryptographichash.cpp index 8b9034c3e7..571e805ceb 100644 --- a/tests/benchmarks/corelib/tools/qcryptographichash/tst_bench_qcryptographichash.cpp +++ b/tests/benchmarks/corelib/tools/qcryptographichash/tst_bench_qcryptographichash.cpp @@ -1,3 +1,4 @@ +// Copyright (C) 2023 The Qt Company Ltd. // Copyright (C) 2017 Intel Corporation. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 @@ -5,11 +6,13 @@ #include #include #include +#include #include #include #include #include +#include #include @@ -30,6 +33,14 @@ private Q_SLOTS: void addData(); void addDataChunked_data() { hash_data(); } void addDataChunked(); + + // QMessageAuthenticationCode: + void hmac_hash_data() { hash_data(); } + void hmac_hash(); + void hmac_addData_data() { hash_data(); } + void hmac_addData(); + void hmac_setKey_data(); + void hmac_setKey(); }; const int MaxBlockSize = 65536; @@ -120,6 +131,75 @@ void tst_QCryptographicHash::addDataChunked() } } +static QByteArray hmacKey() { + static QByteArray key = [] { + QByteArray result(277, Qt::Uninitialized); + std::iota(result.begin(), result.end(), uchar(0)); // uchar so wraps after UCHAR_MAX + return result; + }(); + return key; +} + +void tst_QCryptographicHash::hmac_hash() +{ + QFETCH(const Algorithm, algo); + QFETCH(const QByteArray, data); + + const auto key = hmacKey(); + QBENCHMARK { + [[maybe_unused]] + auto r = QMessageAuthenticationCode::hash(data, key, algo); + } +} + +void tst_QCryptographicHash::hmac_addData() +{ + QFETCH(const Algorithm, algo); + QFETCH(const QByteArray, data); + + const auto key = hmacKey(); + QMessageAuthenticationCode mac(algo, key); + QBENCHMARK { + mac.reset(); + mac.addData(data); + [[maybe_unused]] + auto r = mac.result(); + } +} + +void tst_QCryptographicHash::hmac_setKey_data() +{ + QTest::addColumn("algo"); + for_each_algorithm([] (Algorithm algo, const char *name) { + if (algo == Algorithm::NumAlgorithms) + return; + QTest::addRow("%s", name) << algo; + }); +} + +void tst_QCryptographicHash::hmac_setKey() +{ + QFETCH(const Algorithm, algo); + + const QByteArrayList keys = [] { + QByteArrayList result; + const auto fullKey = hmacKey(); + result.reserve(fullKey.size()); + for (auto i = fullKey.size(); i > 0; --i) + result.push_back(fullKey.sliced(i)); + return result; + }(); + + QMessageAuthenticationCode mac(algo); + QBENCHMARK { + for (const auto &key : keys) { + mac.setKey(key); + mac.addData("abc", 3); // avoid lazy setKey() + } + } +} + + QTEST_APPLESS_MAIN(tst_QCryptographicHash) #include "tst_bench_qcryptographichash.moc"