Skip to content

Commit 8832172

Browse files
committed
hash: move SHA1 implementations to its own hashing context
Create a separate `git_hash_sha1_ctx` structure that is specific to the SHA1 implementation and move all SHA1 functions over to use that one instead of the generic `git_hash_ctx`. The `git_hash_ctx` for now simply has a union containing this single SHA1 implementation, only, without any mechanism to distinguish between different algortihms.
1 parent d46d3b5 commit 8832172

File tree

15 files changed

+79
-77
lines changed

15 files changed

+79
-77
lines changed

src/hash.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ int git_hash_global_init(void)
1414

1515
int git_hash_ctx_init(git_hash_ctx *ctx)
1616
{
17-
return git_hash_sha1_ctx_init(ctx);
17+
return git_hash_sha1_ctx_init(&ctx->sha1);
1818
}
1919

2020
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
2121
{
22-
git_hash_sha1_ctx_cleanup(ctx);
22+
git_hash_sha1_ctx_cleanup(&ctx->sha1);
2323
}
2424

25-
int git_hash_init(git_hash_ctx *c)
25+
int git_hash_init(git_hash_ctx *ctx)
2626
{
27-
return git_hash_sha1_init(c);
27+
return git_hash_sha1_init(&ctx->sha1);
2828
}
2929

30-
int git_hash_update(git_hash_ctx *c, const void *data, size_t len)
30+
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
3131
{
32-
return git_hash_sha1_update(c, data, len);
32+
return git_hash_sha1_update(&ctx->sha1, data, len);
3333
}
3434

35-
int git_hash_final(git_oid *out, git_hash_ctx *c)
35+
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
3636
{
37-
return git_hash_sha1_final(out, c);
37+
return git_hash_sha1_final(out, &ctx->sha1);
3838
}
3939

4040
int git_hash_buf(git_oid *out, const void *data, size_t len)

src/hash.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212

1313
#include "git2/oid.h"
1414

15-
typedef struct git_hash_ctx git_hash_ctx;
16-
1715
typedef struct {
1816
void *data;
1917
size_t len;
2018
} git_buf_vec;
2119

2220
#include "hash/sha1.h"
2321

22+
typedef struct git_hash_ctx {
23+
union {
24+
git_hash_sha1_ctx sha1;
25+
};
26+
} git_hash_ctx;
27+
2428
int git_hash_global_init(void);
2529

2630
int git_hash_ctx_init(git_hash_ctx *ctx);

src/hash/sha1.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "common.h"
1212

13+
typedef struct git_hash_sha1_ctx git_hash_sha1_ctx;
14+
1315
#if defined(GIT_SHA1_COLLISIONDETECT)
1416
# include "sha1/collisiondetect.h"
1517
#elif defined(GIT_SHA1_COMMON_CRYPTO)
@@ -26,11 +28,11 @@
2628

2729
int git_hash_sha1_global_init(void);
2830

29-
int git_hash_sha1_ctx_init(git_hash_ctx *ctx);
30-
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx);
31+
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx);
32+
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx);
3133

32-
int git_hash_sha1_init(git_hash_ctx *c);
33-
int git_hash_sha1_update(git_hash_ctx *c, const void *data, size_t len);
34-
int git_hash_sha1_final(git_oid *out, git_hash_ctx *c);
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);
3537

3638
#endif

src/hash/sha1/collisiondetect.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ int git_hash_sha1_global_init(void)
1212
return 0;
1313
}
1414

15-
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
15+
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
1616
{
1717
return git_hash_sha1_init(ctx);
1818
}
1919

20-
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
20+
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
2121
{
2222
GIT_UNUSED(ctx);
2323
}
2424

25-
int git_hash_sha1_init(git_hash_ctx *ctx)
25+
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2626
{
2727
assert(ctx);
2828
SHA1DCInit(&ctx->c);
2929
return 0;
3030
}
3131

32-
int git_hash_sha1_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)
3333
{
3434
assert(ctx);
3535
SHA1DCUpdate(&ctx->c, data, len);
3636
return 0;
3737
}
3838

39-
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
39+
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
4040
{
4141
assert(ctx);
4242
if (SHA1DCFinal(out->id, &ctx->c)) {

src/hash/sha1/collisiondetect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
#ifndef INCLUDE_hash_sha1_collisiondetect_h__
99
#define INCLUDE_hash_sha1_collisiondetect_h__
1010

11-
#include "hash.h"
11+
#include "hash/sha1.h"
1212

1313
#include "sha1dc/sha1.h"
1414

15-
struct git_hash_ctx {
15+
struct git_hash_sha1_ctx {
1616
SHA1_CTX c;
1717
};
1818

src/hash/sha1/common_crypto.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ int git_hash_sha1_global_init(void)
1414
return 0;
1515
}
1616

17-
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
17+
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
1818
{
1919
return git_hash_sha1_init(ctx);
2020
}
2121

22-
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
22+
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
2323
{
2424
GIT_UNUSED(ctx);
2525
}
2626

27-
int git_hash_sha1_init(git_hash_ctx *ctx)
27+
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2828
{
2929
assert(ctx);
3030
CC_SHA1_Init(&ctx->c);
3131
return 0;
3232
}
3333

34-
int git_hash_sha1_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)
3535
{
3636
const unsigned char *data = _data;
3737

@@ -49,7 +49,7 @@ int git_hash_sha1_update(git_hash_ctx *ctx, const void *_data, size_t len)
4949
return 0;
5050
}
5151

52-
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
52+
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
5353
{
5454
assert(ctx);
5555
CC_SHA1_Final(out->id, &ctx->c);

src/hash/sha1/common_crypto.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
#ifndef INCLUDE_hash_sha1_common_crypto_h__
99
#define INCLUDE_hash_sha1_common_crypto_h__
1010

11-
#include "hash.h"
11+
#include "hash/sha1.h"
1212

1313
#include <CommonCrypto/CommonDigest.h>
1414

15-
struct git_hash_ctx {
15+
struct git_hash_sha1_ctx {
1616
CC_SHA1_CTX c;
1717
};
1818

src/hash/sha1/generic.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
#define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
112112
#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
113113

114-
static void hash__block(git_hash_ctx *ctx, const unsigned int *data)
114+
static void hash__block(git_hash_sha1_ctx *ctx, const unsigned int *data)
115115
{
116116
unsigned int A,B,C,D,E;
117117
unsigned int array[16];
@@ -224,17 +224,17 @@ int git_hash_sha1_global_init(void)
224224
return 0;
225225
}
226226

227-
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
227+
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
228228
{
229229
return git_hash_sha1_init(ctx);
230230
}
231231

232-
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
232+
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
233233
{
234234
GIT_UNUSED(ctx);
235235
}
236236

237-
int git_hash_sha1_init(git_hash_ctx *ctx)
237+
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
238238
{
239239
ctx->size = 0;
240240

@@ -248,7 +248,7 @@ int git_hash_sha1_init(git_hash_ctx *ctx)
248248
return 0;
249249
}
250250

251-
int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
251+
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
252252
{
253253
unsigned int lenW = ctx->size & 63;
254254

@@ -278,7 +278,7 @@ int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
278278
return 0;
279279
}
280280

281-
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
281+
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
282282
{
283283
static const unsigned char pad[64] = { 0x80 };
284284
unsigned int padlen[2];
@@ -289,8 +289,8 @@ int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
289289
padlen[1] = htonl((uint32_t)(ctx->size << 3));
290290

291291
i = ctx->size & 63;
292-
git_hash_update(ctx, pad, 1+ (63 & (55 - i)));
293-
git_hash_update(ctx, padlen, 8);
292+
git_hash_sha1_update(ctx, pad, 1+ (63 & (55 - i)));
293+
git_hash_sha1_update(ctx, padlen, 8);
294294

295295
/* Output hash */
296296
for (i = 0; i < 5; i++)

src/hash/sha1/generic.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#ifndef INCLUDE_hash_sha1_generic_h__
99
#define INCLUDE_hash_sha1_generic_h__
1010

11-
#include "hash.h"
11+
#include "hash/sha1.h"
1212

13-
struct git_hash_ctx {
13+
struct git_hash_sha1_ctx {
1414
unsigned long long size;
1515
unsigned int H[5];
1616
unsigned int W[16];

src/hash/sha1/mbedtls.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,33 @@ int git_hash_sha1_global_init(void)
1212
return 0;
1313
}
1414

15-
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
15+
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
1616
{
1717
return git_hash_sha1_init(ctx);
1818
}
1919

20-
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
20+
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
2121
{
2222
assert(ctx);
2323
mbedtls_sha1_free(&ctx->c);
2424
}
2525

26-
int git_hash_sha1_init(git_hash_ctx *ctx)
26+
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2727
{
2828
assert(ctx);
2929
mbedtls_sha1_init(&ctx->c);
3030
mbedtls_sha1_starts(&ctx->c);
3131
return 0;
3232
}
3333

34-
int git_hash_sha1_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)
3535
{
3636
assert(ctx);
3737
mbedtls_sha1_update(&ctx->c, data, len);
3838
return 0;
3939
}
4040

41-
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
41+
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
4242
{
4343
assert(ctx);
4444
mbedtls_sha1_finish(&ctx->c, out->id);

0 commit comments

Comments
 (0)