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) |
| 19 | #include "sha256/gcrypt.h" |
brian m. carlson | 4b4e291 | 2018-11-14 04:09:38 +0000 | [diff] [blame] | 20 | #elif defined(SHA256_OPENSSL) |
| 21 | #include <openssl/sha.h> |
brian m. carlson | 27dc04c | 2018-11-14 04:09:37 +0000 | [diff] [blame] | 22 | #else |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 23 | #include "sha256/block/sha256.h" |
brian m. carlson | 27dc04c | 2018-11-14 04:09:37 +0000 | [diff] [blame] | 24 | #endif |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 25 | |
brian m. carlson | 164e716 | 2018-02-01 02:18:37 +0000 | [diff] [blame] | 26 | #ifndef platform_SHA_CTX |
| 27 | /* |
| 28 | * platform's underlying implementation of SHA-1; could be OpenSSL, |
brian m. carlson | b212c0c | 2018-02-08 02:48:58 +0000 | [diff] [blame] | 29 | * blk_SHA, Apple CommonCrypto, etc... Note that the relevant |
| 30 | * 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] | 31 | * own implementations like block-sha1 and ppc-sha1, so we list |
| 32 | * the default for OpenSSL compatible SHA-1 implementations here. |
| 33 | */ |
| 34 | #define platform_SHA_CTX SHA_CTX |
| 35 | #define platform_SHA1_Init SHA1_Init |
| 36 | #define platform_SHA1_Update SHA1_Update |
| 37 | #define platform_SHA1_Final SHA1_Final |
| 38 | #endif |
| 39 | |
| 40 | #define git_SHA_CTX platform_SHA_CTX |
| 41 | #define git_SHA1_Init platform_SHA1_Init |
| 42 | #define git_SHA1_Update platform_SHA1_Update |
| 43 | #define git_SHA1_Final platform_SHA1_Final |
| 44 | |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 45 | #ifndef platform_SHA256_CTX |
| 46 | #define platform_SHA256_CTX SHA256_CTX |
| 47 | #define platform_SHA256_Init SHA256_Init |
| 48 | #define platform_SHA256_Update SHA256_Update |
| 49 | #define platform_SHA256_Final SHA256_Final |
| 50 | #endif |
| 51 | |
| 52 | #define git_SHA256_CTX platform_SHA256_CTX |
| 53 | #define git_SHA256_Init platform_SHA256_Init |
| 54 | #define git_SHA256_Update platform_SHA256_Update |
| 55 | #define git_SHA256_Final platform_SHA256_Final |
| 56 | |
brian m. carlson | 164e716 | 2018-02-01 02:18:37 +0000 | [diff] [blame] | 57 | #ifdef SHA1_MAX_BLOCK_SIZE |
| 58 | #include "compat/sha1-chunked.h" |
| 59 | #undef git_SHA1_Update |
| 60 | #define git_SHA1_Update git_SHA1_Update_Chunked |
| 61 | #endif |
| 62 | |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 63 | /* |
| 64 | * Note that these constants are suitable for indexing the hash_algos array and |
| 65 | * comparing against each other, but are otherwise arbitrary, so they should not |
| 66 | * be exposed to the user or serialized to disk. To know whether a |
| 67 | * git_hash_algo struct points to some usable hash function, test the format_id |
| 68 | * field for being non-zero. Use the name field for user-visible situations and |
| 69 | * the format_id field for fixed-length fields on disk. |
| 70 | */ |
| 71 | /* An unknown hash function. */ |
| 72 | #define GIT_HASH_UNKNOWN 0 |
| 73 | /* SHA-1 */ |
| 74 | #define GIT_HASH_SHA1 1 |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 75 | /* SHA-256 */ |
| 76 | #define GIT_HASH_SHA256 2 |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 77 | /* Number of algorithms supported (including unknown). */ |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 78 | #define GIT_HASH_NALGOS (GIT_HASH_SHA256 + 1) |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 79 | |
brian m. carlson | ac73ced | 2018-02-01 02:18:38 +0000 | [diff] [blame] | 80 | /* A suitably aligned type for stack allocations of hash contexts. */ |
| 81 | union git_hash_ctx { |
| 82 | git_SHA_CTX sha1; |
brian m. carlson | 13eeedb | 2018-11-14 04:09:36 +0000 | [diff] [blame] | 83 | git_SHA256_CTX sha256; |
brian m. carlson | ac73ced | 2018-02-01 02:18:38 +0000 | [diff] [blame] | 84 | }; |
| 85 | typedef union git_hash_ctx git_hash_ctx; |
| 86 | |
| 87 | typedef void (*git_hash_init_fn)(git_hash_ctx *ctx); |
| 88 | typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len); |
| 89 | 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] | 90 | |
| 91 | struct git_hash_algo { |
| 92 | /* |
| 93 | * The name of the algorithm, as appears in the config file and in |
| 94 | * messages. |
| 95 | */ |
| 96 | const char *name; |
| 97 | |
| 98 | /* A four-byte version identifier, used in pack indices. */ |
| 99 | uint32_t format_id; |
| 100 | |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 101 | /* The length of the hash in binary. */ |
| 102 | size_t rawsz; |
| 103 | |
| 104 | /* The length of the hash in hex characters. */ |
| 105 | size_t hexsz; |
| 106 | |
brian m. carlson | a2ce0a7 | 2018-11-14 04:09:33 +0000 | [diff] [blame] | 107 | /* The block size of the hash. */ |
| 108 | size_t blksz; |
| 109 | |
brian m. carlson | f50e766 | 2017-11-12 21:28:52 +0000 | [diff] [blame] | 110 | /* The hash initialization function. */ |
| 111 | git_hash_init_fn init_fn; |
| 112 | |
| 113 | /* The hash update function. */ |
| 114 | git_hash_update_fn update_fn; |
| 115 | |
| 116 | /* The hash finalization function. */ |
| 117 | git_hash_final_fn final_fn; |
| 118 | |
| 119 | /* The OID of the empty tree. */ |
| 120 | const struct object_id *empty_tree; |
| 121 | |
| 122 | /* The OID of the empty blob. */ |
| 123 | const struct object_id *empty_blob; |
| 124 | }; |
| 125 | extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS]; |
| 126 | |
brian m. carlson | 2f90b9d | 2018-10-22 02:43:32 +0000 | [diff] [blame] | 127 | /* |
| 128 | * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if |
| 129 | * the name doesn't match a known algorithm. |
| 130 | */ |
| 131 | int hash_algo_by_name(const char *name); |
| 132 | /* Identical, except based on the format ID. */ |
| 133 | int hash_algo_by_id(uint32_t format_id); |
brian m. carlson | 9539978 | 2019-02-19 00:05:17 +0000 | [diff] [blame] | 134 | /* Identical, except based on the length. */ |
| 135 | int hash_algo_by_length(int len); |
brian m. carlson | 2f90b9d | 2018-10-22 02:43:32 +0000 | [diff] [blame] | 136 | /* Identical, except for a pointer to struct git_hash_algo. */ |
| 137 | static inline int hash_algo_by_ptr(const struct git_hash_algo *p) |
| 138 | { |
| 139 | return p - hash_algos; |
| 140 | } |
| 141 | |
brian m. carlson | f18f816 | 2017-03-11 22:28:18 +0000 | [diff] [blame] | 142 | #endif |