tests: updated SHA1 test
* added new testcase with 128 'a' * added check that SHA1 functions don't change original strings
This commit is contained in:
@@ -4,26 +4,30 @@
|
|||||||
/* gcc -o test_sha1 -I./src tests/test_sha1.c src/sha1.c */
|
/* gcc -o test_sha1 -I./src tests/test_sha1.c src/sha1.c */
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "sha1.h"
|
#include "sha1.h"
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
/* Test Vectors (from FIPS PUB 180-1) */
|
/* Test Vectors (from FIPS PUB 180-1) */
|
||||||
static char *test_data[] = {
|
static char *test_data[] = {
|
||||||
"abc",
|
"abc",
|
||||||
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
||||||
"A million repetitions of 'a'"};
|
"A million repetitions of 'a'",
|
||||||
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||||
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"};
|
||||||
static char *test_results[] = {
|
static char *test_results[] = {
|
||||||
"A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
|
"A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
|
||||||
"84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
|
"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)
|
static void digest_to_hex(const uint8_t *digest, char *output)
|
||||||
{
|
{
|
||||||
int i,j;
|
int i,j;
|
||||||
char *c = output;
|
char *c = output;
|
||||||
|
|
||||||
for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) {
|
for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) {
|
||||||
for (j = 0; j < 4; j++) {
|
for (j = 0; j < 4; j++) {
|
||||||
sprintf(c,"%02X", digest[i*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';
|
*(c - 1) = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int k;
|
int k;
|
||||||
SHA1_CTX context;
|
SHA1_CTX context;
|
||||||
uint8_t digest[20];
|
uint8_t digest[20];
|
||||||
char output[80];
|
char output[80];
|
||||||
|
char *copy;
|
||||||
|
|
||||||
fprintf(stdout, "verifying SHA-1 implementation... ");
|
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_Init(&context);
|
||||||
crypto_SHA1_Update(&context, (uint8_t*)test_data[k],
|
crypto_SHA1_Update(&context, (uint8_t*)test_data[k],
|
||||||
strlen(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 returned\n", output);
|
||||||
fprintf(stderr,"\t%s is correct\n", test_results[k]);
|
fprintf(stderr,"\t%s is correct\n", test_results[k]);
|
||||||
return (1);
|
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 */
|
/* million 'a' vector we feed separately */
|
||||||
crypto_SHA1_Init(&context);
|
crypto_SHA1_Init(&context);
|
||||||
|
|||||||
Reference in New Issue
Block a user