From patchwork Fri Jul 9 15:39:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 1503247 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=Wti5Qm7v; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GLy574l22z9sWX for ; Sat, 10 Jul 2021 01:39:23 +1000 (AEST) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4GLy573gzrz30B2 for ; Sat, 10 Jul 2021 01:39:23 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=Wti5Qm7v; dkim-atps=neutral X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=linux.vnet.ibm.com (client-ip=148.163.158.5; helo=mx0b-001b2d01.pphosted.com; envelope-from=stefanb@linux.vnet.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=Wti5Qm7v; dkim-atps=neutral Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4GLy4z4yvCz2yyb for ; Sat, 10 Jul 2021 01:39:15 +1000 (AEST) Received: from pps.filterd (m0127361.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 169FXACF075697; Fri, 9 Jul 2021 11:39:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : mime-version; s=pp1; bh=dteMknbQ8+E1iOuVZKLdbVn6aTRtTX4j2sV5FNFGr2Q=; b=Wti5Qm7vbCp4RgRFQv+rkQuCLA5G+fiXjkz1yA5f1SH6YN/3x5e15m6pDzqQXdTCtQpQ fVtZyjqnYM/ciJZvyAXk7U0L57dWXwFqCjOghOtRyi8jErfFrQFyszFmvqZAdtl/tJyd t3zQjda8IcN+dW9yNRITcj75GlVVff5JussfC9oB4jdc5WCElQ8M/oqUvNoHKL1z2YEu q5epNnjVRTTsksIW1J4jN+xVbjiYmn4gMQuG0qyPuGbcb7aR436NEQ+/bdi9kkcbYcbF KB5ECVYWmwuvXoy7EPXQ3JJ21ZfT8SOjE8AXpNVpyzufALQo5Mhve3BB7HPuPQkUGRum cw== Received: from ppma04dal.us.ibm.com (7a.29.35a9.ip4.static.sl-reverse.com [169.53.41.122]) by mx0a-001b2d01.pphosted.com with ESMTP id 39p1ycq3dt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 09 Jul 2021 11:39:12 -0400 Received: from pps.filterd (ppma04dal.us.ibm.com [127.0.0.1]) by ppma04dal.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 169FSAXV000583; Fri, 9 Jul 2021 15:39:12 GMT Received: from b03cxnp07028.gho.boulder.ibm.com (b03cxnp07028.gho.boulder.ibm.com [9.17.130.15]) by ppma04dal.us.ibm.com with ESMTP id 39jfhfhn5u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 09 Jul 2021 15:39:12 +0000 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp07028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 169FdAUM41681394 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 9 Jul 2021 15:39:10 GMT Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3C96978070; Fri, 9 Jul 2021 15:39:10 +0000 (GMT) Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 02D237805E; Fri, 9 Jul 2021 15:39:10 +0000 (GMT) Received: from localhost.localdomain (unknown [9.47.158.152]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP; Fri, 9 Jul 2021 15:39:09 +0000 (GMT) From: Stefan Berger To: slof@lists.ozlabs.org Date: Fri, 9 Jul 2021 11:39:02 -0400 Message-Id: <20210709153903.691945-5-stefanb@linux.vnet.ibm.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210709153903.691945-1-stefanb@linux.vnet.ibm.com> References: <20210709153903.691945-1-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-GUID: nyJGfD9gBSsasPJ_jFL6HXdtytNNhlgM X-Proofpoint-ORIG-GUID: nyJGfD9gBSsasPJ_jFL6HXdtytNNhlgM X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391, 18.0.790 definitions=2021-07-09_09:2021-07-09, 2021-07-09 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 suspectscore=0 malwarescore=0 impostorscore=0 bulkscore=0 mlxlogscore=999 priorityscore=1501 adultscore=0 mlxscore=0 spamscore=0 phishscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107090078 Subject: [SLOF] [PATCH v4 4/5] tcgbios: Add test cases and test script to run them X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Patches for https://github.com/aik/SLOF" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Berger Errors-To: slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "SLOF" From: Stefan Berger Add test cases for sha1, sha256, sha384, and sha512 and a test script to run the test cases. The tests are passing on little and big endian machines (Fedora 28). Signed-off-by: Stefan Berger --- lib/libtpm/sha.c | 27 ++++++++++++++++++++ lib/libtpm/sha256.c | 26 +++++++++++++++++++ lib/libtpm/sha512.c | 36 ++++++++++++++++++++++++++ lib/libtpm/sha_test.h | 59 +++++++++++++++++++++++++++++++++++++++++++ lib/libtpm/test.sh | 31 +++++++++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 lib/libtpm/sha_test.h create mode 100755 lib/libtpm/test.sh diff --git a/lib/libtpm/sha.c b/lib/libtpm/sha.c index 43de658..902a4ba 100644 --- a/lib/libtpm/sha.c +++ b/lib/libtpm/sha.c @@ -203,3 +203,30 @@ void sha1(const uint8_t *data, uint32_t length, uint8_t *hash) sha1_do(&ctx, data, length); memcpy(hash, &ctx.h[0], 20); } + +#ifdef MAIN + +#include "sha_test.h" + +int main(void) +{ + TESTVECTORS(data); + uint8_t hash[20]; + char input[64]; + int err = 0; + size_t i; + + for (i = 0; i < ARRAY_SIZE(data); i++) + err |= test_hash(sha1, hash, sizeof(hash), + data[i], strlen(data[i]), + SHA1); + + memset(input, 'a', sizeof(input)); + /* cover critical input size around 56 bytes */ + for (i = 50; i < sizeof(input); i++) + err |= test_hash(sha1, hash, sizeof(hash), + input, i, SHA1); + + return err; +} +#endif diff --git a/lib/libtpm/sha256.c b/lib/libtpm/sha256.c index 1a0aa9a..79bcb83 100644 --- a/lib/libtpm/sha256.c +++ b/lib/libtpm/sha256.c @@ -218,3 +218,29 @@ void sha256(const uint8_t *data, uint32_t length, uint8_t *hash) sha256_do(&ctx, data, length); memcpy(hash, ctx.h, sizeof(ctx.h)); } + +#ifdef MAIN + +#include "sha_test.h" + +int main(void) +{ + TESTVECTORS(data); + uint8_t hash[32]; + char input[64]; + int err = 0; + size_t i; + + for (i = 0; i < ARRAY_SIZE(data); i++) + err |= test_hash(sha256, hash, sizeof(hash), + data[i], strlen(data[i]), + SHA256); + + memset(input, 'a', sizeof(input)); + /* cover critical input size around 56 bytes */ + for (i = 50; i < sizeof(input); i++) + err |= test_hash(sha256, hash, sizeof(hash), input, i, SHA256); + + return err; +} +#endif diff --git a/lib/libtpm/sha512.c b/lib/libtpm/sha512.c index f9267ef..86831ab 100644 --- a/lib/libtpm/sha512.c +++ b/lib/libtpm/sha512.c @@ -247,3 +247,39 @@ void sha512(const uint8_t *data, uint32_t length, uint8_t *hash) sha512_do(&ctx, data, length); memcpy(hash, ctx.h, sizeof(ctx.h)); } + + +#ifdef MAIN + +#include "sha_test.h" + +int main(void) +{ + TESTVECTORS(data); + uint8_t hash512[64]; + uint8_t hash384[48]; + char input[128]; + int err = 0; + size_t i; + + for (i = 0; i < ARRAY_SIZE(data); i++) { + err |= test_hash(sha384, hash384, sizeof(hash384), + data[i], strlen(data[i]), + SHA384); + err |= test_hash(sha512, hash512, sizeof(hash512), + data[i], strlen(data[i]), + SHA512); + } + + memset(input, 'a', sizeof(input)); + /* cover critical input size around 112 bytes */ + for (i = 110; i < sizeof(input); i++) { + err |= test_hash(sha384, hash384, sizeof(hash384), + input, i, SHA384); + err |= test_hash(sha512, hash512, sizeof(hash512), + input, i, SHA512); + } + + return err; +} +#endif diff --git a/lib/libtpm/sha_test.h b/lib/libtpm/sha_test.h new file mode 100644 index 0000000..af82fac --- /dev/null +++ b/lib/libtpm/sha_test.h @@ -0,0 +1,59 @@ +/***************************************************************************** + * Copyright (c) 2021 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#ifndef SHA_TEST_H +#define SHA_TEST_H + +#include + +#include "helpers.h" + +/* to avoid compilation issues do not include openssl/sha.h */ +unsigned char *SHA1(const unsigned char *, size_t, unsigned char *); +unsigned char *SHA256(const unsigned char *, size_t, unsigned char *); +unsigned char *SHA384(const unsigned char *, size_t, unsigned char *); +unsigned char *SHA512(const unsigned char *, size_t, unsigned char *); + +typedef void (*hashfunc)(const uint8_t *data, uint32_t length, uint8_t *hash); +typedef unsigned char *(*osslhashfunc)(const unsigned char *, size_t, + unsigned char *); + +#define TESTVECTORS(NAME) \ +char *NAME[] = { \ + "", \ + "abc", \ + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", \ + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" \ +}; + +static inline int +test_hash(hashfunc hf, uint8_t *hash, size_t hashlen, + const char *data, uint32_t length, + osslhashfunc osslhf) +{ + unsigned char expected[hashlen]; + int ret = 0; + + osslhf((const unsigned char *)data, length, expected); + + hf((uint8_t *)data, length, hash); + if (!memcmp(hash, expected, hashlen)) { + printf("PASS: input length: %u\n", length); + } else { + printf("FAIL data: %s\n", data); + ret = 1; + } + + return ret; +} + +#endif /* SHA_TEST_H */ diff --git a/lib/libtpm/test.sh b/lib/libtpm/test.sh new file mode 100755 index 0000000..4b0567a --- /dev/null +++ b/lib/libtpm/test.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +cd $(dirname "$0") + +CC=${HOSTCC:-gcc} +CFLAGS="-Wall -Wextra -Werror -I../../include -I../../slof -I../../lib/libc/include -DMAIN" +LDFLAGS="-lcrypto" + +function fail() { + rm -f ${EXEC} + echo "Test failed" + exit 1 +} + +function run_test() { + local msg="$1" + local src="$2" + + EXEC="./${src%%.c}-test" + + echo ${msg} + ${CC} ${CFLAGS} ${src} -o ${EXEC} ${LDFLAGS} || exit 1 + ${EXEC} || fail + rm -f ${EXEC} +} + +run_test "SHA-1 test:" sha.c +run_test "SHA-256 test:" sha256.c +run_test "SHA-384 & SHA-512 test:" sha512.c + +echo "All tests passed" +exit 0