]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - hash.h
The fifth batch
[thirdparty/git.git] / hash.h
... / ...
CommitLineData
1#ifndef HASH_H
2#define HASH_H
3
4#if defined(SHA1_APPLE)
5#define SHA1_BACKEND "SHA1_APPLE (No collision detection)"
6#include <CommonCrypto/CommonDigest.h>
7#elif defined(SHA1_OPENSSL)
8# define SHA1_BACKEND "SHA1_OPENSSL (No collision detection)"
9# include <openssl/sha.h>
10# if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
11# define SHA1_NEEDS_CLONE_HELPER
12# include "sha1/openssl.h"
13# endif
14#elif defined(SHA1_DC)
15#define SHA1_BACKEND "SHA1_DC"
16#include "sha1dc_git.h"
17#else /* SHA1_BLK */
18#define SHA1_BACKEND "SHA1_BLK (No collision detection)"
19#include "block-sha1/sha1.h"
20#endif
21
22#if defined(SHA1_APPLE_UNSAFE)
23# define SHA1_UNSAFE_BACKEND "SHA1_APPLE_UNSAFE"
24# include <CommonCrypto/CommonDigest.h>
25# define platform_SHA_CTX_unsafe CC_SHA1_CTX
26# define platform_SHA1_Init_unsafe CC_SHA1_Init
27# define platform_SHA1_Update_unsafe CC_SHA1_Update
28# define platform_SHA1_Final_unsafe CC_SHA1_Final
29#elif defined(SHA1_OPENSSL_UNSAFE)
30# define SHA1_UNSAFE_BACKEND "SHA1_OPENSSL_UNSAFE"
31# include <openssl/sha.h>
32# if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
33# define SHA1_NEEDS_CLONE_HELPER_UNSAFE
34# include "sha1/openssl.h"
35# define platform_SHA_CTX_unsafe openssl_SHA1_CTX
36# define platform_SHA1_Init_unsafe openssl_SHA1_Init
37# define platform_SHA1_Clone_unsafe openssl_SHA1_Clone
38# define platform_SHA1_Update_unsafe openssl_SHA1_Update
39# define platform_SHA1_Final_unsafe openssl_SHA1_Final
40# else
41# define platform_SHA_CTX_unsafe SHA_CTX
42# define platform_SHA1_Init_unsafe SHA1_Init
43# define platform_SHA1_Update_unsafe SHA1_Update
44# define platform_SHA1_Final_unsafe SHA1_Final
45# endif
46#elif defined(SHA1_BLK_UNSAFE)
47# define SHA1_UNSAFE_BACKEND "SHA1_BLK_UNSAFE"
48# include "block-sha1/sha1.h"
49# define platform_SHA_CTX_unsafe blk_SHA_CTX
50# define platform_SHA1_Init_unsafe blk_SHA1_Init
51# define platform_SHA1_Update_unsafe blk_SHA1_Update
52# define platform_SHA1_Final_unsafe blk_SHA1_Final
53#endif
54
55#if defined(SHA256_NETTLE)
56#define SHA256_BACKEND "SHA256_NETTLE"
57#include "sha256/nettle.h"
58#elif defined(SHA256_GCRYPT)
59#define SHA256_BACKEND "SHA256_GCRYPT"
60#define SHA256_NEEDS_CLONE_HELPER
61#include "sha256/gcrypt.h"
62#elif defined(SHA256_OPENSSL)
63# define SHA256_BACKEND "SHA256_OPENSSL"
64# include <openssl/sha.h>
65# if defined(OPENSSL_API_LEVEL) && OPENSSL_API_LEVEL >= 3
66# define SHA256_NEEDS_CLONE_HELPER
67# include "sha256/openssl.h"
68# endif
69#else
70#define SHA256_BACKEND "SHA256_BLK"
71#include "sha256/block/sha256.h"
72#endif
73
74#ifndef platform_SHA_CTX
75/*
76 * platform's underlying implementation of SHA-1; could be OpenSSL,
77 * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
78 * SHA-1 header may have already defined platform_SHA_CTX for our
79 * own implementations like block-sha1, so we list
80 * the default for OpenSSL compatible SHA-1 implementations here.
81 */
82#define platform_SHA_CTX SHA_CTX
83#define platform_SHA1_Init SHA1_Init
84#define platform_SHA1_Update SHA1_Update
85#define platform_SHA1_Final SHA1_Final
86#endif
87
88#ifndef platform_SHA_CTX_unsafe
89# define platform_SHA_CTX_unsafe platform_SHA_CTX
90# define platform_SHA1_Init_unsafe platform_SHA1_Init
91# define platform_SHA1_Update_unsafe platform_SHA1_Update
92# define platform_SHA1_Final_unsafe platform_SHA1_Final
93# ifdef platform_SHA1_Clone
94# define platform_SHA1_Clone_unsafe platform_SHA1_Clone
95# endif
96# ifdef SHA1_NEEDS_CLONE_HELPER
97# define SHA1_NEEDS_CLONE_HELPER_UNSAFE
98# endif
99#endif
100
101#define git_SHA_CTX platform_SHA_CTX
102#define git_SHA1_Init platform_SHA1_Init
103#define git_SHA1_Update platform_SHA1_Update
104#define git_SHA1_Final platform_SHA1_Final
105
106#define git_SHA_CTX_unsafe platform_SHA_CTX_unsafe
107#define git_SHA1_Init_unsafe platform_SHA1_Init_unsafe
108#define git_SHA1_Update_unsafe platform_SHA1_Update_unsafe
109#define git_SHA1_Final_unsafe platform_SHA1_Final_unsafe
110
111#ifdef platform_SHA1_Clone
112#define git_SHA1_Clone platform_SHA1_Clone
113#endif
114#ifdef platform_SHA1_Clone_unsafe
115# define git_SHA1_Clone_unsafe platform_SHA1_Clone_unsafe
116#endif
117
118#ifndef platform_SHA256_CTX
119#define platform_SHA256_CTX SHA256_CTX
120#define platform_SHA256_Init SHA256_Init
121#define platform_SHA256_Update SHA256_Update
122#define platform_SHA256_Final SHA256_Final
123#endif
124
125#define git_SHA256_CTX platform_SHA256_CTX
126#define git_SHA256_Init platform_SHA256_Init
127#define git_SHA256_Update platform_SHA256_Update
128#define git_SHA256_Final platform_SHA256_Final
129
130#ifdef platform_SHA256_Clone
131#define git_SHA256_Clone platform_SHA256_Clone
132#endif
133
134#ifdef SHA1_MAX_BLOCK_SIZE
135#include "compat/sha1-chunked.h"
136#undef git_SHA1_Update
137#define git_SHA1_Update git_SHA1_Update_Chunked
138#endif
139
140#ifndef SHA1_NEEDS_CLONE_HELPER
141static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src)
142{
143 memcpy(dst, src, sizeof(*dst));
144}
145#endif
146#ifndef SHA1_NEEDS_CLONE_HELPER_UNSAFE
147static inline void git_SHA1_Clone_unsafe(git_SHA_CTX_unsafe *dst,
148 const git_SHA_CTX_unsafe *src)
149{
150 memcpy(dst, src, sizeof(*dst));
151}
152#endif
153
154#ifndef SHA256_NEEDS_CLONE_HELPER
155static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src)
156{
157 memcpy(dst, src, sizeof(*dst));
158}
159#endif
160
161/*
162 * Note that these constants are suitable for indexing the hash_algos array and
163 * comparing against each other, but are otherwise arbitrary, so they should not
164 * be exposed to the user or serialized to disk. To know whether a
165 * git_hash_algo struct points to some usable hash function, test the format_id
166 * field for being non-zero. Use the name field for user-visible situations and
167 * the format_id field for fixed-length fields on disk.
168 */
169/* An unknown hash function. */
170#define GIT_HASH_UNKNOWN 0
171/* SHA-1 */
172#define GIT_HASH_SHA1 1
173/* SHA-256 */
174#define GIT_HASH_SHA256 2
175/* Number of algorithms supported (including unknown). */
176#define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
177
178/* "sha1", big-endian */
179#define GIT_SHA1_FORMAT_ID 0x73686131
180
181/* The length in bytes and in hex digits of an object name (SHA-1 value). */
182#define GIT_SHA1_RAWSZ 20
183#define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
184/* The block size of SHA-1. */
185#define GIT_SHA1_BLKSZ 64
186
187/* "s256", big-endian */
188#define GIT_SHA256_FORMAT_ID 0x73323536
189
190/* The length in bytes and in hex digits of an object name (SHA-256 value). */
191#define GIT_SHA256_RAWSZ 32
192#define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
193/* The block size of SHA-256. */
194#define GIT_SHA256_BLKSZ 64
195
196/* The length in byte and in hex digits of the largest possible hash value. */
197#define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
198#define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
199/* The largest possible block size for any supported hash. */
200#define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
201
202struct object_id {
203 unsigned char hash[GIT_MAX_RAWSZ];
204 int algo; /* XXX requires 4-byte alignment */
205};
206
207#define GET_OID_QUIETLY 01
208#define GET_OID_COMMIT 02
209#define GET_OID_COMMITTISH 04
210#define GET_OID_TREE 010
211#define GET_OID_TREEISH 020
212#define GET_OID_BLOB 040
213#define GET_OID_FOLLOW_SYMLINKS 0100
214#define GET_OID_RECORD_PATH 0200
215#define GET_OID_ONLY_TO_DIE 04000
216#define GET_OID_REQUIRE_PATH 010000
217#define GET_OID_HASH_ANY 020000
218#define GET_OID_SKIP_AMBIGUITY_CHECK 040000
219#define GET_OID_GENTLY 0100000
220
221#define GET_OID_DISAMBIGUATORS \
222 (GET_OID_COMMIT | GET_OID_COMMITTISH | \
223 GET_OID_TREE | GET_OID_TREEISH | \
224 GET_OID_BLOB)
225
226enum get_oid_result {
227 FOUND = 0,
228 MISSING_OBJECT = -1, /* The requested object is missing */
229 SHORT_NAME_AMBIGUOUS = -2,
230 /* The following only apply when symlinks are followed */
231 DANGLING_SYMLINK = -4, /*
232 * The initial symlink is there, but
233 * (transitively) points to a missing
234 * in-tree file
235 */
236 SYMLINK_LOOP = -5,
237 NOT_DIR = -6, /*
238 * Somewhere along the symlink chain, a path is
239 * requested which contains a file as a
240 * non-final element.
241 */
242};
243
244#ifdef USE_THE_REPOSITORY_VARIABLE
245# include "repository.h"
246# define the_hash_algo the_repository->hash_algo
247#endif
248
249/* A suitably aligned type for stack allocations of hash contexts. */
250struct git_hash_ctx {
251 const struct git_hash_algo *algop;
252 union {
253 git_SHA_CTX sha1;
254 git_SHA_CTX_unsafe sha1_unsafe;
255 git_SHA256_CTX sha256;
256 } state;
257};
258
259typedef void (*git_hash_init_fn)(struct git_hash_ctx *ctx);
260typedef void (*git_hash_clone_fn)(struct git_hash_ctx *dst, const struct git_hash_ctx *src);
261typedef void (*git_hash_update_fn)(struct git_hash_ctx *ctx, const void *in, size_t len);
262typedef void (*git_hash_final_fn)(unsigned char *hash, struct git_hash_ctx *ctx);
263typedef void (*git_hash_final_oid_fn)(struct object_id *oid, struct git_hash_ctx *ctx);
264
265struct git_hash_algo {
266 /*
267 * The name of the algorithm, as appears in the config file and in
268 * messages.
269 */
270 const char *name;
271
272 /* A four-byte version identifier, used in pack indices. */
273 uint32_t format_id;
274
275 /* The length of the hash in binary. */
276 size_t rawsz;
277
278 /* The length of the hash in hex characters. */
279 size_t hexsz;
280
281 /* The block size of the hash. */
282 size_t blksz;
283
284 /* The hash initialization function. */
285 git_hash_init_fn init_fn;
286
287 /* The hash context cloning function. */
288 git_hash_clone_fn clone_fn;
289
290 /* The hash update function. */
291 git_hash_update_fn update_fn;
292
293 /* The hash finalization function. */
294 git_hash_final_fn final_fn;
295
296 /* The hash finalization function for object IDs. */
297 git_hash_final_oid_fn final_oid_fn;
298
299 /* The OID of the empty tree. */
300 const struct object_id *empty_tree;
301
302 /* The OID of the empty blob. */
303 const struct object_id *empty_blob;
304
305 /* The all-zeros OID. */
306 const struct object_id *null_oid;
307
308 /* The unsafe variant of this hash function, if one exists. */
309 const struct git_hash_algo *unsafe;
310};
311extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
312
313static inline void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src)
314{
315 src->algop->clone_fn(dst, src);
316}
317
318static inline void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len)
319{
320 ctx->algop->update_fn(ctx, in, len);
321}
322
323static inline void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx)
324{
325 ctx->algop->final_fn(hash, ctx);
326}
327
328static inline void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx)
329{
330 ctx->algop->final_oid_fn(oid, ctx);
331}
332
333/*
334 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
335 * the name doesn't match a known algorithm.
336 */
337int hash_algo_by_name(const char *name);
338/* Identical, except based on the format ID. */
339int hash_algo_by_id(uint32_t format_id);
340/* Identical, except based on the length. */
341int hash_algo_by_length(size_t len);
342/* Identical, except for a pointer to struct git_hash_algo. */
343static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
344{
345 size_t i;
346 for (i = 0; i < GIT_HASH_NALGOS; i++) {
347 const struct git_hash_algo *algop = &hash_algos[i];
348 if (p == algop)
349 return i;
350 }
351 return GIT_HASH_UNKNOWN;
352}
353
354const struct git_hash_algo *unsafe_hash_algo(const struct git_hash_algo *algop);
355
356const struct object_id *null_oid(const struct git_hash_algo *algop);
357
358static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
359{
360 /*
361 * Teach the compiler that there are only two possibilities of hash size
362 * here, so that it can optimize for this case as much as possible.
363 */
364 if (algop->rawsz == GIT_MAX_RAWSZ)
365 return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
366 return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
367}
368
369static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
370{
371 /*
372 * We write this here instead of deferring to hashcmp so that the
373 * compiler can properly inline it and avoid calling memcmp.
374 */
375 if (algop->rawsz == GIT_MAX_RAWSZ)
376 return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
377 return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
378}
379
380static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src,
381 const struct git_hash_algo *algop)
382{
383 memcpy(sha_dst, sha_src, algop->rawsz);
384}
385
386static inline void hashclr(unsigned char *hash, const struct git_hash_algo *algop)
387{
388 memset(hash, 0, algop->rawsz);
389}
390
391static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
392{
393 return memcmp(oid1->hash, oid2->hash, GIT_MAX_RAWSZ);
394}
395
396static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
397{
398 return !memcmp(oid1->hash, oid2->hash, GIT_MAX_RAWSZ);
399}
400
401static inline void oidcpy(struct object_id *dst, const struct object_id *src)
402{
403 memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
404 dst->algo = src->algo;
405}
406
407static inline void oidread(struct object_id *oid, const unsigned char *hash,
408 const struct git_hash_algo *algop)
409{
410 memcpy(oid->hash, hash, algop->rawsz);
411 if (algop->rawsz < GIT_MAX_RAWSZ)
412 memset(oid->hash + algop->rawsz, 0, GIT_MAX_RAWSZ - algop->rawsz);
413 oid->algo = hash_algo_by_ptr(algop);
414}
415
416static inline void oidclr(struct object_id *oid,
417 const struct git_hash_algo *algop)
418{
419 memset(oid->hash, 0, GIT_MAX_RAWSZ);
420 oid->algo = hash_algo_by_ptr(algop);
421}
422
423static inline struct object_id *oiddup(const struct object_id *src)
424{
425 struct object_id *dst = xmalloc(sizeof(struct object_id));
426 oidcpy(dst, src);
427 return dst;
428}
429
430static inline void oid_set_algo(struct object_id *oid, const struct git_hash_algo *algop)
431{
432 oid->algo = hash_algo_by_ptr(algop);
433}
434
435/*
436 * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
437 * for use in hash tables. Cryptographic hashes are supposed to have
438 * uniform distribution, so in contrast to `memhash()`, this just copies
439 * the first `sizeof(int)` bytes without shuffling any bits. Note that
440 * the results will be different on big-endian and little-endian
441 * platforms, so they should not be stored or transferred over the net.
442 */
443static inline unsigned int oidhash(const struct object_id *oid)
444{
445 /*
446 * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
447 * platforms that don't support unaligned reads.
448 */
449 unsigned int hash;
450 memcpy(&hash, oid->hash, sizeof(hash));
451 return hash;
452}
453
454static inline int is_null_oid(const struct object_id *oid)
455{
456 static const unsigned char null_hash[GIT_MAX_RAWSZ];
457 return !memcmp(oid->hash, null_hash, GIT_MAX_RAWSZ);
458}
459
460const char *empty_tree_oid_hex(const struct git_hash_algo *algop);
461
462static inline int is_empty_blob_oid(const struct object_id *oid,
463 const struct git_hash_algo *algop)
464{
465 return oideq(oid, algop->empty_blob);
466}
467
468static inline int is_empty_tree_oid(const struct object_id *oid,
469 const struct git_hash_algo *algop)
470{
471 return oideq(oid, algop->empty_tree);
472}
473
474#endif