]> git.ipfire.org Git - thirdparty/git.git/blame - hash.h
sha256: add support for Nettle
[thirdparty/git.git] / hash.h
CommitLineData
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
e5557358 19#if defined(SHA256_NETTLE)
20#include "sha256/nettle.h"
21#elif defined(SHA256_GCRYPT)
768e30ea 22#define SHA256_NEEDS_CLONE_HELPER
27dc04c5 23#include "sha256/gcrypt.h"
4b4e2918 24#elif defined(SHA256_OPENSSL)
25#include <openssl/sha.h>
27dc04c5 26#else
13eeedb5 27#include "sha256/block/sha256.h"
27dc04c5 28#endif
13eeedb5 29
164e7163 30#ifndef platform_SHA_CTX
31/*
32 * platform's underlying implementation of SHA-1; could be OpenSSL,
b212c0ca 33 * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
34 * SHA-1 header may have already defined platform_SHA_CTX for our
164e7163 35 * own implementations like block-sha1 and ppc-sha1, so we list
36 * the default for OpenSSL compatible SHA-1 implementations here.
37 */
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
42#endif
43
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
48
13eeedb5 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
54#endif
55
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
60
768e30ea 61#ifdef platform_SHA256_Clone
62#define git_SHA256_Clone platform_SHA256_Clone
63#endif
64
164e7163 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
69#endif
70
768e30ea 71static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src)
72{
73 memcpy(dst, src, sizeof(*dst));
74}
75
76#ifndef SHA256_NEEDS_CLONE_HELPER
77static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src)
78{
79 memcpy(dst, src, sizeof(*dst));
80}
81#endif
82
f50e766b 83/*
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.
90 */
91/* An unknown hash function. */
92#define GIT_HASH_UNKNOWN 0
93/* SHA-1 */
94#define GIT_HASH_SHA1 1
13eeedb5 95/* SHA-256 */
96#define GIT_HASH_SHA256 2
f50e766b 97/* Number of algorithms supported (including unknown). */
13eeedb5 98#define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
f50e766b 99
27f3796a
HWN
100/* "sha1", big-endian */
101#define GIT_SHA1_FORMAT_ID 0x73686131
102
ab795f0d 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
108
27f3796a
HWN
109/* "s256", big-endian */
110#define GIT_SHA256_FORMAT_ID 0x73323536
111
ab795f0d 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
117
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
123
124struct object_id {
125 unsigned char hash[GIT_MAX_RAWSZ];
8bcda98d 126 int algo; /* XXX requires 4-byte alignment */
ab795f0d 127};
128
ac73cedf 129/* A suitably aligned type for stack allocations of hash contexts. */
130union git_hash_ctx {
131 git_SHA_CTX sha1;
13eeedb5 132 git_SHA256_CTX sha256;
ac73cedf 133};
134typedef union git_hash_ctx git_hash_ctx;
135
136typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
768e30ea 137typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src);
ac73cedf 138typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
139typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
ab795f0d 140typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx);
f50e766b 141
142struct git_hash_algo {
143 /*
144 * The name of the algorithm, as appears in the config file and in
145 * messages.
146 */
147 const char *name;
148
149 /* A four-byte version identifier, used in pack indices. */
150 uint32_t format_id;
151
f50e766b 152 /* The length of the hash in binary. */
153 size_t rawsz;
154
155 /* The length of the hash in hex characters. */
156 size_t hexsz;
157
a2ce0a75 158 /* The block size of the hash. */
159 size_t blksz;
160
f50e766b 161 /* The hash initialization function. */
162 git_hash_init_fn init_fn;
163
768e30ea 164 /* The hash context cloning function. */
165 git_hash_clone_fn clone_fn;
166
f50e766b 167 /* The hash update function. */
168 git_hash_update_fn update_fn;
169
170 /* The hash finalization function. */
171 git_hash_final_fn final_fn;
172
ab795f0d 173 /* The hash finalization function for object IDs. */
174 git_hash_final_oid_fn final_oid_fn;
175
f50e766b 176 /* The OID of the empty tree. */
177 const struct object_id *empty_tree;
178
179 /* The OID of the empty blob. */
180 const struct object_id *empty_blob;
14228447 181
182 /* The all-zeros OID. */
183 const struct object_id *null_oid;
f50e766b 184};
185extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
186
2f90b9d9 187/*
188 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
189 * the name doesn't match a known algorithm.
190 */
191int hash_algo_by_name(const char *name);
192/* Identical, except based on the format ID. */
193int hash_algo_by_id(uint32_t format_id);
95399788 194/* Identical, except based on the length. */
195int hash_algo_by_length(int len);
2f90b9d9 196/* Identical, except for a pointer to struct git_hash_algo. */
197static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
198{
199 return p - hash_algos;
200}
201
c0566d78
JK
202#define the_hash_algo the_repository->hash_algo
203
14228447 204const struct object_id *null_oid(void);
3fa6f2aa 205
5a6dce70 206static inline int hashcmp_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
3fa6f2aa
JK
207{
208 /*
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.
211 */
5a6dce70 212 if (algop->rawsz == GIT_MAX_RAWSZ)
3fa6f2aa
JK
213 return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
214 return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
215}
216
5a6dce70 217static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
218{
219 return hashcmp_algop(sha1, sha2, the_hash_algo);
220}
221
3fa6f2aa
JK
222static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
223{
5a6dce70 224 const struct git_hash_algo *algop;
225 if (!oid1->algo)
226 algop = the_hash_algo;
227 else
228 algop = &hash_algos[oid1->algo];
229 return hashcmp_algop(oid1->hash, oid2->hash, algop);
3fa6f2aa
JK
230}
231
5a6dce70 232static inline int hasheq_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
3fa6f2aa
JK
233{
234 /*
235 * We write this here instead of deferring to hashcmp so that the
236 * compiler can properly inline it and avoid calling memcmp.
237 */
5a6dce70 238 if (algop->rawsz == GIT_MAX_RAWSZ)
3fa6f2aa
JK
239 return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
240 return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
241}
242
5a6dce70 243static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
244{
245 return hasheq_algop(sha1, sha2, the_hash_algo);
246}
247
3fa6f2aa
JK
248static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
249{
5a6dce70 250 const struct git_hash_algo *algop;
251 if (!oid1->algo)
252 algop = the_hash_algo;
253 else
254 algop = &hash_algos[oid1->algo];
255 return hasheq_algop(oid1->hash, oid2->hash, algop);
3fa6f2aa
JK
256}
257
258static inline int is_null_oid(const struct object_id *oid)
259{
14228447 260 return oideq(oid, null_oid());
3fa6f2aa
JK
261}
262
263static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
264{
265 memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
266}
267
268static inline void oidcpy(struct object_id *dst, const struct object_id *src)
269{
270 memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
5a6dce70 271 dst->algo = src->algo;
3fa6f2aa
JK
272}
273
3d20ed27
MT
274/* Like oidcpy() but zero-pads the unused bytes in dst's hash array. */
275static inline void oidcpy_with_padding(struct object_id *dst,
90e07f0a 276 const struct object_id *src)
3d20ed27
MT
277{
278 size_t hashsz;
279
280 if (!src->algo)
281 hashsz = the_hash_algo->rawsz;
282 else
283 hashsz = hash_algos[src->algo].rawsz;
284
285 memcpy(dst->hash, src->hash, hashsz);
286 memset(dst->hash + hashsz, 0, GIT_MAX_RAWSZ - hashsz);
287 dst->algo = src->algo;
288}
289
3fa6f2aa
JK
290static inline struct object_id *oiddup(const struct object_id *src)
291{
292 struct object_id *dst = xmalloc(sizeof(struct object_id));
293 oidcpy(dst, src);
294 return dst;
295}
296
297static inline void hashclr(unsigned char *hash)
298{
299 memset(hash, 0, the_hash_algo->rawsz);
300}
301
302static inline void oidclr(struct object_id *oid)
303{
304 memset(oid->hash, 0, GIT_MAX_RAWSZ);
5a6dce70 305 oid->algo = hash_algo_by_ptr(the_hash_algo);
3fa6f2aa
JK
306}
307
308static inline void oidread(struct object_id *oid, const unsigned char *hash)
309{
310 memcpy(oid->hash, hash, the_hash_algo->rawsz);
5a6dce70 311 oid->algo = hash_algo_by_ptr(the_hash_algo);
3fa6f2aa
JK
312}
313
314static inline int is_empty_blob_sha1(const unsigned char *sha1)
315{
316 return hasheq(sha1, the_hash_algo->empty_blob->hash);
317}
318
319static inline int is_empty_blob_oid(const struct object_id *oid)
320{
321 return oideq(oid, the_hash_algo->empty_blob);
322}
323
324static inline int is_empty_tree_sha1(const unsigned char *sha1)
325{
326 return hasheq(sha1, the_hash_algo->empty_tree->hash);
327}
328
329static inline int is_empty_tree_oid(const struct object_id *oid)
330{
331 return oideq(oid, the_hash_algo->empty_tree);
332}
333
5a6dce70 334static inline void oid_set_algo(struct object_id *oid, const struct git_hash_algo *algop)
335{
336 oid->algo = hash_algo_by_ptr(algop);
337}
338
3fa6f2aa
JK
339const char *empty_tree_oid_hex(void);
340const char *empty_blob_oid_hex(void);
341
f18f816c 342#endif