@@ -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
@@ -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
@@ -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
new file mode 100644
@@ -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 */
new file mode 100755
@@ -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