git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] hash: Allow building with the external sha1dc library
@ 2017-07-25  5:57 Takashi Iwai
  2017-07-25 19:06 ` Junio C Hamano
  2017-07-28 15:58 ` Ævar Arnfjörð Bjarmason
  0 siblings, 2 replies; 13+ messages in thread
From: Takashi Iwai @ 2017-07-25  5:57 UTC (permalink / raw)
  To: git; +Cc: Andreas Stieger

Some distros provide SHA1 collision detect code as a shared library.
It's the very same code as we have in git tree, and git can link with
it as well; at least, it may make maintenance easier, according to our
security guys.

This patch allows user to build git linking with the external sha1dc
library instead of the built-in sha1dc code.  User needs to define
DC_SHA1_EXTERNAL explicitly.  As default, the built-in sha1dc code is
used like before.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 Makefile         | 12 ++++++++++++
 hash.h           |  4 +++-
 sha1dc_git_ext.c | 11 +++++++++++
 sha1dc_git_ext.h | 25 +++++++++++++++++++++++++
 4 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 sha1dc_git_ext.c
 create mode 100644 sha1dc_git_ext.h

diff --git a/Makefile b/Makefile
index 461c845d33cb..f1a262d56254 100644
--- a/Makefile
+++ b/Makefile
@@ -162,6 +162,12 @@ all::
 # algorithm. This is slower, but may detect attempted collision attacks.
 # Takes priority over other *_SHA1 knobs.
 #
+# Define DC_SHA1_EXTERNAL in addition to DC_SHA1 if you want to build / link
+# git with the external sha1collisiondetection library.
+# Without this option, i.e. the default behavior is to build git with its
+# own sha1dc code.  If any extra linker option is required, define them in
+# DC_SHA1_LINK variable in addition.
+#
 # Define DC_SHA1_SUBMODULE in addition to DC_SHA1 to use the
 # sha1collisiondetection shipped as a submodule instead of the
 # non-submodule copy in sha1dc/. This is an experimental option used
@@ -1472,6 +1478,11 @@ ifdef APPLE_COMMON_CRYPTO
 	BASIC_CFLAGS += -DSHA1_APPLE
 else
 	DC_SHA1 := YesPlease
+ifdef DC_SHA1_EXTERNAL
+	LIB_OBJS += sha1dc_git_ext.o
+	BASIC_CFLAGS += -DSHA1_DC -DDC_SHA1_EXTERNAL
+	EXTLIBS += $(DC_SHA1_LINK) -lsha1detectcoll
+else
 ifdef DC_SHA1_SUBMODULE
 	LIB_OBJS += sha1collisiondetection/lib/sha1.o
 	LIB_OBJS += sha1collisiondetection/lib/ubc_check.o
@@ -1492,6 +1503,7 @@ endif
 endif
 endif
 endif
+endif
 
 ifdef SHA1_MAX_BLOCK_SIZE
 	LIB_OBJS += compat/sha1-chunked.o
diff --git a/hash.h b/hash.h
index bef3e630a093..dce327d58d07 100644
--- a/hash.h
+++ b/hash.h
@@ -8,7 +8,9 @@
 #elif defined(SHA1_OPENSSL)
 #include <openssl/sha.h>
 #elif defined(SHA1_DC)
-#ifdef DC_SHA1_SUBMODULE
+#if defined(DC_SHA1_EXTERNAL)
+#include "sha1dc_git_ext.h"
+#elif defined(DC_SHA1_SUBMODULE)
 #include "sha1collisiondetection/lib/sha1.h"
 #else
 #include "sha1dc/sha1.h"
diff --git a/sha1dc_git_ext.c b/sha1dc_git_ext.c
new file mode 100644
index 000000000000..359439fc3d93
--- /dev/null
+++ b/sha1dc_git_ext.c
@@ -0,0 +1,11 @@
+/* Only for DC_SHA1_EXTERNAL; sharing the same hooks as built-in sha1dc */
+
+#include "cache.h"
+#include <sha1.h>
+#include "sha1dc_git.c"
+
+void git_SHA1DCInit(SHA1_CTX *ctx)
+{
+	SHA1DCInit(ctx);
+	SHA1DCSetSafeHash(ctx, 0);
+}
diff --git a/sha1dc_git_ext.h b/sha1dc_git_ext.h
new file mode 100644
index 000000000000..d0ea8ce518db
--- /dev/null
+++ b/sha1dc_git_ext.h
@@ -0,0 +1,25 @@
+/*
+ * This file is included by hash.h for DC_SHA1_EXTERNAL
+ */
+
+#include <sha1.h>
+
+/*
+ * Same as SHA1DCInit, but with default save_hash=0
+ */
+void git_SHA1DCInit(SHA1_CTX *);
+
+/*
+ * Same as SHA1DCFinal, but convert collision attack case into a verbose die().
+ */
+void git_SHA1DCFinal(unsigned char [20], SHA1_CTX *);
+
+/*
+ * Same as SHA1DCUpdate, but adjust types to match git's usual interface.
+ */
+void git_SHA1DCUpdate(SHA1_CTX *ctx, const void *data, unsigned long len);
+
+#define platform_SHA_CTX SHA1_CTX
+#define platform_SHA1_Init git_SHA1DCInit
+#define platform_SHA1_Update git_SHA1DCUpdate
+#define platform_SHA1_Final git_SHA1DCFinal
-- 
2.13.3


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2017-08-12  6:50 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-25  5:57 [PATCH] hash: Allow building with the external sha1dc library Takashi Iwai
2017-07-25 19:06 ` Junio C Hamano
2017-07-28 15:58 ` Ævar Arnfjörð Bjarmason
2017-07-28 16:04   ` Ævar Arnfjörð Bjarmason
2017-07-31 22:28     ` Junio C Hamano
2017-08-01  5:52     ` Takashi Iwai
2017-08-12  0:42     ` Junio C Hamano
2017-08-12  6:50       ` Takashi Iwai
2017-08-01  5:46   ` Takashi Iwai
2017-08-01 15:56     ` Junio C Hamano
2017-08-01 16:12       ` Takashi Iwai
2017-08-01 19:55         ` Ævar Arnfjörð Bjarmason
2017-08-01 20:50           ` Takashi Iwai

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).