]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Additional tests for sha1 and sha256 compression.
authorNiels Möller <nisse@lysator.liu.se>
Mon, 20 Jun 2022 19:05:40 +0000 (21:05 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Mon, 20 Jun 2022 19:05:40 +0000 (21:05 +0200)
* 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.

ChangeLog
testsuite/sha1-test.c
testsuite/sha256-test.c

index f35b078b6d715c0d151f2fdfbf974c0f8d9a00d7..d4a20095a61e1b8e8650bb9e43d0999ae03ac3e3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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:
index 8356555b04b8285d6faa2987330c3c2b8b55cae6..549363f8988016d590e6efa36bfaec5b889fef42 100644 (file)
@@ -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")); 
 
index 551b140c83ed89ea8c5575494bbeacba32b00b39..a85d5ba78febf084dff92b59b38332e8f7c8e035 100644 (file)
@@ -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"