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 /* "sha1", big-endian */
179 #define GIT_SHA1_FORMAT_ID 0x73686131
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
187 /* "s256", big-endian */
188 #define GIT_SHA256_FORMAT_ID 0x73323536
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
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
203 unsigned char hash
[GIT_MAX_RAWSZ
];
204 int algo
; /* XXX requires 4-byte alignment */
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
221 #define GET_OID_DISAMBIGUATORS \
222 (GET_OID_COMMIT | GET_OID_COMMITTISH | \
223 GET_OID_TREE | GET_OID_TREEISH | \
226 enum get_oid_result
{
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
238 * Somewhere along the symlink chain, a path is
239 * requested which contains a file as a
244 #ifdef USE_THE_REPOSITORY_VARIABLE
245 # include "repository.h"
246 # define the_hash_algo the_repository->hash_algo
249 /* A suitably aligned type for stack allocations of hash contexts. */
250 struct git_hash_ctx
{
251 const struct git_hash_algo
*algop
;
254 git_SHA_CTX_unsafe sha1_unsafe
;
255 git_SHA256_CTX sha256
;
259 typedef void (*git_hash_init_fn
)(struct git_hash_ctx
*ctx
);
260 typedef void (*git_hash_clone_fn
)(struct git_hash_ctx
*dst
, const struct git_hash_ctx
*src
);
261 typedef void (*git_hash_update_fn
)(struct git_hash_ctx
*ctx
, const void *in
, size_t len
);
262 typedef void (*git_hash_final_fn
)(unsigned char *hash
, struct git_hash_ctx
*ctx
);
263 typedef void (*git_hash_final_oid_fn
)(struct object_id
*oid
, struct git_hash_ctx
*ctx
);
265 struct git_hash_algo
{
267 * The name of the algorithm, as appears in the config file and in
272 /* A four-byte version identifier, used in pack indices. */
275 /* The length of the hash in binary. */
278 /* The length of the hash in hex characters. */
281 /* The block size of the hash. */
284 /* The hash initialization function. */
285 git_hash_init_fn init_fn
;
287 /* The hash context cloning function. */
288 git_hash_clone_fn clone_fn
;
290 /* The hash update function. */
291 git_hash_update_fn update_fn
;
293 /* The hash finalization function. */
294 git_hash_final_fn final_fn
;
296 /* The hash finalization function for object IDs. */
297 git_hash_final_oid_fn final_oid_fn
;
299 /* The OID of the empty tree. */
300 const struct object_id
*empty_tree
;
302 /* The OID of the empty blob. */
303 const struct object_id
*empty_blob
;
305 /* The all-zeros OID. */
306 const struct object_id
*null_oid
;
308 /* The unsafe variant of this hash function, if one exists. */
309 const struct git_hash_algo
*unsafe
;
311 extern const struct git_hash_algo hash_algos
[GIT_HASH_NALGOS
];
313 static inline void git_hash_clone(struct git_hash_ctx
*dst
, const struct git_hash_ctx
*src
)
315 src
->algop
->clone_fn(dst
, src
);
318 static inline void git_hash_update(struct git_hash_ctx
*ctx
, const void *in
, size_t len
)
320 ctx
->algop
->update_fn(ctx
, in
, len
);
323 static inline void git_hash_final(unsigned char *hash
, struct git_hash_ctx
*ctx
)
325 ctx
->algop
->final_fn(hash
, ctx
);
328 static inline void git_hash_final_oid(struct object_id
*oid
, struct git_hash_ctx
*ctx
)
330 ctx
->algop
->final_oid_fn(oid
, ctx
);
334 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
335 * the name doesn't match a known algorithm.
337 int hash_algo_by_name(const char *name
);
338 /* Identical, except based on the format ID. */
339 int hash_algo_by_id(uint32_t format_id
);
340 /* Identical, except based on the length. */
341 int hash_algo_by_length(size_t len
);
342 /* Identical, except for a pointer to struct git_hash_algo. */
343 static inline int hash_algo_by_ptr(const struct git_hash_algo
*p
)
346 for (i
= 0; i
< GIT_HASH_NALGOS
; i
++) {
347 const struct git_hash_algo
*algop
= &hash_algos
[i
];
351 return GIT_HASH_UNKNOWN
;
354 const struct git_hash_algo
*unsafe_hash_algo(const struct git_hash_algo
*algop
);
356 const struct object_id
*null_oid(const struct git_hash_algo
*algop
);
358 static inline int hashcmp(const unsigned char *sha1
, const unsigned char *sha2
, const struct git_hash_algo
*algop
)
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.
364 if (algop
->rawsz
== GIT_MAX_RAWSZ
)
365 return memcmp(sha1
, sha2
, GIT_MAX_RAWSZ
);
366 return memcmp(sha1
, sha2
, GIT_SHA1_RAWSZ
);
369 static inline int hasheq(const unsigned char *sha1
, const unsigned char *sha2
, const struct git_hash_algo
*algop
)
372 * We write this here instead of deferring to hashcmp so that the
373 * compiler can properly inline it and avoid calling memcmp.
375 if (algop
->rawsz
== GIT_MAX_RAWSZ
)
376 return !memcmp(sha1
, sha2
, GIT_MAX_RAWSZ
);
377 return !memcmp(sha1
, sha2
, GIT_SHA1_RAWSZ
);
380 static inline void hashcpy(unsigned char *sha_dst
, const unsigned char *sha_src
,
381 const struct git_hash_algo
*algop
)
383 memcpy(sha_dst
, sha_src
, algop
->rawsz
);
386 static inline void hashclr(unsigned char *hash
, const struct git_hash_algo
*algop
)
388 memset(hash
, 0, algop
->rawsz
);
391 static inline int oidcmp(const struct object_id
*oid1
, const struct object_id
*oid2
)
393 return memcmp(oid1
->hash
, oid2
->hash
, GIT_MAX_RAWSZ
);
396 static inline int oideq(const struct object_id
*oid1
, const struct object_id
*oid2
)
398 return !memcmp(oid1
->hash
, oid2
->hash
, GIT_MAX_RAWSZ
);
401 static inline void oidcpy(struct object_id
*dst
, const struct object_id
*src
)
403 memcpy(dst
->hash
, src
->hash
, GIT_MAX_RAWSZ
);
404 dst
->algo
= src
->algo
;
407 static inline void oidread(struct object_id
*oid
, const unsigned char *hash
,
408 const struct git_hash_algo
*algop
)
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
);
416 static inline void oidclr(struct object_id
*oid
,
417 const struct git_hash_algo
*algop
)
419 memset(oid
->hash
, 0, GIT_MAX_RAWSZ
);
420 oid
->algo
= hash_algo_by_ptr(algop
);
423 static inline struct object_id
*oiddup(const struct object_id
*src
)
425 struct object_id
*dst
= xmalloc(sizeof(struct object_id
));
430 static inline void oid_set_algo(struct object_id
*oid
, const struct git_hash_algo
*algop
)
432 oid
->algo
= hash_algo_by_ptr(algop
);
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.
443 static inline unsigned int oidhash(const struct object_id
*oid
)
446 * Equivalent to 'return *(unsigned int *)oid->hash;', but safe on
447 * platforms that don't support unaligned reads.
450 memcpy(&hash
, oid
->hash
, sizeof(hash
));
454 static inline int is_null_oid(const struct object_id
*oid
)
456 static const unsigned char null_hash
[GIT_MAX_RAWSZ
];
457 return !memcmp(oid
->hash
, null_hash
, GIT_MAX_RAWSZ
);
460 const char *empty_tree_oid_hex(const struct git_hash_algo
*algop
);
462 static inline int is_empty_blob_oid(const struct object_id
*oid
,
463 const struct git_hash_algo
*algop
)
465 return oideq(oid
, algop
->empty_blob
);
468 static inline int is_empty_tree_oid(const struct object_id
*oid
,
469 const struct git_hash_algo
*algop
)
471 return oideq(oid
, algop
->empty_tree
);