From 370d37318cd47796da86ff3aa9d9c0c8f5d0910a Mon Sep 17 00:00:00 2001 From: Loup Vaillant Date: Wed, 19 Jul 2017 23:31:35 +0200 Subject: [PATCH] added self consistency tests for sha512 --- makefile | 2 +- tests/properties.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/makefile b/makefile index 3153509..bcdd9f8 100644 --- a/makefile +++ b/makefile @@ -32,7 +32,7 @@ clean: rm -f vectors properties sodium donna speed TEST_DEPS=tests/vectors.c bin/monocypher.o bin/sha512.o -PROP_DEPS=tests/properties.c bin/classic_monocypher.o +PROP_DEPS=tests/properties.c bin/classic_monocypher.o bin/sha512.o GEN_HEADERS=bin/argon2i.h \ bin/blake2b.h \ bin/blake2b_easy.h \ diff --git a/tests/properties.c b/tests/properties.c index 9803ca6..eed3a4a 100644 --- a/tests/properties.c +++ b/tests/properties.c @@ -197,6 +197,43 @@ static int blake2b() return status; } +// Tests that hashing bit by bit yields the same hash than hashing all +// at once. (for sha512) +static int sha512() +{ + static const size_t block_size = 128; // Blake2b block size + static const size_t input_size = block_size * 4; // total input size + static const size_t c_max_size = block_size * 2; // maximum chunk size + int status = 0; + FOR (i, 0, 1000) { + size_t offset = 0; + // outputs + u8 hash_chunk[64]; + u8 hash_whole[64]; + // inputs + u8 input[input_size]; p_random(input, input_size); + + // Authenticate bit by bit + crypto_sha512_ctx ctx; + crypto_sha512_init(&ctx); + while (1) { + size_t chunk_size = rand64() % c_max_size; + if (offset + chunk_size > input_size) { break; } + crypto_sha512_update(&ctx, input + offset, chunk_size); + offset += chunk_size; + } + crypto_sha512_final(&ctx, hash_chunk); + + // Authenticate all at once + crypto_sha512(hash_whole, input, offset); + + // Compare the results (must be the same) + status |= crypto_memcmp(hash_chunk, hash_whole, 64); + } + printf("%s: Sha512\n", status != 0 ? "FAILED" : "OK"); + return status; +} + static int aead() { int status = 0; @@ -240,6 +277,7 @@ int main(void) status |= chacha20_set_ctr(); status |= poly1305(); status |= blake2b(); + status |= sha512(); status |= aead(); return status; } -- 2.47.3