Skip to content

Commit 6b4a6fa

Browse files
committed
sha: support OpenSSL for SHA256
1 parent b3e3fa1 commit 6b4a6fa

File tree

6 files changed

+83
-11
lines changed

6 files changed

+83
-11
lines changed

cmake/SelectHashes.cmake

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@ endif()
2525
if(USE_SHA1 STREQUAL "CollisionDetection")
2626
set(GIT_SHA1_COLLISIONDETECT 1)
2727
elseif(USE_SHA1 STREQUAL "OpenSSL")
28-
# OPENSSL_FOUND should already be set, we're checking USE_HTTPS
29-
3028
set(GIT_SHA1_OPENSSL 1)
31-
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
32-
list(APPEND LIBGIT2_PC_LIBS "-lssl")
33-
else()
34-
list(APPEND LIBGIT2_PC_REQUIRES "openssl")
35-
endif()
3629
elseif(USE_SHA1 STREQUAL "CommonCrypto")
3730
set(GIT_SHA1_COMMON_CRYPTO 1)
3831
elseif(USE_SHA1 STREQUAL "mbedTLS")
@@ -63,6 +56,8 @@ endif()
6356

6457
if(USE_SHA256 STREQUAL "Builtin")
6558
set(GIT_SHA256_BUILTIN 1)
59+
elseif(USE_SHA256 STREQUAL "OpenSSL")
60+
set(GIT_SHA256_OPENSSL 1)
6661
elseif(USE_SHA256 STREQUAL "CommonCrypto")
6762
set(GIT_SHA256_COMMON_CRYPTO 1)
6863
elseif(USE_SHA256 STREQUAL "mbedTLS")
@@ -72,6 +67,13 @@ else()
7267
endif()
7368

7469
# add library requirements
70+
if(USE_SHA1 STREQUAL "OpenSSL" OR USE_SHA256 STREQUAL "OpenSSL")
71+
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
72+
list(APPEND LIBGIT2_PC_LIBS "-lssl")
73+
else()
74+
list(APPEND LIBGIT2_PC_REQUIRES "openssl")
75+
endif()
76+
endif()
7577

7678
if(USE_SHA1 STREQUAL "mbedTLS" OR USE_SHA256 STREQUAL "mbedTLS")
7779
list(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})

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_OPENSSL 1
5354
#cmakedefine GIT_SHA256_MBEDTLS 1
5455

5556
#cmakedefine GIT_RAND_GETENTROPY 1

src/util/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ list(SORT UTIL_SRC_SHA1)
4949

5050
if(USE_SHA256 STREQUAL "Builtin")
5151
file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*)
52+
elseif(USE_SHA256 STREQUAL "OpenSSL")
53+
file(GLOB UTIL_SRC_SHA256 hash/openssl.*)
5254
elseif(USE_SHA256 STREQUAL "CommonCrypto")
5355
file(GLOB UTIL_SRC_SHA256 hash/common_crypto.*)
5456
elseif(USE_SHA256 STREQUAL "mbedTLS")

src/util/hash/openssl.c

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

88
#include "openssl.h"
99

10+
#ifdef GIT_SHA1_OPENSSL
11+
1012
int git_hash_sha1_global_init(void)
1113
{
1214
return 0;
@@ -27,7 +29,7 @@ int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2729
GIT_ASSERT_ARG(ctx);
2830

2931
if (SHA1_Init(&ctx->c) != 1) {
30-
git_error_set(GIT_ERROR_SHA, "hash_openssl: failed to initialize hash context");
32+
git_error_set(GIT_ERROR_SHA, "failed to initialize sha1 context");
3133
return -1;
3234
}
3335

@@ -39,7 +41,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
3941
GIT_ASSERT_ARG(ctx);
4042

4143
if (SHA1_Update(&ctx->c, data, len) != 1) {
42-
git_error_set(GIT_ERROR_SHA, "hash_openssl: failed to update hash");
44+
git_error_set(GIT_ERROR_SHA, "failed to update sha1");
4345
return -1;
4446
}
4547

@@ -51,9 +53,66 @@ int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx)
5153
GIT_ASSERT_ARG(ctx);
5254

5355
if (SHA1_Final(out, &ctx->c) != 1) {
54-
git_error_set(GIT_ERROR_SHA, "hash_openssl: failed to finalize hash");
56+
git_error_set(GIT_ERROR_SHA, "failed to finalize sha1");
57+
return -1;
58+
}
59+
60+
return 0;
61+
}
62+
63+
#endif
64+
65+
#ifdef GIT_SHA256_OPENSSL
66+
67+
int git_hash_sha256_global_init(void)
68+
{
69+
return 0;
70+
}
71+
72+
int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx)
73+
{
74+
return git_hash_sha256_init(ctx);
75+
}
76+
77+
void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx)
78+
{
79+
GIT_UNUSED(ctx);
80+
}
81+
82+
int git_hash_sha256_init(git_hash_sha256_ctx *ctx)
83+
{
84+
GIT_ASSERT_ARG(ctx);
85+
86+
if (SHA256_Init(&ctx->c) != 1) {
87+
git_error_set(GIT_ERROR_SHA, "failed to initialize sha256 context");
88+
return -1;
89+
}
90+
91+
return 0;
92+
}
93+
94+
int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len)
95+
{
96+
GIT_ASSERT_ARG(ctx);
97+
98+
if (SHA256_Update(&ctx->c, data, len) != 1) {
99+
git_error_set(GIT_ERROR_SHA, "failed to update sha256");
55100
return -1;
56101
}
57102

58103
return 0;
59104
}
105+
106+
int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
107+
{
108+
GIT_ASSERT_ARG(ctx);
109+
110+
if (SHA256_Final(out, &ctx->c) != 1) {
111+
git_error_set(GIT_ERROR_SHA, "failed to finalize sha256");
112+
return -1;
113+
}
114+
115+
return 0;
116+
}
117+
118+
#endif

src/util/hash/openssl.h

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

1313
#include <openssl/sha.h>
1414

15+
#ifdef GIT_SHA1_OPENSSL
1516
struct git_hash_sha1_ctx {
1617
SHA_CTX c;
1718
};
19+
#endif
20+
21+
#ifdef GIT_SHA256_OPENSSL
22+
struct git_hash_sha256_ctx {
23+
SHA256_CTX c;
24+
};
25+
#endif
1826

1927
#endif

src/util/hash/sha.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef struct git_hash_sha256_ctx git_hash_sha256_ctx;
1717
# include "common_crypto.h"
1818
#endif
1919

20-
#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA1_COMMON_CRYPTO)
20+
#if defined(GIT_SHA1_OPENSSL) || defined(GIT_SHA256_OPENSSL)
2121
# include "openssl.h"
2222
#endif
2323

0 commit comments

Comments
 (0)