From patchwork Mon Dec 12 08:08:12 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng(Mike)" X-Patchwork-Id: 9470157 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 04CCF60573 for ; Mon, 12 Dec 2016 08:20:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E7C8928437 for ; Mon, 12 Dec 2016 08:20:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DA8D02845A; Mon, 12 Dec 2016 08:20:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 19E3628437 for ; Mon, 12 Dec 2016 08:20:04 +0000 (UTC) Received: from localhost ([::1]:51124 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cGLq7-0005y5-Ck for patchwork-qemu-devel@patchwork.kernel.org; Mon, 12 Dec 2016 03:20:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47954) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cGLfF-0006Gr-Uu for qemu-devel@nongnu.org; Mon, 12 Dec 2016 03:08:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cGLfC-000752-NZ for qemu-devel@nongnu.org; Mon, 12 Dec 2016 03:08:49 -0500 Received: from szxga03-in.huawei.com ([119.145.14.66]:53463) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1cGLfB-00072n-Rk for qemu-devel@nongnu.org; Mon, 12 Dec 2016 03:08:46 -0500 Received: from 172.24.1.136 (EHLO szxeml433-hub.china.huawei.com) ([172.24.1.136]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id CMP44453; Mon, 12 Dec 2016 16:08:42 +0800 (CST) Received: from localhost (10.177.246.209) by szxeml433-hub.china.huawei.com (10.82.67.210) with Microsoft SMTP Server id 14.3.235.1; Mon, 12 Dec 2016 16:08:34 +0800 From: "Longpeng(Mike)" To: , , , Date: Mon, 12 Dec 2016 16:08:12 +0800 Message-ID: <1481530092-20240-8-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1481530092-20240-1-git-send-email-longpeng2@huawei.com> References: <1481530092-20240-1-git-send-email-longpeng2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 119.145.14.66 Subject: [Qemu-devel] [PATCH for-2.9 v2 7/7] crypto: add HMAC algorithms testcases X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Longpeng\(Mike\)" , arei.gonglei@huawei.com, qemu-devel@nongnu.org, wu.wubin@huawei.com, jianjay.zhou@huawei.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch add HMAC algorithms testcases Signed-off-by: Longpeng(Mike) --- tests/Makefile.include | 2 + tests/test-crypto-hmac.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 tests/test-crypto-hmac.c diff --git a/tests/Makefile.include b/tests/Makefile.include index e98d3b6..4841d58 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -91,6 +91,7 @@ gcov-files-test-qemu-opts-y = qom/test-qemu-opts.c check-unit-y += tests/test-write-threshold$(EXESUF) gcov-files-test-write-threshold-y = block/write-threshold.c check-unit-y += tests/test-crypto-hash$(EXESUF) +check-unit-y += tests/test-crypto-hmac$(EXESUF) check-unit-y += tests/test-crypto-cipher$(EXESUF) check-unit-y += tests/test-crypto-secret$(EXESUF) check-unit-$(CONFIG_GNUTLS) += tests/test-crypto-tlscredsx509$(EXESUF) @@ -571,6 +572,7 @@ tests/test-opts-visitor$(EXESUF): tests/test-opts-visitor.o $(test-qapi-obj-y) tests/test-mul64$(EXESUF): tests/test-mul64.o $(test-util-obj-y) tests/test-bitops$(EXESUF): tests/test-bitops.o $(test-util-obj-y) tests/test-crypto-hash$(EXESUF): tests/test-crypto-hash.o $(test-crypto-obj-y) +tests/test-crypto-hmac$(EXESUF): tests/test-crypto-hmac.o $(test-crypto-obj-y) tests/test-crypto-cipher$(EXESUF): tests/test-crypto-cipher.o $(test-crypto-obj-y) tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypto-obj-y) tests/test-crypto-xts$(EXESUF): tests/test-crypto-xts.o $(test-crypto-obj-y) diff --git a/tests/test-crypto-hmac.c b/tests/test-crypto-hmac.c new file mode 100644 index 0000000..678df52 --- /dev/null +++ b/tests/test-crypto-hmac.c @@ -0,0 +1,166 @@ +/* + * QEMU Crypto hmac algorithms tests + * + * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. + * + * Authors: + * Longpeng(Mike) + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "crypto/init.h" +#include "crypto/hmac.h" + +typedef struct QCryptoHmacTestData QCryptoHmacTestData; +struct QCryptoHmacTestData { + const char *path; + QCryptoHmacAlgorithm alg; + const char *key; + const char *message; + const char *digest; +}; + +static QCryptoHmacTestData test_data[] = { + { + .path = "/crypto/hmac/hmac-md5", + .alg = QCRYPTO_HMAC_ALG_MD5, + .key = + "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", + .message = + "4869205468657265", + .digest = + "9294727a3638bb1c13f48ef8158bfc9d", + }, + { + .path = "/crypto/hmac/hmac-sha1", + .alg = QCRYPTO_HMAC_ALG_SHA1, + .key = + "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b" + "0b0b0b0b", + .message = + "4869205468657265", + .digest = + "b617318655057264e28bc0b6fb378c8e" + "f146be00", + }, +}; + +static inline int unhex(char c) +{ + if (c >= 'a' && c <= 'f') { + return 10 + (c - 'a'); + } + if (c >= 'A' && c <= 'F') { + return 10 + (c - 'A'); + } + return c - '0'; +} + +static inline char hex(int i) +{ + if (i < 10) { + return '0' + i; + } + return 'a' + (i - 10); +} + +static size_t unhex_string(const char *hexstr, + uint8_t **data) +{ + size_t len; + size_t i; + + if (!hexstr) { + *data = NULL; + return 0; + } + + len = strlen(hexstr); + *data = g_new0(uint8_t, len / 2); + + for (i = 0; i < len; i += 2) { + (*data)[i / 2] = (unhex(hexstr[i]) << 4) | unhex(hexstr[i + 1]); + } + return len / 2; +} + +static char *hex_string(const uint8_t *bytes, + size_t len) +{ + char *hexstr = g_new0(char, len * 2 + 1); + size_t i; + + for (i = 0; i < len; i++) { + hexstr[i * 2] = hex((bytes[i] >> 4) & 0xf); + hexstr[i * 2 + 1] = hex(bytes[i] & 0xf); + } + hexstr[len * 2] = '\0'; + + return hexstr; +} + +static void test_hmac(const void *opaque) +{ + const QCryptoHmacTestData *data = opaque; + size_t nkey, digest_len, msg_len; + uint8_t *key = NULL; + uint8_t *message = NULL; + uint8_t *digest = NULL; + uint8_t *output = NULL; + char *outputhex = NULL; + QCryptoHmac *hmac = NULL; + Error *err = NULL; + int ret; + + if (!qcrypto_hmac_supports(data->alg)) { + return; + } + + nkey = unhex_string(data->key, &key); + digest_len = unhex_string(data->digest, &digest); + msg_len = unhex_string(data->message, &message); + + output = g_new0(uint8_t, digest_len); + + hmac = qcrypto_hmac_new(data->alg, key, nkey, &err); + g_assert(err == NULL); + g_assert(hmac != NULL); + + ret = qcrypto_hmac_bytes(hmac, (const char *)message, + msg_len, &output, &digest_len, &err); + + g_assert(ret == 0); + + outputhex = hex_string(output, digest_len); + + g_assert_cmpstr(outputhex, ==, data->digest); + + qcrypto_hmac_free(hmac); + + g_free(outputhex); + g_free(output); + g_free(message); + g_free(digest); + g_free(key); +} + +int main(int argc, char **argv) +{ + size_t i; + + g_test_init(&argc, &argv, NULL); + + g_assert(qcrypto_init(NULL) == 0); + + for (i = 0; i < G_N_ELEMENTS(test_data); i++) { + g_test_add_data_func(test_data[i].path, + &test_data[i], test_hmac); + } + + return g_test_run(); +}