Skip to content

Commit 83c2778

Browse files
committed
sha: support CommonCrypto for SHA256
1 parent b900981 commit 83c2778

File tree

7 files changed

+101
-12
lines changed

7 files changed

+101
-12
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ option(USE_NSEC "Support nanosecond precision file mtimes and cti
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)
3232
option(USE_SHA1 "Enable SHA1. Can be set to CollisionDetection(ON)/HTTPS" ON)
33-
option(USE_SHA256 "Enable SHA256." ON)
33+
option(USE_SHA256 "Enable SHA256. Can be set to HTTPS/Builtin" ON)
3434
option(USE_GSSAPI "Link with libgssapi for SPNEGO auth" OFF)
3535
set(USE_HTTP_PARSER "" CACHE STRING "Specifies the HTTP Parser implementation; either system or builtin.")
3636
set(REGEX_BACKEND "" CACHE STRING "Regular expression implementation. One of regcomp_l, pcre2, pcre, regcomp, or builtin.")

cmake/SelectHashes.cmake

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,26 @@ else()
4747
message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}")
4848
endif()
4949

50-
if(USE_SHA256 STREQUAL ON)
50+
if(USE_SHA256 STREQUAL ON AND USE_HTTPS)
51+
SET(USE_SHA256 "HTTPS")
52+
elseif(USE_SHA256 STREQUAL ON)
5153
SET(USE_SHA256 "Builtin")
5254
endif()
5355

56+
if(USE_SHA256 STREQUAL "HTTPS")
57+
if(USE_HTTPS STREQUAL "SecureTransport")
58+
set(USE_SHA256 "CommonCrypto")
59+
elseif(USE_HTTPS STREQUAL "WinHTTP")
60+
set(USE_SHA256 "Win32")
61+
elseif(USE_HTTPS)
62+
set(USE_SHA256 ${USE_HTTPS})
63+
endif()
64+
endif()
65+
5466
if(USE_SHA256 STREQUAL "Builtin")
5567
set(GIT_SHA256_BUILTIN 1)
68+
elseif(USE_SHA256 STREQUAL "CommonCrypto")
69+
set(GIT_SHA256_COMMON_CRYPTO 1)
5670
else()
5771
message(FATAL_ERROR "Asked for unknown SHA256 backend: ${USE_SHA256}")
5872
endif()

src/features.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#cmakedefine GIT_SHA1_MBEDTLS 1
5050

5151
#cmakedefine GIT_SHA256_BUILTIN 1
52+
#cmakedefine GIT_SHA256_COMMON_CRYPTO 1
5253

5354
#cmakedefine GIT_RAND_GETENTROPY 1
5455

src/util/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ endif()
4747

4848
list(SORT UTIL_SRC_SHA1)
4949

50-
file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*)
50+
if(USE_SHA256 STREQUAL "Builtin")
51+
file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*)
52+
elseif(USE_SHA256 STREQUAL "CommonCrypto")
53+
file(GLOB UTIL_SRC_SHA256 hash/common_crypto.*)
54+
else()
55+
message(FATAL_ERROR "Asked for unknown SHA256 backend: ${USE_SHA256}")
56+
endif()
57+
5158
list(SORT UTIL_SRC_SHA256)
5259

5360
#

src/util/hash/common_crypto.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#define CC_LONG_MAX ((CC_LONG)-1)
1111

12+
#ifdef GIT_SHA1_COMMON_CRYPTO
13+
1214
int git_hash_sha1_global_init(void)
1315
{
1416
return 0;
@@ -55,3 +57,56 @@ int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx)
5557
CC_SHA1_Final(out, &ctx->c);
5658
return 0;
5759
}
60+
61+
#endif
62+
63+
#ifdef GIT_SHA256_COMMON_CRYPTO
64+
65+
int git_hash_sha256_global_init(void)
66+
{
67+
return 0;
68+
}
69+
70+
int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx)
71+
{
72+
return git_hash_sha256_init(ctx);
73+
}
74+
75+
void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx)
76+
{
77+
GIT_UNUSED(ctx);
78+
}
79+
80+
int git_hash_sha256_init(git_hash_sha256_ctx *ctx)
81+
{
82+
GIT_ASSERT_ARG(ctx);
83+
CC_SHA256_Init(&ctx->c);
84+
return 0;
85+
}
86+
87+
int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *_data, size_t len)
88+
{
89+
const unsigned char *data = _data;
90+
91+
GIT_ASSERT_ARG(ctx);
92+
93+
while (len > 0) {
94+
CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len;
95+
96+
CC_SHA256_Update(&ctx->c, data, chunk);
97+
98+
data += chunk;
99+
len -= chunk;
100+
}
101+
102+
return 0;
103+
}
104+
105+
int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
106+
{
107+
GIT_ASSERT_ARG(ctx);
108+
CC_SHA256_Final(out, &ctx->c);
109+
return 0;
110+
}
111+
112+
#endif

src/util/hash/common_crypto.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212

1313
#include <CommonCrypto/CommonDigest.h>
1414

15+
#ifdef GIT_SHA1_COMMON_CRYPTO
1516
struct git_hash_sha1_ctx {
1617
CC_SHA1_CTX c;
1718
};
19+
#endif
20+
21+
#ifdef GIT_SHA256_COMMON_CRYPTO
22+
struct git_hash_sha256_ctx {
23+
CC_SHA256_CTX c;
24+
};
25+
#endif
1826

1927
#endif

src/util/hash/sha.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,28 @@
1313
typedef struct git_hash_sha1_ctx git_hash_sha1_ctx;
1414
typedef struct git_hash_sha256_ctx git_hash_sha256_ctx;
1515

16-
#if defined(GIT_SHA1_COMMON_CRYPTO)
16+
#if defined(GIT_SHA1_COMMON_CRYPTO) || defined(GIT_SHA256_COMMON_CRYPTO)
1717
# include "common_crypto.h"
18-
#elif defined(GIT_SHA1_OPENSSL)
18+
#endif
19+
20+
#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA1_COMMON_CRYPTO)
1921
# include "openssl.h"
20-
#elif defined(GIT_SHA1_WIN32)
22+
#endif
23+
24+
#if defined(GIT_SHA1_WIN32) || defined(GIT_SHA256_WIN32)
2125
# include "win32.h"
22-
#elif defined(GIT_SHA1_MBEDTLS)
26+
#endif
27+
28+
#if defined(GIT_SHA1_MBEDTLS) || defined(GIT_SHA256_MBEDTLS)
2329
# include "mbedtls.h"
24-
#elif defined(GIT_SHA1_COLLISIONDETECT)
30+
#endif
31+
32+
#if defined(GIT_SHA1_COLLISIONDETECT)
2533
# include "collisiondetect.h"
26-
#else
27-
# error "unknown sha1 implementation"
2834
#endif
2935

3036
#if defined(GIT_SHA256_BUILTIN)
3137
# include "builtin.h"
32-
#else
33-
# error "unknown sha256 implementation"
3438
#endif
3539

3640
/*

0 commit comments

Comments
 (0)