4 #include "git-compat-util.h"
5 #include "repository.h"
9 #elif defined(SHA1_APPLE)
10 #include <CommonCrypto/CommonDigest.h>
11 #elif defined(SHA1_OPENSSL)
12 #include <openssl/sha.h>
13 #elif defined(SHA1_DC)
14 #include "sha1dc_git.h"
16 #include "block-sha1/sha1.h"
19 #if defined(SHA256_NETTLE)
20 #include "sha256/nettle.h"
21 #elif defined(SHA256_GCRYPT)
22 #define SHA256_NEEDS_CLONE_HELPER
23 #include "sha256/gcrypt.h"
24 #elif defined(SHA256_OPENSSL)
25 #include <openssl/sha.h>
27 #include "sha256/block/sha256.h"
30 #ifndef platform_SHA_CTX
32 * platform's underlying implementation of SHA-1; could be OpenSSL,
33 * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
34 * SHA-1 header may have already defined platform_SHA_CTX for our
35 * own implementations like block-sha1 and ppc-sha1, so we list
36 * the default for OpenSSL compatible SHA-1 implementations here.
38 #define platform_SHA_CTX SHA_CTX
39 #define platform_SHA1_Init SHA1_Init
40 #define platform_SHA1_Update SHA1_Update
41 #define platform_SHA1_Final SHA1_Final
44 #define git_SHA_CTX platform_SHA_CTX
45 #define git_SHA1_Init platform_SHA1_Init
46 #define git_SHA1_Update platform_SHA1_Update
47 #define git_SHA1_Final platform_SHA1_Final
49 #ifndef platform_SHA256_CTX
50 #define platform_SHA256_CTX SHA256_CTX
51 #define platform_SHA256_Init SHA256_Init
52 #define platform_SHA256_Update SHA256_Update
53 #define platform_SHA256_Final SHA256_Final
56 #define git_SHA256_CTX platform_SHA256_CTX
57 #define git_SHA256_Init platform_SHA256_Init
58 #define git_SHA256_Update platform_SHA256_Update
59 #define git_SHA256_Final platform_SHA256_Final
61 #ifdef platform_SHA256_Clone
62 #define git_SHA256_Clone platform_SHA256_Clone
65 #ifdef SHA1_MAX_BLOCK_SIZE
66 #include "compat/sha1-chunked.h"
67 #undef git_SHA1_Update
68 #define git_SHA1_Update git_SHA1_Update_Chunked
71 static inline void git_SHA1_Clone(git_SHA_CTX
*dst
, const git_SHA_CTX
*src
)
73 memcpy(dst
, src
, sizeof(*dst
));
76 #ifndef SHA256_NEEDS_CLONE_HELPER
77 static inline void git_SHA256_Clone(git_SHA256_CTX
*dst
, const git_SHA256_CTX
*src
)
79 memcpy(dst
, src
, sizeof(*dst
));
84 * Note that these constants are suitable for indexing the hash_algos array and
85 * comparing against each other, but are otherwise arbitrary, so they should not
86 * be exposed to the user or serialized to disk. To know whether a
87 * git_hash_algo struct points to some usable hash function, test the format_id
88 * field for being non-zero. Use the name field for user-visible situations and
89 * the format_id field for fixed-length fields on disk.
91 /* An unknown hash function. */
92 #define GIT_HASH_UNKNOWN 0
94 #define GIT_HASH_SHA1 1
96 #define GIT_HASH_SHA256 2
97 /* Number of algorithms supported (including unknown). */
98 #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
100 /* "sha1", big-endian */
101 #define GIT_SHA1_FORMAT_ID 0x73686131
103 /* The length in bytes and in hex digits of an object name (SHA-1 value). */
104 #define GIT_SHA1_RAWSZ 20
105 #define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ)
106 /* The block size of SHA-1. */
107 #define GIT_SHA1_BLKSZ 64
109 /* "s256", big-endian */
110 #define GIT_SHA256_FORMAT_ID 0x73323536
112 /* The length in bytes and in hex digits of an object name (SHA-256 value). */
113 #define GIT_SHA256_RAWSZ 32
114 #define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ)
115 /* The block size of SHA-256. */
116 #define GIT_SHA256_BLKSZ 64
118 /* The length in byte and in hex digits of the largest possible hash value. */
119 #define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ
120 #define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ
121 /* The largest possible block size for any supported hash. */
122 #define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ
125 unsigned char hash
[GIT_MAX_RAWSZ
];
126 int algo
; /* XXX requires 4-byte alignment */
129 /* A suitably aligned type for stack allocations of hash contexts. */
132 git_SHA256_CTX sha256
;
134 typedef union git_hash_ctx git_hash_ctx
;
136 typedef void (*git_hash_init_fn
)(git_hash_ctx
*ctx
);
137 typedef void (*git_hash_clone_fn
)(git_hash_ctx
*dst
, const git_hash_ctx
*src
);
138 typedef void (*git_hash_update_fn
)(git_hash_ctx
*ctx
, const void *in
, size_t len
);
139 typedef void (*git_hash_final_fn
)(unsigned char *hash
, git_hash_ctx
*ctx
);
140 typedef void (*git_hash_final_oid_fn
)(struct object_id
*oid
, git_hash_ctx
*ctx
);
142 struct git_hash_algo
{
144 * The name of the algorithm, as appears in the config file and in
149 /* A four-byte version identifier, used in pack indices. */
152 /* The length of the hash in binary. */
155 /* The length of the hash in hex characters. */
158 /* The block size of the hash. */
161 /* The hash initialization function. */
162 git_hash_init_fn init_fn
;
164 /* The hash context cloning function. */
165 git_hash_clone_fn clone_fn
;
167 /* The hash update function. */
168 git_hash_update_fn update_fn
;
170 /* The hash finalization function. */
171 git_hash_final_fn final_fn
;
173 /* The hash finalization function for object IDs. */
174 git_hash_final_oid_fn final_oid_fn
;
176 /* The OID of the empty tree. */
177 const struct object_id
*empty_tree
;
179 /* The OID of the empty blob. */
180 const struct object_id
*empty_blob
;
182 /* The all-zeros OID. */
183 const struct object_id
*null_oid
;
185 extern const struct git_hash_algo hash_algos
[GIT_HASH_NALGOS
];
188 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
189 * the name doesn't match a known algorithm.
191 int hash_algo_by_name(const char *name
);
192 /* Identical, except based on the format ID. */
193 int hash_algo_by_id(uint32_t format_id
);
194 /* Identical, except based on the length. */
195 int hash_algo_by_length(int len
);
196 /* Identical, except for a pointer to struct git_hash_algo. */
197 static inline int hash_algo_by_ptr(const struct git_hash_algo
*p
)
199 return p
- hash_algos
;
202 #define the_hash_algo the_repository->hash_algo
204 const struct object_id
*null_oid(void);
206 static inline int hashcmp_algop(const unsigned char *sha1
, const unsigned char *sha2
, const struct git_hash_algo
*algop
)
209 * Teach the compiler that there are only two possibilities of hash size
210 * here, so that it can optimize for this case as much as possible.
212 if (algop
->rawsz
== GIT_MAX_RAWSZ
)
213 return memcmp(sha1
, sha2
, GIT_MAX_RAWSZ
);
214 return memcmp(sha1
, sha2
, GIT_SHA1_RAWSZ
);
217 static inline int hashcmp(const unsigned char *sha1
, const unsigned char *sha2
)
219 return hashcmp_algop(sha1
, sha2
, the_hash_algo
);
222 static inline int oidcmp(const struct object_id
*oid1
, const struct object_id
*oid2
)
224 const struct git_hash_algo
*algop
;
226 algop
= the_hash_algo
;
228 algop
= &hash_algos
[oid1
->algo
];
229 return hashcmp_algop(oid1
->hash
, oid2
->hash
, algop
);
232 static inline int hasheq_algop(const unsigned char *sha1
, const unsigned char *sha2
, const struct git_hash_algo
*algop
)
235 * We write this here instead of deferring to hashcmp so that the
236 * compiler can properly inline it and avoid calling memcmp.
238 if (algop
->rawsz
== GIT_MAX_RAWSZ
)
239 return !memcmp(sha1
, sha2
, GIT_MAX_RAWSZ
);
240 return !memcmp(sha1
, sha2
, GIT_SHA1_RAWSZ
);
243 static inline int hasheq(const unsigned char *sha1
, const unsigned char *sha2
)
245 return hasheq_algop(sha1
, sha2
, the_hash_algo
);
248 static inline int oideq(const struct object_id
*oid1
, const struct object_id
*oid2
)
250 const struct git_hash_algo
*algop
;
252 algop
= the_hash_algo
;
254 algop
= &hash_algos
[oid1
->algo
];
255 return hasheq_algop(oid1
->hash
, oid2
->hash
, algop
);
258 static inline int is_null_oid(const struct object_id
*oid
)
260 return oideq(oid
, null_oid());
263 static inline void hashcpy(unsigned char *sha_dst
, const unsigned char *sha_src
)
265 memcpy(sha_dst
, sha_src
, the_hash_algo
->rawsz
);
268 static inline void oidcpy(struct object_id
*dst
, const struct object_id
*src
)
270 memcpy(dst
->hash
, src
->hash
, GIT_MAX_RAWSZ
);
271 dst
->algo
= src
->algo
;
274 /* Like oidcpy() but zero-pads the unused bytes in dst's hash array. */
275 static inline void oidcpy_with_padding(struct object_id
*dst
,
276 const struct object_id
*src
)
281 hashsz
= the_hash_algo
->rawsz
;
283 hashsz
= hash_algos
[src
->algo
].rawsz
;
285 memcpy(dst
->hash
, src
->hash
, hashsz
);
286 memset(dst
->hash
+ hashsz
, 0, GIT_MAX_RAWSZ
- hashsz
);
287 dst
->algo
= src
->algo
;
290 static inline struct object_id
*oiddup(const struct object_id
*src
)
292 struct object_id
*dst
= xmalloc(sizeof(struct object_id
));
297 static inline void hashclr(unsigned char *hash
)
299 memset(hash
, 0, the_hash_algo
->rawsz
);
302 static inline void oidclr(struct object_id
*oid
)
304 memset(oid
->hash
, 0, GIT_MAX_RAWSZ
);
305 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
308 static inline void oidread(struct object_id
*oid
, const unsigned char *hash
)
310 memcpy(oid
->hash
, hash
, the_hash_algo
->rawsz
);
311 oid
->algo
= hash_algo_by_ptr(the_hash_algo
);
314 static inline int is_empty_blob_sha1(const unsigned char *sha1
)
316 return hasheq(sha1
, the_hash_algo
->empty_blob
->hash
);
319 static inline int is_empty_blob_oid(const struct object_id
*oid
)
321 return oideq(oid
, the_hash_algo
->empty_blob
);
324 static inline int is_empty_tree_sha1(const unsigned char *sha1
)
326 return hasheq(sha1
, the_hash_algo
->empty_tree
->hash
);
329 static inline int is_empty_tree_oid(const struct object_id
*oid
)
331 return oideq(oid
, the_hash_algo
->empty_tree
);
334 static inline void oid_set_algo(struct object_id
*oid
, const struct git_hash_algo
*algop
)
336 oid
->algo
= hash_algo_by_ptr(algop
);
339 const char *empty_tree_oid_hex(void);
340 const char *empty_blob_oid_hex(void);