{"thread":{"id":"45400","subject":"[PATCH 2/2] Integrate the sha1dc code with the git build","startedAt":"2017-03-16T20:33:13Z","lastAt":"2017-03-16T20:33:13Z","messageCount":1,"participants":["Linus Torvalds"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"314255","messageId":"alpine.LFD.2.20.1703161325081.18484@i7.lan","threadId":"45400","inReplyTo":null,"subject":"[PATCH 2/2] Integrate the sha1dc code with the git build","fromName":"Linus Torvalds","fromEmail":"torvalds@linux-foundation.org","sentAt":"2017-03-16T20:25:29Z","receivedAt":"2017-03-16T20:33:13Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nFrom: Linus Torvalds <torvalds@linux-foundation.org>\nDate: Thu, 16 Mar 2017 13:08:38 -0700\nSubject: [PATCH 2/2] Integrate the sha1dc code with the git build\n\nThis adds the proper magic to actually build the sha1dc code as part of\ngit when USE_SHA1DC is enabled.\n\nThis includes\n\n - adjusting the sha1dc include directives for git use\n\n - adding the proper USE_SHA1DC logic to the Makefile\n\n - adding the SHA1DC case to the \"hash.h\" header\n\n - adding the proper \"git platform\" wrappers for the SHA1 interface\n\nMuch of this comes from Jeff King's previous integration effort, with\nmodifications for the new world order of hash.h.\n\nSigned-off-by: Linus Torvalds <torvalds@linux-foundation.org>\n---\n Makefile           | 10 ++++++++++\n hash.h             |  2 ++\n sha1dc/sha1.c      | 33 +++++++++++++++++++++++++--------\n sha1dc/sha1.h      | 18 ++++++++++++++++--\n sha1dc/ubc_check.c |  4 ++--\n sha1dc/ubc_check.h |  2 --\n 6 files changed, 55 insertions(+), 14 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex a5a11e721..186ce17f2 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -140,6 +140,10 @@ all::\n # Define PPC_SHA1 environment variable when running make to make use of\n # a bundled SHA1 routine optimized for PowerPC.\n #\n+# Define USE_SHA1DC to unconditionally enable the collision-detecting sha1\n+# algorithm. This is slower, but may detect attempted collision attacks.\n+# Takes priority over other *_SHA1 knobs.\n+#\n # Define SHA1_MAX_BLOCK_SIZE to limit the amount of data that will be hashed\n # in one call to the platform's SHA1_Update(). e.g. APPLE_COMMON_CRYPTO\n # wants 'SHA1_MAX_BLOCK_SIZE=1024L*1024L*1024L' defined.\n@@ -1383,6 +1387,11 @@ ifdef APPLE_COMMON_CRYPTO\n \tSHA1_MAX_BLOCK_SIZE = 1024L*1024L*1024L\n endif\n \n+ifdef USE_SHA1DC\n+\tLIB_OBJS += sha1dc/sha1.o\n+\tLIB_OBJS += sha1dc/ubc_check.o\n+\tBASIC_CFLAGS += -DSHA1DC\n+else\n ifdef BLK_SHA1\n \tLIB_OBJS += block-sha1/sha1.o\n \tBASIC_CFLAGS += -DSHA1_BLK\n@@ -1400,6 +1409,7 @@ else\n endif\n endif\n endif\n+endif\n \n ifdef SHA1_MAX_BLOCK_SIZE\n \tLIB_OBJS += compat/sha1-chunked.o\ndiff --git a/hash.h b/hash.h\nindex f0d9ddd0c..b9e7e34fc 100644\n--- a/hash.h\n+++ b/hash.h\n@@ -3,6 +3,8 @@\n \n #if defined(SHA1_PPC)\n #include \"ppc/sha1.h\"\n+#elif defined(SHA1DC)\n+#include \"sha1dc/sha1.h\"\n #elif defined(SHA1_APPLE)\n #include <CommonCrypto/CommonDigest.h>\n #elif defined(SHA1_OPENSSL)\ndiff --git a/sha1dc/sha1.c b/sha1dc/sha1.c\nindex 8d12b832b..76a8d1a85 100644\n--- a/sha1dc/sha1.c\n+++ b/sha1dc/sha1.c\n@@ -5,14 +5,9 @@\n * https://opensource.org/licenses/MIT\n ***/\n \n-#include <string.h>\n-#include <memory.h>\n-#include <stdio.h>\n-#include <stdlib.h>\n-\n-#include \"sha1.h\"\n-#include \"ubc_check.h\"\n-\n+#include \"git-compat-util.h\"\n+#include \"sha1dc/sha1.h\"\n+#include \"sha1dc/ubc_check.h\"\n \n /* \n    Because Little-Endian architectures are most common,\n@@ -1790,3 +1785,25 @@ int SHA1DCFinal(unsigned char output[20], SHA1_CTX *ctx)\n \toutput[19] = (unsigned char)(ctx->ihv[4]);\n \treturn ctx->found_collision;\n }\n+\n+static const char collision_message[] =\n+\"The SHA1 computation detected evidence of a collision attack;\\n\"\n+\"refusing to process the contents.\";\n+\n+void git_SHA1DCFinal(unsigned char hash[20], SHA1_CTX *ctx)\n+{\n+\tif (SHA1DCFinal(hash, ctx))\n+\t\tdie(collision_message);\n+}\n+\n+void git_SHA1DCUpdate(SHA1_CTX *ctx, const void *vdata, unsigned long len)\n+{\n+\tconst char *data = vdata;\n+\t/* We expect an unsigned long, but sha1dc only takes an int */\n+\twhile (len > INT_MAX) {\n+\t\tSHA1DCUpdate(ctx, data, INT_MAX);\n+\t\tdata += INT_MAX;\n+\t\tlen -= INT_MAX;\n+\t}\n+\tSHA1DCUpdate(ctx, data, len);\n+}\ndiff --git a/sha1dc/sha1.h b/sha1dc/sha1.h\nindex e867724c0..37ee415da 100644\n--- a/sha1dc/sha1.h\n+++ b/sha1dc/sha1.h\n@@ -9,8 +9,6 @@\n extern \"C\" {\n #endif\n \n-#include <stdint.h>\n-\n /* uses SHA-1 message expansion to expand the first 16 words of W[] to 80 words */\n /* void sha1_message_expansion(uint32_t W[80]); */\n \n@@ -100,6 +98,22 @@ void SHA1DCUpdate(SHA1_CTX*, const char*, size_t);\n /* returns: 0 = no collision detected, otherwise = collision found => warn user for active attack */\n int  SHA1DCFinal(unsigned char[20], SHA1_CTX*); \n \n+\n+/*\n+ * Same as SHA1DCFinal, but convert collision attack case into a verbose die().\n+ */\n+void git_SHA1DCFinal(unsigned char [20], SHA1_CTX *);\n+\n+/*\n+ * Same as SHA1DCUpdate, but adjust types to match git's usual interface.\n+ */\n+void git_SHA1DCUpdate(SHA1_CTX *ctx, const void *data, unsigned long len);\n+\n+#define platform_SHA_CTX SHA1_CTX\n+#define platform_SHA1_Init SHA1DCInit\n+#define platform_SHA1_Update git_SHA1DCUpdate\n+#define platform_SHA1_Final git_SHA1DCFinal\n+\n #if defined(__cplusplus)\n }\n #endif\ndiff --git a/sha1dc/ubc_check.c b/sha1dc/ubc_check.c\nindex 27d0976da..089dd4743 100644\n--- a/sha1dc/ubc_check.c\n+++ b/sha1dc/ubc_check.c\n@@ -24,8 +24,8 @@\n // ubc_check has been verified against ubc_check_verify using the 'ubc_check_test' program in the tools section\n */\n \n-#include <stdint.h>\n-#include \"ubc_check.h\"\n+#include \"git-compat-util.h\"\n+#include \"sha1dc/ubc_check.h\"\n \n static const uint32_t DV_I_43_0_bit \t= (uint32_t)(1) << 0;\n static const uint32_t DV_I_44_0_bit \t= (uint32_t)(1) << 1;\ndiff --git a/sha1dc/ubc_check.h b/sha1dc/ubc_check.h\nindex b349bed92..b64c306d7 100644\n--- a/sha1dc/ubc_check.h\n+++ b/sha1dc/ubc_check.h\n@@ -27,8 +27,6 @@\n extern \"C\" {\n #endif\n \n-#include <stdint.h>\n-\n #define DVMASKSIZE 1\n typedef struct { int dvType; int dvK; int dvB; int testt; int maski; int maskb; uint32_t dm[80]; } dv_info_t;\n extern dv_info_t sha1_dvs[];\n-- \n2.12.0.434.g76f8c11c1\n\n"}]}