4 #include "git-compat-util.h"
8 #elif defined(SHA1_APPLE)
9 #include <CommonCrypto/CommonDigest.h>
10 #elif defined(SHA1_OPENSSL)
11 #include <openssl/sha.h>
12 #elif defined(SHA1_DC)
13 #include "sha1dc_git.h"
15 #include "block-sha1/sha1.h"
18 #include "sha256/block/sha256.h"
20 #ifndef platform_SHA_CTX
22 * platform's underlying implementation of SHA-1; could be OpenSSL,
23 * blk_SHA, Apple CommonCrypto, etc... Note that the relevant
24 * SHA-1 header may have already defined platform_SHA_CTX for our
25 * own implementations like block-sha1 and ppc-sha1, so we list
26 * the default for OpenSSL compatible SHA-1 implementations here.
28 #define platform_SHA_CTX SHA_CTX
29 #define platform_SHA1_Init SHA1_Init
30 #define platform_SHA1_Update SHA1_Update
31 #define platform_SHA1_Final SHA1_Final
34 #define git_SHA_CTX platform_SHA_CTX
35 #define git_SHA1_Init platform_SHA1_Init
36 #define git_SHA1_Update platform_SHA1_Update
37 #define git_SHA1_Final platform_SHA1_Final
39 #ifndef platform_SHA256_CTX
40 #define platform_SHA256_CTX SHA256_CTX
41 #define platform_SHA256_Init SHA256_Init
42 #define platform_SHA256_Update SHA256_Update
43 #define platform_SHA256_Final SHA256_Final
46 #define git_SHA256_CTX platform_SHA256_CTX
47 #define git_SHA256_Init platform_SHA256_Init
48 #define git_SHA256_Update platform_SHA256_Update
49 #define git_SHA256_Final platform_SHA256_Final
51 #ifdef SHA1_MAX_BLOCK_SIZE
52 #include "compat/sha1-chunked.h"
53 #undef git_SHA1_Update
54 #define git_SHA1_Update git_SHA1_Update_Chunked
58 * Note that these constants are suitable for indexing the hash_algos array and
59 * comparing against each other, but are otherwise arbitrary, so they should not
60 * be exposed to the user or serialized to disk. To know whether a
61 * git_hash_algo struct points to some usable hash function, test the format_id
62 * field for being non-zero. Use the name field for user-visible situations and
63 * the format_id field for fixed-length fields on disk.
65 /* An unknown hash function. */
66 #define GIT_HASH_UNKNOWN 0
68 #define GIT_HASH_SHA1 1
70 #define GIT_HASH_SHA256 2
71 /* Number of algorithms supported (including unknown). */
72 #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1)
74 /* A suitably aligned type for stack allocations of hash contexts. */
77 git_SHA256_CTX sha256;
79 typedef union git_hash_ctx git_hash_ctx;
81 typedef void (*git_hash_init_fn)(git_hash_ctx *ctx);
82 typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len);
83 typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx);
85 struct git_hash_algo {
87 * The name of the algorithm, as appears in the config file and in
92 /* A four-byte version identifier, used in pack indices. */
95 /* The length of the hash in binary. */
98 /* The length of the hash in hex characters. */
101 /* The block size of the hash. */
104 /* The hash initialization function. */
105 git_hash_init_fn init_fn;
107 /* The hash update function. */
108 git_hash_update_fn update_fn;
110 /* The hash finalization function. */
111 git_hash_final_fn final_fn;
113 /* The OID of the empty tree. */
114 const struct object_id *empty_tree;
116 /* The OID of the empty blob. */
117 const struct object_id *empty_blob;
119 extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
122 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
123 * the name doesn't match a known algorithm.
125 int hash_algo_by_name(const char *name);
126 /* Identical, except based on the format ID. */
127 int hash_algo_by_id(uint32_t format_id);
128 /* Identical, except for a pointer to struct git_hash_algo. */
129 static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
131 return p - hash_algos;