Skip to content

Commit db4461d

Browse files
authored
Merge pull request libgit2#6144 from libgit2/ethomson/sha256
SHA256: add a SHA256 implementation backend
2 parents 3a08bc4 + 0e30bec commit db4461d

37 files changed

+2248
-908
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ option(USE_NSEC "Support nanosecond precision file mtimes and cti
2929
# Backend selection
3030
option(USE_SSH "Link with libssh2 to enable SSH support" OFF)
3131
option(USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON)
32-
option(USE_SHA1 "Enable SHA1. Can be set to CollisionDetection(ON)/HTTPS/Generic" ON)
32+
option(USE_SHA1 "Enable SHA1. Can be set to CollisionDetection(ON)/HTTPS" ON)
33+
option(USE_SHA256 "Enable SHA256. Can be set to HTTPS/Builtin" ON)
3334
option(USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF)
3435
set(USE_HTTP_PARSER "" CACHE STRING "Specifies the HTTP Parser implementation; either system or builtin.")
3536
set(REGEX_BACKEND "" CACHE STRING "Regular expression implementation. One of regcomp_l, pcre2, pcre, regcomp, or builtin.")

COPYING

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,3 +1144,43 @@ worldwide. This software is distributed without any warranty.
11441144

11451145
See <http://creativecommons.org/publicdomain/zero/1.0/>.
11461146

1147+
----------------------------------------------------------------------
1148+
1149+
The built-in SHA256 support (src/hash/rfc6234) is taken from RFC 6234
1150+
under the following license:
1151+
1152+
Copyright (c) 2011 IETF Trust and the persons identified as
1153+
authors of the code. All rights reserved.
1154+
1155+
Redistribution and use in source and binary forms, with or
1156+
without modification, are permitted provided that the following
1157+
conditions are met:
1158+
1159+
- Redistributions of source code must retain the above
1160+
copyright notice, this list of conditions and
1161+
the following disclaimer.
1162+
1163+
- Redistributions in binary form must reproduce the above
1164+
copyright notice, this list of conditions and the following
1165+
disclaimer in the documentation and/or other materials provided
1166+
with the distribution.
1167+
1168+
- Neither the name of Internet Society, IETF or IETF Trust, nor
1169+
the names of specific contributors, may be used to endorse or
1170+
promote products derived from this software without specific
1171+
prior written permission.
1172+
1173+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
1174+
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
1175+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1176+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1177+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1178+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1179+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1180+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1181+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1182+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1183+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
1184+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
1185+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1186+

cmake/SelectHTTPSBackend.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if(USE_HTTPS)
6464

6565
if(NOT CERT_LOCATION)
6666
message(STATUS "Auto-detecting default certificates location")
67-
if(CMAKE_SYSTEM_NAME MATCHES Darwin)
67+
if(EXISTS "/usr/local/opt/openssl/bin/openssl")
6868
# Check for an Homebrew installation
6969
set(OPENSSL_CMD "/usr/local/opt/openssl/bin/openssl")
7070
else()

cmake/SelectHashes.cmake

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ include(SanitizeBool)
44

55
# USE_SHA1=CollisionDetection(ON)/HTTPS/Generic/OFF
66
sanitizebool(USE_SHA1)
7+
sanitizebool(USE_SHA256)
8+
9+
# sha1
710

811
if(USE_SHA1 STREQUAL ON)
912
SET(USE_SHA1 "CollisionDetection")
@@ -22,28 +25,68 @@ endif()
2225
if(USE_SHA1 STREQUAL "CollisionDetection")
2326
set(GIT_SHA1_COLLISIONDETECT 1)
2427
elseif(USE_SHA1 STREQUAL "OpenSSL")
25-
# OPENSSL_FOUND should already be set, we're checking USE_HTTPS
26-
2728
set(GIT_SHA1_OPENSSL 1)
29+
elseif(USE_SHA1 STREQUAL "CommonCrypto")
30+
set(GIT_SHA1_COMMON_CRYPTO 1)
31+
elseif(USE_SHA1 STREQUAL "mbedTLS")
32+
set(GIT_SHA1_MBEDTLS 1)
33+
elseif(USE_SHA1 STREQUAL "Win32")
34+
set(GIT_SHA1_WIN32 1)
35+
else()
36+
message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}")
37+
endif()
38+
39+
# sha256
40+
41+
if(USE_SHA256 STREQUAL ON AND USE_HTTPS)
42+
SET(USE_SHA256 "HTTPS")
43+
elseif(USE_SHA256 STREQUAL ON)
44+
SET(USE_SHA256 "Builtin")
45+
endif()
46+
47+
if(USE_SHA256 STREQUAL "HTTPS")
48+
if(USE_HTTPS STREQUAL "SecureTransport")
49+
set(USE_SHA256 "CommonCrypto")
50+
elseif(USE_HTTPS STREQUAL "WinHTTP")
51+
set(USE_SHA256 "Win32")
52+
elseif(USE_HTTPS)
53+
set(USE_SHA256 ${USE_HTTPS})
54+
endif()
55+
endif()
56+
57+
if(USE_SHA256 STREQUAL "Builtin")
58+
set(GIT_SHA256_BUILTIN 1)
59+
elseif(USE_SHA256 STREQUAL "OpenSSL")
60+
set(GIT_SHA256_OPENSSL 1)
61+
elseif(USE_SHA256 STREQUAL "CommonCrypto")
62+
set(GIT_SHA256_COMMON_CRYPTO 1)
63+
elseif(USE_SHA256 STREQUAL "mbedTLS")
64+
set(GIT_SHA256_MBEDTLS 1)
65+
elseif(USE_SHA256 STREQUAL "Win32")
66+
set(GIT_SHA256_WIN32 1)
67+
else()
68+
message(FATAL_ERROR "Asked for unknown SHA256 backend: ${USE_SHA256}")
69+
endif()
70+
71+
# add library requirements
72+
if(USE_SHA1 STREQUAL "OpenSSL" OR USE_SHA256 STREQUAL "OpenSSL")
2873
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
2974
list(APPEND LIBGIT2_PC_LIBS "-lssl")
3075
else()
3176
list(APPEND LIBGIT2_PC_REQUIRES "openssl")
3277
endif()
33-
elseif(USE_SHA1 STREQUAL "CommonCrypto")
34-
set(GIT_SHA1_COMMON_CRYPTO 1)
35-
elseif(USE_SHA1 STREQUAL "mbedTLS")
36-
set(GIT_SHA1_MBEDTLS 1)
78+
endif()
79+
80+
if(USE_SHA1 STREQUAL "mbedTLS" OR USE_SHA256 STREQUAL "mbedTLS")
3781
list(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
3882
list(APPEND LIBGIT2_SYSTEM_LIBS ${MBEDTLS_LIBRARIES})
3983
# mbedTLS has no pkgconfig file, hence we can't require it
4084
# https://github.com/ARMmbed/mbedtls/issues/228
4185
# For now, pass its link flags as our own
4286
list(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
43-
elseif(USE_SHA1 STREQUAL "Win32")
44-
set(GIT_SHA1_WIN32 1)
45-
elseif(NOT (USE_SHA1 STREQUAL "Generic"))
46-
message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}")
4787
endif()
4888

49-
add_feature_info(SHA ON "using ${USE_SHA1}")
89+
# notify feature enablement
90+
91+
add_feature_info(SHA1 ON "using ${USE_SHA1}")
92+
add_feature_info(SHA256 ON "using ${USE_SHA256}")

include/git2/deprecated.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ GIT_EXTERN(int) git_diff_format_email_options_init(
436436
#define GITERR_WORKTREE GIT_ERROR_WORKTREE
437437
#define GITERR_SHA1 GIT_ERROR_SHA1
438438

439+
#define GIT_ERROR_SHA1 GIT_ERROR_SHA
440+
439441
/**
440442
* Return the last `git_error` object that was generated for the
441443
* current thread. This is an alias of `git_error_last` and is

include/git2/errors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ typedef enum {
106106
GIT_ERROR_FILESYSTEM,
107107
GIT_ERROR_PATCH,
108108
GIT_ERROR_WORKTREE,
109-
GIT_ERROR_SHA1,
109+
GIT_ERROR_SHA,
110110
GIT_ERROR_HTTP,
111111
GIT_ERROR_INTERNAL
112112
} git_error_t;

src/features.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
#cmakedefine GIT_SHA1_OPENSSL 1
4949
#cmakedefine GIT_SHA1_MBEDTLS 1
5050

51+
#cmakedefine GIT_SHA256_BUILTIN 1
52+
#cmakedefine GIT_SHA256_WIN32 1
53+
#cmakedefine GIT_SHA256_COMMON_CRYPTO 1
54+
#cmakedefine GIT_SHA256_OPENSSL 1
55+
#cmakedefine GIT_SHA256_MBEDTLS 1
56+
5157
#cmakedefine GIT_RAND_GETENTROPY 1
5258

5359
#endif

src/util/CMakeLists.txt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set(UTIL_INCLUDES
99
"${PROJECT_SOURCE_DIR}/src/util"
1010
"${PROJECT_SOURCE_DIR}/include")
1111

12-
file(GLOB UTIL_SRC *.c *.h allocators/*.c allocators/*.h hash/sha1.h)
12+
file(GLOB UTIL_SRC *.c *.h allocators/*.c allocators/*.h hash.h)
1313
list(SORT UTIL_SRC)
1414

1515
#
@@ -29,31 +29,45 @@ endif()
2929
#
3030

3131
if(USE_SHA1 STREQUAL "CollisionDetection")
32-
file(GLOB UTIL_SRC_HASH hash/sha1/collisiondetect.* hash/sha1/sha1dc/*)
32+
file(GLOB UTIL_SRC_SHA1 hash/collisiondetect.* hash/sha1dc/*)
3333
target_compile_definitions(util PRIVATE SHA1DC_NO_STANDARD_INCLUDES=1)
3434
target_compile_definitions(util PRIVATE SHA1DC_CUSTOM_INCLUDE_SHA1_C=\"git2_util.h\")
3535
target_compile_definitions(util PRIVATE SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"git2_util.h\")
3636
elseif(USE_SHA1 STREQUAL "OpenSSL")
37-
file(GLOB UTIL_SRC_HASH hash/sha1/openssl.*)
37+
file(GLOB UTIL_SRC_SHA1 hash/openssl.*)
3838
elseif(USE_SHA1 STREQUAL "CommonCrypto")
39-
file(GLOB UTIL_SRC_HASH hash/sha1/common_crypto.*)
39+
file(GLOB UTIL_SRC_SHA1 hash/common_crypto.*)
4040
elseif(USE_SHA1 STREQUAL "mbedTLS")
41-
file(GLOB UTIL_SRC_HASH hash/sha1/mbedtls.*)
41+
file(GLOB UTIL_SRC_SHA1 hash/mbedtls.*)
4242
elseif(USE_SHA1 STREQUAL "Win32")
43-
file(GLOB UTIL_SRC_HASH hash/sha1/win32.*)
44-
elseif(USE_SHA1 STREQUAL "Generic")
45-
file(GLOB UTIL_SRC_HASH hash/sha1/generic.*)
43+
file(GLOB UTIL_SRC_SHA1 hash/win32.*)
4644
else()
4745
message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}")
4846
endif()
4947

50-
list(SORT UTIL_SRC_HASH)
48+
list(SORT UTIL_SRC_SHA1)
49+
50+
if(USE_SHA256 STREQUAL "Builtin")
51+
file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*)
52+
elseif(USE_SHA256 STREQUAL "OpenSSL")
53+
file(GLOB UTIL_SRC_SHA256 hash/openssl.*)
54+
elseif(USE_SHA256 STREQUAL "CommonCrypto")
55+
file(GLOB UTIL_SRC_SHA256 hash/common_crypto.*)
56+
elseif(USE_SHA256 STREQUAL "mbedTLS")
57+
file(GLOB UTIL_SRC_SHA256 hash/mbedtls.*)
58+
elseif(USE_SHA256 STREQUAL "Win32")
59+
file(GLOB UTIL_SRC_SHA256 hash/win32.*)
60+
else()
61+
message(FATAL_ERROR "Asked for unknown SHA256 backend: ${USE_SHA256}")
62+
endif()
63+
64+
list(SORT UTIL_SRC_SHA256)
5165

5266
#
5367
# Build the library
5468
#
5569

56-
target_sources(util PRIVATE ${UTIL_SRC} ${UTIL_SRC_OS} ${UTIL_SRC_HASH})
70+
target_sources(util PRIVATE ${UTIL_SRC} ${UTIL_SRC_OS} ${UTIL_SRC_SHA1} ${UTIL_SRC_SHA256})
5771
ide_split_sources(util)
5872

5973
target_include_directories(util PRIVATE ${UTIL_INCLUDES} ${LIBGIT2_DEPENDENCY_INCLUDES} PUBLIC ${libgit2_SOURCE_DIR}/include)

src/util/hash.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
int git_hash_global_init(void)
1111
{
12-
return git_hash_sha1_global_init();
12+
if (git_hash_sha1_global_init() < 0 ||
13+
git_hash_sha256_global_init() < 0)
14+
return -1;
15+
16+
return 0;
1317
}
1418

1519
int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm)
@@ -20,6 +24,9 @@ int git_hash_ctx_init(git_hash_ctx *ctx, git_hash_algorithm_t algorithm)
2024
case GIT_HASH_ALGORITHM_SHA1:
2125
error = git_hash_sha1_ctx_init(&ctx->ctx.sha1);
2226
break;
27+
case GIT_HASH_ALGORITHM_SHA256:
28+
error = git_hash_sha256_ctx_init(&ctx->ctx.sha256);
29+
break;
2330
default:
2431
git_error_set(GIT_ERROR_INTERNAL, "unknown hash algorithm");
2532
error = -1;
@@ -35,6 +42,9 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx)
3542
case GIT_HASH_ALGORITHM_SHA1:
3643
git_hash_sha1_ctx_cleanup(&ctx->ctx.sha1);
3744
return;
45+
case GIT_HASH_ALGORITHM_SHA256:
46+
git_hash_sha256_ctx_cleanup(&ctx->ctx.sha256);
47+
return;
3848
default:
3949
/* unreachable */ ;
4050
}
@@ -45,6 +55,8 @@ int git_hash_init(git_hash_ctx *ctx)
4555
switch (ctx->algorithm) {
4656
case GIT_HASH_ALGORITHM_SHA1:
4757
return git_hash_sha1_init(&ctx->ctx.sha1);
58+
case GIT_HASH_ALGORITHM_SHA256:
59+
return git_hash_sha256_init(&ctx->ctx.sha256);
4860
default:
4961
/* unreachable */ ;
5062
}
@@ -58,6 +70,8 @@ int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
5870
switch (ctx->algorithm) {
5971
case GIT_HASH_ALGORITHM_SHA1:
6072
return git_hash_sha1_update(&ctx->ctx.sha1, data, len);
73+
case GIT_HASH_ALGORITHM_SHA256:
74+
return git_hash_sha256_update(&ctx->ctx.sha256, data, len);
6175
default:
6276
/* unreachable */ ;
6377
}
@@ -71,6 +85,8 @@ int git_hash_final(unsigned char *out, git_hash_ctx *ctx)
7185
switch (ctx->algorithm) {
7286
case GIT_HASH_ALGORITHM_SHA1:
7387
return git_hash_sha1_final(out, &ctx->ctx.sha1);
88+
case GIT_HASH_ALGORITHM_SHA256:
89+
return git_hash_sha256_final(out, &ctx->ctx.sha256);
7490
default:
7591
/* unreachable */ ;
7692
}

src/util/hash.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "git2_util.h"
1212

13-
#include "hash/sha1.h"
13+
#include "hash/sha.h"
1414

1515
typedef struct {
1616
void *data;
@@ -19,12 +19,14 @@ typedef struct {
1919

2020
typedef enum {
2121
GIT_HASH_ALGORITHM_NONE = 0,
22-
GIT_HASH_ALGORITHM_SHA1
22+
GIT_HASH_ALGORITHM_SHA1,
23+
GIT_HASH_ALGORITHM_SHA256
2324
} git_hash_algorithm_t;
2425

2526
typedef struct git_hash_ctx {
2627
union {
2728
git_hash_sha1_ctx sha1;
29+
git_hash_sha256_ctx sha256;
2830
} ctx;
2931
git_hash_algorithm_t algorithm;
3032
} git_hash_ctx;

0 commit comments

Comments
 (0)