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"
14 #elif defined(SHA1_DC)
15 #define SHA1_BACKEND "SHA1_DC"
16 #include "sha1dc_git.h"
18 #define SHA1_BACKEND "SHA1_BLK (No collision detection)"
19 #include "block-sha1/sha1.h"
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
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
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
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"
70 #define SHA256_BACKEND "SHA256_BLK"
71 #include "sha256/block/sha256.h"
74 #ifndef platform_SHA_CTX
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.
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
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
96 # ifdef SHA1_NEEDS_CLONE_HELPER
97 # define SHA1_NEEDS_CLONE_HELPER_UNSAFE
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
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
111 #ifdef platform_SHA1_Clone
112 #define git_SHA1_Clone platform_SHA1_Clone
114 #ifdef platform_SHA1_Clone_unsafe
115 # define git_SHA1_Clone_unsafe platform_SHA1_Clone_unsafe
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
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
130 #ifdef platform_SHA256_Clone
131 #define git_SHA256_Clone platform_SHA256_Clone
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
140 #ifndef SHA1_NEEDS_CLONE_HELPER
141 static inline void git_SHA1_Clone(git_SHA_CTX
*dst
, const git_SHA_CTX
*src
)
143 memcpy(dst
, src
, sizeof(*dst
));
146 #ifndef SHA1_NEEDS_CLONE_HELPER_UNSAFE
147 static inline void git_SHA1_Clone_unsafe(git_SHA_CTX_unsafe
*dst
,
148 const git_SHA_CTX_unsafe
*src
)
150 memcpy(dst
, src
, sizeof(*dst
));
154 #ifndef SHA256_NEEDS_CLONE_HELPER
155 static inline void git_SHA256_Clone(git_SHA256_CTX
*dst
, const git_SHA256_CTX
*src
)
157 memcpy(dst
, src
, sizeof(*dst
));
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.
169 /* An unknown hash function. */
170 #define GIT_HASH_UNKNOWN 0
172 #define GIT_HASH_SHA1 1
174 #define GIT_HASH_SHA256 2
175 /* Number of algorithms supported (including unknown). */
176 #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
178 /* Default hash algorithm if unspecified. */
179 #ifdef WITH_BREAKING_CHANGES
180 # define GIT_HASH_DEFAULT GIT_HASH_SHA256
182 # define GIT_HASH_DEFAULT GIT_HASH_SHA1
185 /* Legacy hash algorithm. Implied for older data formats which don't specify. */
186 #define GIT_HASH_SHA1_LEGACY GIT_HASH_SHA1
188 /* "sha1", big-endian */
189 #define GIT_SHA1_FORMAT_ID 0x73686131
191 /* The length in bytes and in hex digits of an object name (SHA-1 value). */
192 #define GIT_SHA1_RAWSZ 20
193 #define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
194 /* The block size of SHA-1. */
195 #define GIT_SHA1_BLKSZ 64
197 /* "s256", big-endian */
198 #define GIT_SHA256_FORMAT_ID 0x73323536
200 /* The length in bytes and in hex digits of an object name (SHA-256 value). */
201 #define GIT_SHA256_RAWSZ 32
202 #define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
203 /* The block size of SHA-256. */
204 #define GIT_SHA256_BLKSZ 64
206 /* The length in byte and in hex digits of the largest possible hash value. */
207 #define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
208 #define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
209 /* The largest possible block size for any supported hash. */
210 #define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
213 unsigned char hash
[GIT_MAX_RAWSZ
];
214 int algo
; /* XXX requires 4-byte alignment */
217 #define GET_OID_QUIETLY 01
218 #define GET_OID_COMMIT 02
219 #define GET_OID_COMMITTISH 04
220 #define GET_OID_TREE 010
221 #define GET_OID_TREEISH 020
222 #define GET_OID_BLOB 040
223 #define GET_OID_FOLLOW_SYMLINKS 0100
224 #define GET_OID_RECORD_PATH 0200
225 #define GET_OID_ONLY_TO_DIE 04000
226 #define GET_OID_REQUIRE_PATH 010000
227 #define GET_OID_HASH_ANY 020000
228 #define GET_OID_SKIP_AMBIGUITY_CHECK 040000
229 #define GET_OID_GENTLY 0100000
231 #define GET_OID_DISAMBIGUATORS \
232 (GET_OID_COMMIT | GET_OID_COMMITTISH | \
233 GET_OID_TREE | GET_OID_TREEISH | \
236 enum get_oid_result
{
238 MISSING_OBJECT
= -1, /* The requested object is missing */
239 SHORT_NAME_AMBIGUOUS
= -2,
240 /* The following only apply when symlinks are followed */
241 DANGLING_SYMLINK
= -4, /*
242 * The initial symlink is there, but
243 * (transitively) points to a missing
248 * Somewhere along the symlink chain, a path is
249 * requested which contains a file as a
254 #ifdef USE_THE_REPOSITORY_VARIABLE
255 # include "repository.h"
256 # define the_hash_algo the_repository->hash_algo
259 /* A suitably aligned type for stack allocations of hash contexts. */
260 struct git_hash_ctx
{
261 const struct git_hash_algo
*algop
;
264 git_SHA_CTX_unsafe sha1_unsafe
;
265 git_SHA256_CTX sha256
;
269 typedef void (*git_hash_init_fn
)(struct git_hash_ctx
*ctx
);
270 typedef void (*git_hash_clone_fn
)(struct git_hash_ctx
*dst
, const struct git_hash_ctx
*src
);
271 typedef void (*git_hash_update_fn
)(struct git_hash_ctx
*ctx
, const void *in
, size_t len
);
272 typedef void (*git_hash_final_fn
)(unsigned char *hash
, struct git_hash_ctx
*ctx
);
273 typedef void (*git_hash_final_oid_fn
)(struct object_id
*oid
, struct git_hash_ctx
*ctx
);
275 struct git_hash_algo
{
277 * The name of the algorithm, as appears in the config file and in
282 /* A four-byte version identifier, used in pack indices. */
285 /* The length of the hash in binary. */
288 /* The length of the hash in hex characters. */
291 /* The block size of the hash. */
294 /* The hash initialization function. */
295 git_hash_init_fn init_fn
;
297 /* The hash context cloning function. */
298 git_hash_clone_fn clone_fn
;
300 /* The hash update function. */
301 git_hash_update_fn update_fn
;
303 /* The hash finalization function. */
304 git_hash_final_fn final_fn
;
306 /* The hash finalization function for object IDs. */
307 git_hash_final_oid_fn final_oid_fn
;
309 /* The OID of the empty tree. */
310 const struct object_id
*empty_tree
;
312 /* The OID of the empty blob. */
313 const struct object_id
*empty_blob
;
315 /* The all-zeros OID. */
316 const struct object_id
*null_oid
;
318 /* The unsafe variant of this hash function, if one exists. */
319 const struct git_hash_algo
*unsafe
;
321 extern const struct git_hash_algo hash_algos
[GIT_HASH_NALGOS
];
323 static inline void git_hash_clone(struct git_hash_ctx
*dst
, const struct git_hash_ctx
*src
)
325 src
->algop
->clone_fn(dst
, src
);
328 static inline void git_hash_update(struct git_hash_ctx
*ctx
, const void *in
, size_t len
)
330 ctx
->algop
->update_fn(ctx
, in
, len
);
333 static inline void git_hash_final(unsigned char *hash
, struct git_hash_ctx
*ctx
)
335 ctx
->algop
->final_fn(hash
, ctx
);
338 static inline void git_hash_final_oid(struct object_id
*oid
, struct git_hash_ctx
*ctx
)
340 ctx
->algop
->final_oid_fn(oid
, ctx
);
344 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
345 * the name doesn't match a known algorithm.
347 int hash_algo_by_name(const char *name
);
348 /* Identical, except based on the format ID. */
349 int hash_algo_by_id(uint32_t format_id
);
350 /* Identical, except based on the length. */
351 int hash_algo_by_length(size_t len
);
352 /* Identical, except for a pointer to struct git_hash_algo. */
353 static inline int hash_algo_by_ptr(const struct git_hash_algo
*p
)
356 for (i
= 0; i
< GIT_HASH_NALGOS
; i
++) {
357 const struct git_hash_algo
*algop
= &hash_algos
[i
];
361 return GIT_HASH_UNKNOWN
;
364 const struct git_hash_algo
*unsafe_hash_algo(const struct git_hash_algo
*algop
);
366 const struct object_id
*null_oid(const struct git_hash_algo
*algop
);
368 static inline int hashcmp(const unsigned char *sha1
, const unsigned char *sha2
, const struct git_hash_algo
*algop
)
371 * Teach the compiler that there are only two possibilities of hash size
372 * here, so that it can optimize for this case as much as possible.
374 if (algop
->rawsz
== GIT_MAX_RAWSZ
)
375 return memcmp(sha1
, sha2
, GIT_MAX_RAWSZ
);
376 return memcmp(sha1
, sha2
, GIT_SHA1_RAWSZ
);
379 static inline int hasheq(const unsigned char *sha1
, const unsigned char *sha2
, const struct git_hash_algo
*algop
)
382 * We write this here instead of deferring to hashcmp so that the
383 * compiler can properly inline it and avoid calling memcmp.
385 if (algop
->rawsz
== GIT_MAX_RAWSZ
)
386 return !memcmp(sha1
, sha2
, GIT_MAX_RAWSZ
);
387 return !memcmp(sha1
, sha2
, GIT_SHA1_RAWSZ
);
390 static inline void hashcpy(unsigned char *sha_dst
, const unsigned char *sha_src
,
391 const struct git_hash_algo
*algop
)
393 memcpy(sha_dst
, sha_src
, algop
->rawsz
);
396 static inline void hashclr(unsigned char *hash
, const struct git_hash_algo
*algop
)
398 memset(hash
, 0, algop
->rawsz
);
401 static inline int oidcmp(const struct object_id
*oid1
, const struct object_id
*oid2
)
403 return memcmp(oid1
->hash
, oid2
->hash
, GIT_MAX_RAWSZ
);
406 static inline int oideq(const struct object_id
*oid1
, const struct object_id
*oid2
)
408 return !memcmp(oid1
->hash
, oid2
->hash
, GIT_MAX_RAWSZ
);
411 static inline void oidcpy(struct object_id
*dst
, const struct object_id
*src
)
413 memcpy(dst
->hash
, src
->hash
, GIT_MAX_RAWSZ
);
414 dst
->algo
= src
->algo
;
417 static inline void oidread(struct object_id
*oid
, const unsigned char *hash
,
418 const struct git_hash_algo
*algop
)
420 memcpy(oid
->hash
, hash
, algop
->rawsz
);
421 if (algop
->rawsz
< GIT_MAX_RAWSZ
)
422 memset(oid
->hash
+ algop
->rawsz
, 0, GIT_MAX_RAWSZ
- algop
->rawsz
);
423 oid
->algo
= hash_algo_by_ptr(algop
);
426 static inline void oidclr(struct object_id
*oid
,
427 const struct git_hash_algo
*algop
)
429 memset(oid
->hash
, 0, GIT_MAX_RAWSZ
);
430 oid
->algo
= hash_algo_by_ptr(algop
);
433 static inline struct object_id
*oiddup(const struct object_id
*src
)
435 struct object_id
*dst
= xmalloc(sizeof(struct object_id
));
440 static inline void oid_set_algo(struct object_id
*oid
, const struct git_hash_algo
*algop
)
442 oid
->algo
= hash_algo_by_ptr(algop
);
446 * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
447 * for use in hash tables. Cryptographic hashes are supposed to have
448 * uniform distribution, so in contrast to `memhash()`, this just copies
449 * the first `sizeof(int)` bytes without shuffling any bits. Note that
450 * the results will be different on big-endian and little-endian
451 * platforms, so they should not be stored or transferred over the net.
453 static inline unsigned int oidhash(const struct object_id
*oid
)
456 * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
457 * platforms that don't support unaligned reads.
460 memcpy(&hash
, oid
->hash
, sizeof(hash
));
464 static inline int is_null_oid(const struct object_id
*oid
)
466 static const unsigned char null_hash
[GIT_MAX_RAWSZ
];
467 return !memcmp(oid
->hash
, null_hash
, GIT_MAX_RAWSZ
);
470 const char *empty_tree_oid_hex(const struct git_hash_algo
*algop
);
472 static inline int is_empty_blob_oid(const struct object_id
*oid
,
473 const struct git_hash_algo
*algop
)
475 return oideq(oid
, algop
->empty_blob
);
478 static inline int is_empty_tree_oid(const struct object_id
*oid
,
479 const struct git_hash_algo
*algop
)
481 return oideq(oid
, algop
->empty_tree
);