]> git.proxmox.com Git - libgit2.git/blob - src/hash/hash_collisiondetect.h
Merge pull request #4175 from libgit2/ethomson/dont_trunc_and_excl
[libgit2.git] / src / hash / hash_collisiondetect.h
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_collisiondetect_h__
9 #define INCLUDE_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
22 GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
23 {
24 assert(ctx);
25 SHA1DCInit(&ctx->c);
26 return 0;
27 }
28
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
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 /* INCLUDE_hash_collisiondetect_h__ */