Commit 8832172e by Patrick Steinhardt

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.
parent d46d3b53
......@@ -14,27 +14,27 @@ int git_hash_global_init(void)
int git_hash_ctx_init(git_hash_ctx *ctx)
{
return git_hash_sha1_ctx_init(ctx);
return git_hash_sha1_ctx_init(&ctx->sha1);
}
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
{
git_hash_sha1_ctx_cleanup(ctx);
git_hash_sha1_ctx_cleanup(&ctx->sha1);
}
int git_hash_init(git_hash_ctx *c)
int git_hash_init(git_hash_ctx *ctx)
{
return git_hash_sha1_init(c);
return git_hash_sha1_init(&ctx->sha1);
}
int git_hash_update(git_hash_ctx *c, const void *data, size_t len)
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
{
return git_hash_sha1_update(c, data, len);
return git_hash_sha1_update(&ctx->sha1, data, len);
}
int git_hash_final(git_oid *out, git_hash_ctx *c)
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
{
return git_hash_sha1_final(out, c);
return git_hash_sha1_final(out, &ctx->sha1);
}
int git_hash_buf(git_oid *out, const void *data, size_t len)
......
......@@ -12,8 +12,6 @@
#include "git2/oid.h"
typedef struct git_hash_ctx git_hash_ctx;
typedef struct {
void *data;
size_t len;
......@@ -21,6 +19,12 @@ typedef struct {
#include "hash/sha1.h"
typedef struct git_hash_ctx {
union {
git_hash_sha1_ctx sha1;
};
} git_hash_ctx;
int git_hash_global_init(void);
int git_hash_ctx_init(git_hash_ctx *ctx);
......
......@@ -10,6 +10,8 @@
#include "common.h"
typedef struct git_hash_sha1_ctx git_hash_sha1_ctx;
#if defined(GIT_SHA1_COLLISIONDETECT)
# include "sha1/collisiondetect.h"
#elif defined(GIT_SHA1_COMMON_CRYPTO)
......@@ -26,11 +28,11 @@
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_ctx_init(git_hash_sha1_ctx *ctx);
void git_hash_sha1_ctx_cleanup(git_hash_sha1_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);
int git_hash_sha1_init(git_hash_sha1_ctx *c);
int git_hash_sha1_update(git_hash_sha1_ctx *c, const void *data, size_t len);
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *c);
#endif
......@@ -12,31 +12,31 @@ int git_hash_sha1_global_init(void)
return 0;
}
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
return git_hash_sha1_init(ctx);
}
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
GIT_UNUSED(ctx);
}
int git_hash_sha1_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx);
SHA1DCInit(&ctx->c);
return 0;
}
int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
{
assert(ctx);
SHA1DCUpdate(&ctx->c, data, len);
return 0;
}
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
assert(ctx);
if (SHA1DCFinal(out->id, &ctx->c)) {
......
......@@ -8,11 +8,11 @@
#ifndef INCLUDE_hash_sha1_collisiondetect_h__
#define INCLUDE_hash_sha1_collisiondetect_h__
#include "hash.h"
#include "hash/sha1.h"
#include "sha1dc/sha1.h"
struct git_hash_ctx {
struct git_hash_sha1_ctx {
SHA1_CTX c;
};
......
......@@ -14,24 +14,24 @@ int git_hash_sha1_global_init(void)
return 0;
}
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
return git_hash_sha1_init(ctx);
}
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
GIT_UNUSED(ctx);
}
int git_hash_sha1_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx);
CC_SHA1_Init(&ctx->c);
return 0;
}
int git_hash_sha1_update(git_hash_ctx *ctx, const void *_data, size_t len)
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
{
const unsigned char *data = _data;
......@@ -49,7 +49,7 @@ int git_hash_sha1_update(git_hash_ctx *ctx, const void *_data, size_t len)
return 0;
}
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
assert(ctx);
CC_SHA1_Final(out->id, &ctx->c);
......
......@@ -8,11 +8,11 @@
#ifndef INCLUDE_hash_sha1_common_crypto_h__
#define INCLUDE_hash_sha1_common_crypto_h__
#include "hash.h"
#include "hash/sha1.h"
#include <CommonCrypto/CommonDigest.h>
struct git_hash_ctx {
struct git_hash_sha1_ctx {
CC_SHA1_CTX c;
};
......
......@@ -111,7 +111,7 @@
#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 )
#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
static void hash__block(git_hash_ctx *ctx, const unsigned int *data)
static void hash__block(git_hash_sha1_ctx *ctx, const unsigned int *data)
{
unsigned int A,B,C,D,E;
unsigned int array[16];
......@@ -224,17 +224,17 @@ int git_hash_sha1_global_init(void)
return 0;
}
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
return git_hash_sha1_init(ctx);
}
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
GIT_UNUSED(ctx);
}
int git_hash_sha1_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
ctx->size = 0;
......@@ -248,7 +248,7 @@ int git_hash_sha1_init(git_hash_ctx *ctx)
return 0;
}
int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
{
unsigned int lenW = ctx->size & 63;
......@@ -278,7 +278,7 @@ int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
return 0;
}
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
static const unsigned char pad[64] = { 0x80 };
unsigned int padlen[2];
......@@ -289,8 +289,8 @@ int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
padlen[1] = htonl((uint32_t)(ctx->size << 3));
i = ctx->size & 63;
git_hash_update(ctx, pad, 1+ (63 & (55 - i)));
git_hash_update(ctx, padlen, 8);
git_hash_sha1_update(ctx, pad, 1+ (63 & (55 - i)));
git_hash_sha1_update(ctx, padlen, 8);
/* Output hash */
for (i = 0; i < 5; i++)
......
......@@ -8,9 +8,9 @@
#ifndef INCLUDE_hash_sha1_generic_h__
#define INCLUDE_hash_sha1_generic_h__
#include "hash.h"
#include "hash/sha1.h"
struct git_hash_ctx {
struct git_hash_sha1_ctx {
unsigned long long size;
unsigned int H[5];
unsigned int W[16];
......
......@@ -12,18 +12,18 @@ int git_hash_sha1_global_init(void)
return 0;
}
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
return git_hash_sha1_init(ctx);
}
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
assert(ctx);
mbedtls_sha1_free(&ctx->c);
}
int git_hash_sha1_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx);
mbedtls_sha1_init(&ctx->c);
......@@ -31,14 +31,14 @@ int git_hash_sha1_init(git_hash_ctx *ctx)
return 0;
}
int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
{
assert(ctx);
mbedtls_sha1_update(&ctx->c, data, len);
return 0;
}
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
assert(ctx);
mbedtls_sha1_finish(&ctx->c, out->id);
......
......@@ -8,11 +8,11 @@
#ifndef INCLUDE_hash_sha1_mbedtls_h__
#define INCLUDE_hash_sha1_mbedtls_h__
#include "hash.h"
#include "hash/sha1.h"
#include <mbedtls/sha1.h>
struct git_hash_ctx {
struct git_hash_sha1_ctx {
mbedtls_sha1_context c;
};
......
......@@ -12,17 +12,17 @@ int git_hash_sha1_global_init(void)
return 0;
}
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
return git_hash_sha1_init(ctx);
}
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
GIT_UNUSED(ctx);
}
int git_hash_sha1_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx);
......@@ -34,7 +34,7 @@ int git_hash_sha1_init(git_hash_ctx *ctx)
return 0;
}
int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
{
assert(ctx);
......@@ -46,7 +46,7 @@ int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
return 0;
}
int git_hash_sha1_final(git_oid *out, git_hash_ctx *ctx)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
assert(ctx);
......
......@@ -8,13 +8,11 @@
#ifndef INCLUDE_hash_sha1_openssl_h__
#define INCLUDE_hash_sha1_openssl_h__
#include "common.h"
#include "hash.h"
#include "hash/sha1.h"
#include <openssl/sha.h>
struct git_hash_ctx {
struct git_hash_sha1_ctx {
SHA_CTX c;
};
......
......@@ -136,7 +136,7 @@ int git_hash_sha1_global_init(void)
/* CryptoAPI: available in Windows XP and newer */
GIT_INLINE(int) hash_ctx_cryptoapi_init(git_hash_ctx *ctx)
GIT_INLINE(int) hash_ctx_cryptoapi_init(git_hash_sha1_ctx *ctx)
{
ctx->type = CRYPTOAPI;
ctx->prov = &hash_prov;
......@@ -144,7 +144,7 @@ GIT_INLINE(int) hash_ctx_cryptoapi_init(git_hash_ctx *ctx)
return git_hash_sha1_init(ctx);
}
GIT_INLINE(int) hash_cryptoapi_init(git_hash_ctx *ctx)
GIT_INLINE(int) hash_cryptoapi_init(git_hash_sha1_ctx *ctx)
{
if (ctx->ctx.cryptoapi.valid)
CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle);
......@@ -159,7 +159,7 @@ GIT_INLINE(int) hash_cryptoapi_init(git_hash_ctx *ctx)
return 0;
}
GIT_INLINE(int) hash_cryptoapi_update(git_hash_ctx *ctx, const void *_data, size_t len)
GIT_INLINE(int) hash_cryptoapi_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
{
const BYTE *data = (BYTE *)_data;
......@@ -180,7 +180,7 @@ GIT_INLINE(int) hash_cryptoapi_update(git_hash_ctx *ctx, const void *_data, size
return 0;
}
GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_ctx *ctx)
GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
DWORD len = 20;
int error = 0;
......@@ -198,7 +198,7 @@ GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_ctx *ctx)
return error;
}
GIT_INLINE(void) hash_ctx_cryptoapi_cleanup(git_hash_ctx *ctx)
GIT_INLINE(void) hash_ctx_cryptoapi_cleanup(git_hash_sha1_ctx *ctx)
{
if (ctx->ctx.cryptoapi.valid)
CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle);
......@@ -206,7 +206,7 @@ GIT_INLINE(void) hash_ctx_cryptoapi_cleanup(git_hash_ctx *ctx)
/* CNG: Available in Windows Server 2008 and newer */
GIT_INLINE(int) hash_ctx_cng_init(git_hash_ctx *ctx)
GIT_INLINE(int) hash_ctx_cng_init(git_hash_sha1_ctx *ctx)
{
if ((ctx->ctx.cng.hash_object = git__malloc(hash_prov.prov.cng.hash_object_size)) == NULL)
return -1;
......@@ -224,7 +224,7 @@ GIT_INLINE(int) hash_ctx_cng_init(git_hash_ctx *ctx)
return 0;
}
GIT_INLINE(int) hash_cng_init(git_hash_ctx *ctx)
GIT_INLINE(int) hash_cng_init(git_hash_sha1_ctx *ctx)
{
BYTE hash[GIT_OID_RAWSZ];
......@@ -242,7 +242,7 @@ GIT_INLINE(int) hash_cng_init(git_hash_ctx *ctx)
return 0;
}
GIT_INLINE(int) hash_cng_update(git_hash_ctx *ctx, const void *_data, size_t len)
GIT_INLINE(int) hash_cng_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
{
PBYTE data = (PBYTE)_data;
......@@ -261,7 +261,7 @@ GIT_INLINE(int) hash_cng_update(git_hash_ctx *ctx, const void *_data, size_t len
return 0;
}
GIT_INLINE(int) hash_cng_final(git_oid *out, git_hash_ctx *ctx)
GIT_INLINE(int) hash_cng_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
if (ctx->prov->prov.cng.finish_hash(ctx->ctx.cng.hash_handle, out->id, GIT_OID_RAWSZ, 0) < 0) {
git_error_set(GIT_ERROR_OS, "hash could not be finished");
......@@ -273,7 +273,7 @@ GIT_INLINE(int) hash_cng_final(git_oid *out, git_hash_ctx *ctx)
return 0;
}
GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_ctx *ctx)
GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_sha1_ctx *ctx)
{
ctx->prov->prov.cng.destroy_hash(ctx->ctx.cng.hash_handle);
git__free(ctx->ctx.cng.hash_object);
......@@ -281,7 +281,7 @@ GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_ctx *ctx)
/* Indirection between CryptoAPI and CNG */
int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
int error = 0;
......@@ -295,30 +295,30 @@ int git_hash_sha1_ctx_init(git_hash_ctx *ctx)
if (hash_prov.type == INVALID && (error = git_hash_sha1_global_init()) < 0)
return error;
memset(ctx, 0x0, sizeof(git_hash_ctx));
memset(ctx, 0x0, sizeof(git_hash_sha1_ctx));
return (hash_prov.type == CNG) ? hash_ctx_cng_init(ctx) : hash_ctx_cryptoapi_init(ctx);
}
int git_hash_sha1_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx && ctx->type);
return (ctx->type == CNG) ? hash_cng_init(ctx) : hash_cryptoapi_init(ctx);
}
int git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
int git_hash_sha1_update(git_hash_sha1_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_sha1_final(git_oid *out, git_hash_ctx *ctx)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
assert(ctx && ctx->type);
return (ctx->type == CNG) ? hash_cng_final(out, ctx) : hash_cryptoapi_final(out, ctx);
}
void git_hash_sha1_ctx_cleanup(git_hash_ctx *ctx)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
assert(ctx);
......
......@@ -5,12 +5,10 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_hash_win32_h__
#define INCLUDE_hash_hash_win32_h__
#ifndef INCLUDE_hash_sha1_win32_h__
#define INCLUDE_hash_sha1_win32_h__
#include "common.h"
#include "hash.h"
#include "hash/sha1.h"
#include <wincrypt.h>
#include <strsafe.h>
......@@ -117,7 +115,7 @@ struct hash_cng_ctx {
PBYTE hash_object;
};
struct git_hash_ctx {
struct git_hash_sha1_ctx {
enum hash_win32_prov_type type;
git_hash_prov *prov;
......
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