Skip to content

Commit b3e3fa1

Browse files
committed
sha: support mbedTLS for SHA256
1 parent 83c2778 commit b3e3fa1

File tree

6 files changed

+80
-8
lines changed

6 files changed

+80
-8
lines changed

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: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ include(SanitizeBool)
66
sanitizebool(USE_SHA1)
77
sanitizebool(USE_SHA256)
88

9+
# sha1
10+
911
if(USE_SHA1 STREQUAL ON)
1012
SET(USE_SHA1 "CollisionDetection")
1113
elseif(USE_SHA1 STREQUAL "HTTPS")
@@ -35,18 +37,14 @@ elseif(USE_SHA1 STREQUAL "CommonCrypto")
3537
set(GIT_SHA1_COMMON_CRYPTO 1)
3638
elseif(USE_SHA1 STREQUAL "mbedTLS")
3739
set(GIT_SHA1_MBEDTLS 1)
38-
list(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
39-
list(APPEND LIBGIT2_SYSTEM_LIBS ${MBEDTLS_LIBRARIES})
40-
# mbedTLS has no pkgconfig file, hence we can't require it
41-
# https://github.com/ARMmbed/mbedtls/issues/228
42-
# For now, pass its link flags as our own
43-
list(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
4440
elseif(USE_SHA1 STREQUAL "Win32")
4541
set(GIT_SHA1_WIN32 1)
4642
else()
4743
message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}")
4844
endif()
4945

46+
# sha256
47+
5048
if(USE_SHA256 STREQUAL ON AND USE_HTTPS)
5149
SET(USE_SHA256 "HTTPS")
5250
elseif(USE_SHA256 STREQUAL ON)
@@ -67,9 +65,24 @@ if(USE_SHA256 STREQUAL "Builtin")
6765
set(GIT_SHA256_BUILTIN 1)
6866
elseif(USE_SHA256 STREQUAL "CommonCrypto")
6967
set(GIT_SHA256_COMMON_CRYPTO 1)
68+
elseif(USE_SHA256 STREQUAL "mbedTLS")
69+
set(GIT_SHA256_MBEDTLS 1)
7070
else()
7171
message(FATAL_ERROR "Asked for unknown SHA256 backend: ${USE_SHA256}")
7272
endif()
7373

74+
# add library requirements
75+
76+
if(USE_SHA1 STREQUAL "mbedTLS" OR USE_SHA256 STREQUAL "mbedTLS")
77+
list(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
78+
list(APPEND LIBGIT2_SYSTEM_LIBS ${MBEDTLS_LIBRARIES})
79+
# mbedTLS has no pkgconfig file, hence we can't require it
80+
# https://github.com/ARMmbed/mbedtls/issues/228
81+
# For now, pass its link flags as our own
82+
list(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
83+
endif()
84+
85+
# notify feature enablement
86+
7487
add_feature_info(SHA1 ON "using ${USE_SHA1}")
7588
add_feature_info(SHA256 ON "using ${USE_SHA256}")

src/features.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
#cmakedefine GIT_SHA256_BUILTIN 1
5252
#cmakedefine GIT_SHA256_COMMON_CRYPTO 1
53+
#cmakedefine GIT_SHA256_MBEDTLS 1
5354

5455
#cmakedefine GIT_RAND_GETENTROPY 1
5556

src/util/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ if(USE_SHA256 STREQUAL "Builtin")
5151
file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*)
5252
elseif(USE_SHA256 STREQUAL "CommonCrypto")
5353
file(GLOB UTIL_SRC_SHA256 hash/common_crypto.*)
54+
elseif(USE_SHA256 STREQUAL "mbedTLS")
55+
file(GLOB UTIL_SRC_SHA256 hash/mbedtls.*)
5456
else()
5557
message(FATAL_ERROR "Asked for unknown SHA256 backend: ${USE_SHA256}")
5658
endif()

src/util/hash/mbedtls.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "mbedtls.h"
99

10+
#ifdef GIT_SHA1_MBEDTLS
11+
1012
int git_hash_sha1_global_init(void)
1113
{
1214
return 0;
@@ -44,3 +46,47 @@ int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx)
4446
mbedtls_sha1_finish(&ctx->c, out);
4547
return 0;
4648
}
49+
50+
#endif
51+
52+
#ifdef GIT_SHA256_MBEDTLS
53+
54+
int git_hash_sha256_global_init(void)
55+
{
56+
return 0;
57+
}
58+
59+
int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx)
60+
{
61+
return git_hash_sha256_init(ctx);
62+
}
63+
64+
void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx)
65+
{
66+
if (ctx)
67+
mbedtls_sha256_free(&ctx->c);
68+
}
69+
70+
int git_hash_sha256_init(git_hash_sha256_ctx *ctx)
71+
{
72+
GIT_ASSERT_ARG(ctx);
73+
mbedtls_sha256_init(&ctx->c);
74+
mbedtls_sha256_starts(&ctx->c, 0);
75+
return 0;
76+
}
77+
78+
int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len)
79+
{
80+
GIT_ASSERT_ARG(ctx);
81+
mbedtls_sha256_update(&ctx->c, data, len);
82+
return 0;
83+
}
84+
85+
int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
86+
{
87+
GIT_ASSERT_ARG(ctx);
88+
mbedtls_sha256_finish(&ctx->c, out);
89+
return 0;
90+
}
91+
92+
#endif

src/util/hash/mbedtls.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@
1010

1111
#include "hash/sha.h"
1212

13-
#include <mbedtls/sha1.h>
13+
#ifdef GIT_SHA1_MBEDTLS
14+
# include <mbedtls/sha1.h>
1415

1516
struct git_hash_sha1_ctx {
1617
mbedtls_sha1_context c;
1718
};
19+
#endif
20+
21+
#ifdef GIT_SHA256_MBEDTLS
22+
# include <mbedtls/sha256.h>
23+
24+
struct git_hash_sha256_ctx {
25+
mbedtls_sha256_context c;
26+
};
27+
#endif
1828

1929
#endif /* INCLUDE_hash_sha1_mbedtls_h__ */

0 commit comments

Comments
 (0)