From d83d4a4bf11ab0d052932f7e98bf843aee434f11 Mon Sep 17 00:00:00 2001 From: "Matthias J. Kannwischer" Date: Tue, 10 Sep 2019 11:27:23 +0200 Subject: [PATCH 1/2] add test for incremental sha512 --- test/test_common/sha2.c | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/test_common/sha2.c b/test/test_common/sha2.c index b04f59a2..41f3bb3f 100644 --- a/test/test_common/sha2.c +++ b/test/test_common/sha2.c @@ -7,6 +7,20 @@ const unsigned char plaintext[113] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; +const unsigned char plaintext2[128] = { + 0xfd, 0x22, 0x03, 0xe4, 0x67, 0x57, 0x4e, 0x83, 0x4a, 0xb0, 0x7c, 0x90, + 0x97, 0xae, 0x16, 0x45, 0x32, 0xf2, 0x4b, 0xe1, 0xeb, 0x5d, 0x88, 0xf1, + 0xaf, 0x77, 0x48, 0xce, 0xff, 0x0d, 0x2c, 0x67, 0xa2, 0x1f, 0x4e, 0x40, + 0x97, 0xf9, 0xd3, 0xbb, 0x4e, 0x9f, 0xbf, 0x97, 0x18, 0x6e, 0x0d, 0xb6, + 0xdb, 0x01, 0x00, 0x23, 0x0a, 0x52, 0xb4, 0x53, 0xd4, 0x21, 0xf8, 0xab, + 0x9c, 0x9a, 0x60, 0x43, 0xaa, 0x32, 0x95, 0xea, 0x20, 0xd2, 0xf0, 0x6a, + 0x2f, 0x37, 0x47, 0x0d, 0x8a, 0x99, 0x07, 0x5f, 0x1b, 0x8a, 0x83, 0x36, + 0xf6, 0x22, 0x8c, 0xf0, 0x8b, 0x59, 0x42, 0xfc, 0x1f, 0xb4, 0x29, 0x9c, + 0x7d, 0x24, 0x80, 0xe8, 0xe8, 0x2b, 0xce, 0x17, 0x55, 0x40, 0xbd, 0xfa, + 0xd7, 0x75, 0x2b, 0xc9, 0x5b, 0x57, 0x7f, 0x22, 0x95, 0x15, 0x39, 0x4f, + 0x3a, 0xe5, 0xce, 0xc8, 0x70, 0xa4, 0xb2, 0xf8 +}; + const unsigned char expected_224[28] = { 0xc9, 0x7c, 0xa9, 0xa5, 0x59, 0x85, 0x0c, 0xe9, 0x7a, 0x04, 0xa9, 0x6d, 0xef, 0x6d, 0x99, 0xa9, 0xe0, 0xe0, 0xe2, 0xab, 0x14, 0xe6, 0xb8, 0xdf, @@ -34,6 +48,14 @@ const unsigned char expected_512[64] = { 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 }; +const unsigned char expected_512_2[64] = { + 0xa2, 0x1b, 0x10, 0x77, 0xd5, 0x2b, 0x27, 0xac, 0x54, 0x5a, 0xf6, 0x3b, + 0x32, 0x74, 0x6c, 0x6e, 0x3c, 0x51, 0xcb, 0x0c, 0xb9, 0xf2, 0x81, 0xeb, + 0x9f, 0x35, 0x80, 0xa6, 0xd4, 0x99, 0x6d, 0x5c, 0x99, 0x17, 0xd2, 0xa6, + 0xe4, 0x84, 0x62, 0x7a, 0x9d, 0x5a, 0x06, 0xfa, 0x1b, 0x25, 0x32, 0x7a, + 0x9d, 0x71, 0x0e, 0x02, 0x73, 0x87, 0xfc, 0x3e, 0x07, 0xd7, 0xc4, 0xd1, + 0x4c, 0x60, 0x86, 0xcc +}; static int test_sha256_incremental(void) { unsigned char output[32]; @@ -62,6 +84,31 @@ static int test_sha256_incremental(void) { return 0; } +static int test_sha512_incremental(void) { + unsigned char output[64]; + sha512ctx state; + int i = 0; + + sha512_inc_init(&state); + sha512_inc_blocks(&state, plaintext2, 1); + sha512_inc_finalize(output, &state, NULL, 0); + if (memcmp(expected_512_2, output, 64) != 0) { + printf("ERROR sha512 incremental output did not match test vector.\n"); + printf("Expected: "); + for (i = 0; i < 64; i++) { + printf("%02X", expected_512[i]); + } + printf("\n"); + printf("Received: "); + for (i = 0; i < 64; i++) { + printf("%02X", output[i]); + } + printf("\n"); + return 1; + } + return 0; +} + static int test_sha224(void) { unsigned char output[28]; int i = 0; @@ -165,6 +212,7 @@ int main(void) { result += test_sha256_incremental(); result += test_sha384(); result += test_sha512(); + result += test_sha512_incremental(); if (result != 0) { puts("Errors occurred"); From 6ee774a5933bbaa07711f62262511d46c8d0daf9 Mon Sep 17 00:00:00 2001 From: "Matthias J. Kannwischer" Date: Tue, 10 Sep 2019 11:27:37 +0200 Subject: [PATCH 2/2] fix incremental sha512 --- common/sha2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/sha2.c b/common/sha2.c index c7214345..99773a42 100644 --- a/common/sha2.c +++ b/common/sha2.c @@ -561,7 +561,7 @@ void sha224_inc_blocks(sha224ctx *state, const uint8_t *in, size_t inblocks) { void sha512_inc_blocks(sha512ctx *state, const uint8_t *in, size_t inblocks) { uint64_t bytes = load_bigendian_64(state->ctx + 64); - crypto_hashblocks_sha256(state->ctx, in, 128 * inblocks); + crypto_hashblocks_sha512(state->ctx, in, 128 * inblocks); bytes += 128 * inblocks; store_bigendian_64(state->ctx + 64, bytes);