[SLOF] [PATCH 4/4] Add testprograms

Stefan Berger stefanb at linux.ibm.com
Wed Jun 16 00:41:15 AEST 2021


---
 lib/libtpm/sha.c    | 42 ++++++++++++++++++++++++++++++
 lib/libtpm/sha512.c | 62 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+)

diff --git a/lib/libtpm/sha.c b/lib/libtpm/sha.c
index 98ab300..d8330f4 100644
--- a/lib/libtpm/sha.c
+++ b/lib/libtpm/sha.c
@@ -197,3 +197,45 @@ 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 <stdio.h>
+// gcc -DMAIN sha.c -o sha-test -I ../../include -I ../../slof -I ../../lib/libc/include
+int main(void)
+{
+    unsigned i, j;
+    uint8_t hash[20];
+    char buffer[70];
+
+    sha1("abc", 3, hash);
+    for (i = 0; i < sizeof(hash); i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+    sha1("", 0, hash);
+    for (i = 0; i < sizeof(hash); i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+    sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 448/8, hash);
+    for (i = 0; i < sizeof(hash); i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+    for (i = 54; i < sizeof(buffer); i++) {
+        memset(buffer, 0, sizeof(buffer));
+        for (j = 0; j <= i; j++)
+	    buffer[j] = 'a';
+        printf("input %d: %s\n", i, buffer);
+        sha1(buffer, i, hash);
+        for (j = 0; j < sizeof(hash); j++) {
+            printf("%02x", hash[j]);
+        }
+        printf("\n");
+    }
+}
+#endif
diff --git a/lib/libtpm/sha512.c b/lib/libtpm/sha512.c
index a59f659..addeacf 100644
--- a/lib/libtpm/sha512.c
+++ b/lib/libtpm/sha512.c
@@ -241,3 +241,65 @@ 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 <stdio.h>
+// gcc -DMAIN sha512.c -o sha512-test -I ../../include -I ../../slof -I ../../lib/libc/include
+int main(void)
+{
+    unsigned i, j;
+    uint8_t hash[64];
+    char buffer[128];
+
+    sha512("abc", 3, hash);
+    for (i = 0; i < sizeof(hash); i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+    sha384("abc", 3, hash);
+    for (i = 0; i < 384/8; i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+    sha512("", 0, hash);
+    for (i = 0; i < sizeof(hash); i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+    sha384("", 0, hash);
+    for (i = 0; i < 384/8; i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+    sha512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 448/8, hash);
+    for (i = 0; i < sizeof(hash); i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+    sha384("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 448/8, hash);
+    for (i = 0; i < 384/8; i++) {
+        printf("%02x", hash[i]);
+    }
+    printf("\n");
+
+
+    for (i = 110; i < sizeof(buffer); i++) {
+        memset(buffer, 0, sizeof(buffer));
+        for (j = 0; j <= i; j++)
+	    buffer[j] = 'a';
+        printf("input %d: %s\n", i, buffer);
+        sha512(buffer, i, hash);
+        for (j = 0; j < sizeof(hash); j++) {
+            printf("%02x", hash[j]);
+        }
+        printf("\n");
+    }
+}
+#endif
-- 
2.17.1



More information about the SLOF mailing list