Commit d46d3b539b3e0ed3871d2ac22271a6469ed7c215

Patrick Steinhardt 2019-04-05T10:59:46

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.

diff --git a/src/hash.c b/src/hash.c
index cc6676d..a3cb1c4 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -7,6 +7,36 @@
 
 #include "hash.h"
 
+int git_hash_global_init(void)
+{
+	return git_hash_sha1_global_init();
+}
+
+int git_hash_ctx_init(git_hash_ctx *ctx)
+{
+	return git_hash_sha1_ctx_init(ctx);
+}
+
+void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+{
+	git_hash_sha1_ctx_cleanup(ctx);
+}
+
+int git_hash_init(git_hash_ctx *c)
+{
+	return git_hash_sha1_init(c);
+}
+
+int git_hash_update(git_hash_ctx *c, const void *data, size_t len)
+{
+	return git_hash_sha1_update(c, data, len);
+}
+
+int git_hash_final(git_oid *out, git_hash_ctx *c)
+{
+	return git_hash_sha1_final(out, c);
+}
+
 int git_hash_buf(git_oid *out, const void *data, size_t len)
 {
 	git_hash_ctx ctx;
diff --git a/src/hash/sha1.h b/src/hash/sha1.h
index db0e5c1..38a4464 100644
--- a/src/hash/sha1.h
+++ b/src/hash/sha1.h
@@ -24,4 +24,13 @@
 # include "sha1/generic.h"
 #endif
 
+int git_hash_sha1_global_init(void);
+
+int git_hash_sha1_ctx_init(git_hash_ctx *ctx);
+void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx);
+
+int git_hash_sha1_init(git_hash_ctx *c);
+int git_hash_sha1_update(git_hash_ctx *c, const void *data, size_t len);
+int git_hash_sha1_final(git_oid *out, git_hash_ctx *c);
+
 #endif
diff --git a/src/hash/sha1/collisiondetect.c b/src/hash/sha1/collisiondetect.c
index 79ec4e5..e0c3db6 100644
--- a/src/hash/sha1/collisiondetect.c
+++ b/src/hash/sha1/collisiondetect.c
@@ -7,36 +7,36 @@
 
 #include "collisiondetect.h"
 
-int git_hash_global_init(void)
+int git_hash_sha1_global_init(void)
 {
 	return 0;
 }
 
-int git_hash_ctx_init(git_hash_ctx *ctx)
+int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
 {
-	return git_hash_init(ctx);
+	return git_hash_sha1_init(ctx);
 }
 
-void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
 {
 	GIT_UNUSED(ctx);
 }
 
-int git_hash_init(git_hash_ctx *ctx)
+int git_hash_sha1_init(git_hash_ctx *ctx)
 {
 	assert(ctx);
 	SHA1DCInit(&ctx->c);
 	return 0;
 }
 
-int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
+int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
 {
 	assert(ctx);
 	SHA1DCUpdate(&ctx->c, data, len);
 	return 0;
 }
 
-int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
 {
 	assert(ctx);
 	if (SHA1DCFinal(out->id, &ctx->c)) {
diff --git a/src/hash/sha1/common_crypto.c b/src/hash/sha1/common_crypto.c
index 2fa5218..3539fd0 100644
--- a/src/hash/sha1/common_crypto.c
+++ b/src/hash/sha1/common_crypto.c
@@ -9,29 +9,29 @@
 
 #define CC_LONG_MAX ((CC_LONG)-1)
 
-int git_hash_global_init(void)
+int git_hash_sha1_global_init(void)
 {
 	return 0;
 }
 
-int git_hash_ctx_init(git_hash_ctx *ctx)
+int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
 {
-	return git_hash_init(ctx);
+	return git_hash_sha1_init(ctx);
 }
 
-void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
 {
 	GIT_UNUSED(ctx);
 }
 
-int git_hash_init(git_hash_ctx *ctx)
+int git_hash_sha1_init(git_hash_ctx *ctx)
 {
 	assert(ctx);
 	CC_SHA1_Init(&ctx->c);
 	return 0;
 }
 
-int git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len)
+int git_hash_sha1_update(git_hash_ctx *ctx, const void *_data, size_t len)
 {
 	const unsigned char *data = _data;
 
@@ -49,7 +49,7 @@ int git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len)
 	return 0;
 }
 
-int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
 {
 	assert(ctx);
 	CC_SHA1_Final(out->id, &ctx->c);
diff --git a/src/hash/sha1/generic.c b/src/hash/sha1/generic.c
index 157b918..a5a9f4c 100644
--- a/src/hash/sha1/generic.c
+++ b/src/hash/sha1/generic.c
@@ -219,22 +219,22 @@ static void hash__block(git_hash_ctx *ctx, const unsigned int *data)
 	ctx->H[4] += E;
 }
 
-int git_hash_global_init(void)
+int git_hash_sha1_global_init(void)
 {
 	return 0;
 }
 
-int git_hash_ctx_init(git_hash_ctx *ctx)
+int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
 {
-	return git_hash_init(ctx);
+	return git_hash_sha1_init(ctx);
 }
 
-void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
 {
 	GIT_UNUSED(ctx);
 }
 
-int git_hash_init(git_hash_ctx *ctx)
+int git_hash_sha1_init(git_hash_ctx *ctx)
 {
 	ctx->size = 0;
 
@@ -248,7 +248,7 @@ int git_hash_init(git_hash_ctx *ctx)
 	return 0;
 }
 
-int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
+int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
 {
 	unsigned int lenW = ctx->size & 63;
 
@@ -278,7 +278,7 @@ int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
 	return 0;
 }
 
-int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
 {
 	static const unsigned char pad[64] = { 0x80 };
 	unsigned int padlen[2];
diff --git a/src/hash/sha1/mbedtls.c b/src/hash/sha1/mbedtls.c
index 1215197..8ebf940 100644
--- a/src/hash/sha1/mbedtls.c
+++ b/src/hash/sha1/mbedtls.c
@@ -7,23 +7,23 @@
 
 #include "mbedtls.h"
 
-int git_hash_global_init(void)
+int git_hash_sha1_global_init(void)
 {
 	return 0;
 }
 
-int git_hash_ctx_init(git_hash_ctx *ctx)
+int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
 {
-	return git_hash_init(ctx);
+	return git_hash_sha1_init(ctx);
 }
 
-void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
 {
     assert(ctx);
     mbedtls_sha1_free(&ctx->c);
 }
 
-int git_hash_init(git_hash_ctx *ctx)
+int git_hash_sha1_init(git_hash_ctx *ctx)
 {
     assert(ctx);
     mbedtls_sha1_init(&ctx->c);
@@ -31,14 +31,14 @@ int git_hash_init(git_hash_ctx *ctx)
     return 0;
 }
 
-int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
+int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
 {
     assert(ctx);
     mbedtls_sha1_update(&ctx->c, data, len);
     return 0;
 }
 
-int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
 {
     assert(ctx);
     mbedtls_sha1_finish(&ctx->c, out->id);
diff --git a/src/hash/sha1/openssl.c b/src/hash/sha1/openssl.c
index fd3a4da..d93cd8b 100644
--- a/src/hash/sha1/openssl.c
+++ b/src/hash/sha1/openssl.c
@@ -7,22 +7,22 @@
 
 #include "openssl.h"
 
-int git_hash_global_init(void)
+int git_hash_sha1_global_init(void)
 {
 	return 0;
 }
 
-int git_hash_ctx_init(git_hash_ctx *ctx)
+int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
 {
-	return git_hash_init(ctx);
+	return git_hash_sha1_init(ctx);
 }
 
-void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
 {
 	GIT_UNUSED(ctx);
 }
 
-int git_hash_init(git_hash_ctx *ctx)
+int git_hash_sha1_init(git_hash_ctx *ctx)
 {
 	assert(ctx);
 
@@ -34,7 +34,7 @@ int git_hash_init(git_hash_ctx *ctx)
 	return 0;
 }
 
-int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
+int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
 {
 	assert(ctx);
 
@@ -46,7 +46,7 @@ int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
 	return 0;
 }
 
-int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
 {
 	assert(ctx);
 
diff --git a/src/hash/sha1/win32.c b/src/hash/sha1/win32.c
index 0dac9be..8814ac3 100644
--- a/src/hash/sha1/win32.c
+++ b/src/hash/sha1/win32.c
@@ -119,7 +119,7 @@ static void sha1_shutdown(void)
 		hash_cryptoapi_prov_shutdown();
 }
 
-int git_hash_global_init(void)
+int git_hash_sha1_global_init(void)
 {
 	int error = 0;
 
@@ -141,7 +141,7 @@ GIT_INLINE(int) hash_ctx_cryptoapi_init(git_hash_ctx *ctx)
 	ctx->type = CRYPTOAPI;
 	ctx->prov = &hash_prov;
 
-	return git_hash_init(ctx);
+	return git_hash_sha1_init(ctx);
 }
 
 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)
 
 /* Indirection between CryptoAPI and CNG */
 
-int git_hash_ctx_init(git_hash_ctx *ctx)
+int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
 {
 	int error = 0;
 
@@ -292,7 +292,7 @@ int git_hash_ctx_init(git_hash_ctx *ctx)
 	 * initialized with git_libgit2_init.  Otherwise, it must be initialized
 	 * at first use.
 	 */
-	if (hash_prov.type == INVALID && (error = git_hash_global_init()) < 0)
+	if (hash_prov.type == INVALID && (error = git_hash_sha1_global_init()) < 0)
 		return error;
 
 	memset(ctx, 0x0, sizeof(git_hash_ctx));
@@ -300,25 +300,25 @@ int git_hash_ctx_init(git_hash_ctx *ctx)
 	return (hash_prov.type == CNG) ? hash_ctx_cng_init(ctx) : hash_ctx_cryptoapi_init(ctx);
 }
 
-int git_hash_init(git_hash_ctx *ctx)
+int git_hash_sha1_init(git_hash_ctx *ctx)
 {
 	assert(ctx && ctx->type);
 	return (ctx->type == CNG) ? hash_cng_init(ctx) : hash_cryptoapi_init(ctx);
 }
 
-int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
+int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
 {
 	assert(ctx && ctx->type);
 	return (ctx->type == CNG) ? hash_cng_update(ctx, data, len) : hash_cryptoapi_update(ctx, data, len);
 }
 
-int git_hash_final(git_oid *out, git_hash_ctx *ctx)
+int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
 {
 	assert(ctx && ctx->type);
 	return (ctx->type == CNG) ? hash_cng_final(out, ctx) : hash_cryptoapi_final(out, ctx);
 }
 
-void git_hash_ctx_cleanup(git_hash_ctx *ctx)
+void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
 {
 	assert(ctx);