Skip to content

Commit b900981

Browse files
committed
sha: add sha256 algorithm
Add support for a SHA256 hash algorithm, and add the "builtin" SHA256 hash engine (from RFC 6234).
1 parent 36df49c commit b900981

File tree

14 files changed

+1221
-10
lines changed

14 files changed

+1221
-10
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +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)
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/SelectHashes.cmake

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include(SanitizeBool)
44

55
# USE_SHA1=CollisionDetection(ON)/HTTPS/Generic/OFF
66
sanitizebool(USE_SHA1)
7+
sanitizebool(USE_SHA256)
78

89
if(USE_SHA1 STREQUAL ON)
910
SET(USE_SHA1 "CollisionDetection")
@@ -46,4 +47,15 @@ else()
4647
message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}")
4748
endif()
4849

49-
add_feature_info(SHA ON "using ${USE_SHA1}")
50+
if(USE_SHA256 STREQUAL ON)
51+
SET(USE_SHA256 "Builtin")
52+
endif()
53+
54+
if(USE_SHA256 STREQUAL "Builtin")
55+
set(GIT_SHA256_BUILTIN 1)
56+
else()
57+
message(FATAL_ERROR "Asked for unknown SHA256 backend: ${USE_SHA256}")
58+
endif()
59+
60+
add_feature_info(SHA1 ON "using ${USE_SHA1}")
61+
add_feature_info(SHA256 ON "using ${USE_SHA256}")

src/features.h.in

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

51+
#cmakedefine GIT_SHA256_BUILTIN 1
52+
5153
#cmakedefine GIT_RAND_GETENTROPY 1
5254

5355
#endif

src/util/CMakeLists.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,32 @@ endif()
2929
#
3030

3131
if(USE_SHA1 STREQUAL "CollisionDetection")
32-
file(GLOB UTIL_SRC_HASH hash/collisiondetect.* hash/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/openssl.*)
37+
file(GLOB UTIL_SRC_SHA1 hash/openssl.*)
3838
elseif(USE_SHA1 STREQUAL "CommonCrypto")
39-
file(GLOB UTIL_SRC_HASH hash/common_crypto.*)
39+
file(GLOB UTIL_SRC_SHA1 hash/common_crypto.*)
4040
elseif(USE_SHA1 STREQUAL "mbedTLS")
41-
file(GLOB UTIL_SRC_HASH hash/mbedtls.*)
41+
file(GLOB UTIL_SRC_SHA1 hash/mbedtls.*)
4242
elseif(USE_SHA1 STREQUAL "Win32")
43-
file(GLOB UTIL_SRC_HASH hash/win32.*)
43+
file(GLOB UTIL_SRC_SHA1 hash/win32.*)
4444
else()
4545
message(FATAL_ERROR "Asked for unknown SHA1 backend: ${USE_SHA1}")
4646
endif()
4747

48-
list(SORT UTIL_SRC_HASH)
48+
list(SORT UTIL_SRC_SHA1)
49+
50+
file(GLOB UTIL_SRC_SHA256 hash/builtin.* hash/rfc6234/*)
51+
list(SORT UTIL_SRC_SHA256)
4952

5053
#
5154
# Build the library
5255
#
5356

54-
target_sources(util PRIVATE ${UTIL_SRC} ${UTIL_SRC_OS} ${UTIL_SRC_HASH})
57+
target_sources(util PRIVATE ${UTIL_SRC} ${UTIL_SRC_OS} ${UTIL_SRC_SHA1} ${UTIL_SRC_SHA256})
5558
ide_split_sources(util)
5659

5760
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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;

src/util/hash/builtin.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "builtin.h"
9+
10+
int git_hash_sha256_global_init(void)
11+
{
12+
return 0;
13+
}
14+
15+
int git_hash_sha256_ctx_init(git_hash_sha256_ctx *ctx)
16+
{
17+
return git_hash_sha256_init(ctx);
18+
}
19+
20+
void git_hash_sha256_ctx_cleanup(git_hash_sha256_ctx *ctx)
21+
{
22+
GIT_UNUSED(ctx);
23+
}
24+
25+
int git_hash_sha256_init(git_hash_sha256_ctx *ctx)
26+
{
27+
GIT_ASSERT_ARG(ctx);
28+
if (SHA256Reset(&ctx->c)) {
29+
git_error_set(GIT_ERROR_SHA, "SHA256 error");
30+
return -1;
31+
}
32+
return 0;
33+
}
34+
35+
int git_hash_sha256_update(git_hash_sha256_ctx *ctx, const void *data, size_t len)
36+
{
37+
GIT_ASSERT_ARG(ctx);
38+
if (SHA256Input(&ctx->c, data, len)) {
39+
git_error_set(GIT_ERROR_SHA, "SHA256 error");
40+
return -1;
41+
}
42+
return 0;
43+
}
44+
45+
int git_hash_sha256_final(unsigned char *out, git_hash_sha256_ctx *ctx)
46+
{
47+
GIT_ASSERT_ARG(ctx);
48+
if (SHA256Result(&ctx->c, out)) {
49+
git_error_set(GIT_ERROR_SHA, "SHA256 error");
50+
return -1;
51+
}
52+
return 0;
53+
}

src/util/hash/builtin.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_hash_builtin_h__
9+
#define INCLUDE_hash_builtin_h__
10+
11+
#include "hash/sha.h"
12+
13+
#include "rfc6234/sha.h"
14+
15+
struct git_hash_sha256_ctx {
16+
SHA256Context c;
17+
};
18+
19+
#endif

0 commit comments

Comments
 (0)