summaryrefslogtreecommitdiff
path: root/test/c/sha1.c
diff options
context:
space:
mode:
authorGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2006-09-08 15:43:41 +0000
committerGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2006-09-08 15:43:41 +0000
commita5b33dcab2e6218e9e17f36a26520fd1dabc58bb (patch)
tree93f6b4595b7ba079ed3517b7bc07e50c3049adcf /test/c/sha1.c
parent43b4d97a655e52e3962c0d14bda39dacb24af901 (diff)
MAJ des tests C
git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@86 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
Diffstat (limited to 'test/c/sha1.c')
-rw-r--r--test/c/sha1.c70
1 files changed, 64 insertions, 6 deletions
diff --git a/test/c/sha1.c b/test/c/sha1.c
index 024e8ad..ec03b7d 100644
--- a/test/c/sha1.c
+++ b/test/c/sha1.c
@@ -2,12 +2,8 @@
/* Ref: Handbook of Applied Cryptography, section 9.4.2, algorithm 9.53 */
#include <string.h>
-
-extern void memcpy_static(void *, void *, int),
- memset_static(void *, int, char);
-
-#define memcpy memcpy_static
-#define memset memset_static
+#include <stdio.h>
+#include <stdlib.h>
#define ARCH_BIG_ENDIAN
@@ -169,3 +165,65 @@ void SHA1_finish(struct SHA1Context * ctx, unsigned char output[20])
/* Final hash value is in ctx->state modulo big-endian conversion */
SHA1_copy_and_swap(ctx->state, output, 5);
}
+
+/* Test harness */
+
+static void do_test(unsigned char * txt, unsigned char * expected_output)
+{
+ struct SHA1Context ctx;
+ unsigned char output[20];
+ int ok;
+
+ SHA1_init(&ctx);
+ SHA1_add_data(&ctx, txt, strlen((char *) txt));
+ SHA1_finish(&ctx, output);
+ ok = memcmp(output, expected_output, 20) == 0;
+ printf("Test `%s': %s\n",
+ (char *) txt, (ok ? "passed" : "FAILED"));
+}
+
+/* Test vectors:
+ *
+ * "abc"
+ * A999 3E36 4706 816A BA3E 2571 7850 C26C 9CD0 D89D
+ *
+ * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+ * 8498 3E44 1C3B D26E BAAE 4AA1 F951 29E5 E546 70F1
+ */
+
+unsigned char test_input_1[] = "abc";
+
+unsigned char test_output_1[20] =
+{ 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E ,
+ 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D };
+
+unsigned char test_input_2[] =
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+
+unsigned char test_output_2[20] =
+{ 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
+ 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 };
+
+
+static void do_bench(int nblocks)
+{
+ struct SHA1Context ctx;
+ unsigned char output[20];
+ unsigned char data[64];
+
+ SHA1_init(&ctx);
+ for (; nblocks > 0; nblocks--)
+ SHA1_add_data(&ctx, data, 64);
+ SHA1_finish(&ctx, output);
+}
+
+int main(int argc, char ** argv)
+{
+ if (argc < 2) {
+ do_test(test_input_1, test_output_1);
+ do_test(test_input_2, test_output_2);
+ } else {
+ do_bench(atoi(argv[1]));
+ }
+ return 0;
+}