]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Use BLAKE2b checksum instead of bundled MD4
authorAnders F Björklund <anders.f.bjorklund@gmail.com>
Sun, 21 Jul 2019 12:33:18 +0000 (14:33 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 22 Jul 2019 20:41:37 +0000 (22:41 +0200)
configure.ac
src/hash.c
test/suites/base.bash
test/suites/direct.bash
unittest/test_hash.c

index 9f5458d5219cc315004aa380845983403608e790..8a85089c90b87bb4af296a8aa48069b963680342 100644 (file)
@@ -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"
index 22e92d348f802fe08e6f7d9c1e999f280473f2d7..b194c48ffc6159b0c2af9a54c8ddd19e13466cf9 100644 (file)
 
 #include "ccache.h"
 #include "hash.h"
-#include "mdfour.h"
+#include <blake2.h>
 
 #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(&copy->state, digest->bytes, DIGEST_SIZE);
+       hash_free(copy);
 }
 
 void
index e5765d3af4bf211a12f5c2e2b57fa78b7ca0d5cb..f031e9b8ddbcf2cdcceaa71d9053f304a2c72ab1 100644 (file)
@@ -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"
index 71810e933733c6facd8af00de18208539d8e4053..e250ad8f0eaf065e2164c150379a8d73bb6a2f07 100644 (file)
@@ -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"
index 83ae4ae0258f92e8f1578c8b6e7f6671a8e79a1b..25858de8e2996f0cc82abe31b3ea62ced5578150 100644 (file)
@@ -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);