]>
Commit | Line | Data |
---|---|---|
f18f816c | 1 | #ifndef HASH_H |
2 | #define HASH_H | |
3 | ||
f50e766b | 4 | #include "git-compat-util.h" |
3fa6f2aa | 5 | #include "repository.h" |
f50e766b | 6 | |
f18f816c | 7 | #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> | |
8325e43b | 13 | #elif defined(SHA1_DC) |
36f048c5 | 14 | #include "sha1dc_git.h" |
f18f816c | 15 | #else /* SHA1_BLK */ |
16 | #include "block-sha1/sha1.h" | |
17 | #endif | |
18 | ||
27dc04c5 | 19 | #if defined(SHA256_GCRYPT) |
768e30ea | 20 | #define SHA256_NEEDS_CLONE_HELPER |
27dc04c5 | 21 | #include "sha256/gcrypt.h" |
4b4e2918 | 22 | #elif defined(SHA256_OPENSSL) |
23 | #include <openssl/sha.h> | |
27dc04c5 | 24 | #else |
13eeedb5 | 25 | #include "sha256/block/sha256.h" |
27dc04c5 | 26 | #endif |
13eeedb5 | 27 | |
164e7163 | 28 | #ifndef platform_SHA_CTX |
29 | /* | |
30 | * platform's underlying implementation of SHA-1; could be OpenSSL, | |
b212c0ca | 31 | * blk_SHA, Apple CommonCrypto, etc... Note that the relevant |
32 | * SHA-1 header may have already defined platform_SHA_CTX for our | |
164e7163 | 33 | * 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 | ||
13eeedb5 | 47 | #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 | ||
768e30ea | 59 | #ifdef platform_SHA256_Clone |
60 | #define git_SHA256_Clone platform_SHA256_Clone | |
61 | #endif | |
62 | ||
164e7163 | 63 | #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 | ||
768e30ea | 69 | static 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 | |
75 | static 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 | ||
f50e766b | 81 | /* |
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 | |
13eeedb5 | 93 | /* SHA-256 */ |
94 | #define GIT_HASH_SHA256 2 | |
f50e766b | 95 | /* Number of algorithms supported (including unknown). */ |
13eeedb5 | 96 | #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1) |
f50e766b | 97 | |
ac73cedf | 98 | /* A suitably aligned type for stack allocations of hash contexts. */ |
99 | union git_hash_ctx { | |
100 | git_SHA_CTX sha1; | |
13eeedb5 | 101 | git_SHA256_CTX sha256; |
ac73cedf | 102 | }; |
103 | typedef union git_hash_ctx git_hash_ctx; | |
104 | ||
105 | typedef void (*git_hash_init_fn)(git_hash_ctx *ctx); | |
768e30ea | 106 | typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src); |
ac73cedf | 107 | typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len); |
108 | typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx); | |
f50e766b | 109 | |
110 | struct git_hash_algo { | |
111 | /* | |
112 | * The name of the algorithm, as appears in the config file and in | |
113 | * messages. | |
114 | */ | |
115 | const char *name; | |
116 | ||
117 | /* A four-byte version identifier, used in pack indices. */ | |
118 | uint32_t format_id; | |
119 | ||
f50e766b | 120 | /* The length of the hash in binary. */ |
121 | size_t rawsz; | |
122 | ||
123 | /* The length of the hash in hex characters. */ | |
124 | size_t hexsz; | |
125 | ||
a2ce0a75 | 126 | /* The block size of the hash. */ |
127 | size_t blksz; | |
128 | ||
f50e766b | 129 | /* The hash initialization function. */ |
130 | git_hash_init_fn init_fn; | |
131 | ||
768e30ea | 132 | /* The hash context cloning function. */ |
133 | git_hash_clone_fn clone_fn; | |
134 | ||
f50e766b | 135 | /* The hash update function. */ |
136 | git_hash_update_fn update_fn; | |
137 | ||
138 | /* The hash finalization function. */ | |
139 | git_hash_final_fn final_fn; | |
140 | ||
141 | /* The OID of the empty tree. */ | |
142 | const struct object_id *empty_tree; | |
143 | ||
144 | /* The OID of the empty blob. */ | |
145 | const struct object_id *empty_blob; | |
146 | }; | |
147 | extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS]; | |
148 | ||
2f90b9d9 | 149 | /* |
150 | * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if | |
151 | * the name doesn't match a known algorithm. | |
152 | */ | |
153 | int hash_algo_by_name(const char *name); | |
154 | /* Identical, except based on the format ID. */ | |
155 | int hash_algo_by_id(uint32_t format_id); | |
95399788 | 156 | /* Identical, except based on the length. */ |
157 | int hash_algo_by_length(int len); | |
2f90b9d9 | 158 | /* Identical, except for a pointer to struct git_hash_algo. */ |
159 | static inline int hash_algo_by_ptr(const struct git_hash_algo *p) | |
160 | { | |
161 | return p - hash_algos; | |
162 | } | |
163 | ||
c0566d78 JK |
164 | /* The length in bytes and in hex digits of an object name (SHA-1 value). */ |
165 | #define GIT_SHA1_RAWSZ 20 | |
166 | #define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ) | |
167 | /* The block size of SHA-1. */ | |
168 | #define GIT_SHA1_BLKSZ 64 | |
169 | ||
170 | /* The length in bytes and in hex digits of an object name (SHA-256 value). */ | |
171 | #define GIT_SHA256_RAWSZ 32 | |
172 | #define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ) | |
173 | /* The block size of SHA-256. */ | |
174 | #define GIT_SHA256_BLKSZ 64 | |
175 | ||
176 | /* The length in byte and in hex digits of the largest possible hash value. */ | |
177 | #define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ | |
178 | #define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ | |
179 | /* The largest possible block size for any supported hash. */ | |
180 | #define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ | |
181 | ||
182 | struct object_id { | |
183 | unsigned char hash[GIT_MAX_RAWSZ]; | |
184 | }; | |
185 | ||
186 | #define the_hash_algo the_repository->hash_algo | |
187 | ||
3fa6f2aa JK |
188 | extern const struct object_id null_oid; |
189 | ||
190 | static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2) | |
191 | { | |
192 | /* | |
193 | * Teach the compiler that there are only two possibilities of hash size | |
194 | * here, so that it can optimize for this case as much as possible. | |
195 | */ | |
196 | if (the_hash_algo->rawsz == GIT_MAX_RAWSZ) | |
197 | return memcmp(sha1, sha2, GIT_MAX_RAWSZ); | |
198 | return memcmp(sha1, sha2, GIT_SHA1_RAWSZ); | |
199 | } | |
200 | ||
201 | static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2) | |
202 | { | |
203 | return hashcmp(oid1->hash, oid2->hash); | |
204 | } | |
205 | ||
206 | static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2) | |
207 | { | |
208 | /* | |
209 | * We write this here instead of deferring to hashcmp so that the | |
210 | * compiler can properly inline it and avoid calling memcmp. | |
211 | */ | |
212 | if (the_hash_algo->rawsz == GIT_MAX_RAWSZ) | |
213 | return !memcmp(sha1, sha2, GIT_MAX_RAWSZ); | |
214 | return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ); | |
215 | } | |
216 | ||
217 | static inline int oideq(const struct object_id *oid1, const struct object_id *oid2) | |
218 | { | |
219 | return hasheq(oid1->hash, oid2->hash); | |
220 | } | |
221 | ||
222 | static inline int is_null_oid(const struct object_id *oid) | |
223 | { | |
224 | return oideq(oid, &null_oid); | |
225 | } | |
226 | ||
227 | static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src) | |
228 | { | |
229 | memcpy(sha_dst, sha_src, the_hash_algo->rawsz); | |
230 | } | |
231 | ||
232 | static inline void oidcpy(struct object_id *dst, const struct object_id *src) | |
233 | { | |
234 | memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ); | |
235 | } | |
236 | ||
237 | static inline struct object_id *oiddup(const struct object_id *src) | |
238 | { | |
239 | struct object_id *dst = xmalloc(sizeof(struct object_id)); | |
240 | oidcpy(dst, src); | |
241 | return dst; | |
242 | } | |
243 | ||
244 | static inline void hashclr(unsigned char *hash) | |
245 | { | |
246 | memset(hash, 0, the_hash_algo->rawsz); | |
247 | } | |
248 | ||
249 | static inline void oidclr(struct object_id *oid) | |
250 | { | |
251 | memset(oid->hash, 0, GIT_MAX_RAWSZ); | |
252 | } | |
253 | ||
254 | static inline void oidread(struct object_id *oid, const unsigned char *hash) | |
255 | { | |
256 | memcpy(oid->hash, hash, the_hash_algo->rawsz); | |
257 | } | |
258 | ||
259 | static inline int is_empty_blob_sha1(const unsigned char *sha1) | |
260 | { | |
261 | return hasheq(sha1, the_hash_algo->empty_blob->hash); | |
262 | } | |
263 | ||
264 | static inline int is_empty_blob_oid(const struct object_id *oid) | |
265 | { | |
266 | return oideq(oid, the_hash_algo->empty_blob); | |
267 | } | |
268 | ||
269 | static inline int is_empty_tree_sha1(const unsigned char *sha1) | |
270 | { | |
271 | return hasheq(sha1, the_hash_algo->empty_tree->hash); | |
272 | } | |
273 | ||
274 | static inline int is_empty_tree_oid(const struct object_id *oid) | |
275 | { | |
276 | return oideq(oid, the_hash_algo->empty_tree); | |
277 | } | |
278 | ||
279 | const char *empty_tree_oid_hex(void); | |
280 | const char *empty_blob_oid_hex(void); | |
281 | ||
f18f816c | 282 | #endif |