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") ...@@ -30,7 +30,7 @@ IF(SHA1_BACKEND STREQUAL "CollisionDetection")
ADD_DEFINITIONS(-DSHA1DC_NO_STANDARD_INCLUDES=1) ADD_DEFINITIONS(-DSHA1DC_NO_STANDARD_INCLUDES=1)
ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\") ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\")
ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_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") ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL")
# OPENSSL_FOUND should already be set, we're checking HTTPS_BACKEND # OPENSSL_FOUND should already be set, we're checking HTTPS_BACKEND
...@@ -40,11 +40,13 @@ ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL") ...@@ -40,11 +40,13 @@ ELSEIF(SHA1_BACKEND STREQUAL "OpenSSL")
ELSE() ELSE()
LIST(APPEND LIBGIT2_PC_REQUIRES "openssl") LIST(APPEND LIBGIT2_PC_REQUIRES "openssl")
ENDIF() ENDIF()
FILE(GLOB SRC_SHA1 hash/sha1/openssl.c)
ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto") ELSEIF(SHA1_BACKEND STREQUAL "CommonCrypto")
SET(GIT_SHA1_COMMON_CRYPTO 1) SET(GIT_SHA1_COMMON_CRYPTO 1)
FILE(GLOB SRC_SHA1 hash/sha1/common_crypto.c)
ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS") ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS")
SET(GIT_SHA1_MBEDTLS 1) 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_SYSTEM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES}) LIST(APPEND LIBGIT2_LIBS ${MBEDTLS_LIBRARIES})
# mbedTLS has no pkgconfig file, hence we can't require it # mbedTLS has no pkgconfig file, hence we can't require it
...@@ -53,9 +55,9 @@ ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS") ...@@ -53,9 +55,9 @@ ELSEIF(SHA1_BACKEND STREQUAL "mbedTLS")
LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES}) LIST(APPEND LIBGIT2_PC_LIBS ${MBEDTLS_LIBRARIES})
ELSEIF(SHA1_BACKEND STREQUAL "Win32") ELSEIF(SHA1_BACKEND STREQUAL "Win32")
SET(GIT_SHA1_WIN32 1) 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") 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) # ELSEIF(NOT USE_SHA1)
ELSE() ELSE()
MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend: ${SHA1_BACKEND}") MESSAGE(FATAL_ERROR "Asked for unknown SHA1 backend: ${SHA1_BACKEND}")
......
...@@ -7,6 +7,64 @@ ...@@ -7,6 +7,64 @@
#include "hash.h" #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) int git_hash_buf(git_oid *out, const void *data, size_t len)
{ {
git_hash_ctx ctx; git_hash_ctx ctx;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with * This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#ifndef INCLUDE_hash_h__ #ifndef INCLUDE_hash_h__
#define INCLUDE_hash_h__ #define INCLUDE_hash_h__
...@@ -11,31 +12,30 @@ ...@@ -11,31 +12,30 @@
#include "git2/oid.h" #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 { typedef struct {
void *data; void *data;
size_t len; size_t len;
} git_buf_vec; } 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_init(git_hash_ctx *c);
int git_hash_update(git_hash_ctx *c, const void *data, size_t len); 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); 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 @@ ...@@ -5,39 +5,38 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#ifndef INCLUDE_hash_hash_collisiondetect_h__ #include "collisiondetect.h"
#define INCLUDE_hash_hash_collisiondetect_h__
#include "hash.h" int git_hash_sha1_global_init(void)
#include "sha1dc/sha1.h" {
return 0;
struct git_hash_ctx { }
SHA1_CTX c;
};
#define git_hash_ctx_init(ctx) git_hash_init(ctx) int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
#define git_hash_ctx_cleanup(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); assert(ctx);
SHA1DCInit(&ctx->c); SHA1DCInit(&ctx->c);
return 0; 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); assert(ctx);
SHA1DCUpdate(&ctx->c, data, len); SHA1DCUpdate(&ctx->c, data, len);
return 0; 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); assert(ctx);
if (SHA1DCFinal(out->id, &ctx->c)) { if (SHA1DCFinal(out->id, &ctx->c)) {
...@@ -47,5 +46,3 @@ GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx) ...@@ -47,5 +46,3 @@ GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
return 0; 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 @@ ...@@ -5,35 +5,33 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#ifndef INCLUDE_hash_hash_common_crypto_h__ #include "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;
};
#define CC_LONG_MAX ((CC_LONG)-1) #define CC_LONG_MAX ((CC_LONG)-1)
#define git_hash_ctx_init(ctx) git_hash_init(ctx) int git_hash_sha1_global_init(void)
#define git_hash_ctx_cleanup(ctx)
GIT_INLINE(int) git_hash_global_init(void)
{ {
return 0; 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); assert(ctx);
CC_SHA1_Init(&ctx->c); CC_SHA1_Init(&ctx->c);
return 0; 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; 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 ...@@ -51,11 +49,9 @@ GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len
return 0; 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); assert(ctx);
CC_SHA1_Final(out->id, &ctx->c); CC_SHA1_Final(out->id, &ctx->c);
return 0; 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 @@ ...@@ -5,9 +5,7 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#include "hash_generic.h" #include "generic.h"
#include "hash.h"
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
...@@ -113,7 +111,7 @@ ...@@ -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_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 ) #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 A,B,C,D,E;
unsigned int array[16]; unsigned int array[16];
...@@ -221,7 +219,22 @@ static void hash__block(git_hash_ctx *ctx, const unsigned int *data) ...@@ -221,7 +219,22 @@ static void hash__block(git_hash_ctx *ctx, const unsigned int *data)
ctx->H[4] += E; 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; ctx->size = 0;
...@@ -235,7 +248,7 @@ int git_hash_init(git_hash_ctx *ctx) ...@@ -235,7 +248,7 @@ int git_hash_init(git_hash_ctx *ctx)
return 0; 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; unsigned int lenW = ctx->size & 63;
...@@ -265,7 +278,7 @@ int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) ...@@ -265,7 +278,7 @@ int git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
return 0; 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 }; static const unsigned char pad[64] = { 0x80 };
unsigned int padlen[2]; unsigned int padlen[2];
...@@ -276,8 +289,8 @@ int git_hash_final(git_oid *out, git_hash_ctx *ctx) ...@@ -276,8 +289,8 @@ int git_hash_final(git_oid *out, git_hash_ctx *ctx)
padlen[1] = htonl((uint32_t)(ctx->size << 3)); padlen[1] = htonl((uint32_t)(ctx->size << 3));
i = ctx->size & 63; i = ctx->size & 63;
git_hash_update(ctx, pad, 1+ (63 & (55 - i))); git_hash_sha1_update(ctx, pad, 1+ (63 & (55 - i)));
git_hash_update(ctx, padlen, 8); git_hash_sha1_update(ctx, padlen, 8);
/* Output hash */ /* Output hash */
for (i = 0; i < 5; i++) for (i = 0; i < 5; i++)
...@@ -285,4 +298,3 @@ int git_hash_final(git_oid *out, git_hash_ctx *ctx) ...@@ -285,4 +298,3 @@ int git_hash_final(git_oid *out, git_hash_ctx *ctx)
return 0; return 0;
} }
...@@ -5,25 +5,15 @@ ...@@ -5,25 +5,15 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#ifndef INCLUDE_hash_hash_generic_h__ #ifndef INCLUDE_hash_sha1_generic_h__
#define INCLUDE_hash_hash_generic_h__ #define INCLUDE_hash_sha1_generic_h__
#include "common.h" #include "hash/sha1.h"
#include "hash.h" struct git_hash_sha1_ctx {
struct git_hash_ctx {
unsigned long long size; unsigned long long size;
unsigned int H[5]; unsigned int H[5];
unsigned int W[16]; 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 #endif
...@@ -5,17 +5,25 @@ ...@@ -5,17 +5,25 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#include "common.h" #include "mbedtls.h"
#include "hash.h"
#include "hash/hash_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); assert(ctx);
mbedtls_sha1_free(&ctx->c); 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); assert(ctx);
mbedtls_sha1_init(&ctx->c); mbedtls_sha1_init(&ctx->c);
...@@ -23,14 +31,14 @@ int git_hash_init(git_hash_ctx *ctx) ...@@ -23,14 +31,14 @@ int git_hash_init(git_hash_ctx *ctx)
return 0; 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); assert(ctx);
mbedtls_sha1_update(&ctx->c, data, len); mbedtls_sha1_update(&ctx->c, data, len);
return 0; 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); assert(ctx);
mbedtls_sha1_finish(&ctx->c, out->id); mbedtls_sha1_finish(&ctx->c, out->id);
......
...@@ -5,20 +5,15 @@ ...@@ -5,20 +5,15 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#ifndef INCLUDE_hash_mbedtld_h__ #ifndef INCLUDE_hash_sha1_mbedtls_h__
#define INCLUDE_hash_mbedtld_h__ #define INCLUDE_hash_sha1_mbedtls_h__
#include "hash/sha1.h"
#include <mbedtls/sha1.h> #include <mbedtls/sha1.h>
struct git_hash_ctx { struct git_hash_sha1_ctx {
mbedtls_sha1_context c; mbedtls_sha1_context c;
}; };
#define git_hash_ctx_init(ctx) git_hash_init(ctx) #endif /* INCLUDE_hash_sha1_mbedtls_h__ */
GIT_INLINE(int) git_hash_global_init(void)
{
return 0;
}
#endif /* INCLUDE_hash_mbedtld_h__ */
...@@ -5,26 +5,24 @@ ...@@ -5,26 +5,24 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#ifndef INCLUDE_hash_hash_openssl_h__ #include "openssl.h"
#define INCLUDE_hash_hash_openssl_h__
#include "hash.h" int git_hash_sha1_global_init(void)
{
#include <openssl/sha.h> return 0;
}
struct git_hash_ctx {
SHA_CTX c;
};
#define git_hash_ctx_init(ctx) git_hash_init(ctx) int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
#define git_hash_ctx_cleanup(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); assert(ctx);
...@@ -36,7 +34,7 @@ GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx) ...@@ -36,7 +34,7 @@ GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
return 0; 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); assert(ctx);
...@@ -48,7 +46,7 @@ GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len) ...@@ -48,7 +46,7 @@ GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
return 0; 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); assert(ctx);
...@@ -59,5 +57,3 @@ GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx) ...@@ -59,5 +57,3 @@ GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
return 0; 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 @@ ...@@ -5,14 +5,24 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#include "hash_win32.h" #include "win32.h"
#include "global.h" #include "global.h"
#include "hash.h"
#include <wincrypt.h> #include <wincrypt.h>
#include <strsafe.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}; static struct git_hash_prov hash_prov = {0};
/* Hash initialization */ /* Hash initialization */
...@@ -101,7 +111,7 @@ GIT_INLINE(void) hash_cryptoapi_prov_shutdown(void) ...@@ -101,7 +111,7 @@ GIT_INLINE(void) hash_cryptoapi_prov_shutdown(void)
hash_prov.type = INVALID; hash_prov.type = INVALID;
} }
static void git_hash_global_shutdown(void) static void sha1_shutdown(void)
{ {
if (hash_prov.type == CNG) if (hash_prov.type == CNG)
hash_cng_prov_shutdown(); hash_cng_prov_shutdown();
...@@ -109,7 +119,7 @@ static void git_hash_global_shutdown(void) ...@@ -109,7 +119,7 @@ static void git_hash_global_shutdown(void)
hash_cryptoapi_prov_shutdown(); hash_cryptoapi_prov_shutdown();
} }
int git_hash_global_init(void) int git_hash_sha1_global_init(void)
{ {
int error = 0; int error = 0;
...@@ -119,22 +129,22 @@ int git_hash_global_init(void) ...@@ -119,22 +129,22 @@ int git_hash_global_init(void)
if ((error = hash_cng_prov_init()) < 0) if ((error = hash_cng_prov_init()) < 0)
error = hash_cryptoapi_prov_init(); error = hash_cryptoapi_prov_init();
git__on_shutdown(git_hash_global_shutdown); git__on_shutdown(sha1_shutdown);
return error; return error;
} }
/* CryptoAPI: available in Windows XP and newer */ /* 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->type = CRYPTOAPI;
ctx->prov = &hash_prov; 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) if (ctx->ctx.cryptoapi.valid)
CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle); CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle);
...@@ -149,7 +159,7 @@ GIT_INLINE(int) hash_cryptoapi_init(git_hash_ctx *ctx) ...@@ -149,7 +159,7 @@ GIT_INLINE(int) hash_cryptoapi_init(git_hash_ctx *ctx)
return 0; 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; const BYTE *data = (BYTE *)_data;
...@@ -170,7 +180,7 @@ GIT_INLINE(int) hash_cryptoapi_update(git_hash_ctx *ctx, const void *_data, size ...@@ -170,7 +180,7 @@ GIT_INLINE(int) hash_cryptoapi_update(git_hash_ctx *ctx, const void *_data, size
return 0; 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; DWORD len = 20;
int error = 0; int error = 0;
...@@ -188,7 +198,7 @@ GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_ctx *ctx) ...@@ -188,7 +198,7 @@ GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_ctx *ctx)
return error; 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) if (ctx->ctx.cryptoapi.valid)
CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle); CryptDestroyHash(ctx->ctx.cryptoapi.hash_handle);
...@@ -196,7 +206,7 @@ GIT_INLINE(void) hash_ctx_cryptoapi_cleanup(git_hash_ctx *ctx) ...@@ -196,7 +206,7 @@ GIT_INLINE(void) hash_ctx_cryptoapi_cleanup(git_hash_ctx *ctx)
/* CNG: Available in Windows Server 2008 and newer */ /* 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) if ((ctx->ctx.cng.hash_object = git__malloc(hash_prov.prov.cng.hash_object_size)) == NULL)
return -1; return -1;
...@@ -214,7 +224,7 @@ GIT_INLINE(int) hash_ctx_cng_init(git_hash_ctx *ctx) ...@@ -214,7 +224,7 @@ GIT_INLINE(int) hash_ctx_cng_init(git_hash_ctx *ctx)
return 0; 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]; BYTE hash[GIT_OID_RAWSZ];
...@@ -232,7 +242,7 @@ GIT_INLINE(int) hash_cng_init(git_hash_ctx *ctx) ...@@ -232,7 +242,7 @@ GIT_INLINE(int) hash_cng_init(git_hash_ctx *ctx)
return 0; 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; PBYTE data = (PBYTE)_data;
...@@ -251,7 +261,7 @@ GIT_INLINE(int) hash_cng_update(git_hash_ctx *ctx, const void *_data, size_t len ...@@ -251,7 +261,7 @@ GIT_INLINE(int) hash_cng_update(git_hash_ctx *ctx, const void *_data, size_t len
return 0; 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) { 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"); 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) ...@@ -263,7 +273,7 @@ GIT_INLINE(int) hash_cng_final(git_oid *out, git_hash_ctx *ctx)
return 0; 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); ctx->prov->prov.cng.destroy_hash(ctx->ctx.cng.hash_handle);
git__free(ctx->ctx.cng.hash_object); git__free(ctx->ctx.cng.hash_object);
...@@ -271,7 +281,7 @@ GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_ctx *ctx) ...@@ -271,7 +281,7 @@ GIT_INLINE(void) hash_ctx_cng_cleanup(git_hash_ctx *ctx)
/* Indirection between CryptoAPI and CNG */ /* 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; int error = 0;
...@@ -282,33 +292,33 @@ int git_hash_ctx_init(git_hash_ctx *ctx) ...@@ -282,33 +292,33 @@ int git_hash_ctx_init(git_hash_ctx *ctx)
* initialized with git_libgit2_init. Otherwise, it must be initialized * initialized with git_libgit2_init. Otherwise, it must be initialized
* at first use. * 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; 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); 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); assert(ctx && ctx->type);
return (ctx->type == CNG) ? hash_cng_init(ctx) : hash_cryptoapi_init(ctx); 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); assert(ctx && ctx->type);
return (ctx->type == CNG) ? hash_cng_update(ctx, data, len) : hash_cryptoapi_update(ctx, data, len); 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); assert(ctx && ctx->type);
return (ctx->type == CNG) ? hash_cng_final(out, ctx) : hash_cryptoapi_final(out, ctx); 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); assert(ctx);
......
...@@ -5,12 +5,10 @@ ...@@ -5,12 +5,10 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#ifndef INCLUDE_hash_hash_win32_h__ #ifndef INCLUDE_hash_sha1_win32_h__
#define INCLUDE_hash_hash_win32_h__ #define INCLUDE_hash_sha1_win32_h__
#include "common.h" #include "hash/sha1.h"
#include "hash.h"
#include <wincrypt.h> #include <wincrypt.h>
#include <strsafe.h> #include <strsafe.h>
...@@ -36,17 +34,6 @@ struct hash_cryptoapi_prov { ...@@ -36,17 +34,6 @@ struct hash_cryptoapi_prov {
* would not exist when building in pre-Windows 2008 environments. * 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 */ /* Function declarations for CNG */
typedef NTSTATUS (WINAPI *hash_win32_cng_open_algorithm_provider_fn)( typedef NTSTATUS (WINAPI *hash_win32_cng_open_algorithm_provider_fn)(
HANDLE /* BCRYPT_ALG_HANDLE */ *phAlgorithm, HANDLE /* BCRYPT_ALG_HANDLE */ *phAlgorithm,
...@@ -106,14 +93,14 @@ struct hash_cng_prov { ...@@ -106,14 +93,14 @@ struct hash_cng_prov {
DWORD hash_object_size; DWORD hash_object_size;
}; };
struct git_hash_prov { typedef struct {
enum hash_win32_prov_type type; enum hash_win32_prov_type type;
union { union {
struct hash_cryptoapi_prov cryptoapi; struct hash_cryptoapi_prov cryptoapi;
struct hash_cng_prov cng; struct hash_cng_prov cng;
} prov; } prov;
}; } git_hash_prov;
/* Hash contexts */ /* Hash contexts */
...@@ -128,7 +115,7 @@ struct hash_cng_ctx { ...@@ -128,7 +115,7 @@ struct hash_cng_ctx {
PBYTE hash_object; PBYTE hash_object;
}; };
struct git_hash_ctx { struct git_hash_sha1_ctx {
enum hash_win32_prov_type type; enum hash_win32_prov_type type;
git_hash_prov *prov; git_hash_prov *prov;
...@@ -138,6 +125,4 @@ struct git_hash_ctx { ...@@ -138,6 +125,4 @@ struct git_hash_ctx {
} ctx; } ctx;
}; };
extern int git_hash_global_init(void);
#endif #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