From e41b7a06e4455d815064facba48f52df76f172e9 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Thu, 15 Oct 2015 00:35:19 +0300 Subject: [PATCH] sha1: moved self test to separated test --- .gitignore | 1 + Makefile.am | 6 +- src/sha1.c | 151 ---------------------------------------------- tests/test_sha1.c | 79 ++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 152 deletions(-) create mode 100644 tests/test_sha1.c diff --git a/.gitignore b/.gitignore index 45f7661..438ab53 100644 --- a/.gitignore +++ b/.gitignore @@ -48,6 +48,7 @@ tests/test_jid tests/test_rand tests/test_sasl tests/test_scram +tests/test_sha1 tests/test_sock m4/ libstrophe.project diff --git a/Makefile.am b/Makefile.am index 1947bff..9af4ce5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,7 +65,8 @@ examples_uuid_LDADD = $(STROPHE_LIBS) ## Tests -TESTS = tests/check_parser tests/test_rand tests/test_scram tests/test_base64 +TESTS = tests/check_parser tests/test_sha1 tests/test_rand tests/test_scram \ + tests/test_base64 check_PROGRAMS = $(TESTS) tests_check_parser_SOURCES = tests/check_parser.c tests/test.h @@ -84,3 +85,6 @@ tests_test_rand_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src tests_test_scram_SOURCES = tests/test_scram.c tests/test.c src/sha1.c tests_test_scram_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src + +tests_test_sha1_SOURCES = tests/test_sha1.c src/sha1.c +tests_test_sha1_CFLAGS = -I$(top_srcdir)/src diff --git a/src/sha1.c b/src/sha1.c index f3559d8..45beefa 100644 --- a/src/sha1.c +++ b/src/sha1.c @@ -70,24 +70,11 @@ use SHA1_ prefix for public api move public api to sha1.h */ -/* -Test Vectors (from FIPS PUB 180-1) -"abc" - A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D -"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 -A million repetitions of "a" - 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F -*/ - /* #define SHA1HANDSOFF */ -#include #include -/* make sure the stdint.h types are available */ #include "ostypes.h" - #include "sha1.h" static uint32_t host_to_be(uint32_t i); @@ -109,19 +96,6 @@ static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]); #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); -#ifdef VERBOSE /* SAK */ -void SHAPrintContext(SHA1_CTX *context, char *msg){ - printf("%s (%d,%d) %x %x %x %x %x\n", - msg, - context->count[0], context->count[1], - context->state[0], - context->state[1], - context->state[2], - context->state[3], - context->state[4]); -} -#endif /* VERBOSE */ - static uint32_t host_to_be(uint32_t i) { #define le_to_be(i) ((rol((i),24) & 0xFF00FF00) | (rol((i),8) & 0x00FF00FF)) @@ -220,10 +194,6 @@ void crypto_SHA1_Update(SHA1_CTX* context, const uint8_t* data, { size_t i, j; -#ifdef VERBOSE - SHAPrintContext(context, "before"); -#endif - j = (context->count[0] >> 3) & 63; if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; context->count[1] += (len >> 29); @@ -237,10 +207,6 @@ void crypto_SHA1_Update(SHA1_CTX* context, const uint8_t* data, } else i = 0; memcpy(&context->buffer[j], &data[i], len - i); - -#ifdef VERBOSE - SHAPrintContext(context, "after "); -#endif } @@ -284,120 +250,3 @@ void crypto_SHA1(const uint8_t* data, size_t len, uint8_t* digest) crypto_SHA1_Update(&ctx, data, len); crypto_SHA1_Final(&ctx, digest); } - -/*************************************************************/ - -#if 0 -int main(int argc, char** argv) -{ -int i, j; -SHA1_CTX context; -unsigned char digest[SHA1_DIGEST_SIZE], buffer[16384]; -FILE* file; - - if (argc > 2) { - puts("Public domain SHA-1 implementation - by Steve Reid "); - puts("Modified for 16 bit environments 7/98 - by James H. Brown "); /* JHB */ - puts("Produces the SHA-1 hash of a file, or stdin if no file is specified."); - return(0); - } - if (argc < 2) { - file = stdin; - } - else { - if (!(file = fopen(argv[1], "rb"))) { - fputs("Unable to open file.", stderr); - return(-1); - } - } - crypto_SHA1_Init(&context); - while (!feof(file)) { /* note: what if ferror(file) */ - i = fread(buffer, 1, 16384, file); - crypto_SHA1_Update(&context, buffer, i); - } - crypto_SHA1_Final(&context, digest); - fclose(file); - for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) { - for (j = 0; j < 4; j++) { - printf("%02X", digest[i*4+j]); - } - putchar(' '); - } - putchar('\n'); - return(0); /* JHB */ -} -#endif - -/* self test */ - -#ifdef TEST - -static char *test_data[] = { - "abc", - "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "A million repetitions of 'a'"}; -static char *test_results[] = { - "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D", - "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1", - "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"}; - - -void digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output) -{ - int i,j; - char *c = output; - - for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) { - for (j = 0; j < 4; j++) { - sprintf(c,"%02X", digest[i*4+j]); - c += 2; - } - sprintf(c, " "); - c += 1; - } - *(c - 1) = '\0'; -} - -int main(int argc, char** argv) -{ - int k; - SHA1_CTX context; - uint8_t digest[20]; - char output[80]; - - fprintf(stdout, "verifying SHA-1 implementation... "); - - for (k = 0; k < 2; k++){ - crypto_SHA1_Init(&context); - crypto_SHA1_Update(&context, (uint8_t*)test_data[k], - strlen(test_data[k])); - crypto_SHA1_Final(&context, digest); - digest_to_hex(digest, output); - - if (strcmp(output, test_results[k])) { - fprintf(stdout, "FAIL\n"); - fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]); - fprintf(stderr,"\t%s returned\n", output); - fprintf(stderr,"\t%s is correct\n", test_results[k]); - return (1); - } - } - /* million 'a' vector we feed separately */ - crypto_SHA1_Init(&context); - for (k = 0; k < 1000000; k++) - crypto_SHA1_Update(&context, (uint8_t*)"a", 1); - crypto_SHA1_Final(&context, digest); - digest_to_hex(digest, output); - if (strcmp(output, test_results[2])) { - fprintf(stdout, "FAIL\n"); - fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]); - fprintf(stderr,"\t%s returned\n", output); - fprintf(stderr,"\t%s is correct\n", test_results[2]); - return (1); - } - - /* success */ - fprintf(stdout, "ok\n"); - return(0); -} -#endif /* TEST */ diff --git a/tests/test_sha1.c b/tests/test_sha1.c new file mode 100644 index 0000000..5150616 --- /dev/null +++ b/tests/test_sha1.c @@ -0,0 +1,79 @@ +/* Tests for Steve Reid's public domain SHA-1 implementation */ +/* This file is in the public domain */ + +/* gcc -o test_sha1 -I./src tests/test_sha1.c src/sha1.c */ + +#include +#include + +#include "sha1.h" + +/* Test Vectors (from FIPS PUB 180-1) */ +static char *test_data[] = { + "abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "A million repetitions of 'a'"}; +static char *test_results[] = { + "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D", + "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1", + "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"}; + + +static void digest_to_hex(const uint8_t *digest, char *output) +{ + int i,j; + char *c = output; + + for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) { + for (j = 0; j < 4; j++) { + sprintf(c,"%02X", digest[i*4+j]); + c += 2; + } + sprintf(c, " "); + c += 1; + } + *(c - 1) = '\0'; +} + +int main(int argc, char** argv) +{ + int k; + SHA1_CTX context; + uint8_t digest[20]; + char output[80]; + + fprintf(stdout, "verifying SHA-1 implementation... "); + + for (k = 0; k < 2; k++){ + crypto_SHA1_Init(&context); + crypto_SHA1_Update(&context, (uint8_t*)test_data[k], + strlen(test_data[k])); + crypto_SHA1_Final(&context, digest); + digest_to_hex(digest, output); + + if (strcmp(output, test_results[k])) { + fprintf(stdout, "FAIL\n"); + fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]); + fprintf(stderr,"\t%s returned\n", output); + fprintf(stderr,"\t%s is correct\n", test_results[k]); + return (1); + } + } + /* million 'a' vector we feed separately */ + crypto_SHA1_Init(&context); + for (k = 0; k < 1000000; k++) + crypto_SHA1_Update(&context, (uint8_t*)"a", 1); + crypto_SHA1_Final(&context, digest); + digest_to_hex(digest, output); + if (strcmp(output, test_results[2])) { + fprintf(stdout, "FAIL\n"); + fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]); + fprintf(stderr,"\t%s returned\n", output); + fprintf(stderr,"\t%s is correct\n", test_results[2]); + return (1); + } + + /* success */ + fprintf(stdout, "ok\n"); + return(0); +}