1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #ifndef INCLUDE_hash_hash_collisiondetect_h__
9 #define INCLUDE_hash_hash_collisiondetect_h__
10
11 #include "hash.h"
12 #include "sha1dc/sha1.h"
13
14 struct git_hash_ctx {
15 SHA1_CTX c;
16 };
17
18 #define git_hash_global_init() 0
19 #define git_hash_ctx_init(ctx) git_hash_init(ctx)
20 #define git_hash_ctx_cleanup(ctx)
21
git_hash_init(git_hash_ctx * ctx)22 GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
23 {
24 assert(ctx);
25 SHA1DCInit(&ctx->c);
26 return 0;
27 }
28
git_hash_update(git_hash_ctx * ctx,const void * data,size_t len)29 GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
30 {
31 assert(ctx);
32 SHA1DCUpdate(&ctx->c, data, len);
33 return 0;
34 }
35
git_hash_final(git_oid * out,git_hash_ctx * ctx)36 GIT_INLINE(int) git_hash_final(git_oid *out, git_hash_ctx *ctx)
37 {
38 assert(ctx);
39 if (SHA1DCFinal(out->id, &ctx->c)) {
40 giterr_set(GITERR_SHA1, "SHA1 collision attack detected");
41 return -1;
42 }
43
44 return 0;
45 }
46
47 #endif
48