From 98c8afd5fca9c86127bbc694892c6bae6ecfa3bf Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Fri, 4 Dec 2015 09:57:09 +0200 Subject: [PATCH] tests: updated SHA1 test * added new testcase with 128 'a' * added check that SHA1 functions don't change original strings --- tests/test_sha1.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/tests/test_sha1.c b/tests/test_sha1.c index 5150616..33a0a26 100644 --- a/tests/test_sha1.c +++ b/tests/test_sha1.c @@ -4,26 +4,30 @@ /* gcc -o test_sha1 -I./src tests/test_sha1.c src/sha1.c */ #include +#include #include #include "sha1.h" +#include "test.h" /* Test Vectors (from FIPS PUB 180-1) */ static char *test_data[] = { "abc", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - "A million repetitions of 'a'"}; + "A million repetitions of 'a'", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}; static char *test_results[] = { "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D", "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1", - "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"}; - + "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F", + "AD5B3FDB CB526778 C2839D2F 151EA753 995E26A0"}; 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]); @@ -34,17 +38,23 @@ static void digest_to_hex(const uint8_t *digest, char *output) } *(c - 1) = '\0'; } - + int main(int argc, char** argv) { int k; SHA1_CTX context; uint8_t digest[20]; char output[80]; + char *copy; fprintf(stdout, "verifying SHA-1 implementation... "); - - for (k = 0; k < 2; k++){ + + for (k = 0; k < ARRAY_SIZE(test_data); k++){ + if (k == 2) { + /* this case will be checked below */ + continue; + } + copy = strdup(test_data[k]); crypto_SHA1_Init(&context); crypto_SHA1_Update(&context, (uint8_t*)test_data[k], strlen(test_data[k])); @@ -57,7 +67,13 @@ int main(int argc, char** argv) fprintf(stderr,"\t%s returned\n", output); fprintf(stderr,"\t%s is correct\n", test_results[k]); return (1); - } + } + if (strcmp(copy, test_data[k])) { + fprintf(stdout, "FAIL\n"); + fprintf(stdout, "* original string was changed by SHA1\n"); + return (1); + } + free(copy); } /* million 'a' vector we feed separately */ crypto_SHA1_Init(&context);