diff options
author | Niels Möller <nisse@lysator.liu.se> | 2022-06-20 21:05:40 +0200 |
---|---|---|
committer | Niels Möller <nisse@lysator.liu.se> | 2022-06-20 21:05:40 +0200 |
commit | 17bbcd3b021cca32744fc7aadee33de72c39b46b (patch) | |
tree | 7caade178917750c4d6cb8976969f9b9bda166fe | |
parent | e8462c8a96e3d47bff6e1e3b1b2b00b847f9328c (diff) | |
download | nettle-17bbcd3b021cca32744fc7aadee33de72c39b46b.tar.gz |
Additional tests for sha1 and sha256 compression.
* testsuite/sha1-test.c (test_sha1_compress): New function.
(test_main): Add tests for compressing 0, 1 or 2 blocks.
* testsuite/sha256-test.c (test_sha256_compress): New function.
(test_main): Add tests for compressing 0, 1 or 2 blocks.
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | testsuite/sha1-test.c | 52 | ||||
-rw-r--r-- | testsuite/sha256-test.c | 52 |
3 files changed, 111 insertions, 0 deletions
@@ -1,3 +1,10 @@ +2022-06-20 Niels Möller <nisse@lysator.liu.se> + + * testsuite/sha1-test.c (test_sha1_compress): New function. + (test_main): Add tests for compressing 0, 1 or 2 blocks. + * testsuite/sha256-test.c (test_sha256_compress): New function. + (test_main): Add tests for compressing 0, 1 or 2 blocks. + 2022-06-12 Niels Möller <nisse@lysator.liu.se> From Christian Weisgerber: diff --git a/testsuite/sha1-test.c b/testsuite/sha1-test.c index 8356555b..549363f8 100644 --- a/testsuite/sha1-test.c +++ b/testsuite/sha1-test.c @@ -1,8 +1,60 @@ #include "testutils.h" +#include "nettle-write.h" + +/* Test compression only. */ +static void +test_sha1_compress(const struct tstring *input, + const struct tstring *expected) { + size_t split; + + ASSERT (input->length % SHA1_BLOCK_SIZE == 0); + ASSERT (expected->length == SHA1_DIGEST_SIZE); + + for (split = 0; split <= input->length; split += SHA1_BLOCK_SIZE) + { + struct sha1_ctx ctx; + uint8_t digest[SHA1_DIGEST_SIZE]; + sha1_init (&ctx); + sha1_update (&ctx, split, input->data); + sha1_update (&ctx, input->length - split, input->data + split); + + _nettle_write_be32 (SHA1_DIGEST_SIZE, digest, ctx.state); + if (!MEMEQ (SHA1_DIGEST_SIZE, digest, expected->data)) { + fprintf (stderr, "sha1_compress failed: length %u, split %u \nInput:", + (unsigned) input->length, (unsigned) split); + tstring_print_hex (input); + fprintf (stderr, "\nOutput: "); + print_hex (SHA1_DIGEST_SIZE, digest); + fprintf(stdout, "\nExpected:\n"); + tstring_print_hex (expected); + fprintf (stderr, "\n"); + abort (); + } + } +} + void test_main(void) { + /* Initial state */ + test_sha1_compress (SDATA(""), + SHEX("67452301efcdab89 98badcfe10325476" + "c3d2e1f0")); + + /* Single block compressed */ + test_sha1_compress (SDATA("0123456789abcdefghijklmnopqrstuv" + "wxyzABCDEFGHIJKLMNOPQRSTUVWXYZZY"), + SHEX("005cf6fb02d9a17e f77d0b8eac9da60c" + "08ecaf1c")); + /* Two blocks compressed */ + test_sha1_compress (SDATA("0123456789abcdefghijklmnopqrstuv" + "wxyzABCDEFGHIJKLMNOPQRSTUVWXYZZY" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345" + "6789abcdefghijklmnopqrstuvwxyzzy"), + SHEX("3e0dd3db30fc4e45 c17a97f0c431f07b" + "6b4a5cc5")); + test_hash(&nettle_sha1, SDATA(""), SHEX("DA39A3EE5E6B4B0D 3255BFEF95601890 AFD80709")); diff --git a/testsuite/sha256-test.c b/testsuite/sha256-test.c index 551b140c..a85d5ba7 100644 --- a/testsuite/sha256-test.c +++ b/testsuite/sha256-test.c @@ -1,8 +1,60 @@ #include "testutils.h" +#include "nettle-write.h" + +/* Test compression only. */ +static void +test_sha256_compress(const struct tstring *input, + const struct tstring *expected) { + size_t split; + ASSERT (input->length % SHA256_BLOCK_SIZE == 0); + ASSERT (expected->length == SHA256_DIGEST_SIZE); + + for (split = 0; split <= input->length; split += SHA256_BLOCK_SIZE) + { + struct sha256_ctx ctx; + uint8_t digest[SHA256_DIGEST_SIZE]; + + sha256_init (&ctx); + sha256_update (&ctx, split, input->data); + sha256_update (&ctx, input->length - split, input->data + split); + + _nettle_write_be32 (SHA256_DIGEST_SIZE, digest, ctx.state); + if (!MEMEQ (SHA256_DIGEST_SIZE, digest, expected->data)) { + fprintf (stderr, "sha256_compress failed: length %u, split %u\nInput:", + (unsigned) input->length, (unsigned) split); + tstring_print_hex (input); + fprintf (stderr, "\nOutput: "); + print_hex (SHA256_DIGEST_SIZE, digest); + fprintf(stdout, "\nExpected:\n"); + tstring_print_hex (expected); + fprintf (stderr, "\n"); + abort (); + } + } +} + void test_main(void) { + /* Initial state */ + test_sha256_compress (SDATA(""), + SHEX("6a09e667 bb67ae85 3c6ef372 a54ff53a" + "510e527f 9b05688c 1f83d9ab 5be0cd19")); + + /* Single block compressed */ + test_sha256_compress (SDATA("0123456789abcdefghijklmnopqrstuv" + "wxyzABCDEFGHIJKLMNOPQRSTUVWXYZZY"), + SHEX("932558b453a68273 4daf0008efb6b5e5" + "32808baaf92bc749 2ac377107618ac67")); + /* Two blocks compressed */ + test_sha256_compress (SDATA("0123456789abcdefghijklmnopqrstuv" + "wxyzABCDEFGHIJKLMNOPQRSTUVWXYZZY" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345" + "6789abcdefghijklmnopqrstuvwxyzzy"), + SHEX("d82038b1732bbe97 94b879b41f98e9fc" + "2777fd8ab76737f5 60919c4fe1366c8e")); + /* From FIPS180-2 */ test_hash(&nettle_sha256, SDATA("abc"), SHEX("ba7816bf8f01cfea 414140de5dae2223" |