sha1dc: allow building with the external sha1dc library

Some distros provide SHA1 collision-detect code as a shared library. It's the same code as we have in git tree (but may be with a different init default for hash), 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 code. User needs to define DC_SHA1_EXTERNAL explicitly. As default without it, the built-in sha1dc code is used like before. Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Takashi Iwai committed Aug 15, 2017 at 14:04 UTC 3964cbbb5c30609ebd795a979074251cf59436c3
3 files changed +33 -1
Makefile
+13
@@ -162,6 +162,11 @@ all::
162 # algorithm. This is slower, but may detect attempted collision attacks.
163 # Takes priority over other *_SHA1 knobs.
164 #
165 +# Define DC_SHA1_EXTERNAL in addition to DC_SHA1 if you want to build / link
166 +# git with the external SHA1 collision-detect library.
167 +# Without this option, i.e. the default behavior is to build git with its
168 +# own built-in code (or submodule).
169 +#
170 # Define DC_SHA1_SUBMODULE in addition to DC_SHA1 to use the
171 # sha1collisiondetection shipped as a submodule instead of the
172 # non-submodule copy in sha1dc/. This is an experimental option used
@@ -1474,6 +1479,13 @@ else
1479 DC_SHA1 := YesPlease
1480 BASIC_CFLAGS += -DSHA1_DC
1481 LIB_OBJS += sha1dc_git.o
1482 +ifdef DC_SHA1_EXTERNAL
1483 + ifdef DC_SHA1_SUBMODULE
1484 +$(error Only set DC_SHA1_EXTERNAL or DC_SHA1_SUBMODULE, not both)
1485 + endif
1486 + BASIC_CFLAGS += -DDC_SHA1_EXTERNAL
1487 + EXTLIBS += -lsha1detectcoll
1488 +else
1489 ifdef DC_SHA1_SUBMODULE
1490 LIB_OBJS += sha1collisiondetection/lib/sha1.o
1491 LIB_OBJS += sha1collisiondetection/lib/ubc_check.o
@@ -1491,6 +1503,7 @@ endif
1503 endif
1504 endif
1505 endif
1506 +endif
1507
1508 ifdef SHA1_MAX_BLOCK_SIZE
1509 LIB_OBJS += compat/sha1-chunked.o
sha1dc_git.c
+11
@@ -1,5 +1,16 @@
1 #include "cache.h"
2
3 +#ifdef DC_SHA1_EXTERNAL
4 +/*
5 + * Same as SHA1DCInit, but with default save_hash=0
6 + */
7 +void git_SHA1DCInit(SHA1_CTX *ctx)
8 +{
9 + SHA1DCInit(ctx);
10 + SHA1DCSetSafeHash(ctx, 0);
11 +}
12 +#endif
13 +
14 /*
15 * Same as SHA1DCFinal, but convert collision attack case into a verbose die().
16 */
sha1dc_git.h
+9 -1
@@ -2,14 +2,22 @@
2
3 #ifdef DC_SHA1_SUBMODULE
4 #include "sha1collisiondetection/lib/sha1.h"
5 +#elif defined(DC_SHA1_EXTERNAL)
6 +#include <sha1dc/sha1.h>
7 #else
8 #include "sha1dc/sha1.h"
9 #endif
10
11 +#ifdef DC_SHA1_EXTERNAL
12 +void git_SHA1DCInit(SHA1_CTX *);
13 +#else
14 +#define git_SHA1DCInit SHA1DCInit
15 +#endif
16 +
17 void git_SHA1DCFinal(unsigned char [20], SHA1_CTX *);
18 void git_SHA1DCUpdate(SHA1_CTX *ctx, const void *data, unsigned long len);
19
20 #define platform_SHA_CTX SHA1_CTX
13 -#define platform_SHA1_Init SHA1DCInit
21 +#define platform_SHA1_Init git_SHA1DCInit
22 #define platform_SHA1_Update git_SHA1DCUpdate
23 #define platform_SHA1_Final git_SHA1DCFinal