Commit 3ff56dae by Edward Thomson

hash: use GIT_ASSERT

parent cac36006
...@@ -24,21 +24,21 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) ...@@ -24,21 +24,21 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx) int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{ {
assert(ctx); GIT_ASSERT_ARG(ctx);
SHA1DCInit(&ctx->c); SHA1DCInit(&ctx->c);
return 0; return 0;
} }
int git_hash_sha1_update(git_hash_sha1_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); GIT_ASSERT_ARG(ctx);
SHA1DCUpdate(&ctx->c, data, len); SHA1DCUpdate(&ctx->c, data, len);
return 0; return 0;
} }
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{ {
assert(ctx); GIT_ASSERT_ARG(ctx);
if (SHA1DCFinal(out->id, &ctx->c)) { if (SHA1DCFinal(out->id, &ctx->c)) {
git_error_set(GIT_ERROR_SHA1, "SHA1 collision attack detected"); git_error_set(GIT_ERROR_SHA1, "SHA1 collision attack detected");
return -1; return -1;
......
...@@ -26,7 +26,7 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) ...@@ -26,7 +26,7 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx) int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{ {
assert(ctx); GIT_ASSERT_ARG(ctx);
CC_SHA1_Init(&ctx->c); CC_SHA1_Init(&ctx->c);
return 0; return 0;
} }
...@@ -35,7 +35,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len) ...@@ -35,7 +35,7 @@ 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;
assert(ctx); GIT_ASSERT_ARG(ctx);
while (len > 0) { while (len > 0) {
CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len; CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len;
...@@ -51,7 +51,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len) ...@@ -51,7 +51,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{ {
assert(ctx); GIT_ASSERT_ARG(ctx);
CC_SHA1_Final(out->id, &ctx->c); CC_SHA1_Final(out->id, &ctx->c);
return 0; return 0;
} }
...@@ -19,28 +19,28 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) ...@@ -19,28 +19,28 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{ {
assert(ctx); if (ctx)
mbedtls_sha1_free(&ctx->c); mbedtls_sha1_free(&ctx->c);
} }
int git_hash_sha1_init(git_hash_sha1_ctx *ctx) int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{ {
assert(ctx); GIT_ASSERT_ARG(ctx);
mbedtls_sha1_init(&ctx->c); mbedtls_sha1_init(&ctx->c);
mbedtls_sha1_starts(&ctx->c); mbedtls_sha1_starts(&ctx->c);
return 0; return 0;
} }
int git_hash_sha1_update(git_hash_sha1_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); GIT_ASSERT_ARG(ctx);
mbedtls_sha1_update(&ctx->c, data, len); mbedtls_sha1_update(&ctx->c, data, len);
return 0; return 0;
} }
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{ {
assert(ctx); GIT_ASSERT_ARG(ctx);
mbedtls_sha1_finish(&ctx->c, out->id); mbedtls_sha1_finish(&ctx->c, out->id);
return 0; return 0;
} }
...@@ -24,7 +24,7 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) ...@@ -24,7 +24,7 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx) int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{ {
assert(ctx); GIT_ASSERT_ARG(ctx);
if (SHA1_Init(&ctx->c) != 1) { if (SHA1_Init(&ctx->c) != 1) {
git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to initialize hash context"); git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to initialize hash context");
...@@ -36,7 +36,7 @@ int git_hash_sha1_init(git_hash_sha1_ctx *ctx) ...@@ -36,7 +36,7 @@ int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
int git_hash_sha1_update(git_hash_sha1_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); GIT_ASSERT_ARG(ctx);
if (SHA1_Update(&ctx->c, data, len) != 1) { if (SHA1_Update(&ctx->c, data, len) != 1) {
git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to update hash"); git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to update hash");
...@@ -48,7 +48,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len) ...@@ -48,7 +48,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{ {
assert(ctx); GIT_ASSERT_ARG(ctx);
if (SHA1_Final(out->id, &ctx->c) != 1) { if (SHA1_Final(out->id, &ctx->c) != 1) {
git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash"); git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash");
......
...@@ -164,7 +164,7 @@ GIT_INLINE(int) hash_cryptoapi_update(git_hash_sha1_ctx *ctx, const void *_data, ...@@ -164,7 +164,7 @@ GIT_INLINE(int) hash_cryptoapi_update(git_hash_sha1_ctx *ctx, const void *_data,
{ {
const BYTE *data = (BYTE *)_data; const BYTE *data = (BYTE *)_data;
assert(ctx->ctx.cryptoapi.valid); GIT_ASSERT(ctx->ctx.cryptoapi.valid);
while (len > 0) { while (len > 0) {
DWORD chunk = (len > MAXDWORD) ? MAXDWORD : (DWORD)len; DWORD chunk = (len > MAXDWORD) ? MAXDWORD : (DWORD)len;
...@@ -186,7 +186,7 @@ GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_sha1_ctx *ctx) ...@@ -186,7 +186,7 @@ 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;
assert(ctx->ctx.cryptoapi.valid); 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->id, &len, 0)) {
git_error_set(GIT_ERROR_OS, "legacy hash data could not be finished"); git_error_set(GIT_ERROR_OS, "legacy hash data could not be finished");
...@@ -286,7 +286,7 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) ...@@ -286,7 +286,7 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
{ {
int error = 0; int error = 0;
assert(ctx); GIT_ASSERT_ARG(ctx);
/* /*
* When compiled with GIT_THREADS, the global hash_prov data is * When compiled with GIT_THREADS, the global hash_prov data is
...@@ -303,27 +303,30 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx) ...@@ -303,27 +303,30 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
int git_hash_sha1_init(git_hash_sha1_ctx *ctx) int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
{ {
assert(ctx && ctx->type); GIT_ASSERT_ARG(ctx);
GIT_ASSERT_ARG(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_sha1_update(git_hash_sha1_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); GIT_ASSERT_ARG(ctx);
GIT_ASSERT_ARG(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_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx) int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
{ {
assert(ctx && ctx->type); GIT_ASSERT_ARG(ctx);
GIT_ASSERT_ARG(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_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx) void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
{ {
assert(ctx); if (!ctx)
return;
if (ctx->type == CNG) else if (ctx->type == CNG)
hash_ctx_cng_cleanup(ctx); hash_ctx_cng_cleanup(ctx);
else if(ctx->type == CRYPTOAPI) else if(ctx->type == CRYPTOAPI)
hash_ctx_cryptoapi_cleanup(ctx); hash_ctx_cryptoapi_cleanup(ctx);
......
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