brian m. carlson | f18f816 | 2017-03-11 22:28:18 +0000 | [diff] [blame] | 1 | #ifndef HASH_H |
| 2 | #define HASH_H |
| 3 | |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 4 | #include "git-compat-util.h" |
| 5 | |
brian m. carlson | f18f816 | 2017-03-11 22:28:18 +0000 | [diff] [blame] | 6 | #if defined(SHA1_PPC) |
| 7 | #include "ppc/sha1.h" |
| 8 | #elif defined(SHA1_APPLE) |
| 9 | #include <CommonCrypto/CommonDigest.h> |
| 10 | #elif defined(SHA1_OPENSSL) |
| 11 | #include <openssl/sha.h> |
Jeff King | 8325e43 | 2017-03-16 18:09:12 -0400 | [diff] [blame] | 12 | #elif defined(SHA1_DC) |
Takashi Iwai | 36f048c | 2017-08-15 14:04:16 +0200 | [diff] [blame] | 13 | #include "sha1dc_git.h" |
brian m. carlson | f18f816 | 2017-03-11 22:28:18 +0000 | [diff] [blame] | 14 | #else /* SHA1_BLK */ |
| 15 | #include "block-sha1/sha1.h" |
| 16 | #endif |
| 17 | |
brian m. carlson | 27dc04c | 2018-11-14 04:09:37 +0000 | [diff] [blame] | 18 | #if defined(SHA256_GCRYPT) |
brian m. carlson | 768e30e | 2020-02-22 20:17:27 +0000 | [diff] [blame] | 19 | #define SHA256_NEEDS_CLONE_HELPER |
brian m. carlson | 27dc04c | 2018-11-14 04:09:37 +0000 | [diff] [blame] | 20 | #include "sha256/gcrypt.h" |
brian m. carlson | 4b4e291 | 2018-11-14 04:09:38 +0000 | [diff] [blame] | 21 | #elif defined(SHA256_OPENSSL) |
| 22 | #include <openssl/sha.h> |
brian m. carlson | 27dc04c | 2018-11-14 04:09:37 +0000 | [diff] [blame] | 23 | #else |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 24 | #include "sha256/block/sha256.h" |
brian m. carlson | 27dc04c | 2018-11-14 04:09:37 +0000 | [diff] [blame] | 25 | #endif |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 26 | |
brian m. carlson | 164e716 | 2018-02-01 02:18:37 +0000 | [diff] [blame] | 27 | #ifndef platform_SHA_CTX |
| 28 | /* |
| 29 | * platform's underlying implementation of SHA-1; could be OpenSSL, |
brian m. carlson | b212c0c | 2018-02-08 02:48:58 +0000 | [diff] [blame] | 30 | * blk_SHA, Apple CommonCrypto, etc... Note that the relevant |
| 31 | * SHA-1 header may have already defined platform_SHA_CTX for our |
brian m. carlson | 164e716 | 2018-02-01 02:18:37 +0000 | [diff] [blame] | 32 | * own implementations like block-sha1 and ppc-sha1, so we list |
| 33 | * the default for OpenSSL compatible SHA-1 implementations here. |
| 34 | */ |
| 35 | #define platform_SHA_CTX SHA_CTX |
| 36 | #define platform_SHA1_Init SHA1_Init |
| 37 | #define platform_SHA1_Update SHA1_Update |
| 38 | #define platform_SHA1_Final SHA1_Final |
| 39 | #endif |
| 40 | |
| 41 | #define git_SHA_CTX platform_SHA_CTX |
| 42 | #define git_SHA1_Init platform_SHA1_Init |
| 43 | #define git_SHA1_Update platform_SHA1_Update |
| 44 | #define git_SHA1_Final platform_SHA1_Final |
| 45 | |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 46 | #ifndef platform_SHA256_CTX |
| 47 | #define platform_SHA256_CTX SHA256_CTX |
| 48 | #define platform_SHA256_Init SHA256_Init |
| 49 | #define platform_SHA256_Update SHA256_Update |
| 50 | #define platform_SHA256_Final SHA256_Final |
| 51 | #endif |
| 52 | |
| 53 | #define git_SHA256_CTX platform_SHA256_CTX |
| 54 | #define git_SHA256_Init platform_SHA256_Init |
| 55 | #define git_SHA256_Update platform_SHA256_Update |
| 56 | #define git_SHA256_Final platform_SHA256_Final |
| 57 | |
brian m. carlson | 768e30e | 2020-02-22 20:17:27 +0000 | [diff] [blame] | 58 | #ifdef platform_SHA256_Clone |
| 59 | #define git_SHA256_Clone platform_SHA256_Clone |
| 60 | #endif |
| 61 | |
brian m. carlson | 164e716 | 2018-02-01 02:18:37 +0000 | [diff] [blame] | 62 | #ifdef SHA1_MAX_BLOCK_SIZE |
| 63 | #include "compat/sha1-chunked.h" |
| 64 | #undef git_SHA1_Update |
| 65 | #define git_SHA1_Update git_SHA1_Update_Chunked |
| 66 | #endif |
| 67 | |
brian m. carlson | 768e30e | 2020-02-22 20:17:27 +0000 | [diff] [blame] | 68 | static inline void git_SHA1_Clone(git_SHA_CTX *dst, const git_SHA_CTX *src) |
| 69 | { |
| 70 | memcpy(dst, src, sizeof(*dst)); |
| 71 | } |
| 72 | |
| 73 | #ifndef SHA256_NEEDS_CLONE_HELPER |
| 74 | static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *src) |
| 75 | { |
| 76 | memcpy(dst, src, sizeof(*dst)); |
| 77 | } |
| 78 | #endif |
| 79 | |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 80 | /* |
| 81 | * Note that these constants are suitable for indexing the hash_algos array and |
| 82 | * comparing against each other, but are otherwise arbitrary, so they should not |
| 83 | * be exposed to the user or serialized to disk. To know whether a |
| 84 | * git_hash_algo struct points to some usable hash function, test the format_id |
| 85 | * field for being non-zero. Use the name field for user-visible situations and |
| 86 | * the format_id field for fixed-length fields on disk. |
| 87 | */ |
| 88 | /* An unknown hash function. */ |
| 89 | #define GIT_HASH_UNKNOWN 0 |
| 90 | /* SHA-1 */ |
| 91 | #define GIT_HASH_SHA1 1 |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 92 | /* SHA-256 */ |
| 93 | #define GIT_HASH_SHA256 2 |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 94 | /* Number of algorithms supported (including unknown). */ |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 95 | #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1) |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 96 | |
brian m. carlson | ac73ced | 2018-02-01 02:18:38 +0000 | [diff] [blame] | 97 | /* A suitably aligned type for stack allocations of hash contexts. */ |
| 98 | union git_hash_ctx { |
| 99 | git_SHA_CTX sha1; |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 100 | git_SHA256_CTX sha256; |
brian m. carlson | ac73ced | 2018-02-01 02:18:38 +0000 | [diff] [blame] | 101 | }; |
| 102 | typedef union git_hash_ctx git_hash_ctx; |
| 103 | |
| 104 | typedef void (*git_hash_init_fn)(git_hash_ctx *ctx); |
brian m. carlson | 768e30e | 2020-02-22 20:17:27 +0000 | [diff] [blame] | 105 | typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src); |
brian m. carlson | ac73ced | 2018-02-01 02:18:38 +0000 | [diff] [blame] | 106 | typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len); |
| 107 | typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx); |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 108 | |
| 109 | struct git_hash_algo { |
| 110 | /* |
| 111 | * The name of the algorithm, as appears in the config file and in |
| 112 | * messages. |
| 113 | */ |
| 114 | const char *name; |
| 115 | |
| 116 | /* A four-byte version identifier, used in pack indices. */ |
| 117 | uint32_t format_id; |
| 118 | |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 119 | /* The length of the hash in binary. */ |
| 120 | size_t rawsz; |
| 121 | |
| 122 | /* The length of the hash in hex characters. */ |
| 123 | size_t hexsz; |
| 124 | |
brian m. carlson | a2ce0a7 | 2018-11-14 04:09:33 +0000 | [diff] [blame] | 125 | /* The block size of the hash. */ |
| 126 | size_t blksz; |
| 127 | |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 128 | /* The hash initialization function. */ |
| 129 | git_hash_init_fn init_fn; |
| 130 | |
brian m. carlson | 768e30e | 2020-02-22 20:17:27 +0000 | [diff] [blame] | 131 | /* The hash context cloning function. */ |
| 132 | git_hash_clone_fn clone_fn; |
| 133 | |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 134 | /* The hash update function. */ |
| 135 | git_hash_update_fn update_fn; |
| 136 | |
| 137 | /* The hash finalization function. */ |
| 138 | git_hash_final_fn final_fn; |
| 139 | |
| 140 | /* The OID of the empty tree. */ |
| 141 | const struct object_id *empty_tree; |
| 142 | |
| 143 | /* The OID of the empty blob. */ |
| 144 | const struct object_id *empty_blob; |
| 145 | }; |
| 146 | extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS]; |
| 147 | |
brian m. carlson | 2f90b9d | 2018-10-22 02:43:32 +0000 | [diff] [blame] | 148 | /* |
| 149 | * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if |
| 150 | * the name doesn't match a known algorithm. |
| 151 | */ |
| 152 | int hash_algo_by_name(const char *name); |
| 153 | /* Identical, except based on the format ID. */ |
| 154 | int hash_algo_by_id(uint32_t format_id); |
brian m. carlson | 9539978 | 2019-02-19 00:05:17 +0000 | [diff] [blame] | 155 | /* Identical, except based on the length. */ |
| 156 | int hash_algo_by_length(int len); |
brian m. carlson | 2f90b9d | 2018-10-22 02:43:32 +0000 | [diff] [blame] | 157 | /* Identical, except for a pointer to struct git_hash_algo. */ |
| 158 | static inline int hash_algo_by_ptr(const struct git_hash_algo *p) |
| 159 | { |
| 160 | return p - hash_algos; |
| 161 | } |
| 162 | |
Jeff King | c0566d7 | 2019-06-20 03:41:45 -0400 | [diff] [blame] | 163 | /* The length in bytes and in hex digits of an object name (SHA-1 value). */ |
| 164 | #define GIT_SHA1_RAWSZ 20 |
| 165 | #define GIT_SHA1_HEXSZ (2 * GIT_SHA1_RAWSZ) |
| 166 | /* The block size of SHA-1. */ |
| 167 | #define GIT_SHA1_BLKSZ 64 |
| 168 | |
| 169 | /* The length in bytes and in hex digits of an object name (SHA-256 value). */ |
| 170 | #define GIT_SHA256_RAWSZ 32 |
| 171 | #define GIT_SHA256_HEXSZ (2 * GIT_SHA256_RAWSZ) |
| 172 | /* The block size of SHA-256. */ |
| 173 | #define GIT_SHA256_BLKSZ 64 |
| 174 | |
| 175 | /* The length in byte and in hex digits of the largest possible hash value. */ |
| 176 | #define GIT_MAX_RAWSZ GIT_SHA256_RAWSZ |
| 177 | #define GIT_MAX_HEXSZ GIT_SHA256_HEXSZ |
| 178 | /* The largest possible block size for any supported hash. */ |
| 179 | #define GIT_MAX_BLKSZ GIT_SHA256_BLKSZ |
| 180 | |
| 181 | struct object_id { |
| 182 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 183 | }; |
| 184 | |
| 185 | #define the_hash_algo the_repository->hash_algo |
| 186 | |
brian m. carlson | f18f816 | 2017-03-11 22:28:18 +0000 | [diff] [blame] | 187 | #endif |