From: Anders F Björklund Date: Sun, 21 Jul 2019 12:33:18 +0000 (+0200) Subject: Use BLAKE2b checksum instead of bundled MD4 X-Git-Tag: v4.0~879 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ebb40d18721cc17fc3cfa0823ea74d09ba074962;p=thirdparty%2Fccache.git Use BLAKE2b checksum instead of bundled MD4 --- diff --git a/configure.ac b/configure.ac index 9f5458d52..8a85089c9 100644 --- a/configure.ac +++ b/configure.ac @@ -176,6 +176,9 @@ if test x${enable_tracing} = xyes; then extra_sources="src/third_party/minitrace.c" fi +AC_CHECK_HEADERS(blake2.h) +AC_CHECK_LIB([b2],[blake2b]) + dnl Linking on Windows needs ws2_32 if test x${windows_os} = xyes; then LIBS="$LIBS -lws2_32" diff --git a/src/hash.c b/src/hash.c index 22e92d348..b194c48ff 100644 --- a/src/hash.c +++ b/src/hash.c @@ -19,12 +19,12 @@ #include "ccache.h" #include "hash.h" -#include "mdfour.h" +#include #define HASH_DELIMITER "\000cCaChE" struct hash { - struct mdfour md; + blake2b_state state; FILE *debug_binary; FILE *debug_text; }; @@ -46,7 +46,7 @@ do_hash_buffer(struct hash *hash, const void *s, size_t len) { assert(s); - mdfour_update(&hash->md, (const unsigned char *)s, len); + blake2b_update(&hash->state, (const uint8_t *)s, len); if (len > 0 && hash->debug_binary) { (void) fwrite(s, 1, len, hash->debug_binary); } @@ -64,7 +64,7 @@ struct hash * hash_init(void) { struct hash *hash = malloc(sizeof(struct hash)); - mdfour_begin(&hash->md); + blake2b_init(&hash->state, DIGEST_SIZE); hash->debug_binary = NULL; hash->debug_text = NULL; return hash; @@ -74,7 +74,7 @@ struct hash * hash_copy(struct hash *hash) { struct hash *result = malloc(sizeof(struct hash)); - result->md = hash->md; + result->state = hash->state; result->debug_binary = NULL; result->debug_text = NULL; return result; @@ -107,11 +107,10 @@ hash_buffer(struct hash *hash, const void *s, size_t len) void hash_result_as_bytes(struct hash *hash, struct digest *digest) { - mdfour_result(&hash->md, digest->bytes); - size_t input_size = hash->md.totalN + hash->md.tail_len; - for (size_t i = 0; i < 4; i++) { - digest->bytes[16 + i] = (input_size >> ((3 - i) * 8)) & 0xFF; - } + // make a copy before altering state + struct hash *copy = hash_copy(hash); + blake2b_final(©->state, digest->bytes, DIGEST_SIZE); + hash_free(copy); } void diff --git a/test/suites/base.bash b/test/suites/base.bash index e5765d3af..f031e9b8d 100644 --- a/test/suites/base.bash +++ b/test/suites/base.bash @@ -1073,8 +1073,11 @@ EOF $CCACHE --hash-file /dev/null > hash.out printf "a" | $CCACHE --hash-file - >> hash.out - if grep '31d6cfe0d16ae931b73c59d7e0c089c000000000' hash.out >/dev/null && \ - grep 'bde52cb31de33e46245e05fbdbd6fb2400000001' hash.out >/dev/null; then + hash_0='3345524abf6bbe1809449224b5972c41790b6cf2' + hash_1='948caa2db61bc4cdb4faf7740cd491f195043914' + + if grep "$hash_0" hash.out >/dev/null 2>&1 && \ + grep "$hash_1" hash.out >/dev/null 2>&1; then : OK else test_failed "Unexpected output of --hash-file" diff --git a/test/suites/direct.bash b/test/suites/direct.bash index 71810e933..e250ad8f0 100644 --- a/test/suites/direct.bash +++ b/test/suites/direct.bash @@ -883,9 +883,13 @@ EOF manifest=`find $CCACHE_DIR -name '*.manifest'` $CCACHE --dump-manifest $manifest >manifest.dump - if grep 'Hash: d4de2f956b4a386c6660990a7a1ab13f0000001e' manifest.dump >/dev/null && \ - grep 'Hash: e94ceb9f1b196c387d098a5f1f4fe8620000000b' manifest.dump >/dev/null && \ - grep 'Hash: ba753bebf9b5eb99524bb7447095e2e60000000b' manifest.dump >/dev/null; then + checksum_test1_h='7706603374730d6e19c22f607768040f13be686d' + checksum_test2_h='0f1d0cf7d790a6a31248d8a52e826dad433d191c' + checksum_test3_h='d07f2a91a649bc56a1b008279b326201077d341b' + + if grep "Hash: $checksum_test1_h" manifest.dump >/dev/null 2>&1 && \ + grep "Hash: $checksum_test2_h" manifest.dump >/dev/null 2>&1 && \ + grep "Hash: $checksum_test3_h" manifest.dump >/dev/null 2>&1; then : OK else test_failed "Unexpected output of --dump-manifest" diff --git a/unittest/test_hash.c b/unittest/test_hash.c index 83ae4ae02..25858de8e 100644 --- a/unittest/test_hash.c +++ b/unittest/test_hash.c @@ -32,7 +32,7 @@ TEST(test_vectors_from_rfc_1320_should_be_correct) struct hash *h = hash_init(); hash_string(h, ""); hash_result_as_string(h, d); - CHECK_STR_EQ("31d6cfe0d16ae931b73c59d7e0c089c000000000", d); + CHECK_STR_EQ("3345524abf6bbe1809449224b5972c41790b6cf2", d); hash_free(h); } @@ -40,7 +40,7 @@ TEST(test_vectors_from_rfc_1320_should_be_correct) struct hash *h = hash_init(); hash_string(h, "a"); hash_result_as_string(h, d); - CHECK_STR_EQ("bde52cb31de33e46245e05fbdbd6fb2400000001", d); + CHECK_STR_EQ("948caa2db61bc4cdb4faf7740cd491f195043914", d); hash_free(h); } @@ -48,7 +48,7 @@ TEST(test_vectors_from_rfc_1320_should_be_correct) struct hash *h = hash_init(); hash_string(h, "message digest"); hash_result_as_string(h, d); - CHECK_STR_EQ("d9130a8164549fe818874806e1c7014b0000000e", d); + CHECK_STR_EQ("6bfec6f65e52962be863d6ea1005fc5e4cc8478c", d); hash_free(h); } @@ -59,7 +59,7 @@ TEST(test_vectors_from_rfc_1320_should_be_correct) "12345678901234567890123456789012345678901234567890123456789012345678901" "234567890"); hash_result_as_string(h, d); - CHECK_STR_EQ("e33b4ddc9c38f2199c3e7b164fcc053600000050", d); + CHECK_STR_EQ("c2be0e534a67d25947f0c7e78527b2f82abd260f", d); hash_free(h); } } @@ -72,7 +72,7 @@ TEST(hash_result_should_not_alter_state) hash_result_as_string(h, d); hash_string(h, " digest"); hash_result_as_string(h, d); - CHECK_STR_EQ("d9130a8164549fe818874806e1c7014b0000000e", d); + CHECK_STR_EQ("6bfec6f65e52962be863d6ea1005fc5e4cc8478c", d); hash_free(h); } @@ -82,9 +82,9 @@ TEST(hash_result_should_be_idempotent) struct hash *h = hash_init(); hash_string(h, ""); hash_result_as_string(h, d); - CHECK_STR_EQ("31d6cfe0d16ae931b73c59d7e0c089c000000000", d); + CHECK_STR_EQ("3345524abf6bbe1809449224b5972c41790b6cf2", d); hash_result_as_string(h, d); - CHECK_STR_EQ("31d6cfe0d16ae931b73c59d7e0c089c000000000", d); + CHECK_STR_EQ("3345524abf6bbe1809449224b5972c41790b6cf2", d); hash_free(h); } @@ -96,8 +96,8 @@ TEST(hash_result_as_bytes) struct digest d; hash_result_as_bytes(h, &d); uint8_t expected[sizeof(d.bytes)] = { - 0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 0x18, 0x87, 0x48, 0x06, - 0xe1, 0xc7, 0x01, 0x4b, 0x00, 0x00, 0x00, 0x0e + 0x6b, 0xfe, 0xc6, 0xf6, 0x5e, 0x52, 0x96, 0x2b, 0xe8, 0x63, 0xd6, 0xea, + 0x10, 0x05, 0xfc, 0x5e, 0x4c, 0xc8, 0x47, 0x8c }; CHECK_DATA_EQ(d.bytes, expected, sizeof(d.bytes)); hash_free(h);