From a5b33dcab2e6218e9e17f36a26520fd1dabc58bb Mon Sep 17 00:00:00 2001 From: xleroy Date: Fri, 8 Sep 2006 15:43:41 +0000 Subject: MAJ des tests C git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@86 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e --- test/c/sha1.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-) (limited to 'test/c/sha1.c') diff --git a/test/c/sha1.c b/test/c/sha1.c index 024e8ad5..ec03b7d9 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 - -extern void memcpy_static(void *, void *, int), - memset_static(void *, int, char); - -#define memcpy memcpy_static -#define memset memset_static +#include +#include #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; +} -- cgit