Skip to content
Snippets Groups Projects
Commit 17bbcd3b authored by Niels Möller's avatar Niels Möller
Browse files

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.
parent e8462c8a
No related branches found
No related tags found
No related merge requests found
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:
......
#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"));
......
#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"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment