aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Berger <stefanb@linux.ibm.com>2021-07-09 11:39:02 -0400
committerAlexey Kardashevskiy <aik@ozlabs.ru>2021-07-11 23:46:49 +1000
commit62e0d4153468bc29873a34346c945726cd3c197d (patch)
treee86a2383b0c4cc44536f38e59311633e55e6d17c
parent89509533ace768d593fb4eab4293a2a5e24839ab (diff)
downloadSLOF-62e0d4153468bc29873a34346c945726cd3c197d.zip
SLOF-62e0d4153468bc29873a34346c945726cd3c197d.tar.gz
SLOF-62e0d4153468bc29873a34346c945726cd3c197d.tar.bz2
tcgbios: Add test cases and test script to run them
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 <stefanb@linux.ibm.com> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
-rw-r--r--lib/libtpm/sha.c27
-rw-r--r--lib/libtpm/sha256.c26
-rw-r--r--lib/libtpm/sha512.c36
-rw-r--r--lib/libtpm/sha_test.h59
-rwxr-xr-xlib/libtpm/test.sh31
5 files changed, 179 insertions, 0 deletions
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 <stdio.h>
+
+#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