Skip to content

Commit

Permalink
configure: if not macOS, use the openssl sha1 algorithm if available
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Widgren <stefan.widgren@gmail.com>
  • Loading branch information
stewid committed Mar 16, 2019
1 parent 670eef6 commit ac8a757
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 12 deletions.
1 change: 0 additions & 1 deletion Makefile
Expand Up @@ -130,7 +130,6 @@ sync_libgit2:
-rm -f src/libgit2/src/hash/hash_generic.h
-rm -f src/libgit2/src/hash/hash_mbedtls.c
-rm -f src/libgit2/src/hash/hash_mbedtls.h
-rm -f src/libgit2/src/hash/hash_openssl.h
-rm -f src/libgit2/src/hash/hash_win32.c
-rm -f src/libgit2/src/hash/hash_win32.h
-rm -f src/libgit2/src/transports/auth_negotiate.c
Expand Down
21 changes: 19 additions & 2 deletions configure
Expand Up @@ -623,6 +623,7 @@ ac_includes_default="\
#endif"

ac_subst_vars='PKG_CPPFLAGS
GIT2R_SRC_SHA1DC
GIT2R_SRC_REGEX
EGREP
GREP
Expand Down Expand Up @@ -5332,8 +5333,23 @@ CPPFLAGS="-I. -Ilibgit2/src -Ilibgit2/include -Ilibgit2/deps/http-parser ${CPPFL
CPPFLAGS="${CPPFLAGS} -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -DLIBGIT2_NO_FEATURES_H -DR_NO_REMAP -DSTRICT_R_HEADERS"
# Specify sha1 implementation
CPPFLAGS="${CPPFLAGS} -DGIT_SHA1_COLLISIONDETECT=1 -DSHA1DC_NO_STANDARD_INCLUDES=1"
CPPFLAGS="${CPPFLAGS} -DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\" -DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\""
case "${host_os}" in
darwin*)
CPPFLAGS="${CPPFLAGS} -DGIT_SHA1_COLLISIONDETECT=1 -DSHA1DC_NO_STANDARD_INCLUDES=1"
CPPFLAGS="${CPPFLAGS} -DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\" -DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\""
GIT2R_SRC_SHA1DC="libgit2/src/hash/sha1dc/sha1.o libgit2/src/hash/sha1dc/ubc_check.o"
;;
*)
if test "x${have_ssl}" = xyes; then
CPPFLAGS="${CPPFLAGS} -DGIT_SHA1_OPENSSL=1"
else
CPPFLAGS="${CPPFLAGS} -DGIT_SHA1_COLLISIONDETECT=1 -DSHA1DC_NO_STANDARD_INCLUDES=1"
CPPFLAGS="${CPPFLAGS} -DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\" -DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\""
GIT2R_SRC_SHA1DC="libgit2/src/hash/sha1dc/sha1.o libgit2/src/hash/sha1dc/ubc_check.o"
fi
;;
esac
# Add definitions specific for solaris
case "${host_os}" in
Expand Down Expand Up @@ -5821,6 +5837,7 @@ CPPFLAGS="${CPPFLAGS} -DGIT2R_HAVE_OBJECT_ANY"
CPPFLAGS="${CPPFLAGS} -DGIT2R_HAVE_GIT_ERROR"
PKG_CFLAGS="${PKG_CFLAGS} ${LIBSSH2_CFLAGS}"
PKG_CPPFLAGS="${CPPFLAGS}"
Expand Down
20 changes: 18 additions & 2 deletions configure.ac
Expand Up @@ -377,8 +377,23 @@ CPPFLAGS="-I. -Ilibgit2/src -Ilibgit2/include -Ilibgit2/deps/http-parser ${CPPFL
CPPFLAGS="${CPPFLAGS} -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -DLIBGIT2_NO_FEATURES_H -DR_NO_REMAP -DSTRICT_R_HEADERS"

# Specify sha1 implementation
CPPFLAGS="${CPPFLAGS} -DGIT_SHA1_COLLISIONDETECT=1 -DSHA1DC_NO_STANDARD_INCLUDES=1"
CPPFLAGS="${CPPFLAGS} -DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\" -DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\""
case "${host_os}" in
darwin*)
CPPFLAGS="${CPPFLAGS} -DGIT_SHA1_COLLISIONDETECT=1 -DSHA1DC_NO_STANDARD_INCLUDES=1"
CPPFLAGS="${CPPFLAGS} -DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\" -DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\""
GIT2R_SRC_SHA1DC="libgit2/src/hash/sha1dc/sha1.o libgit2/src/hash/sha1dc/ubc_check.o"
;;
*)
if test "x${have_ssl}" = xyes; then
CPPFLAGS="${CPPFLAGS} -DGIT_SHA1_OPENSSL=1"
else
CPPFLAGS="${CPPFLAGS} -DGIT_SHA1_COLLISIONDETECT=1 -DSHA1DC_NO_STANDARD_INCLUDES=1"
CPPFLAGS="${CPPFLAGS} -DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\\\"common.h\\\" -DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\\\"common.h\\\""
GIT2R_SRC_SHA1DC="libgit2/src/hash/sha1dc/sha1.o libgit2/src/hash/sha1dc/ubc_check.o"
fi
;;
esac


# Add definitions specific for solaris
case "${host_os}" in
Expand Down Expand Up @@ -443,6 +458,7 @@ CPPFLAGS="${CPPFLAGS} -DGIT2R_HAVE_OBJECT_ANY"
CPPFLAGS="${CPPFLAGS} -DGIT2R_HAVE_GIT_ERROR"

AC_SUBST(GIT2R_SRC_REGEX)
AC_SUBST(GIT2R_SRC_SHA1DC)
AC_SUBST([PKG_CFLAGS], ["${PKG_CFLAGS} ${LIBSSH2_CFLAGS}"])
AC_SUBST([PKG_CPPFLAGS], ["${CPPFLAGS}"])
AC_SUBST([PKG_LIBS], ["${LIBS} ${LIBSSH2_LIBS} ${LIBICONV}"])
Expand Down
3 changes: 1 addition & 2 deletions scripts/build_Makevars.R
Expand Up @@ -70,7 +70,6 @@ build_Makevars.in <- function() {
on.exit(close(Makevars))

files <- list(libgit2 = o_files("src/libgit2/src"),
libgit2.sha1dc = o_files("src/libgit2/src/hash/sha1dc"),
libgit2.streams = o_files("src/libgit2/src/streams"),
libgit2.transports =
o_files("src/libgit2/src/transports",
Expand All @@ -87,7 +86,7 @@ build_Makevars.in <- function() {
cat("PKG_LIBS = -L. -lmygit @PKG_LIBS@\n", file = Makevars)
cat("\n", file = Makevars)

build_objects(files, " @GIT2R_SRC_REGEX@", Makevars)
build_objects(files, " @GIT2R_SRC_REGEX@ @GIT2R_SRC_SHA1DC@", Makevars)

cat("\n$(SHLIB): libmygit.a\n\n", file = Makevars)
cat("libmygit.a: $(LIBGIT)\n\t$(AR) rcs libmygit.a $(LIBGIT)\n\n", file = Makevars)
Expand Down
7 changes: 2 additions & 5 deletions src/Makevars_libgit2.in
Expand Up @@ -46,8 +46,6 @@ OBJECTS.libgit2 = libgit2/src/alloc.o libgit2/src/annotated_commit.o libgit2/src
libgit2/src/util.o libgit2/src/varint.o libgit2/src/vector.o \
libgit2/src/worktree.o libgit2/src/zstream.o

OBJECTS.libgit2.sha1dc = libgit2/src/hash/sha1dc/sha1.o libgit2/src/hash/sha1dc/ubc_check.o

OBJECTS.libgit2.streams = libgit2/src/streams/mbedtls.o libgit2/src/streams/openssl.o libgit2/src/streams/registry.o \
libgit2/src/streams/socket.o libgit2/src/streams/stransport.o libgit2/src/streams/tls.o

Expand All @@ -64,9 +62,8 @@ OBJECTS.libgit2.xdiff = libgit2/src/xdiff/xdiffi.o libgit2/src/xdiff/xemit.o lib

OBJECTS.http_parser = libgit2/deps/http-parser/http_parser.o

LIBGIT = $(OBJECTS.libgit2) $(OBJECTS.libgit2.sha1dc) $(OBJECTS.libgit2.streams) \
$(OBJECTS.libgit2.transports) $(OBJECTS.libgit2.unix) $(OBJECTS.libgit2.xdiff) \
$(OBJECTS.http_parser) @GIT2R_SRC_REGEX@
LIBGIT = $(OBJECTS.libgit2) $(OBJECTS.libgit2.streams) $(OBJECTS.libgit2.transports) \
$(OBJECTS.libgit2.unix) $(OBJECTS.libgit2.xdiff) $(OBJECTS.http_parser) @GIT2R_SRC_REGEX@ @GIT2R_SRC_SHA1DC@

$(SHLIB): libmygit.a

Expand Down
63 changes: 63 additions & 0 deletions src/libgit2/src/hash/hash_openssl.h
@@ -0,0 +1,63 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/

#ifndef INCLUDE_hash_hash_openssl_h__
#define INCLUDE_hash_hash_openssl_h__

#include "hash.h"

#include <openssl/sha.h>

struct git_hash_ctx {
SHA_CTX c;
};

#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)

GIT_INLINE(int) git_hash_global_init(void)
{
return 0;
}

GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
{
assert(ctx);

if (SHA1_Init(&ctx->c) != 1) {
git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to initialize hash context");
return -1;
}

return 0;
}

GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
{
assert(ctx);

if (SHA1_Update(&ctx->c, data, len) != 1) {
git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to update hash");
return -1;
}

return 0;
}

GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
{
assert(ctx);

if (SHA1_Final(out->id, &ctx->c) != 1) {
git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash");
return -1;
}

return 0;
}

#endif

0 comments on commit ac8a757

Please sign in to comment.