Unverified Commit e9102def by Patrick Steinhardt Committed by GitHub

Merge pull request #4438 from pks-t/pks/hash-algorithm

Multiple hash algorithms
parents b6625a3b b7187ed7
......@@ -30,7 +30,7 @@ IF(SHA1_BACKEND STREQUAL "CollisionDetection")
ADD_DEFINITIONS(-DSHA1DC_NO_STANDARD_INCLUDES=1)
ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\")
ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\")
FILE(GLOB SRC_SHA1 hash/hash_collisiondetect.c hash/sha1dc/*)
FILE(GLOB SRC_SHA1 hash/sha1/collisiondetect.c hash/sha1/sha1dc/*)
ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL")
# OPENSSL_FOUND should already be set, we're checking HTTPS_BACKEND
......@@ -40,11 +40,13 @@ ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL")
ELSE()
LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
ENDIF()
FILE(GLOB SRC_SHA1 hash/sha1/openssl.c)
ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto")
SET(GIT_SHA1_COMMON_CRYPTO 1)
FILE(GLOB SRC_SHA1 hash/sha1/common_crypto.c)
ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS")
SET(GIT_SHA1_MBEDTLS 1)
FILE(GLOB SRC_SHA1 hash/hash_mbedtls.c)
FILE(GLOB SRC_SHA1 hash/sha1/mbedtls.c)
LIST(APPEND LIBGIT2_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
# mbedTLS has no pkgconfig file, hence we can't require it
......@@ -53,9 +55,9 @@ ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS")
LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
ELSEIF(SHA1_BACKEND STREQUAL "Win32")
SET(GIT_SHA1_WIN32 1)
FILE(GLOB SRC_SHA1 hash/hash_win32.c)
FILE(GLOB SRC_SHA1 hash/sha1/win32.c)
ELSEIF(SHA1_BACKEND STREQUAL "Generic")
FILE(GLOB SRC_SHA1 hash/hash_generic.c)
FILE(GLOB SRC_SHA1 hash/sha1/generic.c)
# ELSEIF(NOT USE_SHA1)
ELSE()
MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend: ${SHA1_BACKEND}")
......
......@@ -7,6 +7,64 @@
#include "hash.h"
int git_hash_global_init(void)
{
return git_hash_sha1_global_init();
}
int git_hash_ctx_init(git_hash_ctx *ctx)
{
int error;
if ((error = git_hash_sha1_ctx_init(&ctx->sha1)) < 0)
return error;
ctx->algo = GIT_HASH_ALGO_SHA1;
return 0;
}
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
{
switch (ctx->algo) {
case GIT_HASH_ALGO_SHA1:
git_hash_sha1_ctx_cleanup(&ctx->sha1);
return;
default:
assert(0);
}
}
int git_hash_init(git_hash_ctx *ctx)
{
switch (ctx->algo) {
case GIT_HASH_ALGO_SHA1:
return git_hash_sha1_init(&ctx->sha1);
default:
assert(0);
}
}
int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
{
switch (ctx->algo) {
case GIT_HASH_ALGO_SHA1:
return git_hash_sha1_update(&ctx->sha1, data, len);
default:
assert(0);
}
}
int git_hash_final(git_oid *out, git_hash_ctx *ctx)
{
switch (ctx->algo) {
case GIT_HASH_ALGO_SHA1:
return git_hash_sha1_final(out, &ctx->sha1);
default:
assert(0);
}
}
int git_hash_buf(git_oid *out, const void *data, size_t len)
{
git_hash_ctx ctx;
......
......@@ -4,6 +4,7 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_h__
#define INCLUDE_hash_h__
......@@ -11,31 +12,30 @@
#include "git2/oid.h"
typedef struct git_hash_prov git_hash_prov;
typedef struct git_hash_ctx git_hash_ctx;
int git_hash_ctx_init(git_hash_ctx *ctx);
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
#if defined(GIT_SHA1_COLLISIONDETECT)
# include "hash/hash_collisiondetect.h"
#elif defined(GIT_SHA1_COMMON_CRYPTO)
# include "hash/hash_common_crypto.h"
#elif defined(GIT_SHA1_OPENSSL)
# include "hash/hash_openssl.h"
#elif defined(GIT_SHA1_WIN32)
# include "hash/hash_win32.h"
#elif defined(GIT_SHA1_MBEDTLS)
# include "hash/hash_mbedtls.h"
#else
# include "hash/hash_generic.h"
#endif
typedef struct {
void *data;
size_t len;
} git_buf_vec;
typedef enum {
GIT_HASH_ALGO_UNKNOWN = 0,
GIT_HASH_ALGO_SHA1,
} git_hash_algo_t;
#include "hash/sha1.h"
typedef struct git_hash_ctx {
union {
git_hash_sha1_ctx sha1;
};
git_hash_algo_t algo;
} git_hash_ctx;
int git_hash_global_init(void);
int git_hash_ctx_init(git_hash_ctx *ctx);
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
int git_hash_init(git_hash_ctx *c);
int git_hash_update(git_hash_ctx *c, const void *data, size_t len);
int git_hash_final(git_oid *out, git_hash_ctx *c);
......
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_sha1_h__
#define INCLUDE_hash_sha1_h__
#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)
# include "sha1/common_crypto.h"
#elif defined(GIT_SHA1_OPENSSL)
# include "sha1/openssl.h"
#elif defined(GIT_SHA1_WIN32)
# include "sha1/win32.h"
#elif defined(GIT_SHA1_MBEDTLS)
# include "sha1/mbedtls.h"
#else
# include "sha1/generic.h"
#endif
int git_hash_sha1_global_init(void);
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_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
......@@ -5,39 +5,38 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_hash_collisiondetect_h__
#define INCLUDE_hash_hash_collisiondetect_h__
#include "collisiondetect.h"
#include "hash.h"
#include "sha1dc/sha1.h"
struct git_hash_ctx {
SHA1_CTX c;
};
int git_hash_sha1_global_init(void)
{
return 0;
}
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
return git_hash_sha1_init(ctx);
}
GIT_INLINE(int) git_hash_global_init(void)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
return 0;
GIT_UNUSED(ctx);
}
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx);
SHA1DCInit(&ctx->c);
return 0;
}
GIT_INLINE(int) git_hash_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;
}
GIT_INLINE(int) git_hash_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)) {
......@@ -47,5 +46,3 @@ GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
return 0;
}
#endif
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_sha1_collisiondetect_h__
#define INCLUDE_hash_sha1_collisiondetect_h__
#include "hash/sha1.h"
#include "sha1dc/sha1.h"
struct git_hash_sha1_ctx {
SHA1_CTX c;
};
#endif
......@@ -5,35 +5,33 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_hash_common_crypto_h__
#define INCLUDE_hash_hash_common_crypto_h__
#include "hash.h"
#include <CommonCrypto/CommonDigest.h>
struct git_hash_ctx {
CC_SHA1_CTX c;
};
#include "common_crypto.h"
#define CC_LONG_MAX ((CC_LONG)-1)
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
GIT_INLINE(int) git_hash_global_init(void)
int git_hash_sha1_global_init(void)
{
return 0;
}
GIT_INLINE(int) git_hash_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_sha1_ctx *ctx)
{
GIT_UNUSED(ctx);
}
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx);
CC_SHA1_Init(&ctx->c);
return 0;
}
GIT_INLINE(int) git_hash_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;
......@@ -51,11 +49,9 @@ GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len
return 0;
}
GIT_INLINE(int) git_hash_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);
return 0;
}
#endif
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_sha1_common_crypto_h__
#define INCLUDE_hash_sha1_common_crypto_h__
#include "hash/sha1.h"
#include <CommonCrypto/CommonDigest.h>
struct git_hash_sha1_ctx {
CC_SHA1_CTX c;
};
#endif
......@@ -5,9 +5,7 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "hash_generic.h"
#include "hash.h"
#include "generic.h"
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
......@@ -113,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];
......@@ -221,7 +219,22 @@ static void hash__block(git_hash_ctx *ctx, const unsigned int *data)
ctx->H[4] += E;
}
int git_hash_init(git_hash_ctx *ctx)
int git_hash_sha1_global_init(void)
{
return 0;
}
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_sha1_ctx *ctx)
{
GIT_UNUSED(ctx);
}
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
ctx->size = 0;
......@@ -235,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_sha1_ctx *ctx, const void *data, size_t len)
{
unsigned int lenW = ctx->size & 63;
......@@ -265,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_sha1_ctx *ctx)
{
static const unsigned char pad[64] = { 0x80 };
unsigned int padlen[2];
......@@ -276,8 +289,8 @@ int git_hash_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++)
......@@ -285,4 +298,3 @@ int git_hash_final(git_oid *out, git_hash_ctx *ctx)
return 0;
}
......@@ -5,25 +5,15 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_hash_generic_h__
#define INCLUDE_hash_hash_generic_h__
#ifndef INCLUDE_hash_sha1_generic_h__
#define INCLUDE_hash_sha1_generic_h__
#include "common.h"
#include "hash/sha1.h"
#include "hash.h"
struct git_hash_ctx {
struct git_hash_sha1_ctx {
unsigned long long size;
unsigned int H[5];
unsigned int W[16];
};
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
GIT_INLINE(int) git_hash_global_init(void)
{
return 0;
}
#endif
......@@ -5,17 +5,25 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "hash.h"
#include "hash/hash_mbedtls.h"
#include "mbedtls.h"
void git_hash_ctx_cleanup(git_hash_ctx *ctx)
int git_hash_sha1_global_init(void)
{
return 0;
}
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_sha1_ctx *ctx)
{
assert(ctx);
mbedtls_sha1_free(&ctx->c);
}
int git_hash_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx);
mbedtls_sha1_init(&ctx->c);
......@@ -23,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_sha1_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_sha1_ctx *ctx)
{
assert(ctx);
mbedtls_sha1_finish(&ctx->c, out->id);
......
......@@ -5,20 +5,15 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_mbedtld_h__
#define INCLUDE_hash_mbedtld_h__
#ifndef INCLUDE_hash_sha1_mbedtls_h__
#define INCLUDE_hash_sha1_mbedtls_h__
#include "hash/sha1.h"
#include <mbedtls/sha1.h>
struct git_hash_ctx {
struct git_hash_sha1_ctx {
mbedtls_sha1_context c;
};
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
GIT_INLINE(int) git_hash_global_init(void)
{
return 0;
}
#endif /* INCLUDE_hash_mbedtld_h__ */
#endif /* INCLUDE_hash_sha1_mbedtls_h__ */
......@@ -5,26 +5,24 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_hash_openssl_h__
#define INCLUDE_hash_hash_openssl_h__
#include "openssl.h"
#include "hash.h"
#include <openssl/sha.h>
struct git_hash_ctx {
SHA_CTX c;
};
int git_hash_sha1_global_init(void)
{
return 0;
}
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
#define git_hash_ctx_cleanup(ctx)
int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{
return git_hash_sha1_init(ctx);
}
GIT_INLINE(int) git_hash_global_init(void)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{
return 0;
GIT_UNUSED(ctx);
}
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{
assert(ctx);
......@@ -36,7 +34,7 @@ GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
return 0;
}
GIT_INLINE(int) git_hash_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);
......@@ -48,7 +46,7 @@ GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
return 0;
}
GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{
assert(ctx);
......@@ -59,5 +57,3 @@ GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
return 0;
}
#endif
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_hash_sha1_openssl_h__
#define INCLUDE_hash_sha1_openssl_h__
#include "hash/sha1.h"
#include <openssl/sha.h>
struct git_hash_sha1_ctx {
SHA_CTX c;
};
#endif
......@@ -5,14 +5,24 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "hash_win32.h"
#include "win32.h"
#include "global.h"
#include "hash.h"
#include <wincrypt.h>
#include <strsafe.h>
#define GIT_HASH_CNG_DLL_NAME "bcrypt.dll"
/* BCRYPT_SHA1_ALGORITHM */
#define GIT_HASH_CNG_HASH_TYPE L"SHA1"
/* BCRYPT_OBJECT_LENGTH */
#define GIT_HASH_CNG_HASH_OBJECT_LEN L"ObjectLength"
/* BCRYPT_HASH_REUSEABLE_FLAGS */
#define GIT_HASH_CNG_HASH_REUSABLE 0x00000020
static struct git_hash_prov hash_prov = {0};
/* Hash initialization */
......@@ -101,7 +111,7 @@ GIT_INLINE(void) hash_cryptoapi_prov_shutdown(void)
hash_prov.type = INVALID;
}
static void git_hash_global_shutdown(void)
static void sha1_shutdown(void)
{
if (hash_prov.type == CNG)
hash_cng_prov_shutdown();
......@@ -109,7 +119,7 @@ static void git_hash_global_shutdown(void)
hash_cryptoapi_prov_shutdown();
}
int git_hash_global_init(void)
int git_hash_sha1_global_init(void)
{
int error = 0;
......@@ -119,22 +129,22 @@ int git_hash_global_init(void)
if ((error = hash_cng_prov_init()) < 0)
error = hash_cryptoapi_prov_init();
git__on_shutdown(git_hash_global_shutdown);
git__on_shutdown(sha1_shutdown);
return error;
}
/* 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;
return git_hash_init(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);
......@@ -149,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;
......@@ -170,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;
......@@ -188,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);
......@@ -196,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;
......@@ -214,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];
......@@ -232,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;
......@@ -251,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");
......@@ -263,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);
......@@ -271,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_sha1_ctx *ctx)
{
int error = 0;
......@@ -282,33 +292,33 @@ 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));
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_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_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_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_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>
......@@ -36,17 +34,6 @@ struct hash_cryptoapi_prov {
* would not exist when building in pre-Windows 2008 environments.
*/
#define GIT_HASH_CNG_DLL_NAME "bcrypt.dll"
/* BCRYPT_SHA1_ALGORITHM */
#define GIT_HASH_CNG_HASH_TYPE L"SHA1"
/* BCRYPT_OBJECT_LENGTH */
#define GIT_HASH_CNG_HASH_OBJECT_LEN L"ObjectLength"
/* BCRYPT_HASH_REUSEABLE_FLAGS */
#define GIT_HASH_CNG_HASH_REUSABLE 0x00000020
/* Function declarations for CNG */
typedef NTSTATUS (WINAPI *hash_win32_cng_open_algorithm_provider_fn)(
HANDLE /* BCRYPT_ALG_HANDLE */ *phAlgorithm,
......@@ -106,14 +93,14 @@ struct hash_cng_prov {
DWORD hash_object_size;
};
struct git_hash_prov {
typedef struct {
enum hash_win32_prov_type type;
union {
struct hash_cryptoapi_prov cryptoapi;
struct hash_cng_prov cng;
} prov;
};
} git_hash_prov;
/* Hash contexts */
......@@ -128,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;
......@@ -138,6 +125,4 @@ struct git_hash_ctx {
} ctx;
};
extern int git_hash_global_init(void);
#endif
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