Skip to content

Commit e9102de

Browse files
authored
Merge pull request libgit2#4438 from pks-t/pks/hash-algorithm
Multiple hash algorithms
2 parents b6625a3 + b7187ed commit e9102de

File tree

20 files changed

+308
-164
lines changed

20 files changed

+308
-164
lines changed

cmake/Modules/SelectHashes.cmake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ IF(SHA1_BACKEND STREQUAL "CollisionDetection")
3030
ADD_DEFINITIONS(-DSHA1DC_NO_STANDARD_INCLUDES=1)
3131
ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\")
3232
ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\")
33-
FILE(GLOB SRC_SHA1 hash/hash_collisiondetect.c hash/sha1dc/*)
33+
FILE(GLOB SRC_SHA1 hash/sha1/collisiondetect.c hash/sha1/sha1dc/*)
3434
ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL")
3535
# OPENSSL_FOUND should already be set, we're checking HTTPS_BACKEND
3636

@@ -40,11 +40,13 @@ ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL")
4040
ELSE()
4141
LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
4242
ENDIF()
43+
FILE(GLOB SRC_SHA1 hash/sha1/openssl.c)
4344
ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto")
4445
SET(GIT_SHA1_COMMON_CRYPTO 1)
46+
FILE(GLOB SRC_SHA1 hash/sha1/common_crypto.c)
4547
ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS")
4648
SET(GIT_SHA1_MBEDTLS 1)
47-
FILE(GLOB SRC_SHA1 hash/hash_mbedtls.c)
49+
FILE(GLOB SRC_SHA1 hash/sha1/mbedtls.c)
4850
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
4951
LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
5052
# mbedTLS has no pkgconfig file, hence we can't require it
@@ -53,9 +55,9 @@ ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS")
5355
LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
5456
ELSEIF(SHA1_BACKEND STREQUAL "Win32")
5557
SET(GIT_SHA1_WIN32 1)
56-
FILE(GLOB SRC_SHA1 hash/hash_win32.c)
58+
FILE(GLOB SRC_SHA1 hash/sha1/win32.c)
5759
ELSEIF(SHA1_BACKEND STREQUAL "Generic")
58-
FILE(GLOB SRC_SHA1 hash/hash_generic.c)
60+
FILE(GLOB SRC_SHA1 hash/sha1/generic.c)
5961
# ELSEIF(NOT USE_SHA1)
6062
ELSE()
6163
MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend: ${SHA1_BACKEND}")

src/hash.c

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

88
#include "hash.h"
99

10+
int git_hash_global_init(void)
11+
{
12+
return git_hash_sha1_global_init();
13+
}
14+
15+
int git_hash_ctx_init(git_hash_ctx *ctx)
16+
{
17+
int error;
18+
19+
if ((error = git_hash_sha1_ctx_init(&ctx->sha1)) < 0)
20+
return error;
21+
22+
ctx->algo = GIT_HASH_ALGO_SHA1;
23+
24+
return 0;
25+
}
26+
27+
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
28+
{
29+
switch (ctx->algo) {
30+
case GIT_HASH_ALGO_SHA1:
31+
git_hash_sha1_ctx_cleanup(&ctx->sha1);
32+
return;
33+
default:
34+
assert(0);
35+
}
36+
}
37+
38+
int git_hash_init(git_hash_ctx *ctx)
39+
{
40+
switch (ctx->algo) {
41+
case GIT_HASH_ALGO_SHA1:
42+
return git_hash_sha1_init(&ctx->sha1);
43+
default:
44+
assert(0);
45+
}
46+
}
47+
48+
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
49+
{
50+
switch (ctx->algo) {
51+
case GIT_HASH_ALGO_SHA1:
52+
return git_hash_sha1_update(&ctx->sha1, data, len);
53+
default:
54+
assert(0);
55+
}
56+
}
57+
58+
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
59+
{
60+
switch (ctx->algo) {
61+
case GIT_HASH_ALGO_SHA1:
62+
return git_hash_sha1_final(out, &ctx->sha1);
63+
default:
64+
assert(0);
65+
}
66+
}
67+
1068
int git_hash_buf(git_oid *out, const void *data, size_t len)
1169
{
1270
git_hash_ctx ctx;

src/hash.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@
44
* This file is part of libgit2, distributed under the GNU GPL v2 with
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
7+
78
#ifndef INCLUDE_hash_h__
89
#define INCLUDE_hash_h__
910

1011
#include "common.h"
1112

1213
#include "git2/oid.h"
1314

14-
typedef struct git_hash_prov git_hash_prov;
15-
typedef struct git_hash_ctx git_hash_ctx;
16-
17-
int git_hash_ctx_init(git_hash_ctx *ctx);
18-
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
19-
20-
#if defined(GIT_SHA1_COLLISIONDETECT)
21-
# include "hash/hash_collisiondetect.h"
22-
#elif defined(GIT_SHA1_COMMON_CRYPTO)
23-
# include "hash/hash_common_crypto.h"
24-
#elif defined(GIT_SHA1_OPENSSL)
25-
# include "hash/hash_openssl.h"
26-
#elif defined(GIT_SHA1_WIN32)
27-
# include "hash/hash_win32.h"
28-
#elif defined(GIT_SHA1_MBEDTLS)
29-
# include "hash/hash_mbedtls.h"
30-
#else
31-
# include "hash/hash_generic.h"
32-
#endif
33-
3415
typedef struct {
3516
void *data;
3617
size_t len;
3718
} git_buf_vec;
3819

20+
typedef enum {
21+
GIT_HASH_ALGO_UNKNOWN = 0,
22+
GIT_HASH_ALGO_SHA1,
23+
} git_hash_algo_t;
24+
25+
#include "hash/sha1.h"
26+
27+
typedef struct git_hash_ctx {
28+
union {
29+
git_hash_sha1_ctx sha1;
30+
};
31+
git_hash_algo_t algo;
32+
} git_hash_ctx;
33+
34+
int git_hash_global_init(void);
35+
36+
int git_hash_ctx_init(git_hash_ctx *ctx);
37+
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
38+
3939
int git_hash_init(git_hash_ctx *c);
4040
int git_hash_update(git_hash_ctx *c, const void *data, size_t len);
4141
int git_hash_final(git_oid *out, git_hash_ctx *c);

src/hash/sha1.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_sha1_h__
9+
#define INCLUDE_hash_sha1_h__
10+
11+
#include "common.h"
12+
13+
typedef struct git_hash_sha1_ctx git_hash_sha1_ctx;
14+
15+
#if defined(GIT_SHA1_COLLISIONDETECT)
16+
# include "sha1/collisiondetect.h"
17+
#elif defined(GIT_SHA1_COMMON_CRYPTO)
18+
# include "sha1/common_crypto.h"
19+
#elif defined(GIT_SHA1_OPENSSL)
20+
# include "sha1/openssl.h"
21+
#elif defined(GIT_SHA1_WIN32)
22+
# include "sha1/win32.h"
23+
#elif defined(GIT_SHA1_MBEDTLS)
24+
# include "sha1/mbedtls.h"
25+
#else
26+
# include "sha1/generic.h"
27+
#endif
28+
29+
int git_hash_sha1_global_init(void);
30+
31+
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx);
32+
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx);
33+
34+
int git_hash_sha1_init(git_hash_sha1_ctx *c);
35+
int git_hash_sha1_update(git_hash_sha1_ctx *c, const void *data, size_t len);
36+
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *c);
37+
38+
#endif
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,38 @@
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
77

8-
#ifndef INCLUDE_hash_hash_collisiondetect_h__
9-
#define INCLUDE_hash_hash_collisiondetect_h__
8+
#include "collisiondetect.h"
109

11-
#include "hash.h"
12-
#include "sha1dc/sha1.h"
13-
14-
struct git_hash_ctx {
15-
SHA1_CTX c;
16-
};
10+
int git_hash_sha1_global_init(void)
11+
{
12+
return 0;
13+
}
1714

18-
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
19-
#define git_hash_ctx_cleanup(ctx)
15+
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
16+
{
17+
return git_hash_sha1_init(ctx);
18+
}
2019

21-
GIT_INLINE(int) git_hash_global_init(void)
20+
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
2221
{
23-
return 0;
22+
GIT_UNUSED(ctx);
2423
}
2524

26-
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
25+
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2726
{
2827
assert(ctx);
2928
SHA1DCInit(&ctx->c);
3029
return 0;
3130
}
3231

33-
GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
32+
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
3433
{
3534
assert(ctx);
3635
SHA1DCUpdate(&ctx->c, data, len);
3736
return 0;
3837
}
3938

40-
GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
39+
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
4140
{
4241
assert(ctx);
4342
if (SHA1DCFinal(out->id, &ctx->c)) {
@@ -47,5 +46,3 @@ GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
4746

4847
return 0;
4948
}
50-
51-
#endif

src/hash/sha1/collisiondetect.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_sha1_collisiondetect_h__
9+
#define INCLUDE_hash_sha1_collisiondetect_h__
10+
11+
#include "hash/sha1.h"
12+
13+
#include "sha1dc/sha1.h"
14+
15+
struct git_hash_sha1_ctx {
16+
SHA1_CTX c;
17+
};
18+
19+
#endif
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,33 @@
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
77

8-
#ifndef INCLUDE_hash_hash_common_crypto_h__
9-
#define INCLUDE_hash_hash_common_crypto_h__
10-
11-
#include "hash.h"
12-
13-
#include <CommonCrypto/CommonDigest.h>
14-
15-
struct git_hash_ctx {
16-
CC_SHA1_CTX c;
17-
};
8+
#include "common_crypto.h"
189

1910
#define CC_LONG_MAX ((CC_LONG)-1)
2011

21-
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
22-
#define git_hash_ctx_cleanup(ctx)
23-
24-
GIT_INLINE(int) git_hash_global_init(void)
12+
int git_hash_sha1_global_init(void)
2513
{
2614
return 0;
2715
}
2816

29-
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
17+
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
18+
{
19+
return git_hash_sha1_init(ctx);
20+
}
21+
22+
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
23+
{
24+
GIT_UNUSED(ctx);
25+
}
26+
27+
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
3028
{
3129
assert(ctx);
3230
CC_SHA1_Init(&ctx->c);
3331
return 0;
3432
}
3533

36-
GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len)
34+
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
3735
{
3836
const unsigned char *data = _data;
3937

@@ -51,11 +49,9 @@ GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len
5149
return 0;
5250
}
5351

54-
GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
52+
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
5553
{
5654
assert(ctx);
5755
CC_SHA1_Final(out->id, &ctx->c);
5856
return 0;
5957
}
60-
61-
#endif

src/hash/sha1/common_crypto.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_sha1_common_crypto_h__
9+
#define INCLUDE_hash_sha1_common_crypto_h__
10+
11+
#include "hash/sha1.h"
12+
13+
#include <CommonCrypto/CommonDigest.h>
14+
15+
struct git_hash_sha1_ctx {
16+
CC_SHA1_CTX c;
17+
};
18+
19+
#endif

0 commit comments

Comments
 (0)