Commit d46d3b53 by Patrick Steinhardt

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.
parent fda20622
......@@ -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;
......
......@@ -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
......@@ -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)) {
......
......@@ -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);
......
......@@ -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];
......
......@@ -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);
......
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment