blob: 27a180248f96fd4cf394b059ed92b02bcaeebc7b [file] [log] [blame]
brian m. carlsonf18f8162017-03-11 22:28:18 +00001#ifndef HASH_H
2#define HASH_H
3
brian m. carlsonf50e7662017-11-12 21:28:52 +00004#include "git-compat-util.h"
Jeff King3fa6f2a2020-12-04 13:51:39 -05005#include "repository.h"
brian m. carlsonf50e7662017-11-12 21:28:52 +00006
brian m. carlsonf18f8162017-03-11 22:28:18 +00007#if defined(SHA1_PPC)
8#include "ppc/sha1.h"
9#elif defined(SHA1_APPLE)
10#include <CommonCrypto/CommonDigest.h>
11#elif defined(SHA1_OPENSSL)
12#include <openssl/sha.h>
Jeff King8325e432017-03-16 18:09:12 -040013#elif defined(SHA1_DC)
Takashi Iwai36f048c2017-08-15 14:04:16 +020014#include "sha1dc_git.h"
brian m. carlsonf18f8162017-03-11 22:28:18 +000015#else /* SHA1_BLK */
16#include "block-sha1/sha1.h"
17#endif
18
brian m. carlson27dc04c2018-11-14 04:09:37 +000019#if defined(SHA256_GCRYPT)
brian m. carlson768e30e2020-02-22 20:17:27 +000020#define SHA256_NEEDS_CLONE_HELPER
brian m. carlson27dc04c2018-11-14 04:09:37 +000021#include "sha256/gcrypt.h"
brian m. carlson4b4e2912018-11-14 04:09:38 +000022#elif defined(SHA256_OPENSSL)
23#include <openssl/sha.h>
brian m. carlson27dc04c2018-11-14 04:09:37 +000024#else
brian m. carlson13eeedb2018-11-14 04:09:36 +000025#include "sha256/block/sha256.h"
brian m. carlson27dc04c2018-11-14 04:09:37 +000026#endif
brian m. carlson13eeedb2018-11-14 04:09:36 +000027
brian m. carlson164e7162018-02-01 02:18:37 +000028#ifndef platform_SHA_CTX
29/*
30 * platform's underlying implementation of SHA-1; could be OpenSSL,
brian m. carlsonb212c0c2018-02-08 02:48:58 +000031 * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
32 * SHA-1 header may have already defined platform_SHA_CTX for our
brian m. carlson164e7162018-02-01 02:18:37 +000033 * own implementations like block-sha1 and ppc-sha1, so we list
34 * the default for OpenSSL compatible SHA-1 implementations here.
35 */
36#define platform_SHA_CTX SHA_CTX
37#define platform_SHA1_Init SHA1_Init
38#define platform_SHA1_Update SHA1_Update
39#define platform_SHA1_Final SHA1_Final
40#endif
41
42#define git_SHA_CTX platform_SHA_CTX
43#define git_SHA1_Init platform_SHA1_Init
44#define git_SHA1_Update platform_SHA1_Update
45#define git_SHA1_Final platform_SHA1_Final
46
brian m. carlson13eeedb2018-11-14 04:09:36 +000047#ifndef platform_SHA256_CTX
48#define platform_SHA256_CTX SHA256_CTX
49#define platform_SHA256_Init SHA256_Init
50#define platform_SHA256_Update SHA256_Update
51#define platform_SHA256_Final SHA256_Final
52#endif
53
54#define git_SHA256_CTX platform_SHA256_CTX
55#define git_SHA256_Init platform_SHA256_Init
56#define git_SHA256_Update platform_SHA256_Update
57#define git_SHA256_Final platform_SHA256_Final
58
brian m. carlson768e30e2020-02-22 20:17:27 +000059#ifdef platform_SHA256_Clone
60#define git_SHA256_Clone platform_SHA256_Clone
61#endif
62
brian m. carlson164e7162018-02-01 02:18:37 +000063#ifdef SHA1_MAX_BLOCK_SIZE
64#include "compat/sha1-chunked.h"
65#undef git_SHA1_Update
66#define git_SHA1_Update git_SHA1_Update_Chunked
67#endif
68
brian m. carlson768e30e2020-02-22 20:17:27 +000069static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src)
70{
71 memcpy(dst, src, sizeof(*dst));
72}
73
74#ifndef SHA256_NEEDS_CLONE_HELPER
75static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src)
76{
77 memcpy(dst, src, sizeof(*dst));
78}
79#endif
80
brian m. carlsonf50e7662017-11-12 21:28:52 +000081/*
82 * Note that these constants are suitable for indexing the hash_algos array and
83 * comparing against each other, but are otherwise arbitrary, so they should not
84 * be exposed to the user or serialized to disk. To know whether a
85 * git_hash_algo struct points to some usable hash function, test the format_id
86 * field for being non-zero. Use the name field for user-visible situations and
87 * the format_id field for fixed-length fields on disk.
88 */
89/* An unknown hash function. */
90#define GIT_HASH_UNKNOWN 0
91/* SHA-1 */
92#define GIT_HASH_SHA1 1
brian m. carlson13eeedb2018-11-14 04:09:36 +000093/* SHA-256 */
94#define GIT_HASH_SHA256 2
brian m. carlsonf50e7662017-11-12 21:28:52 +000095/* Number of algorithms supported (including unknown). */
brian m. carlson13eeedb2018-11-14 04:09:36 +000096#define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
brian m. carlsonf50e7662017-11-12 21:28:52 +000097
brian m. carlsonab795f02021-04-26 01:02:52 +000098/* The length in bytes and in hex digits of an object name (SHA-1 value). */
99#define GIT_SHA1_RAWSZ 20
100#define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
101/* The block size of SHA-1. */
102#define GIT_SHA1_BLKSZ 64
103
104/* The length in bytes and in hex digits of an object name (SHA-256 value). */
105#define GIT_SHA256_RAWSZ 32
106#define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
107/* The block size of SHA-256. */
108#define GIT_SHA256_BLKSZ 64
109
110/* The length in byte and in hex digits of the largest possible hash value. */
111#define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
112#define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
113/* The largest possible block size for any supported hash. */
114#define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
115
116struct object_id {
117 unsigned char hash[GIT_MAX_RAWSZ];
118 int algo;
119};
120
brian m. carlsonac73ced2018-02-01 02:18:38 +0000121/* A suitably aligned type for stack allocations of hash contexts. */
122union git_hash_ctx {
123 git_SHA_CTX sha1;
brian m. carlson13eeedb2018-11-14 04:09:36 +0000124 git_SHA256_CTX sha256;
brian m. carlsonac73ced2018-02-01 02:18:38 +0000125};
126typedef union git_hash_ctx git_hash_ctx;
127
128typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
brian m. carlson768e30e2020-02-22 20:17:27 +0000129typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
brian m. carlsonac73ced2018-02-01 02:18:38 +0000130typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
131typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
brian m. carlsonab795f02021-04-26 01:02:52 +0000132typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx);
brian m. carlsonf50e7662017-11-12 21:28:52 +0000133
134struct git_hash_algo {
135 /*
136 * The name of the algorithm, as appears in the config file and in
137 * messages.
138 */
139 const char *name;
140
141 /* A four-byte version identifier, used in pack indices. */
142 uint32_t format_id;
143
brian m. carlsonf50e7662017-11-12 21:28:52 +0000144 /* The length of the hash in binary. */
145 size_t rawsz;
146
147 /* The length of the hash in hex characters. */
148 size_t hexsz;
149
brian m. carlsona2ce0a72018-11-14 04:09:33 +0000150 /* The block size of the hash. */
151 size_t blksz;
152
brian m. carlsonf50e7662017-11-12 21:28:52 +0000153 /* The hash initialization function. */
154 git_hash_init_fn init_fn;
155
brian m. carlson768e30e2020-02-22 20:17:27 +0000156 /* The hash context cloning function. */
157 git_hash_clone_fn clone_fn;
158
brian m. carlsonf50e7662017-11-12 21:28:52 +0000159 /* The hash update function. */
160 git_hash_update_fn update_fn;
161
162 /* The hash finalization function. */
163 git_hash_final_fn final_fn;
164
brian m. carlsonab795f02021-04-26 01:02:52 +0000165 /* The hash finalization function for object IDs. */
166 git_hash_final_oid_fn final_oid_fn;
167
brian m. carlsonf50e7662017-11-12 21:28:52 +0000168 /* The OID of the empty tree. */
169 const struct object_id *empty_tree;
170
171 /* The OID of the empty blob. */
172 const struct object_id *empty_blob;
brian m. carlson14228442021-04-26 01:02:56 +0000173
174 /* The all-zeros OID. */
175 const struct object_id *null_oid;
brian m. carlsonf50e7662017-11-12 21:28:52 +0000176};
177extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
178
brian m. carlson2f90b9d2018-10-22 02:43:32 +0000179/*
180 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
181 * the name doesn't match a known algorithm.
182 */
183int hash_algo_by_name(const char *name);
184/* Identical, except based on the format ID. */
185int hash_algo_by_id(uint32_t format_id);
brian m. carlson95399782019-02-19 00:05:17 +0000186/* Identical, except based on the length. */
187int hash_algo_by_length(int len);
brian m. carlson2f90b9d2018-10-22 02:43:32 +0000188/* Identical, except for a pointer to struct git_hash_algo. */
189static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
190{
191 return p - hash_algos;
192}
193
Jeff Kingc0566d72019-06-20 03:41:45 -0400194#define the_hash_algo the_repository->hash_algo
195
brian m. carlson14228442021-04-26 01:02:56 +0000196const struct object_id *null_oid(void);
Jeff King3fa6f2a2020-12-04 13:51:39 -0500197
brian m. carlson5a6dce72021-04-26 01:02:55 +0000198static inline int hashcmp_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
Jeff King3fa6f2a2020-12-04 13:51:39 -0500199{
200 /*
201 * Teach the compiler that there are only two possibilities of hash size
202 * here, so that it can optimize for this case as much as possible.
203 */
brian m. carlson5a6dce72021-04-26 01:02:55 +0000204 if (algop->rawsz == GIT_MAX_RAWSZ)
Jeff King3fa6f2a2020-12-04 13:51:39 -0500205 return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
206 return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
207}
208
brian m. carlson5a6dce72021-04-26 01:02:55 +0000209static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
Jeff King3fa6f2a2020-12-04 13:51:39 -0500210{
brian m. carlson5a6dce72021-04-26 01:02:55 +0000211 return hashcmp_algop(sha1, sha2, the_hash_algo);
Jeff King3fa6f2a2020-12-04 13:51:39 -0500212}
213
brian m. carlson5a6dce72021-04-26 01:02:55 +0000214static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
215{
216 const struct git_hash_algo *algop;
217 if (!oid1->algo)
218 algop = the_hash_algo;
219 else
220 algop = &hash_algos[oid1->algo];
221 return hashcmp_algop(oid1->hash, oid2->hash, algop);
222}
223
224static inline int hasheq_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
Jeff King3fa6f2a2020-12-04 13:51:39 -0500225{
226 /*
227 * We write this here instead of deferring to hashcmp so that the
228 * compiler can properly inline it and avoid calling memcmp.
229 */
brian m. carlson5a6dce72021-04-26 01:02:55 +0000230 if (algop->rawsz == GIT_MAX_RAWSZ)
Jeff King3fa6f2a2020-12-04 13:51:39 -0500231 return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
232 return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
233}
234
brian m. carlson5a6dce72021-04-26 01:02:55 +0000235static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
236{
237 return hasheq_algop(sha1, sha2, the_hash_algo);
238}
239
Jeff King3fa6f2a2020-12-04 13:51:39 -0500240static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
241{
brian m. carlson5a6dce72021-04-26 01:02:55 +0000242 const struct git_hash_algo *algop;
243 if (!oid1->algo)
244 algop = the_hash_algo;
245 else
246 algop = &hash_algos[oid1->algo];
247 return hasheq_algop(oid1->hash, oid2->hash, algop);
Jeff King3fa6f2a2020-12-04 13:51:39 -0500248}
249
250static inline int is_null_oid(const struct object_id *oid)
251{
brian m. carlson14228442021-04-26 01:02:56 +0000252 return oideq(oid, null_oid());
Jeff King3fa6f2a2020-12-04 13:51:39 -0500253}
254
255static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
256{
257 memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
258}
259
260static inline void oidcpy(struct object_id *dst, const struct object_id *src)
261{
262 memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000263 dst->algo = src->algo;
Jeff King3fa6f2a2020-12-04 13:51:39 -0500264}
265
Matheus Tavares3d20ed22021-05-17 16:49:03 -0300266/* Like oidcpy() but zero-pads the unused bytes in dst's hash array. */
267static inline void oidcpy_with_padding(struct object_id *dst,
Eric Wong90e07f02021-07-07 23:10:18 +0000268 const struct object_id *src)
Matheus Tavares3d20ed22021-05-17 16:49:03 -0300269{
270 size_t hashsz;
271
272 if (!src->algo)
273 hashsz = the_hash_algo->rawsz;
274 else
275 hashsz = hash_algos[src->algo].rawsz;
276
277 memcpy(dst->hash, src->hash, hashsz);
278 memset(dst->hash + hashsz, 0, GIT_MAX_RAWSZ - hashsz);
279 dst->algo = src->algo;
280}
281
Jeff King3fa6f2a2020-12-04 13:51:39 -0500282static inline struct object_id *oiddup(const struct object_id *src)
283{
284 struct object_id *dst = xmalloc(sizeof(struct object_id));
285 oidcpy(dst, src);
286 return dst;
287}
288
289static inline void hashclr(unsigned char *hash)
290{
291 memset(hash, 0, the_hash_algo->rawsz);
292}
293
294static inline void oidclr(struct object_id *oid)
295{
296 memset(oid->hash, 0, GIT_MAX_RAWSZ);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000297 oid->algo = hash_algo_by_ptr(the_hash_algo);
Jeff King3fa6f2a2020-12-04 13:51:39 -0500298}
299
300static inline void oidread(struct object_id *oid, const unsigned char *hash)
301{
302 memcpy(oid->hash, hash, the_hash_algo->rawsz);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000303 oid->algo = hash_algo_by_ptr(the_hash_algo);
Jeff King3fa6f2a2020-12-04 13:51:39 -0500304}
305
306static inline int is_empty_blob_sha1(const unsigned char *sha1)
307{
308 return hasheq(sha1, the_hash_algo->empty_blob->hash);
309}
310
311static inline int is_empty_blob_oid(const struct object_id *oid)
312{
313 return oideq(oid, the_hash_algo->empty_blob);
314}
315
316static inline int is_empty_tree_sha1(const unsigned char *sha1)
317{
318 return hasheq(sha1, the_hash_algo->empty_tree->hash);
319}
320
321static inline int is_empty_tree_oid(const struct object_id *oid)
322{
323 return oideq(oid, the_hash_algo->empty_tree);
324}
325
brian m. carlson5a6dce72021-04-26 01:02:55 +0000326static inline void oid_set_algo(struct object_id *oid, const struct git_hash_algo *algop)
327{
328 oid->algo = hash_algo_by_ptr(algop);
329}
330
Jeff King3fa6f2a2020-12-04 13:51:39 -0500331const char *empty_tree_oid_hex(void);
332const char *empty_blob_oid_hex(void);
333
brian m. carlsonf18f8162017-03-11 22:28:18 +0000334#endif