diff options
| author | Patrick Steinhardt <ps@pks.im> | 2019-02-22 14:32:40 +0100 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2019-06-24 18:33:22 +0200 |
| commit | 8832172e9b73bc4edf938fbac6db850eef436699 (patch) | |
| tree | 9584054470c432bcd0dcc0d2826034b386c5e7b0 /src/hash | |
| parent | d46d3b539b3e0ed3871d2ac22271a6469ed7c215 (diff) | |
| download | libgit2-8832172e9b73bc4edf938fbac6db850eef436699.tar.gz | |
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.
Diffstat (limited to 'src/hash')
| -rw-r--r-- | src/hash/sha1.h | 12 | ||||
| -rw-r--r-- | src/hash/sha1/collisiondetect.c | 10 | ||||
| -rw-r--r-- | src/hash/sha1/collisiondetect.h | 4 | ||||
| -rw-r--r-- | src/hash/sha1/common_crypto.c | 10 | ||||
| -rw-r--r-- | src/hash/sha1/common_crypto.h | 4 | ||||
| -rw-r--r-- | src/hash/sha1/generic.c | 16 | ||||
| -rw-r--r-- | src/hash/sha1/generic.h | 4 | ||||
| -rw-r--r-- | src/hash/sha1/mbedtls.c | 10 | ||||
| -rw-r--r-- | src/hash/sha1/mbedtls.h | 4 | ||||
| -rw-r--r-- | src/hash/sha1/openssl.c | 10 | ||||
| -rw-r--r-- | src/hash/sha1/openssl.h | 6 | ||||
| -rw-r--r-- | src/hash/sha1/win32.c | 32 | ||||
| -rw-r--r-- | src/hash/sha1/win32.h | 10 |
13 files changed, 65 insertions, 67 deletions
diff --git a/src/hash/sha1.h b/src/hash/sha1.h index 38a4464be..fb8d62f80 100644 --- a/src/hash/sha1.h +++ b/src/hash/sha1.h @@ -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 diff --git a/src/hash/sha1/collisiondetect.c b/src/hash/sha1/collisiondetect.c index e0c3db642..e6a126780 100644 --- a/src/hash/sha1/collisiondetect.c +++ b/src/hash/sha1/collisiondetect.c @@ -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)) { diff --git a/src/hash/sha1/collisiondetect.h b/src/hash/sha1/collisiondetect.h index 3dc30febe..eb88e86c1 100644 --- a/src/hash/sha1/collisiondetect.h +++ b/src/hash/sha1/collisiondetect.h @@ -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; }; diff --git a/src/hash/sha1/common_crypto.c b/src/hash/sha1/common_crypto.c index 3539fd012..0449a3c9d 100644 --- a/src/hash/sha1/common_crypto.c +++ b/src/hash/sha1/common_crypto.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); diff --git a/src/hash/sha1/common_crypto.h b/src/hash/sha1/common_crypto.h index 3be882892..a5fcfb33e 100644 --- a/src/hash/sha1/common_crypto.h +++ b/src/hash/sha1/common_crypto.h @@ -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; }; diff --git a/src/hash/sha1/generic.c b/src/hash/sha1/generic.c index a5a9f4cc8..607fe3a43 100644 --- a/src/hash/sha1/generic.c +++ b/src/hash/sha1/generic.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++) diff --git a/src/hash/sha1/generic.h b/src/hash/sha1/generic.h index 041b9ef68..e4cc6026b 100644 --- a/src/hash/sha1/generic.h +++ b/src/hash/sha1/generic.h @@ -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]; diff --git a/src/hash/sha1/mbedtls.c b/src/hash/sha1/mbedtls.c index 8ebf9409b..e44343fcf 100644 --- a/src/hash/sha1/mbedtls.c +++ b/src/hash/sha1/mbedtls.c @@ -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); diff --git a/src/hash/sha1/mbedtls.h b/src/hash/sha1/mbedtls.h index d066cbdd9..15f7462a4 100644 --- a/src/hash/sha1/mbedtls.h +++ b/src/hash/sha1/mbedtls.h @@ -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; }; diff --git a/src/hash/sha1/openssl.c b/src/hash/sha1/openssl.c index d93cd8b05..ba3212ff2 100644 --- a/src/hash/sha1/openssl.c +++ b/src/hash/sha1/openssl.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); diff --git a/src/hash/sha1/openssl.h b/src/hash/sha1/openssl.h index f2ec5815e..a223ca03e 100644 --- a/src/hash/sha1/openssl.h +++ b/src/hash/sha1/openssl.h @@ -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; }; diff --git a/src/hash/sha1/win32.c b/src/hash/sha1/win32.c index 8814ac3a6..1b944e27f 100644 --- a/src/hash/sha1/win32.c +++ b/src/hash/sha1/win32.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); diff --git a/src/hash/sha1/win32.h b/src/hash/sha1/win32.h index e4970ce28..791d20a42 100644 --- a/src/hash/sha1/win32.h +++ b/src/hash/sha1/win32.h @@ -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; |
