diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2021-09-30 08:11:40 -0400 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-10-02 16:34:47 -0400 |
| commit | 31ecaca2d3e87d20782a54cb319f5c19e6c6e62f (patch) | |
| tree | 06131413f2e42a8c3e51bbe0134f9267531bad62 /src/hash | |
| parent | 2a713da1ec2e9f74c9edc75b06540ab095c68c34 (diff) | |
| download | libgit2-31ecaca2d3e87d20782a54cb319f5c19e6c6e62f.tar.gz | |
hash: hash functions operate on byte arrays not git_oids
Separate the concerns of the hash functions from the git_oid functions.
The git_oid structure will need to understand either SHA1 or SHA256; the
hash functions should only deal with the appropriate one of these.
Diffstat (limited to 'src/hash')
| -rw-r--r-- | src/hash/sha1.h | 4 | ||||
| -rw-r--r-- | src/hash/sha1/collisiondetect.c | 4 | ||||
| -rw-r--r-- | src/hash/sha1/common_crypto.c | 4 | ||||
| -rw-r--r-- | src/hash/sha1/generic.c | 4 | ||||
| -rw-r--r-- | src/hash/sha1/mbedtls.c | 4 | ||||
| -rw-r--r-- | src/hash/sha1/openssl.c | 4 | ||||
| -rw-r--r-- | src/hash/sha1/win32.c | 12 |
7 files changed, 19 insertions, 17 deletions
diff --git a/src/hash/sha1.h b/src/hash/sha1.h index fb8d62f80..4b4dae3f8 100644 --- a/src/hash/sha1.h +++ b/src/hash/sha1.h @@ -26,6 +26,8 @@ typedef struct git_hash_sha1_ctx git_hash_sha1_ctx; # include "sha1/generic.h" #endif +#define GIT_HASH_SHA1_SIZE 20 + int git_hash_sha1_global_init(void); int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx); @@ -33,6 +35,6 @@ 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); +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *c); #endif diff --git a/src/hash/sha1/collisiondetect.c b/src/hash/sha1/collisiondetect.c index 722ebf36f..ec7059c4c 100644 --- a/src/hash/sha1/collisiondetect.c +++ b/src/hash/sha1/collisiondetect.c @@ -36,10 +36,10 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) return 0; } -int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) { GIT_ASSERT_ARG(ctx); - if (SHA1DCFinal(out->id, &ctx->c)) { + if (SHA1DCFinal(out, &ctx->c)) { git_error_set(GIT_ERROR_SHA1, "SHA1 collision attack detected"); return -1; } diff --git a/src/hash/sha1/common_crypto.c b/src/hash/sha1/common_crypto.c index 4250e0b61..9d608f449 100644 --- a/src/hash/sha1/common_crypto.c +++ b/src/hash/sha1/common_crypto.c @@ -49,9 +49,9 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len) return 0; } -int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) { GIT_ASSERT_ARG(ctx); - CC_SHA1_Final(out->id, &ctx->c); + CC_SHA1_Final(out, &ctx->c); return 0; } diff --git a/src/hash/sha1/generic.c b/src/hash/sha1/generic.c index 607fe3a43..85b34c578 100644 --- a/src/hash/sha1/generic.c +++ b/src/hash/sha1/generic.c @@ -278,7 +278,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) return 0; } -int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) { static const unsigned char pad[64] = { 0x80 }; unsigned int padlen[2]; @@ -294,7 +294,7 @@ int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) /* Output hash */ for (i = 0; i < 5; i++) - put_be32(out->id + i*4, ctx->H[i]); + put_be32(out + i*4, ctx->H[i]); return 0; } diff --git a/src/hash/sha1/mbedtls.c b/src/hash/sha1/mbedtls.c index 04e7da5fa..56016bec8 100644 --- a/src/hash/sha1/mbedtls.c +++ b/src/hash/sha1/mbedtls.c @@ -38,9 +38,9 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) return 0; } -int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) { GIT_ASSERT_ARG(ctx); - mbedtls_sha1_finish(&ctx->c, out->id); + mbedtls_sha1_finish(&ctx->c, out); return 0; } diff --git a/src/hash/sha1/openssl.c b/src/hash/sha1/openssl.c index 68d9611d4..64bf99b3c 100644 --- a/src/hash/sha1/openssl.c +++ b/src/hash/sha1/openssl.c @@ -46,11 +46,11 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) return 0; } -int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) { GIT_ASSERT_ARG(ctx); - if (SHA1_Final(out->id, &ctx->c) != 1) { + if (SHA1_Final(out, &ctx->c) != 1) { git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash"); return -1; } diff --git a/src/hash/sha1/win32.c b/src/hash/sha1/win32.c index a6e7061c6..b89dfbad8 100644 --- a/src/hash/sha1/win32.c +++ b/src/hash/sha1/win32.c @@ -181,14 +181,14 @@ GIT_INLINE(int) hash_cryptoapi_update(git_hash_sha1_ctx *ctx, const void *_data, return 0; } -GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_sha1_ctx *ctx) +GIT_INLINE(int) hash_cryptoapi_final(unsigned char *out, git_hash_sha1_ctx *ctx) { - DWORD len = 20; + DWORD len = GIT_HASH_SHA1_SIZE; int error = 0; GIT_ASSERT(ctx->ctx.cryptoapi.valid); - if (!CryptGetHashParam(ctx->ctx.cryptoapi.hash_handle, HP_HASHVAL, out->id, &len, 0)) { + if (!CryptGetHashParam(ctx->ctx.cryptoapi.hash_handle, HP_HASHVAL, out, &len, 0)) { git_error_set(GIT_ERROR_OS, "legacy hash data could not be finished"); error = -1; } @@ -262,9 +262,9 @@ GIT_INLINE(int) hash_cng_update(git_hash_sha1_ctx *ctx, const void *_data, size_ return 0; } -GIT_INLINE(int) hash_cng_final(git_oid *out, git_hash_sha1_ctx *ctx) +GIT_INLINE(int) hash_cng_final(unsigned char *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, GIT_HASH_SHA1_SIZE, 0) < 0) { git_error_set(GIT_ERROR_OS, "hash could not be finished"); return -1; } @@ -315,7 +315,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) 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_sha1_ctx *ctx) +int git_hash_sha1_final(unsigned char *out, git_hash_sha1_ctx *ctx) { GIT_ASSERT_ARG(ctx); GIT_ASSERT_ARG(ctx->type); |
