From patchwork Mon Dec 12 07:27:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng (Mike, Cloud Infrastructure Service Product Dept.)" X-Patchwork-Id: 704944 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 ozlabs.org (Postfix) with ESMTPS id 3tcZN11jp3z9ryQ for ; Mon, 12 Dec 2016 18:34:09 +1100 (AEDT) Received: from localhost ([::1]:48160 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cGL7f-0004OH-79 for incoming@patchwork.ozlabs.org; Mon, 12 Dec 2016 02:34:07 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53124) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cGL1t-0007N5-6y for qemu-devel@nongnu.org; Mon, 12 Dec 2016 02:28:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cGL1p-0006Hc-5x for qemu-devel@nongnu.org; Mon, 12 Dec 2016 02:28:09 -0500 Received: from szxga01-in.huawei.com ([58.251.152.64]:7770) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1cGL1n-0006D1-Ge for qemu-devel@nongnu.org; Mon, 12 Dec 2016 02:28:05 -0500 Received: from 172.24.1.137 (EHLO szxeml431-hub.china.huawei.com) ([172.24.1.137]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DWI93625; Mon, 12 Dec 2016 15:27:53 +0800 (CST) Received: from localhost (10.177.246.209) by szxeml431-hub.china.huawei.com (10.82.67.208) with Microsoft SMTP Server id 14.3.235.1; Mon, 12 Dec 2016 15:27:44 +0800 From: "Longpeng(Mike)" To: , , , Date: Mon, 12 Dec 2016 15:27:19 +0800 Message-ID: <1481527639-17520-8-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 In-Reply-To: <1481527639-17520-1-git-send-email-longpeng2@huawei.com> References: <1481527639-17520-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: 58.251.152.64 Subject: [Qemu-devel] [PATCH for-2.9 v1 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" This patch add HMAC algorithms testcases Signed-off-by: Longpeng(Mike) --- tests/Makefile.include | 2 + tests/test-crypto-hmac.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 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..08e1887 --- /dev/null +++ b/tests/test-crypto-hmac.c @@ -0,0 +1,162 @@ +/* + * 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; + + 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(); +}