Blame src/hash/hash_collisiondetect.h

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#ifndef INCLUDE_hash_collisiondetect_h__
Packit ae9e2a
#define INCLUDE_hash_collisiondetect_h__
Packit ae9e2a
Packit ae9e2a
#include "hash.h"
Packit ae9e2a
#include "sha1dc/sha1.h"
Packit ae9e2a
Packit ae9e2a
struct git_hash_ctx {
Packit ae9e2a
	SHA1_CTX c;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
#define git_hash_global_init() 0
Packit ae9e2a
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
Packit ae9e2a
#define git_hash_ctx_cleanup(ctx)
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
Packit ae9e2a
{
Packit ae9e2a
	assert(ctx);
Packit ae9e2a
	SHA1DCInit(&ctx->c);
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
Packit ae9e2a
{
Packit ae9e2a
	assert(ctx);
Packit ae9e2a
	SHA1DCUpdate(&ctx->c, data, len);
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
Packit ae9e2a
{
Packit ae9e2a
	assert(ctx);
Packit ae9e2a
	if (SHA1DCFinal(out->id, &ctx->c)) {
Packit ae9e2a
		giterr_set(GITERR_SHA1, "SHA1 collision attack detected");
Packit ae9e2a
		return -1;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
#endif /* INCLUDE_hash_collisiondetect_h__ */