Skip to content

Commit

Permalink
Add internal digest compared to expected value.
Browse files Browse the repository at this point in the history
  • Loading branch information
pemensik committed Dec 3, 2024
1 parent 51171e3 commit d77ec94
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions test/test_digest.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@

typedef unsigned char * (*ldns_md_f)(digest_data_t *data, unsigned int data_len, unsigned char *digest);

const unsigned char test_dg_sha1[] = { 0xf2,0x7e,0x9f,0xc6,0x96,0x91,0x51,0x42,0xa0,0x8c,0x47,0x81,0x01,0x75,0xa5,0x42,0xf0,0x6e,0x7c,0xa5 };
const unsigned char test_dg_sha256[] = { 0x11,0xbf,0xfb,0x5c,0xd0,0x16,0x14,0x8d,0xce,0xdc,0x72,0xa5,0xa9,0xd8,0x14,0xab,0xfd,0x88,0x2e,
0xa8,0xd8,0x55,0x27,0xaf,0xc4,0xef,0xed,0x6a,0x3f,0x81,0x9c,0x1d };
const unsigned char test_dg_sha384[] = { 0x8f,0xa5,0xa2,0x34,0x17,0x72,0x19,0x62,0x9b,0x1c,0x6e,0x79,0x35,0xc7,0x07,0x49,0x24,0x18,0xf5,0xe3,
0x9f,0xcf,0x83,0x6a,0x20,0xae,0x45,0xc8,0xad,0xfa,0x4a,0xd2,0xa1,0x51,0xd0,0xb8,0x10,0xd3,0xee,0x83,
0xeb,0x3e,0xb6,0x33,0xa0,0xdd,0xc3,0xf9 };
const unsigned char test_dg_sha512[] = { 0xd9,0xf4,0x3d,0x10,0x9d,0xb3,0x9a,0x18,0x5f,0x95,0x3b,0xfe,0x90,0xfa,0xf2,0xd5,0x69,0xae,0x99,0x19,
0x43,0x5e,0x03,0x7e,0xb8,0x0f,0xc4,0xdf,0x10,0xd7,0x77,0xf7,0x2d,0x82,0xa2,0xe4,0xf6,0x91,0x88,0xcc,
0xd4,0x78,0xbe,0xec,0xd7,0x02,0x18,0x61,0xb0,0x57,0xbb,0x15,0x1e,0x79,0x9a,0xf2,0xfa,0x40,0xb2,0xb1,
0xa3,0xd8,0x74,0x89 };

static void print_hex(const unsigned char *digest, unsigned int digest_len)
{
for (size_t i = 0; i < digest_len; i++)
Expand All @@ -68,11 +79,12 @@ static void print_hex(const unsigned char *digest, unsigned int digest_len)
printf("\n");
}

static int test_md(ldns_md_f ldns_md, unsigned int digest_len, const char *md_name, unsigned char *sign_buf, size_t sign_len)
static int test_md(ldns_md_f ldns_md, unsigned int digest_len, const char *md_name,
unsigned char *sign_buf, size_t sign_len, const unsigned char *check)
{
unsigned char *digest = NULL;
#ifdef HAVE_OPENSSL_EVP_H
int match = 1;
#ifdef HAVE_OPENSSL_EVP_H
const EVP_MD *md = EVP_get_digestbyname(md_name);
unsigned char *digest_o = NULL;
#endif
Expand All @@ -82,6 +94,12 @@ static int test_md(ldns_md_f ldns_md, unsigned int digest_len, const char *md_na
printf("%-6s: ", md_name);
print_hex(digest, digest_len);

match = memcmp(digest, check, digest_len);
if (match == 0)
printf("Result %s matches stored digest.\n", md_name);
else
printf("Result %s DIFFERS from stored digest: %d\n", md_name, match);

#ifdef HAVE_OPENSSL_EVP_H
/* recheck output with openssl */
digest_o = calloc(1, digest_len);
Expand All @@ -95,7 +113,7 @@ static int test_md(ldns_md_f ldns_md, unsigned int digest_len, const char *md_na

free(digest);
free(digest_o);
return match;
return abs(match);
#else
/* print only ldns digest, nothing to compare it to. */
return 0;
Expand Down Expand Up @@ -131,10 +149,10 @@ int main(void)
}
printf("\n digests: ");
}
match += (test_md(&ldns_sha1, LDNS_SHA1_DIGEST_LENGTH, "sha1", sign_buf, sizeof(sign_buf)) != 0);
match += (test_md(&ldns_sha256, LDNS_SHA256_DIGEST_LENGTH, "sha256", sign_buf, sizeof(sign_buf)) != 0);
match += (test_md(&ldns_sha384, LDNS_SHA384_DIGEST_LENGTH, "sha384", sign_buf, sizeof(sign_buf)) != 0);
match += (test_md(&ldns_sha512, LDNS_SHA512_DIGEST_LENGTH, "sha512", sign_buf, sizeof(sign_buf)) != 0);
match += (test_md(&ldns_sha1, LDNS_SHA1_DIGEST_LENGTH, "sha1", sign_buf, sizeof(sign_buf), test_dg_sha1) != 0);
match += (test_md(&ldns_sha256, LDNS_SHA256_DIGEST_LENGTH, "sha256", sign_buf, sizeof(sign_buf), test_dg_sha256) != 0);
match += (test_md(&ldns_sha384, LDNS_SHA384_DIGEST_LENGTH, "sha384", sign_buf, sizeof(sign_buf), test_dg_sha384) != 0);
match += (test_md(&ldns_sha512, LDNS_SHA512_DIGEST_LENGTH, "sha512", sign_buf, sizeof(sign_buf), test_dg_sha512) != 0);

return (abs(match));
}

0 comments on commit d77ec94

Please sign in to comment.