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