Skip to content

Commit d46d3b5

Browse files
committed
hash: split into generic and SHA1-specific interface
As a preparatory step to allow multiple hashing APIs to exist at the same time, split the hashing functions into one layer for generic hashing and one layer for SHA1-specific hashing. Right now, this is simply an additional indirection layer that doesn't yet serve any purpose. In the future, the generic API will be extended to allow for choosing which hash to use, though, by simply passing an enum to the hash context initialization function. This is necessary as a first step to be ready for Git's move to SHA256.
1 parent fda2062 commit d46d3b5

File tree

8 files changed

+82
-43
lines changed

8 files changed

+82
-43
lines changed

src/hash.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,36 @@
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+
return git_hash_sha1_ctx_init(ctx);
18+
}
19+
20+
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
21+
{
22+
git_hash_sha1_ctx_cleanup(ctx);
23+
}
24+
25+
int git_hash_init(git_hash_ctx *c)
26+
{
27+
return git_hash_sha1_init(c);
28+
}
29+
30+
int git_hash_update(git_hash_ctx *c, const void *data, size_t len)
31+
{
32+
return git_hash_sha1_update(c, data, len);
33+
}
34+
35+
int git_hash_final(git_oid *out, git_hash_ctx *c)
36+
{
37+
return git_hash_sha1_final(out, c);
38+
}
39+
1040
int git_hash_buf(git_oid *out, const void *data, size_t len)
1141
{
1242
git_hash_ctx ctx;

src/hash/sha1.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@
2424
# include "sha1/generic.h"
2525
#endif
2626

27+
int git_hash_sha1_global_init(void);
28+
29+
int git_hash_sha1_ctx_init(git_hash_ctx *ctx);
30+
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx);
31+
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);
35+
2736
#endif

src/hash/sha1/collisiondetect.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,36 @@
77

88
#include "collisiondetect.h"
99

10-
int git_hash_global_init(void)
10+
int git_hash_sha1_global_init(void)
1111
{
1212
return 0;
1313
}
1414

15-
int git_hash_ctx_init(git_hash_ctx *ctx)
15+
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
1616
{
17-
return git_hash_init(ctx);
17+
return git_hash_sha1_init(ctx);
1818
}
1919

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

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

32-
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
32+
int git_hash_sha1_update(git_hash_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_final(git_oid *out, git_hash_ctx *ctx)
39+
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
4040
{
4141
assert(ctx);
4242
if (SHA1DCFinal(out->id, &ctx->c)) {

src/hash/sha1/common_crypto.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@
99

1010
#define CC_LONG_MAX ((CC_LONG)-1)
1111

12-
int git_hash_global_init(void)
12+
int git_hash_sha1_global_init(void)
1313
{
1414
return 0;
1515
}
1616

17-
int git_hash_ctx_init(git_hash_ctx *ctx)
17+
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
1818
{
19-
return git_hash_init(ctx);
19+
return git_hash_sha1_init(ctx);
2020
}
2121

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

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

34-
int git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len)
34+
int git_hash_sha1_update(git_hash_ctx *ctx, const void *_data, size_t len)
3535
{
3636
const unsigned char *data = _data;
3737

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

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

src/hash/sha1/generic.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,22 @@ static void hash__block(git_hash_ctx *ctx, const unsigned int *data)
219219
ctx->H[4] += E;
220220
}
221221

222-
int git_hash_global_init(void)
222+
int git_hash_sha1_global_init(void)
223223
{
224224
return 0;
225225
}
226226

227-
int git_hash_ctx_init(git_hash_ctx *ctx)
227+
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
228228
{
229-
return git_hash_init(ctx);
229+
return git_hash_sha1_init(ctx);
230230
}
231231

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

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

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

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

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

281-
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
281+
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
282282
{
283283
static const unsigned char pad[64] = { 0x80 };
284284
unsigned int padlen[2];

src/hash/sha1/mbedtls.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@
77

88
#include "mbedtls.h"
99

10-
int git_hash_global_init(void)
10+
int git_hash_sha1_global_init(void)
1111
{
1212
return 0;
1313
}
1414

15-
int git_hash_ctx_init(git_hash_ctx *ctx)
15+
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
1616
{
17-
return git_hash_init(ctx);
17+
return git_hash_sha1_init(ctx);
1818
}
1919

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

26-
int git_hash_init(git_hash_ctx *ctx)
26+
int git_hash_sha1_init(git_hash_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_update(git_hash_ctx *ctx, const void *data, size_t len)
34+
int git_hash_sha1_update(git_hash_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_final(git_oid *out, git_hash_ctx *ctx)
41+
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
4242
{
4343
assert(ctx);
4444
mbedtls_sha1_finish(&ctx->c, out->id);

src/hash/sha1/openssl.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77

88
#include "openssl.h"
99

10-
int git_hash_global_init(void)
10+
int git_hash_sha1_global_init(void)
1111
{
1212
return 0;
1313
}
1414

15-
int git_hash_ctx_init(git_hash_ctx *ctx)
15+
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
1616
{
17-
return git_hash_init(ctx);
17+
return git_hash_sha1_init(ctx);
1818
}
1919

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

25-
int git_hash_init(git_hash_ctx *ctx)
25+
int git_hash_sha1_init(git_hash_ctx *ctx)
2626
{
2727
assert(ctx);
2828

@@ -34,7 +34,7 @@ int git_hash_init(git_hash_ctx *ctx)
3434
return 0;
3535
}
3636

37-
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
37+
int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
3838
{
3939
assert(ctx);
4040

@@ -46,7 +46,7 @@ int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
4646
return 0;
4747
}
4848

49-
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
49+
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
5050
{
5151
assert(ctx);
5252

src/hash/sha1/win32.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static void sha1_shutdown(void)
119119
hash_cryptoapi_prov_shutdown();
120120
}
121121

122-
int git_hash_global_init(void)
122+
int git_hash_sha1_global_init(void)
123123
{
124124
int error = 0;
125125

@@ -141,7 +141,7 @@ GIT_INLINE(int) hash_ctx_cryptoapi_init(git_hash_ctx *ctx)
141141
ctx->type = CRYPTOAPI;
142142
ctx->prov = &hash_prov;
143143

144-
return git_hash_init(ctx);
144+
return git_hash_sha1_init(ctx);
145145
}
146146

147147
GIT_INLINE(int) hash_cryptoapi_init(git_hash_ctx *ctx)
@@ -281,7 +281,7 @@ GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_ctx *ctx)
281281

282282
/* Indirection between CryptoAPI and CNG */
283283

284-
int git_hash_ctx_init(git_hash_ctx *ctx)
284+
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
285285
{
286286
int error = 0;
287287

@@ -292,33 +292,33 @@ int git_hash_ctx_init(git_hash_ctx *ctx)
292292
* initialized with git_libgit2_init. Otherwise, it must be initialized
293293
* at first use.
294294
*/
295-
if (hash_prov.type == INVALID && (error = git_hash_global_init()) < 0)
295+
if (hash_prov.type == INVALID && (error = git_hash_sha1_global_init()) < 0)
296296
return error;
297297

298298
memset(ctx, 0x0, sizeof(git_hash_ctx));
299299

300300
return (hash_prov.type == CNG) ? hash_ctx_cng_init(ctx) : hash_ctx_cryptoapi_init(ctx);
301301
}
302302

303-
int git_hash_init(git_hash_ctx *ctx)
303+
int git_hash_sha1_init(git_hash_ctx *ctx)
304304
{
305305
assert(ctx && ctx->type);
306306
return (ctx->type == CNG) ? hash_cng_init(ctx) : hash_cryptoapi_init(ctx);
307307
}
308308

309-
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
309+
int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
310310
{
311311
assert(ctx && ctx->type);
312312
return (ctx->type == CNG) ? hash_cng_update(ctx, data, len) : hash_cryptoapi_update(ctx, data, len);
313313
}
314314

315-
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
315+
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
316316
{
317317
assert(ctx && ctx->type);
318318
return (ctx->type == CNG) ? hash_cng_final(out, ctx) : hash_cryptoapi_final(out, ctx);
319319
}
320320

321-
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
321+
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
322322
{
323323
assert(ctx);
324324

0 commit comments

Comments
 (0)